forge
Explore
Log in

Don't you dare go Hollow

Verify VPP filter probe, struct layout, and P010 zero-copy encode against a real 4K HEVC HDR file

Browse files at this commit

cffd9afced70cfa609f2e52bd25b2e16259eed34Ahmet Kaan GümüşParent: 67c8428
  • test
    • verify VPP filter probe, struct layout, and P010 zero-copy encode against a real 4K HEVC HDR file
Show full message
test(ec-va,ec-hw): verify VPP filter probe, struct layout, and P010 zero-copy encode against a real 4K HEVC HDR file

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.rsmodified+213 -0
@@ -1508,6 +1508,219 @@ fn ten_bit_4k_frame_path_is_realtime() {
1508 println!("target: decode+to_i420_16 <= 39.00 ms/frame median (24 fps budget); got {got:.2}");1508 println!("target: decode+to_i420_16 <= 39.00 ms/frame median (24 fps budget); got {got:.2}");
1509 }1509 }
1510 1510
1511+const REELD: &str = "/home/user/Downloads/reeld\
1512+ /reeld.mkv";
1513+
1514+/// `Encoder::encode_frame` of a 10-bit (P010) source, through the VPP
1515+/// NV12 conversion, against the CPU path (`to_i420` + `encode`) — the same
1516+/// shape as `encode_frame_zero_copy_matches_the_cpu_path`, but for a real
1517+/// P010 4K HDR source instead of a synthetic 8-bit one, and comparing Y and
1518+/// UV PSNR separately rather than the combined figure.
1519+#[test]
1520+fn encode_frame_accepts_p010_via_vpp() {
1521+ let Some(display) = display() else { return };
1522+ let path = Path::new(REELD);
1523+ if !path.exists() {
1524+ eprintln!("skipped: film not present");
1525+ return;
1526+ }
1527+ let frames = 10usize;
1528+ let Some(data) = elementary(path, Codec::H265, frames) else {
1529+ eprintln!("skipped: no elementary stream");
1530+ return;
1531+ };
1532+
1533+ // This film's HEVC SPS `pic_height_in_luma_samples` is 2160, which is a
1534+ // multiple of the H.264 encode block size (16) but not the HEVC one
1535+ // (32, next_multiple_of gives 2176) — `encode_frame` refuses that
1536+ // mismatch by documented design ("frame's coded size does not match
1537+ // this encoder's ... the VPP conversion pass does not resize") rather
1538+ // than silently stretching the picture, so the HEVC target is expected
1539+ // to be *refused* here, not to round-trip; only H.264 gets the full
1540+ // encode/decode/PSNR comparison.
1541+ for (target, dec_codec) in [(EncCodec::H264, Codec::H264), (EncCodec::H265, Codec::H265)] {
1542+ let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens");
1543+ let mut zc_stream = Vec::new();
1544+ let mut cpu_stream = Vec::new();
1545+ let mut count = 0i64;
1546+ let mut zero_copy: Option<Encoder> = None;
1547+ let mut cpu_path: Option<Encoder> = None;
1548+ let mut refused = false;
1549+
1550+ decode_stream(&mut decoder, Codec::H265, &data, frames, |frame| {
1551+ if refused {
1552+ return;
1553+ }
1554+ let (w, h) = frame.display_size;
1555+ let zero_copy = zero_copy.get_or_insert_with(|| {
1556+ let mut cfg = EncoderConfig::new(target, w, h);
1557+ cfg.gop_size = 30;
1558+ cfg.rate_control = RateControlMode::ConstantQp { qp: 22 };
1559+ Encoder::new(&display, cfg).expect("zero-copy encoder opens")
1560+ });
1561+ let cpu_path = cpu_path.get_or_insert_with(|| {
1562+ let mut cfg = EncoderConfig::new(target, w, h);
1563+ cfg.gop_size = 30;
1564+ cfg.rate_control = RateControlMode::ConstantQp { qp: 22 };
1565+ Encoder::new(&display, cfg).expect("cpu-path encoder opens")
1566+ });
1567+ let meta = FrameMetadata {
1568+ timestamp: count,
1569+ force_keyframe: false,
1570+ };
1571+ match zero_copy.encode_frame(&frame, meta) {
1572+ Ok(zc) => zc_stream.extend_from_slice(&zc.data),
1573+ Err(e) if target == EncCodec::H265 => {
1574+ println!(
1575+ "{target:?}: encode_frame refused as documented (block size vs SPS \
1576+ pic height): {e}"
1577+ );
1578+ assert!(
1579+ e.to_string().contains("encoder surfaces are"),
1580+ "unexpected refusal reason: {e}"
1581+ );
1582+ refused = true;
1583+ return;
1584+ }
1585+ Err(e) => panic!("encode_frame (zero copy, P010 via VPP): {e}"),
1586+ }
1587+ let i420 = frame.to_i420().expect("readback for the CPU-path baseline");
1588+ let cpu = cpu_path.encode(&i420, meta).expect("encode (CPU path)");
1589+ cpu_stream.extend_from_slice(&cpu.data);
1590+ count += 1;
1591+ })
1592+ .expect("decode");
1593+ if refused {
1594+ continue;
1595+ }
1596+ assert_eq!(
1597+ count as usize, frames,
1598+ "{target:?}: decoded {count} of {frames}"
1599+ );
1600+
1601+ let mut zc_decoder = Decoder::new(&display, dec_codec).expect("zc decoder opens");
1602+ let mut cpu_decoder = Decoder::new(&display, dec_codec).expect("cpu decoder opens");
1603+ let mut zc_frames = Vec::new();
1604+ let mut cpu_frames = Vec::new();
1605+ decode_stream(&mut zc_decoder, dec_codec, &zc_stream, frames, |f| {
1606+ zc_frames.push(f.to_i420().expect("readback"))
1607+ })
1608+ .expect("decode zero-copy stream");
1609+ decode_stream(&mut cpu_decoder, dec_codec, &cpu_stream, frames, |f| {
1610+ cpu_frames.push(f.to_i420().expect("readback"))
1611+ })
1612+ .expect("decode CPU-path stream");
1613+ assert_eq!(zc_frames.len(), frames, "{target:?}: zero-copy frame count");
1614+ assert_eq!(cpu_frames.len(), frames, "{target:?}: CPU-path frame count");
1615+
1616+ let widen = |v: &[u8]| -> Vec<u16> { v.iter().map(|&s| u16::from(s)).collect() };
1617+ let mut worst_y = f64::INFINITY;
1618+ let mut worst_uv = f64::INFINITY;
1619+ for (a, b) in zc_frames.iter().zip(&cpu_frames) {
1620+ let y_db = plane_psnr(&widen(&a.y), &widen(&b.y), 255.0);
1621+ let uv_db = plane_psnr(
1622+ &[widen(&a.u), widen(&a.v)].concat(),
1623+ &[widen(&b.u), widen(&b.v)].concat(),
1624+ 255.0,
1625+ );
1626+ worst_y = worst_y.min(y_db);
1627+ worst_uv = worst_uv.min(uv_db);
1628+ }
1629+ println!("{target:?} encode_frame (P010 via VPP) vs CPU path: worst Y {worst_y:.1} dB, worst UV {worst_uv:.1} dB");
1630+ assert!(worst_y >= 40.0, "{target:?}: worst Y {worst_y:.1} dB vs the CPU path");
1631+ assert!(worst_uv >= 40.0, "{target:?}: worst UV {worst_uv:.1} dB vs the CPU path");
1632+ }
1633+}
1634+
1635+/// Median ms/frame at 3840x2160 for `decode+encode_frame` (P010 zero-copy,
1636+/// through VPP) against `decode+to_i420+encode` (CPU path) — the number that
1637+/// says whether the VPP conversion is cheap enough to keep the zero-copy path
1638+/// meaningfully faster than reading the frame back to system memory.
1639+///
1640+/// Targets H.264: this film's HEVC SPS `pic_height_in_luma_samples` (2160)
1641+/// is not a multiple of this crate's HEVC encode block size (32), so
1642+/// `encode_frame` refuses the HEVC target on this file by documented design
1643+/// (see `encode_frame_accepts_p010_via_vpp`) — 2160 *is* a multiple of the
1644+/// H.264 block size (16), so H.264 is the target that actually exercises the
1645+/// zero-copy path on this real source.
1646+#[test]
1647+fn p010_zero_copy_encode_is_realtime() {
1648+ let Some(display) = display() else { return };
1649+ let path = Path::new(REELD);
1650+ if !path.exists() {
1651+ eprintln!("skipped: film not present");
1652+ return;
1653+ }
1654+ let limit = 60usize;
1655+ let Some(data) = elementary(path, Codec::H265, limit) else {
1656+ eprintln!("skipped: no elementary stream");
1657+ return;
1658+ };
1659+ let units = split_access_units(&data, Codec::H265);
1660+ assert!(units.len() > limit, "film clip too short for {limit} frames");
1661+
1662+ println!("{:<24} {:>10} {:>10} {:>10}", "stage", "median ms", "min ms", "max ms");
1663+ let mut zc_median = None;
1664+ let mut cpu_median = None;
1665+ for (name, zero_copy) in [("decode+encode_frame", true), ("decode+to_i420+encode", false)] {
1666+ let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens");
1667+ let mut encoder: Option<Encoder> = None;
1668+ let mut durations: Vec<Duration> = Vec::new();
1669+ let mut count = 0i64;
1670+ 'outer: for (i, unit) in units.iter().enumerate() {
1671+ decoder.decode(unit, i as i64).expect("decode");
1672+ while let Some(frame) = decoder.next_frame() {
1673+ let (w, h) = frame.display_size;
1674+ let encoder = encoder.get_or_insert_with(|| {
1675+ let mut cfg = EncoderConfig::new(EncCodec::H264, w, h);
1676+ cfg.gop_size = 30;
1677+ cfg.rate_control = RateControlMode::ConstantQp { qp: 22 };
1678+ Encoder::new(&display, cfg).expect("encoder opens")
1679+ });
1680+ let meta = FrameMetadata {
1681+ timestamp: count,
1682+ force_keyframe: false,
1683+ };
1684+ let t0 = Instant::now();
1685+ if zero_copy {
1686+ encoder.encode_frame(&frame, meta).expect("encode_frame");
1687+ } else {
1688+ let i420 = frame.to_i420().expect("readback");
1689+ encoder.encode(&i420, meta).expect("encode");
1690+ }
1691+ durations.push(t0.elapsed());
1692+ count += 1;
1693+ if durations.len() >= limit {
1694+ break 'outer;
1695+ }
1696+ }
1697+ }
1698+ assert!(
1699+ durations.len() >= limit,
1700+ "{name}: only {} of {limit} frames encoded",
1701+ durations.len()
1702+ );
1703+ durations.sort();
1704+ let ms = |d: Duration| d.as_secs_f64() * 1000.0;
1705+ let median = ms(durations[durations.len() / 2]);
1706+ let min = ms(durations[0]);
1707+ let max = ms(durations[durations.len() - 1]);
1708+ println!("{name:<24} {median:>10.2} {min:>10.2} {max:>10.2}");
1709+ if zero_copy {
1710+ zc_median = Some(median);
1711+ } else {
1712+ cpu_median = Some(median);
1713+ }
1714+ }
1715+ let zc = zc_median.expect("zero-copy phase ran");
1716+ let cpu = cpu_median.expect("CPU-path phase ran");
1717+ println!("target: decode+encode_frame <= 20.00 ms/frame median; got {zc:.2} (CPU path {cpu:.2})");
1718+ assert!(
1719+ zc <= 20.0,
1720+ "decode+encode_frame median {zc:.2} ms/frame exceeds the 20 ms budget (CPU path: {cpu:.2} ms)"
1721+ );
1722+}
1723+
1511 /// A CRA GOP boundary stores its RASL leading pictures *after* the CRA in1724 /// A CRA GOP boundary stores its RASL leading pictures *after* the CRA in
1512 /// decode order, even though they display before it (they predict from1725 /// decode order, even though they display before it (they predict from
1513 /// pictures the CRA's own random-access point does not have). A decoder1726 /// pictures the CRA's own random-access point does not have). A decoder
crates/ec-va/src/sys.rsmodified+18 -0
@@ -673,6 +673,24 @@ const _: () = {
673 673
674 assert!(size_of::<VARectangle>() == 8 && align_of::<VARectangle>() == 2);674 assert!(size_of::<VARectangle>() == 8 && align_of::<VARectangle>() == 2);
675 675
676+ assert!(
677+ size_of::<VAProcColorProperties>() == 8 && align_of::<VAProcColorProperties>() == 1
678+ );
679+
680+ // 64-bit `va_vpp.h:886`: 25 fields down to `output_hdr_metadata` (152
681+ // bytes on this target) plus `va_reserved[VA_PADDING_LARGE - 16]` (16
682+ // `u32`s, 64 bytes) = 224.
683+ assert!(
684+ size_of::<VAProcPipelineParameterBuffer>() == 224
685+ && align_of::<VAProcPipelineParameterBuffer>() == 8
686+ );
687+ assert!(offset_of!(VAProcPipelineParameterBuffer, surface_region) == 8);
688+ assert!(offset_of!(VAProcPipelineParameterBuffer, input_color_properties) == 132);
689+ assert!(offset_of!(VAProcPipelineParameterBuffer, output_color_properties) == 140);
690+ assert!(offset_of!(VAProcPipelineParameterBuffer, processing_mode) == 148);
691+ assert!(offset_of!(VAProcPipelineParameterBuffer, output_hdr_metadata) == 152);
692+ assert!(offset_of!(VAProcPipelineParameterBuffer, va_reserved) == 160);
693+
676 // Assumption 2: C `enum` and `unsigned int` are both 4 bytes here.694 // Assumption 2: C `enum` and `unsigned int` are both 4 bytes here.
677 assert!(size_of::<VAStatus>() == 4);695 assert!(size_of::<VAStatus>() == 4);
678 assert!(size_of::<VAGenericID>() == 4);696 assert!(size_of::<VAGenericID>() == 4);
crates/ec-va/tests/gpu.rsmodified+45 -0
@@ -477,6 +477,51 @@ fn context_keeps_its_dependencies_alive() {
477 assert!(display.is_valid());477 assert!(display.is_valid());
478 }478 }
479 479
480+// --------------------------------------------------------------------------
481+// VPP
482+// --------------------------------------------------------------------------
483+
484+/// `vaQueryVideoProcFilters` on the real display: whatever this driver
485+/// advertises, the call must succeed and return values `Vpp` can act on. Not
486+/// a claim about which filters exist — radeonsi's list is print-only here —
487+/// just that the query path works end to end.
488+#[test]
489+fn vpp_filters_probe() {
490+ let _serial = serial();
491+ let display = display();
492+ let targets = Surface::create_pool(&display, &SurfaceSpec::nv12(1920, 1080), 1).expect("pool");
493+ let vpp = ec_va::Vpp::new(&display, sys::VA_RT_FORMAT_YUV420, &targets).expect("vpp opens");
494+ let filters = vpp.filters().expect("vaQueryVideoProcFilters");
495+ println!("radeonsi VAProcFilterType list: {filters:?}");
496+ println!(
497+ "HighDynamicRangeToneMapping (8) advertised: {}",
498+ filters.contains(&sys::VAProcFilterHighDynamicRangeToneMapping)
499+ );
500+}
501+
502+/// `Vpp::convert` end to end: a P010 source surface into an NV12 destination,
503+/// no readback — the conversion `ec-hw`'s `encode_frame` relies on for a
504+/// 10-bit source.
505+#[test]
506+fn vpp_converts_p010_to_nv12() {
507+ let _serial = serial();
508+ let display = display();
509+ let src_spec = SurfaceSpec::p010(64, 64);
510+ let source = Surface::create(&display, &src_spec).expect("p010 source surface");
511+ let dest_targets = Surface::create_pool(&display, &SurfaceSpec::nv12(64, 64), 1).expect("pool");
512+ let vpp = ec_va::Vpp::new(
513+ &display,
514+ sys::VA_RT_FORMAT_YUV420 | sys::VA_RT_FORMAT_YUV420_10,
515+ &dest_targets,
516+ )
517+ .expect("vpp opens");
518+ let dest = vpp
519+ .convert(&source, Arc::clone(&dest_targets[0]))
520+ .expect("convert");
521+ assert_eq!(dest.id(), dest_targets[0].id());
522+ assert!(display.is_valid());
523+}
524+
480 // --------------------------------------------------------------------------525 // --------------------------------------------------------------------------
481 // helpers526 // helpers
482 // --------------------------------------------------------------------------527 // --------------------------------------------------------------------------