Two-tile-column key frame decodes pixel-exact, bypassing the multi-tile refusal
Browse files at this commita5441e893b7600aec0ba2cc81a55cad4b8837f2bParent: 565a78b- test
- two-tile-column key frame decodes pixel-exact, bypassing the multi-tile refusal
1 changed file +234 -0
- crates/ec-av1/src/stream.rs +234 -0
crates/ec-av1/src/stream.rsmodified+234 -0
| @@ -5481,4 +5481,238 @@ mod tests { | ||
| 5481 | 5481 | } |
| 5482 | 5482 | } |
| 5483 | 5483 | } |
| 5484 | + | |
| 5485 | + /// lane-tiles r3: a real two-tile-column aomenc key frame, decoded | |
| 5486 | + /// through [`decode_key_frame_tile_with_cdfs`] directly with the whole | |
| 5487 | + /// `tiles: &[&[u8]]` list -- `decode_stream`'s `cols > 1 || rows > 1` | |
| 5488 | + /// refusal (this file, ~line 292) still fires for any stream this wide, | |
| 5489 | + /// so this gate bypasses it on purpose to prove the per-tile loop r2 | |
| 5490 | + /// landed (fresh `SymbolDecoder`/CDF copy per tile, `Neighbours::start_tile`, | |
| 5491 | + /// `PlaneBuf::set_tile_origin`) actually decodes both tiles pixel-exact | |
| 5492 | + /// before that refusal is lifted. 128x64 is exactly two 64x64 | |
| 5493 | + /// superblocks wide with `--tile-columns=1` (`TileCols=2`), so tile 1 is | |
| 5494 | + /// also the frame's own right edge -- the known gap (`PlaneBuf`'s | |
| 5495 | + /// tile_x0/tile_y0 only clip left/top reach, not a non-last column's | |
| 5496 | + /// right-edge reach) does not apply to this fixture; it would to a | |
| 5497 | + /// 3+-column one. Tile count itself is a structural aomenc param, not | |
| 5498 | + /// RD-dependent content, but this decoder's chroma smooth/paeth gap | |
| 5499 | + /// (`decode.rs` "a smooth or paeth chroma mode (round 2)", unrelated to | |
| 5500 | + /// tiles, pre-dating this lane) is content-dependent -- no aomenc flag | |
| 5501 | + /// disables UV-only smooth/paeth search, so a small seed sweep is | |
| 5502 | + /// needed the same way the sibling `a_real_aomenc_stream_with_*` gates | |
| 5503 | + /// retry past unrelated named refusals (every refusal in this crate is | |
| 5504 | + /// prefixed `"AV1 tile"` regardless of category, so unlike the sibling | |
| 5505 | + /// gates' `warp`/`obmc`/`interintra` banned lists there is no | |
| 5506 | + /// tile-specific substring to forbid here -- `matched > 0` plus the | |
| 5507 | + /// hard `tile_hits > 1`/pixel-exact asserts on any actual decode are | |
| 5508 | + /// the real proof). | |
| 5509 | + #[test] | |
| 5510 | + fn a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact() { | |
| 5511 | + const NAME: &str = "a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact"; | |
| 5512 | + if !have_ffmpeg() { | |
| 5513 | + eprintln!("SKIP {NAME}: no ffmpeg"); | |
| 5514 | + return; | |
| 5515 | + } | |
| 5516 | + if !have_aomenc() { | |
| 5517 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | |
| 5518 | + return; | |
| 5519 | + } | |
| 5520 | + let (width, height) = (128usize, 64usize); | |
| 5521 | + let mut matched = 0u32; | |
| 5522 | + let mut named_refusals = 0u32; | |
| 5523 | + for attempt in 0..20u32 { | |
| 5524 | + let seed = 42 + attempt; | |
| 5525 | + let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); | |
| 5526 | + let y4m = Command::new("ffmpeg") | |
| 5527 | + .args([ | |
| 5528 | + "-v", | |
| 5529 | + "error", | |
| 5530 | + "-f", | |
| 5531 | + "lavfi", | |
| 5532 | + "-i", | |
| 5533 | + &source, | |
| 5534 | + "-pix_fmt", | |
| 5535 | + "yuv420p", | |
| 5536 | + "-f", | |
| 5537 | + "yuv4mpegpipe", | |
| 5538 | + "-", | |
| 5539 | + ]) | |
| 5540 | + .stdin(Stdio::null()) | |
| 5541 | + .stdout(Stdio::piped()) | |
| 5542 | + .stderr(Stdio::piped()) | |
| 5543 | + .output() | |
| 5544 | + .expect("ffmpeg failed to run"); | |
| 5545 | + assert!( | |
| 5546 | + y4m.status.success(), | |
| 5547 | + "ffmpeg fixture: {}", | |
| 5548 | + String::from_utf8_lossy(&y4m.stderr) | |
| 5549 | + ); | |
| 5550 | + let mut child = Command::new(aomenc_path()) | |
| 5551 | + .args([ | |
| 5552 | + "--codec=av1", | |
| 5553 | + "--passes=1", | |
| 5554 | + "--end-usage=q", | |
| 5555 | + "--cq-level=45", | |
| 5556 | + "--cpu-used=0", | |
| 5557 | + "--limit=1", | |
| 5558 | + "--kf-max-dist=1000", | |
| 5559 | + "--threads=1", | |
| 5560 | + "--row-mt=0", | |
| 5561 | + "--tile-columns=1", | |
| 5562 | + "--tile-rows=0", | |
| 5563 | + // This decoder hardcodes SB_MI=16 (64px) superblocks | |
| 5564 | + // everywhere (`decode.rs`'s sb128 dead-end, ledger | |
| 5565 | + // `av1-superblock-contexts`) -- without this, aomenc's | |
| 5566 | + // own default 128x128 superblock makes 128x64 exactly | |
| 5567 | + // ONE superblock wide, so `--tile-columns=1` has nothing | |
| 5568 | + // to split and `cols` comes back 1. | |
| 5569 | + "--sb-size=64", | |
| 5570 | + // Deblocking crosses tile boundaries by default (spec | |
| 5571 | + // `loop_filter_across_tiles_enabled`); `PlaneBuf`'s | |
| 5572 | + // tile_x0/tile_y0 origin only ever clips a tile's own | |
| 5573 | + // left/top reach (r2/r3's documented known gap), so a | |
| 5574 | + // non-last tile column's right-edge deblock would read | |
| 5575 | + // into the next tile's still-undecoded/differently- | |
| 5576 | + // origined samples. Off here since deblocking itself is | |
| 5577 | + // not what this gate is proving. | |
| 5578 | + "--loopfilter-control=0", | |
| 5579 | + "--enable-rect-partitions=0", | |
| 5580 | + "--enable-ab-partitions=0", | |
| 5581 | + "--enable-1to4-partitions=0", | |
| 5582 | + "--enable-filter-intra=0", | |
| 5583 | + "--enable-smooth-intra=0", | |
| 5584 | + "--enable-paeth-intra=0", | |
| 5585 | + "--enable-directional-intra=0", | |
| 5586 | + "--enable-angle-delta=0", | |
| 5587 | + "--enable-tx-size-search=0", | |
| 5588 | + "--enable-cdef=0", | |
| 5589 | + "--enable-restoration=0", | |
| 5590 | + "--max-partition-size=32", | |
| 5591 | + "--min-partition-size=32", | |
| 5592 | + "--enable-palette=0", | |
| 5593 | + "--enable-intrabc=0", | |
| 5594 | + "--enable-cfl-intra=0", | |
| 5595 | + "--obu", | |
| 5596 | + "-o", | |
| 5597 | + "-", | |
| 5598 | + "-", | |
| 5599 | + ]) | |
| 5600 | + .stdin(Stdio::piped()) | |
| 5601 | + .stdout(Stdio::piped()) | |
| 5602 | + .stderr(Stdio::piped()) | |
| 5603 | + .spawn() | |
| 5604 | + .expect("aomenc failed to start"); | |
| 5605 | + child | |
| 5606 | + .stdin | |
| 5607 | + .take() | |
| 5608 | + .expect("aomenc stdin") | |
| 5609 | + .write_all(&y4m.stdout) | |
| 5610 | + .expect("writing y4m to aomenc"); | |
| 5611 | + let out = child.wait_with_output().expect("aomenc failed to run"); | |
| 5612 | + assert!( | |
| 5613 | + out.status.success(), | |
| 5614 | + "aomenc refused the fixture: {}", | |
| 5615 | + String::from_utf8_lossy(&out.stderr) | |
| 5616 | + ); | |
| 5617 | + let stream = out.stdout; | |
| 5618 | + | |
| 5619 | + // Parse the stream ourselves (rather than `decode_stream`, whose | |
| 5620 | + // multi-tile refusal is exactly what this gate exists to run | |
| 5621 | + // ahead of) to reach the real `Frame` OBU's `tile_info` and its | |
| 5622 | + // full `Vec<Tile>` -- `Av1Parser::parse_obu` already locates | |
| 5623 | + // every tile in a multi-tile tile group (`parse_tile_group`), it | |
| 5624 | + // is only `decode_stream` that ever narrows this to | |
| 5625 | + // `tiles.first()`. | |
| 5626 | + let mut parser = Av1Parser::new(); | |
| 5627 | + let mut pos = 0usize; | |
| 5628 | + let mut frame = None; | |
| 5629 | + while pos < stream.len() { | |
| 5630 | + let obu_offset = pos; | |
| 5631 | + let obu = parser.parse_obu(&stream[pos..]).unwrap(); | |
| 5632 | + pos += obu.total_size; | |
| 5633 | + if let ObuKind::Frame(header, tiles) = obu.kind { | |
| 5634 | + frame = Some((header, tiles, obu_offset)); | |
| 5635 | + break; | |
| 5636 | + } | |
| 5637 | + } | |
| 5638 | + let (header, tiles, obu_offset) = frame.expect("stream has a Frame OBU"); | |
| 5639 | + assert!( | |
| 5640 | + header.tile_info.cols > 1, | |
| 5641 | + "{NAME}: aomenc did not actually split into >1 tile column (cols={}, seed {seed})", | |
| 5642 | + header.tile_info.cols | |
| 5643 | + ); | |
| 5644 | + assert_eq!( | |
| 5645 | + tiles.len(), | |
| 5646 | + header.tile_info.cols as usize, | |
| 5647 | + "{NAME}: expected one tile per column (seed {seed})" | |
| 5648 | + ); | |
| 5649 | + // `Tile::offset` is relative to the buffer `parse_obu` was | |
| 5650 | + // handed (`&stream[pos..]` at the time this OBU was parsed), so | |
| 5651 | + // it needs `obu_offset` added back -- the same adjustment | |
| 5652 | + // `decode_stream` makes for its own single-tile slice above. | |
| 5653 | + let tile_bufs: Vec<&[u8]> = tiles | |
| 5654 | + .iter() | |
| 5655 | + .map(|t| &stream[obu_offset + t.offset..obu_offset + t.offset + t.size]) | |
| 5656 | + .collect(); | |
| 5657 | + | |
| 5658 | + let enable_filter_intra = parser | |
| 5659 | + .sequence_header() | |
| 5660 | + .is_some_and(|seq| seq.enable_filter_intra); | |
| 5661 | + let enable_edge_filter = parser | |
| 5662 | + .sequence_header() | |
| 5663 | + .is_some_and(|seq| seq.enable_intra_edge_filter); | |
| 5664 | + | |
| 5665 | + let before = crate::decode::tile_hits(); | |
| 5666 | + let picture = match decode_key_frame_tile_with_cdfs( | |
| 5667 | + &tile_bufs, | |
| 5668 | + &header.tile_info, | |
| 5669 | + header.mi_cols, | |
| 5670 | + header.mi_rows, | |
| 5671 | + header.quantization.base_q_idx, | |
| 5672 | + header.frame_width, | |
| 5673 | + header.frame_height, | |
| 5674 | + enable_filter_intra, | |
| 5675 | + enable_edge_filter, | |
| 5676 | + &header.cdef, | |
| 5677 | + &header.loop_filter, | |
| 5678 | + None, | |
| 5679 | + header.tx_mode == TxMode::Select, | |
| 5680 | + header.reduced_tx_set, | |
| 5681 | + header.allow_screen_content_tools, | |
| 5682 | + header.allow_intrabc, | |
| 5683 | + ) { | |
| 5684 | + Err(e) => { | |
| 5685 | + let msg = e.to_string(); | |
| 5686 | + assert!( | |
| 5687 | + msg.contains("unsupported"), | |
| 5688 | + "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" | |
| 5689 | + ); | |
| 5690 | + named_refusals += 1; | |
| 5691 | + eprintln!("seed {seed} refusal: {msg}"); | |
| 5692 | + continue; | |
| 5693 | + } | |
| 5694 | + Ok((picture, _end_cdfs)) => picture, | |
| 5695 | + }; | |
| 5696 | + let hits = crate::decode::tile_hits() - before; | |
| 5697 | + assert!( | |
| 5698 | + hits > 1, | |
| 5699 | + "{NAME}: tile_hits delta was {hits}, expected >1 -- both tiles must actually \ | |
| 5700 | + decode, not just tile 0 (seed {seed})" | |
| 5701 | + ); | |
| 5702 | + | |
| 5703 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); | |
| 5704 | + assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); | |
| 5705 | + assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); | |
| 5706 | + assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); | |
| 5707 | + matched += 1; | |
| 5708 | + } | |
| 5709 | + assert!( | |
| 5710 | + matched > 0, | |
| 5711 | + "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \ | |
| 5712 | + a two-tile-column stream" | |
| 5713 | + ); | |
| 5714 | + eprintln!( | |
| 5715 | + "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" | |
| 5716 | + ); | |
| 5717 | + } | |
| 5484 | 5718 | } |