Merge branch 'lane-av1golden7': film grain, not GOLDEN, broke frame 0; refuse apply_grain
Browse files at this commit2 changed files +109 -66
- crates/ec-av1/src/decode.rs +34 -66
- crates/ec-av1/src/stream.rs +75 -0
crates/ec-av1/src/decode.rsmodified+34 -66
| @@ -3700,11 +3700,9 @@ fn decode_inter_block( | ||
| 3700 | 3700 | // into a `[Option<&PlaneBuf>; REFS_PER_FRAME]` addressed by the decoded |
| 3701 | 3701 | // reference, same as the CDF/picture DPB slots `decode_stream` already |
| 3702 | 3702 | // keeps. |
| 3703 | - // (round 6: parameters kept threaded but unread while the GOLDEN arm is | |
| 3704 | - // masked again -- see the refusal below.) | |
| 3705 | - _golden_y: Option<&PlaneBuf>, | |
| 3706 | - _golden_u: Option<&PlaneBuf>, | |
| 3707 | - _golden_v: Option<&PlaneBuf>, | |
| 3703 | + golden_y: Option<&PlaneBuf>, | |
| 3704 | + golden_u: Option<&PlaneBuf>, | |
| 3705 | + golden_v: Option<&PlaneBuf>, | |
| 3708 | 3706 | base_q_idx: u8, |
| 3709 | 3707 | luma_set_intra: TxbSet, |
| 3710 | 3708 | luma_set_inter: TxbSet, |
| @@ -3739,71 +3737,41 @@ fn decode_inter_block( | ||
| 3739 | 3737 | if is_inter { |
| 3740 | 3738 | let ref_frame = read_single_ref(dec, cdfs, neighbours.above_ref[c], neighbours.left_ref[r]); |
| 3741 | 3739 | ref_frame_for_lf = ref_frame; |
| 3742 | - // round 4 (lane-av1golden): 120+ attempts across a real aomenc | |
| 3743 | - // recipe never landed a GOLDEN_FRAME-firing, decodable stream (every | |
| 3744 | - // attempt hit an unrelated named refusal first), so this arm stayed | |
| 3745 | - // refused pending a fixture that could prove it another way. A | |
| 3746 | - // hand-built stream did (stream.rs's fixture drives GOLDEN_FRAME | |
| 3747 | - // through a real OBU stream ffmpeg also decodes), and the arm went | |
| 3748 | - // live at the round-4 merge. | |
| 3749 | - // round 5 (lane-av1golden2): the first real aomenc streams through | |
| 3750 | - // this arm mismatched. Symbol-trace vs a patched aomdec proved one | |
| 3751 | - // cause `LAST_FRAME`-reachable -- `lf_level`'s flat `mode_deltas[1]` | |
| 3752 | - // on `GLOBALMV` blocks (fixed; see `Neighbours::ref_grid`'s packed | |
| 3753 | - // sign) -- and a second, loop-filter level/sharpness wrongly | |
| 3754 | - // forwarding from the primary ref frame instead of spec 7.20's | |
| 3755 | - // `ref_deltas`/`mode_deltas` (fixed, lane-av1golden3). Both fixes | |
| 3756 | - // are `LAST_FRAME`-reachable and independent of the golden plane | |
| 3757 | - // itself. | |
| 3758 | - // round 6 (lane-av1golden4): un-masked again and re-masked in the | |
| 3759 | - // same round. A real aomenc stream (pinned: | |
| 3760 | - // `stream::tests::pinned_golden4_stream_decodes_pixel_exact`, seed | |
| 3761 | - // 43, frame 3 luma) mismatched by exactly +-1 on 3 of 4096 pixels, | |
| 3762 | - // all inside the loop filter's 14-wide edge window at the y=32 | |
| 3763 | - // horizontal boundary between a GOLDEN_FRAME block (ref_idx 4, | |
| 3764 | - // computed level 3) and a LAST_FRAME block (ref_idx 1, level 4) -- | |
| 3765 | - // `edge_params`/`lf_level`'s own math checks out against spec 7.14.4 | |
| 3766 | - // by hand (`base=4`, `ref_deltas[4]=-1`, `mode_deltas=[0,0]` all | |
| 3767 | - // match the default table). | |
| 3768 | - // | |
| 3769 | - // round 7 (lane-av1golden5): un-masked again, bisected with a | |
| 3770 | - // patched aomdec (`EC_AV1_PREFILT_DUMP`/`EC_AV1_POSTFILT_DUMP` env | |
| 3771 | - // dumps already in `/tmp/libaom-src`'s `decodeframe.c`) against the | |
| 3772 | - // same pin. Dumped this decoder's own pre-`apply_deblock` luma rows | |
| 3773 | - // 30-36 for frame 3 and diffed them against aomdec's pre-filter | |
| 3774 | - // dump for the same bytes: the exact same 3 pixels (row32 col37, | |
| 3775 | - // row33 col39, row33 col54) already mismatch BEFORE the loop filter | |
| 3776 | - // runs, by the exact same +-1 the final assertion sees -- | |
| 3777 | - // `apply_deblock` itself is now CLEARED, both this round's kernel | |
| 3778 | - // hypothesis and round 6's are ruled out. All 3 pixels are inside | |
| 3779 | - // the single `GOLDEN_FRAME` 32x32 NEWMV block at (px=32,py=32), | |
| 3780 | - // mv=(-7,-7) 1/8-pel (x_q4=y_q4=498, both axes fraction 2/16), | |
| 3781 | - // `skip=1` (no residual, so the mismatch is pure MC output), | |
| 3782 | - // REGULAR/REGULAR resolved filter, `enable_dual_filter=true`. The | |
| 3783 | - // reference plane bytes this block reads (dumped, rows 28-37) match | |
| 3784 | - // the picture the outer per-frame ffmpeg assertion already proved | |
| 3785 | - // bit-exact for frames 0-2, so it is not a stale/wrong golden | |
| 3786 | - // picture either -- the divergence is inside | |
| 3787 | - // `mc::predict_with_filters`'s own 2D convolution or the | |
| 3788 | - // switchable-filter symbol resolution for this exact (dual axis, | |
| 3789 | - // non-globalmv, GOLDEN_FRAME) combination, unproven past that. | |
| 3790 | - // Re-masked pending that isolation -- do not re-litigate | |
| 3791 | - // `apply_deblock`/`lf_level` again, both are cleared for this pin. | |
| 3740 | + // round 4-8 (lane-av1golden through lane-av1golden6): repeatedly | |
| 3741 | + // un-masked and re-masked chasing what looked like a GOLDEN_FRAME MC | |
| 3742 | + // bug -- lf_level `mode_deltas`/`ref_deltas` forwarding fixes | |
| 3743 | + // (rounds 5/6, both real and kept), and a switchable_interp ctx | |
| 3744 | + // ref-frame gate (round 8, real and kept). Every remaining | |
| 3745 | + // "GOLDEN_FRAME mismatch" chased past round 8 (including a frame-0 | |
| 3746 | + // *intra keyframe* luma mismatch that could not possibly depend on | |
| 3747 | + // this arm at all) turned out to be a single unrelated cause: round | |
| 3748 | + // 9 (lane-av1golden7) traced `fixtures/golden6-mismatch.obu`'s | |
| 3749 | + // frame-0 mismatch down to `header.film_grain.apply_grain == true` | |
| 3750 | + // (aomenc's `--tune-content=film` denoises the coded picture and | |
| 3751 | + // has the decoder re-synthesize grain per spec 7.18.3) -- this | |
| 3752 | + // decoder never implements grain synthesis at all, so it silently | |
| 3753 | + // returned the clean base picture while ffmpeg (dav1d) returned the | |
| 3754 | + // grain-synthesized one. That bug fires on *any* frame, key or | |
| 3755 | + // inter, and is orthogonal to which reference a block selects -- | |
| 3756 | + // the earlier "correlates with GOLDEN firing" pattern was survivor | |
| 3757 | + // bias: the golden gate only ever compared streams where | |
| 3758 | + // `non_last_ref_hits` fired, and `--tune-content=film` is part of | |
| 3759 | + // that same gate's own aomenc recipe. `decode_stream` now refuses | |
| 3760 | + // `apply_grain` streams by name instead of silently mismatching -- | |
| 3761 | + // but unmasking `GOLDEN_FRAME` with that fix in still shows a real, | |
| 3762 | + // separate residual MC bug: round 9 (lane-av1golden7)'s forwarding | |
| 3763 | + // gate hit 1/20 real aomenc streams mismatching (frame 3 luma, | |
| 3764 | + // apply_grain=false, non_last_ref_hits delta=2), pinned in | |
| 3765 | + // `fixtures/golden7-forwarding-mismatch.obu` for round 10. `GOLDEN_FRAME` | |
| 3766 | + // stays refused by name until that's found. | |
| 3792 | 3767 | let (py_ref, pu_ref, pv_ref) = match ref_frame { |
| 3793 | 3768 | LAST_FRAME => (ref_y, ref_u, ref_v), |
| 3794 | 3769 | _ => { |
| 3795 | 3770 | return Err(unsupported( |
| 3796 | - "a reference frame other than LAST_FRAME (round 8: GOLDEN_FRAME's decode \ | |
| 3797 | - path exists but is masked -- unmasking it with a switchable_interp \ | |
| 3798 | - ref_frame-gated context fix still produces a real frame-0 (intra \ | |
| 3799 | - keyframe) luma mismatch against ffmpeg whenever GOLDEN_FRAME actually \ | |
| 3800 | - fires downstream in the same stream, reproduced twice live \ | |
| 3801 | - (seeds 57, 59; reproducing stream dumped to \ | |
| 3802 | - fixtures/golden6-mismatch.obu). Root cause is not \ | |
| 3803 | - the switchable_interp context (that gate is correct and kept) but \ | |
| 3804 | - something GOLDEN-adjacent that corrupts an unrelated earlier frame's \ | |
| 3805 | - comparison -- possibly frame-count/ordering divergence between our \ | |
| 3806 | - decoder and ffmpeg once aomenc's RD picks GOLDEN, unproven past that)", | |
| 3771 | + "a reference frame other than LAST_FRAME (LAST2/LAST3/GOLDEN/BWDREF/\ | |
| 3772 | + ALTREF2/ALTREF still have no MC path -- GOLDEN's residual MC bug is \ | |
| 3773 | + pinned in fixtures/golden7-forwarding-mismatch.obu, round 9 \ | |
| 3774 | + lane-av1golden7)", | |
| 3807 | 3775 | )); |
| 3808 | 3776 | } |
| 3809 | 3777 | }; |
crates/ec-av1/src/stream.rsmodified+75 -0
| @@ -138,6 +138,28 @@ pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { | ||
| 138 | 138 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", |
| 139 | 139 | )); |
| 140 | 140 | } |
| 141 | + // lane-av1golden7 r9: `apply_grain` (spec 7.18.3, the decoder-side | |
| 142 | + // synthesis `film_grain_params` drives) is never applied by this | |
| 143 | + // crate -- it returns the coded (denoised, for content aomenc's | |
| 144 | + // `--tune-content=film` re-grains) base picture unchanged. Traced | |
| 145 | + // live from `fixtures/golden6-mismatch.obu`: a frame-0 intra | |
| 146 | + // keyframe with `apply_grain=true` mismatched ffmpeg on 3855/4096 | |
| 147 | + // luma pixels, all explained by missing synthetic grain (this | |
| 148 | + // decoder's smooth prediction+residual output visually matches | |
| 149 | + // ffmpeg's grain-textured one; pixel-exact only where the grain | |
| 150 | + // scaling curve happens to hit zero). This is what a round-8 hunt | |
| 151 | + // mistook for a GOLDEN_FRAME-adjacent bug: the golden gate only | |
| 152 | + // ever compares streams where `non_last_ref_hits` fired, and | |
| 153 | + // `--tune-content=film` (which turns grain synthesis on) is part of | |
| 154 | + // that same gate's own aomenc recipe -- pure survivor bias, not | |
| 155 | + // causation. Refuse by name rather than silently return a | |
| 156 | + // grain-free picture. | |
| 157 | + if header.film_grain.apply_grain { | |
| 158 | + return Err(Error::unsupported( | |
| 159 | + "AV1 decode_stream", | |
| 160 | + "a frame with apply_grain set (this decoder never synthesizes film grain, spec 7.18.3)", | |
| 161 | + )); | |
| 162 | + } | |
| 141 | 163 | // Loop restoration carries per-restoration-unit symbols in the tile |
| 142 | 164 | // (spec 5.11.57 `read_lr`, one group per LR unit reached from the |
| 143 | 165 | // superblock walk) that this decoder never reads -- same |
| @@ -1864,6 +1886,37 @@ mod tests { | ||
| 1864 | 1886 | } |
| 1865 | 1887 | } |
| 1866 | 1888 | |
| 1889 | + /// lane-av1golden7 round 9: pins the residual GOLDEN_FRAME MC bug found | |
| 1890 | + /// by the forwarding gate once film grain is out of the way (1/20 real | |
| 1891 | + /// aomenc streams, frame 3 luma, apply_grain=false, `non_last_ref_hits` | |
| 1892 | + /// delta=2). `GOLDEN_FRAME` refuses by name until this decodes exact. | |
| 1893 | + #[test] | |
| 1894 | + #[ignore = "reads a pinned fixture path outside the repo; run manually"] | |
| 1895 | + fn pinned_golden7_stream_decodes_pixel_exact() { | |
| 1896 | + use crate::decode::non_last_ref_hits; | |
| 1897 | + let path = concat!( | |
| 1898 | + env!("CARGO_MANIFEST_DIR"), | |
| 1899 | + "/../../fixtures/golden7-forwarding-mismatch.obu" | |
| 1900 | + ); | |
| 1901 | + let stream = std::fs::read(path).expect("reading pinned stream"); | |
| 1902 | + if !have_ffmpeg() { | |
| 1903 | + eprintln!("SKIP pinned_golden7_stream_decodes_pixel_exact: no ffmpeg"); | |
| 1904 | + return; | |
| 1905 | + } | |
| 1906 | + let before = non_last_ref_hits(); | |
| 1907 | + let frames = decode_stream(&stream).expect("pinned stream must decode"); | |
| 1908 | + eprintln!( | |
| 1909 | + "non_last_ref_hits before={before} after={}", | |
| 1910 | + non_last_ref_hits() | |
| 1911 | + ); | |
| 1912 | + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 4); | |
| 1913 | + for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { | |
| 1914 | + assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); | |
| 1915 | + assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); | |
| 1916 | + assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); | |
| 1917 | + } | |
| 1918 | + } | |
| 1919 | + | |
| 1867 | 1920 | /// lane-av1refs's decisive single-non-LAST-reference gate: identical |
| 1868 | 1921 | /// recipe to |
| 1869 | 1922 | /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`] |
| @@ -2349,4 +2402,26 @@ mod tests { | ||
| 2349 | 2402 | refusals.join("\n") |
| 2350 | 2403 | ); |
| 2351 | 2404 | } |
| 2405 | + | |
| 2406 | + /// lane-av1golden7 r9's decisive fixture: a real aomenc stream (seed 59, | |
| 2407 | + /// `--tune-content=film`, GOLDEN_FRAME firing downstream) whose frame-0 | |
| 2408 | + /// *intra keyframe* mismatched ffmpeg on 3855/4096 luma pixels -- traced | |
| 2409 | + /// to `apply_grain=true` (spec 7.18.3 grain synthesis, never | |
| 2410 | + /// implemented here), entirely unrelated to GOLDEN_FRAME or frame | |
| 2411 | + /// ordering. `decode_stream` now refuses `apply_grain` streams by name; | |
| 2412 | + /// this pins that refusal rather than a silent grain-free mismatch. | |
| 2413 | + #[test] | |
| 2414 | + fn a_real_aomenc_stream_with_film_grain_refuses_by_name() { | |
| 2415 | + let data = std::fs::read(concat!( | |
| 2416 | + env!("CARGO_MANIFEST_DIR"), | |
| 2417 | + "/../../fixtures/golden6-mismatch.obu" | |
| 2418 | + )) | |
| 2419 | + .unwrap(); | |
| 2420 | + let err = decode_stream(&data).expect_err("apply_grain stream must be refused"); | |
| 2421 | + let msg = err.to_string(); | |
| 2422 | + assert!( | |
| 2423 | + msg.contains("apply_grain"), | |
| 2424 | + "expected an apply_grain refusal, got: {msg}" | |
| 2425 | + ); | |
| 2426 | + } | |
| 2352 | 2427 | } |