Merge branch 'lane-av1golden8': GOLDEN_FRAME decodes live; mvstack extension pass ported exactly
Browse files at this commit3 changed files +179 -34
- crates/ec-av1/src/decode.rs +11 -3
- crates/ec-av1/src/mvstack.rs +115 -0
- crates/ec-av1/src/stream.rs +53 -31
crates/ec-av1/src/decode.rsmodified+11 -3
| @@ -3766,6 +3766,15 @@ fn decode_inter_block( | |||
| 3766 | // stays refused by name until that's found. | 3766 | // stays refused by name until that's found. |
| 3767 | let (py_ref, pu_ref, pv_ref) = match ref_frame { | 3767 | let (py_ref, pu_ref, pv_ref) = match ref_frame { |
| 3768 | LAST_FRAME => (ref_y, ref_u, ref_v), | 3768 | LAST_FRAME => (ref_y, ref_u, ref_v), |
| 3769 | + GOLDEN_FRAME => match (golden_y, golden_u, golden_v) { | ||
| 3770 | + (Some(gy), Some(gu), Some(gv)) => (gy, gu, gv), | ||
| 3771 | + _ => { | ||
| 3772 | + return Err(unsupported( | ||
| 3773 | + "GOLDEN_FRAME selected with no golden picture at this frame's \ | ||
| 3774 | + ref_frame_idx[3] slot", | ||
| 3775 | + )); | ||
| 3776 | + } | ||
| 3777 | + }, | ||
| 3769 | _ => { | 3778 | _ => { |
| 3770 | return Err(unsupported( | 3779 | return Err(unsupported( |
| 3771 | "a reference frame other than LAST_FRAME (LAST2/LAST3/GOLDEN/BWDREF/\ | 3780 | "a reference frame other than LAST_FRAME (LAST2/LAST3/GOLDEN/BWDREF/\ |
| @@ -3788,7 +3797,6 @@ fn decode_inter_block( | |||
| 3788 | mi_cols as usize, | 3797 | mi_cols as usize, |
| 3789 | mi_rows as usize, | 3798 | mi_rows as usize, |
| 3790 | ); | 3799 | ); |
| 3791 | - | ||
| 3792 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; | 3800 | let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1; |
| 3793 | let mut is_globalmv = false; | 3801 | let mut is_globalmv = false; |
| 3794 | let (mv, is_new_mv) = if !not_new { | 3802 | let (mv, is_new_mv) = if !not_new { |
| @@ -3879,8 +3887,8 @@ fn decode_inter_block( | |||
| 3879 | globalmv_for_lf = is_globalmv; | 3887 | globalmv_for_lf = is_globalmv; |
| 3880 | if std::env::var_os("EC_AV1_TRACE").is_some() { | 3888 | if std::env::var_os("EC_AV1_TRACE").is_some() { |
| 3881 | eprintln!( | 3889 | eprintln!( |
| 3882 | - "EC_TRACE mi_row={mi_row} mi_col={mi_col} skip={} is_inter=1 mv=({},{}) is_new_mv={is_new_mv} bsize={side}", | 3890 | + "EC_TRACE mi_row={mi_row} mi_col={mi_col} skip={} is_inter=1 mv=({},{}) is_new_mv={is_new_mv} bsize={side} ref={ref_frame} filter={:?}", |
| 3883 | - skip as u8, mv.0, mv.1 | 3891 | + skip as u8, mv.0, mv.1, block_filter |
| 3884 | ); | 3892 | ); |
| 3885 | } | 3893 | } |
| 3886 | for dr in 0..bw4 { | 3894 | for dr in 0..bw4 { |
crates/ec-av1/src/mvstack.rsmodified+115 -0
| @@ -192,6 +192,15 @@ const REF_CAT_LEVEL: u32 = 640; | |||
| 192 | /// The most candidates a stack keeps (spec `MAX_REF_MV_STACK_SIZE`). | 192 | /// The most candidates a stack keeps (spec `MAX_REF_MV_STACK_SIZE`). |
| 193 | const MAX_STACK_SIZE: usize = 8; | 193 | const MAX_STACK_SIZE: usize = 8; |
| 194 | 194 | ||
| 195 | +/// The single-reference extension's own, *smaller* cap (spec's | ||
| 196 | +/// `MAX_MV_REF_CANDIDATES`, `av1/common/enums.h`): that pass only ever tops | ||
| 197 | +/// the stack up to two entries — enough to guarantee `NEARESTMV`/`NEARMV` | ||
| 198 | +/// predictors — never all the way to [`MAX_STACK_SIZE`]. libaom gates its | ||
| 199 | +/// two extension loops on `*refmv_count < MAX_MV_REF_CANDIDATES`, not the | ||
| 200 | +/// 8-entry stack cap; this module's prior hand-written pass used | ||
| 201 | +/// `MAX_STACK_SIZE` here, which is the wrong bound. | ||
| 202 | +const MAX_MV_REF_CANDIDATES: usize = 2; | ||
| 203 | + | ||
| 195 | /// The floor libaom's `scan_row_mbmi`/`scan_col_mbmi` weight starts at | 204 | /// The floor libaom's `scan_row_mbmi`/`scan_col_mbmi` weight starts at |
| 196 | /// before a same-or-wider candidate block can raise it (`uint16_t weight = | 205 | /// before a same-or-wider candidate block can raise it (`uint16_t weight = |
| 197 | /// 2;`, mvref_common.c). | 206 | /// 2;`, mvref_common.c). |
| @@ -218,6 +227,53 @@ fn add_candidate(candidates: &mut Vec<StackEntry>, mv: (i32, i32), weight: u32) | |||
| 218 | } | 227 | } |
| 219 | } | 228 | } |
| 220 | 229 | ||
| 230 | +/// Whether `ref_frame`'s prediction runs backward in display order | ||
| 231 | +/// (`cm->ref_frame_sign_bias`, libaom): flips a borrowed neighbour's MV | ||
| 232 | +/// sign in [`process_single_ref_mv_candidate`] when the neighbour's own | ||
| 233 | +/// reference disagrees. This decoder only ever threads forward references | ||
| 234 | +/// (`LAST_FRAME`/`GOLDEN_FRAME`) to MC — never `BWDREF`/`ALTREF` — so every | ||
| 235 | +/// call site compares `false != false` and the flip is always inert. Ported | ||
| 236 | +/// anyway (never proved away, per this round's own bounds): a real | ||
| 237 | +/// backward reference would need this to be correct on day one. | ||
| 238 | +fn sign_bias(_ref_frame: i8) -> bool { | ||
| 239 | + false | ||
| 240 | +} | ||
| 241 | + | ||
| 242 | +/// libaom's `process_single_ref_mv_candidate` (mvref_common.c): folds one | ||
| 243 | +/// already-coded neighbour into the stack as a low-weight (`2`, unboosted) | ||
| 244 | +/// filler entry, used only by the single-reference extension pass below. | ||
| 245 | +/// Unlike every scan above, this runs over *any* inter neighbour regardless | ||
| 246 | +/// of which reference frame it coded — that is the whole point of the | ||
| 247 | +/// pass (topping the stack up when the real scans found too few same-ref | ||
| 248 | +/// candidates) — and dedupes by MV value against the *whole* stack so far, | ||
| 249 | +/// silently dropping the candidate (no weight bump, unlike | ||
| 250 | +/// [`add_candidate`]) when a match is already there. | ||
| 251 | +/// | ||
| 252 | +/// libaom's real signature loops `rf_idx` over the candidate's up to two | ||
| 253 | +/// coded reference-frame slots (a compound-coded neighbour donates two | ||
| 254 | +/// candidates, one per slot); this crate's [`MiInfo`] carries a single | ||
| 255 | +/// `ref_frame`, since no coded neighbour here is ever compound — ported as | ||
| 256 | +/// that loop's `rf_idx == 0` iteration alone. | ||
| 257 | +fn process_single_ref_mv_candidate( | ||
| 258 | + candidate: &MiInfo, | ||
| 259 | + ref_frame: i8, | ||
| 260 | + candidates: &mut Vec<StackEntry>, | ||
| 261 | +) { | ||
| 262 | + if !candidate.is_inter { | ||
| 263 | + return; | ||
| 264 | + } | ||
| 265 | + let mut this_mv = candidate.mv; | ||
| 266 | + if sign_bias(candidate.ref_frame) != sign_bias(ref_frame) { | ||
| 267 | + this_mv = (-this_mv.0, -this_mv.1); | ||
| 268 | + } | ||
| 269 | + if !candidates.iter().any(|e| e.mv == this_mv) { | ||
| 270 | + candidates.push(StackEntry { | ||
| 271 | + mv: this_mv, | ||
| 272 | + weight: 2, | ||
| 273 | + }); | ||
| 274 | + } | ||
| 275 | +} | ||
| 276 | + | ||
| 221 | /// The row scan (spec 7.10.2.2, libaom `scan_row_mbmi`): walks candidate | 277 | /// The row scan (spec 7.10.2.2, libaom `scan_row_mbmi`): walks candidate |
| 222 | /// *blocks* — not individual 4x4 cells — along the row `row_offset` mi units | 278 | /// *blocks* — not individual 4x4 cells — along the row `row_offset` mi units |
| 223 | /// above the block's own row, in run-length steps sized by each candidate's | 279 | /// above the block's own row, in run-length steps sized by each candidate's |
| @@ -605,6 +661,65 @@ pub fn find_mv_stack( | |||
| 605 | candidates.sort_by_key(|e| std::cmp::Reverse(e.weight)); | 661 | candidates.sort_by_key(|e| std::cmp::Reverse(e.weight)); |
| 606 | candidates.truncate(MAX_STACK_SIZE); | 662 | candidates.truncate(MAX_STACK_SIZE); |
| 607 | 663 | ||
| 664 | + // libaom's "single reference frame extension" (`setup_ref_mv_list`, | ||
| 665 | + // mvref_common.c, the `else` branch under "Handle single reference | ||
| 666 | + // frame extension", calling `process_single_ref_mv_candidate`): when | ||
| 667 | + // fewer than [`MAX_MV_REF_CANDIDATES`] (2, *not* `MAX_STACK_SIZE`) survive | ||
| 668 | + // the scans above, top the stack up to that guarantee by re-walking the | ||
| 669 | + // row directly above and the column directly left, gated on | ||
| 670 | + // `refmv_count < MAX_MV_REF_CANDIDATES` per step (so it can stop after | ||
| 671 | + // just one addition) -- over *any* inter neighbour regardless of which | ||
| 672 | + // reference frame it coded, not just `ref_frame`. This module's doc | ||
| 673 | + // used to call this pass a no-op ("whatever it would add was already | ||
| 674 | + // added by the row/col scan"), true only while a stream ever has one | ||
| 675 | + // live reference frame: once a second reference (`GOLDEN_FRAME`) is | ||
| 676 | + // live next to `LAST_FRAME` blocks, this pass pulls in the other | ||
| 677 | + // reference's MV as a low-weight (`2`, unboosted -- below | ||
| 678 | + // `REF_CAT_LEVEL`) filler entry purely to make `entries.len()` (and | ||
| 679 | + // hence how many `drl_mode` bits a `NEWMV` block reads) match the real | ||
| 680 | + // bitstream -- round 10 (lane-av1golden8) traced a `GOLDEN_FRAME` | ||
| 681 | + // `NEWMV` block desyncing its own `read_mv` by exactly this gap (real | ||
| 682 | + // stack `len=2`, this module's `len=1`, one missing `drl_mode` bit). | ||
| 683 | + // Round 11 replaced that round's hand-proved reduction (a bespoke | ||
| 684 | + // re-scan gated on `MAX_STACK_SIZE`, weighing every hit `2` unconditionally) | ||
| 685 | + // with this exact port: real gate is `MAX_MV_REF_CANDIDATES` (2), real | ||
| 686 | + // reach is the block's own `bw4`/`bh4` (see `mi_size` below, not a flat | ||
| 687 | + // 16), and dedup/weight/sign-bias now live in | ||
| 688 | + // [`process_single_ref_mv_candidate`] itself, ported from libaom's | ||
| 689 | + // function of the same name rather than re-derived by hand. | ||
| 690 | + // libaom's `mi_size` for this pass (`setup_ref_mv_list`): the block's | ||
| 691 | + // own span (`xd->width`/`xd->height`, i.e. `bw4`/`bh4` here), capped at | ||
| 692 | + // a 64x64 superblock's 16 mi units, further clipped to the frame edge — | ||
| 693 | + // never just "16, clipped to the frame edge" the way the prior | ||
| 694 | + // hand-written pass had it, which over-walked past a small query | ||
| 695 | + // block's own row/col run into cells that real neighbour scan (a wider | ||
| 696 | + // block covering more than `bw4`/`bh4`) had already folded in above. | ||
| 697 | + let mi_width = (16usize).min(bw4).min(mi_cols.saturating_sub(mi_col)); | ||
| 698 | + let mi_height = (16usize).min(bh4).min(mi_rows.saturating_sub(mi_row)); | ||
| 699 | + let mi_size = mi_width.min(mi_height); | ||
| 700 | + if max_row_offset.unsigned_abs() >= 1 { | ||
| 701 | + let mut idx = 0usize; | ||
| 702 | + while idx < mi_size && candidates.len() < MAX_MV_REF_CANDIDATES { | ||
| 703 | + let cand = grid.get(mi_row - 1, mi_col + idx); | ||
| 704 | + let step = cand.map_or(1, |c| c.size).max(1); | ||
| 705 | + if let Some(c) = cand { | ||
| 706 | + process_single_ref_mv_candidate(c, ref_frame, &mut candidates); | ||
| 707 | + } | ||
| 708 | + idx += step; | ||
| 709 | + } | ||
| 710 | + } | ||
| 711 | + if max_col_offset.unsigned_abs() >= 1 { | ||
| 712 | + let mut idx = 0usize; | ||
| 713 | + while idx < mi_size && candidates.len() < MAX_MV_REF_CANDIDATES { | ||
| 714 | + let cand = grid.get(mi_row + idx, mi_col - 1); | ||
| 715 | + let step = cand.map_or(1, |c| c.size).max(1); | ||
| 716 | + if let Some(c) = cand { | ||
| 717 | + process_single_ref_mv_candidate(c, ref_frame, &mut candidates); | ||
| 718 | + } | ||
| 719 | + idx += step; | ||
| 720 | + } | ||
| 721 | + } | ||
| 722 | + | ||
| 608 | let clamp = |mv| clamp_mv_ref(mv, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows); | 723 | let clamp = |mv| clamp_mv_ref(mv, mi_row, mi_col, bw4, bh4, mi_cols, mi_rows); |
| 609 | let nearest_mv = candidates.first().map_or((0, 0), |e| clamp(e.mv)); | 724 | let nearest_mv = candidates.first().map_or((0, 0), |e| clamp(e.mv)); |
| 610 | let near_mv = candidates.get(1).map_or((0, 0), |e| clamp(e.mv)); | 725 | let near_mv = candidates.get(1).map_or((0, 0), |e| clamp(e.mv)); |
crates/ec-av1/src/stream.rsmodified+53 -31
| @@ -558,23 +558,26 @@ mod tests { | |||
| 558 | /// (GOLDEN_FRAME's slot) and `ref_frame_idx[0] = 0` (LAST_FRAME's), whose | 558 | /// (GOLDEN_FRAME's slot) and `ref_frame_idx[0] = 0` (LAST_FRAME's), whose |
| 559 | /// lone block codes `single_ref` all the way out to `GOLDEN_FRAME` | 559 | /// lone block codes `single_ref` all the way out to `GOLDEN_FRAME` |
| 560 | /// (`p1=0, p3=1, p5=1`) and a `skip` zero-MV `NEARESTMV`: a direct copy | 560 | /// (`p1=0, p3=1, p5=1`) and a `skip` zero-MV `NEARESTMV`: a direct copy |
| 561 | - /// of GOLDEN's picture. Round 4 asserted a pixel-exact decode against | 561 | + /// of GOLDEN's picture, so its output must equal frame 0's own |
| 562 | - /// ffmpeg here; round 5 (lane-av1golden2) found that only proved the | 562 | + /// reconstruction exactly, and must differ from frame 1's -- proof the |
| 563 | - /// symbol path plus a whole-pel copy -- the first real aomenc GOLDEN | 563 | + /// decoder actually read the golden plane, not silently substituted |
| 564 | - /// streams mismatch past a stacked defect, so the arm is masked again | 564 | + /// `LAST_FRAME`'s. Rounds 4-9 masked and re-masked this arm chasing a |
| 565 | - /// and this fixture now asserts the mask: the stream must still reach | 565 | + /// deblock/MC residue that turned out to be film-grain synthesis |
| 566 | - /// the `GOLDEN_FRAME` `single_ref` read (hit counter) and must then | 566 | + /// (lane-av1golden7's `apply_grain` refusal, unrelated to GOLDEN); |
| 567 | - /// refuse by name, not decode-and-be-wrong or misparse. Round 6 | 567 | + /// lane-av1golden8 fixed mvstack's single-ref extension pass, clearing |
| 568 | - /// (lane-av1golden4) un-masked, found the residue is now a narrow | 568 | + /// the last real defect (`pinned_golden7` non_last_ref_hits 0->2), so |
| 569 | - /// deblock-edge rounding defect at GOLDEN/LAST boundaries (`decode.rs`'s | 569 | + /// this fixture flips back to its round-4 pixel-exact form. ffmpeg |
| 570 | - /// round-6 note, pinned by | 570 | + /// decodes the identical wire bytes as the foreign oracle |
| 571 | - /// `stream::tests::pinned_golden4_stream_decodes_pixel_exact`), and | 571 | + /// (shared-oracle-blindness class): this crate's own decoder agreeing |
| 572 | - /// re-masked -- this fixture proves the symbol path and a whole-pel | 572 | + /// with itself would prove nothing. |
| 573 | - /// copy only, not real subpel MC + deblock off the golden plane. The | ||
| 574 | - /// fixture itself is kept verbatim -- the next round flips this test | ||
| 575 | - /// back to the pixel-exact form the moment the live path holds. | ||
| 576 | #[test] | 573 | #[test] |
| 577 | - fn a_hand_built_golden_reference_refuses_by_name() { | 574 | + fn a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg() { |
| 575 | + if !have_ffmpeg() { | ||
| 576 | + eprintln!( | ||
| 577 | + "SKIP a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg: no ffmpeg" | ||
| 578 | + ); | ||
| 579 | + return; | ||
| 580 | + } | ||
| 578 | use crate::cdf_state::Cdfs; | 581 | use crate::cdf_state::Cdfs; |
| 579 | use crate::decode::{intra_inter_ctx, non_last_ref_hits, q_ctx_of}; | 582 | use crate::decode::{intra_inter_ctx, non_last_ref_hits, q_ctx_of}; |
| 580 | use crate::encode::{inter_frame_headers, key_frame_headers}; | 583 | use crate::encode::{inter_frame_headers, key_frame_headers}; |
| @@ -648,20 +651,31 @@ mod tests { | |||
| 648 | stream.extend(frame_obu(&seq, &header2, &tile2).unwrap()); | 651 | stream.extend(frame_obu(&seq, &header2, &tile2).unwrap()); |
| 649 | 652 | ||
| 650 | let before = non_last_ref_hits(); | 653 | let before = non_last_ref_hits(); |
| 651 | - let err = decode_stream(&stream).expect_err( | 654 | + let decoded = decode_stream(&stream).unwrap(); |
| 652 | - "the masked GOLDEN_FRAME arm decoded a hand-built GOLDEN stream -- \ | 655 | + assert_eq!(decoded.len(), 3, "run {run}: expected 3 pictures"); |
| 653 | - if the mask was lifted on purpose, flip this test back to its round-4 \ | ||
| 654 | - pixel-exact form", | ||
| 655 | - ); | ||
| 656 | assert!( | 656 | assert!( |
| 657 | non_last_ref_hits() > before, | 657 | non_last_ref_hits() > before, |
| 658 | - "run {run}: GOLDEN_FRAME's single_ref read never fired -- the stream \ | 658 | + "run {run}: GOLDEN_FRAME never fired" |
| 659 | - misparsed before reaching the refusal it is supposed to prove" | ||
| 660 | ); | 659 | ); |
| 661 | - let msg = err.to_string(); | 660 | + |
| 662 | - assert!( | 661 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 3); |
| 663 | - msg.contains("other than LAST_FRAME"), | 662 | + for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { |
| 664 | - "run {run}: refused, but not by the GOLDEN mask's own name: {msg}" | 663 | + assert_eq!(got.y, want.y, "run {run} frame {i}: luma vs ffmpeg"); |
| 664 | + assert_eq!(got.u, want.u, "run {run} frame {i}: U vs ffmpeg"); | ||
| 665 | + assert_eq!(got.v, want.v, "run {run} frame {i}: V vs ffmpeg"); | ||
| 666 | + } | ||
| 667 | + assert_eq!( | ||
| 668 | + decoded[0].y, key.reconstruction.y, | ||
| 669 | + "run {run}: frame 0 vs the encoder's own reconstruction" | ||
| 670 | + ); | ||
| 671 | + assert_eq!( | ||
| 672 | + decoded[2].y, key.reconstruction.y, | ||
| 673 | + "run {run}: GOLDEN_FRAME's block did not reproduce the keyframe it names" | ||
| 674 | + ); | ||
| 675 | + assert_ne!( | ||
| 676 | + decoded[2].y, decoded[1].y, | ||
| 677 | + "run {run}: LAST_FRAME's and GOLDEN_FRAME's slots were not actually \ | ||
| 678 | + distinguishable -- the gate proves nothing" | ||
| 665 | ); | 679 | ); |
| 666 | } | 680 | } |
| 667 | } | 681 | } |
| @@ -2045,11 +2059,19 @@ mod tests { | |||
| 2045 | "--enable-palette=0", | 2059 | "--enable-palette=0", |
| 2046 | "--enable-intrabc=0", | 2060 | "--enable-intrabc=0", |
| 2047 | "--enable-cfl-intra=0", | 2061 | "--enable-cfl-intra=0", |
| 2048 | - // `default` still lets aomenc's heuristic flip | 2062 | + // `--tune-content=film` was dropped: on this box it |
| 2063 | + // makes aomenc emit `apply_grain` almost every attempt | ||
| 2064 | + // (lane-av1golden8 round 10 measurement, live: 120/120 | ||
| 2065 | + // real attempts hit a named refusal, 0 ever fired | ||
| 2066 | + // GOLDEN_FRAME -- mostly `apply_grain`, dominated by its | ||
| 2067 | + // own recipe flag) -- film grain synthesis has its own | ||
| 2068 | + // dedicated refusal test and must not starve this gate | ||
| 2069 | + // of the GOLDEN_FRAME draws it exists to prove. Default | ||
| 2070 | + // content tuning still occasionally flips | ||
| 2049 | // `allow_screen_content_tools` on for this synthetic | 2071 | // `allow_screen_content_tools` on for this synthetic |
| 2050 | - // gradient content -- `film` forces the non-screen path | 2072 | + // gradient content (a separate, already-documented open |
| 2051 | - // instead. | 2073 | + // refusal), which the 120-attempt coin flip below |
| 2052 | - "--tune-content=film", | 2074 | + // tolerates the same way. |
| 2053 | "--obu", | 2075 | "--obu", |
| 2054 | "-o", | 2076 | "-o", |
| 2055 | "-", | 2077 | "-", |