The LAST2 witness -- alternating textures, 36 blocks off LAST2, exact through both decoders
Browse files at this commitddabc818754375abeef648ede414fd8f36482366Parent: ec4f10e- test
- the LAST2 witness -- alternating textures, 36 blocks off LAST2, exact through both decoders
1 changed file +107 -0
- crates/ec-av1/src/encoder.rs +107 -0
crates/ec-av1/src/encoder.rsmodified+107 -0
| @@ -974,12 +974,34 @@ const LEAF_SLOTS: [u8; 2] = [LEAF_SLOT, 7]; | |||
| 974 | /// that priced the lever is `lanes/refs.report.md` §1; what it buys on the | 974 | /// that priced the lever is `lanes/refs.report.md` §1; what it buys on the |
| 975 | /// gate is `lanes/last2.report.md`. | 975 | /// gate is `lanes/last2.report.md`. |
| 976 | fn last2() -> bool { | 976 | fn last2() -> bool { |
| 977 | + match LAST2_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) { | ||
| 978 | + 0 => return false, | ||
| 979 | + 1 => return true, | ||
| 980 | + _ => {} | ||
| 981 | + } | ||
| 977 | match std::env::var("EC_AV1_LAST2").ok() { | 982 | match std::env::var("EC_AV1_LAST2").ok() { |
| 978 | Some(v) => v != "0", | 983 | Some(v) => v != "0", |
| 979 | None => LAST2, | 984 | None => LAST2, |
| 980 | } | 985 | } |
| 981 | } | 986 | } |
| 982 | 987 | ||
| 988 | +/// A PROCESS-GLOBAL [`last2`] override that wins over the environment | ||
| 989 | +/// (`u8::MAX` = unset), the shape `crate::encode::set_frame_interp` has -- | ||
| 990 | +/// this crate's tests never call `set_var` (`crate::envflags`), so the | ||
| 991 | +/// witness turns the lever on through this while holding | ||
| 992 | +/// [`crate::speed::knob_write`]. | ||
| 993 | +static LAST2_OVERRIDE: std::sync::atomic::AtomicU8 = | ||
| 994 | + std::sync::atomic::AtomicU8::new(u8::MAX); | ||
| 995 | + | ||
| 996 | +/// Sets [`LAST2_OVERRIDE`]; `None` clears it. | ||
| 997 | +#[allow(dead_code)] // set only from the `#[cfg(test)]` witness | ||
| 998 | +pub(crate) fn set_last2(on: Option<bool>) { | ||
| 999 | + LAST2_OVERRIDE.store( | ||
| 1000 | + on.map_or(u8::MAX, u8::from), | ||
| 1001 | + std::sync::atomic::Ordering::Relaxed, | ||
| 1002 | + ); | ||
| 1003 | +} | ||
| 1004 | + | ||
| 983 | /// [`last2`]'s default. | 1005 | /// [`last2`]'s default. |
| 984 | const LAST2: bool = false; | 1006 | const LAST2: bool = false; |
| 985 | 1007 | ||
| @@ -3001,6 +3023,91 @@ mod tests { | |||
| 3001 | assert!(finer > base, "key_q_offset -48 did not reach the key frame ({finer} <= {base})"); | 3023 | assert!(finer > base, "key_q_offset -48 did not reach the key frame ({finer} <= {base})"); |
| 3002 | } | 3024 | } |
| 3003 | 3025 | ||
| 3026 | + /// lane-last2's witness: a clip whose pictures ALTERNATE between two | ||
| 3027 | + /// textures, so for every leaf the picture two back is an exact match and | ||
| 3028 | + /// the one before it (`LAST_FRAME`) is not. With the lever on, the leaf | ||
| 3029 | + /// chain alternates its refresh over [`LEAF_SLOTS`] and names the older | ||
| 3030 | + /// slot `LAST2_FRAME`; blocks must actually be CODED off it | ||
| 3031 | + /// ([[gate-blind-to-feature]]: a slot that is retained but never chosen | ||
| 3032 | + /// proves only that the bookkeeping compiles), the stream must decode | ||
| 3033 | + /// sample-exact through OUR decoder and through ffmpeg, and the hidden | ||
| 3034 | + /// frames must still come out in display order. With the lever OFF the | ||
| 3035 | + /// same clip codes ZERO `LAST2` blocks, which is what makes the counter a | ||
| 3036 | + /// measurement of this lane and not of the reference set we already had. | ||
| 3037 | + #[test] | ||
| 3038 | + fn a_leaf_predicts_off_last2_when_the_picture_two_back_matches() { | ||
| 3039 | + let _knobs = crate::speed::knob_write(); | ||
| 3040 | + crate::encode::force_screen(Some(false)); | ||
| 3041 | + let (width, height) = (128usize, 128usize); | ||
| 3042 | + let sources: Vec<Picture> = | ||
| 3043 | + (0..9).map(|t| test_card(width, height, (t % 2) * 24)).collect(); | ||
| 3044 | + let run = |on: bool| -> ([usize; 8], Vec<u8>, Vec<Packet>) { | ||
| 3045 | + set_last2(Some(on)); | ||
| 3046 | + let config = EncoderConfig { | ||
| 3047 | + width, | ||
| 3048 | + height, | ||
| 3049 | + base_q_idx: 120, | ||
| 3050 | + gop: 32, | ||
| 3051 | + colour: Colour::Bt709Limited, | ||
| 3052 | + tile_cols_log2: 0, | ||
| 3053 | + tile_rows_log2: 0, | ||
| 3054 | + }; | ||
| 3055 | + let pyramid = Pyramid { mini_gop: 4, mid_q_offset: None, ..Pyramid::default() }; | ||
| 3056 | + let mut enc = Av1Encoder::with_pyramid(config, pyramid).unwrap(); | ||
| 3057 | + let _ = crate::encode::take_ref_frame_hits(); | ||
| 3058 | + let mut packets = Vec::new(); | ||
| 3059 | + for picture in &sources { | ||
| 3060 | + packets.extend(enc.encode_frames(picture).unwrap()); | ||
| 3061 | + } | ||
| 3062 | + packets.extend(enc.flush().unwrap()); | ||
| 3063 | + let hits = crate::encode::take_ref_frame_hits(); | ||
| 3064 | + (hits, concat(&packets), packets) | ||
| 3065 | + }; | ||
| 3066 | + let (off_hits, off_stream, _) = run(false); | ||
| 3067 | + let (on_hits, stream, packets) = run(true); | ||
| 3068 | + set_last2(None); | ||
| 3069 | + eprintln!( | ||
| 3070 | + "LAST2 off: LAST {} LAST2 {} GOLDEN {} ALTREF {} ({} B); \ | ||
| 3071 | + on: LAST {} LAST2 {} GOLDEN {} ALTREF {} ({} B)", | ||
| 3072 | + off_hits[1], off_hits[2], off_hits[4], off_hits[7], off_stream.len(), | ||
| 3073 | + on_hits[1], on_hits[2], on_hits[4], on_hits[7], stream.len(), | ||
| 3074 | + ); | ||
| 3075 | + assert_eq!(off_hits[2], 0, "LAST2 was coded with the lever OFF"); | ||
| 3076 | + assert!( | ||
| 3077 | + on_hits[2] >= 8, | ||
| 3078 | + "the picture two back matches exactly and only {} blocks were coded off LAST2", | ||
| 3079 | + on_hits[2], | ||
| 3080 | + ); | ||
| 3081 | + | ||
| 3082 | + // Display order and sample-exactness, both decoders (the same shape | ||
| 3083 | + // `pyramid_round_trip` gates the hidden frames with). | ||
| 3084 | + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); | ||
| 3085 | + assert_eq!(ours.len(), sources.len(), "our decoder's display-order count"); | ||
| 3086 | + if !have_ffmpeg() { | ||
| 3087 | + eprintln!("SKIP the ffmpeg half of the LAST2 witness: no ffmpeg"); | ||
| 3088 | + return; | ||
| 3089 | + } | ||
| 3090 | + let theirs = ffmpeg_decode_luma(&stream, width, height); | ||
| 3091 | + assert_eq!(theirs.len(), sources.len(), "ffmpeg's display-order count"); | ||
| 3092 | + for (i, (a, b)) in ours.iter().zip(&theirs).enumerate() { | ||
| 3093 | + let got: Vec<u8> = a.y.iter().map(|&v| v as u8).collect(); | ||
| 3094 | + assert_eq!(got.len(), b.len(), "frame {i}: luma size"); | ||
| 3095 | + if let Some(at) = got.iter().zip(b).position(|(x, y)| x != y) { | ||
| 3096 | + panic!( | ||
| 3097 | + "display frame {i}: luma differs first at ({}, {}): ours {} vs ffmpeg {}", | ||
| 3098 | + at % width, | ||
| 3099 | + at / width, | ||
| 3100 | + got[at], | ||
| 3101 | + b[at], | ||
| 3102 | + ); | ||
| 3103 | + } | ||
| 3104 | + } | ||
| 3105 | + // The reordering itself is unchanged by the second leaf slot. | ||
| 3106 | + let shown = packets.iter().filter(|p| p.level == Level::ShowExisting).count(); | ||
| 3107 | + let arfs = packets.iter().filter(|p| p.level == Level::Arf).count(); | ||
| 3108 | + assert_eq!(shown, arfs, "one show_existing_frame per hidden frame"); | ||
| 3109 | + } | ||
| 3110 | + | ||
| 3004 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { | 3111 | fn pyramid_round_trip(pyramid: Pyramid, hidden: usize) { |
| 3005 | let (width, height) = (128usize, 128usize); | 3112 | let (width, height) = (128usize, 128usize); |
| 3006 | let config = EncoderConfig { | 3113 | let config = EncoderConfig { |