Pixel-exact single-ref decode for LAST2/LAST3/BWDREF/ALTREF2/ALTREF
Browse files at this commit1719c8e8cb817cfa9c6eb95db428568bb26e7f1eParent: 25e8a4f- feat
- pixel-exact single-ref decode for LAST2/LAST3/BWDREF/ALTREF2/ALTREF
2 changed files +318 -1
- crates/ec-av1/src/decode.rs +40 -1
- crates/ec-av1/src/stream.rs +278 -0
crates/ec-av1/src/decode.rsmodified+40 -1
| @@ -118,6 +118,34 @@ pub(crate) fn non_last_ref_hits() -> usize { | |||
| 118 | NON_LAST_REF_HITS.load(std::sync::atomic::Ordering::Relaxed) | 118 | NON_LAST_REF_HITS.load(std::sync::atomic::Ordering::Relaxed) |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | +/// Per-reference [`NON_LAST_REF_HITS`] breakdown (lane-av1refs): one counter | ||
| 122 | +/// per `MV_REFERENCE_FRAME` past `LAST_FRAME`, so a gate targeting one | ||
| 123 | +/// specific reference (`LAST2`/`LAST3`/`GOLDEN`/`BWDREF`/`ALTREF2`/`ALTREF`) | ||
| 124 | +/// can prove THAT reference fired rather than any non-`LAST_FRAME` one -- | ||
| 125 | +/// `NON_LAST_REF_HITS` alone cannot tell a `LAST2_FRAME` gate from a | ||
| 126 | +/// `GOLDEN_FRAME` draw it did not ask for. | ||
| 127 | +static LAST2_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 128 | +static LAST3_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 129 | +static GOLDEN_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 130 | +static BWDREF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 131 | +static ALTREF2_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 132 | +static ALTREF_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | ||
| 133 | + | ||
| 134 | +/// Current value of the counter for `ref_frame` (`LAST2_FRAME`..=`ALTREF_FRAME`; | ||
| 135 | +/// panics on `LAST_FRAME`/`INTRA_FRAME`/`NONE`, which have no counter here). | ||
| 136 | +pub(crate) fn ref_hits(ref_frame: i8) -> usize { | ||
| 137 | + let c = match ref_frame { | ||
| 138 | + LAST2_FRAME => &LAST2_HITS, | ||
| 139 | + LAST3_FRAME => &LAST3_HITS, | ||
| 140 | + GOLDEN_FRAME => &GOLDEN_HITS, | ||
| 141 | + BWDREF_FRAME => &BWDREF_HITS, | ||
| 142 | + ALTREF2_FRAME => &ALTREF2_HITS, | ||
| 143 | + ALTREF_FRAME => &ALTREF_HITS, | ||
| 144 | + other => panic!("ref_hits: no per-reference counter for ref_frame {other}"), | ||
| 145 | + }; | ||
| 146 | + c.load(std::sync::atomic::Ordering::Relaxed) | ||
| 147 | +} | ||
| 148 | + | ||
| 121 | thread_local! { | 149 | thread_local! { |
| 122 | /// The sequence header's `enable_intra_edge_filter`, set once per | 150 | /// The sequence header's `enable_intra_edge_filter`, set once per |
| 123 | /// [`decode_key_frame_tile_with_cdfs`] call: [`PlaneBuf::reconstruct`]'s | 151 | /// [`decode_key_frame_tile_with_cdfs`] call: [`PlaneBuf::reconstruct`]'s |
| @@ -3665,6 +3693,16 @@ fn read_single_ref(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, above_ref: i8, left | |||
| 3665 | }; | 3693 | }; |
| 3666 | if ref_frame != LAST_FRAME { | 3694 | if ref_frame != LAST_FRAME { |
| 3667 | NON_LAST_REF_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 3695 | NON_LAST_REF_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 3696 | + let c = match ref_frame { | ||
| 3697 | + LAST2_FRAME => &LAST2_HITS, | ||
| 3698 | + LAST3_FRAME => &LAST3_HITS, | ||
| 3699 | + GOLDEN_FRAME => &GOLDEN_HITS, | ||
| 3700 | + BWDREF_FRAME => &BWDREF_HITS, | ||
| 3701 | + ALTREF2_FRAME => &ALTREF2_HITS, | ||
| 3702 | + ALTREF_FRAME => &ALTREF_HITS, | ||
| 3703 | + _ => unreachable!("read_single_ref only ever returns LAST_FRAME..=ALTREF_FRAME"), | ||
| 3704 | + }; | ||
| 3705 | + c.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 3668 | } | 3706 | } |
| 3669 | ref_frame | 3707 | ref_frame |
| 3670 | } | 3708 | } |
| @@ -4714,7 +4752,8 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 4714 | ) | 4752 | ) |
| 4715 | }) | 4753 | }) |
| 4716 | }); | 4754 | }); |
| 4717 | - let ref_slots: RefSlots = std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c))); | 4755 | + let ref_slots: RefSlots = |
| 4756 | + std::array::from_fn(|i| owned_refs[i].as_ref().map(|(a, b, c)| (a, b, c))); | ||
| 4718 | 4757 | ||
| 4719 | let mut y = PlaneBuf { | 4758 | let mut y = PlaneBuf { |
| 4720 | data: vec![0u8; width * height], | 4759 | data: vec![0u8; width * height], |
crates/ec-av1/src/stream.rsmodified+278 -0
| @@ -2166,6 +2166,284 @@ mod tests { | |||
| 2166 | ); | 2166 | ); |
| 2167 | } | 2167 | } |
| 2168 | 2168 | ||
| 2169 | + /// lane-av1refs's decisive gate for the 5 references widened past | ||
| 2170 | + /// `LAST_FRAME`/`GOLDEN_FRAME`: same envelope and pin-on-mismatch | ||
| 2171 | + /// pattern as | ||
| 2172 | + /// [`a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact`], | ||
| 2173 | + /// generalised over `(target_ref, extra_args, frame_count)` so each of | ||
| 2174 | + /// `LAST2`/`LAST3`/`BWDREF`/`ALTREF2`/`ALTREF` gets its own attempt pool | ||
| 2175 | + /// and its own proof that `decode::ref_hits(target_ref)` -- not just | ||
| 2176 | + /// `non_last_ref_hits`, which cannot distinguish one reference from | ||
| 2177 | + /// another -- actually advanced. `--max-reference-frames=3 | ||
| 2178 | + /// --reduced-reference-set=1` (the golden gate's own narrowing) is | ||
| 2179 | + /// DROPPED here: both suppress exactly the references this gate needs | ||
| 2180 | + /// aomenc's RD to reach. | ||
| 2181 | + fn a_real_aomenc_single_ref_gate( | ||
| 2182 | + gate_name: &str, | ||
| 2183 | + target_ref: i8, | ||
| 2184 | + target_ref_name: &str, | ||
| 2185 | + extra_args: &[&str], | ||
| 2186 | + width: usize, | ||
| 2187 | + height: usize, | ||
| 2188 | + frame_count: usize, | ||
| 2189 | + attempts: u32, | ||
| 2190 | + ) { | ||
| 2191 | + if !have_ffmpeg() { | ||
| 2192 | + eprintln!("SKIP {gate_name}: no ffmpeg"); | ||
| 2193 | + return; | ||
| 2194 | + } | ||
| 2195 | + if !have_aomenc() { | ||
| 2196 | + eprintln!("SKIP {gate_name}: no aomenc at {}", aomenc_path().display()); | ||
| 2197 | + return; | ||
| 2198 | + } | ||
| 2199 | + let mut refusals = Vec::new(); | ||
| 2200 | + let mut never_fired = 0u32; | ||
| 2201 | + for attempt in 0..attempts { | ||
| 2202 | + let seed = 42 + attempt % 40; | ||
| 2203 | + let duration = frame_count as f64 / 25.0; | ||
| 2204 | + let source = | ||
| 2205 | + format!("gradients=size={width}x{height}:seed={seed}:duration={duration}:rate=25"); | ||
| 2206 | + let y4m = Command::new("ffmpeg") | ||
| 2207 | + .args([ | ||
| 2208 | + "-v", | ||
| 2209 | + "error", | ||
| 2210 | + "-f", | ||
| 2211 | + "lavfi", | ||
| 2212 | + "-i", | ||
| 2213 | + &source, | ||
| 2214 | + "-pix_fmt", | ||
| 2215 | + "yuv420p", | ||
| 2216 | + "-f", | ||
| 2217 | + "yuv4mpegpipe", | ||
| 2218 | + "-", | ||
| 2219 | + ]) | ||
| 2220 | + .stdin(Stdio::null()) | ||
| 2221 | + .stdout(Stdio::piped()) | ||
| 2222 | + .stderr(Stdio::piped()) | ||
| 2223 | + .output() | ||
| 2224 | + .expect("ffmpeg failed to run"); | ||
| 2225 | + assert!( | ||
| 2226 | + y4m.status.success(), | ||
| 2227 | + "ffmpeg fixture: {}", | ||
| 2228 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 2229 | + ); | ||
| 2230 | + let mut args: Vec<&str> = vec![ | ||
| 2231 | + "--codec=av1", | ||
| 2232 | + "--passes=1", | ||
| 2233 | + "--end-usage=q", | ||
| 2234 | + "--cq-level=55", | ||
| 2235 | + "--cpu-used=0", | ||
| 2236 | + "--kf-max-dist=1000", | ||
| 2237 | + "--threads=1", | ||
| 2238 | + "--row-mt=0", | ||
| 2239 | + "--enable-warped-motion=0", | ||
| 2240 | + "--enable-obmc=0", | ||
| 2241 | + "--enable-masked-comp=0", | ||
| 2242 | + "--enable-interintra-comp=0", | ||
| 2243 | + "--enable-dist-wtd-comp=0", | ||
| 2244 | + "--enable-diff-wtd-comp=0", | ||
| 2245 | + "--enable-onesided-comp=0", | ||
| 2246 | + "--enable-interintra-wedge=0", | ||
| 2247 | + "--enable-smooth-interintra=0", | ||
| 2248 | + "--enable-rect-partitions=0", | ||
| 2249 | + "--enable-ab-partitions=0", | ||
| 2250 | + "--enable-1to4-partitions=0", | ||
| 2251 | + "--enable-filter-intra=0", | ||
| 2252 | + "--enable-smooth-intra=0", | ||
| 2253 | + "--enable-paeth-intra=0", | ||
| 2254 | + "--enable-directional-intra=0", | ||
| 2255 | + "--enable-angle-delta=0", | ||
| 2256 | + "--enable-tx-size-search=0", | ||
| 2257 | + "--enable-cdef=0", | ||
| 2258 | + "--enable-restoration=0", | ||
| 2259 | + "--max-partition-size=32", | ||
| 2260 | + "--min-partition-size=32", | ||
| 2261 | + "--enable-palette=0", | ||
| 2262 | + "--enable-intrabc=0", | ||
| 2263 | + "--enable-cfl-intra=0", | ||
| 2264 | + // use_ref_frame_mvs (temporal MV projection) is unimplemented in | ||
| 2265 | + // mvstack; leaving it on silently desyncs symbols on inter frames. | ||
| 2266 | + "--enable-ref-frame-mvs=0", | ||
| 2267 | + "--obu", | ||
| 2268 | + "-o", | ||
| 2269 | + "-", | ||
| 2270 | + "-", | ||
| 2271 | + ]; | ||
| 2272 | + // extra_args goes right before the trailing "-o", "-", "-" triple. | ||
| 2273 | + let tail = args.split_off(args.len() - 3); | ||
| 2274 | + args.extend_from_slice(extra_args); | ||
| 2275 | + args.extend_from_slice(&tail); | ||
| 2276 | + let mut child = Command::new(aomenc_path()) | ||
| 2277 | + .args(&args) | ||
| 2278 | + .stdin(Stdio::piped()) | ||
| 2279 | + .stdout(Stdio::piped()) | ||
| 2280 | + .stderr(Stdio::piped()) | ||
| 2281 | + .spawn() | ||
| 2282 | + .expect("aomenc failed to start"); | ||
| 2283 | + child | ||
| 2284 | + .stdin | ||
| 2285 | + .take() | ||
| 2286 | + .expect("aomenc stdin") | ||
| 2287 | + .write_all(&y4m.stdout) | ||
| 2288 | + .expect("writing y4m to aomenc"); | ||
| 2289 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 2290 | + assert!( | ||
| 2291 | + out.status.success(), | ||
| 2292 | + "aomenc refused the fixture: {}", | ||
| 2293 | + String::from_utf8_lossy(&out.stderr) | ||
| 2294 | + ); | ||
| 2295 | + let stream = out.stdout; | ||
| 2296 | + let before = decode::ref_hits(target_ref); | ||
| 2297 | + let frames = match decode_stream(&stream) { | ||
| 2298 | + Err(e) => { | ||
| 2299 | + let msg = e.to_string(); | ||
| 2300 | + assert!( | ||
| 2301 | + msg.contains("unsupported"), | ||
| 2302 | + "{gate_name} failed outright (seed {seed}): {msg}" | ||
| 2303 | + ); | ||
| 2304 | + refusals.push(format!("seed {seed}: {msg}")); | ||
| 2305 | + continue; | ||
| 2306 | + } | ||
| 2307 | + Ok(frames) => frames, | ||
| 2308 | + }; | ||
| 2309 | + if decode::ref_hits(target_ref) == before { | ||
| 2310 | + never_fired += 1; | ||
| 2311 | + continue; | ||
| 2312 | + } | ||
| 2313 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); | ||
| 2314 | + assert_eq!(frames.len(), frame_count); | ||
| 2315 | + let mismatched = frames | ||
| 2316 | + .iter() | ||
| 2317 | + .zip(&ffmpeg_frames) | ||
| 2318 | + .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); | ||
| 2319 | + if mismatched { | ||
| 2320 | + if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { | ||
| 2321 | + std::fs::write(&path, &stream).expect("writing pinned stream"); | ||
| 2322 | + eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); | ||
| 2323 | + } | ||
| 2324 | + } | ||
| 2325 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | ||
| 2326 | + assert_eq!( | ||
| 2327 | + got.y, want.y, | ||
| 2328 | + "{gate_name} frame {i} luma vs ffmpeg (seed {seed})" | ||
| 2329 | + ); | ||
| 2330 | + assert_eq!( | ||
| 2331 | + got.u, want.u, | ||
| 2332 | + "{gate_name} frame {i} U vs ffmpeg (seed {seed})" | ||
| 2333 | + ); | ||
| 2334 | + assert_eq!( | ||
| 2335 | + got.v, want.v, | ||
| 2336 | + "{gate_name} frame {i} V vs ffmpeg (seed {seed})" | ||
| 2337 | + ); | ||
| 2338 | + } | ||
| 2339 | + eprintln!( | ||
| 2340 | + "{gate_name} FIRING seed {seed}: {target_ref_name} hits advanced by {}", | ||
| 2341 | + decode::ref_hits(target_ref) - before | ||
| 2342 | + ); | ||
| 2343 | + return; | ||
| 2344 | + } | ||
| 2345 | + eprintln!( | ||
| 2346 | + "SKIP {gate_name}: {never_fired} attempts decoded but never fired {target_ref_name}, \ | ||
| 2347 | + and every other attempt hit a named refusal:\n{}", | ||
| 2348 | + refusals.join("\n") | ||
| 2349 | + ); | ||
| 2350 | + } | ||
| 2351 | + | ||
| 2352 | + /// `LAST2_FRAME`/`LAST3_FRAME` are just older slots in the same forward | ||
| 2353 | + /// reference-buffer ring `LAST_FRAME`/`GOLDEN_FRAME` already prove -- | ||
| 2354 | + /// dropping the golden gate's `--max-reference-frames=3 | ||
| 2355 | + /// --reduced-reference-set=1` narrowing (which suppresses exactly these) | ||
| 2356 | + /// and running long enough for aomenc's own buffer rotation to reach | ||
| 2357 | + /// past the first 2 frames is the whole difference. | ||
| 2358 | + #[test] | ||
| 2359 | + fn a_real_aomenc_stream_with_a_last2_reference_decodes_pixel_exact() { | ||
| 2360 | + a_real_aomenc_single_ref_gate( | ||
| 2361 | + "a_real_aomenc_stream_with_a_last2_reference_decodes_pixel_exact", | ||
| 2362 | + crate::mvstack::LAST2_FRAME, | ||
| 2363 | + "LAST2_FRAME", | ||
| 2364 | + &["--auto-alt-ref=0", "--lag-in-frames=0"], | ||
| 2365 | + 64, | ||
| 2366 | + 64, | ||
| 2367 | + 8, | ||
| 2368 | + 120, | ||
| 2369 | + ); | ||
| 2370 | + } | ||
| 2371 | + | ||
| 2372 | + #[test] | ||
| 2373 | + fn a_real_aomenc_stream_with_a_last3_reference_decodes_pixel_exact() { | ||
| 2374 | + a_real_aomenc_single_ref_gate( | ||
| 2375 | + "a_real_aomenc_stream_with_a_last3_reference_decodes_pixel_exact", | ||
| 2376 | + crate::mvstack::LAST3_FRAME, | ||
| 2377 | + "LAST3_FRAME", | ||
| 2378 | + &["--auto-alt-ref=0", "--lag-in-frames=0"], | ||
| 2379 | + 64, | ||
| 2380 | + 64, | ||
| 2381 | + 8, | ||
| 2382 | + 120, | ||
| 2383 | + ); | ||
| 2384 | + } | ||
| 2385 | + | ||
| 2386 | + /// `BWDREF`/`ALTREF2`/`ALTREF` are all backward (display-order-forward) | ||
| 2387 | + /// references that only exist once aomenc builds a hierarchical GOP -- | ||
| 2388 | + /// `--auto-alt-ref=1 --lag-in-frames` (need at least one full mini-GOP | ||
| 2389 | + /// of lookahead) is what makes aomenc code them at all; without it | ||
| 2390 | + /// every attempt would hit this decoder's own real, still-open | ||
| 2391 | + /// per-block sign-bias gap by never drawing the reference in the first | ||
| 2392 | + /// place, not by proving it correct. | ||
| 2393 | + #[test] | ||
| 2394 | + fn a_real_aomenc_stream_with_a_bwdref_reference_decodes_pixel_exact() { | ||
| 2395 | + a_real_aomenc_single_ref_gate( | ||
| 2396 | + "a_real_aomenc_stream_with_a_bwdref_reference_decodes_pixel_exact", | ||
| 2397 | + crate::mvstack::BWDREF_FRAME, | ||
| 2398 | + "BWDREF_FRAME", | ||
| 2399 | + &[ | ||
| 2400 | + "--auto-alt-ref=1", | ||
| 2401 | + "--lag-in-frames=16", | ||
| 2402 | + "--enable-fwd-kf=0", | ||
| 2403 | + ], | ||
| 2404 | + 64, | ||
| 2405 | + 64, | ||
| 2406 | + 16, | ||
| 2407 | + 120, | ||
| 2408 | + ); | ||
| 2409 | + } | ||
| 2410 | + | ||
| 2411 | + #[test] | ||
| 2412 | + fn a_real_aomenc_stream_with_an_altref2_reference_decodes_pixel_exact() { | ||
| 2413 | + a_real_aomenc_single_ref_gate( | ||
| 2414 | + "a_real_aomenc_stream_with_an_altref2_reference_decodes_pixel_exact", | ||
| 2415 | + crate::mvstack::ALTREF2_FRAME, | ||
| 2416 | + "ALTREF2_FRAME", | ||
| 2417 | + &[ | ||
| 2418 | + "--auto-alt-ref=1", | ||
| 2419 | + "--lag-in-frames=16", | ||
| 2420 | + "--enable-fwd-kf=0", | ||
| 2421 | + ], | ||
| 2422 | + 64, | ||
| 2423 | + 64, | ||
| 2424 | + 16, | ||
| 2425 | + 120, | ||
| 2426 | + ); | ||
| 2427 | + } | ||
| 2428 | + | ||
| 2429 | + #[test] | ||
| 2430 | + fn a_real_aomenc_stream_with_an_altref_reference_decodes_pixel_exact() { | ||
| 2431 | + a_real_aomenc_single_ref_gate( | ||
| 2432 | + "a_real_aomenc_stream_with_an_altref_reference_decodes_pixel_exact", | ||
| 2433 | + crate::mvstack::ALTREF_FRAME, | ||
| 2434 | + "ALTREF_FRAME", | ||
| 2435 | + &[ | ||
| 2436 | + "--auto-alt-ref=1", | ||
| 2437 | + "--lag-in-frames=16", | ||
| 2438 | + "--enable-fwd-kf=0", | ||
| 2439 | + ], | ||
| 2440 | + 64, | ||
| 2441 | + 64, | ||
| 2442 | + 16, | ||
| 2443 | + 120, | ||
| 2444 | + ); | ||
| 2445 | + } | ||
| 2446 | + | ||
| 2169 | /// scratch: decode a pinned stream end to end and report where/why it | 2447 | /// scratch: decode a pinned stream end to end and report where/why it |
| 2170 | /// refuses, plus `non_last_ref_hits` progression. | 2448 | /// refuses, plus `non_last_ref_hits` progression. |
| 2171 | #[test] | 2449 | #[test] |