Lift the multi-tile refusal for key frames, scoped
Browse files at this commit4be6f4cbc822e77ca02507ba8eea194869544743Parent: d1f3af8- feat
- lift the multi-tile refusal for key frames, scoped
1 changed file +193 -6
- crates/ec-av1/src/stream.rs +193 -6
crates/ec-av1/src/stream.rsmodified+193 -6
| @@ -286,13 +286,58 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 286 | 286 | // `skip_mode_present` refusal is gone. |
| 287 | 287 | // `context_update_tile_id` (spec `decode_tile`/`exit_symbol`) names |
| 288 | 288 | // which tile's end-of-tile adapted CDFs become the frame's own; this |
| 289 | - // decoder only ever decodes tile 0 (`tiles.first()` above), so a | |
| 290 | - // multi-tile frame is refused rather than silently forwarding the | |
| 291 | - // wrong tile's table (or leaving the rest of the picture undecoded). | |
| 292 | - if header.tile_info.cols > 1 || header.tile_info.rows > 1 { | |
| 289 | + // decoder still only ever *keeps* tile 0's adapted table (see the | |
| 290 | + // `context_update_tile_id != 0` refusal further down), so any frame | |
| 291 | + // naming a different tile there is refused rather than silently | |
| 292 | + // forwarding the wrong tile's table. | |
| 293 | + // | |
| 294 | + // lane-tiles r4: the multi-tile refusal itself is now scoped, not | |
| 295 | + // blanket. `decode_key_frame_tile_with_cdfs` genuinely loops every | |
| 296 | + // tile (r2/r3), proven pixel-exact by | |
| 297 | + // `a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact` | |
| 298 | + // (2 tile columns, `--loopfilter-control=0`) -- but that gate's own | |
| 299 | + // doc comment names two gaps it deliberately sidesteps, so this | |
| 300 | + // refusal keeps refusing exactly those: | |
| 301 | + // - `decode_inter_frame_tile_with_cdfs` takes one `&[u8]` tile | |
| 302 | + // payload, not a `tiles: &[&[u8]]` list -- it has no per-tile | |
| 303 | + // loop at all, so an inter frame with >1 tile is still refused | |
| 304 | + // unconditionally. | |
| 305 | + // - Deblocking crosses tile boundaries by spec default (AV1 has | |
| 306 | + // no `loop_filter_across_tiles_enabled`-style flag at all -- | |
| 307 | + // confirmed absent from `ec-av1-syntax`'s `loop_filter_params` | |
| 308 | + // read, spec 5.9.11 has no such field either), but `PlaneBuf`'s | |
| 309 | + // `tile_x0`/`tile_y0` origin only ever clips a tile's own | |
| 310 | + // left/top reach, not a non-last column's right-edge reach -- | |
| 311 | + // so a key frame with `loop_filter.level` non-zero (deblocking | |
| 312 | + // actually on) still refuses. | |
| 313 | + // - The proven gate is 2 tile *columns*, 1 tile *row*; a | |
| 314 | + // non-last column's right-edge reach bound past column 0 is | |
| 315 | + // unimplemented for 3+ columns (r3's own known gap), so `cols` | |
| 316 | + // is capped at 2. Tile rows are untested this round, so `rows` | |
| 317 | + // stays capped at 1. | |
| 318 | + let loop_filter_on = header.loop_filter.level.iter().any(|&l| l != 0); | |
| 319 | + if header.frame_type != FrameType::Key && header.tile_info.cols > 1 { | |
| 320 | + return Err(Error::unsupported( | |
| 321 | + "AV1 decode_stream", | |
| 322 | + "an inter frame with more than one tile (the inter tile-decode path has no per-tile loop, only key frames do)", | |
| 323 | + )); | |
| 324 | + } | |
| 325 | + if header.tile_info.rows > 1 { | |
| 326 | + return Err(Error::unsupported( | |
| 327 | + "AV1 decode_stream", | |
| 328 | + "a frame with more than one tile row (only tile columns are proven pixel-exact so far)", | |
| 329 | + )); | |
| 330 | + } | |
| 331 | + if header.tile_info.cols > 2 { | |
| 293 | 332 | return Err(Error::unsupported( |
| 294 | 333 | "AV1 decode_stream", |
| 295 | - "a frame with more than one tile (this decoder only ever decodes tile 0)", | |
| 334 | + "a frame with more than two tile columns (a non-last column's right-edge reach bound past column 0 is unimplemented)", | |
| 335 | + )); | |
| 336 | + } | |
| 337 | + if header.tile_info.cols > 1 && loop_filter_on { | |
| 338 | + return Err(Error::unsupported( | |
| 339 | + "AV1 decode_stream", | |
| 340 | + "a multi-tile frame with loop filtering enabled (deblocking crosses tile boundaries by spec default; PlaneBuf's tile origin does not clip a non-last column's right-edge reach)", | |
| 296 | 341 | )); |
| 297 | 342 | } |
| 298 | 343 | // `Tile::offset` is relative to the buffer `parse_obu` was handed |
| @@ -300,6 +345,13 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 300 | 345 | // to `obu_offset`, not to `data` as a whole. |
| 301 | 346 | let start = obu_offset + tile.offset; |
| 302 | 347 | let tile_bytes = data.get(start..start + tile.size).ok_or(Error::NeedMore)?; |
| 348 | + let tile_bufs: Vec<&[u8]> = tiles | |
| 349 | + .iter() | |
| 350 | + .map(|t| { | |
| 351 | + data.get(obu_offset + t.offset..obu_offset + t.offset + t.size) | |
| 352 | + .ok_or(Error::NeedMore) | |
| 353 | + }) | |
| 354 | + .collect::<Result<_>>()?; | |
| 303 | 355 | let enable_filter_intra = parser |
| 304 | 356 | .sequence_header() |
| 305 | 357 | .is_some_and(|seq| seq.enable_filter_intra); |
| @@ -360,7 +412,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 360 | 412 | |
| 361 | 413 | let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key { |
| 362 | 414 | let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs( |
| 363 | - &[tile_bytes], | |
| 415 | + &tile_bufs, | |
| 364 | 416 | &header.tile_info, |
| 365 | 417 | header.mi_cols, |
| 366 | 418 | header.mi_rows, |
| @@ -5715,4 +5767,139 @@ mod tests { | ||
| 5715 | 5767 | "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" |
| 5716 | 5768 | ); |
| 5717 | 5769 | } |
| 5770 | + | |
| 5771 | + /// lane-tiles r4: the same real two-tile-column stream as the gate | |
| 5772 | + /// above, this time run through `decode_stream` itself -- the entry | |
| 5773 | + /// point real callers use, not the `decode_key_frame_tile_with_cdfs` | |
| 5774 | + /// bypass. Proves the `cols > 1` refusal is actually lifted (not just | |
| 5775 | + /// that the underlying per-tile loop works) for the scoped case this | |
| 5776 | + /// round proved safe: 2 tile columns, 1 tile row, key frame, loop | |
| 5777 | + /// filtering off (`--loopfilter-control=0` stays -- deblocking crosses | |
| 5778 | + /// tile boundaries by spec default and `PlaneBuf` cannot bound a | |
| 5779 | + /// non-last column's right-edge reach yet, see the refusal's own doc | |
| 5780 | + /// comment above `decode_stream`). | |
| 5781 | + #[test] | |
| 5782 | + fn a_real_aomenc_stream_with_two_tile_columns_decodes_through_decode_stream() { | |
| 5783 | + const NAME: &str = "a_real_aomenc_stream_with_two_tile_columns_decodes_through_decode_stream"; | |
| 5784 | + if !have_ffmpeg() { | |
| 5785 | + eprintln!("SKIP {NAME}: no ffmpeg"); | |
| 5786 | + return; | |
| 5787 | + } | |
| 5788 | + if !have_aomenc() { | |
| 5789 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | |
| 5790 | + return; | |
| 5791 | + } | |
| 5792 | + let (width, height) = (128usize, 64usize); | |
| 5793 | + let mut matched = 0u32; | |
| 5794 | + let mut named_refusals = 0u32; | |
| 5795 | + for attempt in 0..20u32 { | |
| 5796 | + let seed = 42 + attempt; | |
| 5797 | + let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); | |
| 5798 | + let y4m = Command::new("ffmpeg") | |
| 5799 | + .args([ | |
| 5800 | + "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-f", | |
| 5801 | + "yuv4mpegpipe", "-", | |
| 5802 | + ]) | |
| 5803 | + .stdin(Stdio::null()) | |
| 5804 | + .stdout(Stdio::piped()) | |
| 5805 | + .stderr(Stdio::piped()) | |
| 5806 | + .output() | |
| 5807 | + .expect("ffmpeg failed to run"); | |
| 5808 | + assert!( | |
| 5809 | + y4m.status.success(), | |
| 5810 | + "ffmpeg fixture: {}", | |
| 5811 | + String::from_utf8_lossy(&y4m.stderr) | |
| 5812 | + ); | |
| 5813 | + let mut child = Command::new(aomenc_path()) | |
| 5814 | + .args([ | |
| 5815 | + "--codec=av1", | |
| 5816 | + "--passes=1", | |
| 5817 | + "--end-usage=q", | |
| 5818 | + "--cq-level=45", | |
| 5819 | + "--cpu-used=0", | |
| 5820 | + "--limit=1", | |
| 5821 | + "--kf-max-dist=1000", | |
| 5822 | + "--threads=1", | |
| 5823 | + "--row-mt=0", | |
| 5824 | + "--tile-columns=1", | |
| 5825 | + "--tile-rows=0", | |
| 5826 | + "--sb-size=64", | |
| 5827 | + "--loopfilter-control=0", | |
| 5828 | + "--enable-rect-partitions=0", | |
| 5829 | + "--enable-ab-partitions=0", | |
| 5830 | + "--enable-1to4-partitions=0", | |
| 5831 | + "--enable-filter-intra=0", | |
| 5832 | + "--enable-smooth-intra=0", | |
| 5833 | + "--enable-paeth-intra=0", | |
| 5834 | + "--enable-directional-intra=0", | |
| 5835 | + "--enable-angle-delta=0", | |
| 5836 | + "--enable-tx-size-search=0", | |
| 5837 | + "--enable-cdef=0", | |
| 5838 | + "--enable-restoration=0", | |
| 5839 | + "--max-partition-size=32", | |
| 5840 | + "--min-partition-size=32", | |
| 5841 | + "--enable-palette=0", | |
| 5842 | + "--enable-intrabc=0", | |
| 5843 | + "--enable-cfl-intra=0", | |
| 5844 | + "--obu", | |
| 5845 | + "-o", | |
| 5846 | + "-", | |
| 5847 | + "-", | |
| 5848 | + ]) | |
| 5849 | + .stdin(Stdio::piped()) | |
| 5850 | + .stdout(Stdio::piped()) | |
| 5851 | + .stderr(Stdio::piped()) | |
| 5852 | + .spawn() | |
| 5853 | + .expect("aomenc failed to start"); | |
| 5854 | + child | |
| 5855 | + .stdin | |
| 5856 | + .take() | |
| 5857 | + .expect("aomenc stdin") | |
| 5858 | + .write_all(&y4m.stdout) | |
| 5859 | + .expect("writing y4m to aomenc"); | |
| 5860 | + let out = child.wait_with_output().expect("aomenc failed to run"); | |
| 5861 | + assert!( | |
| 5862 | + out.status.success(), | |
| 5863 | + "aomenc refused the fixture: {}", | |
| 5864 | + String::from_utf8_lossy(&out.stderr) | |
| 5865 | + ); | |
| 5866 | + let stream = out.stdout; | |
| 5867 | + | |
| 5868 | + let before = crate::decode::tile_hits(); | |
| 5869 | + let pictures = match decode_stream(&stream) { | |
| 5870 | + Err(e) => { | |
| 5871 | + let msg = e.to_string(); | |
| 5872 | + assert!( | |
| 5873 | + msg.contains("unsupported"), | |
| 5874 | + "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" | |
| 5875 | + ); | |
| 5876 | + named_refusals += 1; | |
| 5877 | + eprintln!("seed {seed} refusal: {msg}"); | |
| 5878 | + continue; | |
| 5879 | + } | |
| 5880 | + Ok(pictures) => pictures, | |
| 5881 | + }; | |
| 5882 | + let hits = crate::decode::tile_hits() - before; | |
| 5883 | + assert!( | |
| 5884 | + hits > 1, | |
| 5885 | + "{NAME}: tile_hits delta was {hits}, expected >1 -- both tiles must actually \ | |
| 5886 | + decode, not just tile 0 (seed {seed})" | |
| 5887 | + ); | |
| 5888 | + assert_eq!(pictures.len(), 1, "{NAME}: expected exactly 1 picture (seed {seed})"); | |
| 5889 | + | |
| 5890 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); | |
| 5891 | + assert_eq!(pictures[0].y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); | |
| 5892 | + assert_eq!(pictures[0].u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); | |
| 5893 | + assert_eq!(pictures[0].v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); | |
| 5894 | + matched += 1; | |
| 5895 | + } | |
| 5896 | + assert!( | |
| 5897 | + matched > 0, | |
| 5898 | + "{NAME}: every attempt refused ({named_refusals} refusals); decode_stream never \ | |
| 5899 | + decoded a two-tile-column stream" | |
| 5900 | + ); | |
| 5901 | + eprintln!( | |
| 5902 | + "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" | |
| 5903 | + ); | |
| 5904 | + } | |
| 5718 | 5905 | } |