Merge branch 'lane-av1golden': GOLDEN references, proven by a hand-built stream
Browse files at this commit2 changed files +157 -13
- crates/ec-av1/src/decode.rs +27 -13
- crates/ec-av1/src/stream.rs +130 -0
crates/ec-av1/src/decode.rsmodified+27 -13
| @@ -3455,7 +3455,12 @@ fn resolve_interp_filter( | ||
| 3455 | 3455 | |
| 3456 | 3456 | /// The `is_inter` context (spec 5.11.16 via `av1_get_intra_inter_context`), |
| 3457 | 3457 | /// duplicating [`crate::tile`]'s private copy of the same rule. |
| 3458 | -fn intra_inter_ctx(has_above: bool, has_left: bool, above_inter: bool, left_inter: bool) -> usize { | |
| 3458 | +pub(crate) fn intra_inter_ctx( | |
| 3459 | + has_above: bool, | |
| 3460 | + has_left: bool, | |
| 3461 | + above_inter: bool, | |
| 3462 | + left_inter: bool, | |
| 3463 | +) -> usize { | |
| 3459 | 3464 | match (has_above, has_left) { |
| 3460 | 3465 | (true, true) => { |
| 3461 | 3466 | let (above_intra, left_intra) = (!above_inter, !left_inter); |
| @@ -3724,22 +3729,31 @@ fn decode_inter_block( | ||
| 3724 | 3729 | if is_inter { |
| 3725 | 3730 | let ref_frame = read_single_ref(dec, cdfs, neighbours.above_ref[c], neighbours.left_ref[r]); |
| 3726 | 3731 | ref_frame_for_lf = ref_frame; |
| 3727 | - // round 3 (lane-av1refs): the GOLDEN_FRAME arm below decodes and | |
| 3728 | - // threads a real golden picture through MC/mvstack/deblock, but 120+ | |
| 3729 | - // attempts across a real aomenc recipe built to isolate exactly this | |
| 3730 | - // path (min==max partition size, order-hint off, screen-content | |
| 3731 | - // tools off) still could not clear this crate's OWN gate | |
| 3732 | - // (`a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact`, | |
| 3733 | - // stream.rs) 4 consecutive times -- every attempt either never fired | |
| 3734 | - // GOLDEN_FRAME or hit an unrelated named refusal first (see that | |
| 3735 | - // test's own refusal log). Refuse GOLDEN_FRAME here, narrowly, until | |
| 3736 | - // a round actually gets that gate green: main should not carry a | |
| 3737 | - // reference-selection path with zero live pixel-exact proof. | |
| 3732 | + // round 4 (lane-av1golden): 120+ attempts across a real aomenc | |
| 3733 | + // recipe never landed a GOLDEN_FRAME-firing, decodable stream (every | |
| 3734 | + // attempt hit an unrelated named refusal first), so this arm stayed | |
| 3735 | + // refused pending a fixture that could prove it another way. A | |
| 3736 | + // hand-built stream now does: `a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg` | |
| 3737 | + // (stream.rs) drives GOLDEN_FRAME through a real OBU stream that | |
| 3738 | + // ffmpeg also decodes, byte for byte. GOLDEN_FRAME is threaded to | |
| 3739 | + // MC/mvstack/deblock the same as LAST_FRAME from here; every other | |
| 3740 | + // reference (LAST2/LAST3/BWDREF/ALTREF2/ALTREF) still refuses by | |
| 3741 | + // name below -- none of them have a live pixel-exact proof yet. | |
| 3738 | 3742 | let (py_ref, pu_ref, pv_ref) = match ref_frame { |
| 3739 | 3743 | LAST_FRAME => (ref_y, ref_u, ref_v), |
| 3744 | + GOLDEN_FRAME => match (golden_y, golden_u, golden_v) { | |
| 3745 | + (Some(gy), Some(gu), Some(gv)) => (gy, gu, gv), | |
| 3746 | + _ => { | |
| 3747 | + return Err(unsupported( | |
| 3748 | + "GOLDEN_FRAME selected with no golden picture at this frame's \ | |
| 3749 | + ref_frame_idx[3] slot", | |
| 3750 | + )); | |
| 3751 | + } | |
| 3752 | + }, | |
| 3740 | 3753 | _ => { |
| 3741 | 3754 | return Err(unsupported( |
| 3742 | - "a reference frame other than LAST_FRAME (round 3: GOLDEN_FRAME's decode path exists but has no live pixel-exact proof yet)", | |
| 3755 | + "a reference frame other than LAST_FRAME/GOLDEN_FRAME (LAST2/LAST3/\ | |
| 3756 | + BWDREF/ALTREF2/ALTREF still have no live pixel-exact proof)", | |
| 3743 | 3757 | )); |
| 3744 | 3758 | } |
| 3745 | 3759 | }; |
crates/ec-av1/src/stream.rsmodified+130 -0
| @@ -522,6 +522,136 @@ mod tests { | ||
| 522 | 522 | } |
| 523 | 523 | } |
| 524 | 524 | |
| 525 | + /// A hand-built 3-frame stream that actually fires `GOLDEN_FRAME` | |
| 526 | + /// through [`decode_stream`], proving the round-4 flip (`decode.rs`'s | |
| 527 | + /// `decode_inter_block` `GOLDEN_FRAME` arm) the way 120+ real aomenc | |
| 528 | + /// attempts never could: frame 0 is a real key frame (this crate's own | |
| 529 | + /// encoder, real texture), refreshing all 8 DPB slots as spec requires -- | |
| 530 | + /// slot 3 keeps that exact picture from here on. Frame 1 is a hand-coded | |
| 531 | + /// inter frame whose lone 32x32 block is *intra* (`DC_PRED`, `skip`), | |
| 532 | + /// refreshing only slot 0 with a flat 128 block -- distinguishable from | |
| 533 | + /// frame 0's real texture, so slot 0 (`LAST_FRAME`) and slot 3 | |
| 534 | + /// (`GOLDEN_FRAME`) hold visibly different pictures once frame 2 runs. | |
| 535 | + /// Frame 2 is a hand-coded inter frame naming `ref_frame_idx[3] = 3` | |
| 536 | + /// (GOLDEN_FRAME's slot) and `ref_frame_idx[0] = 0` (LAST_FRAME's), whose | |
| 537 | + /// lone block codes `single_ref` all the way out to `GOLDEN_FRAME` | |
| 538 | + /// (`p1=0, p3=1, p5=1`) and a `skip` zero-MV `NEARESTMV`: a direct copy | |
| 539 | + /// of GOLDEN's picture, so its output must equal frame 0's own | |
| 540 | + /// reconstruction exactly, and must differ from frame 1's -- proof the | |
| 541 | + /// decoder actually read the golden plane, not silently substituted | |
| 542 | + /// `LAST_FRAME`'s. ffmpeg decodes the identical wire bytes as the | |
| 543 | + /// foreign oracle (shared-oracle-blindness class): this crate's own | |
| 544 | + /// decoder agreeing with itself would prove nothing. | |
| 545 | + #[test] | |
| 546 | + fn a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg() { | |
| 547 | + if !have_ffmpeg() { | |
| 548 | + eprintln!( | |
| 549 | + "SKIP a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg: no ffmpeg" | |
| 550 | + ); | |
| 551 | + return; | |
| 552 | + } | |
| 553 | + use crate::cdf_state::Cdfs; | |
| 554 | + use crate::decode::{intra_inter_ctx, non_last_ref_hits, q_ctx_of}; | |
| 555 | + use crate::encode::{inter_frame_headers, key_frame_headers}; | |
| 556 | + use crate::frame::frame_obu; | |
| 557 | + use crate::msac::SymbolEncoder; | |
| 558 | + use crate::mvstack::{ | |
| 559 | + GOLDEN_FRAME, MiGrid, find_mv_stack, single_ref_p1_ctx, single_ref_p3_ctx, | |
| 560 | + single_ref_p5_ctx, | |
| 561 | + }; | |
| 562 | + use crate::obu::temporal_delimiter; | |
| 563 | + | |
| 564 | + let (width, height) = (32usize, 32usize); | |
| 565 | + let base_q_idx = 100u8; | |
| 566 | + // The whole picture is one 32x32 block: `partition_ctx`/`skip_ctx`/ | |
| 567 | + // `intra_inter_ctx`'s neighbour inputs are all "no neighbour yet" -- | |
| 568 | + // a fresh `Neighbours`' `above_side_mi`/`left_side_mi` start at `SB` | |
| 569 | + // (64), so `partition_ctx_mi`'s `side_mi * 2 <= side` (32) is false | |
| 570 | + // on both axes (ctx 0), and `above_skip`/`left_skip`/`above_inter`/ | |
| 571 | + // `left_inter`/`above_ref`/`left_ref` all start `false`/`-1` | |
| 572 | + // (decode.rs `Neighbours::new`) -- computed here without | |
| 573 | + // constructing the (module-private) `Neighbours` itself. | |
| 574 | + const PARTITION_NONE: usize = 0; | |
| 575 | + let ii_ctx = intra_inter_ctx(false, false, false, false); | |
| 576 | + let sr_p1_ctx = single_ref_p1_ctx(None, None); | |
| 577 | + let sr_p3_ctx = single_ref_p3_ctx(None, None); | |
| 578 | + let sr_p5_ctx = single_ref_p5_ctx(None, None); | |
| 579 | + | |
| 580 | + for run in 0..4 { | |
| 581 | + let picture = test_card(width, height); | |
| 582 | + let key = encode_key_frame(&picture, base_q_idx, 0.5).unwrap(); | |
| 583 | + let (seq, _) = key_frame_headers(width, height, base_q_idx).unwrap(); | |
| 584 | + | |
| 585 | + // Frame 1: hand-coded, intra-only, refreshes slot 0 with a flat | |
| 586 | + // 128 block (no neighbours to predict `DC_PRED` from). | |
| 587 | + let (_, header1) = inter_frame_headers(width, height, base_q_idx, 1, 0).unwrap(); | |
| 588 | + let tile1 = { | |
| 589 | + let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); | |
| 590 | + let mut enc = SymbolEncoder::new(); | |
| 591 | + enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[0]); | |
| 592 | + enc.symbol(1, &mut cdfs.skip[0]); // skip | |
| 593 | + enc.symbol(0, &mut cdfs.intra_inter[ii_ctx]); // is_inter = false | |
| 594 | + enc.symbol(0, &mut cdfs.y_mode[3]); // DC_PRED, size group 3 (32x32) | |
| 595 | + enc.symbol(0, &mut cdfs.uv_mode_cfl[0]); // DC_PRED | |
| 596 | + enc.finish() | |
| 597 | + }; | |
| 598 | + let mut stream = key.stream.clone(); | |
| 599 | + stream.extend(temporal_delimiter()); | |
| 600 | + stream.extend(frame_obu(&seq, &header1, &tile1).unwrap()); | |
| 601 | + | |
| 602 | + // Frame 2: hand-coded, single_ref all the way to GOLDEN_FRAME, | |
| 603 | + // skip zero-MV NEARESTMV -- a direct copy of slot 3 (frame 0). | |
| 604 | + let (_, mut header2) = inter_frame_headers(width, height, base_q_idx, 2, 0).unwrap(); | |
| 605 | + header2.ref_frame_idx[3] = 3; // GOLDEN_FRAME's own slot: frame 0's, untouched by frame 1 | |
| 606 | + let tile2 = { | |
| 607 | + let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); | |
| 608 | + let mut enc = SymbolEncoder::new(); | |
| 609 | + let grid = MiGrid::new(8, 8); | |
| 610 | + let stack = find_mv_stack(&grid, 0, 0, 8, 8, GOLDEN_FRAME, 8, 8); | |
| 611 | + enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[0]); | |
| 612 | + enc.symbol(1, &mut cdfs.skip[0]); // skip | |
| 613 | + enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]); // is_inter = true | |
| 614 | + enc.symbol(0, &mut cdfs.single_ref[sr_p1_ctx][0]); // p1: LAST/LAST2/LAST3/GOLDEN group | |
| 615 | + enc.symbol(1, &mut cdfs.single_ref[sr_p3_ctx][2]); // p3: GOLDEN/LAST3 sub-group | |
| 616 | + enc.symbol(1, &mut cdfs.single_ref[sr_p5_ctx][4]); // p5: GOLDEN_FRAME | |
| 617 | + enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not_new -> not NEWMV | |
| 618 | + enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not_zero -> not GLOBALMV | |
| 619 | + enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // nearest -> NEARESTMV, (0, 0) | |
| 620 | + enc.finish() | |
| 621 | + }; | |
| 622 | + stream.extend(temporal_delimiter()); | |
| 623 | + stream.extend(frame_obu(&seq, &header2, &tile2).unwrap()); | |
| 624 | + | |
| 625 | + let before = non_last_ref_hits(); | |
| 626 | + let decoded = decode_stream(&stream).unwrap(); | |
| 627 | + assert_eq!(decoded.len(), 3, "run {run}: expected 3 pictures"); | |
| 628 | + assert!( | |
| 629 | + non_last_ref_hits() > before, | |
| 630 | + "run {run}: GOLDEN_FRAME never fired" | |
| 631 | + ); | |
| 632 | + | |
| 633 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 3); | |
| 634 | + for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { | |
| 635 | + assert_eq!(got.y, want.y, "run {run} frame {i}: luma vs ffmpeg"); | |
| 636 | + assert_eq!(got.u, want.u, "run {run} frame {i}: U vs ffmpeg"); | |
| 637 | + assert_eq!(got.v, want.v, "run {run} frame {i}: V vs ffmpeg"); | |
| 638 | + } | |
| 639 | + assert_eq!( | |
| 640 | + decoded[0].y, key.reconstruction.y, | |
| 641 | + "run {run}: frame 0 vs the encoder's own reconstruction" | |
| 642 | + ); | |
| 643 | + assert_eq!( | |
| 644 | + decoded[2].y, key.reconstruction.y, | |
| 645 | + "run {run}: GOLDEN_FRAME's block did not reproduce the keyframe it names" | |
| 646 | + ); | |
| 647 | + assert_ne!( | |
| 648 | + decoded[2].y, decoded[1].y, | |
| 649 | + "run {run}: LAST_FRAME's and GOLDEN_FRAME's slots were not actually \ | |
| 650 | + distinguishable -- the gate proves nothing" | |
| 651 | + ); | |
| 652 | + } | |
| 653 | + } | |
| 654 | + | |
| 525 | 655 | /// Encodes a real libaom-av1 stream (via ffmpeg's own encoder, not this |
| 526 | 656 | /// crate's) with the same reduced-partition flags lane-av1adst's r6 |
| 527 | 657 | /// probe found still select `ADST_ADST`/`ADST_DCT`/`DCT_ADST` on |