//! Encoders through the system's FFmpeg (loaded at runtime), decoded with //! this crate's own decoders: H.264 with Cisco's OpenH264 //! (`VOELIN_OPENH264_LIB`), AV1 with dav1d (`--features av1`). //! //! Skipped without FFmpeg. Hardware backends need their GPU and driver; on a //! machine without them the self-test reports why and they are skipped. #![cfg(feature = "ffmpeg")] use std::time::Duration; use voelin_media::capture::synthetic::SyntheticScreen; use voelin_media::ffmpeg::{BACKENDS, Ffmpeg, FfmpegEncoder, probe}; use voelin_media::{ Codec, Codecs, EncoderBackend, EncoderConfig, VideoDecoder, VideoEncoder, convert, }; const W: u32 = 320; const H: u32 = 240; fn ffmpeg() -> Option<&'static Ffmpeg> { match Ffmpeg::get() { Ok(f) => Some(f), Err(e) => { eprintln!("FFmpeg not available ({e}), skipped"); None } } } fn decoder(codec: Codec) -> Option> { match codec { #[cfg(feature = "openh264")] Codec::H264 => { let path = std::env::var_os("VOELIN_OPENH264_LIB")?; let library = voelin_media::codec::h264::OpenH264::load(path).ok()?; Some(Box::new(library.decoder().unwrap())) } #[cfg(feature = "av1")] Codec::Av1 => Some(Box::new(voelin_media::codec::av1::Dav1dDecoder::new().unwrap())), _ => None, } } /// Twelve frames of the moving test pattern at `w` x `h`, a keyframe forced /// at 8, a bitrate change at 5: keyframes where asked, timestamps kept, and /// (with a decoder) every picture decodable at its own size, PSNR > 28 dB. fn roundtrip(name: &'static str, w: u32, h: u32) { let screen = SyntheticScreen::new(w, h); let config = EncoderConfig { fps: 30, bitrate_bps: 1_500_000, ..EncoderConfig::default() }; let mut encoder = FfmpegEncoder::new(name, config).unwrap(); assert_eq!(encoder.backend(), EncoderBackend::Ffmpeg(name)); let (aw, ah) = probe().iter().find(|s| s.spec.name == name).unwrap().alignment; let codec = encoder.codec(); let mut decoder = decoder(codec); let mut keyframes = Vec::new(); let mut sent = Vec::new(); let mut decoded = 0; let mut worst = f64::INFINITY; let mut sources = Vec::new(); // Frames in until the first packet came out (the encoder's delay). let mut delay = None; // Until frame 11 came out (encoders with a delay need more input). for n in 0..60u32 { if sources.len() > 11 && sent.len() >= 12 { break; } let frame = screen.frame(u64::from(n), 30); if n == 5 { encoder.set_bitrate(800_000).unwrap(); } let out = encoder.encode(&frame, n == 8).unwrap(); sources.push(frame); if delay.is_none() && !out.is_empty() { delay = Some(n); } for f in &out { sent.push(f.pts_90khz); // Encoders with a delay hand out older frames: match by time. let index = sources.iter().position(|s| s.pts_90khz() == f.pts_90khz).unwrap(); if f.keyframe { keyframes.push(index); } if let Some(decoder) = &mut decoder && let Some(picture) = decoder.decode(&f.data).unwrap() { // The top-left part the encoder's alignment allows, never // padding (which the viewer would see). let (pw, ph) = (picture.width, picture.height); assert!( pw <= w && ph <= h && w - pw < aw && h - ph < ah, "{name}: {w}x{h} decoded as {pw}x{ph} (alignment {aw}x{ah})" ); let mut source = sources[index].view(); (source.width, source.height) = (pw, ph); let psnr = convert::psnr(&source.to_frame(), &picture).unwrap(); assert!(psnr > 28.0, "{name} {w}x{h} frame {index}: PSNR {psnr:.1} dB"); worst = worst.min(psnr); decoded += 1; } } } eprintln!( "{name} {w}x{h} (alignment {aw}x{ah}): {} packets from {} frames (first after \ {delay:?}), keyframes at {keyframes:?}, decoded {decoded}, lowest PSNR {worst:.1} dB", sent.len(), sources.len() ); assert!(sent.len() >= 8, "{name}: only {} packets", sent.len()); assert_eq!(keyframes.first(), Some(&0), "{name}"); assert!(keyframes.contains(&8), "{name}: forced keyframe missing: {keyframes:?}"); assert!(sent.windows(2).all(|w| w[0] < w[1]), "{name}: timestamps {sent:?}"); assert_eq!(sent[0], 0); if decoder.is_some() { assert!(decoded >= 8, "{name}: decoded {decoded}"); } } fn available(name: &str) -> bool { probe().iter().any(|s| s.spec.name == name && s.available.is_ok()) } #[test] fn probe_reports_every_backend() { let Some(ffmpeg) = ffmpeg() else { return }; eprintln!("{:?}", ffmpeg.info()); let statuses = probe(); assert_eq!(statuses.len(), BACKENDS.len()); for status in statuses { eprintln!("{:>18}: {:?}", status.spec.name, status.available); if let Err(reason) = &status.available { assert!(!reason.is_empty()); } } // The report lists them with the built-in ones. let report = Codecs::new().report(); assert!(report.ffmpeg.is_ok(), "{report:?}"); assert!(report.encoders.iter().any(|e| e.name == "libvpx")); for status in statuses { let info = report.encoders.iter().find(|e| e.name == status.spec.name).unwrap(); assert_eq!(info.status, status.available); let automatic = status.available.is_ok() && status.spec.is_automatic(); assert_eq!(info.rank.is_some(), automatic, "{}", status.spec.name); } // On Linux, AMF's runtime is not even loaded unless asked for: the // probe crashed in it (SIGSEGV in libamfrt64), and VA-API drives the // same encoder there. Nothing in this process may have loaded it. if cfg!(target_os = "linux") && std::env::var_os("VOELIN_FFMPEG_AMF").is_none() { for status in statuses.iter().filter(|s| s.spec.api == "AMF") { let reason = status.available.as_ref().unwrap_err(); assert!(reason.contains("VOELIN_FFMPEG_AMF=1"), "{}: {reason}", status.spec.name); } let maps = std::fs::read_to_string("/proc/self/maps").unwrap(); assert!(!maps.contains("libamfrt"), "AMF's runtime was loaded"); } } #[test] fn software_encoders_roundtrip() { if ffmpeg().is_none() { return; } let mut tested = Vec::new(); for name in ["libx264", "libopenh264", "libsvtav1", "librav1e", "libaom-av1"] { if available(name) { roundtrip(name, W, H); tested.push(name); } else { eprintln!("{name} skipped: not available"); } } eprintln!("tested: {tested:?}"); } /// What the GPU encoders produce, through the same round-trip as the /// software ones: our own decoders have to decode it (OpenH264 for H.264 with /// `VOELIN_OPENH264_LIB`, dav1d for AV1 with `--features av1`), keyframes /// where asked, timestamps kept, PSNR above 28 dB. /// /// Also at 1920x1080, which is not a whole number of 16-pixel macroblocks: /// the H.264 SPS has to crop the 1088 coded rows, and AV1 has no cropping at /// all, so a GPU that only encodes aligned sizes shows as a wrong decoded /// size. /// /// Whatever hardware the machine has; skipped where none of it works. HEVC is /// left out, having no decoder here. #[test] fn hardware_encoders_roundtrip() { if ffmpeg().is_none() { return; } let names: Vec<&'static str> = BACKENDS .iter() .filter(|b| b.is_hardware() && matches!(b.codec, Codec::H264 | Codec::Av1)) .map(|b| b.name) .filter(|name| available(name)) .collect(); if names.is_empty() { eprintln!("no usable hardware encoder, skipped"); return; } for name in &names { roundtrip(name, W, H); roundtrip(name, 1920, 1080); } eprintln!("tested: {names:?}"); } /// AMF's runtime crashed (SIGSEGV in `AMFFactoryHelper::Init` / /// `Terminate` in `libamfrt64`) when encoders were opened on several /// threads at once, as the probe and simulcast layers do: 10 of 420 probes /// with several processes probing together. Opens and closes are /// serialised now; this opens every usable AMF encoder on parallel threads /// again and again. On Linux it needs `VOELIN_FFMPEG_AMF=1`; ignored by /// default because a regression takes the whole test binary down. #[test] #[ignore = "needs AMF; a regression crashes the process"] fn amf_encoders_open_in_parallel() { if ffmpeg().is_none() { return; } let names: Vec<&str> = ["h264_amf", "hevc_amf", "av1_amf"].into_iter().filter(|n| available(n)).collect(); if names.is_empty() { eprintln!("no usable AMF encoder, skipped"); return; } let frame = voelin_media::VideoFrame::black_i420(W, H); for _ in 0..40 { std::thread::scope(|scope| { for &name in &names { let frame = &frame; scope.spawn(move || { let mut encoder = FfmpegEncoder::new(name, EncoderConfig::default()).unwrap(); encoder.encode(frame, true).unwrap(); }); } }); } eprintln!("opened {names:?} together 40 times"); } #[test] fn x264_is_constrained_high_and_changes_size() { if ffmpeg().is_none() || !available("libx264") { return; } let config = EncoderConfig { fps: 30, bitrate_bps: 1_000_000, ..EncoderConfig::default() }; let mut encoder = FfmpegEncoder::new("libx264", config).unwrap(); let screen = SyntheticScreen::new(W, H); let first = encoder.encode(&screen.frame(0, 30), false).unwrap(); // profile_idc 100 (High) without B-frames: what TeamSpeak decodes. let sps = first[0].data.windows(5).find(|w| w[..4] == [0, 0, 1, 0x67] || w[..4] == [0, 0, 0, 1]); assert!(sps.is_some()); let profile = h264_profile(&first[0].data); assert_eq!(profile, Some(100), "High profile"); // A new size: a new session, starting with a keyframe. let small = SyntheticScreen::new(160, 120); let out = encoder.encode(&small.frame(1, 30), false); assert!(out.unwrap().iter().any(|f| f.keyframe)); // Odd sizes are cropped to even ones instead of failing. let odd = voelin_media::VideoFrame::black_i420(201, 151).with_timestamp(Duration::from_millis(66)); assert!(encoder.encode(&odd, false).unwrap().iter().any(|f| f.keyframe)); // Baseline on request. let baseline = EncoderConfig { h264_profile: voelin_media::H264Profile::ConstrainedBaseline, ..EncoderConfig::default() }; let mut encoder = FfmpegEncoder::new("libx264", baseline).unwrap(); let out = encoder.encode(&screen.frame(0, 30), false).unwrap(); assert_eq!(h264_profile(&out[0].data), Some(66)); } /// `profile_idc` of the first SPS in an Annex B stream. fn h264_profile(data: &[u8]) -> Option { sps(data).map(|(profile, _, _)| profile) } /// `profile_idc`, the constraint-flag byte (`constraint_set0..5` in the top /// six bits) and `level_idc` of the first SPS in an Annex B stream: the very /// three bytes of `profile-level-id` in the SDP `a=fmtp` line. /// /// They are the first three bytes of the SPS payload, so no bit reading is /// needed. Emulation prevention cannot touch them either: it only ever /// inserts a byte after two zeros, and no valid SPS starts /// `profile_idc == 0`. fn sps(data: &[u8]) -> Option<(u8, u8, u8)> { let mut i = 0; while i + 3 < data.len() { if data[i..i + 3] == [0, 0, 1] { if data[i + 3] & 0x1f == 7 { let p = data.get(i + 4..i + 7)?; return Some((p[0], p[1], p[2])); } i += 3; } else { i += 1; } } None } /// What every usable H.264 backend puts in the SPS against the /// `profile-level-id` the signalling offers for the same stream. A decoder /// set up from our offer must be able to decode what we send, so the /// emitted `level_idc` may never exceed the offered one, and `profile_idc` /// has to be the profile we claim. /// /// The constraint-flag byte is only reported, not asserted: VA-API has no /// Constrained High profile at all (`h264_vaapi` offers `main`, `high`, /// `high10`, `constrained_baseline`), so it emits plain High with the flags /// clear, while AMF's `constrained_high` sets `constraint_set4` and /// `constraint_set5` as the offer says. See `docs/media.md`. #[test] fn sps_carries_the_offered_profile_and_level() { use voelin_media::H264Profile; if ffmpeg().is_none() { return; } // 720p30 (offered level 3.1) and 1080p60 (4.2). const CASES: [(u32, u32, u32, u32); 2] = [(1280, 720, 30, 4_000_000), (1920, 1080, 60, 6_000_000)]; let mut tested = 0; for status in probe().iter().filter(|s| s.available.is_ok()) { if status.spec.codec != Codec::H264 { continue; } for profile in [H264Profile::ConstrainedHigh, H264Profile::ConstrainedBaseline] { let (offered_profile, want_idc) = match profile { H264Profile::ConstrainedHigh => (voelin_stream::H264Profile::ConstrainedHigh, 100), H264Profile::ConstrainedBaseline => { (voelin_stream::H264Profile::ConstrainedBaseline, 66) } }; for (w, h, fps, bitrate) in CASES { let config = EncoderConfig { fps, bitrate_bps: bitrate, h264_profile: profile, ..EncoderConfig::default() }; let mut encoder = FfmpegEncoder::new(status.spec.name, config).unwrap(); let screen = SyntheticScreen::new(w, h); // Hardware encoders hold a frame or two before the first packet. let mut packets = Vec::new(); for n in 0..8 { packets.extend(encoder.encode(&screen.frame(n, fps), n == 0).unwrap()); if !packets.is_empty() { break; } } let Some(first) = packets.first() else { panic!("{} produced no packet at {w}x{h}", status.spec.name) }; let Some((idc, flags, level)) = sps(&first.data) else { panic!("{} sent no SPS at {w}x{h}", status.spec.name) }; let offered = voelin_stream::h264::offer_profile_level_id( offered_profile, w, h, fps, u64::from(bitrate), ); let offered_level = (offered & 0xff) as u8; eprintln!( "{:<12} {profile:?} {w}x{h}@{fps}: SPS {idc:02x} {flags:02x} {level:02x}, \ offer {:06x}", status.spec.name, offered ); assert_eq!( idc, want_idc, "{} emits profile_idc {idc}, the offer claims {want_idc}", status.spec.name ); assert!( level <= offered_level, "{} emits level_idc {level} at {w}x{h}@{fps}, above the offered {offered_level}: \ a decoder set up from our SDP could refuse it", status.spec.name ); tested += 1; } } } assert!(tested > 0, "no usable H.264 backend to check"); } #[test] fn codecs_prefer_ffmpeg_h264_over_nothing() { if ffmpeg().is_none() || !available("libx264") { return; } let codecs = Codecs::new(); assert!(codecs.encoder_codecs().contains(&Codec::H264)); let encoder = codecs.new_encoder(Codec::H264, EncoderConfig::default()).unwrap(); assert!(matches!(encoder.backend(), EncoderBackend::Ffmpeg(_))); let named = voelin_media::EncoderPreference { hardware: false, backend: "libx264".parse().unwrap() }; let encoder = codecs.new_encoder_preferring(Codec::H264, EncoderConfig::default(), &named); assert_eq!(encoder.unwrap().backend(), EncoderBackend::Ffmpeg("libx264")); } /// Unknown names and hardware without its device fail with a reason, not /// a panic. #[test] fn unusable_backends_fail_cleanly() { if ffmpeg().is_none() { return; } assert!(FfmpegEncoder::new("no_such_encoder", EncoderConfig::default()).is_err()); for status in probe().iter().filter(|s| s.available.is_err()) { let Ok(mut encoder) = FfmpegEncoder::new(status.spec.name, EncoderConfig::default()) else { continue; }; let frame = voelin_media::VideoFrame::black_i420(W, H); assert!(encoder.encode(&frame, true).is_err(), "{} passed now", status.spec.name); } } /// A backend whose wrapper cannot change the bitrate running (libaom here, /// VA-API alike) waits for the next keyframe, but reopens at once when the /// target falls below half. #[test] fn bitrate_changes_without_live_reconfiguration() { if ffmpeg().is_none() || !available("libaom-av1") { return; } let screen = SyntheticScreen::new(W, H); let config = EncoderConfig { fps: 30, bitrate_bps: 1_000_000, ..EncoderConfig::default() }; let mut encoder = FfmpegEncoder::new("libaom-av1", config).unwrap(); let key = |encoder: &mut FfmpegEncoder, n: u64, force: bool| { encoder.encode(&screen.frame(n, 30), force).unwrap().iter().any(|f| f.keyframe) }; assert!(key(&mut encoder, 0, false)); assert!(!key(&mut encoder, 1, false)); encoder.set_bitrate(800_000).unwrap(); assert!(!key(&mut encoder, 2, false), "a small change waits for a keyframe"); encoder.set_bitrate(300_000).unwrap(); assert!(key(&mut encoder, 3, false), "congestion: at once"); encoder.set_bitrate(400_000).unwrap(); assert!(!key(&mut encoder, 4, false)); assert!(key(&mut encoder, 5, true)); assert!(!key(&mut encoder, 6, false)); } /// DMA-BUF import is for VA-API encoders and NV12 or RGB buffers; anything /// else is refused as unavailable, so the caller maps the buffer instead. A /// buffer the driver cannot import is an error, not a crash: the encoder /// takes the next frame, and drops cleanly although nothing went in (FFmpeg /// 9's VA-API encoders crash when drained before their first frame). #[cfg(target_os = "linux")] #[test] fn dmabuf_import_is_refused_cleanly() { use std::os::fd::AsRawFd; use voelin_media::capture::{DRM_MOD_LINEAR, DmaBufRef, drm_fourcc}; if ffmpeg().is_none() { return; } let not_a_dmabuf = std::fs::File::open("/dev/null").unwrap(); let frame = DmaBufRef { width: W, height: H, timestamp: Duration::ZERO, fourcc: drm_fourcc(b"XR24"), modifier: DRM_MOD_LINEAR, fd: not_a_dmabuf.as_raw_fd(), size: (W * H * 4) as usize, planes: [(0, (W * 4) as usize), (0, 0), (0, 0), (0, 0)], plane_count: 1, }; let unavailable = |name: &str, frame: &DmaBufRef| { let mut encoder = FfmpegEncoder::new(name, EncoderConfig::default()).unwrap(); let err = encoder.encode_dmabuf(frame, true, &mut |_| {}).unwrap_err(); assert!(matches!(err, voelin_media::Error::CodecUnavailable { .. }), "{name}: {err}"); }; unavailable("libx264", &frame); unavailable("h264_vaapi", &DmaBufRef { fourcc: drm_fourcc(b"YUYV"), ..frame }); if !available("h264_vaapi") { return; } let mut encoder = FfmpegEncoder::new("h264_vaapi", EncoderConfig::default()).unwrap(); assert!(encoder.encode_dmabuf(&frame, true, &mut |_| {}).is_err(), "/dev/null imported"); drop(encoder); let mut encoder = FfmpegEncoder::new("h264_vaapi", EncoderConfig::default()).unwrap(); assert!(encoder.encode_dmabuf(&frame, true, &mut |_| {}).is_err()); let black = voelin_media::VideoFrame::black_i420(W, H); assert!(!encoder.encode(&black, true).unwrap().is_empty(), "no packet after a failed import"); }