A 128-superblock witness for the clamped drl predictor
Browse files at this commitd93d5b516bedf4f72d7048fefe145f6ffeabe5c8Parent: d5a4212- test
- a 128-superblock witness for the clamped drl predictor
2 changed files +118 -13
- crates/ec-av1/src/encode.rs +72 -1
- crates/ec-av1/src/tile.rs +46 -12
crates/ec-av1/src/encode.rsmodified+72 -1
| @@ -13279,7 +13279,7 @@ mod tests { | ||
| 13279 | 13279 | eprintln!("SKIP probe_intrabc_key_frame: no OBS recording"); |
| 13280 | 13280 | return; |
| 13281 | 13281 | }; |
| 13282 | - let (width, height) = (640usize, 384usize); | |
| 13282 | + let (width, height) = (1280usize, 768usize); | |
| 13283 | 13283 | let picture = clip_frame(clip, "0", width, height); |
| 13284 | 13284 | eprintln!("| base_q_idx | intrabc | bytes | PSNR | blocks | searches/found/won |"); |
| 13285 | 13285 | for q in [5u8, 20, 35, 45] { |
| @@ -14938,6 +14938,77 @@ mod tests { | ||
| 14938 | 14938 | /// symbol (`EC_TRACE_MODE_STEP`) on the very stream that broke. This is |
| 14939 | 14939 | /// the instrument the inter `TxMode::Select` hunt lacked; on failure it |
| 14940 | 14940 | /// names the first differing frame, plane and position. |
| 14941 | + /// lane-sb128b: the writer used to price a `NEWMV` residual against | |
| 14942 | + /// `stack.pred_mv` whenever the SEARCH chose a `ref_mv_idx` the | |
| 14943 | + /// write-time stack could not carry, while the drl symbols signalled the | |
| 14944 | + /// clamped index -- so the decoder derived a DIFFERENT motion vector with | |
| 14945 | + /// the entropy stream still in sync, and the drift only surfaced frames | |
| 14946 | + /// later as a drl desync (on film B, as the encoder's own trial decode | |
| 14947 | + /// refusing a reference slot). | |
| 14948 | + /// | |
| 14949 | + /// The clamp needs a stack that shrinks between search and write, which | |
| 14950 | + /// is what a 128x128 superblock's visit order does; the counter is the | |
| 14951 | + /// point (class `gate-blind-to-feature`): a green round trip on a clip | |
| 14952 | + /// that never clamps proves nothing. | |
| 14953 | + /// | |
| 14954 | + /// `#[ignore]`: [`force_sb128`] moves a process-global. | |
| 14955 | + /// | |
| 14956 | + /// cargo test -p ec-av1 --release --lib -- --ignored \ | |
| 14957 | + /// a_128_superblock_clip_whose_drl_index_the_write_time_stack --nocapture | |
| 14958 | + #[test] | |
| 14959 | + #[ignore = "sets the process-global superblock size: run it alone"] | |
| 14960 | + fn a_128_superblock_clip_whose_drl_index_the_write_time_stack_cannot_carry_decodes_exact() { | |
| 14961 | + let _knobs = crate::speed::knob_write(); | |
| 14962 | + if !have_ffmpeg() { | |
| 14963 | + eprintln!("SKIP: no ffmpeg"); | |
| 14964 | + return; | |
| 14965 | + } | |
| 14966 | + force_sb128(true); | |
| 14967 | + let dir = std::env::temp_dir().join(format!("ec-av1-sb128b-{}", std::process::id())); | |
| 14968 | + std::fs::create_dir_all(&dir).unwrap(); | |
| 14969 | + let clip = dir.join("mandelbrot-1280x768.y4m"); | |
| 14970 | + let ok = Command::new("ffmpeg") | |
| 14971 | + .args(["-v", "error", "-y", "-f", "lavfi", "-i", "mandelbrot=size=1280x768:rate=24"]) | |
| 14972 | + .args(["-frames:v", "12", "-pix_fmt", "yuv420p"]) | |
| 14973 | + .arg(&clip) | |
| 14974 | + .status() | |
| 14975 | + .expect("ffmpeg failed to run") | |
| 14976 | + .success(); | |
| 14977 | + assert!(ok, "ffmpeg could not write the synthetic clip"); | |
| 14978 | + let path = clip.to_str().unwrap(); | |
| 14979 | + let (width, height) = (1280usize, 768usize); | |
| 14980 | + let pictures = crate::probe::source(path, "0", "null", width, height, 12); | |
| 14981 | + | |
| 14982 | + let _ = crate::tile::take_drl_clamp_hits(); | |
| 14983 | + let encoded = encode_sequence(&pictures, 150, 0.5).unwrap(); | |
| 14984 | + let clamps = crate::tile::take_drl_clamp_hits(); | |
| 14985 | + assert!( | |
| 14986 | + clamps > 0, | |
| 14987 | + "this clip never asked for a drl index the write-time stack could not carry, \ | |
| 14988 | + so it cannot witness the defect" | |
| 14989 | + ); | |
| 14990 | + eprintln!("clamped drl indices: {clamps}"); | |
| 14991 | + | |
| 14992 | + let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); | |
| 14993 | + assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); | |
| 14994 | + for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() { | |
| 14995 | + let recon = &frame.reconstruction; | |
| 14996 | + for (plane, got, want) in [ | |
| 14997 | + ("luma", &dec.y, &recon.y), | |
| 14998 | + ("U", &dec.u, &recon.u), | |
| 14999 | + ("V", &dec.v, &recon.v), | |
| 15000 | + ] { | |
| 15001 | + let differ = got.iter().zip(want).filter(|(a, b)| a != b).count(); | |
| 15002 | + assert_eq!( | |
| 15003 | + differ, 0, | |
| 15004 | + "frame {i}: {plane} -- {differ} of {} samples differ from the encoder's \ | |
| 15005 | + own reconstruction", | |
| 15006 | + got.len() | |
| 15007 | + ); | |
| 15008 | + } | |
| 15009 | + } | |
| 15010 | + } | |
| 15011 | + | |
| 14941 | 15012 | #[test] |
| 14942 | 15013 | fn every_frame_of_a_sequence_decodes_through_our_own_decoder() { |
| 14943 | 15014 | let _knobs = crate::speed::knob_read(); |
crates/ec-av1/src/tile.rsmodified+46 -12
| @@ -1042,10 +1042,13 @@ fn write_compound_block( | ||
| 1042 | 1042 | } |
| 1043 | 1043 | walk += 1; |
| 1044 | 1044 | } |
| 1045 | - stack | |
| 1046 | - .entries | |
| 1047 | - .get(walk) | |
| 1048 | - .map_or(stack.near_mv, |e| (e.mv0, e.mv1)) | |
| 1045 | + let near = |i: usize| { | |
| 1046 | + stack.entries.get(i).map_or(stack.near_mv, |e| (e.mv0, e.mv1)) | |
| 1047 | + }; | |
| 1048 | + if walk != idx && near(walk) != near(idx) { | |
| 1049 | + note_drl_clamp(idx, walk, stack.entries.len()); | |
| 1050 | + } | |
| 1051 | + near(walk) | |
| 1049 | 1052 | } |
| 1050 | 1053 | InterMode::NearestNewMv | InterMode::NewNearestMv => { |
| 1051 | 1054 | // Neither mode codes a DRL index (the decoder leaves `ref_mv_idx` |
| @@ -1074,10 +1077,13 @@ fn write_compound_block( | ||
| 1074 | 1077 | } |
| 1075 | 1078 | walk += 1; |
| 1076 | 1079 | } |
| 1077 | - let base = stack | |
| 1078 | - .entries | |
| 1079 | - .get(walk) | |
| 1080 | - .map_or(stack.nearest_mv, |e| (e.mv0, e.mv1)); | |
| 1080 | + let nearest = |i: usize| { | |
| 1081 | + stack.entries.get(i).map_or(stack.nearest_mv, |e| (e.mv0, e.mv1)) | |
| 1082 | + }; | |
| 1083 | + if walk != idx && nearest(walk) != nearest(idx) { | |
| 1084 | + note_drl_clamp(idx, walk, stack.entries.len()); | |
| 1085 | + } | |
| 1086 | + let base = nearest(walk); | |
| 1081 | 1087 | write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv, base.0)?; |
| 1082 | 1088 | write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv1, base.1)?; |
| 1083 | 1089 | (info.mv, info.mv1) |
| @@ -5937,6 +5943,26 @@ pub(crate) fn take_ref_hits() -> [usize; 7] { | ||
| 5937 | 5943 | } |
| 5938 | 5944 | |
| 5939 | 5945 | /// Bumps the two histograms for one coded inter block. |
| 5946 | +/// How many blocks this process wrote whose search-chosen `ref_mv_idx` the | |
| 5947 | +/// write-time stack could not carry, so the writer signalled a SMALLER index | |
| 5948 | +/// (lane-sb128b). Every one of these used to price its residual against a | |
| 5949 | +/// predictor the decoder never derives. | |
| 5950 | +static DRL_CLAMP_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); | |
| 5951 | + | |
| 5952 | +/// Counts one clamped DRL index whose predictor differs from the one the | |
| 5953 | +/// search asked for -- the harmful case, and the only one a witness can see. | |
| 5954 | +fn note_drl_clamp(target: usize, signalled: usize, entries: usize) { | |
| 5955 | + DRL_CLAMP_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | |
| 5956 | + if crate::envflags::env_flag!("EC_AV1_TRACE") { | |
| 5957 | + eprintln!("DRL_CLAMP target={target} signalled={signalled} entries={entries}"); | |
| 5958 | + } | |
| 5959 | +} | |
| 5960 | + | |
| 5961 | +/// Takes and clears [`DRL_CLAMP_HITS`], for a gate's own before/after delta. | |
| 5962 | +pub(crate) fn take_drl_clamp_hits() -> usize { | |
| 5963 | + DRL_CLAMP_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) | |
| 5964 | +} | |
| 5965 | + | |
| 5940 | 5966 | fn note_inter_mode(mode: usize, drl: usize) { |
| 5941 | 5967 | INTER_MODE_HITS[mode].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 5942 | 5968 | DRL_HITS[drl.min(3)].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| @@ -6016,8 +6042,12 @@ fn write_inter_mode( | ||
| 6016 | 6042 | match info.mode { |
| 6017 | 6043 | InterMode::NewMv => { |
| 6018 | 6044 | enc.symbol(0, &mut cdfs.new_mv[stack.new_mv_ctx]); |
| 6019 | - let idx = write_drl_idx(enc, cdfs, stack, 0, idx); | |
| 6020 | - let base = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv); | |
| 6045 | + let signalled = write_drl_idx(enc, cdfs, stack, 0, idx); | |
| 6046 | + let base = stack.entries.get(signalled).map_or(stack.pred_mv, |e| e.mv); | |
| 6047 | + if signalled != idx && base != stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv) { | |
| 6048 | + note_drl_clamp(idx, signalled, stack.entries.len()); | |
| 6049 | + } | |
| 6050 | + let idx = signalled; | |
| 6021 | 6051 | write_mv(enc, &mut cdfs.mv_comp, &mut cdfs.mv_joint, info.mv, base)?; |
| 6022 | 6052 | note_inter_mode(3, idx); |
| 6023 | 6053 | Ok((info.mv, true)) |
| @@ -6041,8 +6071,12 @@ fn write_inter_mode( | ||
| 6041 | 6071 | enc.symbol(1, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARMV |
| 6042 | 6072 | // `RefMvIdx` starts at 1 for NEARMV (spec 5.11.24). |
| 6043 | 6073 | let idx = idx.max(1); |
| 6044 | - let idx = write_drl_idx(enc, cdfs, stack, 1, idx); | |
| 6045 | - let mv = stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv); | |
| 6074 | + let signalled = write_drl_idx(enc, cdfs, stack, 1, idx); | |
| 6075 | + let mv = stack.entries.get(signalled).map_or(stack.near_mv, |e| e.mv); | |
| 6076 | + if signalled != idx && mv != stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv) { | |
| 6077 | + note_drl_clamp(idx, signalled, stack.entries.len()); | |
| 6078 | + } | |
| 6079 | + let idx = signalled; | |
| 6046 | 6080 | note_inter_mode(1, idx); |
| 6047 | 6081 | Ok((mv, false)) |
| 6048 | 6082 | } |