Merge lane-vp9-inter: VP9 inter syntax next to tile-row byte-exactness
Browse files at this commit10 changed files +2715 -31
- crates/ec-vp9/src/decode.rs +569 -20
- crates/ec-vp9/src/header.rs +264 -5
- crates/ec-vp9/src/inter.rs +1316 -0
- crates/ec-vp9/src/lib.rs +33 -0
- crates/ec-vp9/src/modes.rs +31 -2
- crates/ec-vp9/src/tables/gen_tables.rs +64 -0
- crates/ec-vp9/src/tokens.rs +6 -4
- crates/ec-vp9/tests/ivf.rs +1 -0
- crates/ec-vp9/tests/scratch_interdump.rs +80 -0
- lanes/vp9inter.report.md +351 -0
crates/ec-vp9/src/decode.rsmodified+569 -20
| @@ -76,6 +76,44 @@ pub struct Decoder { | |||
| 76 | refs: [Option<Picture>; 8], | 76 | refs: [Option<Picture>; 8], |
| 77 | /// Frame scratch, live only inside `decode_keyframe`. | 77 | /// Frame scratch, live only inside `decode_keyframe`. |
| 78 | planes: Option<Planes>, | 78 | planes: Option<Planes>, |
| 79 | + /// The four stored frame contexts (spec 6.2 `frame_context_idx`). | ||
| 80 | + frame_ctxs: [FrameContext; 4], | ||
| 81 | + /// The previous frame's per-8x8 MV field and segment map | ||
| 82 | + /// (`prev_frame->mvs` / `last_frame_seg_map`). | ||
| 83 | + prev_mvs: Vec<crate::inter::MvRef>, | ||
| 84 | + prev_seg: Vec<u8>, | ||
| 85 | + last: LastFrameFacts, | ||
| 86 | + /// Live only while an inter frame is being parsed. | ||
| 87 | + iw: Option<InterWalk>, | ||
| 88 | + /// Frames parsed by `decode_syntax` (the dump's frame index). | ||
| 89 | + frames_seen: usize, | ||
| 90 | + /// Mode-info blocks parsed in the most recent inter frame (0 otherwise). | ||
| 91 | + syntax_blocks: usize, | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +/// What `use_prev_frame_mvs` (decodeframe.c:3023) tests about the last frame. | ||
| 95 | +#[derive(Clone, Copy, Default)] | ||
| 96 | +struct LastFrameFacts { | ||
| 97 | + width: u32, | ||
| 98 | + height: u32, | ||
| 99 | + intra_only: bool, | ||
| 100 | + show_frame: bool, | ||
| 101 | + key: bool, | ||
| 102 | +} | ||
| 103 | + | ||
| 104 | +/// State of one inter-syntax frame walk. | ||
| 105 | +struct InterWalk { | ||
| 106 | + ifs: crate::inter::InterFrameState, | ||
| 107 | + sign_bias: [bool; 4], | ||
| 108 | + use_prev: bool, | ||
| 109 | + seg_map: Vec<u8>, | ||
| 110 | + mvs: Vec<crate::inter::MvRef>, | ||
| 111 | + prev_seg: Vec<u8>, | ||
| 112 | + prev_mvs: Vec<crate::inter::MvRef>, | ||
| 113 | + tile_col_end: usize, | ||
| 114 | + frame_index: usize, | ||
| 115 | + /// Mode-info blocks parsed in this frame (the harness's count gate). | ||
| 116 | + blocks: usize, | ||
| 79 | } | 117 | } |
| 80 | 118 | ||
| 81 | impl Decoder { | 119 | impl Decoder { |
| @@ -86,9 +124,25 @@ impl Decoder { | |||
| 86 | ctx: FrameContext::new(true), | 124 | ctx: FrameContext::new(true), |
| 87 | refs: [const { None }; 8], | 125 | refs: [const { None }; 8], |
| 88 | planes: None, | 126 | planes: None, |
| 127 | + frame_ctxs: { | ||
| 128 | + let d = FrameContext::new(false); | ||
| 129 | + [d.clone(), d.clone(), d.clone(), d] | ||
| 130 | + }, | ||
| 131 | + prev_mvs: Vec::new(), | ||
| 132 | + prev_seg: Vec::new(), | ||
| 133 | + last: LastFrameFacts::default(), | ||
| 134 | + iw: None, | ||
| 135 | + frames_seen: 0, | ||
| 136 | + syntax_blocks: 0, | ||
| 89 | } | 137 | } |
| 90 | } | 138 | } |
| 91 | 139 | ||
| 140 | + /// Mode-info blocks the most recent inter frame parsed; `0` for a keyframe | ||
| 141 | + /// or before any frame. The syntax harness pins this number. | ||
| 142 | + pub fn last_frame_blocks(&self) -> usize { | ||
| 143 | + self.syntax_blocks | ||
| 144 | + } | ||
| 145 | + | ||
| 92 | /// Decode one frame (or one superframe chunk); returns the picture | 146 | /// Decode one frame (or one superframe chunk); returns the picture |
| 93 | /// when a frame is for display. Hidden frames still update state. | 147 | /// when a frame is for display. Hidden frames still update state. |
| 94 | pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> { | 148 | pub fn decode(&mut self, frame: &[u8]) -> Result<Option<Picture>> { |
| @@ -131,9 +185,29 @@ impl Decoder { | |||
| 131 | "this lane decodes keyframes only", | 185 | "this lane decodes keyframes only", |
| 132 | )); | 186 | )); |
| 133 | } | 187 | } |
| 134 | - // A keyframe resets the frame context (spec 7.2) before the | 188 | + self.decode_key_frame(&hdr, frame) |
| 135 | - // compressed header codes this frame's updates on top. | 189 | + } |
| 136 | - self.ctx = FrameContext::new(hdr.frame_type == FrameType::Key); | 190 | + |
| 191 | + /// Decode one key frame through to pixels and refresh every slot. | ||
| 192 | + fn decode_key_frame(&mut self, hdr: &FrameHeader, frame: &[u8]) -> Result<Option<Picture>> { | ||
| 193 | + let hdr = hdr.clone(); | ||
| 194 | + // setup_past_independence (spec 7.2): a key frame resets the frame | ||
| 195 | + // context, so every STORED context goes back to the inter defaults. | ||
| 196 | + // The keyframe's own mode info reads the const `vp9_kf_partition_probs` | ||
| 197 | + // (libvpx `get_partition_probs`), which our `new(true)` models on the | ||
| 198 | + // active copy only. | ||
| 199 | + let d = FrameContext::new(false); | ||
| 200 | + self.frame_ctxs = [d.clone(), d.clone(), d.clone(), d]; | ||
| 201 | + self.ctx = FrameContext::new(true); | ||
| 202 | + self.prev_mvs.clear(); | ||
| 203 | + self.prev_seg.clear(); | ||
| 204 | + self.last = LastFrameFacts { | ||
| 205 | + width: hdr.width, | ||
| 206 | + height: hdr.height, | ||
| 207 | + intra_only: hdr.intra_only, | ||
| 208 | + show_frame: hdr.show_frame, | ||
| 209 | + key: true, | ||
| 210 | + }; | ||
| 137 | // [scratch probe] EC_VP9_FORCE_UH / EC_VP9_FORCE_HSZ: override the | 211 | // [scratch probe] EC_VP9_FORCE_UH / EC_VP9_FORCE_HSZ: override the |
| 138 | // uncompressed-header size / header_size_in_bytes split for diagnosis. | 212 | // uncompressed-header size / header_size_in_bytes split for diagnosis. |
| 139 | let hdr = { | 213 | let hdr = { |
| @@ -147,13 +221,17 @@ impl Decoder { | |||
| 147 | h | 221 | h |
| 148 | }; | 222 | }; |
| 149 | let hdr_start = hdr.uncompressed_header_size as usize; | 223 | let hdr_start = hdr.uncompressed_header_size as usize; |
| 150 | - let tx_mode = read_compressed_header( | 224 | + let ch = read_compressed_header( |
| 151 | &frame[hdr_start..hdr_start + hdr.header_size_in_bytes as usize], | 225 | &frame[hdr_start..hdr_start + hdr.header_size_in_bytes as usize], |
| 152 | &mut self.ctx, | 226 | &mut self.ctx, |
| 153 | - hdr.quantization.lossless(), | 227 | + &hdr, |
| 154 | )?; | 228 | )?; |
| 155 | 229 | ||
| 156 | - let pic = self.decode_keyframe(&hdr, frame, tx_mode)?; | 230 | + let pic = self.decode_keyframe(&hdr, frame, ch.tx_mode)?; |
| 231 | + if hdr.refresh_frame_context { | ||
| 232 | + self.ctx.reset_inter_partition(); | ||
| 233 | + self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.clone(); | ||
| 234 | + } | ||
| 157 | // Reference refresh (spec 8.10): a keyframe updates all slots. | 235 | // Reference refresh (spec 8.10): a keyframe updates all slots. |
| 158 | for slot in self.refs.iter_mut() { | 236 | for slot in self.refs.iter_mut() { |
| 159 | *slot = Some(pic.clone()); | 237 | *slot = Some(pic.clone()); |
| @@ -161,6 +239,435 @@ impl Decoder { | |||
| 161 | Ok(if hdr.show_frame { Some(pic) } else { None }) | 239 | Ok(if hdr.show_frame { Some(pic) } else { None }) |
| 162 | } | 240 | } |
| 163 | 241 | ||
| 242 | + /// Parse one frame's syntax: keyframes decode through to pixels, inter | ||
| 243 | + /// frames are parsed (headers, mode info, MVs) with no reconstruction and | ||
| 244 | + /// no picture. `decode` still refuses inter frames. | ||
| 245 | + pub fn decode_syntax(&mut self, frame: &[u8]) -> Result<()> { | ||
| 246 | + for sub in superframe::split(frame)? { | ||
| 247 | + let index = self.frames_seen; | ||
| 248 | + self.frames_seen += 1; | ||
| 249 | + let hdr = self.parser.parse_frame(sub)?; | ||
| 250 | + if hdr.show_existing_frame { | ||
| 251 | + continue; | ||
| 252 | + } | ||
| 253 | + if hdr.profile != 0 { | ||
| 254 | + return Err(Error::unsupported( | ||
| 255 | + format!("vp9 profile {}", hdr.profile), | ||
| 256 | + "this lane decodes profile 0 only", | ||
| 257 | + )); | ||
| 258 | + } | ||
| 259 | + if hdr.frame_type == FrameType::Key { | ||
| 260 | + // Propagate: a corrupt keyframe leaves a half-updated frame | ||
| 261 | + // context, prev-frame MV field and reference slots behind, so | ||
| 262 | + // any inter-frame dump after it would be meaningless evidence. | ||
| 263 | + self.decode_key_frame(&hdr, sub)?; | ||
| 264 | + } else if hdr.intra_only { | ||
| 265 | + return Err(Error::unsupported( | ||
| 266 | + "vp9 intra-only", | ||
| 267 | + "the inter-syntax lane covers inter frames only", | ||
| 268 | + )); | ||
| 269 | + } else { | ||
| 270 | + self.decode_inter_syntax_frame(&hdr, sub, index)?; | ||
| 271 | + } | ||
| 272 | + } | ||
| 273 | + Ok(()) | ||
| 274 | + } | ||
| 275 | + | ||
| 276 | + /// Inter-frame syntax walk: compressed header, then every block's mode | ||
| 277 | + /// info and MVs. Tokens are decoded to keep the entropy stream aligned; | ||
| 278 | + /// nothing is predicted or reconstructed. | ||
| 279 | + fn decode_inter_syntax_frame(&mut self, hdr: &FrameHeader, frame: &[u8], index: usize) -> Result<()> { | ||
| 280 | + // Frame-context bookkeeping (spec 6.2 / libvpx `vp9_decode_frame`): | ||
| 281 | + // 3 resets every stored context, 2 resets the selected one; 1 means | ||
| 282 | + // "reset after this frame" and takes no action at frame start. | ||
| 283 | + if hdr.reset_frame_context == 3 { | ||
| 284 | + let d = FrameContext::new(false); | ||
| 285 | + self.frame_ctxs = [d.clone(), d.clone(), d.clone(), d]; | ||
| 286 | + } else if hdr.reset_frame_context == 2 { | ||
| 287 | + self.frame_ctxs[hdr.frame_context_idx as usize] = FrameContext::new(false); | ||
| 288 | + } | ||
| 289 | + self.ctx = self.frame_ctxs[hdr.frame_context_idx as usize].clone(); | ||
| 290 | + | ||
| 291 | + let hdr_start = hdr.uncompressed_header_size as usize; | ||
| 292 | + if crate::trace_enabled() { | ||
| 293 | + eprintln!("CHSTART frame={index}"); | ||
| 294 | + } | ||
| 295 | + let ch = read_compressed_header( | ||
| 296 | + &frame[hdr_start..hdr_start + hdr.header_size_in_bytes as usize], | ||
| 297 | + &mut self.ctx, | ||
| 298 | + hdr, | ||
| 299 | + )?; | ||
| 300 | + | ||
| 301 | + if crate::interdump_enabled() { | ||
| 302 | + let mut line = format!("PPFLAT frame={index}"); | ||
| 303 | + for row in self.ctx.partition.iter() { | ||
| 304 | + for v in row.iter() { | ||
| 305 | + line.push_str(&format!(" {v}")); | ||
| 306 | + } | ||
| 307 | + } | ||
| 308 | + eprintln!("{line}"); | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + // INTRA_FRAME's sign bias is 0 by definition; the coded three land at | ||
| 312 | + // LAST/GOLDEN/ALTREF. | ||
| 313 | + let sign_bias = [ | ||
| 314 | + false, | ||
| 315 | + hdr.ref_frame_sign_bias[0], | ||
| 316 | + hdr.ref_frame_sign_bias[1], | ||
| 317 | + hdr.ref_frame_sign_bias[2], | ||
| 318 | + ]; | ||
| 319 | + let mut ifs = crate::inter::InterFrameState { | ||
| 320 | + reference_mode: ch.reference_mode, | ||
| 321 | + ..Default::default() | ||
| 322 | + }; | ||
| 323 | + if ch.reference_mode != crate::inter::SINGLE_REFERENCE { | ||
| 324 | + crate::inter::setup_compound_reference_mode(&mut ifs, sign_bias); | ||
| 325 | + } | ||
| 326 | + let use_prev = !hdr.error_resilient_mode | ||
| 327 | + && hdr.width == self.last.width | ||
| 328 | + && hdr.height == self.last.height | ||
| 329 | + && !self.last.intra_only | ||
| 330 | + && self.last.show_frame | ||
| 331 | + && !self.last.key; | ||
| 332 | + | ||
| 333 | + let mi_cols = hdr.mi_cols() as usize; | ||
| 334 | + let mi_rows = hdr.mi_rows() as usize; | ||
| 335 | + let sb64_cols = mi_cols.div_ceil(SB_MI); | ||
| 336 | + let sb64_rows = mi_rows.div_ceil(SB_MI); | ||
| 337 | + let tile_cols = 1usize << hdr.tile_info.cols_log2; | ||
| 338 | + let tile_rows = 1usize << hdr.tile_info.rows_log2; | ||
| 339 | + | ||
| 340 | + self.iw = Some(InterWalk { | ||
| 341 | + ifs, | ||
| 342 | + sign_bias, | ||
| 343 | + use_prev, | ||
| 344 | + seg_map: vec![0; mi_cols * mi_rows], | ||
| 345 | + mvs: vec![crate::inter::MvRef::default(); mi_cols * mi_rows], | ||
| 346 | + prev_seg: std::mem::take(&mut self.prev_seg), | ||
| 347 | + prev_mvs: std::mem::take(&mut self.prev_mvs), | ||
| 348 | + tile_col_end: mi_cols, | ||
| 349 | + frame_index: index, | ||
| 350 | + blocks: 0, | ||
| 351 | + }); | ||
| 352 | + | ||
| 353 | + let mut data: &[u8] = &frame[hdr_start + hdr.header_size_in_bytes as usize..]; | ||
| 354 | + let mut tiles: Vec<&[u8]> = Vec::with_capacity(tile_cols * tile_rows); | ||
| 355 | + for tr in 0..tile_rows { | ||
| 356 | + for tc in 0..tile_cols { | ||
| 357 | + let last = tr == tile_rows - 1 && tc == tile_cols - 1; | ||
| 358 | + if last { | ||
| 359 | + tiles.push(data); | ||
| 360 | + } else { | ||
| 361 | + ensure(data.len() >= 4, "truncated tile size")?; | ||
| 362 | + let sz = u32::from_be_bytes(data[..4].try_into().unwrap()) as usize; | ||
| 363 | + data = &data[4..]; | ||
| 364 | + ensure(data.len() >= sz, "truncated tile data")?; | ||
| 365 | + tiles.push(&data[..sz]); | ||
| 366 | + data = &data[sz..]; | ||
| 367 | + } | ||
| 368 | + } | ||
| 369 | + } | ||
| 370 | + | ||
| 371 | + // The loop-filter grids are never written on this path (no | ||
| 372 | + // reconstruction), but the walk's signature carries them. | ||
| 373 | + let mut grids = LfGrids { | ||
| 374 | + level8: vec![0; mi_cols * mi_rows], | ||
| 375 | + blk: vec![(0u8, 0u32, 0u32); mi_cols * mi_rows], | ||
| 376 | + otx: vec![0; mi_cols * mi_rows], | ||
| 377 | + mi_cols, | ||
| 378 | + mi_rows, | ||
| 379 | + }; | ||
| 380 | + let mut ti = 0usize; | ||
| 381 | + for tr in 0..tile_rows { | ||
| 382 | + let row_lo = ((sb64_rows * tr) >> hdr.tile_info.rows_log2) * SB_MI; | ||
| 383 | + let row_hi = (((sb64_rows * (tr + 1)) >> hdr.tile_info.rows_log2) * SB_MI).min(mi_rows); | ||
| 384 | + for tc in 0..tile_cols { | ||
| 385 | + let col_lo = ((sb64_cols * tc) >> hdr.tile_info.cols_log2) * SB_MI; | ||
| 386 | + let col_hi = | ||
| 387 | + (((sb64_cols * (tc + 1)) >> hdr.tile_info.cols_log2) * SB_MI).min(mi_cols); | ||
| 388 | + if let Some(w) = self.iw.as_mut() { | ||
| 389 | + w.tile_col_end = col_hi; | ||
| 390 | + } | ||
| 391 | + if crate::trace_enabled() { | ||
| 392 | + eprintln!("TILE {} bytes frame={index}", tiles[ti].len()); | ||
| 393 | + } | ||
| 394 | + let mut r = crate::bool::BoolDecoder::new(tiles[ti])?; | ||
| 395 | + 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) { | ||
| 405 | + ectx.iter_mut().for_each(|p| p.left = [0; 32]); | ||
| 406 | + mi.left_seg = [0; 32]; | ||
| 407 | + for sb_col in (col_lo..col_hi).step_by(SB_MI) { | ||
| 408 | + self.decode_partition( | ||
| 409 | + &mut r, | ||
| 410 | + &mut mi, | ||
| 411 | + &mut ectx, | ||
| 412 | + &mut grids, | ||
| 413 | + &[[0u8; 2]; 8], | ||
| 414 | + hdr, | ||
| 415 | + sb_row, | ||
| 416 | + sb_col, | ||
| 417 | + 4, | ||
| 418 | + col_lo, | ||
| 419 | + ch.tx_mode, | ||
| 420 | + )?; | ||
| 421 | + } | ||
| 422 | + } | ||
| 423 | + ensure(r.overreads() == 0, "tile bool decoder desync")?; | ||
| 424 | + } | ||
| 425 | + } | ||
| 426 | + | ||
| 427 | + // The walk state is consumed below, so the block count the harness pins | ||
| 428 | + // has to be captured first. | ||
| 429 | + self.syntax_blocks = self.iw.as_ref().map_or(0, |w| w.blocks); | ||
| 430 | + let w = self.iw.take().expect("inter walk state"); | ||
| 431 | + self.prev_mvs = w.mvs; | ||
| 432 | + self.prev_seg = w.seg_map; | ||
| 433 | + self.last = LastFrameFacts { | ||
| 434 | + width: hdr.width, | ||
| 435 | + height: hdr.height, | ||
| 436 | + intra_only: hdr.intra_only, | ||
| 437 | + show_frame: hdr.show_frame, | ||
| 438 | + key: false, | ||
| 439 | + }; | ||
| 440 | + if hdr.refresh_frame_context { | ||
| 441 | + self.frame_ctxs[hdr.frame_context_idx as usize] = self.ctx.clone(); | ||
| 442 | + } | ||
| 443 | + if crate::interdump_enabled() { | ||
| 444 | + eprintln!( | ||
| 445 | + "CHSUM frame={index} tx_mode={} ref_mode={} allow_hp={} interp={} tile_rows={} tile_cols={} pp12={:?} pp0={:?} pp13={:?}", | ||
| 446 | + ch.tx_mode, | ||
| 447 | + ch.reference_mode, | ||
| 448 | + hdr.allow_high_precision_mv, | ||
| 449 | + hdr.interpolation_filter as u8, | ||
| 450 | + hdr.tile_info.rows_log2, | ||
| 451 | + hdr.tile_info.cols_log2, | ||
| 452 | + self.ctx.partition[12], | ||
| 453 | + self.ctx.partition[0], | ||
| 454 | + self.ctx.partition[13] | ||
| 455 | + ); | ||
| 456 | + } | ||
| 457 | + Ok(()) | ||
| 458 | + } | ||
| 459 | + | ||
| 460 | + /// One inter block's syntax (`read_inter_frame_mode_info` + the MV field | ||
| 461 | + /// `vp9_read_mode_info` stores for the next frame). | ||
| 462 | + #[allow(clippy::too_many_arguments)] | ||
| 463 | + fn read_inter_block_info( | ||
| 464 | + &mut self, | ||
| 465 | + r: &mut crate::bool::BoolDecoder, | ||
| 466 | + mi: &MiState, | ||
| 467 | + hdr: &FrameHeader, | ||
| 468 | + row: usize, | ||
| 469 | + col: usize, | ||
| 470 | + sb_type: usize, | ||
| 471 | + x_mis: usize, | ||
| 472 | + y_mis: usize, | ||
| 473 | + tile_col_start: usize, | ||
| 474 | + tx_mode: u8, | ||
| 475 | + ) -> Result<MiInfo> { | ||
| 476 | + // BLOCK_4X4/4X8/8X4 are the SPLIT/VERT/HORZ partitions of an 8x8. | ||
| 477 | + let partition = match sb_type { | ||
| 478 | + 0 => 3u8, | ||
| 479 | + 1 => 2u8, | ||
| 480 | + 2 => 1u8, | ||
| 481 | + _ => 0u8, | ||
| 482 | + }; | ||
| 483 | + let w = self.iw.as_mut().expect("inter walk state"); | ||
| 484 | + let prev_seg = std::mem::take(&mut w.prev_seg); | ||
| 485 | + let prev_mvs = std::mem::take(&mut w.prev_mvs); | ||
| 486 | + let ctx = crate::inter::InterBlockCtx { | ||
| 487 | + seg: &hdr.segmentation, | ||
| 488 | + ifs: w.ifs, | ||
| 489 | + sign_bias: w.sign_bias, | ||
| 490 | + allow_hp: hdr.allow_high_precision_mv, | ||
| 491 | + frame_interp: hdr.interpolation_filter as u8, | ||
| 492 | + tx_mode, | ||
| 493 | + mi_rows: mi.mi_rows, | ||
| 494 | + mi_cols: mi.mi_cols, | ||
| 495 | + tile_col_start, | ||
| 496 | + tile_col_end: w.tile_col_end, | ||
| 497 | + last_seg: (!prev_seg.is_empty()).then_some(&prev_seg[..]), | ||
| 498 | + prev_mvs: (w.use_prev && !prev_mvs.is_empty()).then_some(&prev_mvs[..]), | ||
| 499 | + }; | ||
| 500 | + let info = crate::inter::read_inter_frame_mode_info( | ||
| 501 | + r, | ||
| 502 | + mi, | ||
| 503 | + &ctx, | ||
| 504 | + &self.ctx, | ||
| 505 | + row, | ||
| 506 | + col, | ||
| 507 | + sb_type, | ||
| 508 | + x_mis, | ||
| 509 | + y_mis, | ||
| 510 | + partition, | ||
| 511 | + &mut w.seg_map, | ||
| 512 | + ); | ||
| 513 | + if let Ok(info) = &info { | ||
| 514 | + w.blocks += 1; | ||
| 515 | + for dy in 0..y_mis { | ||
| 516 | + for dx in 0..x_mis { | ||
| 517 | + let i = (row + dy) * mi.mi_cols + col + dx; | ||
| 518 | + w.mvs[i] = crate::inter::MvRef { | ||
| 519 | + ref_frame: info.ref_frame, | ||
| 520 | + mv: info.mv, | ||
| 521 | + }; | ||
| 522 | + } | ||
| 523 | + } | ||
| 524 | + if crate::interdump_enabled() { | ||
| 525 | + eprintln!( | ||
| 526 | + "MODE {} {} {} {} {} {} {} {} {} tpos=0 ref0={} ref1={} mvrow={} mvcol={} if={}", | ||
| 527 | + w.frame_index, | ||
| 528 | + row, | ||
| 529 | + col, | ||
| 530 | + sb_type, | ||
| 531 | + info.tx_size, | ||
| 532 | + info.mode, | ||
| 533 | + info.uv_mode, | ||
| 534 | + u8::from(info.skip), | ||
| 535 | + info.segment, | ||
| 536 | + info.ref_frame[0], | ||
| 537 | + info.ref_frame[1], | ||
| 538 | + info.mv[0].0, | ||
| 539 | + info.mv[0].1, | ||
| 540 | + info.interp_filter | ||
| 541 | + ); | ||
| 542 | + } | ||
| 543 | + } | ||
| 544 | + w.prev_seg = prev_seg; | ||
| 545 | + w.prev_mvs = prev_mvs; | ||
| 546 | + info | ||
| 547 | + } | ||
| 548 | + | ||
| 549 | + /// Tokens for one inter block: same entropy walk as the keyframe path, | ||
| 550 | + /// but no prediction and no residual add. `is_inter` selects the | ||
| 551 | + /// coefficient-probability ref axis. | ||
| 552 | + #[allow(clippy::too_many_arguments)] | ||
| 553 | + fn decode_inter_block_tokens( | ||
| 554 | + &mut self, | ||
| 555 | + r: &mut crate::bool::BoolDecoder, | ||
| 556 | + mi: &MiState, | ||
| 557 | + ectx: &mut [PlaneContexts; 3], | ||
| 558 | + hdr: &FrameHeader, | ||
| 559 | + info: &MiInfo, | ||
| 560 | + row: usize, | ||
| 561 | + col: usize, | ||
| 562 | + sb_type: usize, | ||
| 563 | + bw: usize, | ||
| 564 | + bh: usize, | ||
| 565 | + ) -> Result<()> { | ||
| 566 | + let n4w = if sb_type < 3 { | ||
| 567 | + [2, 1, 1] | ||
| 568 | + } else { | ||
| 569 | + [bw * 2, bw, bw] | ||
| 570 | + }; | ||
| 571 | + let n4h = if sb_type < 3 { | ||
| 572 | + [2, 1, 1] | ||
| 573 | + } else { | ||
| 574 | + [bh * 2, bh, bh] | ||
| 575 | + }; | ||
| 576 | + if info.skip { | ||
| 577 | + // dec_reset_skip_context | ||
| 578 | + for plane in 0..3usize { | ||
| 579 | + let s = usize::from(plane != 0); | ||
| 580 | + let offx = (col << 1) >> s; | ||
| 581 | + let offy = (row << 1) >> s; | ||
| 582 | + for i in 0..n4w[plane] { | ||
| 583 | + ectx[plane].above[offx + i] = 0; | ||
| 584 | + } | ||
| 585 | + let lrows = 32usize >> s; | ||
| 586 | + for i in 0..n4h[plane].min(lrows) { | ||
| 587 | + ectx[plane].left[(offy + i) % lrows] = 0; | ||
| 588 | + } | ||
| 589 | + } | ||
| 590 | + return Ok(()); | ||
| 591 | + } | ||
| 592 | + let mb_to_right = ((mi.mi_cols as isize - bw as isize - col as isize) * 64) as i32; | ||
| 593 | + let mb_to_bottom = ((mi.mi_rows as isize - bh as isize - row as isize) * 64) as i32; | ||
| 594 | + let lossless = hdr.quantization.lossless(); | ||
| 595 | + for plane in 0..3usize { | ||
| 596 | + let s = usize::from(plane != 0); | ||
| 597 | + let tx_size = if plane == 0 { | ||
| 598 | + info.tx_size | ||
| 599 | + } else { | ||
| 600 | + get_uv_tx_size(info.tx_size, sb_type) | ||
| 601 | + }; | ||
| 602 | + let step = 1usize << tx_size; | ||
| 603 | + let max_blocks_wide = | ||
| 604 | + (n4w[plane] as i32 + (mb_to_right.min(0) >> (5 + s))).max(0) as usize; | ||
| 605 | + let max_blocks_high = | ||
| 606 | + (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) { | ||
| 608 | + for bcol in (0..max_blocks_wide).step_by(step) { | ||
| 609 | + let tx_type = if plane != 0 || lossless || info.is_inter { | ||
| 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, | ||
| 630 | + ptype, | ||
| 631 | + tx_size, | ||
| 632 | + &dequant, | ||
| 633 | + ctx_in, | ||
| 634 | + scan, | ||
| 635 | + nb, | ||
| 636 | + &self.ctx.coef[tx_size], | ||
| 637 | + info.is_inter, | ||
| 638 | + (0, 0), | ||
| 639 | + plane, | ||
| 640 | + ); | ||
| 641 | + // vp9_decode_block_tokens context write-back. | ||
| 642 | + let n4w_s = bw * 2 >> s; | ||
| 643 | + let n4h_s = bh * 2 >> s; | ||
| 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 | + } | ||
| 665 | + } | ||
| 666 | + } | ||
| 667 | + } | ||
| 668 | + Ok(()) | ||
| 669 | + } | ||
| 670 | + | ||
| 164 | fn decode_keyframe(&mut self, hdr: &FrameHeader, frame: &[u8], tx_mode: u8) -> Result<Picture> { | 671 | fn decode_keyframe(&mut self, hdr: &FrameHeader, frame: &[u8], tx_mode: u8) -> Result<Picture> { |
| 165 | let width = hdr.width as usize; | 672 | let width = hdr.width as usize; |
| 166 | let height = hdr.height as usize; | 673 | let height = hdr.height as usize; |
| @@ -556,20 +1063,61 @@ impl Decoder { | |||
| 556 | let x_mis = bw.min(mi.mi_cols - col); | 1063 | let x_mis = bw.min(mi.mi_cols - col); |
| 557 | let y_mis = bh.min(mi.mi_rows - row); | 1064 | let y_mis = bh.min(mi.mi_rows - row); |
| 558 | 1065 | ||
| 559 | - let info = read_intra_frame_mode_info( | 1066 | + let info = if hdr.frame_type == FrameType::Key { |
| 560 | - r, | 1067 | + read_intra_frame_mode_info( |
| 561 | - mi, | 1068 | + r, |
| 562 | - &hdr.segmentation, | 1069 | + mi, |
| 563 | - &self.ctx.partition, | 1070 | + &hdr.segmentation, |
| 564 | - row, | 1071 | + &self.ctx.partition, |
| 565 | - col, | 1072 | + row, |
| 566 | - sb_type, | 1073 | + col, |
| 567 | - x_mis, | 1074 | + sb_type, |
| 568 | - y_mis, | 1075 | + x_mis, |
| 569 | - tile_col_start, | 1076 | + y_mis, |
| 570 | - tx_mode, | 1077 | + tile_col_start, |
| 571 | - &self.ctx, | 1078 | + tx_mode, |
| 572 | - )?; | 1079 | + &self.ctx, |
| 1080 | + )? | ||
| 1081 | + } else { | ||
| 1082 | + self.read_inter_block_info( | ||
| 1083 | + r, | ||
| 1084 | + mi, | ||
| 1085 | + hdr, | ||
| 1086 | + row, | ||
| 1087 | + col, | ||
| 1088 | + sb_type, | ||
| 1089 | + x_mis, | ||
| 1090 | + y_mis, | ||
| 1091 | + tile_col_start, | ||
| 1092 | + tx_mode, | ||
| 1093 | + )? | ||
| 1094 | + }; | ||
| 1095 | + if hdr.frame_type != FrameType::Key { | ||
| 1096 | + // Inter syntax only: tokens keep the entropy stream aligned, the | ||
| 1097 | + // block lands in the mode grid, and no pixel is touched. | ||
| 1098 | + self.decode_inter_block_tokens(r, mi, ectx, hdr, &info, row, col, sb_type, bw, bh)?; | ||
| 1099 | + for dy in 0..y_mis { | ||
| 1100 | + for dx in 0..x_mis { | ||
| 1101 | + if crate::mvdbg2_enabled() | ||
| 1102 | + && row + dy == 7 | ||
| 1103 | + && col + dx >= 30 | ||
| 1104 | + && col + dx <= 40 | ||
| 1105 | + { | ||
| 1106 | + eprintln!( | ||
| 1107 | + "GWR cell=({} {}) idx={} from=({row} {col}) dy={dy} dx={dx} sb={sb_type} bw={bw} bh={bh} x_mis={x_mis} y_mis={y_mis} mv={:?} ref0={} mi_cols={}", | ||
| 1108 | + row + dy, | ||
| 1109 | + col + dx, | ||
| 1110 | + (row + dy) * mi.mi_cols + col + dx, | ||
| 1111 | + info.mv, | ||
| 1112 | + info.ref_frame[0], | ||
| 1113 | + mi.mi_cols | ||
| 1114 | + ); | ||
| 1115 | + } | ||
| 1116 | + mi.grid[(row + dy) * mi.mi_cols + col + dx] = Some(info.clone()); | ||
| 1117 | + } | ||
| 1118 | + } | ||
| 1119 | + return Ok(()); | ||
| 1120 | + } | ||
| 573 | 1121 | ||
| 574 | // Per-plane 4x4 counts. Sub-8x8 shapes still occupy the WHOLE 8x8 | 1122 | // Per-plane 4x4 counts. Sub-8x8 shapes still occupy the WHOLE 8x8 |
| 575 | // mi area (libvpx set_plane_n4 with bw=bh=1): luma walks 2x2 4x4 | 1123 | // mi area (libvpx set_plane_n4 with bw=bh=1): luma walks 2x2 4x4 |
| @@ -802,6 +1350,7 @@ impl Decoder { | |||
| 802 | scan, | 1350 | scan, |
| 803 | nb, | 1351 | nb, |
| 804 | &self.ctx.coef[tx_size], | 1352 | &self.ctx.coef[tx_size], |
| 1353 | + false, | ||
| 805 | (x0, y0), | 1354 | (x0, y0), |
| 806 | plane, | 1355 | plane, |
| 807 | ); | 1356 | ); |
crates/ec-vp9/src/header.rsmodified+264 -5
| @@ -4,7 +4,9 @@ | |||
| 4 | //! differential probability-update machinery (libvpx `vp9_dsubexp.c`). | 4 | //! differential probability-update machinery (libvpx `vp9_dsubexp.c`). |
| 5 | 5 | ||
| 6 | use crate::bool::BoolDecoder; | 6 | use crate::bool::BoolDecoder; |
| 7 | +use crate::inter::{COMPOUND_REFERENCE, REFERENCE_MODE_SELECT, SINGLE_REFERENCE}; | ||
| 7 | use crate::tables::*; | 8 | use crate::tables::*; |
| 9 | +use ec_vp9_syntax::FrameHeader; | ||
| 8 | 10 | ||
| 9 | /// All bool-coded probabilities a keyframe reads. | 11 | /// All bool-coded probabilities a keyframe reads. |
| 10 | #[derive(Clone)] | 12 | #[derive(Clone)] |
| @@ -15,6 +17,36 @@ pub(crate) struct FrameContext { | |||
| 15 | pub tx_p16x16: [[u8; 2]; 2], | 17 | pub tx_p16x16: [[u8; 2]; 2], |
| 16 | pub tx_p32x32: [[u8; 3]; 2], | 18 | pub tx_p32x32: [[u8; 3]; 2], |
| 17 | pub skip: [u8; 3], | 19 | pub skip: [u8; 3], |
| 20 | + /// Inter-frame tables (all defaults until a compressed header updates | ||
| 21 | + /// them): `inter_mode_probs`, `switchable_interp_prob`, `intra_inter_prob`, | ||
| 22 | + /// `comp_inter_prob`, `single_ref_prob`, `comp_ref_prob`, `y_mode_prob`, | ||
| 23 | + /// `uv_mode_prob`, and the MV probs. | ||
| 24 | + pub inter_mode: [[u8; 3]; 7], | ||
| 25 | + pub switchable_interp: [[u8; 2]; 4], | ||
| 26 | + pub intra_inter: [u8; 4], | ||
| 27 | + pub comp_inter: [u8; 5], | ||
| 28 | + pub single_ref: [[u8; 2]; 5], | ||
| 29 | + pub comp_ref: [u8; 5], | ||
| 30 | + pub y_mode: [[u8; 9]; 4], | ||
| 31 | + pub uv_mode: [[u8; 9]; 10], | ||
| 32 | + pub nmv_joints: [u8; 3], | ||
| 33 | + pub nmv_sign: [u8; 2], | ||
| 34 | + pub nmv_classes: [[u8; 10]; 2], | ||
| 35 | + pub nmv_class0: [[u8; 1]; 2], | ||
| 36 | + pub nmv_bits: [[u8; 10]; 2], | ||
| 37 | + pub nmv_class0_fp: [[[u8; 3]; 2]; 2], | ||
| 38 | + pub nmv_fp: [[u8; 3]; 2], | ||
| 39 | + pub nmv_class0_hp: [u8; 2], | ||
| 40 | + pub nmv_hp: [u8; 2], | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +/// Rows from a flat C initializer. | ||
| 44 | +fn rows<const R: usize, const C: usize>(flat: &[u8]) -> [[u8; C]; R] { | ||
| 45 | + let mut out = [[0u8; C]; R]; | ||
| 46 | + for (i, row) in out.iter_mut().enumerate() { | ||
| 47 | + row.copy_from_slice(&flat[i * C..i * C + C]); | ||
| 48 | + } | ||
| 49 | + out | ||
| 18 | } | 50 | } |
| 19 | 51 | ||
| 20 | impl FrameContext { | 52 | impl FrameContext { |
| @@ -51,10 +83,49 @@ impl FrameContext { | |||
| 51 | [TX_P32X32_PROB[3], TX_P32X32_PROB[4], TX_P32X32_PROB[5]], | 83 | [TX_P32X32_PROB[3], TX_P32X32_PROB[4], TX_P32X32_PROB[5]], |
| 52 | ], | 84 | ], |
| 53 | skip: DEFAULT_SKIP_PROBS, | 85 | skip: DEFAULT_SKIP_PROBS, |
| 86 | + // `vp9_init_frame_context` / `vp9_init_mv_probs` defaults. Key | ||
| 87 | + // frames do not use these; inter frames start from them unless a | ||
| 88 | + // stored context (or a compressed header) says otherwise. | ||
| 89 | + inter_mode: rows(&DEFAULT_INTER_MODE_PROBS), | ||
| 90 | + switchable_interp: rows(&DEFAULT_SWITCHABLE_INTERP_PROBS), | ||
| 91 | + intra_inter: DEFAULT_INTRA_INTER_PROBS, | ||
| 92 | + comp_inter: DEFAULT_COMP_INTER_PROBS, | ||
| 93 | + single_ref: rows(&DEFAULT_SINGLE_REF_PROBS), | ||
| 94 | + comp_ref: DEFAULT_COMP_REF_PROBS, | ||
| 95 | + y_mode: rows(&DEFAULT_Y_MODE_PROBS), | ||
| 96 | + uv_mode: rows(&DEFAULT_IF_UV_PROBS), | ||
| 97 | + nmv_joints: DEFAULT_NMVC_JOINTS, | ||
| 98 | + nmv_sign: DEFAULT_NMVC_SIGN, | ||
| 99 | + nmv_classes: rows(&DEFAULT_NMVC_CLASSES), | ||
| 100 | + nmv_class0: [[DEFAULT_NMVC_CLASS0[0]], [DEFAULT_NMVC_CLASS0[1]]], | ||
| 101 | + nmv_bits: rows(&DEFAULT_NMVC_BITS), | ||
| 102 | + nmv_class0_fp: [ | ||
| 103 | + [ | ||
| 104 | + [DEFAULT_NMVC_CLASS0_FP[0], DEFAULT_NMVC_CLASS0_FP[1], DEFAULT_NMVC_CLASS0_FP[2]], | ||
| 105 | + [DEFAULT_NMVC_CLASS0_FP[3], DEFAULT_NMVC_CLASS0_FP[4], DEFAULT_NMVC_CLASS0_FP[5]], | ||
| 106 | + ], | ||
| 107 | + [ | ||
| 108 | + [DEFAULT_NMVC_CLASS0_FP[6], DEFAULT_NMVC_CLASS0_FP[7], DEFAULT_NMVC_CLASS0_FP[8]], | ||
| 109 | + [DEFAULT_NMVC_CLASS0_FP[9], DEFAULT_NMVC_CLASS0_FP[10], DEFAULT_NMVC_CLASS0_FP[11]], | ||
| 110 | + ], | ||
| 111 | + ], | ||
| 112 | + nmv_fp: rows(&DEFAULT_NMVC_FP), | ||
| 113 | + nmv_class0_hp: DEFAULT_NMVC_CLASS0_HP, | ||
| 114 | + nmv_hp: DEFAULT_NMVC_HP, | ||
| 54 | } | 115 | } |
| 55 | } | 116 | } |
| 56 | } | 117 | } |
| 57 | 118 | ||
| 119 | +impl FrameContext { | ||
| 120 | + /// `vp9_kf_partition_probs` are NOT a `FRAME_CONTEXT` field in libvpx | ||
| 121 | + /// (`get_partition_probs` picks the const table for intra-only frames); | ||
| 122 | + /// a context that a keyframe stored must therefore go back to the inter | ||
| 123 | + /// defaults before the next frame reads it. | ||
| 124 | + pub(crate) fn reset_inter_partition(&mut self) { | ||
| 125 | + self.partition = rows(&DEFAULT_PARTITION_PROBS); | ||
| 126 | + } | ||
| 127 | +} | ||
| 128 | + | ||
| 58 | /// `inv_recenter_nonneg` (vp9_dsubexp.c). | 129 | /// `inv_recenter_nonneg` (vp9_dsubexp.c). |
| 59 | fn inv_recenter_nonneg(v: i32, m: i32) -> i32 { | 130 | fn inv_recenter_nonneg(v: i32, m: i32) -> i32 { |
| 60 | if v > 2 * m { | 131 | if v > 2 * m { |
| @@ -127,6 +198,20 @@ fn diff_update_prob(r: &mut BoolDecoder, p: &mut u8) { | |||
| 127 | } | 198 | } |
| 128 | } | 199 | } |
| 129 | 200 | ||
| 201 | +/// `MV_UPDATE_PROB` (`vp9_entropymv.h`): the flag prob of `update_mv_probs`. | ||
| 202 | +const MV_UPDATE_PROB: u8 = 252; | ||
| 203 | + | ||
| 204 | +/// `update_mv_probs` (vp9_decodeframe.c:136). NOT a `vp9_diff_update_prob`: | ||
| 205 | +/// a flag at `MV_UPDATE_PROB`, then seven LITERAL bits, and the new prob is | ||
| 206 | +/// `(literal << 1) | 1` — no `decode_term_subexp`, no `inv_remap_prob`. | ||
| 207 | +fn update_mv_probs(r: &mut BoolDecoder, p: &mut [u8]) { | ||
| 208 | + for v in p.iter_mut() { | ||
| 209 | + if r.read_bool(MV_UPDATE_PROB) { | ||
| 210 | + *v = ((r.read_literal(7) << 1) | 1) as u8; | ||
| 211 | + } | ||
| 212 | + } | ||
| 213 | +} | ||
| 214 | + | ||
| 130 | /// `read_coef_probs_common` (decodeframe.c:1314): one flag per tx size | 215 | /// `read_coef_probs_common` (decodeframe.c:1314): one flag per tx size |
| 131 | /// covers all plane/ref/band/context updates of the first three (base) | 216 | /// covers all plane/ref/band/context updates of the first three (base) |
| 132 | /// probs per slot. | 217 | /// probs per slot. |
| @@ -159,8 +244,17 @@ fn read_tx_mode(r: &mut BoolDecoder) -> u8 { | |||
| 159 | m | 244 | m |
| 160 | } | 245 | } |
| 161 | 246 | ||
| 162 | -/// `read_compressed_header` for intra frames (the inter branch is dead | 247 | +/// What `read_compressed_header` decides for the frame. |
| 163 | -/// in this lane): tx mode, tx probs, coef probs, skip probs. | 248 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 249 | +pub(crate) struct CompressedHeader { | ||
| 250 | + pub tx_mode: u8, | ||
| 251 | + pub reference_mode: u8, | ||
| 252 | +} | ||
| 253 | + | ||
| 254 | +/// `read_compressed_header` (libvpx `vp9_decodeframe.c:2889`): tx mode, tx | ||
| 255 | +/// probs, coef probs, skip probs, then — for inter frames only — the inter | ||
| 256 | +/// mode, switchable-interp, intra/inter, reference-mode, y-mode, partition | ||
| 257 | +/// and MV probability updates. | ||
| 164 | /// | 258 | /// |
| 165 | /// Lossless forces `ONLY_4X4` and consumes no tx-mode bits — decodeframe.c | 259 | /// Lossless forces `ONLY_4X4` and consumes no tx-mode bits — decodeframe.c |
| 166 | /// `read_compressed_header`: `cm->tx_mode = xd->lossless ? ONLY_4X4 : | 260 | /// `read_compressed_header`: `cm->tx_mode = xd->lossless ? ONLY_4X4 : |
| @@ -168,14 +262,27 @@ fn read_tx_mode(r: &mut BoolDecoder) -> u8 { | |||
| 168 | pub(crate) fn read_compressed_header( | 262 | pub(crate) fn read_compressed_header( |
| 169 | data: &[u8], | 263 | data: &[u8], |
| 170 | ctx: &mut FrameContext, | 264 | ctx: &mut FrameContext, |
| 171 | - lossless: bool, | 265 | + hdr: &FrameHeader, |
| 172 | -) -> crate::Result<u8> { | 266 | +) -> crate::Result<CompressedHeader> { |
| 267 | + if crate::interdump_enabled() && !hdr.frame_is_intra { | ||
| 268 | + let mut line = String::from("PPFLAT_PRE"); | ||
| 269 | + for row in ctx.partition.iter() { | ||
| 270 | + for v in row.iter() { | ||
| 271 | + line.push_str(&format!(" {v}")); | ||
| 272 | + } | ||
| 273 | + } | ||
| 274 | + eprintln!("{line}"); | ||
| 275 | + } | ||
| 276 | + let lossless = hdr.quantization.lossless(); | ||
| 173 | let mut r = BoolDecoder::new(data)?; | 277 | let mut r = BoolDecoder::new(data)?; |
| 174 | let tx_mode = if lossless { | 278 | let tx_mode = if lossless { |
| 175 | ONLY_4X4 | 279 | ONLY_4X4 |
| 176 | } else { | 280 | } else { |
| 177 | read_tx_mode(&mut r) | 281 | read_tx_mode(&mut r) |
| 178 | }; | 282 | }; |
| 283 | + if crate::trace_enabled() { | ||
| 284 | + eprintln!("SECT txmode"); | ||
| 285 | + } | ||
| 179 | if tx_mode == TX_MODE_SELECT { | 286 | if tx_mode == TX_MODE_SELECT { |
| 180 | for row in 0..2 { | 287 | for row in 0..2 { |
| 181 | for j in 0..1 { | 288 | for j in 0..1 { |
| @@ -193,10 +300,159 @@ pub(crate) fn read_compressed_header( | |||
| 193 | } | 300 | } |
| 194 | } | 301 | } |
| 195 | } | 302 | } |
| 303 | + if crate::trace_enabled() { | ||
| 304 | + eprintln!("SECT txprobs"); | ||
| 305 | + } | ||
| 196 | read_coef_probs(&mut r, ctx, tx_mode); | 306 | read_coef_probs(&mut r, ctx, tx_mode); |
| 307 | + if crate::trace_enabled() { | ||
| 308 | + eprintln!("SECT coef"); | ||
| 309 | + } | ||
| 197 | for p in &mut ctx.skip { | 310 | for p in &mut ctx.skip { |
| 198 | diff_update_prob(&mut r, p); | 311 | diff_update_prob(&mut r, p); |
| 199 | } | 312 | } |
| 313 | + let mut reference_mode = SINGLE_REFERENCE; | ||
| 314 | + if crate::trace_enabled() { | ||
| 315 | + eprintln!("SECT skip"); | ||
| 316 | + } | ||
| 317 | + if !hdr.frame_is_intra { | ||
| 318 | + if crate::trace_enabled() { | ||
| 319 | + eprintln!("SECT intermode"); | ||
| 320 | + } | ||
| 321 | + for i in 0..7 { | ||
| 322 | + for j in 0..3 { | ||
| 323 | + diff_update_prob(&mut r, &mut ctx.inter_mode[i][j]); | ||
| 324 | + } | ||
| 325 | + } | ||
| 326 | + if crate::trace_enabled() { | ||
| 327 | + eprintln!("SECT switchable"); | ||
| 328 | + } | ||
| 329 | + if hdr.interpolation_filter as u8 == crate::inter::SWITCHABLE { | ||
| 330 | + for j in 0..4 { | ||
| 331 | + for i in 0..2 { | ||
| 332 | + diff_update_prob(&mut r, &mut ctx.switchable_interp[j][i]); | ||
| 333 | + } | ||
| 334 | + } | ||
| 335 | + } | ||
| 336 | + if crate::trace_enabled() { | ||
| 337 | + eprintln!("SECT intrainter"); | ||
| 338 | + } | ||
| 339 | + for p in &mut ctx.intra_inter { | ||
| 340 | + diff_update_prob(&mut r, p); | ||
| 341 | + } | ||
| 342 | + // read_frame_reference_mode: `vp9_compound_reference_allowed` gates | ||
| 343 | + // the two bits (bias[GOLDEN]/bias[ALTREF] vs bias[LAST]). | ||
| 344 | + if crate::trace_enabled() { | ||
| 345 | + eprintln!("SECT refmode"); | ||
| 346 | + } | ||
| 347 | + let allowed = hdr.ref_frame_sign_bias[1] != hdr.ref_frame_sign_bias[0] | ||
| 348 | + || hdr.ref_frame_sign_bias[2] != hdr.ref_frame_sign_bias[0]; | ||
| 349 | + reference_mode = if allowed { | ||
| 350 | + if r.read_bool(128) { | ||
| 351 | + if r.read_bool(128) { | ||
| 352 | + REFERENCE_MODE_SELECT | ||
| 353 | + } else { | ||
| 354 | + COMPOUND_REFERENCE | ||
| 355 | + } | ||
| 356 | + } else { | ||
| 357 | + SINGLE_REFERENCE | ||
| 358 | + } | ||
| 359 | + } else { | ||
| 360 | + SINGLE_REFERENCE | ||
| 361 | + }; | ||
| 362 | + // read_frame_reference_mode_probs. | ||
| 363 | + if crate::trace_enabled() { | ||
| 364 | + eprintln!("SECT refprobs"); | ||
| 365 | + } | ||
| 366 | + if reference_mode == REFERENCE_MODE_SELECT { | ||
| 367 | + for p in &mut ctx.comp_inter { | ||
| 368 | + diff_update_prob(&mut r, p); | ||
| 369 | + } | ||
| 370 | + } | ||
| 371 | + if reference_mode != COMPOUND_REFERENCE { | ||
| 372 | + for i in 0..5 { | ||
| 373 | + for j in 0..2 { | ||
| 374 | + diff_update_prob(&mut r, &mut ctx.single_ref[i][j]); | ||
| 375 | + } | ||
| 376 | + } | ||
| 377 | + } | ||
| 378 | + if reference_mode != SINGLE_REFERENCE { | ||
| 379 | + for p in &mut ctx.comp_ref { | ||
| 380 | + diff_update_prob(&mut r, p); | ||
| 381 | + } | ||
| 382 | + } | ||
| 383 | + if crate::trace_enabled() { | ||
| 384 | + eprintln!("SECT ymode"); | ||
| 385 | + } | ||
| 386 | + for j in 0..4 { | ||
| 387 | + for i in 0..9 { | ||
| 388 | + diff_update_prob(&mut r, &mut ctx.y_mode[j][i]); | ||
| 389 | + } | ||
| 390 | + } | ||
| 391 | + if crate::trace_enabled() { | ||
| 392 | + eprintln!("SECT partition"); | ||
| 393 | + } | ||
| 394 | + for j in 0..16 { | ||
| 395 | + for i in 0..3 { | ||
| 396 | + let before = ctx.partition[j][i]; | ||
| 397 | + diff_update_prob(&mut r, &mut ctx.partition[j][i]); | ||
| 398 | + if crate::interdump_enabled() { | ||
| 399 | + eprintln!("PUPD {} {} {} {}", j, i, before, ctx.partition[j][i]); | ||
| 400 | + } | ||
| 401 | + } | ||
| 402 | + } | ||
| 403 | + // read_mv_probs (vp9_decodeframe.c:76): joints, then per component | ||
| 404 | + // sign/classes/class0/bits, then the fractional parts, then hp. | ||
| 405 | + if crate::trace_enabled() { | ||
| 406 | + eprintln!("SECT mv"); | ||
| 407 | + } | ||
| 408 | + update_mv_probs(&mut r, &mut ctx.nmv_joints); | ||
| 409 | + for comp in 0..2 { | ||
| 410 | + update_mv_probs(&mut r, &mut ctx.nmv_sign[comp..comp + 1]); | ||
| 411 | + update_mv_probs(&mut r, &mut ctx.nmv_classes[comp]); | ||
| 412 | + update_mv_probs(&mut r, &mut ctx.nmv_class0[comp]); | ||
| 413 | + update_mv_probs(&mut r, &mut ctx.nmv_bits[comp]); | ||
| 414 | + } | ||
| 415 | + for comp in 0..2 { | ||
| 416 | + for j in 0..2 { | ||
| 417 | + update_mv_probs(&mut r, &mut ctx.nmv_class0_fp[comp][j]); | ||
| 418 | + } | ||
| 419 | + update_mv_probs(&mut r, &mut ctx.nmv_fp[comp]); | ||
| 420 | + } | ||
| 421 | + if hdr.allow_high_precision_mv { | ||
| 422 | + for comp in 0..2 { | ||
| 423 | + update_mv_probs(&mut r, &mut ctx.nmv_class0_hp[comp..comp + 1]); | ||
| 424 | + update_mv_probs(&mut r, &mut ctx.nmv_hp[comp..comp + 1]); | ||
| 425 | + if crate::mvdump_enabled() { | ||
| 426 | + eprintln!( | ||
| 427 | + "MVPROB j0={} j1={} j2={}", | ||
| 428 | + ctx.nmv_joints[0], ctx.nmv_joints[1], ctx.nmv_joints[2] | ||
| 429 | + ); | ||
| 430 | + for comp in 0..2 { | ||
| 431 | + eprintln!( | ||
| 432 | + "MVPROB c{} sign={} class0={} class0hp={} hp={}", | ||
| 433 | + comp, | ||
| 434 | + ctx.nmv_sign[comp], | ||
| 435 | + ctx.nmv_class0[comp][0], | ||
| 436 | + ctx.nmv_class0_hp[comp], | ||
| 437 | + ctx.nmv_hp[comp] | ||
| 438 | + ); | ||
| 439 | + eprint!("MVPROB c{} cls", comp); | ||
| 440 | + for v in ctx.nmv_classes[comp].iter() { eprint!(" {}", v); } | ||
| 441 | + eprintln!(); | ||
| 442 | + eprint!("MVPROB c{} bits", comp); | ||
| 443 | + for v in ctx.nmv_bits[comp].iter() { eprint!(" {}", v); } | ||
| 444 | + eprintln!(); | ||
| 445 | + eprint!("MVPROB c{} c0fp", comp); | ||
| 446 | + for row in ctx.nmv_class0_fp[comp].iter() { for v in row.iter() { eprint!(" {}", v); } } | ||
| 447 | + eprintln!(); | ||
| 448 | + eprint!("MVPROB c{} fp", comp); | ||
| 449 | + for v in ctx.nmv_fp[comp].iter() { eprint!(" {}", v); } | ||
| 450 | + eprintln!(); | ||
| 451 | + } | ||
| 452 | + } | ||
| 453 | + } | ||
| 454 | + } | ||
| 455 | + } | ||
| 200 | if std::env::var_os("EC_VP9_DBG").is_some() { | 456 | if std::env::var_os("EC_VP9_DBG").is_some() { |
| 201 | eprintln!("DBG tx_mode={} consumed={}", tx_mode, r.byte_offset()); | 457 | eprintln!("DBG tx_mode={} consumed={}", tx_mode, r.byte_offset()); |
| 202 | eprintln!( | 458 | eprintln!( |
| @@ -209,5 +465,8 @@ pub(crate) fn read_compressed_header( | |||
| 209 | ); | 465 | ); |
| 210 | } | 466 | } |
| 211 | ensure(r.overreads() == 0, "compressed header desync")?; | 467 | ensure(r.overreads() == 0, "compressed header desync")?; |
| 212 | - Ok(tx_mode) | 468 | + Ok(CompressedHeader { |
| 469 | + tx_mode, | ||
| 470 | + reference_mode, | ||
| 471 | + }) | ||
| 213 | } | 472 | } |
crates/ec-vp9/src/inter.rsadded+1316 -0
| @@ -0,0 +1,1316 @@ | |||
| 1 | +//! Inter-frame syntax (libvpx `vp9/decoder/vp9_decodemv.c` | ||
| 2 | +//! `read_inter_frame_mode_info`, `vp9/common/vp9_mvref_common.h`). | ||
| 3 | +//! | ||
| 4 | +//! Syntax only: nothing here reconstructs pixels. The MV *predictor* is still | ||
| 5 | +//! parse-load-bearing — `read_mv` reads the high-precision bit only when | ||
| 6 | +//! `allow_high_precision_mv && use_mv_hp(predictor)`, so the candidate scan | ||
| 7 | +//! runs even though no motion compensation happens. | ||
| 8 | + | ||
| 9 | +use crate::bool::BoolDecoder; | ||
| 10 | +use crate::header::FrameContext; | ||
| 11 | +use crate::modes::{MiInfo, MiState}; | ||
| 12 | +use crate::tables::*; | ||
| 13 | + | ||
| 14 | +/// `INTRA_FRAME` .. `ALTREF_FRAME` (`MV_REFERENCE_FRAME`), `NO_REF_FRAME` = -1. | ||
| 15 | +pub(crate) const INTRA_FRAME: i8 = 0; | ||
| 16 | +pub(crate) const LAST_FRAME: i8 = 1; | ||
| 17 | +pub(crate) const GOLDEN_FRAME: i8 = 2; | ||
| 18 | +pub(crate) const ALTREF_FRAME: i8 = 3; | ||
| 19 | +pub(crate) const NO_REF_FRAME: i8 = -1; | ||
| 20 | +/// `PREDICTION_MODE` values of the inter modes. | ||
| 21 | +pub(crate) const NEARESTMV: u8 = 10; | ||
| 22 | +pub(crate) const NEARMV: u8 = 11; | ||
| 23 | +pub(crate) const ZEROMV: u8 = 12; | ||
| 24 | +pub(crate) const NEWMV: u8 = 13; | ||
| 25 | +/// `REFERENCE_MODE`. | ||
| 26 | +pub(crate) const SINGLE_REFERENCE: u8 = 0; | ||
| 27 | +pub(crate) const COMPOUND_REFERENCE: u8 = 1; | ||
| 28 | +pub(crate) const REFERENCE_MODE_SELECT: u8 = 2; | ||
| 29 | +/// `SWITCHABLE`: `interpolation_filter` value meaning "per block". | ||
| 30 | +pub(crate) const SWITCHABLE: u8 = 4; | ||
| 31 | +/// `SWITCHABLE_FILTERS`: the sentinel `mi->interp_filter` of intra blocks. | ||
| 32 | +pub(crate) const SWITCHABLE_FILTERS: u8 = 3; | ||
| 33 | + | ||
| 34 | +const MAX_MV_REF_CANDIDATES: usize = 2; | ||
| 35 | +const MVREF_NEIGHBOURS: usize = 8; | ||
| 36 | +/// `MV_BORDER` (1/8-pel units). | ||
| 37 | +const MV_BORDER: i32 = 16 << 3; | ||
| 38 | +/// `use_mv_hp`'s `kMvRefThresh`. | ||
| 39 | +const MV_HP_THRESH: i32 = 64; | ||
| 40 | +const CLASS0_SIZE: i32 = 2; | ||
| 41 | +const CLASS0_BITS: i32 = 1; | ||
| 42 | +/// `SEG_LVL_SKIP` / `SEG_LVL_REF_FRAME`. | ||
| 43 | +const SEG_LVL_REF_FRAME: usize = 2; | ||
| 44 | +const SEG_LVL_SKIP: usize = 3; | ||
| 45 | + | ||
| 46 | +/// `mv_ref_blocks[BLOCK_SIZES]` (`vp9_mvref_common.h`): `(row, col)` offsets, | ||
| 47 | +/// transcribed verbatim from the C. libvpx's `POSITION` is `{ row, col }`, so | ||
| 48 | +/// every accessor must add the FIRST element to the row and the SECOND to the | ||
| 49 | +/// column (a transposed accessor silently picks a different neighbour for every | ||
| 50 | +/// asymmetric entry). | ||
| 51 | +const MV_REF_BLOCKS: [[(i32, i32); MVREF_NEIGHBOURS]; 13] = [ | ||
| 52 | + // 4X4 | ||
| 53 | + [(-1, 0), (0, -1), (-1, -1), (-2, 0), (0, -2), (-2, -1), (-1, -2), (-2, -2)], | ||
| 54 | + // 4X8 | ||
| 55 | + [(-1, 0), (0, -1), (-1, -1), (-2, 0), (0, -2), (-2, -1), (-1, -2), (-2, -2)], | ||
| 56 | + // 8X4 | ||
| 57 | + [(-1, 0), (0, -1), (-1, -1), (-2, 0), (0, -2), (-2, -1), (-1, -2), (-2, -2)], | ||
| 58 | + // 8X8 | ||
| 59 | + [(-1, 0), (0, -1), (-1, -1), (-2, 0), (0, -2), (-2, -1), (-1, -2), (-2, -2)], | ||
| 60 | + // 8X16 | ||
| 61 | + [(0, -1), (-1, 0), (1, -1), (-1, -1), (0, -2), (-2, 0), (-2, -1), (-1, -2)], | ||
| 62 | + // 16X8 | ||
| 63 | + [(-1, 0), (0, -1), (-1, 1), (-1, -1), (-2, 0), (0, -2), (-1, -2), (-2, -1)], | ||
| 64 | + // 16X16 | ||
| 65 | + [(-1, 0), (0, -1), (-1, 1), (1, -1), (-1, -1), (-3, 0), (0, -3), (-3, -3)], | ||
| 66 | + // 16X32 | ||
| 67 | + [(0, -1), (-1, 0), (2, -1), (-1, -1), (-1, 1), (0, -3), (-3, 0), (-3, -3)], | ||
| 68 | + // 32X16 | ||
| 69 | + [(-1, 0), (0, -1), (-1, 2), (-1, -1), (1, -1), (-3, 0), (0, -3), (-3, -3)], | ||
| 70 | + // 32X32 | ||
| 71 | + [(-1, 1), (1, -1), (-1, 2), (2, -1), (-1, -1), (-3, 0), (0, -3), (-3, -3)], | ||
| 72 | + // 32X64 | ||
| 73 | + [(0, -1), (-1, 0), (4, -1), (-1, 2), (-1, -1), (0, -3), (-3, 0), (2, -1)], | ||
| 74 | + // 64X32 | ||
| 75 | + [(-1, 0), (0, -1), (-1, 4), (2, -1), (-1, -1), (-3, 0), (0, -3), (-1, 2)], | ||
| 76 | + // 64X64 | ||
| 77 | + [(-1, 3), (3, -1), (-1, 4), (4, -1), (-1, -1), (-1, 0), (0, -1), (-1, 6)], | ||
| 78 | +]; | ||
| 79 | + | ||
| 80 | +/// `idx_n_column_to_subblock` (`vp9_mvref_common.h`). | ||
| 81 | +const IDX_N_COLUMN_TO_SUBBLOCK: [[usize; 2]; 4] = [[1, 2], [1, 3], [3, 2], [3, 3]]; | ||
| 82 | + | ||
| 83 | +/// One `MV_REF` entry: the previous frame's per-8x8 reference pair and MVs | ||
| 84 | +/// (`vp9_read_mode_info` stores them for every block of an inter frame). | ||
| 85 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | ||
| 86 | +pub(crate) struct MvRef { | ||
| 87 | + /// `ref_frame[0]`/`[1]`; zeroed means `INTRA_FRAME`. | ||
| 88 | + pub ref_frame: [i8; 2], | ||
| 89 | + /// `mv[0]`/`[1]` in 1/8-pel units. | ||
| 90 | + pub mv: [(i32, i32); 2], | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +/// Frame-level reference-mode state (`VP9_COMMON::reference_mode`, | ||
| 94 | +/// `comp_fixed_ref`, `comp_var_ref`). | ||
| 95 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 96 | +pub(crate) struct InterFrameState { | ||
| 97 | + pub reference_mode: u8, | ||
| 98 | + pub comp_fixed_ref: i8, | ||
| 99 | + pub comp_var_ref: [i8; 2], | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +impl Default for InterFrameState { | ||
| 103 | + fn default() -> Self { | ||
| 104 | + InterFrameState { | ||
| 105 | + reference_mode: SINGLE_REFERENCE, | ||
| 106 | + comp_fixed_ref: ALTREF_FRAME, | ||
| 107 | + comp_var_ref: [LAST_FRAME, GOLDEN_FRAME], | ||
| 108 | + } | ||
| 109 | + } | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +/// `vp9_setup_compound_reference_mode` (`vp9_pred_common.c`): which ref frame | ||
| 113 | +/// of a compound pair is coded explicitly, and which two the coded bit picks. | ||
| 114 | +pub(crate) fn setup_compound_reference_mode(ifs: &mut InterFrameState, sign_bias: [bool; 4]) { | ||
| 115 | + let b = |f: i8| sign_bias[f as usize]; | ||
| 116 | + if b(LAST_FRAME) == b(GOLDEN_FRAME) { | ||
| 117 | + ifs.comp_fixed_ref = ALTREF_FRAME; | ||
| 118 | + ifs.comp_var_ref = [LAST_FRAME, GOLDEN_FRAME]; | ||
| 119 | + } else if b(LAST_FRAME) == b(ALTREF_FRAME) { | ||
| 120 | + ifs.comp_fixed_ref = GOLDEN_FRAME; | ||
| 121 | + ifs.comp_var_ref = [LAST_FRAME, ALTREF_FRAME]; | ||
| 122 | + } else { | ||
| 123 | + ifs.comp_fixed_ref = LAST_FRAME; | ||
| 124 | + ifs.comp_var_ref = [GOLDEN_FRAME, ALTREF_FRAME]; | ||
| 125 | + } | ||
| 126 | +} | ||
| 127 | + | ||
| 128 | +/// Frame- and position-scoped inputs of the block-level reads. | ||
| 129 | +pub(crate) struct InterBlockCtx<'a> { | ||
| 130 | + pub seg: &'a ec_vp9_syntax::SegmentationParams, | ||
| 131 | + pub ifs: InterFrameState, | ||
| 132 | + /// `ref_frame_sign_bias[INTRA..ALTREF]`. | ||
| 133 | + pub sign_bias: [bool; 4], | ||
| 134 | + pub allow_hp: bool, | ||
| 135 | + pub frame_interp: u8, | ||
| 136 | + pub tx_mode: u8, | ||
| 137 | + pub mi_rows: usize, | ||
| 138 | + pub mi_cols: usize, | ||
| 139 | + pub tile_col_start: usize, | ||
| 140 | + /// `tile->mi_col_end`. | ||
| 141 | + pub tile_col_end: usize, | ||
| 142 | + /// The previous frame's segment map, when it has one. | ||
| 143 | + pub last_seg: Option<&'a [u8]>, | ||
| 144 | + /// `prev_frame->mvs` when `use_prev_frame_mvs`. | ||
| 145 | + pub prev_mvs: Option<&'a [MvRef]>, | ||
| 146 | +} | ||
| 147 | + | ||
| 148 | +/// The two decoded neighbours of the current block. | ||
| 149 | +#[derive(Clone, Copy)] | ||
| 150 | +struct Nb<'a> { | ||
| 151 | + above: Option<&'a MiInfo>, | ||
| 152 | + left: Option<&'a MiInfo>, | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +/// `read_inter_frame_mode_info` (`vp9_decodemv.c`). | ||
| 156 | +#[allow(clippy::too_many_arguments)] | ||
| 157 | +pub(crate) fn read_inter_frame_mode_info( | ||
| 158 | + r: &mut BoolDecoder, | ||
| 159 | + st: &MiState, | ||
| 160 | + c: &InterBlockCtx<'_>, | ||
| 161 | + fc: &FrameContext, | ||
| 162 | + row: usize, | ||
| 163 | + col: usize, | ||
| 164 | + sb_type: usize, | ||
| 165 | + x_mis: usize, | ||
| 166 | + y_mis: usize, | ||
| 167 | + partition: u8, | ||
| 168 | + cur_seg: &mut [u8], | ||
| 169 | +) -> crate::Result<MiInfo> { | ||
| 170 | + let above = st.above_mi(row, col).cloned(); | ||
| 171 | + let left = st.left_mi(row, col, c.tile_col_start).cloned(); | ||
| 172 | + let nb = Nb { | ||
| 173 | + above: above.as_ref(), | ||
| 174 | + left: left.as_ref(), | ||
| 175 | + }; | ||
| 176 | + | ||
| 177 | + let mut seg_id_predicted = false; | ||
| 178 | + let segment = read_inter_segment_id(r, c, nb, row, col, x_mis, y_mis, cur_seg, &mut seg_id_predicted); | ||
| 179 | + let skip = read_skip(r, c, fc, segment, nb); | ||
| 180 | + let inter_block = read_is_inter_block(r, c, fc, segment, nb); | ||
| 181 | + let tx_size = read_tx_size(r, c, fc, sb_type, nb, !skip || !inter_block); | ||
| 182 | + | ||
| 183 | + let mut info = MiInfo { | ||
| 184 | + sb_type, | ||
| 185 | + segment, | ||
| 186 | + skip, | ||
| 187 | + tx_size, | ||
| 188 | + mode: 0, | ||
| 189 | + uv_mode: 0, | ||
| 190 | + bmi: [0; 4], | ||
| 191 | + is_inter: inter_block, | ||
| 192 | + ref_frame: [NO_REF_FRAME; 2], | ||
| 193 | + mv: [(0, 0); 2], | ||
| 194 | + mv_diff: [(0, 0); 2], | ||
| 195 | + bmi_mv: [[(0, 0); 2]; 4], | ||
| 196 | + seg_id_predicted, | ||
| 197 | + interp_filter: SWITCHABLE_FILTERS, | ||
| 198 | + }; | ||
| 199 | + if inter_block { | ||
| 200 | + read_inter_block_mode_info(r, st, c, fc, &mut info, nb, row, col, partition)?; | ||
| 201 | + } else { | ||
| 202 | + read_intra_block_mode_info(r, fc, &mut info); | ||
| 203 | + } | ||
| 204 | + Ok(info) | ||
| 205 | +} | ||
| 206 | + | ||
| 207 | +/// `dec_get_segment_id`: the minimum id over the block's covered cells. | ||
| 208 | +fn min_seg(map: &[u8], row: usize, col: usize, x_mis: usize, y_mis: usize, mi_cols: usize) -> u8 { | ||
| 209 | + let mut id = u8::MAX; | ||
| 210 | + for y in 0..y_mis { | ||
| 211 | + for x in 0..x_mis { | ||
| 212 | + id = id.min(map[(row + y) * mi_cols + col + x]); | ||
| 213 | + } | ||
| 214 | + } | ||
| 215 | + id | ||
| 216 | +} | ||
| 217 | + | ||
| 218 | +/// `set_segment_id` / `copy_segment_id`. | ||
| 219 | +fn write_seg( | ||
| 220 | + src: Option<&[u8]>, | ||
| 221 | + dst: &mut [u8], | ||
| 222 | + row: usize, | ||
| 223 | + col: usize, | ||
| 224 | + x_mis: usize, | ||
| 225 | + y_mis: usize, | ||
| 226 | + mi_cols: usize, | ||
| 227 | + id: u8, | ||
| 228 | +) { | ||
| 229 | + for y in 0..y_mis { | ||
| 230 | + for x in 0..x_mis { | ||
| 231 | + let i = (row + y) * mi_cols + col + x; | ||
| 232 | + dst[i] = match src { | ||
| 233 | + Some(m) => m[i], | ||
| 234 | + None => id, | ||
| 235 | + }; | ||
| 236 | + } | ||
| 237 | + } | ||
| 238 | +} | ||
| 239 | + | ||
| 240 | +/// `read_inter_segment_id` (`vp9_decodemv.c:151`). | ||
| 241 | +#[allow(clippy::too_many_arguments)] | ||
| 242 | +fn read_inter_segment_id( | ||
| 243 | + r: &mut BoolDecoder, | ||
| 244 | + c: &InterBlockCtx<'_>, | ||
| 245 | + nb: Nb<'_>, | ||
| 246 | + row: usize, | ||
| 247 | + col: usize, | ||
| 248 | + x_mis: usize, | ||
| 249 | + y_mis: usize, | ||
| 250 | + cur_seg: &mut [u8], | ||
| 251 | + seg_id_predicted: &mut bool, | ||
| 252 | +) -> u8 { | ||
| 253 | + let seg = c.seg; | ||
| 254 | + if !seg.enabled { | ||
| 255 | + return 0; | ||
| 256 | + } | ||
| 257 | + let predicted = c | ||
| 258 | + .last_seg | ||
| 259 | + .map_or(0, |m| min_seg(m, row, col, x_mis, y_mis, c.mi_cols)); | ||
| 260 | + if !seg.update_map { | ||
| 261 | + write_seg(c.last_seg, cur_seg, row, col, x_mis, y_mis, c.mi_cols, 0); | ||
| 262 | + return predicted; | ||
| 263 | + } | ||
| 264 | + let segment_id = if seg.temporal_update { | ||
| 265 | + let ctx = usize::from(nb.above.is_some_and(|m| m.seg_id_predicted)) | ||
| 266 | + + usize::from(nb.left.is_some_and(|m| m.seg_id_predicted)); | ||
| 267 | + *seg_id_predicted = r.read_bool(seg.pred_probs[ctx]); | ||
| 268 | + if *seg_id_predicted { | ||
| 269 | + predicted | ||
| 270 | + } else { | ||
| 271 | + r.read_tree(&SEGMENT_TREE, &seg.tree_probs) | ||
| 272 | + } | ||
| 273 | + } else { | ||
| 274 | + r.read_tree(&SEGMENT_TREE, &seg.tree_probs) | ||
| 275 | + }; | ||
| 276 | + write_seg(None, cur_seg, row, col, x_mis, y_mis, c.mi_cols, segment_id); | ||
| 277 | + segment_id | ||
| 278 | +} | ||
| 279 | + | ||
| 280 | +fn seg_feature(seg: &ec_vp9_syntax::SegmentationParams, segment_id: u8, feature: usize) -> bool { | ||
| 281 | + seg.enabled | ||
| 282 | + && seg | ||
| 283 | + .feature_enabled | ||
| 284 | + .get(segment_id as usize) | ||
| 285 | + .is_some_and(|f| f[feature]) | ||
| 286 | +} | ||
| 287 | + | ||
| 288 | +/// `read_skip`. | ||
| 289 | +fn read_skip( | ||
| 290 | + r: &mut BoolDecoder, | ||
| 291 | + c: &InterBlockCtx<'_>, | ||
| 292 | + fc: &FrameContext, | ||
| 293 | + segment_id: u8, | ||
| 294 | + nb: Nb<'_>, | ||
| 295 | +) -> bool { | ||
| 296 | + if seg_feature(c.seg, segment_id, SEG_LVL_SKIP) { | ||
| 297 | + true | ||
| 298 | + } else { | ||
| 299 | + let ctx = usize::from(nb.above.is_some_and(|m| m.skip)) | ||
| 300 | + + usize::from(nb.left.is_some_and(|m| m.skip)); | ||
| 301 | + r.read_bool(fc.skip[ctx]) | ||
| 302 | + } | ||
| 303 | +} | ||
| 304 | + | ||
| 305 | +/// `get_intra_inter_context` (`vp9_pred_common.h`). | ||
| 306 | +fn intra_inter_context(nb: Nb<'_>) -> usize { | ||
| 307 | + match (nb.above, nb.left) { | ||
| 308 | + (Some(a), Some(l)) => { | ||
| 309 | + let above_intra = !a.is_inter; | ||
| 310 | + let left_intra = !l.is_inter; | ||
| 311 | + if left_intra && above_intra { | ||
| 312 | + 3 | ||
| 313 | + } else { | ||
| 314 | + usize::from(left_intra || above_intra) | ||
| 315 | + } | ||
| 316 | + } | ||
| 317 | + (Some(m), None) | (None, Some(m)) => 2 * usize::from(!m.is_inter), | ||
| 318 | + (None, None) => 0, | ||
| 319 | + } | ||
| 320 | +} | ||
| 321 | + | ||
| 322 | +/// `read_is_inter_block`. | ||
| 323 | +fn read_is_inter_block( | ||
| 324 | + r: &mut BoolDecoder, | ||
| 325 | + c: &InterBlockCtx<'_>, | ||
| 326 | + fc: &FrameContext, | ||
| 327 | + segment_id: u8, | ||
| 328 | + nb: Nb<'_>, | ||
| 329 | +) -> bool { | ||
| 330 | + if seg_feature(c.seg, segment_id, SEG_LVL_REF_FRAME) { | ||
| 331 | + c.seg.feature_data[segment_id as usize][SEG_LVL_REF_FRAME] != INTRA_FRAME as i16 | ||
| 332 | + } else { | ||
| 333 | + r.read_bool(fc.intra_inter[intra_inter_context(nb)]) | ||
| 334 | + } | ||
| 335 | +} | ||
| 336 | + | ||
| 337 | +/// `read_tx_size` with `allow_select` (spec 6.3.5 / `vp9_decodemv.c:85`). | ||
| 338 | +fn read_tx_size( | ||
| 339 | + r: &mut BoolDecoder, | ||
| 340 | + c: &InterBlockCtx<'_>, | ||
| 341 | + fc: &FrameContext, | ||
| 342 | + sb_type: usize, | ||
| 343 | + nb: Nb<'_>, | ||
| 344 | + allow_select: bool, | ||
| 345 | +) -> usize { | ||
| 346 | + let max_tx = MAX_TXSIZE_LOOKUP[sb_type] as usize; | ||
| 347 | + if allow_select && c.tx_mode == TX_MODE_SELECT && sb_type >= 3 { | ||
| 348 | + // get_tx_size_context: default each side to max_tx, then substitute | ||
| 349 | + // the other side when one is missing, in this exact order. | ||
| 350 | + let a0 = nb | ||
| 351 | + .above | ||
| 352 | + .map_or(max_tx, |m| if m.skip { max_tx } else { m.tx_size }); | ||
| 353 | + let l0 = nb | ||
| 354 | + .left | ||
| 355 | + .map_or(max_tx, |m| if m.skip { max_tx } else { m.tx_size }); | ||
| 356 | + let mut a = a0; | ||
| 357 | + let mut l = l0; | ||
| 358 | + if nb.left.is_none() { | ||
| 359 | + l = a; | ||
| 360 | + } | ||
| 361 | + if nb.above.is_none() { | ||
| 362 | + a = l; | ||
| 363 | + } | ||
| 364 | + let ctx = usize::from(a + l > max_tx); | ||
| 365 | + let probs: &[u8] = match max_tx { | ||
| 366 | + 1 => &fc.tx_p8x8[ctx][..], | ||
| 367 | + 2 => &fc.tx_p16x16[ctx][..], | ||
| 368 | + _ => &fc.tx_p32x32[ctx][..], | ||
| 369 | + }; | ||
| 370 | + let mut tx = r.read_bool(probs[0]) as usize; | ||
| 371 | + if tx != TX_4X4 && max_tx >= TX_16X16 { | ||
| 372 | + tx += r.read_bool(probs[1]) as usize; | ||
| 373 | + if tx != TX_8X8 && max_tx >= TX_32X32 { | ||
| 374 | + tx += r.read_bool(probs[2]) as usize; | ||
| 375 | + } | ||
| 376 | + } | ||
| 377 | + tx | ||
| 378 | + } else { | ||
| 379 | + max_tx.min(TX_MODE_TO_BIGGEST_TX_SIZE[c.tx_mode as usize]) | ||
| 380 | + } | ||
| 381 | +} | ||
| 382 | + | ||
| 383 | +/// `read_inter_mode`. | ||
| 384 | +fn read_inter_mode(r: &mut BoolDecoder, fc: &FrameContext, ctx: usize) -> u8 { | ||
| 385 | + NEARESTMV + r.read_tree(&INTER_MODE_TREE, &fc.inter_mode[ctx]) | ||
| 386 | +} | ||
| 387 | + | ||
| 388 | +/// `get_mode_context` (`vp9_decodemv.c:681`): the two nearest candidates' | ||
| 389 | +/// modes fold into one of 5 contexts. | ||
| 390 | +fn get_mode_context(st: &MiState, c: &InterBlockCtx<'_>, bsize: usize, row: usize, col: usize) -> usize { | ||
| 391 | + let mut counter = 0usize; | ||
| 392 | + for p in MV_REF_BLOCKS[bsize][..2].iter() { | ||
| 393 | + if is_inside(c, row, col, *p) { | ||
| 394 | + let mode = st | ||
| 395 | + .at((row as i32 + p.0) as usize, (col as i32 + p.1) as usize) | ||
| 396 | + .map_or(0u8, |m| m.mode); | ||
| 397 | + counter += MODE_2_COUNTER[mode as usize] as usize; | ||
| 398 | + } | ||
| 399 | + } | ||
| 400 | + COUNTER_TO_CONTEXT[counter] as usize | ||
| 401 | +} | ||
| 402 | + | ||
| 403 | +/// `get_pred_context_switchable_interp` (`vp9_pred_common.h:69`). | ||
| 404 | +fn switchable_interp_ctx(nb: Nb<'_>) -> usize { | ||
| 405 | + let left_type = nb.left.map_or(SWITCHABLE_FILTERS, |m| m.interp_filter); | ||
| 406 | + let above_type = nb.above.map_or(SWITCHABLE_FILTERS, |m| m.interp_filter); | ||
| 407 | + if left_type == above_type { | ||
| 408 | + left_type as usize | ||
| 409 | + } else if left_type == SWITCHABLE_FILTERS { | ||
| 410 | + above_type as usize | ||
| 411 | + } else if above_type == SWITCHABLE_FILTERS { | ||
| 412 | + left_type as usize | ||
| 413 | + } else { | ||
| 414 | + SWITCHABLE_FILTERS as usize | ||
| 415 | + } | ||
| 416 | +} | ||
| 417 | + | ||
| 418 | +/// `read_intra_block_mode_info` (the intra fallback inside an inter frame). | ||
| 419 | +fn read_intra_block_mode_info(r: &mut BoolDecoder, fc: &FrameContext, info: &mut MiInfo) { | ||
| 420 | + let bsize = info.sb_type; | ||
| 421 | + match bsize { | ||
| 422 | + 0 => { | ||
| 423 | + for i in 0..4 { | ||
| 424 | + info.bmi[i] = r.read_tree(&INTRA_MODE_TREE, &fc.y_mode[0]); | ||
| 425 | + } | ||
| 426 | + info.mode = info.bmi[3]; | ||
| 427 | + } | ||
| 428 | + 1 => { | ||
| 429 | + let m0 = r.read_tree(&INTRA_MODE_TREE, &fc.y_mode[0]); | ||
| 430 | + info.bmi[0] = m0; | ||
| 431 | + info.bmi[2] = m0; | ||
| 432 | + let m1 = r.read_tree(&INTRA_MODE_TREE, &fc.y_mode[0]); | ||
| 433 | + info.bmi[1] = m1; | ||
| 434 | + info.bmi[3] = m1; | ||
| 435 | + info.mode = m1; | ||
| 436 | + } | ||
| 437 | + 2 => { | ||
| 438 | + let m0 = r.read_tree(&INTRA_MODE_TREE, &fc.y_mode[0]); | ||
| 439 | + info.bmi[0] = m0; | ||
| 440 | + info.bmi[1] = m0; | ||
| 441 | + let m2 = r.read_tree(&INTRA_MODE_TREE, &fc.y_mode[0]); | ||
| 442 | + info.bmi[2] = m2; | ||
| 443 | + info.bmi[3] = m2; | ||
| 444 | + info.mode = m2; | ||
| 445 | + } | ||
| 446 | + _ => { | ||
| 447 | + info.mode = r.read_tree(&INTRA_MODE_TREE, &fc.y_mode[SIZE_GROUP_LOOKUP[bsize] as usize]); | ||
| 448 | + } | ||
| 449 | + } | ||
| 450 | + info.uv_mode = r.read_tree(&INTRA_MODE_TREE, &fc.uv_mode[info.mode as usize]); | ||
| 451 | + info.interp_filter = SWITCHABLE_FILTERS; | ||
| 452 | + info.ref_frame = [INTRA_FRAME, NO_REF_FRAME]; | ||
| 453 | +} | ||
| 454 | + | ||
| 455 | +/// `is_inside` (`vp9_mvref_common.h:278`) — note it bounds rows by the FRAME | ||
| 456 | +/// and columns by the TILE's end; there is no row bound against the tile. | ||
| 457 | +fn is_inside(c: &InterBlockCtx<'_>, row: usize, col: usize, p: (i32, i32)) -> bool { | ||
| 458 | + let r = row as i32 + p.0; | ||
| 459 | + let cc = col as i32 + p.1; | ||
| 460 | + !(r < 0 || cc < c.tile_col_start as i32 || r >= c.mi_rows as i32 || cc >= c.tile_col_end as i32) | ||
| 461 | +} | ||
| 462 | + | ||
| 463 | +/// `get_sub_block_mv` (`vp9_mvref_common.h:224`). | ||
| 464 | +fn get_sub_block_mv(cand: &MiInfo, which: usize, search_col: i32, block: i32) -> (i32, i32) { | ||
| 465 | + if block >= 0 && cand.sb_type < 3 { | ||
| 466 | + let b = IDX_N_COLUMN_TO_SUBBLOCK[block as usize][usize::from(search_col == 0)]; | ||
| 467 | + cand.bmi_mv[b][which] | ||
| 468 | + } else { | ||
| 469 | + cand.mv[which] | ||
| 470 | + } | ||
| 471 | +} | ||
| 472 | + | ||
| 473 | +/// `use_mv_hp`: a small predictor keeps 1/8-pel precision. | ||
| 474 | +fn use_mv_hp(mv: (i32, i32)) -> bool { | ||
| 475 | + mv.0.abs() < MV_HP_THRESH && mv.1.abs() < MV_HP_THRESH | ||
| 476 | +} | ||
| 477 | + | ||
| 478 | +/// `lower_mv_precision`: round the predictor to 1/4-pel when hp is not used. | ||
| 479 | +fn lower_mv_precision(mv: &mut (i32, i32), allow_hp: bool) { | ||
| 480 | + if !(allow_hp && use_mv_hp(*mv)) { | ||
| 481 | + if mv.0 & 1 != 0 { | ||
| 482 | + mv.0 += if mv.0 > 0 { -1 } else { 1 }; | ||
| 483 | + } | ||
| 484 | + if mv.1 & 1 != 0 { | ||
| 485 | + mv.1 += if mv.1 > 0 { -1 } else { 1 }; | ||
| 486 | + } | ||
| 487 | + } | ||
| 488 | +} | ||
| 489 | + | ||
| 490 | +/// `clamp_mv_ref`. | ||
| 491 | +fn clamp_mv_ref(mv: &mut (i32, i32), c: &InterBlockCtx<'_>, row: usize, col: usize, bw: usize, bh: usize) { | ||
| 492 | + // xd->mb_to_{left,right,top,bottom}_edge (vp9_onyxc_int.h:424-427). The | ||
| 493 | + // right/bottom differences are SIGNED in libvpx (a wide/tall block on the | ||
| 494 | + // frame's partial last row/column makes them negative), so they must not be | ||
| 495 | + // computed in usize. | ||
| 496 | + let left = -(col as i32 * 64); | ||
| 497 | + let right = (c.mi_cols as i32 - bw as i32 - col as i32) * 64; | ||
| 498 | + let top = -(row as i32 * 64); | ||
| 499 | + let bottom = (c.mi_rows as i32 - bh as i32 - row as i32) * 64; | ||
| 500 | + // `clamp_mv(mv, min_col, max_col, min_row, max_row)` (vp9_mv.h:47): the | ||
| 501 | + // COLUMN takes the left/right edges and the ROW the top/bottom ones, and | ||
| 502 | + // `mv.0` is the row (read_mv fills it from the vertical joint component). | ||
| 503 | + mv.1 = mv.1.clamp(left - MV_BORDER, right + MV_BORDER); | ||
| 504 | + mv.0 = mv.0.clamp(top - MV_BORDER, bottom + MV_BORDER); | ||
| 505 | +} | ||
| 506 | + | ||
| 507 | +/// `ADD_MV_REF_LIST_EB`: returns true when the C `goto Done` fires. | ||
| 508 | +fn add_mv_ref_list_eb( | ||
| 509 | + mv: (i32, i32), | ||
| 510 | + list: &mut [(i32, i32); MAX_MV_REF_CANDIDATES], | ||
| 511 | + count: &mut usize, | ||
| 512 | + early_break: bool, | ||
| 513 | +) -> bool { | ||
| 514 | + if *count > 0 { | ||
| 515 | + if mv != list[0] { | ||
| 516 | + list[*count] = mv; | ||
| 517 | + *count += 1; | ||
| 518 | + true | ||
| 519 | + } else { | ||
| 520 | + false | ||
| 521 | + } | ||
| 522 | + } else { | ||
| 523 | + list[0] = mv; | ||
| 524 | + *count = 1; | ||
| 525 | + early_break | ||
| 526 | + } | ||
| 527 | +} | ||
| 528 | + | ||
| 529 | +/// `scale_mv` (the decoder's sign-flip-only variant, `vp9_mvref_common.h`). | ||
| 530 | +fn scale_mv(cand: &MiInfo, which: usize, ref_frame: i8, sign_bias: [bool; 4]) -> (i32, i32) { | ||
| 531 | + let mv = cand.mv[which]; | ||
| 532 | + if sign_bias[cand.ref_frame[which] as usize] != sign_bias[ref_frame as usize] { | ||
| 533 | + (-mv.0, -mv.1) | ||
| 534 | + } else { | ||
| 535 | + mv | ||
| 536 | + } | ||
| 537 | +} | ||
| 538 | + | ||
| 539 | +/// `dec_find_mv_refs` (`vp9_decodemv.c:495`): the candidate list, clamped. | ||
| 540 | +/// Returns `(refmv_count, list)`. | ||
| 541 | +#[allow(clippy::too_many_arguments)] | ||
| 542 | +fn find_mv_refs( | ||
| 543 | + st: &MiState, | ||
| 544 | + c: &InterBlockCtx<'_>, | ||
| 545 | + mode: u8, | ||
| 546 | + ref_frame: i8, | ||
| 547 | + bsize: usize, | ||
| 548 | + row: usize, | ||
| 549 | + col: usize, | ||
| 550 | + block: i32, | ||
| 551 | + bw: usize, | ||
| 552 | + bh: usize, | ||
| 553 | +) -> (usize, [(i32, i32); MAX_MV_REF_CANDIDATES]) { | ||
| 554 | + let search = &MV_REF_BLOCKS[bsize]; | ||
| 555 | + let early_break = mode != NEARMV; | ||
| 556 | + let mvref_dbg = crate::mvdbg_enabled() && mode == NEWMV; | ||
| 557 | + let mut list = [(0, 0); MAX_MV_REF_CANDIDATES]; | ||
| 558 | + let mut count = 0usize; | ||
| 559 | + let mut different_ref_found = false; | ||
| 560 | + let mut done = false; | ||
| 561 | + | ||
| 562 | + let cell = |p: (i32, i32)| -> Option<&MiInfo> { | ||
| 563 | + st.at((row as i32 + p.0) as usize, (col as i32 + p.1) as usize) | ||
| 564 | + }; | ||
| 565 | + | ||
| 566 | + let mut i = 0usize; | ||
| 567 | + if block >= 0 { | ||
| 568 | + while i < 2 && !done { | ||
| 569 | + let p = search[i]; | ||
| 570 | + if is_inside(c, row, col, p) { | ||
| 571 | + different_ref_found = true; | ||
| 572 | + if let Some(cand) = cell(p) { | ||
| 573 | + if cand.ref_frame[0] == ref_frame { | ||
| 574 | + done = add_mv_ref_list_eb( | ||
| 575 | + get_sub_block_mv(cand, 0, p.1, block), | ||
| 576 | + &mut list, | ||
| 577 | + &mut count, | ||
| 578 | + early_break, | ||
| 579 | + ); | ||
| 580 | + } else if cand.ref_frame[1] == ref_frame { | ||
| 581 | + done = add_mv_ref_list_eb( | ||
| 582 | + get_sub_block_mv(cand, 1, p.1, block), | ||
| 583 | + &mut list, | ||
| 584 | + &mut count, | ||
| 585 | + early_break, | ||
| 586 | + ); | ||
| 587 | + } | ||
| 588 | + } | ||
| 589 | + } | ||
| 590 | + i += 1; | ||
| 591 | + } | ||
| 592 | + } | ||
| 593 | + while i < MVREF_NEIGHBOURS && !done { | ||
| 594 | + let p = search[i]; | ||
| 595 | + if is_inside(c, row, col, p) { | ||
| 596 | + different_ref_found = true; | ||
| 597 | + if let Some(cand) = cell(p) { | ||
| 598 | + if cand.ref_frame[0] == ref_frame { | ||
| 599 | + done = add_mv_ref_list_eb(cand.mv[0], &mut list, &mut count, early_break); | ||
| 600 | + } else if cand.ref_frame[1] == ref_frame { | ||
| 601 | + done = add_mv_ref_list_eb(cand.mv[1], &mut list, &mut count, early_break); | ||
| 602 | + } | ||
| 603 | + } | ||
| 604 | + } | ||
| 605 | + i += 1; | ||
| 606 | + } | ||
| 607 | + | ||
| 608 | + if !done { | ||
| 609 | + if let Some(prev) = c.prev_mvs { | ||
| 610 | + let e = prev[row * c.mi_cols + col]; | ||
| 611 | + if e.ref_frame[0] == ref_frame { | ||
| 612 | + done = add_mv_ref_list_eb(e.mv[0], &mut list, &mut count, early_break); | ||
| 613 | + } else if e.ref_frame[1] == ref_frame { | ||
| 614 | + done = add_mv_ref_list_eb(e.mv[1], &mut list, &mut count, early_break); | ||
| 615 | + } | ||
| 616 | + } | ||
| 617 | + } | ||
| 618 | + | ||
| 619 | + if crate::mvdbg2_enabled() | ||
| 620 | + && ((row == 8 && col == 104) || (row == 32 && col == 64) || (row == 8 && col == 32)) | ||
| 621 | + { | ||
| 622 | + for (i, p) in search.iter().enumerate() { | ||
| 623 | + let ins = is_inside(c, row, col, *p); | ||
| 624 | + let c2 = st.at((row as i32 + p.0) as usize, (col as i32 + p.1) as usize); | ||
| 625 | + let qr = (row as i32 + p.0) as usize; | ||
| 626 | + let qc = (col as i32 + p.1) as usize; | ||
| 627 | + match c2 { | ||
| 628 | + Some(cd) => eprintln!( | ||
| 629 | + "MVC r={row} c={col} i={i} p={p:?} q=({qr} {qc}) idx={} inside={ins} ref0={} ref1={} mv0={:?} mv1={:?} inter={}", | ||
| 630 | + qr * c.mi_cols + qc, | ||
| 631 | + cd.ref_frame[0], cd.ref_frame[1], cd.mv[0], cd.mv[1], cd.is_inter | ||
| 632 | + ), | ||
| 633 | + None => eprintln!("MVC r={row} c={col} i={i} p={p:?} q=({qr} {qc}) inside={ins} none"), | ||
| 634 | + } | ||
| 635 | + } | ||
| 636 | + } | ||
| 637 | + if mvref_dbg { | ||
| 638 | + eprintln!( | ||
| 639 | + "MVREF r={row} c={col} bsize={bsize} mode={mode} n={count} l0={:?} l1={:?}", | ||
| 640 | + list[0], list[1] | ||
| 641 | + ); | ||
| 642 | + } | ||
| 643 | + if !done && different_ref_found { | ||
| 644 | + for p in search.iter() { | ||
| 645 | + if !is_inside(c, row, col, *p) { | ||
| 646 | + continue; | ||
| 647 | + } | ||
| 648 | + let Some(cand) = cell(*p) else { continue }; | ||
| 649 | + if !cand.is_inter { | ||
| 650 | + continue; | ||
| 651 | + } | ||
| 652 | + if cand.ref_frame[0] != ref_frame { | ||
| 653 | + done = add_mv_ref_list_eb( | ||
| 654 | + scale_mv(cand, 0, ref_frame, c.sign_bias), | ||
| 655 | + &mut list, | ||
| 656 | + &mut count, | ||
| 657 | + early_break, | ||
| 658 | + ); | ||
| 659 | + } | ||
| 660 | + if done { | ||
| 661 | + break; | ||
| 662 | + } | ||
| 663 | + if cand.ref_frame[1] > INTRA_FRAME | ||
| 664 | + && cand.ref_frame[1] != ref_frame | ||
| 665 | + && cand.mv[1] != cand.mv[0] | ||
| 666 | + { | ||
| 667 | + done = add_mv_ref_list_eb( | ||
| 668 | + scale_mv(cand, 1, ref_frame, c.sign_bias), | ||
| 669 | + &mut list, | ||
| 670 | + &mut count, | ||
| 671 | + early_break, | ||
| 672 | + ); | ||
| 673 | + } | ||
| 674 | + } | ||
| 675 | + } | ||
| 676 | + | ||
| 677 | + if !done { | ||
| 678 | + if let Some(prev) = c.prev_mvs { | ||
| 679 | + let e = prev[row * c.mi_cols + col]; | ||
| 680 | + if e.ref_frame[0] != ref_frame && e.ref_frame[0] > INTRA_FRAME { | ||
| 681 | + let mut mv = e.mv[0]; | ||
| 682 | + if c.sign_bias[e.ref_frame[0] as usize] != c.sign_bias[ref_frame as usize] { | ||
| 683 | + mv = (-mv.0, -mv.1); | ||
| 684 | + } | ||
| 685 | + done = add_mv_ref_list_eb(mv, &mut list, &mut count, early_break); | ||
| 686 | + } | ||
| 687 | + if !done | ||
| 688 | + && e.ref_frame[1] > INTRA_FRAME | ||
| 689 | + && e.ref_frame[1] != ref_frame | ||
| 690 | + && e.mv[1] != e.mv[0] | ||
| 691 | + { | ||
| 692 | + let mut mv = e.mv[1]; | ||
| 693 | + if c.sign_bias[e.ref_frame[1] as usize] != c.sign_bias[ref_frame as usize] { | ||
| 694 | + mv = (-mv.0, -mv.1); | ||
| 695 | + } | ||
| 696 | + add_mv_ref_list_eb(mv, &mut list, &mut count, early_break); | ||
| 697 | + } | ||
| 698 | + } | ||
| 699 | + } | ||
| 700 | + | ||
| 701 | + // `dec_find_mv_refs`' tail (vp9_decodemv.c:626): an early exit (`goto Done` | ||
| 702 | + // -- either the second candidate was added, or `early_break` stopped us at | ||
| 703 | + // the first) JUMPS OVER the mode-derived override and returns the count as | ||
| 704 | + // accumulated; falling out of the loops applies the override, so a NEARMV | ||
| 705 | + // block reports two candidates even when only one was found and the caller | ||
| 706 | + // then reads the zeroed second slot. The list was memset to zero first, so | ||
| 707 | + // that second slot is (0, 0), and the clamp covers the final count only. | ||
| 708 | + let n = if done { | ||
| 709 | + count | ||
| 710 | + } else if mode == NEARMV { | ||
| 711 | + MAX_MV_REF_CANDIDATES | ||
| 712 | + } else { | ||
| 713 | + 1 | ||
| 714 | + }; | ||
| 715 | + for k in 0..n { | ||
| 716 | + clamp_mv_ref(&mut list[k], c, row, col, bw, bh); | ||
| 717 | + } | ||
| 718 | + (n, list) | ||
| 719 | +} | ||
| 720 | + | ||
| 721 | +/// `append_sub8x8_mvs_for_idx` (`vp9_decodemv.c:620`). | ||
| 722 | +#[allow(clippy::too_many_arguments)] | ||
| 723 | +fn append_sub8x8_mvs_for_idx( | ||
| 724 | + st: &MiState, | ||
| 725 | + c: &InterBlockCtx<'_>, | ||
| 726 | + info: &MiInfo, | ||
| 727 | + bmode: u8, | ||
| 728 | + block: usize, | ||
| 729 | + rf: usize, | ||
| 730 | + row: usize, | ||
| 731 | + col: usize, | ||
| 732 | + bw: usize, | ||
| 733 | + bh: usize, | ||
| 734 | +) -> (i32, i32) { | ||
| 735 | + let bsize = info.sb_type; | ||
| 736 | + match block { | ||
| 737 | + 0 => { | ||
| 738 | + let (n, list) = find_mv_refs(st, c, bmode, info.ref_frame[rf], bsize, row, col, 0, bw, bh); | ||
| 739 | + list[n - 1] | ||
| 740 | + } | ||
| 741 | + 1 | 2 => { | ||
| 742 | + if bmode == NEARESTMV { | ||
| 743 | + info.bmi_mv[0][rf] | ||
| 744 | + } else { | ||
| 745 | + let (_n, list) = | ||
| 746 | + find_mv_refs(st, c, bmode, info.ref_frame[rf], bsize, row, col, block as i32, bw, bh); | ||
| 747 | + let mut out = (0, 0); | ||
| 748 | + for m in list.iter() { | ||
| 749 | + if info.bmi_mv[0][rf] != *m { | ||
| 750 | + out = *m; | ||
| 751 | + break; | ||
| 752 | + } | ||
| 753 | + } | ||
| 754 | + out | ||
| 755 | + } | ||
| 756 | + } | ||
| 757 | + _ => { | ||
| 758 | + if bmode == NEARESTMV { | ||
| 759 | + info.bmi_mv[2][rf] | ||
| 760 | + } else { | ||
| 761 | + if info.bmi_mv[2][rf] != info.bmi_mv[1][rf] { | ||
| 762 | + info.bmi_mv[1][rf] | ||
| 763 | + } else if info.bmi_mv[2][rf] != info.bmi_mv[0][rf] { | ||
| 764 | + info.bmi_mv[0][rf] | ||
| 765 | + } else { | ||
| 766 | + let (_n, list) = find_mv_refs( | ||
| 767 | + st, | ||
| 768 | + c, | ||
| 769 | + bmode, | ||
| 770 | + info.ref_frame[rf], | ||
| 771 | + bsize, | ||
| 772 | + row, | ||
| 773 | + col, | ||
| 774 | + block as i32, | ||
| 775 | + bw, | ||
| 776 | + bh, | ||
| 777 | + ); | ||
| 778 | + let mut out = (0, 0); | ||
| 779 | + for m in list.iter() { | ||
| 780 | + if info.bmi_mv[2][rf] != *m { | ||
| 781 | + out = *m; | ||
| 782 | + break; | ||
| 783 | + } | ||
| 784 | + } | ||
| 785 | + out | ||
| 786 | + } | ||
| 787 | + } | ||
| 788 | + } | ||
| 789 | + } | ||
| 790 | +} | ||
| 791 | + | ||
| 792 | +/// `read_mv_component` (`vp9_decodemv.c:246`). | ||
| 793 | +fn read_mv_component(r: &mut BoolDecoder, fc: &FrameContext, comp: usize, use_hp: bool) -> i32 { | ||
| 794 | + let dbg = crate::interdump_enabled(); | ||
| 795 | + if dbg { | ||
| 796 | + eprintln!("MVF c{comp} sign"); | ||
| 797 | + } | ||
| 798 | + let sign = r.read_bool(fc.nmv_sign[comp]); | ||
| 799 | + if dbg { | ||
| 800 | + eprintln!("MVF c{comp} cls"); | ||
| 801 | + } | ||
| 802 | + let mv_class = r.read_tree(&MV_CLASS_TREE, &fc.nmv_classes[comp]) as i32; | ||
| 803 | + let class0 = mv_class == 0; | ||
| 804 | + let mut d = 0i32; | ||
| 805 | + let mut mag; | ||
| 806 | + if dbg { | ||
| 807 | + eprintln!("MVF c{comp} class={mv_class}"); | ||
| 808 | + } | ||
| 809 | + if class0 { | ||
| 810 | + if dbg { | ||
| 811 | + eprintln!("MVF c{comp} class0bit"); | ||
| 812 | + } | ||
| 813 | + d = r.read_bool(fc.nmv_class0[comp][0]) as i32; | ||
| 814 | + mag = 0; | ||
| 815 | + } else { | ||
| 816 | + let n = mv_class + CLASS0_BITS - 1; | ||
| 817 | + for i in 0..n { | ||
| 818 | + if dbg { | ||
| 819 | + eprintln!("MVF c{comp} bits[{i}]"); | ||
| 820 | + } | ||
| 821 | + d |= (r.read_bool(fc.nmv_bits[comp][i as usize]) as i32) << i; | ||
| 822 | + } | ||
| 823 | + mag = CLASS0_SIZE << (mv_class + 2); | ||
| 824 | + } | ||
| 825 | + if dbg { | ||
| 826 | + eprintln!("MVF c{comp} fp"); | ||
| 827 | + } | ||
| 828 | + let fr = if class0 { | ||
| 829 | + r.read_tree(&MV_FP_TREE, &fc.nmv_class0_fp[comp][d as usize]) | ||
| 830 | + } else { | ||
| 831 | + r.read_tree(&MV_FP_TREE, &fc.nmv_fp[comp]) | ||
| 832 | + } as i32; | ||
| 833 | + let hp = if use_hp { | ||
| 834 | + if dbg { | ||
| 835 | + eprintln!("MVF c{comp} hp"); | ||
| 836 | + } | ||
| 837 | + r.read_bool(if class0 { | ||
| 838 | + fc.nmv_class0_hp[comp] | ||
| 839 | + } else { | ||
| 840 | + fc.nmv_hp[comp] | ||
| 841 | + }) as i32 | ||
| 842 | + } else { | ||
| 843 | + 1 | ||
| 844 | + }; | ||
| 845 | + mag += ((d << 3) | (fr << 1) | hp) + 1; | ||
| 846 | + if sign { | ||
| 847 | + -mag | ||
| 848 | + } else { | ||
| 849 | + mag | ||
| 850 | + } | ||
| 851 | +} | ||
| 852 | + | ||
| 853 | +/// `read_mv`: returns `(mv, diff)`. | ||
| 854 | +fn read_mv( | ||
| 855 | + r: &mut BoolDecoder, | ||
| 856 | + pred: (i32, i32), | ||
| 857 | + fc: &FrameContext, | ||
| 858 | + allow_hp: bool, | ||
| 859 | +) -> ((i32, i32), (i32, i32)) { | ||
| 860 | + let joint = r.read_tree(&MV_JOINT_TREE, &fc.nmv_joints); | ||
| 861 | + let use_hp = allow_hp && use_mv_hp(pred); | ||
| 862 | + if crate::interdump_enabled() { | ||
| 863 | + eprintln!("MVJ joint={joint} use_hp={use_hp} pred={pred:?}"); | ||
| 864 | + } | ||
| 865 | + // mv_joint_vertical = joint & 2, mv_joint_horizontal = joint & 1. | ||
| 866 | + let mut diff = (0i32, 0i32); | ||
| 867 | + if joint & 2 != 0 { | ||
| 868 | + diff.0 = read_mv_component(r, fc, 0, use_hp); | ||
| 869 | + } | ||
| 870 | + if joint & 1 != 0 { | ||
| 871 | + diff.1 = read_mv_component(r, fc, 1, use_hp); | ||
| 872 | + } | ||
| 873 | + ((pred.0 + diff.0, pred.1 + diff.1), diff) | ||
| 874 | +} | ||
| 875 | + | ||
| 876 | +/// `assign_mv`: returns `(mv, diff)`; only `NEWMV` consumes bits. | ||
| 877 | +fn assign_mv( | ||
| 878 | + r: &mut BoolDecoder, | ||
| 879 | + fc: &FrameContext, | ||
| 880 | + mode: u8, | ||
| 881 | + ref_mv: &[(i32, i32); 2], | ||
| 882 | + near_nearest: &[(i32, i32); 2], | ||
| 883 | + is_compound: usize, | ||
| 884 | + allow_hp: bool, | ||
| 885 | +) -> crate::Result<([(i32, i32); 2], [(i32, i32); 2])> { | ||
| 886 | + let mut mv = [(0, 0); 2]; | ||
| 887 | + let mut diff = [(0, 0); 2]; | ||
| 888 | + match mode { | ||
| 889 | + NEWMV => { | ||
| 890 | + for i in 0..1 + is_compound { | ||
| 891 | + let (m, d) = read_mv(r, ref_mv[i], fc, allow_hp); | ||
| 892 | + mv[i] = m; | ||
| 893 | + diff[i] = d; | ||
| 894 | + } | ||
| 895 | + } | ||
| 896 | + NEARMV | NEARESTMV => mv = *near_nearest, | ||
| 897 | + ZEROMV => {} | ||
| 898 | + other => { | ||
| 899 | + return Err(crate::Error::corrupt(format!( | ||
| 900 | + "VP9 assign_mv: invalid inter mode {other}" | ||
| 901 | + ))) | ||
| 902 | + } | ||
| 903 | + } | ||
| 904 | + Ok((mv, diff)) | ||
| 905 | +} | ||
| 906 | + | ||
| 907 | +/// `read_block_reference_mode`. | ||
| 908 | +fn read_block_reference_mode( | ||
| 909 | + r: &mut BoolDecoder, | ||
| 910 | + c: &InterBlockCtx<'_>, | ||
| 911 | + fc: &FrameContext, | ||
| 912 | + nb: Nb<'_>, | ||
| 913 | +) -> u8 { | ||
| 914 | + if c.ifs.reference_mode == REFERENCE_MODE_SELECT { | ||
| 915 | + let ctx = reference_mode_context(c, nb); | ||
| 916 | + u8::from(r.read_bool(fc.comp_inter[ctx])) | ||
| 917 | + } else { | ||
| 918 | + c.ifs.reference_mode | ||
| 919 | + } | ||
| 920 | +} | ||
| 921 | + | ||
| 922 | +/// `vp9_get_reference_mode_context` (`vp9_pred_common.c`). | ||
| 923 | +fn reference_mode_context(c: &InterBlockCtx<'_>, nb: Nb<'_>) -> usize { | ||
| 924 | + let fixed = c.ifs.comp_fixed_ref; | ||
| 925 | + let has_second = |m: &MiInfo| m.ref_frame[1] > INTRA_FRAME; | ||
| 926 | + match (nb.above, nb.left) { | ||
| 927 | + (Some(a), Some(l)) => { | ||
| 928 | + if !has_second(a) && !has_second(l) { | ||
| 929 | + usize::from((a.ref_frame[0] == fixed) ^ (l.ref_frame[0] == fixed)) | ||
| 930 | + } else if !has_second(a) { | ||
| 931 | + 2 + usize::from(a.ref_frame[0] == fixed || !a.is_inter) | ||
| 932 | + } else if !has_second(l) { | ||
| 933 | + 2 + usize::from(l.ref_frame[0] == fixed || !l.is_inter) | ||
| 934 | + } else { | ||
| 935 | + 4 | ||
| 936 | + } | ||
| 937 | + } | ||
| 938 | + (Some(m), None) | (None, Some(m)) => { | ||
| 939 | + if !has_second(m) { | ||
| 940 | + usize::from(m.ref_frame[0] == fixed) | ||
| 941 | + } else { | ||
| 942 | + 3 | ||
| 943 | + } | ||
| 944 | + } | ||
| 945 | + (None, None) => 1, | ||
| 946 | + } | ||
| 947 | +} | ||
| 948 | + | ||
| 949 | +/// `vp9_get_pred_context_comp_ref_p` (`vp9_pred_common.c`). | ||
| 950 | +fn pred_context_comp_ref_p(c: &InterBlockCtx<'_>, nb: Nb<'_>) -> usize { | ||
| 951 | + let fixed = c.ifs.comp_fixed_ref; | ||
| 952 | + let var = c.ifs.comp_var_ref; | ||
| 953 | + let var_ref_idx = usize::from(!c.sign_bias[fixed as usize]); | ||
| 954 | + let has_second = |m: &MiInfo| m.ref_frame[1] > INTRA_FRAME; | ||
| 955 | + let vr = |m: &MiInfo| -> i8 { | ||
| 956 | + if has_second(m) { | ||
| 957 | + m.ref_frame[var_ref_idx] | ||
| 958 | + } else { | ||
| 959 | + m.ref_frame[0] | ||
| 960 | + } | ||
| 961 | + }; | ||
| 962 | + match (nb.above, nb.left) { | ||
| 963 | + (Some(a), Some(l)) => { | ||
| 964 | + if !a.is_inter && !l.is_inter { | ||
| 965 | + 2 | ||
| 966 | + } else if !a.is_inter || !l.is_inter { | ||
| 967 | + let e = if !a.is_inter { l } else { a }; | ||
| 968 | + if !has_second(e) { | ||
| 969 | + 1 + 2 * usize::from(e.ref_frame[0] != var[1]) | ||
| 970 | + } else { | ||
| 971 | + 1 + 2 * usize::from(e.ref_frame[var_ref_idx] != var[1]) | ||
| 972 | + } | ||
| 973 | + } else { | ||
| 974 | + let a_sg = !has_second(a); | ||
| 975 | + let l_sg = !has_second(l); | ||
| 976 | + let vrfa = vr(a); | ||
| 977 | + let vrfl = vr(l); | ||
| 978 | + if vrfa == vrfl && var[1] == vrfa { | ||
| 979 | + 0 | ||
| 980 | + } else if l_sg && a_sg { | ||
| 981 | + if (vrfa == fixed && vrfl == var[0]) || (vrfl == fixed && vrfa == var[0]) { | ||
| 982 | + 4 | ||
| 983 | + } else if vrfa == vrfl { | ||
| 984 | + 3 | ||
| 985 | + } else { | ||
| 986 | + 1 | ||
| 987 | + } | ||
| 988 | + } else if l_sg || a_sg { | ||
| 989 | + let vrfc = if l_sg { vrfa } else { vrfl }; | ||
| 990 | + let rfs = if a_sg { vrfa } else { vrfl }; | ||
| 991 | + if vrfc == var[1] && rfs != var[1] { | ||
| 992 | + 1 | ||
| 993 | + } else if rfs == var[1] && vrfc != var[1] { | ||
| 994 | + 2 | ||
| 995 | + } else { | ||
| 996 | + 4 | ||
| 997 | + } | ||
| 998 | + } else if vrfa == vrfl { | ||
| 999 | + 4 | ||
| 1000 | + } else { | ||
| 1001 | + 2 | ||
| 1002 | + } | ||
| 1003 | + } | ||
| 1004 | + } | ||
| 1005 | + (Some(e), None) | (None, Some(e)) => { | ||
| 1006 | + if !e.is_inter { | ||
| 1007 | + 2 | ||
| 1008 | + } else if has_second(e) { | ||
| 1009 | + 4 * usize::from(e.ref_frame[var_ref_idx] != var[1]) | ||
| 1010 | + } else { | ||
| 1011 | + 3 * usize::from(e.ref_frame[0] != var[1]) | ||
| 1012 | + } | ||
| 1013 | + } | ||
| 1014 | + (None, None) => 2, | ||
| 1015 | + } | ||
| 1016 | +} | ||
| 1017 | + | ||
| 1018 | +/// `vp9_get_pred_context_single_ref_p1` (`vp9_pred_common.c`). | ||
| 1019 | +fn pred_context_single_ref_p1(nb: Nb<'_>) -> usize { | ||
| 1020 | + let has_second = |m: &MiInfo| m.ref_frame[1] > INTRA_FRAME; | ||
| 1021 | + match (nb.above, nb.left) { | ||
| 1022 | + (Some(a), Some(l)) => { | ||
| 1023 | + if !a.is_inter && !l.is_inter { | ||
| 1024 | + 2 | ||
| 1025 | + } else if !a.is_inter || !l.is_inter { | ||
| 1026 | + let e = if !a.is_inter { l } else { a }; | ||
| 1027 | + if !has_second(e) { | ||
| 1028 | + 4 * usize::from(e.ref_frame[0] == LAST_FRAME) | ||
| 1029 | + } else { | ||
| 1030 | + 1 + usize::from(e.ref_frame[0] == LAST_FRAME || e.ref_frame[1] == LAST_FRAME) | ||
| 1031 | + } | ||
| 1032 | + } else { | ||
| 1033 | + let a2 = has_second(a); | ||
| 1034 | + let l2 = has_second(l); | ||
| 1035 | + if a2 && l2 { | ||
| 1036 | + 1 + usize::from( | ||
| 1037 | + a.ref_frame[0] == LAST_FRAME | ||
| 1038 | + || a.ref_frame[1] == LAST_FRAME | ||
| 1039 | + || l.ref_frame[0] == LAST_FRAME | ||
| 1040 | + || l.ref_frame[1] == LAST_FRAME, | ||
| 1041 | + ) | ||
| 1042 | + } else if a2 || l2 { | ||
| 1043 | + let rfs = if !a2 { a.ref_frame[0] } else { l.ref_frame[0] }; | ||
| 1044 | + let crf1 = if a2 { a.ref_frame[0] } else { l.ref_frame[0] }; | ||
| 1045 | + let crf2 = if a2 { a.ref_frame[1] } else { l.ref_frame[1] }; | ||
| 1046 | + if rfs == LAST_FRAME { | ||
| 1047 | + 3 + usize::from(crf1 == LAST_FRAME || crf2 == LAST_FRAME) | ||
| 1048 | + } else { | ||
| 1049 | + usize::from(crf1 == LAST_FRAME || crf2 == LAST_FRAME) | ||
| 1050 | + } | ||
| 1051 | + } else { | ||
| 1052 | + 2 * usize::from(a.ref_frame[0] == LAST_FRAME) | ||
| 1053 | + + 2 * usize::from(l.ref_frame[0] == LAST_FRAME) | ||
| 1054 | + } | ||
| 1055 | + } | ||
| 1056 | + } | ||
| 1057 | + (Some(e), None) | (None, Some(e)) => { | ||
| 1058 | + if !e.is_inter { | ||
| 1059 | + 2 | ||
| 1060 | + } else if !has_second(e) { | ||
| 1061 | + 4 * usize::from(e.ref_frame[0] == LAST_FRAME) | ||
| 1062 | + } else { | ||
| 1063 | + 1 + usize::from(e.ref_frame[0] == LAST_FRAME || e.ref_frame[1] == LAST_FRAME) | ||
| 1064 | + } | ||
| 1065 | + } | ||
| 1066 | + (None, None) => 2, | ||
| 1067 | + } | ||
| 1068 | +} | ||
| 1069 | + | ||
| 1070 | +/// `vp9_get_pred_context_single_ref_p2` (`vp9_pred_common.c`). | ||
| 1071 | +fn pred_context_single_ref_p2(nb: Nb<'_>) -> usize { | ||
| 1072 | + let has_second = |m: &MiInfo| m.ref_frame[1] > INTRA_FRAME; | ||
| 1073 | + match (nb.above, nb.left) { | ||
| 1074 | + (Some(a), Some(l)) => { | ||
| 1075 | + if !a.is_inter && !l.is_inter { | ||
| 1076 | + 2 | ||
| 1077 | + } else if !a.is_inter || !l.is_inter { | ||
| 1078 | + let e = if !a.is_inter { l } else { a }; | ||
| 1079 | + if !has_second(e) { | ||
| 1080 | + if e.ref_frame[0] == LAST_FRAME { | ||
| 1081 | + 3 | ||
| 1082 | + } else { | ||
| 1083 | + 4 * usize::from(e.ref_frame[0] == GOLDEN_FRAME) | ||
| 1084 | + } | ||
| 1085 | + } else { | ||
| 1086 | + 1 + 2 * usize::from( | ||
| 1087 | + e.ref_frame[0] == GOLDEN_FRAME || e.ref_frame[1] == GOLDEN_FRAME, | ||
| 1088 | + ) | ||
| 1089 | + } | ||
| 1090 | + } else { | ||
| 1091 | + let a2 = has_second(a); | ||
| 1092 | + let l2 = has_second(l); | ||
| 1093 | + if a2 && l2 { | ||
| 1094 | + if a.ref_frame[0] == l.ref_frame[0] && a.ref_frame[1] == l.ref_frame[1] { | ||
| 1095 | + 3 * usize::from( | ||
| 1096 | + a.ref_frame[0] == GOLDEN_FRAME | ||
| 1097 | + || a.ref_frame[1] == GOLDEN_FRAME | ||
| 1098 | + || l.ref_frame[0] == GOLDEN_FRAME | ||
| 1099 | + || l.ref_frame[1] == GOLDEN_FRAME, | ||
| 1100 | + ) | ||
| 1101 | + } else { | ||
| 1102 | + 2 | ||
| 1103 | + } | ||
| 1104 | + } else if a2 || l2 { | ||
| 1105 | + let rfs = if !a2 { a.ref_frame[0] } else { l.ref_frame[0] }; | ||
| 1106 | + let crf1 = if a2 { a.ref_frame[0] } else { l.ref_frame[0] }; | ||
| 1107 | + let crf2 = if a2 { a.ref_frame[1] } else { l.ref_frame[1] }; | ||
| 1108 | + let crf_golden = usize::from(crf1 == GOLDEN_FRAME || crf2 == GOLDEN_FRAME); | ||
| 1109 | + if rfs == GOLDEN_FRAME { | ||
| 1110 | + 3 + crf_golden | ||
| 1111 | + } else if rfs == ALTREF_FRAME { | ||
| 1112 | + crf_golden | ||
| 1113 | + } else { | ||
| 1114 | + 1 + 2 * crf_golden | ||
| 1115 | + } | ||
| 1116 | + } else if a.ref_frame[0] == LAST_FRAME && l.ref_frame[0] == LAST_FRAME { | ||
| 1117 | + 3 | ||
| 1118 | + } else if a.ref_frame[0] == LAST_FRAME || l.ref_frame[0] == LAST_FRAME { | ||
| 1119 | + let edge0 = if a.ref_frame[0] == LAST_FRAME { | ||
| 1120 | + l.ref_frame[0] | ||
| 1121 | + } else { | ||
| 1122 | + a.ref_frame[0] | ||
| 1123 | + }; | ||
| 1124 | + 4 * usize::from(edge0 == GOLDEN_FRAME) | ||
| 1125 | + } else { | ||
| 1126 | + 2 * usize::from(a.ref_frame[0] == GOLDEN_FRAME) | ||
| 1127 | + + 2 * usize::from(l.ref_frame[0] == GOLDEN_FRAME) | ||
| 1128 | + } | ||
| 1129 | + } | ||
| 1130 | + } | ||
| 1131 | + (Some(e), None) | (None, Some(e)) => { | ||
| 1132 | + if !e.is_inter || (e.ref_frame[0] == LAST_FRAME && !has_second(e)) { | ||
| 1133 | + 2 | ||
| 1134 | + } else if !has_second(e) { | ||
| 1135 | + 4 * usize::from(e.ref_frame[0] == GOLDEN_FRAME) | ||
| 1136 | + } else { | ||
| 1137 | + 3 * usize::from(e.ref_frame[0] == GOLDEN_FRAME || e.ref_frame[1] == GOLDEN_FRAME) | ||
| 1138 | + } | ||
| 1139 | + } | ||
| 1140 | + (None, None) => 2, | ||
| 1141 | + } | ||
| 1142 | +} | ||
| 1143 | + | ||
| 1144 | +/// `read_ref_frames`. | ||
| 1145 | +fn read_ref_frames( | ||
| 1146 | + r: &mut BoolDecoder, | ||
| 1147 | + c: &InterBlockCtx<'_>, | ||
| 1148 | + fc: &FrameContext, | ||
| 1149 | + nb: Nb<'_>, | ||
| 1150 | + segment_id: u8, | ||
| 1151 | + out: &mut [i8; 2], | ||
| 1152 | +) { | ||
| 1153 | + if seg_feature(c.seg, segment_id, SEG_LVL_REF_FRAME) { | ||
| 1154 | + out[0] = c.seg.feature_data[segment_id as usize][SEG_LVL_REF_FRAME] as i8; | ||
| 1155 | + out[1] = NO_REF_FRAME; | ||
| 1156 | + return; | ||
| 1157 | + } | ||
| 1158 | + let mode = read_block_reference_mode(r, c, fc, nb); | ||
| 1159 | + if mode == COMPOUND_REFERENCE { | ||
| 1160 | + let idx = usize::from(c.sign_bias[c.ifs.comp_fixed_ref as usize]); | ||
| 1161 | + let ctx = pred_context_comp_ref_p(c, nb); | ||
| 1162 | + let bit = usize::from(r.read_bool(fc.comp_ref[ctx])); | ||
| 1163 | + out[idx] = c.ifs.comp_fixed_ref; | ||
| 1164 | + out[1 - idx] = c.ifs.comp_var_ref[bit]; | ||
| 1165 | + } else { | ||
| 1166 | + let ctx0 = pred_context_single_ref_p1(nb); | ||
| 1167 | + let bit0 = r.read_bool(fc.single_ref[ctx0][0]); | ||
| 1168 | + if bit0 { | ||
| 1169 | + let ctx1 = pred_context_single_ref_p2(nb); | ||
| 1170 | + let bit1 = r.read_bool(fc.single_ref[ctx1][1]); | ||
| 1171 | + out[0] = if bit1 { ALTREF_FRAME } else { GOLDEN_FRAME }; | ||
| 1172 | + } else { | ||
| 1173 | + out[0] = LAST_FRAME; | ||
| 1174 | + } | ||
| 1175 | + out[1] = NO_REF_FRAME; | ||
| 1176 | + } | ||
| 1177 | +} | ||
| 1178 | + | ||
| 1179 | +/// `read_inter_block_mode_info` (`vp9_decodemv.c:702`). | ||
| 1180 | +#[allow(clippy::too_many_arguments)] | ||
| 1181 | +fn read_inter_block_mode_info( | ||
| 1182 | + r: &mut BoolDecoder, | ||
| 1183 | + st: &MiState, | ||
| 1184 | + c: &InterBlockCtx<'_>, | ||
| 1185 | + fc: &FrameContext, | ||
| 1186 | + info: &mut MiInfo, | ||
| 1187 | + nb: Nb<'_>, | ||
| 1188 | + row: usize, | ||
| 1189 | + col: usize, | ||
| 1190 | + partition: u8, | ||
| 1191 | +) -> crate::Result<()> { | ||
| 1192 | + let bsize = info.sb_type; | ||
| 1193 | + let mut best_ref_mvs = [(0, 0); 2]; | ||
| 1194 | + read_ref_frames(r, c, fc, nb, info.segment, &mut info.ref_frame); | ||
| 1195 | + let is_compound = usize::from(info.ref_frame[1] > INTRA_FRAME); | ||
| 1196 | + let inter_mode_ctx = get_mode_context(st, c, bsize, row, col); | ||
| 1197 | + | ||
| 1198 | + if seg_feature(c.seg, info.segment, SEG_LVL_SKIP) { | ||
| 1199 | + info.mode = ZEROMV; | ||
| 1200 | + if bsize < 3 { | ||
| 1201 | + return Err(crate::Error::corrupt( | ||
| 1202 | + "VP9 segment skip feature on a sub-8x8 block", | ||
| 1203 | + )); | ||
| 1204 | + } | ||
| 1205 | + } else if bsize >= 3 { | ||
| 1206 | + info.mode = read_inter_mode(r, fc, inter_mode_ctx); | ||
| 1207 | + } | ||
| 1208 | + | ||
| 1209 | + info.interp_filter = if c.frame_interp == SWITCHABLE { | ||
| 1210 | + let ictx = switchable_interp_ctx(nb); | ||
| 1211 | + if crate::interdump_enabled() { | ||
| 1212 | + eprintln!( | ||
| 1213 | + "SWI ctx={} p0={} p1={}", | ||
| 1214 | + ictx, fc.switchable_interp[ictx][0], fc.switchable_interp[ictx][1] | ||
| 1215 | + ); | ||
| 1216 | + } | ||
| 1217 | + let t = r.read_tree(&SWITCHABLE_INTERP_TREE, &fc.switchable_interp[ictx]); | ||
| 1218 | + if crate::interdump_enabled() { | ||
| 1219 | + eprintln!("SWItype {t}"); | ||
| 1220 | + } | ||
| 1221 | + t | ||
| 1222 | + } else { | ||
| 1223 | + c.frame_interp | ||
| 1224 | + }; | ||
| 1225 | + | ||
| 1226 | + // Block dimensions in 8x8 units for the MV clamp (libvpx's bw/bh from | ||
| 1227 | + // `1 << (bwl - 1)`: a sub-8x8 shape occupies a whole 8x8 cell). | ||
| 1228 | + let bw = if bsize < 3 { | ||
| 1229 | + 1 | ||
| 1230 | + } else { | ||
| 1231 | + 1usize << (B_WIDTH_LOG2_LOOKUP[bsize] as usize - 1) | ||
| 1232 | + }; | ||
| 1233 | + let bh = if bsize < 3 { | ||
| 1234 | + 1 | ||
| 1235 | + } else { | ||
| 1236 | + 1usize << (B_HEIGHT_LOG2_LOOKUP[bsize] as usize - 1) | ||
| 1237 | + }; | ||
| 1238 | + | ||
| 1239 | + if bsize < 3 { | ||
| 1240 | + // PARTITION_VERT clears the wide bit, PARTITION_HORZ the tall one. | ||
| 1241 | + let num_4x4_w = 1usize << u32::from(partition & 2 == 0); | ||
| 1242 | + let num_4x4_h = 1usize << u32::from(partition & 1 == 0); | ||
| 1243 | + let mut got_mv_refs_for_new = false; | ||
| 1244 | + let mut best_sub8x8 = [(0, 0); 2]; | ||
| 1245 | + let mut b_mode = info.mode; | ||
| 1246 | + let mut idy = 0usize; | ||
| 1247 | + while idy < 2 { | ||
| 1248 | + let mut idx = 0usize; | ||
| 1249 | + while idx < 2 { | ||
| 1250 | + let j = idy * 2 + idx; | ||
| 1251 | + b_mode = read_inter_mode(r, fc, inter_mode_ctx); | ||
| 1252 | + info.bmi[j] = b_mode; | ||
| 1253 | + if b_mode == NEARESTMV || b_mode == NEARMV { | ||
| 1254 | + for rf in 0..1 + is_compound { | ||
| 1255 | + best_sub8x8[rf] = append_sub8x8_mvs_for_idx( | ||
| 1256 | + st, c, info, b_mode, j, rf, row, col, bw, bh, | ||
| 1257 | + ); | ||
| 1258 | + } | ||
| 1259 | + } else if b_mode == NEWMV && !got_mv_refs_for_new { | ||
| 1260 | + for rf in 0..1 + is_compound { | ||
| 1261 | + let frame = info.ref_frame[rf]; | ||
| 1262 | + let (_n, list) = | ||
| 1263 | + find_mv_refs(st, c, NEWMV, frame, bsize, row, col, -1, bw, bh); | ||
| 1264 | + let mut mv = list[0]; | ||
| 1265 | + lower_mv_precision(&mut mv, c.allow_hp); | ||
| 1266 | + best_ref_mvs[rf] = mv; | ||
| 1267 | + got_mv_refs_for_new = true; | ||
| 1268 | + } | ||
| 1269 | + } | ||
| 1270 | + let (mv, diff) = assign_mv( | ||
| 1271 | + r, | ||
| 1272 | + fc, | ||
| 1273 | + b_mode, | ||
| 1274 | + &best_ref_mvs, | ||
| 1275 | + &best_sub8x8, | ||
| 1276 | + is_compound, | ||
| 1277 | + c.allow_hp, | ||
| 1278 | + )?; | ||
| 1279 | + info.bmi_mv[j] = mv; | ||
| 1280 | + info.mv_diff = diff; | ||
| 1281 | + if num_4x4_h == 2 { | ||
| 1282 | + info.bmi_mv[j + 2] = info.bmi_mv[j]; | ||
| 1283 | + } | ||
| 1284 | + if num_4x4_w == 2 { | ||
| 1285 | + info.bmi_mv[j + 1] = info.bmi_mv[j]; | ||
| 1286 | + } | ||
| 1287 | + idx += num_4x4_w; | ||
| 1288 | + } | ||
| 1289 | + idy += num_4x4_h; | ||
| 1290 | + } | ||
| 1291 | + info.mode = b_mode; | ||
| 1292 | + info.mv = info.bmi_mv[3]; | ||
| 1293 | + } else { | ||
| 1294 | + if info.mode != ZEROMV { | ||
| 1295 | + for rf in 0..1 + is_compound { | ||
| 1296 | + let frame = info.ref_frame[rf]; | ||
| 1297 | + let (n, list) = find_mv_refs(st, c, info.mode, frame, bsize, row, col, -1, bw, bh); | ||
| 1298 | + let mut mv = list[n - 1]; | ||
| 1299 | + lower_mv_precision(&mut mv, c.allow_hp); | ||
| 1300 | + best_ref_mvs[rf] = mv; | ||
| 1301 | + } | ||
| 1302 | + } | ||
| 1303 | + let (mv, diff) = assign_mv( | ||
| 1304 | + r, | ||
| 1305 | + fc, | ||
| 1306 | + info.mode, | ||
| 1307 | + &best_ref_mvs, | ||
| 1308 | + &best_ref_mvs, | ||
| 1309 | + is_compound, | ||
| 1310 | + c.allow_hp, | ||
| 1311 | + )?; | ||
| 1312 | + info.mv = mv; | ||
| 1313 | + info.mv_diff = diff; | ||
| 1314 | + } | ||
| 1315 | + Ok(()) | ||
| 1316 | +} | ||
crates/ec-vp9/src/lib.rsmodified+33 -0
| @@ -19,6 +19,7 @@ | |||
| 19 | pub mod bool; | 19 | pub mod bool; |
| 20 | pub mod decode; | 20 | pub mod decode; |
| 21 | pub mod header; | 21 | pub mod header; |
| 22 | +pub(crate) mod inter; | ||
| 22 | pub mod intra; | 23 | pub mod intra; |
| 23 | pub mod loopfilter; | 24 | pub mod loopfilter; |
| 24 | pub mod modes; | 25 | pub mod modes; |
| @@ -45,3 +46,35 @@ pub(crate) fn trace_enabled() -> bool { | |||
| 45 | _ => true, | 46 | _ => true, |
| 46 | } | 47 | } |
| 47 | } | 48 | } |
| 49 | + | ||
| 50 | +/// Cached `getenv` probe: `EC_VP9_*` gates are consulted per block, per MV | ||
| 51 | +/// component and per switchable-interp read, and `std::env::var_os` locks std's | ||
| 52 | +/// ENV lock, so each gate resolves once per process and the hot path is a | ||
| 53 | +/// single relaxed atomic load (see `trace_enabled`). | ||
| 54 | +macro_rules! cached_gate { | ||
| 55 | + ($fn_name:ident, $env:literal) => { | ||
| 56 | + pub(crate) fn $fn_name() -> bool { | ||
| 57 | + use std::sync::atomic::{AtomicU8, Ordering}; | ||
| 58 | + static STATE: AtomicU8 = AtomicU8::new(0); | ||
| 59 | + match STATE.load(Ordering::Relaxed) { | ||
| 60 | + 0 => { | ||
| 61 | + let on = std::env::var_os($env).is_some(); | ||
| 62 | + STATE.store(if on { 2 } else { 1 }, Ordering::Relaxed); | ||
| 63 | + on | ||
| 64 | + } | ||
| 65 | + 1 => false, | ||
| 66 | + _ => true, | ||
| 67 | + } | ||
| 68 | + } | ||
| 69 | + }; | ||
| 70 | +} | ||
| 71 | + | ||
| 72 | +// Per-block dump gate for the inter-syntax lane (scratch harnesses); | ||
| 73 | +// a candidate-list dump (`mode == NEWMV` only); | ||
| 74 | +// a search-position cell + grid-store dump; and a post-compressed-header | ||
| 75 | +// MV probability table dump. (Plain comments: a doc comment on a macro | ||
| 76 | +// invocation is an "unused doc comment" warning.) | ||
| 77 | +cached_gate!(interdump_enabled, "EC_VP9_INTERDUMP"); | ||
| 78 | +cached_gate!(mvdbg_enabled, "EC_VP9_MVDGB"); | ||
| 79 | +cached_gate!(mvdbg2_enabled, "EC_VP9_MVDGB2"); | ||
| 80 | +cached_gate!(mvdump_enabled, "EC_VP9_MVDUMP"); | ||
crates/ec-vp9/src/modes.rsmodified+31 -2
| @@ -17,6 +17,21 @@ pub(crate) struct MiInfo { | |||
| 17 | pub uv_mode: u8, | 17 | pub uv_mode: u8, |
| 18 | /// Per-4x4 sub-block y modes, valid when `sb_type < 3` (BLOCK_8X8). | 18 | /// Per-4x4 sub-block y modes, valid when `sb_type < 3` (BLOCK_8X8). |
| 19 | pub bmi: [u8; 4], | 19 | pub bmi: [u8; 4], |
| 20 | + /// `is_inter_block(mi)`: `ref_frame[0] > INTRA_FRAME`. | ||
| 21 | + pub is_inter: bool, | ||
| 22 | + /// `mi->ref_frame[0]`/`[1]`: `INTRA_FRAME` (0), `LAST`/`GOLDEN`/`ALTREF` | ||
| 23 | + /// (1..3), `NO_REF_FRAME` (-1). | ||
| 24 | + pub ref_frame: [i8; 2], | ||
| 25 | + /// `mi->mv[..]`: the refreshed MV (predictor + codeword diff). | ||
| 26 | + pub mv: [(i32, i32); 2], | ||
| 27 | + /// The raw MV codeword diff of each reference frame. | ||
| 28 | + pub mv_diff: [(i32, i32); 2], | ||
| 29 | + /// `mi->bmi[j].as_mv`. | ||
| 30 | + pub bmi_mv: [[(i32, i32); 2]; 4], | ||
| 31 | + /// `mi->seg_id_predicted`. | ||
| 32 | + pub seg_id_predicted: bool, | ||
| 33 | + /// `mi->interp_filter`. | ||
| 34 | + pub interp_filter: u8, | ||
| 20 | } | 35 | } |
| 21 | 36 | ||
| 22 | /// The mode-info grid and the partition contexts of the frame. | 37 | /// The mode-info grid and the partition contexts of the frame. |
| @@ -49,12 +64,17 @@ impl MiState { | |||
| 49 | } | 64 | } |
| 50 | } | 65 | } |
| 51 | 66 | ||
| 67 | + /// The grid cell at an absolute (row, col), `None` when not decoded yet. | ||
| 68 | + pub(crate) fn at(&self, row: usize, col: usize) -> Option<&MiInfo> { | ||
| 69 | + self.grid.get(row * self.mi_cols + col).and_then(|c| c.as_ref()) | ||
| 70 | + } | ||
| 71 | + | ||
| 52 | /// `xd->above_mi` (`set_mi_row_col`, vp9_onyxc_int.h:430): available iff | 72 | /// `xd->above_mi` (`set_mi_row_col`, vp9_onyxc_int.h:430): available iff |
| 53 | /// the block is not on the FRAME's first mi row. libvpx gates on | 73 | /// the block is not on the FRAME's first mi row. libvpx gates on |
| 54 | /// `mi_row != 0`, not on the tile row start — for a tile-row stream the | 74 | /// `mi_row != 0`, not on the tile row start — for a tile-row stream the |
| 55 | /// row above tile row k > 0 was decoded by tile row k - 1 into the same | 75 | /// row above tile row k > 0 was decoded by tile row k - 1 into the same |
| 56 | /// frame-scoped mi grid. | 76 | /// frame-scoped mi grid. |
| 57 | - fn above_mi(&self, row: usize, col: usize) -> Option<&MiInfo> { | 77 | + pub(crate) fn above_mi(&self, row: usize, col: usize) -> Option<&MiInfo> { |
| 58 | if row > 0 { | 78 | if row > 0 { |
| 59 | self.grid[(row - 1) * self.mi_cols + col].as_ref() | 79 | self.grid[(row - 1) * self.mi_cols + col].as_ref() |
| 60 | } else { | 80 | } else { |
| @@ -62,7 +82,7 @@ impl MiState { | |||
| 62 | } | 82 | } |
| 63 | } | 83 | } |
| 64 | 84 | ||
| 65 | - fn left_mi(&self, row: usize, col: usize, tile_col_start: usize) -> Option<&MiInfo> { | 85 | + pub(crate) fn left_mi(&self, row: usize, col: usize, tile_col_start: usize) -> Option<&MiInfo> { |
| 66 | if col > tile_col_start { | 86 | if col > tile_col_start { |
| 67 | self.grid[row * self.mi_cols + col - 1].as_ref() | 87 | self.grid[row * self.mi_cols + col - 1].as_ref() |
| 68 | } else { | 88 | } else { |
| @@ -235,6 +255,15 @@ pub(crate) fn read_intra_frame_mode_info( | |||
| 235 | mode: DC_PRED, | 255 | mode: DC_PRED, |
| 236 | uv_mode: DC_PRED, | 256 | uv_mode: DC_PRED, |
| 237 | bmi: [DC_PRED; 4], | 257 | bmi: [DC_PRED; 4], |
| 258 | + is_inter: false, | ||
| 259 | + ref_frame: [0, -1], | ||
| 260 | + mv: [(0, 0); 2], | ||
| 261 | + mv_diff: [(0, 0); 2], | ||
| 262 | + bmi_mv: [[(0, 0); 2]; 4], | ||
| 263 | + seg_id_predicted: false, | ||
| 264 | + // read_intra_block_mode_info sets this so a later | ||
| 265 | + // get_pred_context_switchable_interp never sees a stale filter. | ||
| 266 | + interp_filter: 3, | ||
| 238 | }; | 267 | }; |
| 239 | match sb_type { | 268 | match sb_type { |
| 240 | 0 => { | 269 | 0 => { |
crates/ec-vp9/src/tables/gen_tables.rsmodified+64 -0
| @@ -1427,3 +1427,67 @@ pub(crate) const KF_UV_MODE_PROB: [u8; 90] = [ | |||
| 1427 | 12, 32, 145, 195, 142, 32, 38, 86, 116, 12, 64, 120, 140, 125, 49, 115, 121, 102, 19, 66, 162, | 1427 | 12, 32, 145, 195, 142, 32, 38, 86, 116, 12, 64, 120, 140, 125, 49, 115, 121, 102, 19, 66, 162, |
| 1428 | 182, 122, 35, 59, 128, | 1428 | 182, 122, 35, 59, 128, |
| 1429 | ]; | 1429 | ]; |
| 1430 | + | ||
| 1431 | +// --------------------------------------------------------------------------- | ||
| 1432 | +// Inter-frame tables (libvpx v1.15: common/vp9_entropymode.c, | ||
| 1433 | +// common/vp9_entropymv.c, common/vp9_mvref_common.h, common/vp9_common_data.c). | ||
| 1434 | +// --------------------------------------------------------------------------- | ||
| 1435 | + | ||
| 1436 | +/// `vp9_inter_mode_tree`; leaves are `mode - NEARESTMV` (0..3). | ||
| 1437 | +pub(crate) const INTER_MODE_TREE: [i8; 6] = [-2, 2, 0, 4, -1, -3]; | ||
| 1438 | +/// `vp9_switchable_interp_tree` = `{ -EIGHTTAP, 2, -EIGHTTAP_SMOOTH, | ||
| 1439 | +/// -EIGHTTAP_SHARP }`. libvpx's filter constants are #defines with | ||
| 1440 | +/// `EIGHTTAP = 0`, `EIGHTTAP_SMOOTH = 1`, `EIGHTTAP_SHARP = 2` | ||
| 1441 | +/// (vp9/common/vp9_filter.h:23) — NOT the spec's naming order, so the tree is | ||
| 1442 | +/// `{0, 2, -1, -2}` and the leaves are the coded `interp_filter` values. | ||
| 1443 | +pub(crate) const SWITCHABLE_INTERP_TREE: [i8; 4] = [0, 2, -1, -2]; | ||
| 1444 | +/// `vp9_mv_joint_tree`. | ||
| 1445 | +pub(crate) const MV_JOINT_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; | ||
| 1446 | +/// `vp9_mv_class_tree`. | ||
| 1447 | +pub(crate) const MV_CLASS_TREE: [i8; 20] = [ | ||
| 1448 | + 0, 2, -1, 4, 6, 8, -2, -3, 10, 12, -4, -5, -6, 14, 16, 18, -7, -8, -9, -10, | ||
| 1449 | +]; | ||
| 1450 | +/// `vp9_mv_fp_tree`. | ||
| 1451 | +pub(crate) const MV_FP_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; | ||
| 1452 | +/// `default_if_y_probs`, `[BLOCK_SIZE_GROUPS][INTRA_MODES - 1]`. | ||
| 1453 | +pub(crate) const DEFAULT_Y_MODE_PROBS: [u8; 36] = [ | ||
| 1454 | + 65, 32, 18, 144, 162, 194, 41, 51, 98, 132, 68, 18, 165, 217, 196, 45, 40, 78, 173, 80, 19, 176, | ||
| 1455 | + 240, 193, 64, 35, 46, 221, 135, 38, 194, 248, 121, 96, 85, 29, | ||
| 1456 | +]; | ||
| 1457 | +/// `default_inter_mode_probs`, `[INTER_MODE_CONTEXTS][INTER_MODES - 1]`. | ||
| 1458 | +pub(crate) const DEFAULT_INTER_MODE_PROBS: [u8; 21] = [ | ||
| 1459 | + 2, 173, 34, 7, 145, 85, 7, 166, 63, 7, 94, 66, 8, 64, 46, 17, 81, 31, 25, 29, 30, | ||
| 1460 | +]; | ||
| 1461 | +/// `default_intra_inter_p`. | ||
| 1462 | +pub(crate) const DEFAULT_INTRA_INTER_PROBS: [u8; 4] = [9, 102, 187, 225]; | ||
| 1463 | +/// `default_comp_inter_p`. | ||
| 1464 | +pub(crate) const DEFAULT_COMP_INTER_PROBS: [u8; 5] = [239, 183, 119, 96, 41]; | ||
| 1465 | +/// `default_single_ref_p`, `[REF_CONTEXTS][2]`. | ||
| 1466 | +pub(crate) const DEFAULT_SINGLE_REF_PROBS: [u8; 10] = | ||
| 1467 | + [33, 16, 77, 74, 142, 142, 172, 170, 238, 247]; | ||
| 1468 | +/// `default_comp_ref_p`, flat `[REF_CONTEXTS]`. | ||
| 1469 | +pub(crate) const DEFAULT_COMP_REF_PROBS: [u8; 5] = [50, 126, 123, 221, 226]; | ||
| 1470 | +/// `default_switchable_interp_prob`, `[SWITCHABLE_FILTER_CONTEXTS][2]`. | ||
| 1471 | +pub(crate) const DEFAULT_SWITCHABLE_INTERP_PROBS: [u8; 8] = [235, 162, 36, 255, 34, 3, 149, 144]; | ||
| 1472 | +/// `default_nmv_context`: joints, then both components. | ||
| 1473 | +pub(crate) const DEFAULT_NMVC_JOINTS: [u8; 3] = [32, 64, 96]; | ||
| 1474 | +pub(crate) const DEFAULT_NMVC_SIGN: [u8; 2] = [128, 128]; | ||
| 1475 | +pub(crate) const DEFAULT_NMVC_CLASSES: [u8; 20] = [ | ||
| 1476 | + 224, 144, 192, 168, 192, 176, 192, 198, 198, 245, 216, 128, 176, 160, 176, 176, 192, 198, 198, | ||
| 1477 | + 208, | ||
| 1478 | +]; | ||
| 1479 | +pub(crate) const DEFAULT_NMVC_CLASS0: [u8; 2] = [216, 208]; | ||
| 1480 | +pub(crate) const DEFAULT_NMVC_BITS: [u8; 20] = [ | ||
| 1481 | + 136, 140, 148, 160, 176, 192, 224, 234, 234, 240, 136, 140, 148, 160, 176, 192, 224, 234, 234, | ||
| 1482 | + 240, | ||
| 1483 | +]; | ||
| 1484 | +pub(crate) const DEFAULT_NMVC_CLASS0_FP: [u8; 12] = | ||
| 1485 | + [128, 128, 64, 96, 112, 64, 128, 128, 64, 96, 112, 64]; | ||
| 1486 | +pub(crate) const DEFAULT_NMVC_FP: [u8; 6] = [64, 96, 64, 64, 96, 64]; | ||
| 1487 | +pub(crate) const DEFAULT_NMVC_CLASS0_HP: [u8; 2] = [160, 160]; | ||
| 1488 | +pub(crate) const DEFAULT_NMVC_HP: [u8; 2] = [128, 128]; | ||
| 1489 | +/// `mode_2_counter`, indexed by `PREDICTION_MODE`. | ||
| 1490 | +pub(crate) const MODE_2_COUNTER: [u8; 14] = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 3, 1]; | ||
| 1491 | +/// `counter_to_context`. | ||
| 1492 | +pub(crate) const COUNTER_TO_CONTEXT: [u8; 19] = | ||
| 1493 | + [2, 3, 4, 1, 3, 9, 0, 9, 9, 5, 5, 9, 5, 9, 9, 9, 9, 9, 6]; | ||
crates/ec-vp9/src/tokens.rsmodified+6 -4
| @@ -51,7 +51,8 @@ pub(crate) struct CoeffBlock { | |||
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | /// `decode_coefs`, verbatim port. `dq` is `[dc, ac]`; plane 0 is luma, | 53 | /// `decode_coefs`, verbatim port. `dq` is `[dc, ac]`; plane 0 is luma, |
| 54 | -/// chroma is 1; the ref axis is fixed at 0 (intra). | 54 | +/// chroma is 1. `is_inter` selects the coefficient-probability ref axis |
| 55 | +/// (libvpx `get_coef_probs`: `coef_probs[tx][plane][ref]`). | ||
| 55 | pub(crate) fn decode_coefs( | 56 | pub(crate) fn decode_coefs( |
| 56 | r: &mut BoolDecoder, | 57 | r: &mut BoolDecoder, |
| 57 | plane_type: usize, | 58 | plane_type: usize, |
| @@ -61,6 +62,7 @@ pub(crate) fn decode_coefs( | |||
| 61 | scan: &[i16], | 62 | scan: &[i16], |
| 62 | nb: &[i16], | 63 | nb: &[i16], |
| 63 | probs4d: &[[[[[u8; 3]; 6]; 6]; 2]; 2], | 64 | probs4d: &[[[[[u8; 3]; 6]; 6]; 2]; 2], |
| 65 | + is_inter: bool, | ||
| 64 | #[allow(unused_variables)] pos: (usize, usize), | 66 | #[allow(unused_variables)] pos: (usize, usize), |
| 65 | #[allow(unused_variables)] plane: usize, | 67 | #[allow(unused_variables)] plane: usize, |
| 66 | ) -> CoeffBlock { | 68 | ) -> CoeffBlock { |
| @@ -74,7 +76,7 @@ pub(crate) fn decode_coefs( | |||
| 74 | let dq_shift = usize::from(tx_size == TX_32X32); | 76 | let dq_shift = usize::from(tx_size == TX_32X32); |
| 75 | let mut dqv = dequant[0] as i32; | 77 | let mut dqv = dequant[0] as i32; |
| 76 | let mut band = band_at(tx_size, c); | 78 | let mut band = band_at(tx_size, c); |
| 77 | - let mut prob: &[u8; 3] = &probs4d[plane_type][0][band][ctx]; | 79 | + let mut prob: &[u8; 3] = &probs4d[plane_type][usize::from(is_inter)][band][ctx]; |
| 78 | if std::env::var_os("EC_VP9_DBG").is_some() && plane_type == 0 && ctx == 0 && band == 0 { | 80 | if std::env::var_os("EC_VP9_DBG").is_some() && plane_type == 0 && ctx == 0 && band == 0 { |
| 79 | eprintln!("DBG coefs tx={tx_size} dq={dequant:?} ctx={ctx} band={band} prob={prob:?}"); | 81 | eprintln!("DBG coefs tx={tx_size} dq={dequant:?} ctx={ctx} band={band} prob={prob:?}"); |
| 80 | } | 82 | } |
| @@ -95,7 +97,7 @@ pub(crate) fn decode_coefs( | |||
| 95 | } | 97 | } |
| 96 | ctx = get_coef_context(nb, &token_cache, c); | 98 | ctx = get_coef_context(nb, &token_cache, c); |
| 97 | band = band_at(tx_size, c); | 99 | band = band_at(tx_size, c); |
| 98 | - prob = &probs4d[plane_type][0][band][ctx]; | 100 | + prob = &probs4d[plane_type][usize::from(is_inter)][band][ctx]; |
| 99 | } | 101 | } |
| 100 | let mut v: i32; | 102 | let mut v: i32; |
| 101 | if r.read_bool(prob[2]) { | 103 | if r.read_bool(prob[2]) { |
| @@ -142,7 +144,7 @@ pub(crate) fn decode_coefs( | |||
| 142 | if c < max_eob { | 144 | if c < max_eob { |
| 143 | ctx = get_coef_context(nb, &token_cache, c); | 145 | ctx = get_coef_context(nb, &token_cache, c); |
| 144 | band = band_at(tx_size, c); | 146 | band = band_at(tx_size, c); |
| 145 | - prob = &probs4d[plane_type][0][band][ctx]; | 147 | + prob = &probs4d[plane_type][usize::from(is_inter)][band][ctx]; |
| 146 | } | 148 | } |
| 147 | dqv = dequant[1] as i32; | 149 | dqv = dequant[1] as i32; |
| 148 | } | 150 | } |
crates/ec-vp9/tests/ivf.rsmodified+1 -0
| @@ -49,6 +49,7 @@ pub fn parse_ivf(bytes: &[u8]) -> (&str, u16, u16, Vec<IvfFrame>) { | |||
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | /// Root of the workspace checkout this test runs in (crates/ec-vp9/../..). | 51 | /// Root of the workspace checkout this test runs in (crates/ec-vp9/../..). |
| 52 | +#[allow(dead_code)] // shared by every test binary that does `mod ivf` | ||
| 52 | pub fn fixture_dir() -> Option<std::path::PathBuf> { | 53 | pub fn fixture_dir() -> Option<std::path::PathBuf> { |
| 53 | let bitstreams = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | 54 | let bitstreams = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) |
| 54 | .join("../../fixtures/bitstreams"); | 55 | .join("../../fixtures/bitstreams"); |
crates/ec-vp9/tests/scratch_interdump.rsadded+80 -0
| @@ -0,0 +1,80 @@ | |||
| 1 | +//! [scratch] Inter-frame syntax dump + count gate. | ||
| 2 | +//! | ||
| 3 | +//! Parses every frame of `INTER_IVF` (default `/tmp/inter1.ivf`, the clean | ||
| 4 | +//! single-tile stream the pinned counts refer to). Keyframes decode as usual; | ||
| 5 | +//! inter frames are parsed syntax-only (no reconstruction). With | ||
| 6 | +//! `EC_VP9_INTERDUMP=1` every block prints one `MODE` line in the oracle's | ||
| 7 | +//! shape, for a by-index diff. Point it at the two-tile-column stream | ||
| 8 | +//! (`INTER_IVF=/tmp/inter.ivf INTER_EXPECT=none`) to exercise the failing- | ||
| 9 | +//! keyframe path: that stream's keyframe desyncs its tile reader and the test | ||
| 10 | +//! is expected to fail there. | ||
| 11 | +//! | ||
| 12 | +//! This is a real gate, not a printer: | ||
| 13 | +//! - any frame error fails the test (keyframe failures are propagated too, | ||
| 14 | +//! because a half-updated frame context would make later dumps meaningless); | ||
| 15 | +//! - every inter frame's mode-info block count must equal `INTER_EXPECT` | ||
| 16 | +//! (default `1213,1851` - the pinned counts of the single-tile fixture | ||
| 17 | +//! `/tmp/inter1.ivf`, sha256 376532734662ba5faba4c4e97a5aac915ce429c11701bcf6 | ||
| 18 | +//! 6439b61a77388fa8; set `INTER_EXPECT=none` for a different stream); | ||
| 19 | +//! - the crate already asserts `overreads() == 0` per tile reader at the end of | ||
| 20 | +//! each tile (decode.rs, "tile bool decoder desync"), which is what would fire | ||
| 21 | +//! if the walk read past the tile's last bool. | ||
| 22 | +//! | ||
| 23 | +//! ```text | ||
| 24 | +//! INTER_IVF=<file> EC_VP9_INTERDUMP=1 \ | ||
| 25 | +//! cargo test -p ec-vp9 --test scratch_interdump -- --nocapture | ||
| 26 | +//! ``` | ||
| 27 | +mod ivf; | ||
| 28 | + | ||
| 29 | +use ec_vp9::decode::Decoder; | ||
| 30 | + | ||
| 31 | +#[test] | ||
| 32 | +fn inter_syntax_dump() { | ||
| 33 | + // An explicitly set INTER_IVF that does not exist is a failure; only the | ||
| 34 | + // unset default may fall back to a loud SKIP (the fixture is generated, | ||
| 35 | + // not committed). | ||
| 36 | + let explicit = std::env::var("INTER_IVF").ok(); | ||
| 37 | + let path = explicit | ||
| 38 | + .clone() | ||
| 39 | + .unwrap_or_else(|| "/tmp/inter1.ivf".to_string()); | ||
| 40 | + let expect_raw = std::env::var("INTER_EXPECT").unwrap_or_else(|_| "1213,1851".to_string()); | ||
| 41 | + let expect: Vec<usize> = if expect_raw == "none" { | ||
| 42 | + Vec::new() | ||
| 43 | + } else { | ||
| 44 | + expect_raw | ||
| 45 | + .split(',') | ||
| 46 | + .map(|t| t.trim().parse().expect("INTER_EXPECT entries are block counts")) | ||
| 47 | + .collect() | ||
| 48 | + }; | ||
| 49 | + let Ok(bytes) = std::fs::read(&path) else { | ||
| 50 | + assert!( | ||
| 51 | + explicit.is_none(), | ||
| 52 | + "INTER_IVF={path} was set explicitly but the file does not exist" | ||
| 53 | + ); | ||
| 54 | + println!("SKIP: fixture {path} not found - set INTER_IVF=<file> to run"); | ||
| 55 | + return; | ||
| 56 | + }; | ||
| 57 | + let (_fourcc, w, h, frames) = ivf::parse_ivf(&bytes); | ||
| 58 | + println!("IVF {w}x{h} frames={}", frames.len()); | ||
| 59 | + let mut dec = Decoder::new(); | ||
| 60 | + let mut counts = Vec::new(); | ||
| 61 | + for (i, f) in frames.iter().enumerate() { | ||
| 62 | + match dec.decode_syntax(&f.data) { | ||
| 63 | + Ok(()) => { | ||
| 64 | + let n = dec.last_frame_blocks(); | ||
| 65 | + println!("FRAME {i} parsed blocks={n}"); | ||
| 66 | + if i > 0 { | ||
| 67 | + counts.push(n); | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + Err(e) => panic!("FRAME {i} failed: {e:?}"), | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + if !expect.is_empty() { | ||
| 74 | + assert_eq!( | ||
| 75 | + counts, expect, | ||
| 76 | + "inter-frame mode-info block counts differ from INTER_EXPECT" | ||
| 77 | + ); | ||
| 78 | + println!("block-count gate passed for {counts:?}"); | ||
| 79 | + } | ||
| 80 | +} | ||
lanes/vp9inter.report.mdadded+351 -0
| @@ -0,0 +1,351 @@ | |||
| 1 | +# lane-vp9inter report — inter-frame SYNTAX, slice 1 (`ec-vp9`) | ||
| 2 | + | ||
| 3 | +Worktree `/home/user/Documents/Code/Rust/edith_codecs-inter` (detached `43c9c913`). | ||
| 4 | +No commit, no push, no fmt. Reconstruction / motion compensation: **NOT implemented, | ||
| 5 | +`inter_is_named_unsupported` still refuses by name.** | ||
| 6 | + | ||
| 7 | +## Status: SYNTAX LAYER BIT-EXACT on two inter frames (2026-09-17, slice 5) — | ||
| 8 | +## frame 1 and frame 2 each match libvpx block-for-block on every comparable | ||
| 9 | +## field (1213/1213 and 1851/1851), which required three fixes in the mode-info | ||
| 10 | +## layer (below) on top of the earlier entropy-layer ones. Pixels are still | ||
| 11 | +## refused: reconstruction/motion compensation remains out of scope. | ||
| 12 | + | ||
| 13 | +## History: THREE defects found, fixed and verified: the compressed header | ||
| 14 | +## is bit-identical to libvpx (1200/1200 bools counting the reader marker and the | ||
| 15 | +## tx-mode literals) and the tile walk now matches the oracle for its first 2628 | ||
| 16 | +## bools. Block (0,0) of frame 1 is equivalent to the oracle on every comparable | ||
| 17 | +## field (row/col/bsize/tx/mode/skip/seg/ref0/ref1/mv/interp; `uv` is libvpx-stale | ||
| 18 | +## by design). The walk still stops early: 1002 of 1213 blocks, desync after tile | ||
| 19 | +## bool #2628 (cause 4 below, not yet diagnosed). | ||
| 20 | + | ||
| 21 | +## What is implemented (all in `crates/ec-vp9`) | ||
| 22 | + | ||
| 23 | +- `src/inter.rs` (new): `read_inter_frame_mode_info` — inter segment id (incl. | ||
| 24 | + temporal-update prediction), skip, `read_is_inter_block`, tx size with the | ||
| 25 | + `allow_select = !skip || !inter` rule, reference frames (single + compound | ||
| 26 | + paths, `single_ref_p1/p2`, `comp_ref_p`, `reference_mode_context` contexts | ||
| 27 | + transcribed from `vp9_pred_common.c`), inter mode, switchable interp filter, | ||
| 28 | + the sub-8x8 `bmi` path, `read_mv`/`read_mv_component` with the | ||
| 29 | + `use_hp = allow_hp && use_mv_hp(predictor)` gate, and the MV predictor | ||
| 30 | + (`dec_find_mv_refs`, `mv_ref_blocks`, `append_sub8x8_mvs_for_idx`, | ||
| 31 | + `lower_mv_precision`, `clamp_mv_ref`, `mode_2_counter`/`counter_to_context`). | ||
| 32 | + MVs are recorded both as the refreshed MV and as the raw codeword diff. | ||
| 33 | +- `src/header.rs`: `FrameContext` gained the inter tables (`inter_mode`, | ||
| 34 | + `switchable_interp`, `intra_inter`, `comp_inter`, `single_ref`, `comp_ref`, | ||
| 35 | + `y_mode`, `uv_mode`, the MV prob components) with libvpx defaults, and | ||
| 36 | + `read_compressed_header` gained the inter branch in libvpx order: | ||
| 37 | + inter-mode probs, switchable-interp probs, `intra_inter`, `read_frame_reference_mode` | ||
| 38 | + (compound-allowed gate), reference-mode probs, y-mode probs, partition probs, | ||
| 39 | + `read_mv_probs` (joints → per-component sign/classes/class0/bits → class0_fp/fp | ||
| 40 | + → hp when `allow_high_precision_mv`). It now returns | ||
| 41 | + `CompressedHeader { tx_mode, reference_mode }`. | ||
| 42 | +- `src/decode.rs`: `Decoder::decode_syntax` (keyframes decode through to pixels, | ||
| 43 | + inter frames are parsed with no picture), the inter tile walk (same tile | ||
| 44 | + geometry/BE size prefixes as the keyframe walk), the frame-context rules | ||
| 45 | + (`reset_frame_context` 2/3, `refresh_frame_context` save, `use_prev_frame_mvs` | ||
| 46 | + gate), the per-8x8 `MV_REF` field and segment map for the next frame, and a | ||
| 47 | + token-only block walk (`decode_inter_block_tokens`) that keeps the entropy | ||
| 48 | + stream aligned without any prediction or residual add. | ||
| 49 | +- `src/tokens.rs`: `decode_coefs` gained the `is_inter` ref-axis selector | ||
| 50 | + (`coef_probs[tx][plane][ref]`), keyframe call sites pass `false`. | ||
| 51 | +- `tests/scratch_interdump.rs` (new): parses every frame of `INTER_IVF` and, with | ||
| 52 | + `EC_VP9_INTERDUMP=1`, prints one oracle-shaped `MODE` line per block. | ||
| 53 | + | ||
| 54 | +Gates: | ||
| 55 | +- `cargo test -p ec-vp9 --test keyframe_exact` → **4/4 PASS** | ||
| 56 | + (`keyframes_match_ffmpeg`, `lossless_64_matches_ffmpeg`, | ||
| 57 | + `profile1_444_is_named_unsupported`, `inter_is_named_unsupported`). | ||
| 58 | +- `cargo check -p ec-vp9` → **exactly the 5 baseline warnings** | ||
| 59 | + (`TM_PRED`, `read_partition`, `partition_probs`, `x_mis`, `y_mis`). | ||
| 60 | +- `cargo test -p ec-vp9` → **green end to end** (14 test binaries, 0 failures). | ||
| 61 | +- `cargo test -p ec-vp9 --test scratch_interdump` → **a real gate**: any frame | ||
| 62 | + error fails it (keyframe errors now propagate) and every inter frame's | ||
| 63 | + mode-info block count must equal `INTER_EXPECT` (default `1213,1851`, the | ||
| 64 | + pinned counts of the fixture; `INTER_EXPECT=none` disables). | ||
| 65 | + `INTER_IVF` defaults to `/tmp/inter1.ivf` (the clean single-tile stream the | ||
| 66 | + counts refer to); when that file is absent the test **skips loudly** | ||
| 67 | + (`SKIP: fixture ... not found`, the `scratch_synth` convention) so a machine | ||
| 68 | + without /tmp fixtures stays green. Current output: `FRAME 1 parsed blocks=1213`, | ||
| 69 | + `FRAME 2 parsed blocks=1851`, `block-count gate passed for [1213, 1851]`. | ||
| 70 | + The failing-keyframe path stays reachable by pointing it at the two-tile-column | ||
| 71 | + stream: `INTER_IVF=/tmp/inter.ivf INTER_EXPECT=none cargo test -p ec-vp9 | ||
| 72 | + --test scratch_interdump` → expected failure at `FRAME 0 failed: Corrupt { | ||
| 73 | + context: "vp9: tile bool decoder desync" }` (instrumentation for the | ||
| 74 | + multi-tile keyframe defect). | ||
| 75 | +- Overread gate: the crate already asserts `overreads() == 0` per tile reader at | ||
| 76 | + the end of each tile (`decode.rs:416` keyframe walk, `decode.rs:809` inter | ||
| 77 | + walk, message "tile bool decoder desync"), which fires if a walk reads past a | ||
| 78 | + tile's last bool - that is exactly the error `/tmp/inter.ivf` (2 tile columns) | ||
| 79 | + now reports for its keyframe. An earlier statement in this report that the | ||
| 80 | + crate's only overread assertion was the `bool.rs` unit test was wrong. | ||
| 81 | +- Knob-free silence: with no `EC_VP9_*` set, the same harness run emits **0** | ||
| 82 | + trace lines (grep over the twelve trace prefixes) and still passes the gate. | ||
| 83 | + | ||
| 84 | +## Landing fixes (post-verdict) | ||
| 85 | + | ||
| 86 | +1. **`clamp_mv_ref` had row and column swapped** (blocking-queue item 1). The | ||
| 87 | + four edges were computed correctly but applied to the wrong components: | ||
| 88 | + `mv.0` is the ROW (filled from the vertical joint component in `read_mv`) | ||
| 89 | + and was clamped with the left/right edges. libvpx's | ||
| 90 | + `clamp_mv(mv, min_col, max_col, min_row, max_row)` (`vp9_mv.h:47`) takes the | ||
| 91 | + COLUMN bounds first, and `clamp_mv_ref` passes | ||
| 92 | + `(mb_to_left_edge - MV_BORDER, mb_to_right_edge + MV_BORDER, | ||
| 93 | + mb_to_top_edge - MV_BORDER, mb_to_bottom_edge + MV_BORDER)` | ||
| 94 | + (`vp9_mvref_common.h:215`). Fixed by clamping `mv.1` with left/right and | ||
| 95 | + `mv.0` with top/bottom. Latent on this fixture (max |mv| 334) - the two | ||
| 96 | + frames still match 100% afterwards - but on edge-heavy content the wrong | ||
| 97 | + bound would move a predictor, flip `use_mv_hp` and desync the stream. | ||
| 98 | +2. **`decode_syntax` no longer swallows keyframe failures.** It propagated the | ||
| 99 | + error only as a `KFAIL` print under `EC_VP9_INTERDUMP`, so a stream whose | ||
| 100 | + keyframe died mid-walk returned success with a half-updated frame context, | ||
| 101 | + prev-frame MV field and reference slots - and every later inter-frame dump was | ||
| 102 | + meaningless evidence. Now the error propagates (`decode_key_frame(..)?`), so | ||
| 103 | + `/tmp/inter.ivf` fails fast with | ||
| 104 | + `FRAME 0 failed: Corrupt { context: "vp9: tile bool decoder desync" }`. | ||
| 105 | +3. **The harness is a gate, not a printer** (see the Gates list). Its block | ||
| 106 | + counts come from `Decoder::last_frame_blocks()`, captured in | ||
| 107 | + `decode_inter_syntax_frame` right before the walk state is consumed. | ||
| 108 | +4. **All `EC_VP9_*` dumps now go through cached `OnceLock`/atomic gates** | ||
| 109 | + (`lib.rs`: `cached_gate!` for `EC_VP9_INTERDUMP`, `EC_VP9_MVDGB`, | ||
| 110 | + `EC_VP9_MVDGB2`, `EC_VP9_MVDUMP`), so the per-block, per-MV-component and | ||
| 111 | + per-switchable-interp checks are a relaxed atomic load instead of a | ||
| 112 | + `std::env::var_os` (stdlib ENV lock) on the decode path. | ||
| 113 | + | ||
| 114 | +## Streams | ||
| 115 | + | ||
| 116 | +- `/tmp/inter.ivf` — a real 1920x1080 clip, `-frames:v 3 -c:v libvpx-vp9 -crf 32`: | ||
| 117 | + keyframe + 2 inter frames, **2 tile columns**, `allow_hp=1`, `interp=SWITCHABLE`, | ||
| 118 | + `tx_mode=SELECT`, frame-level `reference_mode=SINGLE`, `reset_frame_context=1`, | ||
| 119 | + `refresh_frame_context=0` for all three frames (so frames 1/2 start from the | ||
| 120 | + default frame context — verified against the oracle's `CHSUM`). | ||
| 121 | +- `/tmp/inter1.ivf` — **the stream every match count in this report refers to** | ||
| 122 | + (the single-tile control, and the one driving the frame-1/2 MODE diffs, the | ||
| 123 | + bool traces and the candidate probes). Generated with | ||
| 124 | + `ffmpeg -v error -y -i <a real 1920x1080 clip> -frames:v 3 -c:v libvpx-vp9 | ||
| 125 | + -crf 32 -pix_fmt yuv420p -tile-columns 0 -f ivf /tmp/inter1.ivf`; | ||
| 126 | + the exact file used here is `sha256 | ||
| 127 | + 376532734662ba5faba4c4e97a5aac915ce429c11701bcf66439b61a77388fa8`, 161579 bytes, | ||
| 128 | + VP9 1920x1080, 3 frames (keyframe + 2 inter), which is what the counts are tied | ||
| 129 | + to - a different clip or encoder setting gives different block counts, so a | ||
| 130 | + third party reproducing the numbers should use this file (or regenerate from | ||
| 131 | + the same source) and check the hash. | ||
| 132 | + Consumers: | ||
| 133 | + `BTRACE=1 MODEDUMP=1 MVDUMP=1 /tmp/vp9loss/drv /tmp/inter1.ivf` (oracle) and | ||
| 134 | + `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-inter INTER_IVF=/tmp/inter1.ivf | ||
| 135 | + EC_VP9_INTERDUMP=1 EC_VP9_TRACE=1 cargo test -p ec-vp9 --test scratch_interdump | ||
| 136 | + -- --nocapture` (ours). | ||
| 137 | + | ||
| 138 | +## Match counts (oracle `MODE` vs our `MODE`, by index, fields | ||
| 139 | +## row/col/bsize/tx/mode/skip/seg/ref0/ref1/mvrow/mvcol/interp) | ||
| 140 | + | ||
| 141 | +| stage | frame 1 ours/oracle | first divergence | | ||
| 142 | +|---|---|---|---| | ||
| 143 | +| before any fix (single tile) | 551 / 1213 (desync) | block #0, `bsize` (ours 10 vs 12) | | ||
| 144 | +| after cause 1 (`update_mv_probs`) | 794 / 1213 (desync) | block #0, `bsize` | | ||
| 145 | +| after cause 2 (kf partition leak) | **917 / 1213 (desync)** | block #0 compares EQUAL on row/col/bsize/tx/mode/skip/seg/ref0/ref1/mv (`uv` is libvpx-stale, `if` is cause 3) | | ||
| 146 | +| frame 2 | never reached (frame 1 aborts) | – | | ||
| 147 | + | ||
| 148 | +First divergent decision, single-tile stream, block #0 == (0,0) 64x64: | ||
| 149 | + | ||
| 150 | +``` | ||
| 151 | +ours = row 0 col 0 bsize 11 (64x32) tx 3 mode 10 (NEARESTMV) skip 0 ref0 2 mv (0,0) | ||
| 152 | +oracle = row 0 col 0 bsize 12 (64x64) tx 3 mode 13 (NEWMV) skip 1 ref0 1 mv (3,0) | ||
| 153 | +``` | ||
| 154 | + | ||
| 155 | +## Bool index of the divergence (the decisive evidence) | ||
| 156 | + | ||
| 157 | +Instrumentation used: oracle `MODEDUMP` now also prints `ref0/ref1/mvrow/mvcol/if`, | ||
| 158 | +`MVRAW` prints each `read_mv` diff, and `CHSUM` prints | ||
| 159 | +`frame tx_mode ref_mode allow_hp interp fcx refresh_ctx reset_ctx`; our side prints | ||
| 160 | +`CHSTART frame=N` and `TILE <len> bytes frame=N` markers so `EC_VP9_TRACE` bools can | ||
| 161 | +be attributed to a frame and a section. | ||
| 162 | + | ||
| 163 | +- **Compressed header, frame 1 (RESOLVED):** before the fixes our reads diverged at | ||
| 164 | + bool **#1116**: `ours (prob,bit)=(252,0)` vs `oracle (128,0)`, totals **1184 vs 1200**. | ||
| 165 | + Section tables (markers `SECT <name>` on ours, `CHSEC <name>` in the oracle, | ||
| 166 | + bool index counted between markers; ours' window is 4 bools ahead of the oracle's | ||
| 167 | + because the oracle's starts at its first marker): | ||
| 168 | + `txmode 0/22 | txprobs 22/856 | coef 878/18 | skip 896/32 | inter_mode 896/32 | | ||
| 169 | + switchable 928/24 | intra_inter 952/4 | ref_mode_bits 956/0 | ref_mode_probs 956/15 | | ||
| 170 | + y_mode 971/36 | partition 1007/99 | mv_probs 1106/74(ours) vs 1106/90(oracle)`. | ||
| 171 | + Every section entry and count matched except the last: the divergence is inside | ||
| 172 | + `read_mv_probs` (span 74 vs 90). After the two fixes below the CH is | ||
| 173 | + **bit-identical: 1196 == 1196 bools, no divergence**. | ||
| 174 | +- **First tile symbol, frame 1:** after the tile reader's marker bit (both sides | ||
| 175 | + `(128,0)`), the partition read differs: ours `(191,1)` → PARTITION_HORZ, | ||
| 176 | + oracle `(239,0)` → PARTITION_NONE. Different *prob and* different bit ⇒ the | ||
| 177 | + frame probability state is already wrong at the tile start, which is exactly | ||
| 178 | + what the CH divergence predicts. Our walk then stays self-consistent for 551 | ||
| 179 | + blocks and finally over-reads the 1717-byte tile ("tile bool decoder desync"). | ||
| 180 | + | ||
| 181 | +So the first wrong decision is `compressed_header`, bool #1116 — the mode-info, | ||
| 182 | +MV and token layers below it are unaudited until that is fixed. | ||
| 183 | + | ||
| 184 | +## Named causes (evidence, not speculation) | ||
| 185 | + | ||
| 186 | +1. **FIXED — `update_mv_probs` is NOT `vp9_diff_update_prob`** (`vp9_decodeframe.c:136`): | ||
| 187 | + `if (vpx_read(r, MV_UPDATE_PROB)) p[i] = (vpx_read_literal(r, 7) << 1) | 1;` | ||
| 188 | + — a 252-prob flag, then seven literal bits, and the new prob is `(literal << 1) | 1`; | ||
| 189 | + no `decode_term_subexp`, no `inv_remap_prob`. Our mv probs used `diff_update_prob` | ||
| 190 | + for all 69 entries, hence the 16-bool deficit and the (252,0)-vs-(128,0) divergence. | ||
| 191 | + Fix: `fn update_mv_probs(r, &mut [u8])` + all mv loops switched. Evidence of the | ||
| 192 | + fix: the oracle's mv span 90 == ours 90, CH totals equal, PUPD-equivalent traces equal. | ||
| 193 | + | ||
| 194 | +2. **FIXED — keyframe partition probs leaked into the stored frame context.** | ||
| 195 | + libvpx's keyframe partition table is the CONST `vp9_kf_partition_probs`, selected | ||
| 196 | + by `get_partition_probs()` for intra-only frames — it is NOT a `FRAME_CONTEXT` | ||
| 197 | + field. Our `FrameContext::new(true)` baked it into the context and we stored that | ||
| 198 | + context in `frame_ctxs` at every keyframe, so frame 1 started with the keyframe | ||
| 199 | + table (`ours ctx0=[158,97,94]` vs `oracle [199,122,141]`; 44/48 entries wrong — | ||
| 200 | + same bits read, different values, which is why the section tables looked clean). | ||
| 201 | + Fix: stored contexts are always `FrameContext::new(false)`; the active keyframe | ||
| 202 | + copy keeps `new(true)`; `FrameContext::reset_inter_partition()` runs before a | ||
| 203 | + keyframe context save. Evidence: `PPFLAT_PRE` on both sides now equal, and the | ||
| 204 | + 48-entry `PUPD j i before after` traces are IDENTICAL. | ||
| 205 | + | ||
| 206 | +3. **FIXED — `SWITCHABLE_INTERP_TREE` was built from the SPEC's naming order, not | ||
| 207 | + libvpx's.** `vp9/common/vp9_filter.h:23-28` are `#define`s, not an enum: | ||
| 208 | + `EIGHTTAP 0`, `EIGHTTAP_SMOOTH 1`, `EIGHTTAP_SHARP 2` (the spec's `literal_to_filter` | ||
| 209 | + names the *same coded values* in a different order). The tree | ||
| 210 | + `{ -EIGHTTAP, 2, -EIGHTTAP_SMOOTH, -EIGHTTAP_SHARP }` is therefore | ||
| 211 | + `{0, 2, -1, -2}`, not `{-1, 2, 0, -2}`; with the wrong table our interp leaf came | ||
| 212 | + out 0 where libvpx gives 1, which poisoned the *next* block's | ||
| 213 | + `get_pred_context_switchable_interp` (left neighbour's filter is the context). | ||
| 214 | + Evidence after the fix: both sides print `SWI ctx=3 p0=87 p1=252` / `SWItype 1` | ||
| 215 | + for block (0,0) and `SWI ctx=1 p0=19 p1=255` / `SWItype 1` for (0,8); block (0,0) | ||
| 216 | + now matches the oracle on `interp` too. | ||
| 217 | + **Reader-counter evidence** (oracle `g_bool_count` reset at the compressed header, | ||
| 218 | + printed as `bools=` in its `SWI`/`SWItype` lines; ours = CH total + tile bool index | ||
| 219 | + at the same print, both counting the tile reader marker and the tx-mode literals): | ||
| 220 | + `ours 1208 / 1210 / 1221 / 1223 / 1229 / 1231` vs | ||
| 221 | + `oracle 1208 / 1210 / 1225 / 1227 / 1233 / 1235` — the first two agree exactly, so | ||
| 222 | + the two walks are bool-aligned through block (0,0)'s interp read; the later pairs | ||
| 223 | + differ because the walks diverge at #2628 (below). | ||
| 224 | + | ||
| 225 | +4. **FIXED — the tile desync was a transposed `mv_ref_blocks` lookup (POSITION is | ||
| 226 | + `{ row, col }`, our accessors added `p.0` to the column), plus two smaller | ||
| 227 | + defects found in its wake.** All three are in `crates/ec-vp9/src/inter.rs`. | ||
| 228 | + | ||
| 229 | + - **The transposition (the root cause).** `vp9_mvref_common.h`'s | ||
| 230 | + `mv_ref_blocks` is a table of `POSITION struct { int row, col }`, so the | ||
| 231 | + 64x64 row is `{ -1, 3 }, { 3, -1 }, { -1, 4 }, { 4, -1 }, ...` meaning | ||
| 232 | + `(row - 1, col + 3)` first. Our accessors (`is_inside`, `find_mv_refs`'s | ||
| 233 | + cell closure, `get_mode_context`, `get_sub_block_mv`'s `search_col`) added | ||
| 234 | + `p.0` to the **column** and `p.1` to the **row**, so every asymmetric entry | ||
| 235 | + picked a different neighbour. Evidence: for the block at `r=8 c=32` | ||
| 236 | + bsize 12 mode 10 the first candidate came from `p=(-1,3)` mapped to | ||
| 237 | + `(11,31)` (the 64x64 at `(8,24)`, MV `(3,0)`) where libvpx's `(7,35)` (the | ||
| 238 | + 64x64 at `(0,32)`) holds `(-1,0)` - the MV the oracle used. Fixed by | ||
| 239 | + swapping the two components at every accessor (array text kept verbatim). | ||
| 240 | + - **`clamp_mv_ref`'s unsigned underflow.** `mi_rows - bh - row` is negative | ||
| 241 | + for a tall block on the frame's partial last row (1080p: `135 - 8 - 128`), | ||
| 242 | + and libvpx computes it in `int`; ours subtracted in `usize` and panicked | ||
| 243 | + ("attempt to subtract with overflow", inter.rs:497) - which is why the walk | ||
| 244 | + stopped after 1183 of 1213 blocks. Fixed by computing `mb_to_*` in `i32` | ||
| 245 | + (libvpx's formulas: `-(row*64)`, `(mi_rows - bh - row)*64`). | ||
| 246 | + - **The candidate count semantics.** First attempt returned the *real* count | ||
| 247 | + and broke every NEARMV block (frame 1 fell from 100% on the prefix to a | ||
| 248 | + first divergence at block #15, `mode=11`, ours `(1,2)` vs oracle `(0,0)`). | ||
| 249 | + `dec_find_mv_refs`' tail (vp9_decodemv.c:626) is subtler: an early exit | ||
| 250 | + (`goto Done`, i.e. `ADD_MV_REF_LIST_EB` filling the second slot or | ||
| 251 | + `early_break` stopping at the first) **jumps over** the mode-derived | ||
| 252 | + override and returns the count as accumulated; falling out of the loops | ||
| 253 | + applies `count = (mode == NEARMV) ? 2 : 1`, so a NEARMV block whose search | ||
| 254 | + found only one candidate still reports two and the caller reads the zeroed | ||
| 255 | + second slot `(0, 0)`. Reproduced exactly (`if done { count } else if mode == | ||
| 256 | + NEARMV { 2 } else { 1 }`, clamp that many). | ||
| 257 | + | ||
| 258 | + **Result (frame-1/2 by-index MODE diff, oracle vs ours):** | ||
| 259 | + - frame 1: ours 1213, oracle 1213, **identity 1213/1213 (100.0%)** | ||
| 260 | + - frame 2: ours 1851, oracle 1851, **identity 1851/1851 (100.0%)** | ||
| 261 | + compared fields: row/col/bsize/tx/mode/skip/seg/ref0/ref1/interp and the MV - | ||
| 262 | + with `uv` dropped for inter blocks and `mvrow/mvcol` dropped for intra blocks | ||
| 263 | + inside inter frames (both are libvpx-stale, see the non-comparison list below). | ||
| 264 | + Frame 2 exercises `use_prev_frame_mvs` (the from-previous-frame MV_REF field) | ||
| 265 | + and also matches byte-for-byte, so the prev-frame MV_REF store is correct. | ||
| 266 | + | ||
| 267 | + **Defect (b) - frame 2's compressed header - is refuted.** Its earlier | ||
| 268 | + "pristine defaults" `MVPROB` reading was an artifact of the run that aborted | ||
| 269 | + in frame 1; with the walk fixed, frame 2 reproduces 1851/1851 blocks | ||
| 270 | + bit-exactly, which requires frame 2's CH MV tables to be correct. (Our | ||
| 271 | + `MVPROB` dump still prints 4 times per stream where the oracle prints 2, so | ||
| 272 | + the line-by-line pairing is ambiguous - a harness artifact, not a decode | ||
| 273 | + difference; frame 1's dump is identical to the oracle's apart from the | ||
| 274 | + never-updated 11th `classes` slot.) | ||
| 275 | + | ||
| 276 | + **stale-field non-comparisons (extend the list):** `uv_mode` is stale for | ||
| 277 | + *inter* blocks; `mi->mv[]` is stale for *intra* blocks inside an inter frame | ||
| 278 | + (libvpx never writes it from `read_intra_frame_mode_info`), so a MODE diff | ||
| 279 | + must drop `mvrow/mvcol` whenever `ref0 == INTRA_FRAME`. Dropping them is what | ||
| 280 | + took frame 2 from 97.8% to 100%. | ||
| 281 | + | ||
| 282 | +Superseded earlier hypothesis (kept for the record): "a section count/order difference | ||
| 283 | +in the 1116..1200 span" — correct span, wrong mechanism: the counts differed because | ||
| 284 | +the mv-prob payloads are 7-bit literals, not term-subexp payloads. | ||
| 285 | + | ||
| 286 | +## Cause-3 lesson worth carrying forward | ||
| 287 | +Every VP9 tree constant must be rebuilt from libvpx's **macro expressions**, not from | ||
| 288 | +the spec's (or one's memory of the) naming order: `vp9_filter.h` uses `#define EIGHTTAP 0` | ||
| 289 | +while the spec's `literal_to_filter` lists EIGHTTAP_SMOOTH first. Trees whose leaves are | ||
| 290 | +plain integers (partition, mv class/fp, segment) were safe; trees whose leaves are *named | ||
| 291 | +constants* (switchable interp, inter mode via `INTER_OFFSET`) must be evaluated in libvpx's | ||
| 292 | +own numbering. Our inter-mode tree was right only because `INTER_OFFSET` is a subtraction. | ||
| 293 | + | ||
| 294 | +## Earlier ranked causes (now resolved or refuted) | ||
| 295 | + | ||
| 296 | +1. **A compressed-header section count/order difference in the 1116..1200 span.** | ||
| 297 | + The span is ~200 bools, which matches the *inter* half of the header | ||
| 298 | + (inter-mode 21 + switchable 8 + intra/inter 4 + ref-mode 1-2 + single_ref 10 + | ||
| 299 | + y_mode 36 + partition 48 + MV 69 = 197-198 flags before inner literal reads), | ||
| 300 | + so the prime suspect is one of those sections, not the coef/skip part. The | ||
| 301 | + missing-16-bools net says at least one section is read with the wrong count. | ||
| 302 | + Note `CHSUM` (tx_mode=4, ref_mode=0, allow_hp=1, interp=4) already matches the | ||
| 303 | + oracle exactly, so the header's *front* and the uncompressed header size | ||
| 304 | + (`uhs`) are right, and the tile offset itself is not suspect. | ||
| 305 | + Fastest discriminator (next slice, ~20 min): print one marker per CH section | ||
| 306 | + (`inter_mode`, `switchable`, `intra_inter`, `ref_mode_probs`, `y_mode`, | ||
| 307 | + `partition`, `mv_probs`) and diff the markers' bool indices against the | ||
| 308 | + oracle's `BOR` positions; the first section whose start index is off is the bug. | ||
| 309 | +2. **Not the tables.** The default partition probs were diffed entry-by-entry | ||
| 310 | + against `vp9/common/vp9_entropymode.c::default_partition_probs` (48 values, | ||
| 311 | + 0 mismatches), and the inter defaults were extracted mechanically; so a | ||
| 312 | + *rejected* cause unless the marker diff says otherwise. | ||
| 313 | +3. **Pre-existing keyframe limitation on multi-tile-column streams (found here).** | ||
| 314 | + Frame 0 of `/tmp/inter.ivf` (2 tile columns) dies in the *keyframe* path with | ||
| 315 | + "tile bool decoder desync" while the single-tile control (`/tmp/inter1.ivf`) | ||
| 316 | + decodes the same keyframe fine: the kf entropy walk is not multi-tile-safe on | ||
| 317 | + this encode, even though the earlier lane's all-intra sweep was. This is | ||
| 318 | + independent of the inter work (the inter walk needs no keyframe pixels) but it | ||
| 319 | + is a real defect to file, and it means frame 0 in `decode_syntax` must be | ||
| 320 | + tolerated (`KFAIL` line) rather than fatal. | ||
| 321 | +4. Unverified by this slice: the MV predictor's `scale_mv` sign-flip variant and | ||
| 322 | + the sub-8x8 `bmi` path (never reached, since block #0 already diverges), the | ||
| 323 | + compound-reference path (this stream is `SINGLE_REFERENCE` throughout), and | ||
| 324 | + the `prev_frame_mvs` path of frame 2. | ||
| 325 | + | ||
| 326 | +## Exact next slice | ||
| 327 | + | ||
| 328 | +1. CH section markers → first section with a wrong bool index → fix the count/order | ||
| 329 | + in `header.rs`; re-run the by-index `MODE` diff until frames 1 and 2 walk to the | ||
| 330 | + tile's last bool with `overreads() == 0`. | ||
| 331 | +2. Then re-diff `MODE` fields (skip/tx/ref0/ref1/inter_mode/mv) plus the oracle's | ||
| 332 | + `MVRAW` (raw diffs) to audit the MV reader/predictor — the predictor decides the | ||
| 333 | + high-precision bit, so it is parse-load-bearing. | ||
| 334 | +3. Then file/fix the keyframe multi-tile-column desync (ranked cause 3) — it is the | ||
| 335 | + same class as the tile-walk bug fixed in the keyframe lane and is now the only | ||
| 336 | + thing blocking `/tmp/inter.ivf` frame 0. | ||
| 337 | +4. Only after the syntax layer is exact: reconstruction/motion compensation | ||
| 338 | + (out of scope today), which is also what makes the pixels — no pixel-level | ||
| 339 | + inter result is claimed anywhere in this lane. | ||
| 340 | + | ||
| 341 | +## Repro commands | ||
| 342 | + | ||
| 343 | +``` | ||
| 344 | +# oracle dump (ref0/ref1/mv/interp + MVRAW + CHSUM) | ||
| 345 | +MODEDUMP=1 MVDUMP=1 /tmp/vp9loss/drv /tmp/inter1.ivf 2>&1 | grep -E '^MODE|^CHSUM' | ||
| 346 | +# ours | ||
| 347 | +CARGO_TARGET_DIR=$HOME/.cache/cargo-target-inter INTER_IVF=/tmp/inter1.ivf \ | ||
| 348 | + EC_VP9_INTERDUMP=1 cargo test -p ec-vp9 --test scratch_interdump -- --nocapture | ||
| 349 | +# bool-level CH diff: EC_VP9_TRACE=1 on ours vs BTRACE=1 on the oracle, | ||
| 350 | +# split ours on CHSTART/TILE markers and the oracle on the CHSUM/MODE markers | ||
| 351 | +``` | ||