From cffd9afced70cfa609f2e52bd25b2e16259eed34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 16:58:08 +0300 Subject: [PATCH] test(ec-va,ec-hw): verify VPP filter probe, struct layout, and P010 zero-copy encode against a real 4K HEVC HDR file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ec-va/tests/gpu.rs: vpp_filters_probe (vaQueryVideoProcFilters on the real display — radeonsi advertises only VAProcFilterDeinterlacing (2), no HighDynamicRangeToneMapping) and vpp_converts_p010_to_nv12 (Vpp::convert end to end). ec-va/src/sys.rs: a const size_of/offset_of assert block for VAProcPipelineParameterBuffer against va_vpp.h:886 (224 bytes on this target, field offsets checked through va_reserved) plus VAProcColorProperties — the layout was already correct, this just pins it. ec-hw/tests/gpu.rs, against a real HEVC Main10 P010 4K HDR mkv: - encode_frame_accepts_p010_via_vpp: encode_frame (zero-copy, P010 via VPP) vs to_i420()+encode (CPU path), decoded back and compared. H.264: worst Y and UV PSNR both infinite (byte-identical after decode). H.265 is refused by encode_frame's own documented contract ("frame's coded size does not match this encoder's ... the VPP conversion pass does not resize") because this file's SPS pic_height_in_luma_samples (2160) is a multiple of the H.264 encode block size (16) but not the HEVC one (32) — an existing, intentional design choice, not a defect this round touched. - p010_zero_copy_encode_is_realtime: median 6.14 ms/frame for decode+encode_frame (H.264, the target that isn't refused on this file) vs 22.55 ms/frame for decode+to_i420+encode — zero-copy is ~3.7x faster and well inside the 20 ms budget. cargo test -p ec-va -- --test-threads=1: 13 passed (+2 doctests, +4 compile-fail doctests). cargo test -p ec-hw --release --test gpu -- --test-threads=1: 27 passed, 0 failed (most non-P010 tests skip: no generated bitstream fixtures on this checkout). --- crates/ec-hw/tests/gpu.rs | 213 ++++++++++++++++++++++++++++++++++++++ crates/ec-va/src/sys.rs | 18 ++++ crates/ec-va/tests/gpu.rs | 45 ++++++++ 3 files changed, 276 insertions(+) diff --git a/crates/ec-hw/tests/gpu.rs b/crates/ec-hw/tests/gpu.rs index a6928ac4..91904af6 100644 --- a/crates/ec-hw/tests/gpu.rs +++ b/crates/ec-hw/tests/gpu.rs @@ -1508,6 +1508,219 @@ fn ten_bit_4k_frame_path_is_realtime() { println!("target: decode+to_i420_16 <= 39.00 ms/frame median (24 fps budget); got {got:.2}"); } +const REELD: &str = "/home/user/Downloads/reeld\ + /reeld.mkv"; + +/// `Encoder::encode_frame` of a 10-bit (P010) source, through the VPP +/// NV12 conversion, against the CPU path (`to_i420` + `encode`) — the same +/// shape as `encode_frame_zero_copy_matches_the_cpu_path`, but for a real +/// P010 4K HDR source instead of a synthetic 8-bit one, and comparing Y and +/// UV PSNR separately rather than the combined figure. +#[test] +fn encode_frame_accepts_p010_via_vpp() { + let Some(display) = display() else { return }; + let path = Path::new(REELD); + if !path.exists() { + eprintln!("skipped: film not present"); + return; + } + let frames = 10usize; + let Some(data) = elementary(path, Codec::H265, frames) else { + eprintln!("skipped: no elementary stream"); + return; + }; + + // This film's HEVC SPS `pic_height_in_luma_samples` is 2160, which is a + // multiple of the H.264 encode block size (16) but not the HEVC one + // (32, next_multiple_of gives 2176) — `encode_frame` refuses that + // mismatch by documented design ("frame's coded size does not match + // this encoder's ... the VPP conversion pass does not resize") rather + // than silently stretching the picture, so the HEVC target is expected + // to be *refused* here, not to round-trip; only H.264 gets the full + // encode/decode/PSNR comparison. + for (target, dec_codec) in [(EncCodec::H264, Codec::H264), (EncCodec::H265, Codec::H265)] { + let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens"); + let mut zc_stream = Vec::new(); + let mut cpu_stream = Vec::new(); + let mut count = 0i64; + let mut zero_copy: Option = None; + let mut cpu_path: Option = None; + let mut refused = false; + + decode_stream(&mut decoder, Codec::H265, &data, frames, |frame| { + if refused { + return; + } + let (w, h) = frame.display_size; + let zero_copy = zero_copy.get_or_insert_with(|| { + let mut cfg = EncoderConfig::new(target, w, h); + cfg.gop_size = 30; + cfg.rate_control = RateControlMode::ConstantQp { qp: 22 }; + Encoder::new(&display, cfg).expect("zero-copy encoder opens") + }); + let cpu_path = cpu_path.get_or_insert_with(|| { + let mut cfg = EncoderConfig::new(target, w, h); + cfg.gop_size = 30; + cfg.rate_control = RateControlMode::ConstantQp { qp: 22 }; + Encoder::new(&display, cfg).expect("cpu-path encoder opens") + }); + let meta = FrameMetadata { + timestamp: count, + force_keyframe: false, + }; + match zero_copy.encode_frame(&frame, meta) { + Ok(zc) => zc_stream.extend_from_slice(&zc.data), + Err(e) if target == EncCodec::H265 => { + println!( + "{target:?}: encode_frame refused as documented (block size vs SPS \ + pic height): {e}" + ); + assert!( + e.to_string().contains("encoder surfaces are"), + "unexpected refusal reason: {e}" + ); + refused = true; + return; + } + Err(e) => panic!("encode_frame (zero copy, P010 via VPP): {e}"), + } + let i420 = frame.to_i420().expect("readback for the CPU-path baseline"); + let cpu = cpu_path.encode(&i420, meta).expect("encode (CPU path)"); + cpu_stream.extend_from_slice(&cpu.data); + count += 1; + }) + .expect("decode"); + if refused { + continue; + } + assert_eq!( + count as usize, frames, + "{target:?}: decoded {count} of {frames}" + ); + + let mut zc_decoder = Decoder::new(&display, dec_codec).expect("zc decoder opens"); + let mut cpu_decoder = Decoder::new(&display, dec_codec).expect("cpu decoder opens"); + let mut zc_frames = Vec::new(); + let mut cpu_frames = Vec::new(); + decode_stream(&mut zc_decoder, dec_codec, &zc_stream, frames, |f| { + zc_frames.push(f.to_i420().expect("readback")) + }) + .expect("decode zero-copy stream"); + decode_stream(&mut cpu_decoder, dec_codec, &cpu_stream, frames, |f| { + cpu_frames.push(f.to_i420().expect("readback")) + }) + .expect("decode CPU-path stream"); + assert_eq!(zc_frames.len(), frames, "{target:?}: zero-copy frame count"); + assert_eq!(cpu_frames.len(), frames, "{target:?}: CPU-path frame count"); + + let widen = |v: &[u8]| -> Vec { v.iter().map(|&s| u16::from(s)).collect() }; + let mut worst_y = f64::INFINITY; + let mut worst_uv = f64::INFINITY; + for (a, b) in zc_frames.iter().zip(&cpu_frames) { + let y_db = plane_psnr(&widen(&a.y), &widen(&b.y), 255.0); + let uv_db = plane_psnr( + &[widen(&a.u), widen(&a.v)].concat(), + &[widen(&b.u), widen(&b.v)].concat(), + 255.0, + ); + worst_y = worst_y.min(y_db); + worst_uv = worst_uv.min(uv_db); + } + println!("{target:?} encode_frame (P010 via VPP) vs CPU path: worst Y {worst_y:.1} dB, worst UV {worst_uv:.1} dB"); + assert!(worst_y >= 40.0, "{target:?}: worst Y {worst_y:.1} dB vs the CPU path"); + assert!(worst_uv >= 40.0, "{target:?}: worst UV {worst_uv:.1} dB vs the CPU path"); + } +} + +/// Median ms/frame at 3840x2160 for `decode+encode_frame` (P010 zero-copy, +/// through VPP) against `decode+to_i420+encode` (CPU path) — the number that +/// says whether the VPP conversion is cheap enough to keep the zero-copy path +/// meaningfully faster than reading the frame back to system memory. +/// +/// Targets H.264: this film's HEVC SPS `pic_height_in_luma_samples` (2160) +/// is not a multiple of this crate's HEVC encode block size (32), so +/// `encode_frame` refuses the HEVC target on this file by documented design +/// (see `encode_frame_accepts_p010_via_vpp`) — 2160 *is* a multiple of the +/// H.264 block size (16), so H.264 is the target that actually exercises the +/// zero-copy path on this real source. +#[test] +fn p010_zero_copy_encode_is_realtime() { + let Some(display) = display() else { return }; + let path = Path::new(REELD); + if !path.exists() { + eprintln!("skipped: film not present"); + return; + } + let limit = 60usize; + let Some(data) = elementary(path, Codec::H265, limit) else { + eprintln!("skipped: no elementary stream"); + return; + }; + let units = split_access_units(&data, Codec::H265); + assert!(units.len() > limit, "film clip too short for {limit} frames"); + + println!("{:<24} {:>10} {:>10} {:>10}", "stage", "median ms", "min ms", "max ms"); + let mut zc_median = None; + let mut cpu_median = None; + for (name, zero_copy) in [("decode+encode_frame", true), ("decode+to_i420+encode", false)] { + let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens"); + let mut encoder: Option = None; + let mut durations: Vec = Vec::new(); + let mut count = 0i64; + 'outer: for (i, unit) in units.iter().enumerate() { + decoder.decode(unit, i as i64).expect("decode"); + while let Some(frame) = decoder.next_frame() { + let (w, h) = frame.display_size; + let encoder = encoder.get_or_insert_with(|| { + let mut cfg = EncoderConfig::new(EncCodec::H264, w, h); + cfg.gop_size = 30; + cfg.rate_control = RateControlMode::ConstantQp { qp: 22 }; + Encoder::new(&display, cfg).expect("encoder opens") + }); + let meta = FrameMetadata { + timestamp: count, + force_keyframe: false, + }; + let t0 = Instant::now(); + if zero_copy { + encoder.encode_frame(&frame, meta).expect("encode_frame"); + } else { + let i420 = frame.to_i420().expect("readback"); + encoder.encode(&i420, meta).expect("encode"); + } + durations.push(t0.elapsed()); + count += 1; + if durations.len() >= limit { + break 'outer; + } + } + } + assert!( + durations.len() >= limit, + "{name}: only {} of {limit} frames encoded", + durations.len() + ); + durations.sort(); + let ms = |d: Duration| d.as_secs_f64() * 1000.0; + let median = ms(durations[durations.len() / 2]); + let min = ms(durations[0]); + let max = ms(durations[durations.len() - 1]); + println!("{name:<24} {median:>10.2} {min:>10.2} {max:>10.2}"); + if zero_copy { + zc_median = Some(median); + } else { + cpu_median = Some(median); + } + } + let zc = zc_median.expect("zero-copy phase ran"); + let cpu = cpu_median.expect("CPU-path phase ran"); + println!("target: decode+encode_frame <= 20.00 ms/frame median; got {zc:.2} (CPU path {cpu:.2})"); + assert!( + zc <= 20.0, + "decode+encode_frame median {zc:.2} ms/frame exceeds the 20 ms budget (CPU path: {cpu:.2} ms)" + ); +} + /// A CRA GOP boundary stores its RASL leading pictures *after* the CRA in /// decode order, even though they display before it (they predict from /// pictures the CRA's own random-access point does not have). A decoder diff --git a/crates/ec-va/src/sys.rs b/crates/ec-va/src/sys.rs index a568f409..ea73d487 100644 --- a/crates/ec-va/src/sys.rs +++ b/crates/ec-va/src/sys.rs @@ -673,6 +673,24 @@ const _: () = { assert!(size_of::() == 8 && align_of::() == 2); + assert!( + size_of::() == 8 && align_of::() == 1 + ); + + // 64-bit `va_vpp.h:886`: 25 fields down to `output_hdr_metadata` (152 + // bytes on this target) plus `va_reserved[VA_PADDING_LARGE - 16]` (16 + // `u32`s, 64 bytes) = 224. + assert!( + size_of::() == 224 + && align_of::() == 8 + ); + assert!(offset_of!(VAProcPipelineParameterBuffer, surface_region) == 8); + assert!(offset_of!(VAProcPipelineParameterBuffer, input_color_properties) == 132); + assert!(offset_of!(VAProcPipelineParameterBuffer, output_color_properties) == 140); + assert!(offset_of!(VAProcPipelineParameterBuffer, processing_mode) == 148); + assert!(offset_of!(VAProcPipelineParameterBuffer, output_hdr_metadata) == 152); + assert!(offset_of!(VAProcPipelineParameterBuffer, va_reserved) == 160); + // Assumption 2: C `enum` and `unsigned int` are both 4 bytes here. assert!(size_of::() == 4); assert!(size_of::() == 4); diff --git a/crates/ec-va/tests/gpu.rs b/crates/ec-va/tests/gpu.rs index b91d727b..5eda1fc7 100644 --- a/crates/ec-va/tests/gpu.rs +++ b/crates/ec-va/tests/gpu.rs @@ -477,6 +477,51 @@ fn context_keeps_its_dependencies_alive() { assert!(display.is_valid()); } +// -------------------------------------------------------------------------- +// VPP +// -------------------------------------------------------------------------- + +/// `vaQueryVideoProcFilters` on the real display: whatever this driver +/// advertises, the call must succeed and return values `Vpp` can act on. Not +/// a claim about which filters exist — radeonsi's list is print-only here — +/// just that the query path works end to end. +#[test] +fn vpp_filters_probe() { + let _serial = serial(); + let display = display(); + let targets = Surface::create_pool(&display, &SurfaceSpec::nv12(1920, 1080), 1).expect("pool"); + let vpp = ec_va::Vpp::new(&display, sys::VA_RT_FORMAT_YUV420, &targets).expect("vpp opens"); + let filters = vpp.filters().expect("vaQueryVideoProcFilters"); + println!("radeonsi VAProcFilterType list: {filters:?}"); + println!( + "HighDynamicRangeToneMapping (8) advertised: {}", + filters.contains(&sys::VAProcFilterHighDynamicRangeToneMapping) + ); +} + +/// `Vpp::convert` end to end: a P010 source surface into an NV12 destination, +/// no readback — the conversion `ec-hw`'s `encode_frame` relies on for a +/// 10-bit source. +#[test] +fn vpp_converts_p010_to_nv12() { + let _serial = serial(); + let display = display(); + let src_spec = SurfaceSpec::p010(64, 64); + let source = Surface::create(&display, &src_spec).expect("p010 source surface"); + let dest_targets = Surface::create_pool(&display, &SurfaceSpec::nv12(64, 64), 1).expect("pool"); + let vpp = ec_va::Vpp::new( + &display, + sys::VA_RT_FORMAT_YUV420 | sys::VA_RT_FORMAT_YUV420_10, + &dest_targets, + ) + .expect("vpp opens"); + let dest = vpp + .convert(&source, Arc::clone(&dest_targets[0])) + .expect("convert"); + assert_eq!(dest.id(), dest_targets[0].id()); + assert!(display.is_valid()); +} + // -------------------------------------------------------------------------- // helpers // --------------------------------------------------------------------------