lane-av1422warp: fix the 4:2:2 loop-restoration corner bug; pin the coverage witness
Browse files at this commit7373100274022b032a8295b0ffcfcf7e0bc1b720Parent: 68a69e24 changed files +182 -21
- crates/ec-av1/fixtures/422_residual_compound_warp_16f.obu +0 -0
- crates/ec-av1/src/decode.rs +64 -9
- crates/ec-av1/src/restoration.rs +40 -12
- crates/ec-av1/src/stream.rs +78 -0
crates/ec-av1/fixtures/422_residual_compound_warp_16f.obuadded+0 -0
Binary file changed
crates/ec-av1/src/decode.rsmodified+64 -9
| @@ -4488,6 +4488,31 @@ pub(crate) fn warp_selected_hits() -> usize { | ||
| 4488 | 4488 | // into the compound intermediate) rather than translationally -- the |
| 4489 | 4489 | // gate's proof that a `GLOBAL_GLOBALMV` block under a ROTZOOM/AFFINE model |
| 4490 | 4490 | // really reached [`crate::warp::warp_affine_compound`]. |
| 4491 | +// lane-av1422warp: the 4:2:2 coverage witness has to show compound and warped | |
| 4492 | +// motion firing ABOVE the vertical midpoint, and every pre-existing warp | |
| 4493 | +// counter is frame-global. These two are the same arms restricted to blocks | |
| 4494 | +// whose top mode-info row is in the frame's upper half, which is the half | |
| 4495 | +// this lane's evidence covers. | |
| 4496 | +thread_local! { | |
| 4497 | + static TOP_HALF_COMPOUND_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | |
| 4498 | + static TOP_HALF_WARP_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | |
| 4499 | +} | |
| 4500 | + | |
| 4501 | +/// How many `GLOBAL_GLOBALMV` (compound) inter blocks decoded in the frame's | |
| 4502 | +/// upper half. Read by the pinned 4:2:2 coverage gate. | |
| 4503 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | |
| 4504 | +pub(crate) fn top_half_compound_hits() -> usize { | |
| 4505 | + TOP_HALF_COMPOUND_HITS.with(std::cell::Cell::get) | |
| 4506 | +} | |
| 4507 | + | |
| 4508 | +/// How many inter blocks in the frame's upper half had at least one active | |
| 4509 | +/// reference slot on a non-`TRANSLATION` global-motion model -- i.e. blocks | |
| 4510 | +/// whose prediction goes through `crate::warp`. | |
| 4511 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | |
| 4512 | +pub(crate) fn top_half_warp_hits() -> usize { | |
| 4513 | + TOP_HALF_WARP_HITS.with(std::cell::Cell::get) | |
| 4514 | +} | |
| 4515 | + | |
| 4491 | 4516 | thread_local! { |
| 4492 | 4517 | static COMPOUND_WARP_HITS: std::cell::Cell<usize> = |
| 4493 | 4518 | const { std::cell::Cell::new(0) }; |
| @@ -28046,9 +28071,19 @@ fn read_sb128_root( | ||
| 28046 | 28071 | lr, |
| 28047 | 28072 | lr_grid, |
| 28048 | 28073 | lr_reference, |
| 28049 | - mi_r128, | |
| 28050 | - mi_c128, | |
| 28051 | - SB_MI * 2, fctx, | |
| 28074 | + // `read_lr` is the ONE consumer that turns this superblock's | |
| 28075 | + // mode-info origin into ABSOLUTE frame space -- it divides by the | |
| 28076 | + // restoration-unit size, which is a real pixel count. The partition | |
| 28077 | + // decode below is self-consistent with `mi_r128` as written (it | |
| 28078 | + // only ever uses relative offsets from it), which is why a 128x128 | |
| 28079 | + // stream decoded correctly for 4:2:0 and 4:4:4 while placing its | |
| 28080 | + // loop-restoration units at double the offset. `sb_r`/`sb_c` index | |
| 28081 | + // 64px superblocks, so a 128px superblock's origin is half a | |
| 28082 | + // `SB_MI` per 64px step -- `(sb_r & !1) * SB_MI` is twice the true | |
| 28083 | + // mi row. The extent is `SB_MI`: a 128px superblock is 16 mi. | |
| 28084 | + (sb_r & !1) * (SB_MI / 2), | |
| 28085 | + (sb_c & !1) * (SB_MI / 2), | |
| 28086 | + SB_MI, fctx, | |
| 28052 | 28087 | ); |
| 28053 | 28088 | let at128 = ((mi_r128 / SUB_MI) as usize, (mi_c128 / SUB_MI) as usize); |
| 28054 | 28089 | let ctx128 = neighbours.partition_ctx(at128, 128); |
| @@ -28666,9 +28701,14 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( | ||
| 28666 | 28701 | lr, |
| 28667 | 28702 | &mut lr_grid, |
| 28668 | 28703 | &mut lr_reference, |
| 28669 | - sb_r * SB_MI, | |
| 28670 | - sb_c * SB_MI, | |
| 28671 | - SB_MI, fctx, | |
| 28704 | + // True mode-info units: a 64px superblock is 8, and `SB_MI` is 16. | |
| 28705 | + // `read_lr` divides by the restoration-unit size (a real pixel count), | |
| 28706 | + // so it is the one consumer that needs the absolute origin in real mi. | |
| 28707 | + // The half-mi convention these arguments used to carry only worked | |
| 28708 | + // while `read_lr` also counted in half-mi -- the two cancelled. | |
| 28709 | + sb_r * (SB_MI / 2), | |
| 28710 | + sb_c * (SB_MI / 2), | |
| 28711 | + SB_MI / 2, fctx, | |
| 28672 | 28712 | ); |
| 28673 | 28713 | } |
| 28674 | 28714 | fctx.cdef_transmitted.with(|c| c.set(false)); |
| @@ -33411,6 +33451,16 @@ fn decode_inter_block( | ||
| 33411 | 33451 | } |
| 33412 | 33452 | } |
| 33413 | 33453 | let is_globalmv = compound_mode == 6; // GLOBAL_GLOBALMV |
| 33454 | + if mi_row < mi_rows as usize / 2 { | |
| 33455 | + if is_globalmv { | |
| 33456 | + TOP_HALF_COMPOUND_HITS.with(|c| c.set(c.get() + 1)); | |
| 33457 | + } | |
| 33458 | + if global_motion[(ref0 - LAST_FRAME) as usize].model as u8 > 1 | |
| 33459 | + || global_motion[(ref1 - LAST_FRAME) as usize].model as u8 > 1 | |
| 33460 | + { | |
| 33461 | + TOP_HALF_WARP_HITS.with(|c| c.set(c.get() + 1)); | |
| 33462 | + } | |
| 33463 | + } | |
| 33414 | 33464 | // spec `is_nontrans_global_motion`: ALL active refs' models must |
| 33415 | 33465 | // be non-TRANSLATION (IDENTITY counts) for the compound block's |
| 33416 | 33466 | // interp-filter read to be suppressed. |
| @@ -43558,9 +43608,14 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 43558 | 43608 | lr, |
| 43559 | 43609 | &mut lr_grid, |
| 43560 | 43610 | &mut lr_reference, |
| 43561 | - sb_r * SB_MI, | |
| 43562 | - sb_c * SB_MI, | |
| 43563 | - SB_MI, fctx, | |
| 43611 | + // True mode-info units: a 64px superblock is 8, and `SB_MI` is 16. | |
| 43612 | + // `read_lr` divides by the restoration-unit size (a real pixel count), | |
| 43613 | + // so it is the one consumer that needs the absolute origin in real mi. | |
| 43614 | + // The half-mi convention these arguments used to carry only worked | |
| 43615 | + // while `read_lr` also counted in half-mi -- the two cancelled. | |
| 43616 | + sb_r * (SB_MI / 2), | |
| 43617 | + sb_c * (SB_MI / 2), | |
| 43618 | + SB_MI / 2, fctx, | |
| 43564 | 43619 | ); |
| 43565 | 43620 | } |
| 43566 | 43621 | fctx.cdef_transmitted.with(|c| c.set(false)); |
crates/ec-av1/src/restoration.rsmodified+40 -12
| @@ -501,27 +501,49 @@ pub(crate) fn read_lr( | ||
| 501 | 501 | if ftype == RestorationType::None { |
| 502 | 502 | continue; |
| 503 | 503 | } |
| 504 | - let mi_size = if plane == 0 { | |
| 505 | - 4u32 | |
| 504 | + // `av1_loop_restoration_corners_in_sb` (restoration.c:1303-1338), | |
| 505 | + // transcribed per axis. The two axes are NOT symmetric: libaom | |
| 506 | + // derives `mi_size_x` from `subsampling_x` and `mi_size_y` from | |
| 507 | + // `subsampling_y`, and derives the row corners from the Y term | |
| 508 | + // alone. This decoder used one `mi_size = 4 >> subsampling_x` for | |
| 509 | + // both axes, so at 4:2:2 (ss_x 1, ss_y 0) the row range was | |
| 510 | + // computed from the COLUMN axis's subsampling -- a 4x-too-small | |
| 511 | + // row step, which made `rrow0 == rrow1` (an empty range, zero | |
| 512 | + // `read_lr_unit` calls) wherever libaom reads a chroma unit. That | |
| 513 | + // is the two missing `wiener_restore_cdf` reads this lane spent | |
| 514 | + // nine rounds localising: the oracle reads three at mi(30,62), | |
| 515 | + // this decoder read one. | |
| 516 | + // | |
| 517 | + // `MI_SIZE` is 8 -- one mode-info unit is 8 pixels. Superres | |
| 518 | + // widens the column axis only (spec 7.16), so it enters `num_x` | |
| 519 | + // and `denom_x` and leaves the row axis alone, exactly as libaom | |
| 520 | + // does with `av1_superres_scaled`. | |
| 521 | + let is_uv = plane != 0; | |
| 522 | + let ss_x = if is_uv { | |
| 523 | + crate::decode::ss_x(fctx) as u32 | |
| 506 | 524 | } else { |
| 507 | - 4u32 >> (crate::decode::ss_x(fctx) as u32) | |
| 525 | + 0 | |
| 526 | + }; | |
| 527 | + let ss_y = if is_uv { | |
| 528 | + crate::decode::ss_y(fctx) as u32 | |
| 529 | + } else { | |
| 530 | + 0 | |
| 508 | 531 | }; |
| 532 | + let mi_size_x = MI_SIZE >> ss_x; | |
| 533 | + let mi_size_y = MI_SIZE >> ss_y; | |
| 509 | 534 | let unit_size = lr.loop_restoration_size[plane]; |
| 510 | 535 | let horz_units = grid.horz_units[plane] as u32; |
| 511 | 536 | let vert_units = grid.vert_units[plane] as u32; |
| 512 | - // `av1_loop_restoration_corners_in_sb` (restoration.c:1314): under | |
| 513 | - // superres the mi column is in DOWNSCALED units while the unit grid | |
| 514 | - // is in upscaled ones, so the column division scales by | |
| 515 | - // `superres_denom / SCALE_NUMERATOR` (`mi_to_num_x`/`denom_x`). Rows | |
| 516 | - // are never scaled (spec 7.16 widens columns only). | |
| 517 | 537 | let (num_x, denom_x) = match crate::decode::superres(fctx) { |
| 518 | - Some((_, denom)) => (mi_size * denom, unit_size * 8), | |
| 519 | - None => (mi_size, unit_size), | |
| 538 | + Some((_, d)) => (mi_size_x * d, unit_size * SCALE_NUMERATOR), | |
| 539 | + None => (mi_size_x, unit_size), | |
| 520 | 540 | }; |
| 541 | + let num_y = mi_size_y; | |
| 542 | + let denom_y = unit_size; | |
| 521 | 543 | let rcol0 = ceil_div(mi_col * num_x, denom_x); |
| 522 | 544 | let rcol1 = ceil_div((mi_col + sb_mi) * num_x, denom_x).min(horz_units); |
| 523 | - let rrow0 = ceil_div(mi_row * mi_size, unit_size); | |
| 524 | - let rrow1 = ceil_div((mi_row + sb_mi) * mi_size, unit_size).min(vert_units); | |
| 545 | + let rrow0 = ceil_div(mi_row * num_y, denom_y); | |
| 546 | + let rrow1 = ceil_div((mi_row + sb_mi) * num_y, denom_y).min(vert_units); | |
| 525 | 547 | let chroma = plane != 0; |
| 526 | 548 | for rrow in rrow0..rrow1 { |
| 527 | 549 | for rcol in rcol0..rcol1 { |
| @@ -555,6 +577,12 @@ thread_local! { | ||
| 555 | 577 | static LR_LAST_STRIPE_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 556 | 578 | } |
| 557 | 579 | |
| 580 | +/// `MI_SIZE` (aom_scale.h): pixels per mode-info unit. `av1_loop_restoration_ | |
| 581 | +/// corners_in_sb` steps the row axis by this, so it is NOT the "half-mi" | |
| 582 | +/// factor several other helpers here use. | |
| 583 | +const MI_SIZE: u32 = 8; | |
| 584 | +/// `SCALE_NUMERATOR` (aom_scale.h): the superres scale's fixed point, 8. | |
| 585 | +const SCALE_NUMERATOR: u32 = 8; | |
| 558 | 586 | /// Current value of [`LR_STRIPE0_HITS`]. |
| 559 | 587 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 560 | 588 | pub(crate) fn lr_stripe0_hits() -> usize { |
crates/ec-av1/src/stream.rsmodified+78 -0
| @@ -2282,6 +2282,84 @@ pub(crate) mod tests { | ||
| 2282 | 2282 | } |
| 2283 | 2283 | } |
| 2284 | 2284 | |
| 2285 | + /// lane-av1422warp: the SECOND 4:2:2 coverage witness -- the one | |
| 2286 | + /// `422_sb128_3f.obu` could not be, because that stream's inter content | |
| 2287 | + /// is skip-heavy (see `lanes/av1422stripwrite.report.md`, which named a | |
| 2288 | + /// second fixture with real residuals, compound and warped motion above | |
| 2289 | + /// the vertical midpoint as the thing that would justify lifting the | |
| 2290 | + /// refusal). This is that fixture. | |
| 2291 | + /// | |
| 2292 | + /// `422_residual_compound_warp_16f.obu` (38845 bytes), sha256 | |
| 2293 | + /// `d78e2afb43ce311d3d82335a945c6f80f62db4537966d881c1349a68b3aecb95`: | |
| 2294 | + /// | |
| 2295 | + /// ```text | |
| 2296 | + /// ffmpeg -f lavfi -i "mandelbrot=size=256x288:rate=24:maxiter=220: | |
| 2297 | + /// start_scale=3:end_scale=0.35:end_pts=300:rotate=a=0.10" | |
| 2298 | + /// -frames:v 24 -pix_fmt yuv422p -f yuv4mpegpipe src422.y4m | |
| 2299 | + /// aomenc --codec=av1 --profile=2 --input-bit-depth=8 --limit=16 | |
| 2300 | + /// --width=256 --height=288 --lag-in-frames=25 --auto-alt-ref=1 | |
| 2301 | + /// --enable-global-motion=1 --cq-level=24 --cpu-used=0 | |
| 2302 | + /// --threads=4 --kf-min-dist=0 --kf-max-dist=999999 \ | |
| 2303 | + /// -n ffp --pass=2 --fpf=src422.y4m -o a1.webm | |
| 2304 | + /// ffmpeg -i a1.webm -c copy -f obu -y a1.obu | |
| 2305 | + /// ``` | |
| 2306 | + /// | |
| 2307 | + /// Measured on a patch-run-restore build (the `EC_AV1_ALLOW_422_PROBE` | |
| 2308 | + /// bypass applied for the run, reverted after -- it is deliberately NOT | |
| 2309 | + /// committed, and the refusal below is what the committed tree asserts): | |
| 2310 | + /// | |
| 2311 | + /// - all 16 frames pixel-exact against `aomdec --rawvideo`, 2359296 | |
| 2312 | + /// bytes each, sha256 `4bfc2395e5ca6ea178f606e6b1ced773e26fc2d85b | |
| 2313 | + /// c3a6cdb63a402939aeb203` on both sides; | |
| 2314 | + /// - the entropy stream pairs bit-for-bit against the instrumented | |
| 2315 | + /// oracle -- 246735 symbol reads each, no divergence anywhere in the | |
| 2316 | + /// stream, not merely at the window this lane spent nine rounds | |
| 2317 | + /// narrowing; | |
| 2318 | + /// - the arms this fixture exists to cover really fire ABOVE the | |
| 2319 | + /// vertical midpoint: 248 upper-half blocks on a non-`TRANSLATION` | |
| 2320 | + /// global-motion model (`top_half_warp_hits`) and 3 upper-half | |
| 2321 | + /// `GLOBAL_GLOBALMV` compound blocks (`top_half_compound_hits`), | |
| 2322 | + /// alongside 25 compound-warp, 18 compound-warp-8x8-leaf, 84 | |
| 2323 | + /// rotzoom global-warp, 22 Wiener and 10 SGRPROJ loop-restoration | |
| 2324 | + /// units frame-globally. | |
| 2325 | + /// | |
| 2326 | + /// The loop-restoration numbers are load-bearing twice over: decoding | |
| 2327 | + /// this stream is what exposed the per-axis `read_lr` corner bug | |
| 2328 | + /// (`restoration.rs`, `av1_loop_restoration_corners_in_sb`), whose fix | |
| 2329 | + /// is what made the whole sequence pair. 4:2:0 and 4:4:4 are unchanged | |
| 2330 | + /// by it -- byte-identical to their pre-lane decodes, with the LR gate | |
| 2331 | + /// family green. | |
| 2332 | + /// | |
| 2333 | + /// Like its two siblings, the committed assertion is the byte pin plus | |
| 2334 | + /// the refusal-by-name contract: with the header refusal standing, | |
| 2335 | + /// NO committed test can decode a 4:2:2 stream, because the bypass that | |
| 2336 | + /// would allow it must never be committed. | |
| 2337 | + #[test] | |
| 2338 | + fn the_pinned_422_residual_compound_warp_witness_is_present_and_refuses_by_name() { | |
| 2339 | + const NAME: &str = | |
| 2340 | + "the_pinned_422_residual_compound_warp_witness_is_present_and_refuses_by_name"; | |
| 2341 | + const REFUSAL: &str = "a chroma format of 4:2:2"; | |
| 2342 | + const FILE: &str = "422_residual_compound_warp_16f.obu"; | |
| 2343 | + const BYTES: usize = 38845; | |
| 2344 | + const FP: u64 = 0x0e73a51e2cc0c424; | |
| 2345 | + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) | |
| 2346 | + .join("fixtures") | |
| 2347 | + .join(FILE); | |
| 2348 | + let data = std::fs::read(&path).unwrap_or_else(|e| { | |
| 2349 | + panic!( | |
| 2350 | + "{NAME}: pinned witness {} is missing ({e}) -- the gate cannot run", | |
| 2351 | + path.display() | |
| 2352 | + ) | |
| 2353 | + }); | |
| 2354 | + assert_eq!(data.len(), BYTES, "{NAME}: {FILE} size drifted"); | |
| 2355 | + assert_eq!(fnv1a64(&data), FP, "{NAME}: {FILE} bytes drifted"); | |
| 2356 | + let err = decode_stream(&data).unwrap_err().to_string(); | |
| 2357 | + assert!( | |
| 2358 | + err.contains(REFUSAL), | |
| 2359 | + "{NAME}: {FILE} must refuse by name, got: {err}" | |
| 2360 | + ); | |
| 2361 | + } | |
| 2362 | + | |
| 2285 | 2363 | |
| 2286 | 2364 | /// lane-av1-qmatrix: WITNESS for the lifted `using_qmatrix` refusal (the |
| 2287 | 2365 | /// old gate `a_frame_using_quantisation_matrices_is_refused_by_name` is |