U16: remove VA encoder submit_prime, encode_frame, encode_surface and the VPP pipeline they alone used, Encoder::profile, Frame::display; drop their gpu.rs tests
Browse files at this commit2b83b5058d7d9d126a44f6306dedfaa810ce7f2fParent: b14b4933 changed files +2 -504
- crates/ec-hw/src/enc/mod.rs +2 -235
- crates/ec-hw/src/frame.rs +0 -5
- crates/ec-hw/tests/gpu.rs +0 -264
crates/ec-hw/src/enc/mod.rsmodified+2 -235
| @@ -48,10 +48,10 @@ use ec_va::va::{ | |||
| 48 | VA_FOURCC_NV12, VA_FOURCC_P010, VA_RT_FORMAT_YUV420, VA_RT_FORMAT_YUV420_10, | 48 | VA_FOURCC_NV12, VA_FOURCC_P010, VA_RT_FORMAT_YUV420, VA_RT_FORMAT_YUV420_10, |
| 49 | VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER, VAConfigAttribType, VAImage, | 49 | VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER, VAConfigAttribType, VAImage, |
| 50 | }; | 50 | }; |
| 51 | -use ec_va::{CapReport, Config, ConfigAttrib, Context, Display, Surface, SurfaceSpec, Vpp}; | 51 | +use ec_va::{CapReport, Config, ConfigAttrib, Context, Display, Surface, SurfaceSpec}; |
| 52 | 52 | ||
| 53 | use crate::error::{Error, Result}; | 53 | use crate::error::{Error, Result}; |
| 54 | -use crate::frame::{Frame, I420, I420_16}; | 54 | +use crate::frame::{I420, I420_16}; |
| 55 | use crate::pool::{PooledSurface, SurfacePool}; | 55 | use crate::pool::{PooledSurface, SurfacePool}; |
| 56 | use crate::rate::{RATE_WINDOW_MS, RateFeedback, hrd_buffer_ms}; | 56 | use crate::rate::{RATE_WINDOW_MS, RateFeedback, hrd_buffer_ms}; |
| 57 | 57 | ||
| @@ -128,17 +128,6 @@ pub struct Encoder { | |||
| 128 | /// Set when the rate control parameters have to be resent. | 128 | /// Set when the rate control parameters have to be resent. |
| 129 | tunings_dirty: bool, | 129 | tunings_dirty: bool, |
| 130 | rc: RateFeedback, | 130 | rc: RateFeedback, |
| 131 | - profile: Profile, | ||
| 132 | - /// P010 -> NV12 GPU-resident conversion pipeline for [`Encoder::encode_frame`] | ||
| 133 | - /// on a source that is not the encoder's own format or size; built lazily, | ||
| 134 | - /// since most encoders never see one. | ||
| 135 | - vpp: Option<Vpp>, | ||
| 136 | - /// The small pool [`Encoder::vpp`] converts into, round-robined rather | ||
| 137 | - /// than pool-acquired: each conversion is fully synced before this | ||
| 138 | - /// encoder's own picture submission returns, so two surfaces are enough | ||
| 139 | - /// to never hand out one still in flight. | ||
| 140 | - vpp_surfaces: Vec<Rc<Surface>>, | ||
| 141 | - vpp_next: usize, | ||
| 142 | /// Pictures submitted by [`Encoder::submit`], oldest first, not yet read back. | 131 | /// Pictures submitted by [`Encoder::submit`], oldest first, not yet read back. |
| 143 | pending: VecDeque<InFlight<()>>, | 132 | pending: VecDeque<InFlight<()>>, |
| 144 | } | 133 | } |
| @@ -263,10 +252,6 @@ impl Encoder { | |||
| 263 | idr_count: 0, | 252 | idr_count: 0, |
| 264 | tunings_dirty: true, | 253 | tunings_dirty: true, |
| 265 | rc: RateFeedback::default(), | 254 | rc: RateFeedback::default(), |
| 266 | - profile, | ||
| 267 | - vpp: None, | ||
| 268 | - vpp_surfaces: Vec::new(), | ||
| 269 | - vpp_next: 0, | ||
| 270 | pending: VecDeque::new(), | 255 | pending: VecDeque::new(), |
| 271 | }) | 256 | }) |
| 272 | } | 257 | } |
| @@ -368,36 +353,6 @@ impl Encoder { | |||
| 368 | } | 353 | } |
| 369 | } | 354 | } |
| 370 | 355 | ||
| 371 | - /// Pipelined encode of an imported surface: a GPU copy into a pool surface | ||
| 372 | - /// whose padding is then zeroed, matching the CPU upload path. A surface of | ||
| 373 | - /// the coded size is copied whole; any other size is scaled to the | ||
| 374 | - /// configured width x height, so one capture can feed several layers. | ||
| 375 | - pub fn submit_prime( | ||
| 376 | - &mut self, | ||
| 377 | - surface: Rc<ec_va::va::Surface<ec_va::surface::PrimeImport>>, | ||
| 378 | - meta: FrameMetadata, | ||
| 379 | - ) -> Result<Option<CodedFrame>> { | ||
| 380 | - let input = self | ||
| 381 | - .pool | ||
| 382 | - .acquire() | ||
| 383 | - .ok_or_else(|| Error::config("submit_prime: every surface is still in use"))?; | ||
| 384 | - let dest = if surface.size() == self.coded_size { | ||
| 385 | - (0, 0, 0, 0) | ||
| 386 | - } else { | ||
| 387 | - (0, 0, self.config.width, self.config.height) | ||
| 388 | - }; | ||
| 389 | - self.vpp()? | ||
| 390 | - .convert_region(&surface, (0, 0, 0, 0), Rc::clone(input.surface()), dest)?; | ||
| 391 | - zero_padding( | ||
| 392 | - self.context.display(), | ||
| 393 | - input.surface(), | ||
| 394 | - self.config.bit_depth, | ||
| 395 | - (self.config.width, self.config.height), | ||
| 396 | - self.coded_size, | ||
| 397 | - )?; | ||
| 398 | - self.push_pipelined(input, meta) | ||
| 399 | - } | ||
| 400 | - | ||
| 401 | fn retire_oldest(&mut self) -> Result<Option<CodedFrame>> { | 356 | fn retire_oldest(&mut self) -> Result<Option<CodedFrame>> { |
| 402 | match self.pending.pop_front() { | 357 | match self.pending.pop_front() { |
| 403 | Some(f) => self.finish(f).map(Some), | 358 | Some(f) => self.finish(f).map(Some), |
| @@ -430,72 +385,6 @@ impl Encoder { | |||
| 430 | self.encode_settled(Rc::clone(input.surface()), meta) | 385 | self.encode_settled(Rc::clone(input.surface()), meta) |
| 431 | } | 386 | } |
| 432 | 387 | ||
| 433 | - /// Encode a decoded frame's GPU surface directly: no `to_i420` read-back, | ||
| 434 | - /// no re-upload, just the decoder's picture handed straight to the | ||
| 435 | - /// encoder as its source. This is the path a decode-then-encode pipeline | ||
| 436 | - /// (e.g. a transcode) wants — the alternative is a full frame through | ||
| 437 | - /// system memory and back for no reason. | ||
| 438 | - /// | ||
| 439 | - /// A source whose size differs from the encoder's coded size, or whose | ||
| 440 | - /// depth differs from the encoder's, goes through the GPU video-processing | ||
| 441 | - /// pipeline into an NV12 surface of the encoder's size first. | ||
| 442 | - /// | ||
| 443 | - /// Refused, rather than silently working around, when: | ||
| 444 | - /// - `frame` was decoded on a different [`ec_va::Display`]: a surface id | ||
| 445 | - /// only means anything on the display that created it. | ||
| 446 | - /// - `frame`'s bit depth is neither 8 nor 10. | ||
| 447 | - pub fn encode_frame(&mut self, frame: &Frame, meta: FrameMetadata) -> Result<CodedFrame> { | ||
| 448 | - if !self.pending.is_empty() { | ||
| 449 | - return Err(Error::config( | ||
| 450 | - "encode_frame: pipelined frames pending; call flush first", | ||
| 451 | - )); | ||
| 452 | - } | ||
| 453 | - if frame.bit_depth != 8 && frame.bit_depth != 10 { | ||
| 454 | - return Err(Error::unsupported( | ||
| 455 | - format!("encode_frame of a {}-bit source", frame.bit_depth), | ||
| 456 | - "the VPP conversion this crate builds reads 8-bit NV12 and 10-bit P010 only", | ||
| 457 | - )); | ||
| 458 | - } | ||
| 459 | - if !Rc::ptr_eq(frame.display(), self.context.display()) { | ||
| 460 | - return Err(Error::config( | ||
| 461 | - "encode_frame: the frame was decoded on a different Display than this encoder", | ||
| 462 | - )); | ||
| 463 | - } | ||
| 464 | - let source = | ||
| 465 | - if frame.bit_depth != self.config.bit_depth || frame.coded_size != self.coded_size { | ||
| 466 | - self.vpp_into_pool(frame.surface(), frame.display_size)? | ||
| 467 | - } else { | ||
| 468 | - Rc::clone(frame.surface()) | ||
| 469 | - }; | ||
| 470 | - self.encode_settled(source, meta) | ||
| 471 | - } | ||
| 472 | - | ||
| 473 | - /// Encode a surface imported with [`ec_va::Surface::import_prime`] or | ||
| 474 | - /// decoded elsewhere on this encoder's display: read by the GPU in place. | ||
| 475 | - /// | ||
| 476 | - /// The surface must be NV12, since H.264 is 8-bit only. A surface of another | ||
| 477 | - /// size goes through the same GPU conversion as [`Encoder::encode_frame`]; | ||
| 478 | - /// `display_size` is the picture's visible size (its cropped size, when the | ||
| 479 | - /// surface is padded to the codec's block size). | ||
| 480 | - pub fn encode_surface<D: SurfaceMemoryDescriptor>( | ||
| 481 | - &mut self, | ||
| 482 | - surface: &Rc<ec_va::va::Surface<D>>, | ||
| 483 | - display_size: (u32, u32), | ||
| 484 | - meta: FrameMetadata, | ||
| 485 | - ) -> Result<CodedFrame> { | ||
| 486 | - if !self.pending.is_empty() { | ||
| 487 | - return Err(Error::config( | ||
| 488 | - "encode_surface: pipelined frames pending; call flush first", | ||
| 489 | - )); | ||
| 490 | - } | ||
| 491 | - if surface.size() != self.coded_size { | ||
| 492 | - let converted = self.vpp_into_pool(surface, display_size)?; | ||
| 493 | - self.encode_settled(converted, meta) | ||
| 494 | - } else { | ||
| 495 | - self.encode_settled(Rc::clone(surface), meta) | ||
| 496 | - } | ||
| 497 | - } | ||
| 498 | - | ||
| 499 | fn check_size(&self, width: u32, height: u32) -> Result<()> { | 388 | fn check_size(&self, width: u32, height: u32) -> Result<()> { |
| 500 | if width != self.config.width || height != self.config.height { | 389 | if width != self.config.width || height != self.config.height { |
| 501 | return Err(Error::config(format!( | 390 | return Err(Error::config(format!( |
| @@ -506,50 +395,6 @@ impl Encoder { | |||
| 506 | Ok(()) | 395 | Ok(()) |
| 507 | } | 396 | } |
| 508 | 397 | ||
| 509 | - /// Place a source surface's visible rect into this encoder's coded size, | ||
| 510 | - /// in NV12, on the GPU: a whole-surface convert when the source is | ||
| 511 | - /// already the encoder's coded size, a region copy/convert otherwise. | ||
| 512 | - /// Builds the VPP pipeline and its two-surface pool on first use, painting | ||
| 513 | - /// the pool black so any padding beyond `display_size` a region copy never | ||
| 514 | - /// touches is deterministic. | ||
| 515 | - fn vpp(&mut self) -> Result<&Vpp> { | ||
| 516 | - if self.vpp.is_none() { | ||
| 517 | - let spec = surface_spec(self.config.bit_depth, self.coded_size); | ||
| 518 | - let display = Rc::clone(self.context.display()); | ||
| 519 | - let surfaces = ec_va::create_pool(&display, &spec, 2)?; | ||
| 520 | - for s in &surfaces { | ||
| 521 | - fill_black(&display, s, self.coded_size, self.config.bit_depth)?; | ||
| 522 | - } | ||
| 523 | - let vpp = Vpp::new( | ||
| 524 | - &display, | ||
| 525 | - VA_RT_FORMAT_YUV420 | VA_RT_FORMAT_YUV420_10, | ||
| 526 | - self.coded_size.0, | ||
| 527 | - self.coded_size.1, | ||
| 528 | - )?; | ||
| 529 | - self.vpp_surfaces = surfaces.into_iter().map(Rc::new).collect(); | ||
| 530 | - self.vpp = Some(vpp); | ||
| 531 | - } | ||
| 532 | - Ok(self.vpp.as_ref().expect("just built above if it was None")) | ||
| 533 | - } | ||
| 534 | - | ||
| 535 | - fn vpp_into_pool<D: SurfaceMemoryDescriptor>( | ||
| 536 | - &mut self, | ||
| 537 | - source: &ec_va::va::Surface<D>, | ||
| 538 | - display_size: (u32, u32), | ||
| 539 | - ) -> Result<Rc<Surface>> { | ||
| 540 | - self.vpp()?; | ||
| 541 | - let dest = Rc::clone(&self.vpp_surfaces[self.vpp_next]); | ||
| 542 | - self.vpp_next = (self.vpp_next + 1) % self.vpp_surfaces.len(); | ||
| 543 | - let whole = display_size == self.coded_size; | ||
| 544 | - let vpp = self.vpp()?; | ||
| 545 | - if whole { | ||
| 546 | - Ok(vpp.convert(source, dest)?) | ||
| 547 | - } else { | ||
| 548 | - let rect = (0, 0, display_size.0, display_size.1); | ||
| 549 | - Ok(vpp.convert_region(source, rect, dest, rect)?) | ||
| 550 | - } | ||
| 551 | - } | ||
| 552 | - | ||
| 553 | /// Shared synchronous path for [`Encoder::encode`], [`Encoder::encode_frame`] | 398 | /// Shared synchronous path for [`Encoder::encode`], [`Encoder::encode_frame`] |
| 554 | /// and [`Encoder::encode_surface`]: submit, then read back at once. | 399 | /// and [`Encoder::encode_surface`]: submit, then read back at once. |
| 555 | fn encode_settled<D: SurfaceMemoryDescriptor>( | 400 | fn encode_settled<D: SurfaceMemoryDescriptor>( |
| @@ -780,10 +625,6 @@ impl Encoder { | |||
| 780 | self.idr_count | 625 | self.idr_count |
| 781 | } | 626 | } |
| 782 | 627 | ||
| 783 | - /// The VA profile this encoder was configured with. | ||
| 784 | - pub fn profile(&self) -> Profile { | ||
| 785 | - self.profile | ||
| 786 | - } | ||
| 787 | } | 628 | } |
| 788 | 629 | ||
| 789 | /// The surface format an encoder of `bit_depth` works in. | 630 | /// The surface format an encoder of `bit_depth` works in. |
| @@ -864,86 +705,12 @@ fn upload(context: &Rc<Context>, frame: &I420, surface: &Surface, coded: (u32, u | |||
| 864 | }) | 705 | }) |
| 865 | } | 706 | } |
| 866 | 707 | ||
| 867 | -/// Paint an NV12 surface black (Y=16, chroma=128, TV-range neutral): the | ||
| 868 | -/// one-time initialisation [`Encoder::vpp_into_pool`] gives its pool so a | ||
| 869 | -/// region copy's untouched padding reads the same deterministic value on | ||
| 870 | -/// every driver instead of whatever the fresh allocation contained. | ||
| 871 | -fn fill_black(display: &Display, surface: &Surface, size: (u32, u32), bit_depth: u8) -> Result<()> { | ||
| 872 | - write_surface(display, surface, fourcc(bit_depth), size, |layout, data| { | ||
| 873 | - // P010 carries the sample in the top ten bits, so black is the 8-bit | ||
| 874 | - // value shifted up by six, little endian. | ||
| 875 | - let samples = |eight: u8| -> Vec<u8> { | ||
| 876 | - if bit_depth == 8 { | ||
| 877 | - vec![eight] | ||
| 878 | - } else { | ||
| 879 | - (u16::from(eight) << 6).to_le_bytes().to_vec() | ||
| 880 | - } | ||
| 881 | - }; | ||
| 882 | - for (plane, value, rows) in [ | ||
| 883 | - (0usize, 16u8, size.1 as usize), | ||
| 884 | - (1, 128, size.1 as usize / 2), | ||
| 885 | - ] { | ||
| 886 | - let pitch = layout.pitches[plane] as usize; | ||
| 887 | - let base = layout.offsets[plane] as usize; | ||
| 888 | - let unit = samples(value); | ||
| 889 | - let width = size.0 as usize * unit.len(); | ||
| 890 | - for row in 0..rows { | ||
| 891 | - let start = base + row * pitch; | ||
| 892 | - if let Some(line) = data.get_mut(start..start + width) { | ||
| 893 | - for px in line.chunks_exact_mut(unit.len()) { | ||
| 894 | - px.copy_from_slice(&unit); | ||
| 895 | - } | ||
| 896 | - } | ||
| 897 | - } | ||
| 898 | - } | ||
| 899 | - }) | ||
| 900 | -} | ||
| 901 | - | ||
| 902 | fn fill_zero(display: &Display, surface: &Surface, size: (u32, u32), bit_depth: u8) -> Result<()> { | 708 | fn fill_zero(display: &Display, surface: &Surface, size: (u32, u32), bit_depth: u8) -> Result<()> { |
| 903 | write_surface(display, surface, fourcc(bit_depth), size, |_, data| { | 709 | write_surface(display, surface, fourcc(bit_depth), size, |_, data| { |
| 904 | data.fill(0) | 710 | data.fill(0) |
| 905 | }) | 711 | }) |
| 906 | } | 712 | } |
| 907 | 713 | ||
| 908 | -fn zero_padding<D: SurfaceMemoryDescriptor>( | ||
| 909 | - display: &Display, | ||
| 910 | - surface: &ec_va::va::Surface<D>, | ||
| 911 | - bit_depth: u8, | ||
| 912 | - (w, h): (u32, u32), | ||
| 913 | - coded: (u32, u32), | ||
| 914 | -) -> Result<()> { | ||
| 915 | - if (w, h) == coded { | ||
| 916 | - return Ok(()); | ||
| 917 | - } | ||
| 918 | - let (fourcc, px) = if bit_depth > 8 { | ||
| 919 | - (VA_FOURCC_P010, 2) | ||
| 920 | - } else { | ||
| 921 | - (VA_FOURCC_NV12, 1) | ||
| 922 | - }; | ||
| 923 | - let (w, h, cw, ch) = ( | ||
| 924 | - w as usize * px, | ||
| 925 | - h as usize, | ||
| 926 | - coded.0 as usize * px, | ||
| 927 | - coded.1 as usize, | ||
| 928 | - ); | ||
| 929 | - write_surface(display, surface, fourcc, coded, |layout, data| { | ||
| 930 | - for (plane, rows, last, start) in [ | ||
| 931 | - (0, h, ch, w), | ||
| 932 | - (1, h.div_ceil(2), ch / 2, w.next_multiple_of(2 * px)), | ||
| 933 | - ] { | ||
| 934 | - let pitch = layout.pitches[plane] as usize; | ||
| 935 | - let base = layout.offsets[plane] as usize; | ||
| 936 | - for row in 0..last { | ||
| 937 | - let from = if row < rows { start } else { 0 }; | ||
| 938 | - if let Some(dst) = data.get_mut(base + row * pitch + from..base + row * pitch + cw) | ||
| 939 | - { | ||
| 940 | - dst.fill(0); | ||
| 941 | - } | ||
| 942 | - } | ||
| 943 | - } | ||
| 944 | - }) | ||
| 945 | -} | ||
| 946 | - | ||
| 947 | /// Upload one 10-bit frame into a surface as P010: samples move from the low | 714 | /// Upload one 10-bit frame into a surface as P010: samples move from the low |
| 948 | /// ten bits of the caller's `u16` into P010's top ten. | 715 | /// ten bits of the caller's `u16` into P010's top ten. |
| 949 | fn upload_16( | 716 | fn upload_16( |
crates/ec-hw/src/frame.rsmodified+0 -5
| @@ -72,11 +72,6 @@ impl Frame { | |||
| 72 | &self.surface | 72 | &self.surface |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | - /// The display the surface was created on. | ||
| 76 | - pub(crate) fn display(&self) -> &Rc<Display> { | ||
| 77 | - &self.display | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | /// This frame's colour metadata, when the codec's decoder resolves one. | 75 | /// This frame's colour metadata, when the codec's decoder resolves one. |
| 81 | pub fn colour(&self) -> Option<Colour> { | 76 | pub fn colour(&self) -> Option<Colour> { |
| 82 | self.colour | 77 | self.colour |
crates/ec-hw/tests/gpu.rsmodified+0 -264
| @@ -761,94 +761,6 @@ fn our_encoder_and_our_decoder_agree() { | |||
| 761 | /// rounding both land on the same 1920x1088 coded size for it, so the | 761 | /// rounding both land on the same 1920x1088 coded size for it, so the |
| 762 | /// decoder's surfaces satisfy `encode_frame`'s coded-size check against | 762 | /// decoder's surfaces satisfy `encode_frame`'s coded-size check against |
| 763 | /// either target codec without a second fixture. | 763 | /// either target codec without a second fixture. |
| 764 | -#[test] | ||
| 765 | -fn encode_frame_zero_copy_matches_the_cpu_path() { | ||
| 766 | - let Some(display) = display() else { return }; | ||
| 767 | - let (width, height) = (1920u32, 1080u32); | ||
| 768 | - let path = ec_testkit::fixture("bitstreams/h264-1080p-23.976-8bit.264"); | ||
| 769 | - let data = std::fs::read(&path).expect("fixture is readable"); | ||
| 770 | - let frames = 10usize; | ||
| 771 | - | ||
| 772 | - for (target, dec_codec) in [(EncCodec::H264, Codec::H264), (EncCodec::H265, Codec::H265)] { | ||
| 773 | - let mut cfg = EncoderConfig::new(target, width, height); | ||
| 774 | - cfg.gop_size = 30; | ||
| 775 | - cfg.rate_control = RateControlMode::ConstantQp { qp: 22 }; | ||
| 776 | - let mut zero_copy = Encoder::new(&display, cfg).expect("zero-copy encoder opens"); | ||
| 777 | - let mut cpu_path = Encoder::new(&display, cfg).expect("cpu-path encoder opens"); | ||
| 778 | - let mut zc_stream = Vec::new(); | ||
| 779 | - let mut cpu_stream = Vec::new(); | ||
| 780 | - let mut count = 0i64; | ||
| 781 | - | ||
| 782 | - let mut decoder = Decoder::new(&display, Codec::H264).expect("decoder opens"); | ||
| 783 | - decode_stream(&mut decoder, Codec::H264, &data, frames, |frame| { | ||
| 784 | - let meta = FrameMetadata { | ||
| 785 | - timestamp: count, | ||
| 786 | - force_keyframe: false, | ||
| 787 | - }; | ||
| 788 | - // The zero-copy path never reads the surface back to the CPU: the | ||
| 789 | - // only `to_i420` call in this loop is the CPU-path baseline's. | ||
| 790 | - let zc = zero_copy | ||
| 791 | - .encode_frame(&frame, meta) | ||
| 792 | - .expect("encode_frame (zero copy)"); | ||
| 793 | - zc_stream.extend_from_slice(&zc.data); | ||
| 794 | - let i420 = frame.to_i420().expect("readback for the CPU-path baseline"); | ||
| 795 | - let cpu = cpu_path.encode(&i420, meta).expect("encode (CPU path)"); | ||
| 796 | - cpu_stream.extend_from_slice(&cpu.data); | ||
| 797 | - count += 1; | ||
| 798 | - }) | ||
| 799 | - .expect("decode"); | ||
| 800 | - assert_eq!( | ||
| 801 | - count as usize, frames, | ||
| 802 | - "{target:?}: decoded {count} of {frames}" | ||
| 803 | - ); | ||
| 804 | - | ||
| 805 | - let ext = match target { | ||
| 806 | - EncCodec::H264 => "264", | ||
| 807 | - EncCodec::H265 => "265", | ||
| 808 | - EncCodec::Av1 => unreachable!("AV1 is not in this test's target list"), | ||
| 809 | - }; | ||
| 810 | - let dir = std::env::temp_dir().join("ec-hw-encode-frame-zero-copy"); | ||
| 811 | - std::fs::create_dir_all(&dir).expect("temp dir"); | ||
| 812 | - for (name, bytes) in [("zc", &zc_stream), ("cpu", &cpu_stream)] { | ||
| 813 | - std::fs::write(dir.join(format!("{name}.{ext}")), bytes).expect("write bitstream"); | ||
| 814 | - } | ||
| 815 | - | ||
| 816 | - let mut zc_decoder = Decoder::new(&display, dec_codec).expect("zc decoder opens"); | ||
| 817 | - let mut cpu_decoder = Decoder::new(&display, dec_codec).expect("cpu decoder opens"); | ||
| 818 | - let mut zc_frames = Vec::new(); | ||
| 819 | - let mut cpu_frames = Vec::new(); | ||
| 820 | - decode_stream(&mut zc_decoder, dec_codec, &zc_stream, frames, |f| { | ||
| 821 | - zc_frames.push(Planes::from_8bit(&f.to_i420().expect("readback"))) | ||
| 822 | - }) | ||
| 823 | - .expect("decode zero-copy stream"); | ||
| 824 | - decode_stream(&mut cpu_decoder, dec_codec, &cpu_stream, frames, |f| { | ||
| 825 | - cpu_frames.push(Planes::from_8bit(&f.to_i420().expect("readback"))) | ||
| 826 | - }) | ||
| 827 | - .expect("decode CPU-path stream"); | ||
| 828 | - assert_eq!( | ||
| 829 | - zc_frames.len(), | ||
| 830 | - frames, | ||
| 831 | - "{target:?}: zero-copy stream frame count" | ||
| 832 | - ); | ||
| 833 | - assert_eq!( | ||
| 834 | - cpu_frames.len(), | ||
| 835 | - frames, | ||
| 836 | - "{target:?}: CPU-path stream frame count" | ||
| 837 | - ); | ||
| 838 | - | ||
| 839 | - let worst = zc_frames | ||
| 840 | - .iter() | ||
| 841 | - .zip(&cpu_frames) | ||
| 842 | - .map(|(a, b)| psnr(a, b)) | ||
| 843 | - .fold(f64::INFINITY, f64::min); | ||
| 844 | - println!("{target:?} encode_frame vs CPU path: worst {worst:.1} dB"); | ||
| 845 | - assert!( | ||
| 846 | - worst >= 35.0, | ||
| 847 | - "{target:?}: worst {worst:.1} dB vs the CPU path" | ||
| 848 | - ); | ||
| 849 | - } | ||
| 850 | -} | ||
| 851 | - | ||
| 852 | /// `EncoderConfig::colour` reaches the VUI of both codecs' packed SPS: an | 764 | /// `EncoderConfig::colour` reaches the VUI of both codecs' packed SPS: an |
| 853 | /// export with a colour description survives ffprobe reading it back, and | 765 | /// export with a colour description survives ffprobe reading it back, and |
| 854 | /// leaving it unset keeps today's behaviour (no `video_signal_type`, | 766 | /// leaving it unset keeps today's behaviour (no `video_signal_type`, |
| @@ -2617,93 +2529,8 @@ fn cbr_lands_within_five_percent_of_the_request() { | |||
| 2617 | assert!(failures.is_empty(), "outside +/-5%: {failures:?}"); | 2529 | assert!(failures.is_empty(), "outside +/-5%: {failures:?}"); |
| 2618 | } | 2530 | } |
| 2619 | 2531 | ||
| 2620 | -fn imported_nv12( | ||
| 2621 | - display: &Rc<Display>, | ||
| 2622 | - frame: &I420, | ||
| 2623 | -) -> Rc<ec_va::va::Surface<ec_va::surface::PrimeImport>> { | ||
| 2624 | - use ec_va::SurfaceExt; | ||
| 2625 | - | ||
| 2626 | - let (w, h) = (frame.width, frame.height); | ||
| 2627 | - let mut pool = | ||
| 2628 | - ec_va::create_pool(display, &ec_va::SurfaceSpec::nv12(w, h), 1).expect("surface"); | ||
| 2629 | - let surface = pool.pop().expect("one surface"); | ||
| 2630 | - { | ||
| 2631 | - let format = | ||
| 2632 | - ec_va::surface::image_format(display, ec_va::va::VA_FOURCC_NV12).expect("format"); | ||
| 2633 | - let mut image = | ||
| 2634 | - ec_va::surface::create_image(&surface, format, (w, h), (w, h)).expect("image"); | ||
| 2635 | - let layout = *image.image(); | ||
| 2636 | - let data = AsMut::<[u8]>::as_mut(&mut image); | ||
| 2637 | - let (wu, hu) = (w as usize, h as usize); | ||
| 2638 | - let (cw, ch) = (wu.div_ceil(2), hu.div_ceil(2)); | ||
| 2639 | - for row in 0..hu { | ||
| 2640 | - let d = layout.offsets[0] as usize + row * layout.pitches[0] as usize; | ||
| 2641 | - data[d..d + wu].copy_from_slice(&frame.y[row * wu..row * wu + wu]); | ||
| 2642 | - } | ||
| 2643 | - for row in 0..ch { | ||
| 2644 | - let d = layout.offsets[1] as usize + row * layout.pitches[1] as usize; | ||
| 2645 | - for col in 0..cw { | ||
| 2646 | - data[d + col * 2] = frame.u[row * cw + col]; | ||
| 2647 | - data[d + col * 2 + 1] = frame.v[row * cw + col]; | ||
| 2648 | - } | ||
| 2649 | - } | ||
| 2650 | - } | ||
| 2651 | - let prime = surface.export().expect("export"); | ||
| 2652 | - let objects: Vec<ec_va::RawPrimeObject> = prime | ||
| 2653 | - .objects | ||
| 2654 | - .iter() | ||
| 2655 | - .map(|o| ec_va::RawPrimeObject { | ||
| 2656 | - fd: std::os::fd::AsRawFd::as_raw_fd(&o.fd), | ||
| 2657 | - size: o.size, | ||
| 2658 | - modifier: o.modifier, | ||
| 2659 | - }) | ||
| 2660 | - .collect(); | ||
| 2661 | - let imported = ec_va::import_prime_raw( | ||
| 2662 | - display, | ||
| 2663 | - prime.fourcc, | ||
| 2664 | - (prime.width, prime.height), | ||
| 2665 | - &objects, | ||
| 2666 | - &prime.layers, | ||
| 2667 | - ) | ||
| 2668 | - .expect("import"); | ||
| 2669 | - drop(prime); | ||
| 2670 | - Rc::new(imported) | ||
| 2671 | -} | ||
| 2672 | - | ||
| 2673 | /// Encoding an imported DMA-BUF must give the bytes the CPU path gives for the | 2532 | /// Encoding an imported DMA-BUF must give the bytes the CPU path gives for the |
| 2674 | /// same pixels: the import is only a different way of handing over the source. | 2533 | /// same pixels: the import is only a different way of handing over the source. |
| 2675 | -#[test] | ||
| 2676 | -fn encode_surface_of_an_imported_dma_buf_matches_the_cpu_path() { | ||
| 2677 | - let Some(display) = display() else { return }; | ||
| 2678 | - let (w, h) = (640u32, 480u32); | ||
| 2679 | - for codec in [EncCodec::H264, EncCodec::H265] { | ||
| 2680 | - let mut config = EncoderConfig::new(codec, w, h); | ||
| 2681 | - config.gop_size = 3; | ||
| 2682 | - config.rate_control = RateControlMode::ConstantQp { qp: 30 }; | ||
| 2683 | - let mut cpu = Encoder::new(&display, config).expect("cpu encoder"); | ||
| 2684 | - let mut dma = Encoder::new(&display, config).expect("dma encoder"); | ||
| 2685 | - for t in 0..4u32 { | ||
| 2686 | - let frame = source_frame(w, h, t); | ||
| 2687 | - let meta = FrameMetadata { | ||
| 2688 | - timestamp: i64::from(t), | ||
| 2689 | - force_keyframe: false, | ||
| 2690 | - }; | ||
| 2691 | - let want = cpu.encode(&frame, meta).expect("cpu encode"); | ||
| 2692 | - let surface = imported_nv12(&display, &frame); | ||
| 2693 | - let got = dma | ||
| 2694 | - .encode_surface(&surface, (w, h), meta) | ||
| 2695 | - .expect("encode_surface"); | ||
| 2696 | - assert_eq!(got.is_keyframe, want.is_keyframe, "{codec:?} frame {t}"); | ||
| 2697 | - assert_eq!( | ||
| 2698 | - got.data.len(), | ||
| 2699 | - want.data.len(), | ||
| 2700 | - "{codec:?} frame {t}: imported source coded to a different size" | ||
| 2701 | - ); | ||
| 2702 | - assert!(got.data == want.data, "{codec:?} frame {t}: bytes differ"); | ||
| 2703 | - } | ||
| 2704 | - } | ||
| 2705 | -} | ||
| 2706 | - | ||
| 2707 | // --------------------------------------------------------------------------- | 2534 | // --------------------------------------------------------------------------- |
| 2708 | // Broken streams: an inter picture whose references were never decoded | 2535 | // Broken streams: an inter picture whose references were never decoded |
| 2709 | // --------------------------------------------------------------------------- | 2536 | // --------------------------------------------------------------------------- |
| @@ -2840,54 +2667,6 @@ fn a_broken_gop_is_refused_on_every_codec() { | |||
| 2840 | } | 2667 | } |
| 2841 | } | 2668 | } |
| 2842 | 2669 | ||
| 2843 | -#[test] | ||
| 2844 | -fn ten_bit_prime_submit_matches_the_cpu_path() { | ||
| 2845 | - let file = ec_testkit::fixture("derived/sintel720-x265-main10.mp4"); | ||
| 2846 | - let Some(display) = display() else { return }; | ||
| 2847 | - let data = elementary(&file, Codec::H265, 1).expect("elementary stream"); | ||
| 2848 | - let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens"); | ||
| 2849 | - let mut size = (0, 0); | ||
| 2850 | - decode_stream(&mut decoder, Codec::H265, &data, 1, |f| { | ||
| 2851 | - size = f.display_size | ||
| 2852 | - }) | ||
| 2853 | - .expect("decode"); | ||
| 2854 | - let mut cfg = EncoderConfig::new(EncCodec::H265, size.0, size.1).ten_bit(); | ||
| 2855 | - cfg.rate_control = RateControlMode::ConstantQp { qp: 22 }; | ||
| 2856 | - let mut zc = Encoder::new(&display, cfg).expect("encoder"); | ||
| 2857 | - let mut cpu = Encoder::new(&display, cfg).expect("encoder"); | ||
| 2858 | - let (mut a, mut b) = (Vec::new(), Vec::new()); | ||
| 2859 | - let mut t = 0; | ||
| 2860 | - let mut decoder = Decoder::new(&display, Codec::H265).expect("decoder opens"); | ||
| 2861 | - decode_stream(&mut decoder, Codec::H265, &data, 8, |frame| { | ||
| 2862 | - let meta = FrameMetadata { | ||
| 2863 | - timestamp: t, | ||
| 2864 | - force_keyframe: false, | ||
| 2865 | - }; | ||
| 2866 | - t += 1; | ||
| 2867 | - let prime = frame.export_prime().expect("export"); | ||
| 2868 | - let imported = Rc::new(ec_va::import_prime(&display, &prime).expect("import")); | ||
| 2869 | - if let Some(c) = zc.submit_prime(imported, meta).expect("submit_prime") { | ||
| 2870 | - a.extend(c.data); | ||
| 2871 | - } | ||
| 2872 | - if let Some(c) = cpu | ||
| 2873 | - .submit_16(&frame.to_i420_16().expect("readback"), meta) | ||
| 2874 | - .expect("submit_16") | ||
| 2875 | - { | ||
| 2876 | - b.extend(c.data); | ||
| 2877 | - } | ||
| 2878 | - }) | ||
| 2879 | - .expect("decode"); | ||
| 2880 | - a.extend(zc.flush().expect("flush").into_iter().flat_map(|c| c.data)); | ||
| 2881 | - b.extend(cpu.flush().expect("flush").into_iter().flat_map(|c| c.data)); | ||
| 2882 | - assert!(!a.is_empty()); | ||
| 2883 | - assert!( | ||
| 2884 | - a == b, | ||
| 2885 | - "zero-copy and CPU 10-bit streams differ ({} vs {} bytes)", | ||
| 2886 | - a.len(), | ||
| 2887 | - b.len() | ||
| 2888 | - ); | ||
| 2889 | -} | ||
| 2890 | - | ||
| 2891 | #[test] | 2670 | #[test] |
| 2892 | fn h264_encode_decodes_with_no_reorder_delay() { | 2671 | fn h264_encode_decodes_with_no_reorder_delay() { |
| 2893 | let Some(display) = display() else { return }; | 2672 | let Some(display) = display() else { return }; |
| @@ -2909,46 +2688,3 @@ fn h264_encode_decodes_with_no_reorder_delay() { | |||
| 2909 | assert!(decoder.next_frame().is_some(), "frame {t} held back"); | 2688 | assert!(decoder.next_frame().is_some(), "frame {t} held back"); |
| 2910 | } | 2689 | } |
| 2911 | } | 2690 | } |
| 2912 | - | ||
| 2913 | -#[test] | ||
| 2914 | -fn imported_dma_buf_scales_and_crops_through_vpp() { | ||
| 2915 | - let Some(display) = display() else { return }; | ||
| 2916 | - let mean = |y: &[u8]| y.iter().map(|&v| f64::from(v)).sum::<f64>() / y.len() as f64; | ||
| 2917 | - let meta = FrameMetadata { | ||
| 2918 | - timestamp: 0, | ||
| 2919 | - force_keyframe: false, | ||
| 2920 | - }; | ||
| 2921 | - for (source_w, source_h, scale) in [(1280, 720, true), (640, 360, false)] { | ||
| 2922 | - let source = source_frame(source_w, source_h, 0); | ||
| 2923 | - let mut config = EncoderConfig::new(EncCodec::H264, 640, 360); | ||
| 2924 | - config.rate_control = RateControlMode::ConstantQp { qp: 22 }; | ||
| 2925 | - let mut encoder = Encoder::new(&display, config).expect("encoder"); | ||
| 2926 | - let surface = imported_nv12(&display, &source); | ||
| 2927 | - let data = if scale { | ||
| 2928 | - let mut data = Vec::new(); | ||
| 2929 | - if let Some(c) = encoder.submit_prime(surface, meta).expect("submit_prime") { | ||
| 2930 | - data.extend(c.data); | ||
| 2931 | - } | ||
| 2932 | - data.extend( | ||
| 2933 | - encoder | ||
| 2934 | - .flush() | ||
| 2935 | - .expect("flush") | ||
| 2936 | - .into_iter() | ||
| 2937 | - .flat_map(|c| c.data), | ||
| 2938 | - ); | ||
| 2939 | - data | ||
| 2940 | - } else { | ||
| 2941 | - encoder | ||
| 2942 | - .encode_surface(&surface, (640, 360), meta) | ||
| 2943 | - .expect("encode_surface") | ||
| 2944 | - .data | ||
| 2945 | - }; | ||
| 2946 | - let mut decoder = Decoder::new(&display, Codec::H264).expect("decoder"); | ||
| 2947 | - decoder.decode(&data, 0).expect("decode"); | ||
| 2948 | - decoder.flush().expect("flush"); | ||
| 2949 | - let frame = decoder.next_frame().expect("a picture"); | ||
| 2950 | - assert_eq!(frame.display_size, (640, 360)); | ||
| 2951 | - let got = frame.to_i420().expect("readback"); | ||
| 2952 | - assert!((mean(&got.y) - mean(&source.y)).abs() < 3.0); | ||
| 2953 | - } | ||
| 2954 | -} | ||