diff --git a/crates/voelin-media/tests/ffmpeg_decoders.rs b/crates/voelin-media/tests/ffmpeg_decoders.rs index 33affd4..9a7ef1f 100644 --- a/crates/voelin-media/tests/ffmpeg_decoders.rs +++ b/crates/voelin-media/tests/ffmpeg_decoders.rs @@ -299,32 +299,51 @@ fn ffmpeg_command(path: &std::path::Path, args: &[&str]) -> bool { true } -/// H.264 with B-frames at 2560x1440 and 60 fps, like the official client's -/// stream (AMF with B-frames and rare keyframes): every H.264 decoder of -/// the ladder, and Cisco's OpenH264 with `VOELIN_OPENH264_LIB`, decodes it; -/// how fast, the slowest frame, and whether every picture comes out, in -/// order (against FFmpeg's software decoder). The streams come from the -/// `ffmpeg` command: x264 with 3 B-frames, `h264_vaapi` with 2, ten -/// seconds of `testsrc2` at 8 Mbit/s, keyframes every ten seconds. Run with -/// `--release`. -#[test] -#[ignore = "a measurement: needs the ffmpeg command; run with --release"] -fn h264_with_b_frames_at_1440p60() { - if ffmpeg().is_none() { - return; +/// The temporal units of a low-overhead AV1 stream (`-f obu`), each +/// starting with a temporal delimiter OBU. +fn temporal_units(stream: &[u8]) -> Vec<&[u8]> { + let mut starts = Vec::new(); + let mut pos = 0; + while pos < stream.len() { + let header = stream[pos]; + if (header >> 3) & 0xf == 2 { + starts.push(pos); + } + // Every OBU of such a stream has its size (LEB128). + let (mut at, mut size, mut shift) = (pos + 1 + usize::from(header & 4 != 0), 0, 0); + loop { + let byte = stream[at]; + at += 1; + size |= usize::from(byte & 0x7f) << shift; + shift += 7; + if byte & 0x80 == 0 { + break; + } + } + pos = at + size; } + starts.push(stream.len()); + starts.windows(2).map(|w| &stream[w[0]..w[1]]).collect() +} + +/// Streams of ten seconds of `testsrc2` at 2560x1440 and 60 fps, made with +/// the `ffmpeg` command from each encoder's `args` (kept in the temporary +/// directory, made again only when missing). +fn streams_1440p60( + encoders: &[(&'static str, &str)], + format: &str, +) -> Vec<(&'static str, Vec)> { let dir = std::env::temp_dir().join("voelin-h264-bframes"); std::fs::create_dir_all(&dir).unwrap(); let source = "-f lavfi -i testsrc2=size=2560x1440:rate=60 -t 10"; - let rate = "-g 600 -b:v 8M -maxrate 9M -bufsize 9M -f h264"; - let x264 = "-c:v libx264 -preset veryfast -profile:v high -bf 3 -x264-params aud=1"; - let vaapi = "-vf format=nv12,hwupload -c:v h264_vaapi -profile:v high -bf 2 -aud 1"; let mut streams = Vec::new(); - for (name, args) in [ - ("x264", format!("{source} {x264} -pix_fmt yuv420p {rate}")), - ("h264_vaapi", format!("-vaapi_device /dev/dri/renderD128 {source} {vaapi} {rate}")), - ] { - let path = dir.join(format!("{name}-bframes-1440p60.h264")); + for &(name, args) in encoders { + let vaapi = name.ends_with("_vaapi"); + let device = if vaapi { "-vaapi_device /dev/dri/renderD128 " } else { "" }; + let upload = if vaapi { "-vf format=nv12,hwupload " } else { "" }; + let args = format!("{device}{source} {upload}{args} -g 600 -b:v 8M -f {format}"); + let suffix = if format == "h264" { "-bframes" } else { "" }; + let path = dir.join(format!("{name}{suffix}-1440p60.{format}")); let args: Vec<&str> = args.split(' ').collect(); if ffmpeg_command(&path, &args) { streams.push((name, std::fs::read(&path).unwrap())); @@ -332,22 +351,40 @@ fn h264_with_b_frames_at_1440p60() { eprintln!("{name}: the ffmpeg command did not make the stream, skipped"); } } + streams +} + +/// Every decoder of `codec`'s ladder (and OpenH264 with +/// `VOELIN_OPENH264_LIB`, for H.264) on each of `streams` (frames as +/// `split` cuts them), as fast as it goes: pictures, errors, pictures per +/// second of decoding (copied into a `VideoFrame`), the slowest frame, and +/// the pictures that differ from those of `reference`, which comes first +/// in the ladder's software part. Hardware decoders must decode every +/// picture. +fn decode_rates( + codec: Codec, + what: &str, + streams: &[(&str, Vec)], + split: fn(&[u8]) -> Vec<&[u8]>, + reference: &str, +) { let codecs = Codecs::new(); let openh264 = std::env::var_os("VOELIN_OPENH264_LIB") + .filter(|_| codec == Codec::H264) .map(|path| voelin_media::codec::h264::OpenH264::load(path).unwrap()); - let mut names: Vec = - codecs.decoders_for(Codec::H264).iter().map(|b| b.to_string()).collect(); + let mut names: Vec = codecs.decoders_for(codec).iter().map(|b| b.to_string()).collect(); + // The reference first, so that every other decoder is compared with it. + names.sort_by_key(|n| n != reference); names.extend(openh264.as_ref().map(|_| "openh264".to_owned())); - for (stream, data) in &streams { - let units = access_units(data); + for (stream, data) in streams { + let units = split(data); let mut reference: Option>> = None; for name in &names { let mut decoder: Box = match (name.as_str(), &openh264) { ("openh264", Some(library)) => Box::new(library.decoder().unwrap()), _ => { - let backend = - codecs.decoders_for(Codec::H264).into_iter().find(|b| b.name() == name); - codecs.new_decoder_with(Codec::H264, backend.unwrap()).unwrap() + let backend = codecs.decoders_for(codec).into_iter().find(|b| b.name() == name); + codecs.new_decoder_with(codec, backend.unwrap()).unwrap() } }; let mut picture = VideoFrame::black_i420(0, 0); @@ -375,20 +412,48 @@ fn h264_with_b_frames_at_1440p60() { thumbnails.iter().zip(reference).filter(differs).count() }); eprintln!( - "{stream} 2560x1440@60 with B-frames, {name}: {} of {} pictures, {errors} errors, \ - {fps:.0} fps, slowest frame {:.1} ms, {differing} differ from FFmpeg's software decoder", + "{stream} 2560x1440@60{what}, {name}: {} of {} pictures, {errors} errors, {fps:.0} \ + fps, slowest frame {:.1} ms, {differing} differ from the reference decoder", thumbnails.len(), units.len(), slowest.as_secs_f64() * 1000.0, ); - if name == "h264" { + if reference.is_none() { reference = Some(thumbnails); } else if name.ends_with("_vaapi") { assert_eq!(errors, 0, "{name}"); assert!(thumbnails.len() + 3 >= units.len(), "{name}: pictures missing"); + assert_eq!(differing, 0, "{name}"); } } } +} + +/// H.264 with B-frames at 2560x1440 and 60 fps, like the official client's +/// stream (AMF with B-frames and rare keyframes): every H.264 decoder of +/// the ladder, and Cisco's OpenH264 with `VOELIN_OPENH264_LIB`, decodes it; +/// how fast, the slowest frame, and whether every picture comes out, in +/// order (against FFmpeg's software decoder). The streams come from the +/// `ffmpeg` command: x264 with 3 B-frames, `h264_vaapi` with 2, ten +/// seconds of `testsrc2` at 8 Mbit/s, keyframes every ten seconds. Run with +/// `--release`. +#[test] +#[ignore = "a measurement: needs the ffmpeg command; run with --release"] +fn h264_with_b_frames_at_1440p60() { + if ffmpeg().is_none() { + return; + } + let streams = streams_1440p60( + &[ + ( + "x264", + "-c:v libx264 -preset veryfast -profile:v high -bf 3 -x264-params aud=1 -pix_fmt yuv420p -maxrate 9M -bufsize 9M", + ), + ("h264_vaapi", "-c:v h264_vaapi -profile:v high -bf 2 -aud 1 -maxrate 9M"), + ], + "h264", + ); + decode_rates(Codec::H264, " with B-frames", &streams, access_units, "h264"); // What a viewer adds per picture: the NV12 or I420 picture to RGBA. let mut rgba = vec![0; 2560 * 1440 * 4]; let i420 = VideoFrame::black_i420(2560, 1440); @@ -398,3 +463,20 @@ fn h264_with_b_frames_at_1440p60() { } eprintln!("to RGBA at 2560x1440: {:.1} ms", started.elapsed().as_secs_f64() * 1000.0 / 30.0); } + +/// AV1 at 2560x1440 and 60 fps, which the official client sends from GPUs +/// that encode it: every AV1 decoder of the ladder, against dav1d. Streams +/// from `av1_vaapi` (hardware, as the official client's AMF) and SVT-AV1. +/// Run with `--release`. +#[test] +#[ignore = "a measurement: needs the ffmpeg command; run with --release"] +fn av1_at_1440p60() { + if ffmpeg().is_none() { + return; + } + let streams = streams_1440p60( + &[("av1_vaapi", "-c:v av1_vaapi"), ("libsvtav1", "-c:v libsvtav1 -preset 10")], + "obu", + ); + decode_rates(Codec::Av1, "", &streams, temporal_units, "libdav1d"); +} diff --git a/docs/media.md b/docs/media.md index 3ada234..5bfd2f8 100644 --- a/docs/media.md +++ b/docs/media.md @@ -582,7 +582,7 @@ on (`Video::set_decoder_preference`); a running watch keeps its decoder. from there into the caller's `VideoFrame` (`decode_into` reuses its buffers): no Rust allocation per picture for any decoder (`tests/decoder_alloc.rs`). Zero-copy display was not the goal; both - copies are part of the 2.4-2.5 ms per 1440p picture measured in + copies are part of the 2.4-3.3 ms per 1440p picture measured in [Decoders, measured](#decoders-measured). - Packets are handed over without a reference, so FFmpeg copies them into its own padded buffer; pictures come out as `yuv420p` (or `yuvj420p`) @@ -1003,13 +1003,14 @@ build, Radeon RX 7900 GRE, FFmpeg 9.0.1, Cisco's OpenH264 2.6.0. Decoding as fast as it goes (`tests/ffmpeg_decoders.rs` `h264_with_b_frames_at_1440p60`, `--ignored`): pictures out of 600 frames, -pictures per second of decoding (copied into a `VideoFrame`), the slowest -frame. Both FFmpeg decoders give the same pictures, in order. +pictures per second of decoding (copied into a `VideoFrame`; two runs on +a desktop in use), the slowest frame. Both FFmpeg decoders give the same +pictures, in order. | Decoder | x264 stream | `h264_vaapi` stream | |---|---|---| -| `h264_vaapi` | 598, 424 fps, 6.8 ms | 599, 398 fps, 4.0 ms | -| `h264` (software) | 598, 276 fps, 8.0 ms | 599, 302 fps, 6.6 ms | +| `h264_vaapi` | 598, 368-424 fps, 6.8-9.0 ms | 599, 355-398 fps, 4.0-8.8 ms | +| `h264` (software) | 598, 276-283 fps, 4.9-8.0 ms | 599, 268-302 fps, 6.6-8.7 ms | | OpenH264 | 8, 592 errors | 8, 592 errors | Played in real time into a `VideoPipeline` whose consumer converts every @@ -1026,6 +1027,22 @@ pipeline then waited for keyframes the encoder sends every few seconds at most. That was the "very bad frame rate" of an official client's stream in the app. +AV1 at 2560x1440 and 60 fps, which the official client sends from GPUs that +encode it (`av1_at_1440p60`, `--ignored`; streams from `av1_vaapi` and +SVT-AV1 preset 10, 8 Mbit/s): every decoder gave all 600 pictures, the same +as dav1d's. + +| Decoder | `av1_vaapi` stream | SVT-AV1 stream | +|---|---|---| +| `av1_vaapi` | 309 fps, 11.4 ms | 301 fps, 15.4 ms | +| `libdav1d` (8 threads) | 412 fps, 8.6 ms | 426 fps, 31.2 ms | +| `libaom-av1` | 240 fps, 24.7 ms | 286 fps, 49.6 ms | + +dav1d on 8 of the 32 cores outruns the GPU here, whose pictures also have to +be copied out of it; the ladder still takes the GPU first, for the CPU it +leaves free (the rule of the whole project: the best first, the others as +fallbacks). Every decoder has 4-7 times the 60 fps it needs. + ### Wayland screen capture, measured Same machine, a 2560x1440 Wayland desktop through the ScreenCast portal, @@ -1180,7 +1197,7 @@ has not run on Windows yet. | No allocation per picture in any FFmpeg decoder | `tests/decoder_alloc.rs` (counting allocator) | tested: 0 allocations over 30 pictures for each of the ten decoders that work here | | What a viewer accepts with and without FFmpeg (`VOELIN_FFMPEG=0`) | `tests/ffmpeg_decoders.rs` `decoders_with_and_without_ffmpeg` (the second half in a child process) | tested: AV1, HEVC, VP9, H.264, VP8 with FFmpeg; VP9, VP8 (libvpx) without | | A failing decoder replaced by the next of the ladder (errors, or no pictures), pictures going on; after a loss a concealing decoder goes on and one that does not skips to the keyframe, keyframes asked for every 500 ms until one comes | `voelin-core` `media::tests::a_failing_decoder_is_replaced_by_the_next`, `losses_ask_for_keyframes_until_one_comes` | tested (a decoder made to fail ahead of libvpx) | -| H.264 with B-frames at 1440p60: every decoder, and the pipeline in real time with and without losses | `tests/ffmpeg_decoders.rs` `h264_with_b_frames_at_1440p60`, `voelin-core/tests/decode_rate.rs` (both `--ignored`, `--release`) | measured, see [Decoders, measured](#decoders-measured) | +| H.264 with B-frames and AV1 at 1440p60: every decoder, and (H.264) the pipeline in real time with and without losses | `tests/ffmpeg_decoders.rs` `h264_with_b_frames_at_1440p60`, `av1_at_1440p60`, `voelin-core/tests/decode_rate.rs` (all `--ignored`, `--release`) | measured, see [Decoders, measured](#decoders-measured) | | Chromium's AV1, VP9 and H.264 → our viewer → the pipeline, at their size | `voelin-core/tests/browser_codecs.rs` `browser_streams_decode_in_the_pipeline` (`VOELIN_INTEROP=1`, system Chromium 152) | tested: decoded by `av1_vaapi`, `vp9_vaapi`, `h264_vaapi` | | Hardware decoders NVDEC, D3D11VA, DXVA2, VideoToolbox | – | not tested (no such hardware here); NVDEC is skipped by the vendor check, the Windows table type-checks for `x86_64-pc-windows-gnu`, the macOS one is not compiled here | | X11 capture (MIT-SHM and GetImage), sources, window capture → VP8 → decode, cursor | `tests/x11_capture.rs` under Xvfb (`VOELIN_X11_TEST_DISPLAY`) | tested |