Merge branch 'lane-av1dec' (r4): public decode_stream + NEARMV + real bench row
Browse files at this commit4 changed files +512 -15
- crates/ec-av1/src/decode.rs +211 -9
- crates/ec-av1/src/lib.rs +1 -0
- crates/ec-av1/src/stream.rs +282 -0
- tools/ec-bench/src/main.rs +18 -6
crates/ec-av1/src/decode.rsmodified+211 -9
| @@ -1131,9 +1131,10 @@ fn read_inter_plane( | ||
| 1131 | 1131 | /// |
| 1132 | 1132 | /// # Errors |
| 1133 | 1133 | /// Returns an error when a symbol names anything this decoder does not |
| 1134 | -/// reconstruct: a reference other than `LAST_FRAME`, an inter mode other | |
| 1135 | -/// than `NEARESTMV`/`NEWMV`, or an intra mode/tx-type outside what | |
| 1136 | -/// [`read_intra_mode`]/[`read_coeffs`] already refuse. | |
| 1134 | +/// reconstruct: a reference other than `LAST_FRAME`, `GLOBALMV` (round 3; | |
| 1135 | +/// `NEARESTMV`/`NEARMV`/`NEWMV` are all reconstructed), or an intra | |
| 1136 | +/// mode/tx-type outside what [`read_intra_mode`]/[`read_coeffs`] already | |
| 1137 | +/// refuse. | |
| 1137 | 1138 | #[allow(clippy::too_many_arguments)] |
| 1138 | 1139 | fn decode_inter_block( |
| 1139 | 1140 | dec: &mut SymbolDecoder, |
| @@ -1212,13 +1213,29 @@ fn decode_inter_block( | ||
| 1212 | 1213 | ) |
| 1213 | 1214 | } else { |
| 1214 | 1215 | let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1; |
| 1215 | - let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; | |
| 1216 | - if !not_zero || !nearest { | |
| 1217 | - return Err(unsupported( | |
| 1218 | - "an inter mode other than NEARESTMV/NEWMV (round 2)", | |
| 1219 | - )); | |
| 1216 | + if !not_zero { | |
| 1217 | + return Err(unsupported("GLOBALMV (round 3)")); | |
| 1220 | 1218 | } |
| 1221 | - (stack.nearest_mv, false) | |
| 1219 | + let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0; | |
| 1220 | + let mv = if nearest { | |
| 1221 | + stack.nearest_mv | |
| 1222 | + } else { | |
| 1223 | + // NEARMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting | |
| 1224 | + // at 1): read at most two more `drl_mode` bits, one per stack | |
| 1225 | + // entry past the second, stopping at the first `0`. | |
| 1226 | + // `drl_ctx[idx]` is the context between `entries[idx]` and | |
| 1227 | + // `entries[idx + 1]` (`MvStack::drl_ctx`'s own doc), which is | |
| 1228 | + // exactly the pair this index is choosing between. | |
| 1229 | + let mut idx = 1usize; | |
| 1230 | + while idx < 3 && stack.entries.len() > idx + 1 { | |
| 1231 | + if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 { | |
| 1232 | + break; | |
| 1233 | + } | |
| 1234 | + idx += 1; | |
| 1235 | + } | |
| 1236 | + stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) | |
| 1237 | + }; | |
| 1238 | + (mv, false) | |
| 1222 | 1239 | }; |
| 1223 | 1240 | for dr in 0..bw4 { |
| 1224 | 1241 | for dc in 0..bw4 { |
| @@ -2117,6 +2134,191 @@ mod tests { | ||
| 2117 | 2134 | |
| 2118 | 2135 | /// The GOP cross-oracle: ffmpeg decodes the whole sequence the same way |
| 2119 | 2136 | /// this module does, frame for frame. |
| 2137 | + /// A NEARMV block (`not_new`, `not_zero`, `!nearest`) predicts from the | |
| 2138 | + /// MV stack's *second* candidate (`near_mv`), not its first | |
| 2139 | + /// (`nearest_mv`) -- a hand-built symbol stream drives | |
| 2140 | + /// [`decode_inter_block`] directly, since this crate's own encoder never | |
| 2141 | + /// writes NEARMV (round 3: it never chooses the mode). | |
| 2142 | + #[test] | |
| 2143 | + fn a_nearmv_block_predicts_from_the_stacks_second_candidate() { | |
| 2144 | + use crate::msac::SymbolEncoder; | |
| 2145 | + use crate::mvstack::{MiGrid, MiInfo, find_mv_stack, single_ref_ctx}; | |
| 2146 | + | |
| 2147 | + const LAST_FRAME: i8 = 1; | |
| 2148 | + let (mi_cols, mi_rows) = (12u32, 12u32); | |
| 2149 | + let side = 16usize; // one 16x16 leaf block | |
| 2150 | + let (r, c) = (1usize, 1usize); // `at`, in SUB (16px) units | |
| 2151 | + let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize); | |
| 2152 | + let bw4 = side / 4; | |
| 2153 | + | |
| 2154 | + let mut grid = MiGrid::new(mi_cols as usize, mi_rows as usize); | |
| 2155 | + let (above_mv, left_mv) = ((4, 4), (8, 8)); | |
| 2156 | + let neighbour = |mv| MiInfo { | |
| 2157 | + is_inter: true, | |
| 2158 | + ref_frame: LAST_FRAME, | |
| 2159 | + mv, | |
| 2160 | + is_new_mv: false, | |
| 2161 | + size: 1, | |
| 2162 | + }; | |
| 2163 | + for col in mi_col..mi_col + bw4 { | |
| 2164 | + grid.set(mi_row - 1, col, neighbour(above_mv)); | |
| 2165 | + } | |
| 2166 | + for row in mi_row..mi_row + bw4 { | |
| 2167 | + grid.set(row, mi_col - 1, neighbour(left_mv)); | |
| 2168 | + } | |
| 2169 | + | |
| 2170 | + let stack = find_mv_stack( | |
| 2171 | + &grid, | |
| 2172 | + mi_row, | |
| 2173 | + mi_col, | |
| 2174 | + bw4, | |
| 2175 | + bw4, | |
| 2176 | + LAST_FRAME, | |
| 2177 | + mi_cols as usize, | |
| 2178 | + mi_rows as usize, | |
| 2179 | + ); | |
| 2180 | + // Sanity on the test's own setup, not the code under test: two equal- | |
| 2181 | + // weight candidates keep scan order (above, then left -- module doc). | |
| 2182 | + assert_eq!(stack.entries.len(), 2); | |
| 2183 | + assert_eq!(stack.nearest_mv, above_mv); | |
| 2184 | + assert_eq!(stack.near_mv, left_mv); | |
| 2185 | + | |
| 2186 | + let (skip_ctx, ii_ctx, sr_ctx) = ( | |
| 2187 | + 0usize, | |
| 2188 | + intra_inter_ctx(true, true, false, false), | |
| 2189 | + single_ref_ctx(false), | |
| 2190 | + ); | |
| 2191 | + | |
| 2192 | + let mut cdfs = Cdfs::new(q_ctx_of(100)); | |
| 2193 | + let mut enc = SymbolEncoder::new(); | |
| 2194 | + enc.symbol(1, &mut cdfs.skip[skip_ctx]); // skip | |
| 2195 | + enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]); // is_inter | |
| 2196 | + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][0]); // LAST_FRAME | |
| 2197 | + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][2]); | |
| 2198 | + enc.symbol(0, &mut cdfs.single_ref[sr_ctx][3]); | |
| 2199 | + enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not_new | |
| 2200 | + enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not_zero | |
| 2201 | + enc.symbol(1, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // !nearest -> NEARMV | |
| 2202 | + let data = enc.finish(); | |
| 2203 | + | |
| 2204 | + let (width, height) = (64usize, 64usize); | |
| 2205 | + let ref_pattern: Vec<u8> = (0..width * height).map(|i| (i % 251) as u8).collect(); | |
| 2206 | + let ref_plane = |scale: usize| PlaneBuf { | |
| 2207 | + data: ref_pattern | |
| 2208 | + .iter() | |
| 2209 | + .step_by(scale.max(1)) | |
| 2210 | + .take((width / scale) * (height / scale)) | |
| 2211 | + .copied() | |
| 2212 | + .collect(), | |
| 2213 | + width: width / scale, | |
| 2214 | + height: height / scale, | |
| 2215 | + true_width: width / scale, | |
| 2216 | + true_height: height / scale, | |
| 2217 | + }; | |
| 2218 | + let ref_y = PlaneBuf { | |
| 2219 | + data: ref_pattern.clone(), | |
| 2220 | + width, | |
| 2221 | + height, | |
| 2222 | + true_width: width, | |
| 2223 | + true_height: height, | |
| 2224 | + }; | |
| 2225 | + let ref_u = ref_plane(2); | |
| 2226 | + let ref_v = ref_plane(2); | |
| 2227 | + | |
| 2228 | + let mut y = PlaneBuf { | |
| 2229 | + data: vec![0u8; width * height], | |
| 2230 | + width, | |
| 2231 | + height, | |
| 2232 | + true_width: width, | |
| 2233 | + true_height: height, | |
| 2234 | + }; | |
| 2235 | + let mut u = PlaneBuf { | |
| 2236 | + data: vec![0u8; (width / 2) * (height / 2)], | |
| 2237 | + width: width / 2, | |
| 2238 | + height: height / 2, | |
| 2239 | + true_width: width / 2, | |
| 2240 | + true_height: height / 2, | |
| 2241 | + }; | |
| 2242 | + let mut v = PlaneBuf { | |
| 2243 | + data: vec![0u8; (width / 2) * (height / 2)], | |
| 2244 | + width: width / 2, | |
| 2245 | + height: height / 2, | |
| 2246 | + true_width: width / 2, | |
| 2247 | + true_height: height / 2, | |
| 2248 | + }; | |
| 2249 | + | |
| 2250 | + let mut cdfs = Cdfs::new(q_ctx_of(100)); | |
| 2251 | + let mut dec = SymbolDecoder::new(&data); | |
| 2252 | + let mut neighbours = Neighbours::new(3, 3, mi_cols as usize, mi_rows as usize); | |
| 2253 | + decode_inter_block( | |
| 2254 | + &mut dec, | |
| 2255 | + &mut cdfs, | |
| 2256 | + &mut neighbours, | |
| 2257 | + &mut grid, | |
| 2258 | + (r, c), | |
| 2259 | + side, | |
| 2260 | + mi_cols, | |
| 2261 | + mi_rows, | |
| 2262 | + &mut y, | |
| 2263 | + &mut u, | |
| 2264 | + &mut v, | |
| 2265 | + &ref_y, | |
| 2266 | + &ref_u, | |
| 2267 | + &ref_v, | |
| 2268 | + 100, | |
| 2269 | + TxbSet::Luma16, | |
| 2270 | + TxbSet::Luma16Inter, | |
| 2271 | + TxbSet::Chroma8, | |
| 2272 | + TX16, | |
| 2273 | + TX8, | |
| 2274 | + &[], | |
| 2275 | + &[], | |
| 2276 | + 0, | |
| 2277 | + ) | |
| 2278 | + .unwrap(); | |
| 2279 | + | |
| 2280 | + let (px, py) = (c * SUB, r * SUB); | |
| 2281 | + let mut want_near = vec![0u8; side * side]; | |
| 2282 | + crate::mc::predict( | |
| 2283 | + &ref_y.data, | |
| 2284 | + ref_y.width, | |
| 2285 | + ref_y.true_width, | |
| 2286 | + ref_y.true_height, | |
| 2287 | + mv_to_q4(px, left_mv.1, true), | |
| 2288 | + mv_to_q4(py, left_mv.0, true), | |
| 2289 | + side, | |
| 2290 | + side, | |
| 2291 | + &mut want_near, | |
| 2292 | + ); | |
| 2293 | + let mut want_nearest = vec![0u8; side * side]; | |
| 2294 | + crate::mc::predict( | |
| 2295 | + &ref_y.data, | |
| 2296 | + ref_y.width, | |
| 2297 | + ref_y.true_width, | |
| 2298 | + ref_y.true_height, | |
| 2299 | + mv_to_q4(px, above_mv.1, true), | |
| 2300 | + mv_to_q4(py, above_mv.0, true), | |
| 2301 | + side, | |
| 2302 | + side, | |
| 2303 | + &mut want_nearest, | |
| 2304 | + ); | |
| 2305 | + assert_ne!( | |
| 2306 | + want_near, want_nearest, | |
| 2307 | + "test setup: the two candidate MVs must predict differently" | |
| 2308 | + ); | |
| 2309 | + | |
| 2310 | + let mut got = vec![0u8; side * side]; | |
| 2311 | + for row in 0..side { | |
| 2312 | + got[row * side..(row + 1) * side].copy_from_slice( | |
| 2313 | + &y.data[(py + row) * y.width + px..(py + row) * y.width + px + side], | |
| 2314 | + ); | |
| 2315 | + } | |
| 2316 | + assert_eq!( | |
| 2317 | + got, want_near, | |
| 2318 | + "NEARMV predicted from the wrong stack entry" | |
| 2319 | + ); | |
| 2320 | + } | |
| 2321 | + | |
| 2120 | 2322 | #[test] |
| 2121 | 2323 | fn ffmpeg_and_this_decoder_agree_on_a_gop() { |
| 2122 | 2324 | if !have_ffmpeg() { |
crates/ec-av1/src/lib.rsmodified+1 -0
| @@ -27,5 +27,6 @@ pub mod mvstack; | ||
| 27 | 27 | pub mod obu; |
| 28 | 28 | pub mod quant; |
| 29 | 29 | pub mod sequence; |
| 30 | +pub mod stream; | |
| 30 | 31 | pub mod tile; |
| 31 | 32 | pub mod transform; |
crates/ec-av1/src/stream.rsadded+282 -0
| @@ -0,0 +1,282 @@ | ||
| 1 | +//! The stream-level entry point a real decoder needs: walk a raw AV1 OBU | |
| 2 | +//! stream (`Encoded::stream`, or any low-overhead-format bitstream this | |
| 3 | +//! crate's own writers produce) via [`ec_av1_syntax::Av1Parser`] and dispatch | |
| 4 | +//! each frame's tile payload to [`crate::decode::decode_key_frame_tile`] / | |
| 5 | +//! [`crate::decode::decode_inter_frame_tile`], threading a single-slot DPB | |
| 6 | +//! exactly as [`crate::encode::encode_sequence`] does on the write side. | |
| 7 | +//! | |
| 8 | +//! This is the only reachable path for a caller that does not have the | |
| 9 | +//! encoder's own `Encoded::tile`/`mi_cols`/`mi_rows`/`base_q_idx` fields (they | |
| 10 | +//! are `pub(crate)`, and rightly so: the wire is `stream`, everything else is | |
| 11 | +//! an implementation detail of how this crate happens to build it). | |
| 12 | + | |
| 13 | +use ec_av1_syntax::{Av1Parser, FrameType, ObuKind}; | |
| 14 | +use ec_core::{Error, Result}; | |
| 15 | + | |
| 16 | +use crate::decode::{decode_inter_frame_tile, decode_key_frame_tile}; | |
| 17 | +use crate::encode::Picture; | |
| 18 | + | |
| 19 | +/// Decode every frame in a raw AV1 OBU stream, in coding order. | |
| 20 | +/// | |
| 21 | +/// A key frame becomes the reference for the inter frames that follow it, | |
| 22 | +/// until the next key frame resets the DPB — the same single-slot chain | |
| 23 | +/// [`crate::encode::encode_sequence`] threads on the write side. Only a | |
| 24 | +/// `Frame` OBU (an uncompressed header and its tile group in one OBU, which | |
| 25 | +/// is all this crate's encoder ever writes) carries a tile payload; sequence | |
| 26 | +/// headers and temporal delimiters are consumed for the state they carry (or | |
| 27 | +/// skipped) and produce no picture. | |
| 28 | +/// | |
| 29 | +/// # Errors | |
| 30 | +/// Returns an error when the stream is truncated or malformed (as | |
| 31 | +/// [`Av1Parser`] reports it), when a frame header names anything this | |
| 32 | +/// crate's tile decoders do not reconstruct (see their own docs), or when an | |
| 33 | +/// inter frame appears before any key frame has supplied a reference. | |
| 34 | +pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |
| 35 | + let mut parser = Av1Parser::new(); | |
| 36 | + let mut pictures = Vec::new(); | |
| 37 | + let mut reference: Option<Picture> = None; | |
| 38 | + | |
| 39 | + let mut pos = 0usize; | |
| 40 | + while pos < data.len() { | |
| 41 | + let obu = parser.parse_obu(&data[pos..])?; | |
| 42 | + let obu_offset = pos; | |
| 43 | + pos += obu.total_size; | |
| 44 | + | |
| 45 | + let ObuKind::Frame(header, tiles) = obu.kind else { | |
| 46 | + continue; | |
| 47 | + }; | |
| 48 | + let tile = tiles.first().ok_or_else(|| { | |
| 49 | + Error::unsupported("AV1 decode_stream", "a frame OBU with no tile group") | |
| 50 | + })?; | |
| 51 | + // `Tile::offset` is relative to the buffer `parse_obu` was handed | |
| 52 | + // (`&data[pos..]` at the time this OBU was parsed), so it is relative | |
| 53 | + // to `obu_offset`, not to `data` as a whole. | |
| 54 | + let start = obu_offset + tile.offset; | |
| 55 | + let tile_bytes = data.get(start..start + tile.size).ok_or(Error::NeedMore)?; | |
| 56 | + | |
| 57 | + let picture = if header.frame_type == FrameType::Key { | |
| 58 | + decode_key_frame_tile( | |
| 59 | + tile_bytes, | |
| 60 | + header.mi_cols, | |
| 61 | + header.mi_rows, | |
| 62 | + header.quantization.base_q_idx, | |
| 63 | + header.frame_width, | |
| 64 | + header.frame_height, | |
| 65 | + )? | |
| 66 | + } else { | |
| 67 | + let reference = reference.as_ref().ok_or_else(|| { | |
| 68 | + Error::unsupported( | |
| 69 | + "AV1 decode_stream", | |
| 70 | + "an inter frame with no key frame before it", | |
| 71 | + ) | |
| 72 | + })?; | |
| 73 | + decode_inter_frame_tile( | |
| 74 | + tile_bytes, | |
| 75 | + header.mi_cols, | |
| 76 | + header.mi_rows, | |
| 77 | + header.quantization.base_q_idx, | |
| 78 | + header.frame_width, | |
| 79 | + header.frame_height, | |
| 80 | + reference, | |
| 81 | + )? | |
| 82 | + }; | |
| 83 | + reference = Some(picture.clone()); | |
| 84 | + pictures.push(picture); | |
| 85 | + } | |
| 86 | + Ok(pictures) | |
| 87 | +} | |
| 88 | + | |
| 89 | +#[cfg(test)] | |
| 90 | +mod tests { | |
| 91 | + use super::*; | |
| 92 | + use crate::encode::{Picture as Pic, encode_key_frame, encode_sequence}; | |
| 93 | + | |
| 94 | + fn test_card(width: usize, height: usize) -> Pic { | |
| 95 | + let mut picture = Pic::grey(width, height); | |
| 96 | + for row in 0..height { | |
| 97 | + for col in 0..width { | |
| 98 | + picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u8; | |
| 99 | + } | |
| 100 | + } | |
| 101 | + for row in 0..height / 2 { | |
| 102 | + for col in 0..width / 2 { | |
| 103 | + let i = row * width / 2 + col; | |
| 104 | + picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u8; | |
| 105 | + picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u8; | |
| 106 | + } | |
| 107 | + } | |
| 108 | + picture | |
| 109 | + } | |
| 110 | + | |
| 111 | + fn panned_test_card(width: usize, height: usize, shift: i64) -> Pic { | |
| 112 | + let mut picture = Pic::grey(width, height); | |
| 113 | + for y in 0..height { | |
| 114 | + for x in 0..width { | |
| 115 | + let sx = (x as i64 - shift).rem_euclid(width as i64) as f64; | |
| 116 | + let gradient = sx * 200.0 / width as f64; | |
| 117 | + picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u8; | |
| 118 | + } | |
| 119 | + } | |
| 120 | + for y in 0..height / 2 { | |
| 121 | + for x in 0..width / 2 { | |
| 122 | + let sx = (x as i64 - shift / 2).rem_euclid((width / 2) as i64) as usize; | |
| 123 | + let i = y * width / 2 + x; | |
| 124 | + picture.u[i] = (100 + (sx * 60 / (width / 2))) as u8; | |
| 125 | + picture.v[i] = (200 - (y * 80 / (height / 2))) as u8; | |
| 126 | + } | |
| 127 | + } | |
| 128 | + picture | |
| 129 | + } | |
| 130 | + | |
| 131 | + /// `decode_stream` on a lone key frame's stream matches both the tile | |
| 132 | + /// path (`decode_key_frame_tile` called directly on `Encoded::tile`) and | |
| 133 | + /// the encoder's own reconstruction, at an even and an odd size. | |
| 134 | + #[test] | |
| 135 | + fn decode_stream_matches_the_tile_path_on_a_key_frame() { | |
| 136 | + for &(width, height) in &[(64usize, 64usize), (216, 96)] { | |
| 137 | + let picture = test_card(width, height); | |
| 138 | + let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); | |
| 139 | + let via_tile = decode_key_frame_tile( | |
| 140 | + &encoded.tile, | |
| 141 | + encoded.mi_cols, | |
| 142 | + encoded.mi_rows, | |
| 143 | + encoded.base_q_idx, | |
| 144 | + width as u32, | |
| 145 | + height as u32, | |
| 146 | + ) | |
| 147 | + .unwrap(); | |
| 148 | + let via_stream = decode_stream(&encoded.stream).unwrap(); | |
| 149 | + assert_eq!(via_stream.len(), 1, "{width}x{height}: one picture"); | |
| 150 | + assert_eq!(via_stream[0].y, via_tile.y, "{width}x{height}: luma"); | |
| 151 | + assert_eq!(via_stream[0].u, via_tile.u, "{width}x{height}: U"); | |
| 152 | + assert_eq!(via_stream[0].v, via_tile.v, "{width}x{height}: V"); | |
| 153 | + assert_eq!( | |
| 154 | + via_stream[0].y, encoded.reconstruction.y, | |
| 155 | + "{width}x{height}: luma vs encoder reconstruction" | |
| 156 | + ); | |
| 157 | + } | |
| 158 | + } | |
| 159 | + | |
| 160 | + /// A GOP (key frame plus panned inter frames, so blocks actually take | |
| 161 | + /// motion) decodes bit-exact through `decode_stream` alone, threading its | |
| 162 | + /// own DPB rather than a test-supplied reference. | |
| 163 | + fn gop_round_trips(width: usize, height: usize) { | |
| 164 | + let pictures: Vec<_> = (0..4) | |
| 165 | + .map(|i| panned_test_card(width, height, i * 3)) | |
| 166 | + .collect(); | |
| 167 | + let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); | |
| 168 | + let decoded = decode_stream(&encoded.stream).unwrap(); | |
| 169 | + assert_eq!(decoded.len(), encoded.frames.len()); | |
| 170 | + for (i, (got, frame)) in decoded.iter().zip(&encoded.frames).enumerate() { | |
| 171 | + assert_eq!( | |
| 172 | + got.y, frame.reconstruction.y, | |
| 173 | + "{width}x{height} frame {i} luma" | |
| 174 | + ); | |
| 175 | + assert_eq!( | |
| 176 | + got.u, frame.reconstruction.u, | |
| 177 | + "{width}x{height} frame {i} U" | |
| 178 | + ); | |
| 179 | + assert_eq!( | |
| 180 | + got.v, frame.reconstruction.v, | |
| 181 | + "{width}x{height} frame {i} V" | |
| 182 | + ); | |
| 183 | + } | |
| 184 | + } | |
| 185 | + | |
| 186 | + #[test] | |
| 187 | + fn decode_stream_round_trips_a_gop() { | |
| 188 | + gop_round_trips(128, 64); | |
| 189 | + } | |
| 190 | + | |
| 191 | + /// Same claim at a size that is not a whole number of 32x32 blocks, so | |
| 192 | + /// the inter frame's 16x16-leaf split path is exercised too. | |
| 193 | + #[test] | |
| 194 | + fn decode_stream_round_trips_an_odd_size_gop() { | |
| 195 | + gop_round_trips(216, 96); | |
| 196 | + } | |
| 197 | + | |
| 198 | + use std::io::Write; | |
| 199 | + use std::process::{Command, Stdio}; | |
| 200 | + | |
| 201 | + fn have_ffmpeg() -> bool { | |
| 202 | + Command::new("ffmpeg") | |
| 203 | + .arg("-version") | |
| 204 | + .stdout(Stdio::null()) | |
| 205 | + .stderr(Stdio::null()) | |
| 206 | + .status() | |
| 207 | + .is_ok_and(|s| s.success()) | |
| 208 | + } | |
| 209 | + | |
| 210 | + /// Decodes `frames` concatenated 4:2:0 frames out of one AV1 OBU stream. | |
| 211 | + fn ffmpeg_decode_sequence( | |
| 212 | + stream: &[u8], | |
| 213 | + width: usize, | |
| 214 | + height: usize, | |
| 215 | + frames: usize, | |
| 216 | + ) -> Vec<Pic> { | |
| 217 | + let mut child = Command::new("ffmpeg") | |
| 218 | + .args([ | |
| 219 | + "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", | |
| 220 | + ]) | |
| 221 | + .stdin(Stdio::piped()) | |
| 222 | + .stdout(Stdio::piped()) | |
| 223 | + .stderr(Stdio::piped()) | |
| 224 | + .spawn() | |
| 225 | + .expect("ffmpeg failed to start"); | |
| 226 | + child | |
| 227 | + .stdin | |
| 228 | + .take() | |
| 229 | + .expect("ffmpeg stdin") | |
| 230 | + .write_all(stream) | |
| 231 | + .expect("writing the stream to ffmpeg"); | |
| 232 | + let out = child.wait_with_output().expect("ffmpeg failed to run"); | |
| 233 | + assert!( | |
| 234 | + out.status.success(), | |
| 235 | + "ffmpeg refused the stream: {}", | |
| 236 | + String::from_utf8_lossy(&out.stderr) | |
| 237 | + ); | |
| 238 | + let (luma, chroma) = (width * height, width * height / 4); | |
| 239 | + let frame_bytes = luma + 2 * chroma; | |
| 240 | + assert_eq!( | |
| 241 | + out.stdout.len(), | |
| 242 | + frame_bytes * frames, | |
| 243 | + "expected {frames} 4:2:0 frames, ffmpeg said: {}", | |
| 244 | + String::from_utf8_lossy(&out.stderr) | |
| 245 | + ); | |
| 246 | + (0..frames) | |
| 247 | + .map(|i| { | |
| 248 | + let base = i * frame_bytes; | |
| 249 | + Pic { | |
| 250 | + width, | |
| 251 | + height, | |
| 252 | + y: out.stdout[base..base + luma].to_vec(), | |
| 253 | + u: out.stdout[base + luma..base + luma + chroma].to_vec(), | |
| 254 | + v: out.stdout[base + luma + chroma..base + frame_bytes].to_vec(), | |
| 255 | + } | |
| 256 | + }) | |
| 257 | + .collect() | |
| 258 | + } | |
| 259 | + | |
| 260 | + /// `decode_stream` agrees with ffmpeg/dav1d on the same wire bytes -- an | |
| 261 | + /// independent decoder, not just this crate checking its own tile path. | |
| 262 | + #[test] | |
| 263 | + fn decode_stream_agrees_with_ffmpeg_on_a_gop() { | |
| 264 | + if !have_ffmpeg() { | |
| 265 | + eprintln!("SKIP decode_stream_agrees_with_ffmpeg_on_a_gop: no ffmpeg"); | |
| 266 | + return; | |
| 267 | + } | |
| 268 | + let (width, height) = (128usize, 64usize); | |
| 269 | + let pictures: Vec<_> = (0..3) | |
| 270 | + .map(|i| panned_test_card(width, height, i * 3)) | |
| 271 | + .collect(); | |
| 272 | + let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); | |
| 273 | + let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, 3); | |
| 274 | + let decoded = decode_stream(&encoded.stream).unwrap(); | |
| 275 | + assert_eq!(decoded.len(), 3); | |
| 276 | + for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { | |
| 277 | + assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg"); | |
| 278 | + assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg"); | |
| 279 | + assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg"); | |
| 280 | + } | |
| 281 | + } | |
| 282 | +} |
tools/ec-bench/src/main.rsmodified+18 -6
| @@ -312,19 +312,16 @@ fn bench_h265_encode(rows: &mut Vec<Row>) { | ||
| 312 | 312 | // --------------------------------------------------------------------------- |
| 313 | 313 | |
| 314 | 314 | fn bench_av1_encode(rows: &mut Vec<Row>) { |
| 315 | - rows.push(missing_direction( | |
| 316 | - "ec-av1", | |
| 317 | - "decode", | |
| 318 | - "encode-only crate, no decoder present", | |
| 319 | - )); | |
| 320 | 315 | let Some(src) = real_video("h264") else { |
| 321 | 316 | rows.push(missing("ec-av1", "encode")); |
| 317 | + rows.push(missing("ec-av1", "decode")); | |
| 322 | 318 | return; |
| 323 | 319 | }; |
| 324 | 320 | // AV1's block grid is 32x32 superblocks: pick a size already a multiple of it. |
| 325 | 321 | let (planes, w, h, n) = extract_yuv420p(&src, 640, 384, 10); |
| 326 | 322 | if n == 0 { |
| 327 | 323 | rows.push(missing("ec-av1", "encode")); |
| 324 | + rows.push(missing("ec-av1", "decode")); | |
| 328 | 325 | return; |
| 329 | 326 | } |
| 330 | 327 | let cfg = Av1Config { |
| @@ -336,6 +333,7 @@ fn bench_av1_encode(rows: &mut Vec<Row>) { | ||
| 336 | 333 | }; |
| 337 | 334 | let mut enc = Av1Encoder::new(cfg).expect("av1 encoder"); |
| 338 | 335 | let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; |
| 336 | + let mut stream = Vec::new(); | |
| 339 | 337 | let start = Instant::now(); |
| 340 | 338 | for frame in planes.chunks_exact(frame_len) { |
| 341 | 339 | let (y, rest) = frame.split_at((w * h) as usize); |
| @@ -347,7 +345,8 @@ fn bench_av1_encode(rows: &mut Vec<Row>) { | ||
| 347 | 345 | u: u.to_vec(), |
| 348 | 346 | v: v.to_vec(), |
| 349 | 347 | }; |
| 350 | - enc.encode(&pic).expect("av1 encode"); | |
| 348 | + let packet = enc.encode(&pic).expect("av1 encode"); | |
| 349 | + stream.extend_from_slice(&packet.data); | |
| 351 | 350 | } |
| 352 | 351 | let wall = start.elapsed().as_secs_f64(); |
| 353 | 352 | let media_s = n as f64 / 30.0; |
| @@ -359,6 +358,19 @@ fn bench_av1_encode(rows: &mut Vec<Row>) { | ||
| 359 | 358 | wall_ms: wall * 1000.0, |
| 360 | 359 | rtf: (wall > 0.0).then_some(media_s / wall), |
| 361 | 360 | }); |
| 361 | + | |
| 362 | + let start = Instant::now(); | |
| 363 | + let pictures = ec_av1::stream::decode_stream(&stream).expect("av1 decode_stream"); | |
| 364 | + let wall = start.elapsed().as_secs_f64(); | |
| 365 | + assert_eq!(pictures.len(), n as usize, "decode_stream frame count"); | |
| 366 | + rows.push(Row { | |
| 367 | + component: "ec-av1", | |
| 368 | + direction: "decode", | |
| 369 | + content: format!("{w}x{h}, {n} frames, gop=10"), | |
| 370 | + media: format!("{media_s:.1}s"), | |
| 371 | + wall_ms: wall * 1000.0, | |
| 372 | + rtf: (wall > 0.0).then_some(media_s / wall), | |
| 373 | + }); | |
| 362 | 374 | } |
| 363 | 375 | |
| 364 | 376 | // --------------------------------------------------------------------------- |