diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 2e908004..36b81f83 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -1551,6 +1551,7 @@ mod tests { /// exactly one non-empty packet per picture, `depth - 1` calls later. #[test] fn one_in_one_out_delayed_by_the_lookahead_depth() { + let _knobs = crate::speed::knob_read(); let config = EncoderConfig { width: 64, height: 64, @@ -1617,6 +1618,7 @@ mod tests { /// to catch. #[test] fn the_facade_codes_the_same_bytes_as_encode_sequence() { + let _knobs = crate::speed::knob_read(); let frames = 12usize; let film = h264_clip_frames(640, 384, frames).unwrap_or_else(|| { eprintln!("no h264 clip: the facade identity gate runs on a synthetic card"); @@ -1693,6 +1695,7 @@ mod tests { /// that only checked the screen half would pass with the pyramid deleted). #[test] fn the_content_gate_keeps_screen_streams_flat() { + let _knobs = crate::speed::knob_read(); let frames = 8usize; let pictures: Vec = (0..frames).map(|t| test_card(64, 64, t)).collect(); let config = EncoderConfig { @@ -1731,6 +1734,7 @@ mod tests { /// reads back out of the coded bytes. #[test] fn gop_cadence_is_honored() { + let _knobs = crate::speed::knob_read(); let config = EncoderConfig { width: 64, height: 64, @@ -1828,6 +1832,7 @@ mod tests { /// coded size confirm what the facade already reported. #[test] fn thirty_pictures_at_gop_fifteen_decode_to_two_key_frames() { + let _knobs = crate::speed::knob_read(); if !have_ffmpeg() { eprintln!("SKIP thirty_pictures_at_gop_fifteen_decode_to_two_key_frames: no ffmpeg"); return; @@ -1883,6 +1888,7 @@ mod tests { /// which is a visibly different string). #[test] fn bt709_limited_colour_is_reported_by_ffprobe() { + let _knobs = crate::speed::knob_read(); if !have_ffmpeg() { eprintln!("SKIP bt709_limited_colour_is_reported_by_ffprobe: no ffmpeg"); return; @@ -1907,6 +1913,7 @@ mod tests { /// as BT.709 for every input. #[test] fn bt601_limited_colour_is_reported_by_ffprobe() { + let _knobs = crate::speed::knob_read(); if !have_ffmpeg() { eprintln!("SKIP bt601_limited_colour_is_reported_by_ffprobe: no ffmpeg"); return; @@ -1933,6 +1940,7 @@ mod tests { /// is refused by name. #[test] fn geometry_mismatch_is_refused() { + let _knobs = crate::speed::knob_read(); let config = EncoderConfig { width: 64, height: 64, @@ -1950,6 +1958,7 @@ mod tests { /// `gop == 0` is refused at construction, not the first `encode()` call. #[test] fn zero_gop_is_refused() { + let _knobs = crate::speed::knob_read(); let config = EncoderConfig { width: 64, height: 64, @@ -2026,6 +2035,7 @@ mod tests { /// state and the controller's first couple of steps). #[test] fn bytes_per_frame_target_settles_within_20_percent() { + let _knobs = crate::speed::knob_read(); let Some(pictures) = h264_clip_frames(640, 384, 24) else { eprintln!("SKIP bytes_per_frame_target_settles_within_20_percent: no ffmpeg/fixture"); return; @@ -2065,6 +2075,7 @@ mod tests { /// its own level and one clamped proportional step. #[test] fn bitrate_target_lands_within_10_percent_over_48_frames() { + let _knobs = crate::speed::knob_read(); let (width, height, frames) = (640usize, 384usize, 48usize); let Some(pictures) = h264_clip_frames(width, height, frames) else { eprintln!("SKIP bitrate_target_lands_within_10_percent_over_48_frames: no ffmpeg/fixture"); @@ -2132,6 +2143,7 @@ mod tests { /// break it. #[test] fn bytes_per_frame_controller_never_oscillates_past_its_clamp() { + let _knobs = crate::speed::knob_read(); let Some(pictures) = h264_clip_frames(640, 384, 24) else { eprintln!( "SKIP bytes_per_frame_controller_never_oscillates_past_its_clamp: no ffmpeg/fixture" @@ -2235,6 +2247,7 @@ mod tests { /// display-order list and fails here. #[test] fn a_pyramid_stream_decodes_in_display_order_through_both_decoders() { + let _knobs = crate::speed::knob_read(); let (width, height) = (128usize, 128usize); let config = EncoderConfig { width, @@ -2350,6 +2363,7 @@ mod tests { /// nothing about whether a single 64x64 block was ever written. #[test] fn a_static_clip_codes_64x64_roots_and_decodes_sample_exact() { + let _knobs = crate::speed::knob_read(); let _gate_lock = crate::stream::tests::lock_gate_counters(); let (width, height) = (256usize, 128usize); let still = test_card(width, height, 0); @@ -2401,6 +2415,7 @@ mod tests { #[test] fn every_tile_layout_decodes_sample_exact_through_both_decoders() { + let _knobs = crate::speed::knob_read(); let (width, height) = (640usize, 384usize); let sources: Vec = (0..4).map(|t| test_card(width, height, t * 3)).collect(); let mut sizes = Vec::new(); @@ -2493,6 +2508,7 @@ mod tests { #[test] #[ignore = "sets the process-global speed preset: run it alone"] fn every_speed_preset_decodes_sample_exact_through_both_decoders() { + let _knobs = crate::speed::knob_write(); let _gate_lock = crate::stream::tests::lock_gate_counters(); let (width, height) = (640usize, 384usize); let sources: Vec = (0..4).map(|t| test_card(width, height, t * 3)).collect(); @@ -2556,6 +2572,7 @@ mod tests { #[test] #[ignore = "1080p wall table: minutes, run it with --ignored"] fn tile_wall_table_at_1080p() { + let _knobs = crate::speed::knob_write(); let _gate_lock = crate::stream::tests::lock_gate_counters(); let (width, height) = (1920usize, 1080usize); let Some(sources) = h264_clip_frames(width, height, 8) else { @@ -2606,6 +2623,7 @@ mod tests { /// thread and at four, per layout. #[test] fn tile_bytes_do_not_depend_on_the_thread_count() { + let _knobs = crate::speed::knob_write(); let _gate_lock = crate::stream::tests::lock_gate_counters(); let (width, height) = (320usize, 192usize); let sources: Vec = (0..3).map(|t| test_card(width, height, t * 3)).collect(); @@ -2655,6 +2673,7 @@ mod tests { #[test] #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] fn tile_search_wall_1080p() { + let _knobs = crate::speed::knob_write(); tile_search_wall(1920, 1080, 12, &[(0, 0), (1, 0), (1, 1), (2, 1)], &[1, 2, 4, 8]); } @@ -2663,6 +2682,7 @@ mod tests { #[test] #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] fn tile_search_wall_4k() { + let _knobs = crate::speed::knob_write(); tile_search_wall(3840, 1608, 6, &[(2, 1), (3, 2)], &[1, 8, 12]); } @@ -2674,6 +2694,7 @@ mod tests { /// rows tall, which is what makes the bands non-trivial. #[test] fn filter_stage_bytes_do_not_depend_on_the_thread_count() { + let _knobs = crate::speed::knob_write(); let _gate_lock = crate::stream::tests::lock_gate_counters(); let (width, height) = (384usize, 288usize); let sources: Vec = (0..4).map(|t| test_card(width, height, t * 3)).collect(); @@ -2711,6 +2732,7 @@ mod tests { /// which is the arm this lane opened. #[test] fn filter_replay_codes_the_same_stream_as_a_full_decode() { + let _knobs = crate::speed::knob_read(); let _gate_lock = crate::stream::tests::lock_gate_counters(); let run = |width: usize, height: usize, off: bool| { let sources: Vec = (0..4).map(|t| test_card(width, height, t * 3)).collect(); @@ -2756,6 +2778,7 @@ mod tests { /// the loop-restoration search reads are bit-identical. #[test] fn filter_replay_final_matches_the_capture_decode() { + let _knobs = crate::speed::knob_read(); let _gate_lock = crate::stream::tests::lock_gate_counters(); let run = |sources: &[Picture], width: usize, height: usize, tiles: (u32, u32)| { let config = EncoderConfig { @@ -2799,12 +2822,14 @@ mod tests { #[test] #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] fn filter_stage_wall_1080p() { + let _knobs = crate::speed::knob_write(); filter_stage_wall(1920, 1080, 8, (2, 1), &[8]); } #[test] #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] fn filter_stage_wall_4k() { + let _knobs = crate::speed::knob_write(); filter_stage_wall(3840, 1608, 4, (2, 1), &[8, 12]); } @@ -2815,6 +2840,7 @@ mod tests { #[test] #[ignore = "wall measurement: minutes, run it with --ignored --nocapture"] fn filter_stage_wall_film() { + let _knobs = crate::speed::knob_write(); filter_stage_wall(1920, 768, 4, (2, 1), &[8]); } @@ -3039,6 +3065,7 @@ mod tests { #[test] #[ignore = "1080p encode: minutes, run it with --ignored"] fn a_1080p_multi_tile_stream_decodes_sample_exact_through_both_decoders() { + let _knobs = crate::speed::knob_read(); let (width, height) = (1920usize, 1080usize); let Some(sources) = h264_clip_frames(width, height, 3) else { eprintln!("SKIP the 1080p tile round trip: no fixture"); @@ -3087,6 +3114,7 @@ mod tests { /// the same real clip. #[test] fn quality_target_is_monotone_in_bytes_and_psnr() { + let _knobs = crate::speed::knob_read(); if !have_ffmpeg() { eprintln!("SKIP quality_target_is_monotone_in_bytes_and_psnr: no ffmpeg"); return; diff --git a/crates/ec-av1/src/speed.rs b/crates/ec-av1/src/speed.rs index 001211d6..8040c7a2 100644 --- a/crates/ec-av1/src/speed.rs +++ b/crates/ec-av1/src/speed.rs @@ -504,3 +504,35 @@ mod tests { assert!(!levers(MAX_SPEED).is_empty()); } } + +// --------------------------------------------------------------------------- +// Test-only serialisation of the process-global encoder knobs (lane-gaterace) +// --------------------------------------------------------------------------- + +/// [`SPEED`] and [`crate::par::TILE_THREADS`] are PROCESS-global, so a test +/// that stores one of them changes what every test encoding concurrently on +/// another test thread codes: with the default `--test-threads`, +/// `every_speed_preset_decodes_sample_exact_through_both_decoders` (presets +/// 0..=10) and the `tile_*`/`filter_stage_*` wall tables poisoned five of the +/// ignored `encoder::` gates, each of which passes standalone. The rule is +/// reader/writer, not a plain mutex: a test that SETS a knob holds +/// [`knob_write`] for its whole body, and every test that ENCODES (i.e. reads +/// the knobs, over more than one call) holds [`knob_read`], so a setter never +/// overlaps an encode. Poison is ignored deliberately -- one failing gate +/// must not turn every later one into a lock panic. +#[cfg(test)] +static KNOBS: std::sync::RwLock<()> = std::sync::RwLock::new(()); + +/// Held by a test that reads the process-global knobs; see [`KNOBS`]. +#[cfg(test)] +#[must_use] +pub(crate) fn knob_read() -> std::sync::RwLockReadGuard<'static, ()> { + KNOBS.read().unwrap_or_else(std::sync::PoisonError::into_inner) +} + +/// Held by a test that SETS a process-global knob; see [`KNOBS`]. +#[cfg(test)] +#[must_use] +pub(crate) fn knob_write() -> std::sync::RwLockWriteGuard<'static, ()> { + KNOBS.write().unwrap_or_else(std::sync::PoisonError::into_inner) +}