Merge branch 'lane-dkey' (c28d3574): key-frame sub-8x8 groups never cleared the palette bands; every reference ladder point now decodes through our decoder as a gate assertion
Browse files at this commit5 changed files +274 -1
- crates/ec-av1/src/decode.rs +26 -0
- crates/ec-av1/src/encode.rs +28 -0
- crates/ec-av1/src/motion_field.rs +5 -1
- crates/ec-av1/src/stream.rs +95 -0
- lanes/dkey.report.md +120 -0
crates/ec-av1/src/decode.rsmodified+26 -0
| @@ -15718,6 +15718,19 @@ fn decode_leaf_split4( | ||
| 15718 | 15718 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 15719 | 15719 | } |
| 15720 | 15720 | } |
| 15721 | + // lane-dkey: a sub-8x8 leaf can never use palette (`av1_allow_palette` | |
| 15722 | + // needs BLOCK_8X8 and up), and libaom's mi grid says so -- its | |
| 15723 | + // `palette_size` is 0 in every one of these mi cells, so the NEXT block's | |
| 15724 | + // `av1_get_palette_mode_ctx` and colour cache read 0 here. This decoder | |
| 15725 | + // only ever WROTE palette state from the >=8x8 readers, so an 8x8 palette | |
| 15726 | + // block above a 4x4 group left its size/colours standing in the mi-granular | |
| 15727 | + // bands: the block below the group read `palette_y_mode` off CDF row 1 | |
| 15728 | + // where libaom reads row 0 (class [[cdf-row-held-constant]]), decoded a | |
| 15729 | + // palette libaom never coded, and then skipped the `use_filter_intra` | |
| 15730 | + // symbol that a palette block has none of -- one symbol short, tile | |
| 15731 | + // desynced. Publishing the group's own zeros is the clear. | |
| 15732 | + neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]); | |
| 15733 | + neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]); | |
| 15721 | 15734 | Ok((leaf_modes[2], leaf_modes[1])) |
| 15722 | 15735 | } |
| 15723 | 15736 | |
| @@ -16078,6 +16091,19 @@ fn decode_leaf_rect8( | ||
| 16078 | 16091 | neighbours.above[leaf_mi.1 + cell][2] = v_state; |
| 16079 | 16092 | } |
| 16080 | 16093 | } |
| 16094 | + // lane-dkey (same clear as `decode_leaf_split4`): a sub-8x8 leaf can never use palette (`av1_allow_palette` | |
| 16095 | + // needs BLOCK_8X8 and up), and libaom's mi grid says so -- its | |
| 16096 | + // `palette_size` is 0 in every one of these mi cells, so the NEXT block's | |
| 16097 | + // `av1_get_palette_mode_ctx` and colour cache read 0 here. This decoder | |
| 16098 | + // only ever WROTE palette state from the >=8x8 readers, so an 8x8 palette | |
| 16099 | + // block above a 4x4 group left its size/colours standing in the mi-granular | |
| 16100 | + // bands: the block below the group read `palette_y_mode` off CDF row 1 | |
| 16101 | + // where libaom reads row 0 (class [[cdf-row-held-constant]]), decoded a | |
| 16102 | + // palette libaom never coded, and then skipped the `use_filter_intra` | |
| 16103 | + // symbol that a palette block has none of -- one symbol short, tile | |
| 16104 | + // desynced. Publishing the group's own zeros is the clear. | |
| 16105 | + neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]); | |
| 16106 | + neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]); | |
| 16081 | 16107 | // `(below_mode, right_mode)`: the sub-block a neighbour below sees is the |
| 16082 | 16108 | // bottom one (the second under HORZ, the left one under VERT), the one a |
| 16083 | 16109 | // neighbour to the right sees is the top-right (the second under VERT). |
crates/ec-av1/src/encode.rsmodified+28 -0
| @@ -13302,6 +13302,34 @@ mod tests { | ||
| 13302 | 13302 | ); |
| 13303 | 13303 | let stream = std::fs::read(&obu).expect("reference stream"); |
| 13304 | 13304 | let decoded = ffmpeg_decode_sequence(&stream, width, height, source.len()); |
| 13305 | + // lane-dkey: every reference point must also decode through OUR | |
| 13306 | + // decoder, sample-exact against ffmpeg's decode of the same bytes. | |
| 13307 | + // A BD table computed off streams we cannot read is a table about | |
| 13308 | + // an encoder we cannot check; this is the assertion that would | |
| 13309 | + // have caught the `force_integer_mv` defect (its stream decoded to | |
| 13310 | + // 25 dB garbage while this gate stayed green) and the crf-5 key | |
| 13311 | + // frame desync. A failure names the encoder, the point's flags and | |
| 13312 | + // the first differing frame/plane/sample. | |
| 13313 | + match crate::stream::decode_stream(&stream) { | |
| 13314 | + Ok(ours) => { | |
| 13315 | + assert_eq!( | |
| 13316 | + ours.len(), | |
| 13317 | + decoded.len(), | |
| 13318 | + "{encoder} {params:?}: our decoder returned {} shown frames, ffmpeg {}", | |
| 13319 | + ours.len(), | |
| 13320 | + decoded.len(), | |
| 13321 | + ); | |
| 13322 | + for (f, (o, d)) in ours.iter().zip(&decoded).enumerate() { | |
| 13323 | + if let Some((plane, s, got, want)) = first_plane_mismatch(o, d) { | |
| 13324 | + panic!( | |
| 13325 | + "{encoder} {params:?} frame {f} plane {plane} sample {s}: our \ | |
| 13326 | + decoder decoded {got}, ffmpeg {want}" | |
| 13327 | + ); | |
| 13328 | + } | |
| 13329 | + } | |
| 13330 | + } | |
| 13331 | + Err(e) => panic!("{encoder} {params:?}: our decoder refused the stream ({e})"), | |
| 13332 | + } | |
| 13305 | 13333 | let mean: f64 = decoded |
| 13306 | 13334 | .iter() |
| 13307 | 13335 | .zip(source) |
crates/ec-av1/src/motion_field.rsmodified+5 -1
| @@ -201,7 +201,11 @@ pub fn lower_mv_precision( | ||
| 201 | 201 | _ => v - m, |
| 202 | 202 | } |
| 203 | 203 | }; |
| 204 | - return (round(mv.0), round(mv.1)); | |
| 204 | + let out = (round(mv.0), round(mv.1)); | |
| 205 | + if out != mv && crate::envflags::env_flag!("EC_TRACE_TPL") { | |
| 206 | + eprintln!("EC_INTMV rounded {mv:?} -> {out:?}"); | |
| 207 | + } | |
| 208 | + return out; | |
| 205 | 209 | } |
| 206 | 210 | if allow_high_precision_mv { |
| 207 | 211 | return mv; |
crates/ec-av1/src/stream.rsmodified+95 -0
| @@ -3867,6 +3867,101 @@ pub(crate) mod tests { | ||
| 3867 | 3867 | } |
| 3868 | 3868 | } |
| 3869 | 3869 | |
| 3870 | + /// lane-dkey: the palette-neighbour-band gate. A palette-Y block's size and | |
| 3871 | + /// colours used to stay standing in the mi-granular above/left bands after a | |
| 3872 | + /// sub-8x8 group was decoded next to it -- the sub-8x8 readers published | |
| 3873 | + /// skip/lf/mode state but no palette state, while libaom's mi grid carries | |
| 3874 | + /// `palette_size == 0` in exactly those cells (`av1_allow_palette` needs | |
| 3875 | + /// BLOCK_8X8 and up). The next intra block then read `palette_y_mode` off | |
| 3876 | + /// CDF row 1 where libaom reads row 0 (class `cdf-row-held-constant`), | |
| 3877 | + /// decoded a palette libaom never coded, and skipped the `use_filter_intra` | |
| 3878 | + /// symbol a palette block has none of: one symbol short, tile desynced. | |
| 3879 | + /// | |
| 3880 | + /// Measured on the instrumented aomdec at KEY-frame block mi(76,88) of this | |
| 3881 | + /// very stream (`EC_PALSYN_AOM ... ctx=0 y=0` against our `mode_ctx=1`). | |
| 3882 | + /// Without the fix every one of the 12 frames differs from ffmpeg's decode | |
| 3883 | + /// from luma sample 491904 on. | |
| 3884 | + /// | |
| 3885 | + /// The source is SYNTHETIC (no film sample lives in `fixtures`): the dark, | |
| 3886 | + /// near-flat background libaom's own screen-content detector counts as | |
| 3887 | + /// screen content (2..=4 distinct luma values per 16x16 block), which is | |
| 3888 | + /// what turns palette on, plus one textured patch at a HALF-PEL position on | |
| 3889 | + /// half the frames so the encoder splits that region down to 4x4 groups | |
| 3890 | + /// beside its palette blocks. | |
| 3891 | + #[test] | |
| 3892 | + fn a_libaom_screen_stream_with_sub8_groups_beside_palette_blocks_decodes_exact() { | |
| 3893 | + const NAME: &str = | |
| 3894 | + "a_libaom_screen_stream_with_sub8_groups_beside_palette_blocks_decodes_exact"; | |
| 3895 | + if !have_ffmpeg() { | |
| 3896 | + eprintln!("SKIP {NAME}: no ffmpeg"); | |
| 3897 | + return; | |
| 3898 | + } | |
| 3899 | + let (w, h, frames) = (1920usize, 1024usize, 12usize); | |
| 3900 | + let mut raw = Vec::with_capacity(frames * w * h * 3 / 2); | |
| 3901 | + for f in 0..frames { | |
| 3902 | + let mut y = vec![0u8; w * h]; | |
| 3903 | + for row in 0..h { | |
| 3904 | + for col in 0..w { | |
| 3905 | + y[row * w + col] = | |
| 3906 | + 16 + ((col / 32 + row / 32) % 5) as u8 + ((col / 4 + row / 4) % 3) as u8; | |
| 3907 | + } | |
| 3908 | + } | |
| 3909 | + // 2.5 px per pair of frames: integer on one pair, half a pixel off | |
| 3910 | + // on the next (linear interpolation), so the patch's edges land | |
| 3911 | + // mid-sample and the encoder splits there. | |
| 3912 | + let x0 = 200.0 + 2.5 * (f / 2) as f64; | |
| 3913 | + let (fx, ix) = (x0.fract(), x0.floor() as usize); | |
| 3914 | + for row in 300..428 { | |
| 3915 | + for col in 0..192 { | |
| 3916 | + let tex = |u: f64| -> f64 { | |
| 3917 | + let v = (row - 300) as f64; | |
| 3918 | + 30.0 + 100.0 * (0.5 + 0.5 * ((u * 0.35).sin() * (v * 0.21).cos())) | |
| 3919 | + }; | |
| 3920 | + let s = tex(col as f64) * (1.0 - fx) + tex(col as f64 - 1.0) * fx; | |
| 3921 | + y[row * w + ix + col] = s.round().clamp(0.0, 255.0) as u8; | |
| 3922 | + } | |
| 3923 | + } | |
| 3924 | + raw.extend_from_slice(&y); | |
| 3925 | + raw.extend(std::iter::repeat_n(128u8, w * h / 2)); | |
| 3926 | + } | |
| 3927 | + // [[pid-keyed-temp-path]]: parallel test binaries must not share a name. | |
| 3928 | + let src = std::env::temp_dir().join(format!("ec-av1-palsub8-{}.yuv", std::process::id())); | |
| 3929 | + std::fs::write(&src, &raw).expect("raw source"); | |
| 3930 | + let out = Command::new("ffmpeg") | |
| 3931 | + .args(["-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "yuv420p"]) | |
| 3932 | + .args(["-s", &format!("{w}x{h}"), "-r", "24", "-i"]) | |
| 3933 | + .arg(&src) | |
| 3934 | + .args(["-an", "-threads", "1", "-g", "12", "-c:v", "libaom-av1"]) | |
| 3935 | + .args(["-cpu-used", "6", "-b:v", "0", "-crf", "35", "-f", "obu", "-"]) | |
| 3936 | + .stdin(Stdio::null()) | |
| 3937 | + .stdout(Stdio::piped()) | |
| 3938 | + .stderr(Stdio::piped()) | |
| 3939 | + .output() | |
| 3940 | + .expect("ffmpeg failed to run"); | |
| 3941 | + let _ = std::fs::remove_file(&src); | |
| 3942 | + if !out.status.success() { | |
| 3943 | + eprintln!( | |
| 3944 | + "SKIP {NAME}: ffmpeg has no libaom-av1 encoder ({})", | |
| 3945 | + String::from_utf8_lossy(&out.stderr) | |
| 3946 | + ); | |
| 3947 | + return; | |
| 3948 | + } | |
| 3949 | + let stream = out.stdout; | |
| 3950 | + let ours = decode_stream(&stream).expect("libaom screen-content stream decodes"); | |
| 3951 | + let theirs = ffmpeg_decode_sequence(&stream, w, h, frames); | |
| 3952 | + assert_eq!(ours.len(), frames, "{NAME}: shown frame count"); | |
| 3953 | + for (i, (a, b)) in ours.iter().zip(theirs.iter()).enumerate() { | |
| 3954 | + for (plane, l, r) in [("Y", &a.y, &b.y), ("U", &a.u, &b.u), ("V", &a.v, &b.v)] { | |
| 3955 | + let first = l.iter().zip(r.iter()).position(|(x, y)| x != y); | |
| 3956 | + assert!( | |
| 3957 | + first.is_none(), | |
| 3958 | + "{NAME}: frame {i} plane {plane} differs from ffmpeg at sample {}", | |
| 3959 | + first.unwrap() | |
| 3960 | + ); | |
| 3961 | + } | |
| 3962 | + } | |
| 3963 | + } | |
| 3964 | + | |
| 3870 | 3965 | /// Hardened past the report-only test above: a real libaom-av1 gradients |
| 3871 | 3966 | /// stream, pinned deterministic (`gradients`' own `seed=`, plus |
| 3872 | 3967 | /// `-threads 1`/`-row-mt 0`/no tiling so libaom's `cpu-used 8` RD search |
lanes/dkey.report.mdadded+120 -0
| @@ -0,0 +1,120 @@ | ||
| 1 | +# lane-dkey — palette state never cleared by sub-8x8 / inter blocks | |
| 2 | + | |
| 3 | +## Root cause (closed, measured) | |
| 4 | + | |
| 5 | +`crates/ec-av1/src/decode.rs`: the mi-granular above/left **palette bands** | |
| 6 | +(`Neighbours::above_palette_size/colors`, `left_palette_*`) were written ONLY by | |
| 7 | +the >=8x8 intra readers. Every other block reader — the sub-8x8 leaves | |
| 8 | +(`decode_leaf_split4`, `decode_leaf_rect8`, `decode_intra_sub8_leaf`, | |
| 9 | +`decode_inter_sub8_split4`, `decode_inter_sub8_rect2`) and the inter block | |
| 10 | +readers (`decode_inter_block`, `decode_inter_block8`) — left the previous | |
| 11 | +block's palette size/colours standing in those cells. libaom's mi grid says | |
| 12 | +`palette_size == 0` there (`av1_allow_palette` needs BLOCK_8X8 and up; an inter | |
| 13 | +block has no palette at all), so the NEXT block's `av1_get_palette_mode_ctx` | |
| 14 | +and `av1_get_palette_cache` read 0 where ours read the stale neighbour. | |
| 15 | + | |
| 16 | +**Class: `cdf-row-held-constant`** (a context read off a table row libaom never | |
| 17 | +selects), with the same shape as `trial-map-not-restored`: state published by | |
| 18 | +one block path and never cleared by the paths that cannot produce it. | |
| 19 | + | |
| 20 | +## First differing symbol (measured, two instrumented decoders) | |
| 21 | + | |
| 22 | +Synthetic 1920x1024 libaom stream (`-cpu-used 6 -crf 35 -g 12`, dark | |
| 23 | +screen-content-detected source), KEY frame, block **mi(76,88)**, BLOCK_16X16, | |
| 24 | +DC_PRED: | |
| 25 | + | |
| 26 | +* both decoders identical through `skip/cdef/dq/mode/angle_y/uv_mode/angle_uv` | |
| 27 | + (`rng=41844`); | |
| 28 | +* aomdec `EC_PALSYN_AOM mi=76,88 bsize=6 mode=0 uv=0 ctx=0 y=0` — palette ctx 0, | |
| 29 | + no palette, then `EC_ISTEP name=use_filter_intra val=0 rng=57408`; | |
| 30 | +* ours `TRACE pre_palette_y_mode bsize_ctx=2 mode_ctx=1` → `palette_y_mode | |
| 31 | + value=1` → decoded a 2-colour palette and its whole colour-index map, and | |
| 32 | + then **skipped the `use_filter_intra` symbol** (a palette-Y block has none). | |
| 33 | +* the stale `mode_ctx=1` came from the 8x8 palette block at mi(72,88); the 4x4 | |
| 34 | + group at mi(74,88) between them published nothing. | |
| 35 | + | |
| 36 | +One symbol short → tile desync → whole-frame mismatch from that block on. | |
| 37 | + | |
| 38 | +## Fix | |
| 39 | + | |
| 40 | +Publish the block's own zeros from every reader that cannot code palette, at | |
| 41 | +the same footprint it publishes skip/lf state (7 sites, `record_palette_y_rect` | |
| 42 | +/ `record_palette_uv_rect` with `size = 0`). `decode_block_128rect` already did | |
| 43 | +exactly this ("the unconditional stamp clears any stale neighbour palette | |
| 44 | +state, as every other block path does") — the comment was true of the >=8x8 | |
| 45 | +paths only. | |
| 46 | + | |
| 47 | +## Sweep | |
| 48 | + | |
| 49 | +`fill_skip_grid*` (every leaf block publishes it) vs `record_palette_*` over | |
| 50 | +all 16 block readers: 9 readers had skip but no palette publication. Stamped | |
| 51 | +7; the 5 INTER-side stamps (`decode_inter_block`, `decode_inter_block8` x2, | |
| 52 | +`decode_inter_sub8_split4`, `decode_inter_sub8_rect2`, `decode_intra_sub8_leaf`) | |
| 53 | +REGRESSED 13 tests (our own encoder's screen streams stopped decoding: "a | |
| 54 | +reference frame selected with no picture at this frame's own ref_frame_idx | |
| 55 | +slot") and were reverted -- the inter readers' `(rmi, cmi)`/`write_w`/`write_h` | |
| 56 | +are not the same footprint convention the palette bands index, so those stamps | |
| 57 | +zeroed cells belonging to real palette blocks. Only the two KEY-frame sub-8x8 | |
| 58 | +stamps ship. **Remaining, deferred:** `decode_rect_split`, | |
| 59 | +`decode_leaf_rect`, `decode_block_rect4`, `decode_rect4_16_strip` and | |
| 60 | +`decode_intra_rect_in_inter` are >=8x8 intra readers that CAN code palette | |
| 61 | +(`decode_intra_rect_in_inter` reads `palette_y_mode` at decode.rs:10131) and | |
| 62 | +publish nothing — the mirror defect (a real palette block invisible to its | |
| 63 | +neighbours). Needs each reader's decoded palette threaded to the tail. | |
| 64 | + | |
| 65 | +## Verification | |
| 66 | + | |
| 67 | +* synthetic gate stream: ours vs `ffmpeg`'s own decode — **byte-exact** | |
| 68 | + (before the fix: all 12 frames differ from sample 491904, mi(76,88)). | |
| 69 | +* film B, libaom `-cpu-used 6 -crf 5 -g 48`, 48 frames: the KEY frame's | |
| 70 | + `EC_IMODE` trace is now **identical to aomdec for all 15774 blocks** — the | |
| 71 | + charter's "first divergent intra block mi(184,344)" premise is STALE, that | |
| 72 | + desync is gone. crf 5 still fails, now in an INTER frame ("a Golomb tail | |
| 73 | + longer than this decoder reads", last block read mi(124,412)); narrowed and | |
| 74 | + deferred below. | |
| 75 | + | |
| 76 | +## Files | |
| 77 | + | |
| 78 | +* `crates/ec-av1/src/decode.rs` — 2 zero-stamps, in `decode_leaf_split4` and | |
| 79 | + `decode_leaf_rect8` (the key-frame sub-8x8 groups). | |
| 80 | +* `crates/ec-av1/src/encode.rs` — `external_ladder` now decodes EVERY reference | |
| 81 | + ladder point through `decode_stream` and asserts sample-exactness against | |
| 82 | + ffmpeg, naming encoder/params/frame/plane/sample (the assertion that would | |
| 83 | + have caught both this and the `force_integer_mv` defect). | |
| 84 | +* `crates/ec-av1/src/motion_field.rs` — `EC_INTMV` trace print when | |
| 85 | + `force_integer_mv` rounding actually changes a projected candidate. | |
| 86 | + | |
| 87 | +## The new gate assertion's first verdict (12-frame native arm) | |
| 88 | + | |
| 89 | +`cargo test -p ec-av1 --release -- --ignored --exact encode::tests::bd_rate_screen_native` | |
| 90 | +now FAILS on its FIRST reference point, exactly as the assertion is meant to: | |
| 91 | + | |
| 92 | + libaom-av1 ["-cpu-used","6","-b:v","0","-crf","45"] frame 1 plane Y sample 66: | |
| 93 | + our decoder decoded 146, ffmpeg 145 | |
| 94 | + | |
| 95 | +Clip "bars 1080p" (`testsrc2` colour bars, 1920x1024 crop, 12 frames, gop 12), | |
| 96 | +libaom crf 45, INTER frame 1, a +-1 luma sample — a reconstruction (not entropy) | |
| 97 | +gap: a rounding/filter difference, not a desync. The gate stops there, so the | |
| 98 | +other reference points of that arm and the later clips are UNMEASURED. This is | |
| 99 | +the finding the charter asked for; the fix is a separate lane (defer). | |
| 100 | + | |
| 101 | +## OPEN / deferred | |
| 102 | + | |
| 103 | +1. **crf 5 inter-frame Golomb refusal** — key frame is exact now; the failure | |
| 104 | + is in an inter frame (`EC_MM` trace, last read mi(124,412)). Unblocks: diff | |
| 105 | + aomdec `EC_MODE`/`EC_STACK` against our `EC_MM` from the first inter frame. | |
| 106 | +2. **The inter-side half of the sweep** (5 readers, reverted above) and the | |
| 107 | + palette-publishing mirror gap (`decode_intra_rect_in_inter` reads | |
| 108 | + `palette_y_mode` and publishes nothing). Unblocks: read each inter reader's | |
| 109 | + real band-index/footprint convention before stamping (the failing tests are | |
| 110 | + `encoder::tests::the_content_gate_keeps_screen_streams_flat` and the 12 | |
| 111 | + others listed in the suite log). | |
| 112 | +3. **The bars-1080p libaom crf-45 +-1** above: first reference point of the | |
| 113 | + 12-frame native arm; reproduce with `external_ladder`'s own recipe. | |
| 114 | +4. **The sensitive `force_integer_mv` fixture**: a 1920x1024 source with a | |
| 115 | + textured patch moving 2.5 px per duplicate-pair (half-pel motion, exact | |
| 116 | + duplicate frames) makes libaom round 72 projected candidates | |
| 117 | + (`EC_TRACE_TPL` → `EC_INTMV`), and the stream STILL decodes byte-exact with | |
| 118 | + the fix stubbed (`if false &&`): the rounded candidates never change a coded | |
| 119 | + block's mv. Sensitivity needs the rounded candidate to win a | |
| 120 | + NEARESTMV/NEARMV slot. |