AFFINE global motion gated and unrefused -- stock aomenc can never write one
Browse files at this commitf0c06d5d587946a1d9e1bb18f65807bec3e5079fParent: d21774e- feat
- AFFINE global motion gated and unrefused -- stock aomenc can never write one
4 changed files +259 -12
- crates/ec-av1/src/decode.rs +18 -0
- crates/ec-av1/src/refusal_inventory.rs +0 -1
- crates/ec-av1/src/stream.rs +193 -11
- scripts/build-aom-affine-oracle.sh +48 -0
crates/ec-av1/src/decode.rsmodified+18 -0
| @@ -638,6 +638,21 @@ pub(crate) fn warp_selected_hits() -> usize { | |||
| 638 | WARP_SELECTED_HITS.with(|c| c.get()) | 638 | WARP_SELECTED_HITS.with(|c| c.get()) |
| 639 | } | 639 | } |
| 640 | 640 | ||
| 641 | +// lane-gmaffine r1: how many blocks built their prediction from a | ||
| 642 | +// SIX-parameter (`AFFINE`) global-motion model through | ||
| 643 | +// `crate::warp::global_warp_params` -- the gate's proof that a stream really | ||
| 644 | +// carried an AFFINE `global_motion_params` AND that a block was predicted | ||
| 645 | +// with it, not merely that the header parsed one. | ||
| 646 | +thread_local! { | ||
| 647 | + static AFFINE_GM_HITS: std::cell::Cell<usize> = | ||
| 648 | + const { std::cell::Cell::new(0) }; | ||
| 649 | +} | ||
| 650 | + | ||
| 651 | +/// Current value of [`AFFINE_GM_HITS`]. | ||
| 652 | +pub(crate) fn affine_gm_hits() -> usize { | ||
| 653 | + AFFINE_GM_HITS.with(|c| c.get()) | ||
| 654 | +} | ||
| 655 | + | ||
| 641 | // How many blocks decoded `interintra == 1` with the non-wedge blended | 656 | // How many blocks decoded `interintra == 1` with the non-wedge blended |
| 642 | // prediction applied (lane-interintra r1) -- the gate's proof that a | 657 | // prediction applied (lane-interintra r1) -- the gate's proof that a |
| 643 | // stream actually exercised interintra. | 658 | // stream actually exercised interintra. |
| @@ -9994,6 +10009,9 @@ fn decode_inter_block( | |||
| 9994 | let gm_ref = &global_motion[(ref_frame - LAST_FRAME) as usize]; | 10009 | let gm_ref = &global_motion[(ref_frame - LAST_FRAME) as usize]; |
| 9995 | if warp_params.is_none() && is_global_mv_block && !force_integer_mv && !gm_ref.invalid { | 10010 | if warp_params.is_none() && is_global_mv_block && !force_integer_mv && !gm_ref.invalid { |
| 9996 | warp_params = crate::warp::global_warp_params(gm_ref.params); | 10011 | warp_params = crate::warp::global_warp_params(gm_ref.params); |
| 10012 | + if warp_params.is_some() && gm_ref.model == ec_av1_syntax::WarpModel::Affine { | ||
| 10013 | + AFFINE_GM_HITS.with(|c| c.set(c.get() + 1)); | ||
| 10014 | + } | ||
| 9997 | } | 10015 | } |
| 9998 | if std::env::var_os("EC_AV1_TELL").is_some() { | 10016 | if std::env::var_os("EC_AV1_TELL").is_some() { |
| 9999 | eprintln!( | 10017 | eprintln!( |
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
| @@ -65,7 +65,6 @@ const REFUSALS: &[&str] = &[ | |||
| 65 | "a show_existing_frame header naming an empty reference slot", | 65 | "a show_existing_frame header naming an empty reference slot", |
| 66 | "an 8x8 leaf that coded WARPED_CAUSAL (motion_mode == 2): av1_find_projection/the affine warp filter are not ported, only motion_mode_allowed's alphabet choice is", | 66 | "an 8x8 leaf that coded WARPED_CAUSAL (motion_mode == 2): av1_find_projection/the affine warp filter are not ported, only motion_mode_allowed's alphabet choice is", |
| 67 | "an inter frame using TxMode::Select (this decoder's inter path never reads a tx_depth symbol, so it desyncs after the first block's mode)", | 67 | "an inter frame using TxMode::Select (this decoder's inter path never reads a tx_depth symbol, so it desyncs after the first block's mode)", |
| 68 | - "an inter frame whose global motion for a single-reference frame is AFFINE (unverified this round; ROTZOOM/TRANSLATION/IDENTITY are proven)", | ||
| 69 | "an inter frame with no key frame before it", | 68 | "an inter frame with no key frame before it", |
| 70 | "an inter SB-level partition type other than SPLIT (this decoder's inter tile path only recurses a superblock as SPLIT)", | 69 | "an inter SB-level partition type other than SPLIT (this decoder's inter tile path only recurses a superblock as SPLIT)", |
| 71 | "an inter partition below 16x16 (8x8 and smaller inter blocks are not coded yet)", | 70 | "an inter partition below 16x16 (8x8 and smaller inter blocks are not coded yet)", |
crates/ec-av1/src/stream.rsmodified+193 -11
| @@ -17,7 +17,6 @@ | |||
| 17 | 17 | ||
| 18 | use ec_av1_syntax::{ | 18 | use ec_av1_syntax::{ |
| 19 | Av1Parser, FrameHeader, FrameType, NUM_REF_FRAMES, ObuKind, PRIMARY_REF_NONE, Tile, TxMode, | 19 | Av1Parser, FrameHeader, FrameType, NUM_REF_FRAMES, ObuKind, PRIMARY_REF_NONE, Tile, TxMode, |
| 20 | - WarpModel, | ||
| 21 | }; | 20 | }; |
| 22 | use ec_core::{Error, Result}; | 21 | use ec_core::{Error, Result}; |
| 23 | 22 | ||
| @@ -327,16 +326,20 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | |||
| 327 | // needed). AFFINE is untested this round (no aomenc fixture in the | 326 | // needed). AFFINE is untested this round (no aomenc fixture in the |
| 328 | // wedge gate reached a 6-parameter model) -- keep refusing it by | 327 | // wedge gate reached a 6-parameter model) -- keep refusing it by |
| 329 | // name rather than shipping an unverified decode. | 328 | // name rather than shipping an unverified decode. |
| 330 | - if header.frame_type != FrameType::Key | 329 | + // lane-gmaffine r1: AFFINE is now GATED, not refused. Stock libaom |
| 331 | - && header.global_motion[..7] | 330 | + // can never write a 6-parameter model -- its encoder-side search is |
| 332 | - .iter() | 331 | + // pinned to ROTZOOM (`av1/encoder/global_motion_facade.c:24`, |
| 333 | - .any(|gm| gm.model == WarpModel::Affine) | 332 | + // `FIRST_GLOBAL_TRANS_TYPE == LAST_GLOBAL_TRANS_TYPE == ROTZOOM`) -- |
| 334 | - { | 333 | + // which is why four rounds of `--enable-global-motion=1` recipes |
| 335 | - return Err(Error::unsupported( | 334 | + // never reached the case. `scripts/build-aom-affine-oracle.sh` widens |
| 336 | - "AV1 decode_stream", | 335 | + // exactly that search range in a side build, and |
| 337 | - "an inter frame whose global motion for a single-reference frame is AFFINE (unverified this round; ROTZOOM/TRANSLATION/IDENTITY are proven)", | 336 | + // `a_real_affine_global_motion_stream_decodes_pixel_exact` below |
| 338 | - )); | 337 | + // decodes its output pixel-exact against ffmpeg with |
| 339 | - } | 338 | + // `affine_gm_hits() > 0`, i.e. real blocks predicted through the |
| 339 | + // 6-parameter warp. Nothing in the decoder needed changing: the | ||
| 340 | + // header reader already reads params 4/5 with their own subexp refs | ||
| 341 | + // (`ec-av1-syntax` `read_global_motion_params`) and `warp.rs`'s | ||
| 342 | + // shear-validity check + `warp_affine` are model-agnostic. | ||
| 340 | // lane-gm r4/r5/r6: r4 found a NEW mismatch on frames with two | 343 | // lane-gm r4/r5/r6: r4 found a NEW mismatch on frames with two |
| 341 | // concurrently active ROTZOOM/AFFINE ref slots (GOLDEN + BWDREF) and | 344 | // concurrently active ROTZOOM/AFFINE ref slots (GOLDEN + BWDREF) and |
| 342 | // refused the shape by name rather than guess-fix it. r5 localized | 345 | // refused the shape by name rather than guess-fix it. r5 localized |
| @@ -5208,6 +5211,185 @@ mod tests { | |||
| 5208 | ); | 5211 | ); |
| 5209 | } | 5212 | } |
| 5210 | 5213 | ||
| 5214 | + /// The affine-capable encoder (`scripts/build-aom-affine-oracle.sh`), | ||
| 5215 | + /// separate from the shared oracle because stock libaom's global-motion | ||
| 5216 | + /// model search is pinned to ROTZOOM and can never emit AFFINE. | ||
| 5217 | + fn affine_aomenc_path() -> std::path::PathBuf { | ||
| 5218 | + if let Some(p) = std::env::var_os("EC_AV1_AFFINE_AOMENC") { | ||
| 5219 | + return std::path::PathBuf::from(p); | ||
| 5220 | + } | ||
| 5221 | + let home = std::env::var_os("HOME").unwrap_or_default(); | ||
| 5222 | + std::path::PathBuf::from(home).join(".cache/aom-affine/build/aomenc") | ||
| 5223 | + } | ||
| 5224 | + | ||
| 5225 | + fn have_affine_aomenc() -> bool { | ||
| 5226 | + let present = affine_aomenc_path().is_file(); | ||
| 5227 | + assert!( | ||
| 5228 | + present || std::env::var_os("EC_AV1_REQUIRE_AOMENC").is_none(), | ||
| 5229 | + "EC_AV1_REQUIRE_AOMENC is set but no affine-capable aomenc at {} -- run \ | ||
| 5230 | + scripts/build-aom-affine-oracle.sh", | ||
| 5231 | + affine_aomenc_path().display() | ||
| 5232 | + ); | ||
| 5233 | + present | ||
| 5234 | + } | ||
| 5235 | + | ||
| 5236 | + /// lane-gmaffine r1: an inter frame whose `global_motion_params` carries a | ||
| 5237 | + /// SIX-parameter (`AFFINE`) model for a single-reference frame -- refused | ||
| 5238 | + /// by name since lane-gm r4 because no aomenc recipe ever reached it. | ||
| 5239 | + /// The reason it never did is in libaom's own encoder, not in the | ||
| 5240 | + /// content: `av1/encoder/global_motion_facade.c:24` pins the model search | ||
| 5241 | + /// to `ROTZOOM` at both ends, so *no* `--enable-global-motion=1` command | ||
| 5242 | + /// line over any source can produce the case. The decoder must still | ||
| 5243 | + /// handle it (the bitstream allows it, `read_global_motion_params` reads | ||
| 5244 | + /// params 4/5 with their own subexp references, and ffmpeg decodes it), | ||
| 5245 | + /// so this gate widens exactly that one encoder-side search range in a | ||
| 5246 | + /// side build and encodes with it. Everything downstream is ordinary | ||
| 5247 | + /// libaom and the pixel oracle is still ffmpeg. | ||
| 5248 | + /// | ||
| 5249 | + /// Content: rotation composed with a progressive *anisotropic* stretch -- | ||
| 5250 | + /// a similarity transform plus unequal axis scaling is precisely what a | ||
| 5251 | + /// 4-parameter ROTZOOM model cannot express, which is what makes the | ||
| 5252 | + /// encoder's RD pick AFFINE. `affine_gm_hits()` hard-asserts that real | ||
| 5253 | + /// blocks were predicted through the 6-parameter warp, not merely that a | ||
| 5254 | + /// header parsed one (class `gate-blind-to-feature`). | ||
| 5255 | + #[test] | ||
| 5256 | + fn a_real_affine_global_motion_stream_decodes_pixel_exact() { | ||
| 5257 | + const NAME: &str = "a_real_affine_global_motion_stream_decodes_pixel_exact"; | ||
| 5258 | + let _gate_lock = lock_gate_counters(); | ||
| 5259 | + if !have_ffmpeg() { | ||
| 5260 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 5261 | + return; | ||
| 5262 | + } | ||
| 5263 | + if !have_affine_aomenc() { | ||
| 5264 | + eprintln!( | ||
| 5265 | + "SKIP {NAME}: no affine-capable aomenc at {} (scripts/build-aom-affine-oracle.sh)", | ||
| 5266 | + affine_aomenc_path().display() | ||
| 5267 | + ); | ||
| 5268 | + return; | ||
| 5269 | + } | ||
| 5270 | + let (width, height, frame_count) = (64usize, 64usize, 24usize); | ||
| 5271 | + let duration = frame_count as f64 / 25.0; | ||
| 5272 | + // Both depths: his own AV1 library is yuv420p10le throughout, and a | ||
| 5273 | + // 10-bit AFFINE warp runs the same `warp_affine` over wider samples. | ||
| 5274 | + for depth in [8u32, 10u32] { | ||
| 5275 | + // `-t` is the real bound on mandelbrot (it ignores `end_pts` and | ||
| 5276 | + // has hung a gate for an hour); the 96x96 source is rotated and | ||
| 5277 | + // then stretched horizontally only, and cropped back to 64x64. | ||
| 5278 | + let source = format!( | ||
| 5279 | + "mandelbrot=size=96x96:rate=25,rotate=a=0.12*t:c=black,\ | ||
| 5280 | + scale=w='ceil(96*(1+0.03*n)/2)*2':h=96:eval=frame,crop={width}:{height}" | ||
| 5281 | + ); | ||
| 5282 | + let pix_fmt = if depth == 10 { "yuv420p10le" } else { "yuv420p" }; | ||
| 5283 | + let mut ff_args: Vec<&str> = | ||
| 5284 | + vec!["-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", pix_fmt]; | ||
| 5285 | + // y4m has no official 10-bit tag, same as the 10-bit gate above. | ||
| 5286 | + if depth == 10 { | ||
| 5287 | + ff_args.extend(["-strict", "-1"]); | ||
| 5288 | + } | ||
| 5289 | + let duration_arg = duration.to_string(); | ||
| 5290 | + ff_args.extend(["-t", &duration_arg, "-f", "yuv4mpegpipe", "-"]); | ||
| 5291 | + let y4m = Command::new("ffmpeg") | ||
| 5292 | + .args(&ff_args) | ||
| 5293 | + .stdin(Stdio::null()) | ||
| 5294 | + .stdout(Stdio::piped()) | ||
| 5295 | + .stderr(Stdio::piped()) | ||
| 5296 | + .output() | ||
| 5297 | + .expect("ffmpeg failed to run"); | ||
| 5298 | + assert!( | ||
| 5299 | + y4m.status.success(), | ||
| 5300 | + "ffmpeg fixture: {}", | ||
| 5301 | + String::from_utf8_lossy(&y4m.stderr) | ||
| 5302 | + ); | ||
| 5303 | + let bit_depth = format!("--bit-depth={depth}"); | ||
| 5304 | + let input_bit_depth = format!("--input-bit-depth={depth}"); | ||
| 5305 | + let args: Vec<&str> = vec![ | ||
| 5306 | + "--codec=av1", | ||
| 5307 | + "--passes=1", | ||
| 5308 | + "--end-usage=q", | ||
| 5309 | + "--cq-level=45", | ||
| 5310 | + "--cpu-used=0", | ||
| 5311 | + "--kf-max-dist=1000", | ||
| 5312 | + "--threads=1", | ||
| 5313 | + "--row-mt=0", | ||
| 5314 | + "--auto-alt-ref=0", | ||
| 5315 | + "--lag-in-frames=0", | ||
| 5316 | + "--enable-fwd-kf=0", | ||
| 5317 | + "--enable-order-hint=1", | ||
| 5318 | + "--enable-global-motion=1", | ||
| 5319 | + "--enable-warped-motion=0", | ||
| 5320 | + "--enable-obmc=0", | ||
| 5321 | + "--tune-content=default", | ||
| 5322 | + "--enable-masked-comp=0", | ||
| 5323 | + "--enable-interintra-comp=0", | ||
| 5324 | + "--enable-onesided-comp=0", | ||
| 5325 | + "--enable-rect-partitions=0", | ||
| 5326 | + "--enable-ab-partitions=0", | ||
| 5327 | + "--enable-1to4-partitions=0", | ||
| 5328 | + "--enable-filter-intra=0", | ||
| 5329 | + "--enable-smooth-intra=0", | ||
| 5330 | + "--enable-paeth-intra=0", | ||
| 5331 | + "--enable-directional-intra=0", | ||
| 5332 | + "--enable-angle-delta=0", | ||
| 5333 | + "--enable-tx-size-search=0", | ||
| 5334 | + "--enable-cdef=0", | ||
| 5335 | + "--enable-restoration=0", | ||
| 5336 | + "--max-partition-size=32", | ||
| 5337 | + "--min-partition-size=32", | ||
| 5338 | + "--enable-palette=0", | ||
| 5339 | + "--enable-intrabc=0", | ||
| 5340 | + "--enable-cfl-intra=0", | ||
| 5341 | + "--enable-ref-frame-mvs=0", | ||
| 5342 | + &bit_depth, | ||
| 5343 | + &input_bit_depth, | ||
| 5344 | + "--obu", | ||
| 5345 | + "-o", | ||
| 5346 | + "-", | ||
| 5347 | + "-", | ||
| 5348 | + ]; | ||
| 5349 | + let mut child = Command::new(affine_aomenc_path()) | ||
| 5350 | + .args(&args) | ||
| 5351 | + .stdin(Stdio::piped()) | ||
| 5352 | + .stdout(Stdio::piped()) | ||
| 5353 | + .stderr(Stdio::piped()) | ||
| 5354 | + .spawn() | ||
| 5355 | + .expect("affine aomenc failed to start"); | ||
| 5356 | + child | ||
| 5357 | + .stdin | ||
| 5358 | + .take() | ||
| 5359 | + .expect("aomenc stdin") | ||
| 5360 | + .write_all(&y4m.stdout) | ||
| 5361 | + .expect("writing y4m to aomenc"); | ||
| 5362 | + let out = child.wait_with_output().expect("aomenc failed to run"); | ||
| 5363 | + assert!( | ||
| 5364 | + out.status.success(), | ||
| 5365 | + "aomenc refused the fixture: {}", | ||
| 5366 | + String::from_utf8_lossy(&out.stderr) | ||
| 5367 | + ); | ||
| 5368 | + let stream = out.stdout; | ||
| 5369 | + let before = crate::decode::affine_gm_hits(); | ||
| 5370 | + let frames = | ||
| 5371 | + decode_stream(&stream).unwrap_or_else(|e| panic!("{NAME} ({depth}-bit): {e}")); | ||
| 5372 | + let ffmpeg_frames = if depth == 10 { | ||
| 5373 | + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) | ||
| 5374 | + } else { | ||
| 5375 | + ffmpeg_decode_sequence(&stream, width, height, frame_count) | ||
| 5376 | + }; | ||
| 5377 | + assert_eq!(frames.len(), frame_count); | ||
| 5378 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | ||
| 5379 | + assert_eq!(got.y, want.y, "{NAME} {depth}-bit frame {i} luma vs ffmpeg"); | ||
| 5380 | + assert_eq!(got.u, want.u, "{NAME} {depth}-bit frame {i} U vs ffmpeg"); | ||
| 5381 | + assert_eq!(got.v, want.v, "{NAME} {depth}-bit frame {i} V vs ffmpeg"); | ||
| 5382 | + } | ||
| 5383 | + let hits = crate::decode::affine_gm_hits() - before; | ||
| 5384 | + assert!( | ||
| 5385 | + hits > 0, | ||
| 5386 | + "{NAME} ({depth}-bit): decoded pixel-exact but no block was predicted through a \ | ||
| 5387 | + 6-parameter global-motion warp -- the AFFINE path is unexercised" | ||
| 5388 | + ); | ||
| 5389 | + eprintln!("{NAME}: {depth}-bit, {frame_count} frames pixel-exact, affine_gm_hits={hits}"); | ||
| 5390 | + } | ||
| 5391 | + } | ||
| 5392 | + | ||
| 5211 | /// lane-rect16 r1/r2: a plain default-settings aomenc run over lavfi | 5393 | /// lane-rect16 r1/r2: a plain default-settings aomenc run over lavfi |
| 5212 | /// `mandelbrot` (`--cpu-used=4 --end-usage=q --cq-level=32`, no | 5394 | /// `mandelbrot` (`--cpu-used=4 --end-usage=q --cq-level=32`, no |
| 5213 | /// `--enable-*` flags) used to refuse frame 0 outright with "a partition | 5395 | /// `--enable-*` flags) used to refuse frame 0 outright with "a partition |
scripts/build-aom-affine-oracle.shadded+48 -0
| @@ -0,0 +1,48 @@ | |||
| 1 | +#!/usr/bin/env bash | ||
| 2 | +# Provision an aomenc that can emit AFFINE global motion. | ||
| 3 | +# | ||
| 4 | +# Stock libaom NEVER writes a 6-parameter (AFFINE) global-motion model: its | ||
| 5 | +# encoder-side model search is pinned to ROTZOOM, | ||
| 6 | +# | ||
| 7 | +# av1/encoder/global_motion_facade.c:24 | ||
| 8 | +# #define FIRST_GLOBAL_TRANS_TYPE ROTZOOM | ||
| 9 | +# #define LAST_GLOBAL_TRANS_TYPE ROTZOOM | ||
| 10 | +# | ||
| 11 | +# so no `--enable-global-motion=1` recipe over any content can produce the | ||
| 12 | +# AFFINE case the decoder must still handle (the *bitstream* allows it, the | ||
| 13 | +# spec's `global_motion_params` reads 6 params, and ffmpeg decodes it). This | ||
| 14 | +# builds a side copy of the oracle source with that search range widened to | ||
| 15 | +# AFFINE -- decode-normative output, still ordinary libaom, still verified | ||
| 16 | +# against ffmpeg -- into its own prefix so the shared oracle at | ||
| 17 | +# ~/.cache/aom-oracle (used by every other gate) is untouched. | ||
| 18 | +# | ||
| 19 | +# Used by the `a_real_affine_global_motion_stream_decodes_pixel_exact` gate in | ||
| 20 | +# crates/ec-av1/src/stream.rs. | ||
| 21 | +set -euo pipefail | ||
| 22 | + | ||
| 23 | +ROOT="${AOM_AFFINE_ROOT:-$HOME/.cache/aom-affine}" | ||
| 24 | +ORACLE_SRC="${AOM_ORACLE_ROOT:-$HOME/.cache/aom-oracle}/src" | ||
| 25 | +SRC="$ROOT/src" | ||
| 26 | +BUILD="$ROOT/build" | ||
| 27 | + | ||
| 28 | +if [ ! -d "$ORACLE_SRC/.git" ]; then | ||
| 29 | + echo "no oracle source at $ORACLE_SRC -- run scripts/build-aom-oracle.sh first" >&2 | ||
| 30 | + exit 1 | ||
| 31 | +fi | ||
| 32 | + | ||
| 33 | +if [ ! -d "$SRC" ]; then | ||
| 34 | + mkdir -p "$ROOT" | ||
| 35 | + cp -a "$ORACLE_SRC" "$SRC" | ||
| 36 | +fi | ||
| 37 | + | ||
| 38 | +facade="$SRC/av1/encoder/global_motion_facade.c" | ||
| 39 | +sed -i 's/^#define LAST_GLOBAL_TRANS_TYPE ROTZOOM$/#define LAST_GLOBAL_TRANS_TYPE AFFINE/' "$facade" | ||
| 40 | +grep -q '^#define LAST_GLOBAL_TRANS_TYPE AFFINE$' "$facade" || | ||
| 41 | + { echo "patch did not apply to $facade" >&2; exit 1; } | ||
| 42 | + | ||
| 43 | +cmake -S "$SRC" -B "$BUILD" -G Ninja \ | ||
| 44 | + -DCMAKE_BUILD_TYPE=Release \ | ||
| 45 | + -DENABLE_TESTS=0 -DENABLE_DOCS=0 -DENABLE_EXAMPLES=1 | ||
| 46 | +ninja -C "$BUILD" aomenc | ||
| 47 | + | ||
| 48 | +echo "affine-capable encoder ready: $BUILD/aomenc" | ||