wip(av1): tiles r9's in-flight tile-group gate edit, committed verbatim at the turn cap
Browse files at this commit462b1bf0df2ab8195a936e9d2cf24a8c1d23b612Parent: 744952b1 changed file +214 -5
- crates/ec-av1/src/stream.rs +214 -5
crates/ec-av1/src/stream.rsmodified+214 -5
| @@ -16,7 +16,8 @@ | |||
| 16 | //! an implementation detail of how this crate happens to build it). | 16 | //! an implementation detail of how this crate happens to build it). |
| 17 | 17 | ||
| 18 | use ec_av1_syntax::{ | 18 | use ec_av1_syntax::{ |
| 19 | - Av1Parser, FrameType, NUM_REF_FRAMES, ObuKind, PRIMARY_REF_NONE, TxMode, WarpModel, | 19 | + Av1Parser, FrameHeader, FrameType, NUM_REF_FRAMES, ObuKind, PRIMARY_REF_NONE, Tile, TxMode, |
| 20 | + WarpModel, | ||
| 20 | }; | 21 | }; |
| 21 | use ec_core::{Error, Result}; | 22 | use ec_core::{Error, Result}; |
| 22 | 23 | ||
| @@ -75,12 +76,32 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 75 | let mut cdf_slots: [Option<Cdfs>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); | 76 | let mut cdf_slots: [Option<Cdfs>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); |
| 76 | 77 | ||
| 77 | let mut pos = 0usize; | 78 | let mut pos = 0usize; |
| 79 | + // A frame whose tiles are split across several `OBU_TILE_GROUP`s (spec | ||
| 80 | + // 5.11.1, aomenc's `--num-tile-groups`) sends a standalone | ||
| 81 | + // `OBU_FRAME_HEADER` first, with `show_existing_frame == false` and no | ||
| 82 | + // tile payload of its own -- `OBU_FRAME` (the `ObuKind::Frame` arm below) | ||
| 83 | + // only ever folds ONE tile group into the header's own OBU. Stash the | ||
| 84 | + // header here and accumulate tiles from each `OBU_TILE_GROUP` that | ||
| 85 | + // follows until every tile of the frame has arrived. | ||
| 86 | + let mut pending_header: Option<Box<FrameHeader>> = None; | ||
| 87 | + let mut pending_tiles: Vec<Tile> = Vec::new(); | ||
| 78 | while pos < data.len() { | 88 | while pos < data.len() { |
| 79 | let obu = parser.parse_obu(&data[pos..])?; | 89 | let obu = parser.parse_obu(&data[pos..])?; |
| 80 | let obu_offset = pos; | 90 | let obu_offset = pos; |
| 81 | pos += obu.total_size; | 91 | pos += obu.total_size; |
| 82 | 92 | ||
| 83 | if let ObuKind::FrameHeader(header) = &obu.kind { | 93 | if let ObuKind::FrameHeader(header) = &obu.kind { |
| 94 | + if !header.show_existing_frame { | ||
| 95 | + // Before this fix, this branch fired unconditionally for | ||
| 96 | + // EVERY `FrameHeader` OBU (including this one), so a | ||
| 97 | + // standalone header always fell into the `show_existing_frame` | ||
| 98 | + // slot lookup below and misreported "a show_existing_frame | ||
| 99 | + // header naming an empty reference slot" for a frame that was | ||
| 100 | + // never a show_existing_frame header at all. | ||
| 101 | + pending_header = Some(header.clone()); | ||
| 102 | + pending_tiles = Vec::new(); | ||
| 103 | + continue; | ||
| 104 | + } | ||
| 84 | // `show_existing_frame` (spec 7.21/5.9.2): no tile payload, this | 105 | // `show_existing_frame` (spec 7.21/5.9.2): no tile payload, this |
| 85 | // OBU only names a DPB slot to output again -- the coded-frame | 106 | // OBU only names a DPB slot to output again -- the coded-frame |
| 86 | // count and the shown-picture count are different things (a | 107 | // count and the shown-picture count are different things (a |
| @@ -117,9 +138,35 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 117 | pictures.push(output); | 138 | pictures.push(output); |
| 118 | continue; | 139 | continue; |
| 119 | } | 140 | } |
| 120 | - let ObuKind::Frame(header, tiles) = obu.kind else { | 141 | + // `tiles_base_offset` is added to every `Tile::offset` below to reach |
| 121 | - continue; | 142 | + // an absolute position in `data`. `OBU_FRAME`'s tiles are relative to |
| 122 | - }; | 143 | + // this OBU's own `obu_offset`; an accumulated run of `OBU_TILE_GROUP`s |
| 144 | + // may span several different OBUs (each at a different offset in | ||
| 145 | + // `data`), so those tiles are normalised to an absolute offset the | ||
| 146 | + // moment they are appended to `pending_tiles`, and `tiles_base_offset` | ||
| 147 | + // is 0 for that case. | ||
| 148 | + let (header, tiles, tiles_base_offset): (Box<FrameHeader>, Vec<Tile>, usize) = | ||
| 149 | + match obu.kind { | ||
| 150 | + ObuKind::Frame(header, tiles) => (header, tiles, obu_offset), | ||
| 151 | + ObuKind::TileGroup(new_tiles) => { | ||
| 152 | + let header = pending_header.clone().ok_or_else(|| { | ||
| 153 | + Error::corrupt( | ||
| 154 | + "AV1 decode_stream: a tile group OBU with no preceding frame header", | ||
| 155 | + ) | ||
| 156 | + })?; | ||
| 157 | + let expected = header.tile_info.cols * header.tile_info.rows; | ||
| 158 | + pending_tiles.extend(new_tiles.into_iter().map(|mut t| { | ||
| 159 | + t.offset += obu_offset; | ||
| 160 | + t | ||
| 161 | + })); | ||
| 162 | + if (pending_tiles.len() as u32) < expected { | ||
| 163 | + continue; | ||
| 164 | + } | ||
| 165 | + pending_header = None; | ||
| 166 | + (header, std::mem::take(&mut pending_tiles), 0) | ||
| 167 | + } | ||
| 168 | + _ => continue, | ||
| 169 | + }; | ||
| 123 | // Every tile is decoded below through `tile_bufs`; this only rejects a | 170 | // Every tile is decoded below through `tile_bufs`; this only rejects a |
| 124 | // frame OBU that carries no tile group at all. | 171 | // frame OBU that carries no tile group at all. |
| 125 | if tiles.is_empty() { | 172 | if tiles.is_empty() { |
| @@ -354,7 +401,7 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 354 | let tile_bufs: Vec<&[u8]> = tiles | 401 | let tile_bufs: Vec<&[u8]> = tiles |
| 355 | .iter() | 402 | .iter() |
| 356 | .map(|t| { | 403 | .map(|t| { |
| 357 | - data.get(obu_offset + t.offset..obu_offset + t.offset + t.size) | 404 | + data.get(tiles_base_offset + t.offset..tiles_base_offset + t.offset + t.size) |
| 358 | .ok_or(Error::NeedMore) | 405 | .ok_or(Error::NeedMore) |
| 359 | }) | 406 | }) |
| 360 | .collect::<Result<_>>()?; | 407 | .collect::<Result<_>>()?; |
| @@ -7830,4 +7877,166 @@ mod tests { | |||
| 7830 | "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" | 7877 | "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" |
| 7831 | ); | 7878 | ); |
| 7832 | } | 7879 | } |
| 7880 | + | ||
| 7881 | + /// lane-tiles r9: `--num-tile-groups` makes aomenc split one frame's | ||
| 7882 | + /// tiles across several `OBU_TILE_GROUP`s instead of folding them into | ||
| 7883 | + /// the single `OBU_FRAME` every other gate in this file relies on -- | ||
| 7884 | + /// verified live (`scratch_probe_num_tile_groups`, this round) to emit a | ||
| 7885 | + /// standalone `OBU_FRAME_HEADER` (`show_existing_frame == false`) | ||
| 7886 | + /// followed by two `OBU_TILE_GROUP` OBUs, one tile each. Before this | ||
| 7887 | + /// round `decode_stream` had no arm for `ObuKind::TileGroup` at all (it | ||
| 7888 | + /// fell through the catch-all `continue`, silently dropping the frame), | ||
| 7889 | + /// and its `ObuKind::FrameHeader` arm matched *every* frame header | ||
| 7890 | + /// unconditionally, so a standalone header was misrouted into the | ||
| 7891 | + /// `show_existing_frame` slot lookup and returned the wrong named error | ||
| 7892 | + /// ("a show_existing_frame header naming an empty reference slot") for a | ||
| 7893 | + /// frame that was never a show_existing_frame header. Both are fixed | ||
| 7894 | + /// above: the `FrameHeader` arm now checks `show_existing_frame`, and a | ||
| 7895 | + /// `pending_header`/`pending_tiles` accumulator collects tile groups | ||
| 7896 | + /// until the frame's `cols * rows` tiles have all arrived. | ||
| 7897 | + #[test] | ||
| 7898 | + fn a_real_aomenc_stream_with_several_tile_group_obus_decodes_pixel_exact() { | ||
| 7899 | + const NAME: &str = "a_real_aomenc_stream_with_several_tile_group_obus_decodes_pixel_exact"; | ||
| 7900 | + if !have_ffmpeg() { | ||
| 7901 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 7902 | + return; | ||
| 7903 | + } | ||
| 7904 | + if !have_aomenc() { | ||
| 7905 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); | ||
| 7906 | + return; | ||
| 7907 | + } | ||
| 7908 | + let (width, height) = (64usize, 128usize); | ||
| 7909 | + let mut matched = 0u32; | ||
| 7910 | + let mut named_refusals = 0u32; | ||
| 7911 | + let mut saw_multiple_tile_group_obus = false; | ||
| 7912 | + for attempt in 0..20u32 { | ||
| 7913 | + let seed = 42 + attempt; | ||
| 7914 | + let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); | ||
| 7915 | + let y4m = Command::new("ffmpeg") | ||
| 7916 | + .args([ | ||
| 7917 | + "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-f", | ||
| 7918 | + "yuv4mpegpipe", "-", | ||
| 7919 | + ]) | ||
| 7920 | + .stdin(Stdio::null()) | ||
| 7921 | + .stdout(Stdio::piped()) | ||
| 7922 | + .stderr(Stdio::piped()) | ||
| 7923 | + .output() | ||
| 7924 | + .expect("ffmpeg failed to run"); | ||
| 7925 | + assert!( | ||
| 7926 | + y4m.status.success(), | ||
| 7927 | + "ffmpeg fixture: {}", | ||
| 7928 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 7929 | + ); | ||
| 7930 | + let mut child = Command::new(aomenc_path()) | ||
| 7931 | + .args([ | ||
| 7932 | + "--codec=av1", | ||
| 7933 | + "--passes=1", | ||
| 7934 | + "--end-usage=q", | ||
| 7935 | + "--cq-level=45", | ||
| 7936 | + "--cpu-used=0", | ||
| 7937 | + "--limit=1", | ||
| 7938 | + "--kf-max-dist=1000", | ||
| 7939 | + "--threads=1", | ||
| 7940 | + "--row-mt=0", | ||
| 7941 | + "--tile-columns=0", | ||
| 7942 | + "--tile-rows=1", | ||
| 7943 | + "--sb-size=64", | ||
| 7944 | + "--num-tile-groups=4", | ||
| 7945 | + "--loopfilter-control=0", | ||
| 7946 | + "--enable-rect-partitions=0", | ||
| 7947 | + "--enable-ab-partitions=0", | ||
| 7948 | + "--enable-1to4-partitions=0", | ||
| 7949 | + "--enable-filter-intra=0", | ||
| 7950 | + "--enable-smooth-intra=0", | ||
| 7951 | + "--enable-paeth-intra=0", | ||
| 7952 | + "--enable-directional-intra=0", | ||
| 7953 | + "--enable-angle-delta=0", | ||
| 7954 | + "--enable-tx-size-search=0", | ||
| 7955 | + "--enable-cdef=0", | ||
| 7956 | + "--enable-restoration=0", | ||
| 7957 | + "--max-partition-size=32", | ||
| 7958 | + "--min-partition-size=32", | ||
| 7959 | + "--enable-palette=0", | ||
| 7960 | + "--enable-intrabc=0", | ||
| 7961 | + "--enable-cfl-intra=0", | ||
| 7962 | + "--obu", | ||
| 7963 | + "-o", | ||
| 7964 | + "-", | ||
| 7965 | + "-", | ||
| 7966 | + ]) | ||
| 7967 | + .stdin(Stdio::piped()) | ||
| 7968 | + .stdout(Stdio::piped()) | ||
| 7969 | + .stderr(Stdio::piped()) | ||
| 7970 | + .spawn() | ||
| 7971 | + .expect("aomenc failed to start"); | ||
| 7972 | + child | ||
| 7973 | + .stdin | ||
| 7974 | + .take() | ||
| 7975 | + .expect("aomenc stdin") | ||
| 7976 | + .write_all(&y4m.stdout) | ||
| 7977 | + .expect("writing y4m to aomenc"); | ||
| 7978 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 7979 | + assert!( | ||
| 7980 | + out.status.success(), | ||
| 7981 | + "aomenc refused the fixture: {}", | ||
| 7982 | + String::from_utf8_lossy(&out.stderr) | ||
| 7983 | + ); | ||
| 7984 | + let stream = out.stdout; | ||
| 7985 | + | ||
| 7986 | + let mut probe = Av1Parser::new(); | ||
| 7987 | + let mut pos = 0usize; | ||
| 7988 | + let mut tile_group_obus = 0u32; | ||
| 7989 | + while pos < stream.len() { | ||
| 7990 | + let obu = probe.parse_obu(&stream[pos..]).unwrap(); | ||
| 7991 | + pos += obu.total_size; | ||
| 7992 | + if matches!(obu.kind, ObuKind::TileGroup(_)) { | ||
| 7993 | + tile_group_obus += 1; | ||
| 7994 | + } | ||
| 7995 | + } | ||
| 7996 | + if tile_group_obus > 1 { | ||
| 7997 | + saw_multiple_tile_group_obus = true; | ||
| 7998 | + } | ||
| 7999 | + | ||
| 8000 | + let before = crate::decode::tile_hits(); | ||
| 8001 | + let pictures = match decode_stream(&stream) { | ||
| 8002 | + Err(e) => { | ||
| 8003 | + let msg = e.to_string(); | ||
| 8004 | + assert!( | ||
| 8005 | + msg.contains("unsupported"), | ||
| 8006 | + "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" | ||
| 8007 | + ); | ||
| 8008 | + named_refusals += 1; | ||
| 8009 | + eprintln!("seed {seed} refusal: {msg}"); | ||
| 8010 | + continue; | ||
| 8011 | + } | ||
| 8012 | + Ok(pictures) => pictures, | ||
| 8013 | + }; | ||
| 8014 | + let hits = crate::decode::tile_hits() - before; | ||
| 8015 | + assert!( | ||
| 8016 | + hits > 0, | ||
| 8017 | + "{NAME}: tile_hits delta was {hits}, expected >0 (seed {seed})" | ||
| 8018 | + ); | ||
| 8019 | + assert_eq!(pictures.len(), 1, "{NAME}: expected exactly 1 picture (seed {seed})"); | ||
| 8020 | + | ||
| 8021 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); | ||
| 8022 | + assert_eq!(pictures[0].y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); | ||
| 8023 | + assert_eq!(pictures[0].u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); | ||
| 8024 | + assert_eq!(pictures[0].v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); | ||
| 8025 | + matched += 1; | ||
| 8026 | + } | ||
| 8027 | + assert!( | ||
| 8028 | + matched > 0, | ||
| 8029 | + "{NAME}: every attempt refused ({named_refusals} refusals); decode_stream never \ | ||
| 8030 | + decoded a several-tile-group-OBUs stream" | ||
| 8031 | + ); | ||
| 8032 | + assert!( | ||
| 8033 | + saw_multiple_tile_group_obus, | ||
| 8034 | + "{NAME}: no attempt actually split into more than one OBU_TILE_GROUP -- this sweep \ | ||
| 8035 | + proves nothing about the multi-tile-group case (aomenc's --num-tile-groups is \ | ||
| 8036 | + RD/heuristic-driven, same as tile sizing)" | ||
| 8037 | + ); | ||
| 8038 | + eprintln!( | ||
| 8039 | + "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" | ||
| 8040 | + ); | ||
| 8041 | + } | ||
| 7833 | } | 8042 | } |