forge
Explore
Log in

Don't you dare go Hollow

Standing real-content regression gate on encode.rs

Browse files at this commit

7b73e56dab1d81e3fa1902dbc384fd94441954baAhmet Kaan GümüşParent: dda53a8
  • test
    • standing real-content regression gate on encode.rs
Show full message
test(av1): standing real-content regression gate on encode.rs

Two recent regressions (a padded-size inter FrameHeader dav1d rejected;
a 1280x720 export refusal) were only caught by a downstream repo's
gate. real_clip_encodes_within_its_quality_and_size_budget encodes 12
frames of a real 1080p H.264 fixture clip, scaled to 640x384, and
asserts dav1d decodes every frame, mean luma PSNR stays within 0.5 dB
of the measured 45.41 dB, and the stream stays within 15% of the
measured 69861 bytes.
crates/ec-av1/src/encode.rsmodified+108 -0
@@ -4109,6 +4109,114 @@ mod tests {
4109 set_test_top_k_override_inter(None);4109 set_test_top_k_override_inter(None);
4110 }4110 }
4111 4111
4112+ /// `frames` consecutive pictures out of a real clip, scaled to a whole
4113+ /// number of 32x32 blocks. Generalizes [`clip_frame`]/[`clip_frame_pair`]
4114+ /// to an arbitrary count for the fixture regression gate below.
4115+ fn clip_frames(
4116+ clip: &str,
4117+ skip: &str,
4118+ width: usize,
4119+ height: usize,
4120+ frames: usize,
4121+ ) -> Vec<Picture> {
4122+ let out = Command::new("ffmpeg")
4123+ .args(["-v", "error", "-ss", skip, "-i", clip])
4124+ .args(["-frames:v", &frames.to_string()])
4125+ .args(["-vf", &format!("scale={width}:{height}")])
4126+ .args(["-f", "rawvideo", "-pix_fmt", "yuv420p", "-"])
4127+ .output()
4128+ .expect("ffmpeg failed to run");
4129+ assert!(
4130+ out.status.success(),
4131+ "ffmpeg: {}",
4132+ String::from_utf8_lossy(&out.stderr)
4133+ );
4134+ let (luma, chroma) = (width * height, width * height / 4);
4135+ let frame_len = luma + 2 * chroma;
4136+ assert_eq!(
4137+ out.stdout.len(),
4138+ frame_len * frames,
4139+ "expected {frames} 4:2:0 frames"
4140+ );
4141+ (0..frames)
4142+ .map(|i| {
4143+ let bytes = &out.stdout[i * frame_len..][..frame_len];
4144+ Picture {
4145+ width,
4146+ height,
4147+ y: bytes[..luma].to_vec(),
4148+ u: bytes[luma..luma + chroma].to_vec(),
4149+ v: bytes[luma + chroma..].to_vec(),
4150+ }
4151+ })
4152+ .collect()
4153+ }
4154+
4155+ /// The standing regression gate on real content: 12 frames of a real
4156+ /// clip (`fixtures/video/h264-1080p-23.976-8bit.mp4`, decoded down to
4157+ /// this crate's own input regardless of its own source codec), encoded
4158+ /// as an AV1 GOP and checked against three numeric floors so a
4159+ /// compatibility or quality regression fails the suite instead of
4160+ /// waiting for a downstream repo's gate to catch it (as happened for a
4161+ /// padded-size inter `FrameHeader` dav1d rejected, and a 1280x720 export
4162+ /// refusal, both caught only that way).
4163+ ///
4164+ /// 640x384 is a whole number of 32x32 blocks (no straddle edge case;
4165+ /// that geometry is already covered elsewhere), chosen only so encode
4166+ /// time and gate numbers are not muddied by an edge-block class this
4167+ /// test is not about.
4168+ ///
4169+ /// Measured 2026-08-27 at q=100: stream 69861 bytes, average luma PSNR
4170+ /// 45.41 dB across the 12 decoded frames vs the source. Floor/ceiling
4171+ /// below carry a margin off those measured numbers (PSNR floor =
4172+ /// measured - 0.5 dB; byte ceiling = measured * 1.15).
4173+ #[test]
4174+ fn real_clip_encodes_within_its_quality_and_size_budget() {
4175+ if !have_ffmpeg() {
4176+ eprintln!("SKIP real_clip_encodes_within_its_quality_and_size_budget: no ffmpeg");
4177+ return;
4178+ }
4179+ let clip = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
4180+ .join("../../fixtures/video/h264-1080p-23.976-8bit.mp4");
4181+ if !clip.exists() {
4182+ eprintln!(
4183+ "SKIP real_clip_encodes_within_its_quality_and_size_budget: {} missing",
4184+ clip.display()
4185+ );
4186+ return;
4187+ }
4188+ let (width, height, frame_count) = (640usize, 384usize, 12usize);
4189+ let source = clip_frames(clip.to_str().unwrap(), "0", width, height, frame_count);
4190+ let encoded = encode_sequence(&source, 100, 0.5).unwrap();
4191+ assert_eq!(encoded.frames.len(), frame_count);
4192+
4193+ // Gate 1: a real AV1 decoder (dav1d, via ffmpeg) accepts the whole
4194+ // stream and hands back every frame -- a stream a downstream
4195+ // decoder refuses is the exact class of regression this gate
4196+ // exists for.
4197+ let decoded = ffmpeg_decode_sequence(&encoded.stream, width, height, frame_count);
4198+ assert_eq!(decoded.len(), frame_count, "dav1d decoded every frame");
4199+
4200+ // Gate 2: quality vs the source did not silently collapse.
4201+ let mean_psnr: f64 = decoded
4202+ .iter()
4203+ .zip(&source)
4204+ .map(|(d, s)| psnr(&d.y, &s.y))
4205+ .sum::<f64>()
4206+ / frame_count as f64;
4207+ assert!(
4208+ mean_psnr >= 44.91,
4209+ "mean luma PSNR {mean_psnr:.2} dB fell below the 44.91 dB floor"
4210+ );
4211+
4212+ // Gate 3: the stream did not silently bloat.
4213+ assert!(
4214+ encoded.stream.len() <= 80_340,
4215+ "stream grew to {} bytes, over the 80340-byte ceiling",
4216+ encoded.stream.len()
4217+ );
4218+ }
4219+
4112 /// The replica-shaped path: 24 frames, 1280x720, one key frame followed4220 /// The replica-shaped path: 24 frames, 1280x720, one key frame followed
4113 /// by inter frames -- the same GOP shape [`encode_sequence`] gives the4221 /// by inter frames -- the same GOP shape [`encode_sequence`] gives the
4114 /// facade -- timed end to end, so the edith export-time projection is a4222 /// facade -- timed end to end, so the edith export-time projection is a