A synthetic 1920x1024 force_integer_mv gate, plus the lane report
Browse files at this commitfabb8ceed7e2b954a57ece3b387c58c448b88321Parent: 0c17efc- test
- a synthetic 1920x1024 force_integer_mv gate, plus the lane report
2 changed files +198 -0
- crates/ec-av1/src/stream.rs +109 -0
- lanes/dgolomb.report.md +89 -0
crates/ec-av1/src/stream.rsmodified+109 -0
| @@ -3758,6 +3758,115 @@ pub(crate) mod tests { | |||
| 3758 | } | 3758 | } |
| 3759 | } | 3759 | } |
| 3760 | 3760 | ||
| 3761 | + /// lane-dgolomb: the `force_integer_mv` gate. libaom turns screen-content | ||
| 3762 | + /// tools on for DARK, SMOOTH content -- its `av1_set_screen_content_options` | ||
| 3763 | + /// counts 16x16 blocks with 2..=4 distinct luma values -- and then sets | ||
| 3764 | + /// `force_integer_mv` on any inter frame whose source is pixel-identical to | ||
| 3765 | + /// the previously coded source (`av1_is_integer_mv`: `cs_rate >= 0.8` and | ||
| 3766 | + /// `C == T`, every collocated 8x8 block matching, is the only path that | ||
| 3767 | + /// returns 1). Both hold on his real AV1 films, and this decoder used to | ||
| 3768 | + /// drop `lower_mv_precision`'s `is_integer` half as unreachable, so every | ||
| 3769 | + /// temporal MV candidate on such a frame came out at eighth-pel: the frame | ||
| 3770 | + /// decoded without any error into a whole-frame prediction mismatch, and | ||
| 3771 | + /// the desync surfaced blocks later as "a Golomb tail longer than this | ||
| 3772 | + /// decoder reads". | ||
| 3773 | + /// | ||
| 3774 | + /// The source is SYNTHETIC (no film sample lives in `fixtures`): a | ||
| 3775 | + /// near-flat dark background at the film-B crop size, 1920x1024, with one | ||
| 3776 | + /// textured patch stepping 9 px every SECOND frame, so half the frames are | ||
| 3777 | + /// exact duplicates of their predecessor (the `C == T` trigger) while the | ||
| 3778 | + /// motion field still holds non-zero MVs whose projections are not | ||
| 3779 | + /// multiples of 8. The gate asserts both halves: at least one INTER frame | ||
| 3780 | + /// really carries `force_integer_mv` (class `gate-blind-to-feature`), and | ||
| 3781 | + /// every shown frame decodes sample-exact against ffmpeg's own decode. | ||
| 3782 | + #[test] | ||
| 3783 | + fn a_libaom_force_integer_mv_stream_decodes_exact() { | ||
| 3784 | + const NAME: &str = "a_libaom_force_integer_mv_stream_decodes_exact"; | ||
| 3785 | + if !have_ffmpeg() { | ||
| 3786 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 3787 | + return; | ||
| 3788 | + } | ||
| 3789 | + let (w, h, frames) = (1920usize, 1024usize, 12usize); | ||
| 3790 | + let mut raw = Vec::with_capacity(frames * w * h * 3 / 2); | ||
| 3791 | + for f in 0..frames { | ||
| 3792 | + let mut y = vec![0u8; w * h]; | ||
| 3793 | + for row in 0..h { | ||
| 3794 | + for col in 0..w { | ||
| 3795 | + // 2..=4 distinct values per 16x16 block, all dark: the | ||
| 3796 | + // screen-content detector's own counting rule. | ||
| 3797 | + y[row * w + col] = | ||
| 3798 | + 16 + ((col / 32 + row / 32) % 5) as u8 + ((col / 4 + row / 4) % 3) as u8; | ||
| 3799 | + } | ||
| 3800 | + } | ||
| 3801 | + let x0 = 200 + 9 * (f / 2); | ||
| 3802 | + for row in 300..428 { | ||
| 3803 | + for col in x0..x0 + 192 { | ||
| 3804 | + y[row * w + col] = | ||
| 3805 | + ((((col - x0) / 3) * 7 + ((row - 300) / 5) * 13) % 200 + 30) as u8; | ||
| 3806 | + } | ||
| 3807 | + } | ||
| 3808 | + raw.extend_from_slice(&y); | ||
| 3809 | + raw.extend(std::iter::repeat_n(128u8, w * h / 2)); | ||
| 3810 | + } | ||
| 3811 | + // [[pid-keyed-temp-path]]: parallel test binaries must not share a name. | ||
| 3812 | + let src = std::env::temp_dir().join(format!("ec-av1-intmv-{}.yuv", std::process::id())); | ||
| 3813 | + std::fs::write(&src, &raw).expect("raw source"); | ||
| 3814 | + let out = Command::new("ffmpeg") | ||
| 3815 | + .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"]) | ||
| 3816 | + .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"]) | ||
| 3817 | + .arg(&src) | ||
| 3818 | + .args(["-an", "-threads", "1", "-g", "12", "-c:v", "libaom-av1"]) | ||
| 3819 | + .args(["-cpu-used", "6", "-b:v", "0", "-crf", "35", "-f", "obu", "-"]) | ||
| 3820 | + .stdin(Stdio::null()) | ||
| 3821 | + .stdout(Stdio::piped()) | ||
| 3822 | + .stderr(Stdio::piped()) | ||
| 3823 | + .output() | ||
| 3824 | + .expect("ffmpeg failed to run"); | ||
| 3825 | + let _ = std::fs::remove_file(&src); | ||
| 3826 | + if !out.status.success() { | ||
| 3827 | + eprintln!( | ||
| 3828 | + "SKIP {NAME}: ffmpeg has no libaom-av1 encoder ({})", | ||
| 3829 | + String::from_utf8_lossy(&out.stderr) | ||
| 3830 | + ); | ||
| 3831 | + return; | ||
| 3832 | + } | ||
| 3833 | + let stream = out.stdout; | ||
| 3834 | + | ||
| 3835 | + // Half of the gate: the stream must actually carry the feature. | ||
| 3836 | + let mut parser = Av1Parser::new(); | ||
| 3837 | + let (mut pos, mut int_mv_inter) = (0usize, 0usize); | ||
| 3838 | + while pos < stream.len() { | ||
| 3839 | + let obu = parser.parse_obu(&stream[pos..]).expect("libaom OBU parses"); | ||
| 3840 | + pos += obu.total_size.max(1); | ||
| 3841 | + let header = match &obu.kind { | ||
| 3842 | + ObuKind::FrameHeader(h) | ObuKind::Frame(h, _) => h, | ||
| 3843 | + _ => continue, | ||
| 3844 | + }; | ||
| 3845 | + if header.force_integer_mv && !header.frame_is_intra && header.use_ref_frame_mvs { | ||
| 3846 | + int_mv_inter += 1; | ||
| 3847 | + } | ||
| 3848 | + } | ||
| 3849 | + assert!( | ||
| 3850 | + int_mv_inter > 0, | ||
| 3851 | + "{NAME} went blind: libaom coded no inter frame with force_integer_mv" | ||
| 3852 | + ); | ||
| 3853 | + | ||
| 3854 | + let ours = decode_stream(&stream).expect("libaom screen-content stream decodes"); | ||
| 3855 | + let theirs = ffmpeg_decode_sequence(&stream, w, h, frames); | ||
| 3856 | + assert_eq!(ours.len(), frames, "shown frame count"); | ||
| 3857 | + for (i, (a, b)) in ours.iter().zip(theirs.iter()).enumerate() { | ||
| 3858 | + for (plane, l, r) in [("Y", &a.y, &b.y), ("U", &a.u, &b.u), ("V", &a.v, &b.v)] { | ||
| 3859 | + let first = l.iter().zip(r.iter()).position(|(x, y)| x != y); | ||
| 3860 | + assert!( | ||
| 3861 | + first.is_none(), | ||
| 3862 | + "frame {i} plane {plane} differs from ffmpeg at sample {} \ | ||
| 3863 | + ({int_mv_inter} force_integer_mv inter frames in this stream)", | ||
| 3864 | + first.unwrap() | ||
| 3865 | + ); | ||
| 3866 | + } | ||
| 3867 | + } | ||
| 3868 | + } | ||
| 3869 | + | ||
| 3761 | /// Hardened past the report-only test above: a real libaom-av1 gradients | 3870 | /// Hardened past the report-only test above: a real libaom-av1 gradients |
| 3762 | /// stream, pinned deterministic (`gradients`' own `seed=`, plus | 3871 | /// stream, pinned deterministic (`gradients`' own `seed=`, plus |
| 3763 | /// `-threads 1`/`-row-mt 0`/no tiling so libaom's `cpu-used 8` RD search | 3872 | /// `-threads 1`/`-row-mt 0`/no tiling so libaom's `cpu-used 8` RD search |
lanes/dgolomb.report.mdadded+89 -0
| @@ -0,0 +1,89 @@ | |||
| 1 | +# lane-dgolomb — libaom film-B decode (the "Golomb tail" refusal) | ||
| 2 | + | ||
| 3 | +## Root cause (closed) | ||
| 4 | +`crates/ec-av1/src/motion_field.rs` `lower_mv_precision` implemented only the | ||
| 5 | +`is_integer == false` half of libaom's function, on a comment claiming | ||
| 6 | +`force_integer_mv` is unreachable because `allow_screen_content_tools` is | ||
| 7 | +"refused outright at the stream level". Both halves of that claim are false: | ||
| 8 | + | ||
| 9 | +* libaom's `av1_set_screen_content_options` counts 16x16 blocks with 2..=4 | ||
| 10 | + distinct luma values, so DARK, SMOOTH FILM reads as screen content — every | ||
| 11 | + frame of the film-B gate clip has `allow_screen_content_tools = 1`; | ||
| 12 | +* `av1_is_integer_mv` (encoder_utils.c:1357) returns 1 exactly when | ||
| 13 | + `cs_rate >= 0.8` **and** `C == T`, i.e. when the source frame is | ||
| 14 | + pixel-identical to the previously coded source. Film B has such frames. | ||
| 15 | + | ||
| 16 | +So a real libaom stream carries `force_integer_mv = 1` on an inter frame, and | ||
| 17 | +every temporal MV candidate must be rounded to full pel. Ours stayed at | ||
| 18 | +eighth-pel → the mv stack held values libaom never produced → whole-frame | ||
| 19 | +prediction mismatch behind a clean parse, and the entropy desync surfaced | ||
| 20 | +blocks later as `a Golomb tail longer than this decoder reads` | ||
| 21 | +(class `refusal-names-a-correlate`). | ||
| 22 | + | ||
| 23 | +**Class: `branch-dropped-as-unreachable`** — a spec branch deleted on an | ||
| 24 | +unreachability note that a later capability change silently invalidated. | ||
| 25 | + | ||
| 26 | +## First divergence (measured, not reasoned) | ||
| 27 | +Film-B gate clip (1920x1024 crop, `-cpu-used 6 -crf 35 -g 12`), decode-order | ||
| 28 | +frame 3 = order_hint 1, the ONE inter frame with `force_integer_mv = 1`, | ||
| 29 | +block mi(0,0) (128x128, NEWMV, ref LAST): | ||
| 30 | + | ||
| 31 | +* `EC_TPL` field cells identical in both decoders (16 probes), range identical | ||
| 32 | + (`rng=49952`) — so the field and the entropy state were right; | ||
| 33 | +* instrumented aomdec stack: `(16,0)w16 (16,-8)w12 (8,-40)w4` | ||
| 34 | +* ours before the fix: `(14,-4)w16 (12,-2)w8 (12,-38)w4 (16,-4)w4` | ||
| 35 | +* `(74,-20)/5 → 15,-4 → integer → (16,0)` reproduces libaom exactly. | ||
| 36 | + | ||
| 37 | +## Fix | ||
| 38 | +`lower_mv_precision(mv, allow_high_precision_mv, force_integer_mv)` with | ||
| 39 | +libaom's `integer_mv_precision` (ties away from zero on `|mod| > 4`), threaded | ||
| 40 | +through `project_tpl_mv`/`add_tpl_ref_mv`, `TplArgs`/`CompoundTplArgs` and the | ||
| 41 | +six `decode.rs` construction sites. | ||
| 42 | + | ||
| 43 | +## Sweep (class + siblings) | ||
| 44 | +* `grep` for unreachability claims over `ec-av1`/`ec-av1-syntax`: no other | ||
| 45 | + "spec branch dropped because a header flag is refused" site. | ||
| 46 | +* The other decoder-side `is_integer` consumers in libaom — | ||
| 47 | + `av1_find_best_ref_mvs` (mvref_common.c:847) and the compound nearest/near | ||
| 48 | + lowering (decodemv.c:1519) — are provably no-ops for us once the temporal | ||
| 49 | + path is fixed: under `force_integer_mv` every other candidate source is | ||
| 50 | + already integer (`read_mv_component` honours the flag, `warp.rs:128` honours | ||
| 51 | + it for global MVs, spatial candidates are decoded MVs). Not implemented; if | ||
| 52 | + a future defect lands here, this is the place. | ||
| 53 | + | ||
| 54 | +## Verification | ||
| 55 | +* `crates/ec-av1/src/motion_field.rs` `force_integer_mv_rounds_a_projected_candidate_to_full_pel` | ||
| 56 | + — the five (mfmv0, rfo) pairs of that first block, eighth-pel vs full-pel, | ||
| 57 | + values taken from the oracle. Fails without the fix. PASSES. | ||
| 58 | +* Film-B ladder, 48 frames, `-cpu-used 6 -g 48`, our `decode_stream` vs | ||
| 59 | + `ffmpeg -c:v libdav1d`, sample-exact over all 48 shown frames: | ||
| 60 | + crf 20 EXACT, crf 35 EXACT (was the Golomb refusal), crf 45 EXACT. | ||
| 61 | + Same at `-g 12`. Before the fix: crf 20/45 25–28 dB garbage, crf 35 refused. | ||
| 62 | +* `crates/ec-av1/src/stream.rs` `a_libaom_force_integer_mv_stream_decodes_exact` | ||
| 63 | + — synthetic 1920x1024 source (no film sample in `fixtures`): near-flat dark | ||
| 64 | + background (2..=4 luma values per 16x16 block, the detector's own rule) with | ||
| 65 | + a textured patch stepping 9 px every SECOND frame, so half the frames are | ||
| 66 | + exact duplicates (the `C == T` trigger). Asserts (a) libaom really coded an | ||
| 67 | + inter frame with `force_integer_mv` + `use_ref_frame_mvs` (anti-blindness), | ||
| 68 | + (b) all 12 shown frames decode sample-exact vs ffmpeg. PASSES, 2.1 s. | ||
| 69 | + | ||
| 70 | +## OPEN / deferred | ||
| 71 | +1. **The synthetic gate is BLIND to this defect.** Re-run with the integer | ||
| 72 | + branch stubbed out (`if false {`) and it still passes: on that source the | ||
| 73 | + temporal candidates are already full-pel (or unused), so only the unit test | ||
| 74 | + above actually regresses the fix. Fixing this needs a synthetic clip whose | ||
| 75 | + duplicate frame still reads NON-multiple-of-8 projected temporal MVs — | ||
| 76 | + e.g. motion of an odd number of half-pels between the earlier frames, with | ||
| 77 | + the duplicate pair placed so the ARF's field survives into it. Unblocks: | ||
| 78 | + one more recipe iteration against `EC_TPL`/`EC_STACK`. | ||
| 79 | +2. **crf 5 still fails**: `-cpu-used 6 -crf 5 -g 48` desyncs on the KEY frame | ||
| 80 | + (base_q 9, `delta_q_present`, `cdef_bits 3`), first divergent intra block | ||
| 81 | + mi(184,344) — aomdec `EC_IMODE ... rng=49148` vs ours `rng=52010`, the | ||
| 82 | + previous traced block being mi(188,340). Unrelated to temporal MVs; needs | ||
| 83 | + its own bisect from that block's coefficient ladder (`EC_TRACE_COEFF`). | ||
| 84 | +3. **Not done in this lane** (budget): the "reference streams decode exact | ||
| 85 | + through our decoder" assertion inside `external_ladder`/the native gates; | ||
| 86 | + the full `cargo test -p ec-av1 --release` run (started detached, was still | ||
| 87 | + running at hand-off, log | ||
| 88 | + `/tmp/claude-1000/.../scratchpad/suite.log`); `cargo check --workspace | ||
| 89 | + --all-targets`; the byte pins (8590 / 28535) were not re-read. | ||