From 0f0e1ac21aee0ee1ea3af78e510c0e2d64120ba3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 15:27:26 -0800 Subject: [PATCH 1/9] fix(moq-video)!: stop serializing NVIDIA transcodes on per-frame driver calls Every flush gave NVDEC a new parser whose sequence callback re-ran cuvidGetDecoderCaps, a query the driver serializes across every process on the GPU at several milliseconds each. Each frame also allocated and freed device memory, and each NVENC encode created and destroyed a bitstream and registered and unregistered its input, all through the driver's per-GPU lock. Check caps only for a new decoder, recycle NVDEC output through an unbounded pool, scale CUDA frames into one registered input per NVENC session, and reuse the output bitstream and CPU input buffer. moq-nvenc splits registration from mapping so a reused input registers once. Co-Authored-By: Claude Opus 5.5 --- rs/moq-nvenc/CHANGELOG.md | 4 + rs/moq-nvenc/README.md | 9 +- rs/moq-nvenc/src/safe/buffer.rs | 227 ++++++++++++--------- rs/moq-nvenc/src/safe/mod.rs | 2 +- rs/moq-video/CHANGELOG.md | 1 + rs/moq-video/src/decode/backend/nvdec.rs | 80 ++++++-- rs/moq-video/src/encode/backend/mod.rs | 7 + rs/moq-video/src/encode/backend/nvenc.rs | 249 +++++++++++++---------- rs/moq-video/src/encode/encoder.rs | 10 +- rs/moq-video/src/frame.rs | 6 +- rs/moq-video/src/frame/cuda.rs | 208 +++++++++++-------- rs/moq-video/src/frame/cuda_test.rs | 51 ++--- rs/moq-video/src/frame/pool.rs | 76 ++++--- 13 files changed, 559 insertions(+), 371 deletions(-) diff --git a/rs/moq-nvenc/CHANGELOG.md b/rs/moq-nvenc/CHANGELOG.md index b5fd76cb578..b6f0d5ff3fe 100644 --- a/rs/moq-nvenc/CHANGELOG.md +++ b/rs/moq-nvenc/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- `Session::register_generic_resource` and `Session::register_cuda_resource` only register the allocation; call `RegisteredResource::map` for each encode and pass the returned `MappedResource` to `Session::encode_picture`. Registration is the expensive call, so a reused input registers once. + ## [0.1.2](https://github.com/moq-dev/moq/compare/moq-nvenc-v0.1.1...moq-nvenc-v0.1.2) - 2026-09-26 ### Fixed diff --git a/rs/moq-nvenc/README.md b/rs/moq-nvenc/README.md index 96e93207693..2b99189e1b2 100644 --- a/rs/moq-nvenc/README.md +++ b/rs/moq-nvenc/README.md @@ -27,11 +27,10 @@ driver handles, and returns a submission that keeps input and output resources alive until synchronous completion. Raw SDK structs remain available under `sys`; APIs that accept their pointers are explicitly unsafe. -External input registration is transactional. If NVENC registers an allocation -but cannot map it, the safe wrapper unregisters it before releasing its owner. -When rollback also fails, the mapping error remains primary, the unregister -error is available through `EncodeError::cleanup`, and the allocation stays -owned because the driver may still refer to it. +An external input is registered once and mapped for each encode, as NVENC +expects: registration is the expensive call, and the allocation may be written +only while unmapped. When an unmap or unregister fails, or a submission is +abandoned, the allocation stays owned because the driver may still refer to it. The `sys` bindings are generated with bindgen from the vendored headers (`src/sys/headers/`); see the [upstream repo](https://github.com/ViliamVadocz/nvidia-video-codec-sdk) diff --git a/rs/moq-nvenc/src/safe/buffer.rs b/rs/moq-nvenc/src/safe/buffer.rs index 5210abee123..6cb9ceaf407 100644 --- a/rs/moq-nvenc/src/safe/buffer.rs +++ b/rs/moq-nvenc/src/safe/buffer.rs @@ -226,8 +226,8 @@ impl Session { /// /// # Errors /// - /// Could error if registration or mapping fails, - /// if the resource is invalid, or if we run out of memory. + /// Could error if registration fails, if the resource is invalid, or if + /// we run out of memory. pub fn register_cuda_resource( &self, pitch: u32, @@ -259,11 +259,8 @@ impl Session { /// /// # Errors /// - /// Could error if registration or mapping fails, - /// if the resource is invalid, or if we run out of memory. - /// A mapping failure rolls registration back before releasing `marker`. If - /// rollback also fails, [`EncodeError::cleanup`] exposes that failure and - /// the marker is retained because NVENC may still refer to its allocation. + /// Could error if registration fails, if the resource is invalid, or if + /// we run out of memory. A failed registration releases `marker`. /// /// # Safety /// @@ -285,8 +282,8 @@ impl Session { self.buffer_format, ) .pitch(pitch); - let mapping = Mapping::new(self.encoder.clone(), marker, &mut params)?; - Ok(RegisteredResource { mapping, pitch }) + let registration = Registration::new(self.encoder.clone(), marker, &mut params)?; + Ok(RegisteredResource { registration, pitch }) } } @@ -664,69 +661,95 @@ impl Drop for BitstreamLock<'_> { } } -/// Abstraction for a registered and mapped external resource. +/// An external resource registered with one session. /// /// The Encoder API exposes a way to use input buffers allocated externally, -/// for example through CUDA or OpenGL. +/// for example through CUDA or OpenGL. Register the allocation once, then +/// [`map`](Self::map) it for each encode: registration is the expensive half, +/// and NVENC forbids touching the allocation only while it is mapped. /// -/// The buffer is automatically unmapped and unregistered when dropped. -/// The external buffer memory should still be properly destroyed by the client. +/// The resource is unregistered when dropped, and only then is its owner +/// released. The external buffer memory should still be properly destroyed +/// by the client. #[derive(Debug)] pub struct RegisteredResource { - mapping: Mapping, T>, + registration: Registration, T>, pitch: u32, } unsafe impl Send for RegisteredResource {} -/// A registered and mapped external resource plus the owner keeping its -/// allocation alive. +impl RegisteredResource { + /// Map the resource for one [`Session::encode_picture`]. It is unmapped when + /// the returned input drops, which [`Submission::finish`](super::Submission::finish) + /// hands back once NVENC has finished reading it; write the allocation only + /// while it is unmapped. + /// + /// Mapping waits for CUDA work on the default stream to finish; work on any + /// other stream must be synchronized first. + /// + /// # Errors + /// + /// Could error if the driver cannot map the resource. The registration + /// stays usable. + pub fn map(&mut self) -> Result, EncodeError> { + let pitch = self.pitch; + Ok(MappedResource { + mapped: self.registration.map()?, + pitch, + }) + } +} + +/// A [`RegisteredResource`] mapped for one encode, unmapped on drop. #[derive(Debug)] -struct Mapping { +pub struct MappedResource<'a, T> { + mapped: Mapped<'a, Arc, T>, + pitch: u32, +} + +/// A registered external resource plus the owner keeping its allocation alive. +#[derive(Debug)] +struct Registration { reg_ptr: *mut c_void, - map_ptr: *mut c_void, api: A, // Dropped only once the resource is unregistered. marker: ManuallyDrop, + /// Set when NVENC may still hold a mapping, so the registration and the + /// owner of its memory are leaked rather than released. + leaked: bool, } -impl Mapping { - /// Register and map as one transaction: a mapping failure unregisters - /// before `marker` is released. +impl Registration { + /// Register `marker`'s allocation. A failed registration holds nothing, so + /// `marker` is released. fn new(api: A, marker: T, params: &mut NV_ENC_REGISTER_RESOURCE) -> Result { let reg_ptr = api.register_resource(params)?; - let map_ptr = match api.map_input_resource(reg_ptr) { - Ok(map_ptr) => map_ptr, - Err(primary) => { - if let Err(cleanup) = api.unregister_resource(reg_ptr) { - // NVENC may still refer to the allocation and there is no handle - // left to retry with, so leaking it is safer than freeing it. - std::mem::forget(marker); - return Err(primary.with_cleanup(cleanup)); - } - return Err(primary); - } - }; Ok(Self { reg_ptr, - map_ptr, api, marker: ManuallyDrop::new(marker), + leaked: false, + }) + } + + fn map(&mut self) -> Result, EncodeError> { + let map_ptr = self.api.map_input_resource(self.reg_ptr)?; + Ok(Mapped { + registration: self, + map_ptr, }) } } -/// Automatically unmap and unregister the external resource -/// when it goes out of scope. -impl Drop for Mapping { +/// Unregister the external resource when it goes out of scope. +impl Drop for Registration { fn drop(&mut self) { - // A failed unmap leaves the resource mapped. Unregistering or freeing - // it could release memory NVENC still holds, so leak the owner instead. - if self.api.unmap_input_resource(self.map_ptr).is_err() { + if self.leaked { return; } - // As in `new`: NVENC may still refer to an allocation it failed to - // unregister, so leak its owner rather than free it. + // NVENC may still refer to an allocation it failed to unregister, so + // leak its owner rather than free it. if self.api.unregister_resource(self.reg_ptr).is_ok() { // SAFETY: dropped once, here, and never touched again. unsafe { ManuallyDrop::drop(&mut self.marker) }; @@ -734,26 +757,51 @@ impl Drop for Mapping { } } -impl sealed::Input for RegisteredResource { +/// One mapping of a [`Registration`]. +#[derive(Debug)] +struct Mapped<'a, A: ResourceApi, T> { + registration: &'a mut Registration, + map_ptr: *mut c_void, +} + +impl Mapped<'_, A, T> { + /// Give up a mapping the driver may still be reading: never unmap it, and + /// never unregister or release its allocation. fn abandon(self) { - // Skip `Drop`, leaking the registration and the owner of its memory. let this = ManuallyDrop::new(self); // SAFETY: read once from a value whose destructor never runs. - drop(unsafe { ptr::read(&this.mapping.api) }); + let registration = unsafe { ptr::read(&this.registration) }; + registration.leaked = true; + } +} + +impl Drop for Mapped<'_, A, T> { + fn drop(&mut self) { + // A failed unmap leaves the resource mapped. Unregistering or freeing + // it could release memory NVENC still holds, so leak the owner instead. + if self.registration.api.unmap_input_resource(self.map_ptr).is_err() { + self.registration.leaked = true; + } + } +} + +impl sealed::Input for MappedResource<'_, T> { + fn abandon(self) { + self.mapped.abandon(); } } -impl EncoderInput for RegisteredResource { +impl EncoderInput for MappedResource<'_, T> { fn pitch(&self) -> u32 { self.pitch } fn handle(&mut self) -> *mut c_void { - self.mapping.map_ptr + self.mapped.map_ptr } fn encoder(&self) -> &Arc { - &self.mapping.api + &self.mapped.registration.api } } @@ -761,7 +809,6 @@ impl EncoderInput for RegisteredResource { mod tests { use std::{ cell::{Cell, RefCell}, - error::Error, rc::Rc, }; @@ -866,7 +913,7 @@ mod tests { (Owner(alive.clone()), alive) } - fn register(api: &TestApi, owner: Owner) -> Result, EncodeError> { + fn register(api: &TestApi, owner: Owner) -> Result, EncodeError> { let mut params = NV_ENC_REGISTER_RESOURCE::new( NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, 1920, @@ -875,53 +922,47 @@ mod tests { NV_ENC_BUFFER_FORMAT::NV_ENC_BUFFER_FORMAT_NV12, ) .pitch(1920); - Mapping::new(api, owner, &mut params) + Registration::new(api, owner, &mut params) } #[test] - fn mapping_failure_unregisters_before_releasing_the_owner() { + fn one_registration_serves_every_mapping() { let (owner, alive) = setup(); - let mut api = TestApi::new(alive.clone()); - api.map_error = Some(ErrorKind::MapFailed); + let api = TestApi::new(alive.clone()); - let error = register(&api, owner).expect_err("mapping should fail"); + let mut registration = register(&api, owner).expect("registration should succeed"); + for _ in 0..3 { + drop(registration.map().expect("mapping should succeed")); + assert!(!api.mapped.get(), "each mapping is released before the next"); + } + assert!(alive.get()); + drop(registration); - assert_eq!(error.kind(), ErrorKind::MapFailed); - assert!(error.cleanup().is_none()); - assert!(!alive.get(), "owner should be released after successful rollback"); + assert!(!alive.get(), "owner should be released after destruction"); assert_eq!( *api.calls.borrow(), Calls { register: 1, - map: 1, - unmap: 0, + map: 3, + unmap: 3, unregister: 1, } ); } #[test] - fn rollback_failure_retains_both_errors_and_the_owner() { + fn mapping_failure_keeps_the_registration() { let (owner, alive) = setup(); let mut api = TestApi::new(alive.clone()); api.map_error = Some(ErrorKind::MapFailed); - api.unregister_error = Some(ErrorKind::ResourceNotRegistered); - - let error = register(&api, owner).expect_err("mapping and rollback should fail"); + let mut registration = register(&api, owner).expect("registration should succeed"); + let error = registration.map().expect_err("mapping should fail"); assert_eq!(error.kind(), ErrorKind::MapFailed); - assert_eq!( - error.cleanup().map(EncodeError::kind), - Some(ErrorKind::ResourceNotRegistered) - ); - assert_eq!( - error - .source() - .and_then(|source| source.downcast_ref::()) - .map(EncodeError::kind), - Some(ErrorKind::ResourceNotRegistered) - ); - assert!(alive.get(), "a possibly registered allocation must remain owned"); + assert!(alive.get()); + drop(registration); + + assert!(!alive.get(), "owner should be released after unregistering"); assert_eq!( *api.calls.borrow(), Calls { @@ -934,41 +975,43 @@ mod tests { } #[test] - fn mapped_resource_cleans_up_once_before_releasing_the_owner() { + fn failed_unmap_retains_the_owner() { let (owner, alive) = setup(); - let api = TestApi::new(alive.clone()); + let mut api = TestApi::new(alive.clone()); + api.unmap_error = Some(ErrorKind::ResourceNotMapped); - let resource = register(&api, owner).expect("mapping should succeed"); - assert!(alive.get()); - drop(resource); + let mut registration = register(&api, owner).expect("registration should succeed"); + drop(registration.map().expect("mapping should succeed")); + drop(registration); - assert!(!alive.get(), "owner should be released after destruction"); + assert!(alive.get(), "a possibly mapped allocation must remain owned"); assert_eq!( *api.calls.borrow(), Calls { register: 1, map: 1, unmap: 1, - unregister: 1, + unregister: 0, } ); } #[test] - fn failed_unmap_on_drop_retains_the_owner() { + fn an_abandoned_mapping_retains_the_owner() { let (owner, alive) = setup(); - let mut api = TestApi::new(alive.clone()); - api.unmap_error = Some(ErrorKind::ResourceNotMapped); + let api = TestApi::new(alive.clone()); - drop(register(&api, owner).expect("mapping should succeed")); + let mut registration = register(&api, owner).expect("registration should succeed"); + registration.map().expect("mapping should succeed").abandon(); + drop(registration); - assert!(alive.get(), "a possibly mapped allocation must remain owned"); + assert!(alive.get(), "an allocation the driver may still read must remain owned"); assert_eq!( *api.calls.borrow(), Calls { register: 1, map: 1, - unmap: 1, + unmap: 0, unregister: 0, } ); @@ -980,15 +1023,15 @@ mod tests { let mut api = TestApi::new(alive.clone()); api.unregister_error = Some(ErrorKind::ResourceNotRegistered); - drop(register(&api, owner).expect("mapping should succeed")); + drop(register(&api, owner).expect("registration should succeed")); assert!(alive.get(), "a possibly registered allocation must remain owned"); assert_eq!( *api.calls.borrow(), Calls { register: 1, - map: 1, - unmap: 1, + map: 0, + unmap: 0, unregister: 1, } ); diff --git a/rs/moq-nvenc/src/safe/mod.rs b/rs/moq-nvenc/src/safe/mod.rs index 99e1cff8473..40e9360f937 100644 --- a/rs/moq-nvenc/src/safe/mod.rs +++ b/rs/moq-nvenc/src/safe/mod.rs @@ -11,7 +11,7 @@ mod result; mod session; pub use api::LoadError; -pub use buffer::{Bitstream, BitstreamLock, Buffer, BufferLock, EncoderInput, RegisteredResource}; +pub use buffer::{Bitstream, BitstreamLock, Buffer, BufferLock, EncoderInput, MappedResource, RegisteredResource}; pub use encoder::{Encoder, EncoderInitParams}; pub use result::{EncodeError, ErrorKind}; pub use session::{EncodePictureParams, Session, Submission}; diff --git a/rs/moq-video/CHANGELOG.md b/rs/moq-video/CHANGELOG.md index 77f57f89a14..938f538cef2 100644 --- a/rs/moq-video/CHANGELOG.md +++ b/rs/moq-video/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- NVIDIA transcodes no longer serialize the whole GPU: NVDEC skips the multi-millisecond decoder-caps query when a flush's new parser keeps the decoder, decoded frames reuse pooled device memory instead of a `cuMemAlloc` and `cuMemFree` each, and each NVENC session scales CUDA frames into one input it registers once and reuses one output bitstream, where it used to allocate, register, and free all of them per frame. - A V4L2 camera that opens and never delivers a frame no longer blocks its capture thread forever: reads time out after a second, so dropping the `capture::Stream` stops the thread and closes the device promptly. - The PipeWire camera listing hides nodes that offer no format the backend converts, such as an IR camera with only GREY, as the V4L2 listing already does. - `capture::cameras()` lists a webcam once with the `pipewire` feature: the dedupe read `api.v4l2.path`, which a PipeWire registry global lacks, and now reads `object.path`. diff --git a/rs/moq-video/src/decode/backend/nvdec.rs b/rs/moq-video/src/decode/backend/nvdec.rs index f7ea9c2704d..a6577866cd1 100644 --- a/rs/moq-video/src/decode/backend/nvdec.rs +++ b/rs/moq-video/src/decode/backend/nvdec.rs @@ -28,7 +28,7 @@ use std::ptr; use std::sync::Arc; use bytes::Bytes; -use cudarc::driver::CudaContext; +use cudarc::driver::{CudaContext, CudaStream}; use moq_net::Timestamp; use moq_nvenc::cuvid; use moq_nvenc::sys::cuviddec::{ @@ -64,6 +64,9 @@ pub(crate) struct Nvdec { struct State { api: &'static cuvid::Api, ctx: Arc, + /// Where each picture is post-processed and copied out: never the legacy + /// default stream, which would wait for every other thread's work. + stream: Arc, /// Requested output size; `None` decodes at the stream's display size. resize: Option, decoder: Option, @@ -93,6 +96,8 @@ struct Decoder { /// Decode surfaces it was created with; the parser may index any of the /// count it reports, so a larger requirement needs a new decoder. surfaces: c_int, + /// The buffers pictures are copied out into, recycled as their frames drop. + frames: cuda::Frames, } impl Drop for Decoder { @@ -109,6 +114,10 @@ impl Drop for Decoder { impl Nvdec { pub(crate) fn open(stream: &Stream, config: &super::Config) -> Result, Error> { + Ok(Box::new(Self::new(stream, config)?)) + } + + fn new(stream: &Stream, config: &super::Config) -> Result { let codec = stream.codec; // cudarc panics when libcuda is missing, so probe it and turn a missing // driver into a recoverable error; the cuvid table already loads fallibly. @@ -140,8 +149,10 @@ impl Nvdec { }; check_caps(api, cuda_codec, coded)?; + let stream = ctx.new_stream().map_err(|e| codec_err(format!("CUDA stream: {e:?}")))?; let mut state = Box::new(State { api, + stream, ctx, resize: config.scale_hint, decoder: None, @@ -151,12 +162,12 @@ impl Nvdec { let parser = create_parser(cuda_codec, &mut state)?; tracing::info!(decoder = NAME, codec = ?codec, resize = ?config.scale_hint, "opened video decoder"); - Ok(Box::new(Self { + Ok(Self { parser: Some(parser), codec: cuda_codec, state, ready: VecDeque::new(), - })) + }) } /// Submit one parser packet and queue a copy of every picture its display @@ -313,12 +324,6 @@ impl State { format.bit_depth_luma_minus8 + 8 ))); } - check_caps( - self.api, - format.codec, - Coded::Exact(format.coded_width, format.coded_height), - )?; - // Output size: the requested resize, or the display area (the coded size // minus cropping). Rounded down to even for NV12. let display = crate::Size::new( @@ -351,6 +356,16 @@ impl State { } self.decoder = None; + // Only for a new decoder: the answer cannot change for the same coded + // size, and the driver serializes the query across every process on the + // GPU at several milliseconds each, while every flush brings a new + // parser that repeats the sequence header. + check_caps( + self.api, + format.codec, + Coded::Exact(format.coded_width, format.coded_height), + )?; + let mut info = CUVIDDECODECREATEINFO { ulWidth: format.coded_width as c_ulong, ulHeight: format.coded_height as c_ulong, @@ -404,11 +419,12 @@ impl State { width, height, surfaces, + frames: cuda::Frames::new(&self.ctx), }); Ok(surfaces) } - /// Map one decoded picture and copy it device-to-device into an owned CUDA + /// Map one decoded picture and copy it device-to-device into a recycled CUDA /// buffer, so the fixed surface pool is released before the next decode. fn map_frame(&self, disp: &CUVIDPARSERDISPINFO) -> Result { let decoder = self @@ -419,6 +435,7 @@ impl State { let mut proc_params = CUVIDPROCPARAMS { progressive_frame: disp.progressive_frame, top_field_first: disp.top_field_first, + output_stream: self.stream.cu_stream() as _, ..Default::default() }; @@ -441,14 +458,23 @@ impl State { // The mapped surface is NV12 at the decoder's target size: `height` luma // rows of `pitch` bytes, then `height / 2` interleaved-UV rows. Copy it - // linearly into an owned buffer with the identical layout. + // linearly into a buffer with the identical layout, on the stream that + // post-processed it, and wait before the surface is unmapped. let copied = (|| -> Result { - let frame = cuda::Frame::alloc(&self.ctx, decoder.width, decoder.height, pitch)?; + let frame = decoder + .frames + .alloc(crate::Size::new(decoder.width, decoder.height), pitch)?; let len = pitch as usize * decoder.height as usize * 3 / 2; // SAFETY: both regions are `len` bytes of device memory: the mapped - // surface by cuvid's layout above, the destination by `alloc`. - unsafe { cudarc::driver::result::memcpy_dtod_sync(frame.device_ptr(), dev_ptr, len) } - .map_err(|e| codec_err(format!("CUDA device copy: {e:?}")))?; + // surface by cuvid's layout above, the destination by `alloc`, and + // both outlive the synchronize below. + unsafe { + cudarc::driver::result::memcpy_dtod_async(frame.device_ptr(), dev_ptr, len, self.stream.cu_stream()) + } + .map_err(|e| codec_err(format!("CUDA device copy: {e:?}")))?; + self.stream + .synchronize() + .map_err(|e| codec_err(format!("CUDA device copy sync: {e:?}")))?; Ok(frame) })(); @@ -695,8 +721,19 @@ mod tests { out } + /// Counts `cuvidGetDecoderCaps` calls, forwarding each to the driver. + static CAPS_QUERIES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + + unsafe extern "C" fn counting_caps(caps: *mut CUVIDDECODECAPS) -> cudarc::driver::sys::CUresult { + CAPS_QUERIES.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + // SAFETY: forwards the caller's pointer unchanged to the real entry point. + unsafe { (cuvid::Api::get().unwrap().get_decoder_caps)(caps) } + } + /// The transcode feed drains at every group end, so a drain must leave the - /// decoder decoding the next keyframe and everything behind it. + /// decoder decoding the next keyframe and everything behind it, without + /// asking the driver for caps again: that query is serialized across every + /// process on the GPU and takes milliseconds. #[test] fn nvdec_decodes_after_a_flush() { if !hw_available() { @@ -708,7 +745,11 @@ mod tests { ..EncodeConfig::new(w, h, crate::FrameRate::new(30, 1).unwrap()) }) .unwrap(); - let mut decoder = Nvdec::open(&Codec::H264.into(), &decode_config(None)).expect("NVDEC H.264 decoder"); + let mut decoder = Nvdec::new(&Codec::H264.into(), &decode_config(None)).expect("NVDEC H.264 decoder"); + decoder.state.api = Box::leak(Box::new(cuvid::Api { + get_decoder_caps: counting_caps, + ..*cuvid::Api::get().unwrap() + })); let rgba = gradient_rgba(w, h); let mut decoded = Vec::new(); @@ -738,6 +779,11 @@ mod tests { let expected: Vec = (0..10).map(|i| (group as u64 * 10 + i) * 33_333).collect(); assert_eq!(timestamps, &expected, "group {group} after {group} flushes"); } + assert_eq!( + CAPS_QUERIES.load(std::sync::atomic::Ordering::Relaxed), + 1, + "only the decoder the first group created asked for caps" + ); } /// H.264 through the real hardware: openh264 encodes a gradient (Annex-B, diff --git a/rs/moq-video/src/encode/backend/mod.rs b/rs/moq-video/src/encode/backend/mod.rs index b14de0839db..013f49949c7 100644 --- a/rs/moq-video/src/encode/backend/mod.rs +++ b/rs/moq-video/src/encode/backend/mod.rs @@ -131,6 +131,13 @@ pub(crate) trait Backend { /// inherit a yes and let the refusal surface as a mislaid group boundary. fn can_cut(&self) -> bool; + /// Whether [`encode`](Self::encode) scales `surface` to the session size + /// itself, on the surface's device, so a resizing encoder hands the frame + /// over at its own size. The default leaves scaling to the encoder. + fn scales(&self, _surface: &crate::Surface) -> bool { + false + } + /// The encoder name in use, e.g. `"videotoolbox"` (for logging and errors). fn name(&self) -> &'static str; diff --git a/rs/moq-video/src/encode/backend/nvenc.rs b/rs/moq-video/src/encode/backend/nvenc.rs index 20ecd84c7e0..f207255aa43 100644 --- a/rs/moq-video/src/encode/backend/nvenc.rs +++ b/rs/moq-video/src/encode/backend/nvenc.rs @@ -24,8 +24,12 @@ //! //! The session's input format is NV12 (NVENC's native layout). A CPU I420 frame //! is interleaved into an NVENC input buffer; a CUDA frame ([`Surface::Cuda`], -//! NVDEC output, already NV12) is registered as an external resource and encoded -//! in place, so the NVDEC -> NVENC transcode path never touches the CPU. +//! NVDEC output, already NV12) is scaled on the GPU into the session's own +//! [`cuda::Target`], an exact copy at the session size, so the NVDEC -> NVENC +//! transcode path never touches the CPU. The target, the input buffer, and the +//! output bitstream are each created and registered once per session: those +//! calls take the driver's per-GPU lock, which every session in every process +//! on the device contends for, so per frame they cap the whole GPU. //! //! The hardware sets a floor on the picture: a session narrower than the //! driver's minimum is refused at open (145 on an RTX 3070 Ti), so the bottom @@ -43,7 +47,7 @@ use moq_nvenc::sys::nvEncodeAPI::{ NV_ENC_PARAMS_RC_MODE, NV_ENC_PRESET_P1_GUID, NV_ENC_PRESET_P4_GUID, NV_ENC_PRESET_P7_GUID, NV_ENC_TUNING_INFO, NV_ENC_VUI_COLOR_PRIMARIES, NV_ENC_VUI_MATRIX_COEFFS, NV_ENC_VUI_TRANSFER_CHARACTERISTIC, NV_ENC_VUI_VIDEO_FORMAT, }; -use moq_nvenc::{Encoder, EncoderInitParams, Session}; +use moq_nvenc::{Bitstream, Buffer, Encoder, EncoderInitParams, RegisteredResource, Session}; use super::super::encoder::{Applied, Codec, Config, Preset}; use super::{Backend, Encoded, keyframe_annexb}; @@ -79,9 +83,15 @@ fn nvenc_preset(preset: Preset) -> (GUID, &'static str) { pub(crate) struct Nvenc { session: Session, + /// Where CUDA frames are scaled for encoding, created by the first one. + target: Option, + /// Where CPU frames are written, created by the first one. + input: Option, + /// The output of every encode, which completes before the next begins. + output: Option, applied: Applied, // Keep the CUDA context alive for as long as the session uses it. - _cuda: Arc, + cuda: Arc, timestamp: u64, codec: Codec, size: crate::Size, @@ -89,6 +99,32 @@ pub(crate) struct Nvenc { ready: VecDeque, } +/// The session's CUDA input: one buffer every CUDA frame is scaled into, +/// registered with the session once and mapped for each encode. +struct Target { + buffer: cuda::Target, + resource: RegisteredResource, +} + +impl Target { + fn new(session: &Session, ctx: &Arc, size: crate::Size) -> Result { + let buffer = cuda::Target::new(ctx, size)?; + let frame = buffer.frame().clone(); + // SAFETY: the registration owns a clone of the frame, which keeps the + // allocation addressed by the pointer alive until it is unregistered. + let resource = unsafe { + session.register_generic_resource( + frame.clone(), + NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, + frame.device_ptr() as *mut std::ffi::c_void, + frame.pitch, + ) + } + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC register CUDA input: {e}")))?; + Ok(Self { buffer, resource }) + } +} + impl Nvenc { pub(crate) fn open(config: &Config) -> Result, Error> { Encoder::load().map_err(|error| Error::codec(anyhow::anyhow!("NVENC unavailable: {error}")))?; @@ -236,9 +272,12 @@ impl Nvenc { .input .map(|_| cuda::Converter::new(cuda.ordinal(), config.resolved_color(), NonZeroUsize::new(8).unwrap())) .transpose()?, + target: None, + input: None, + output: None, size: config.size(), ready: VecDeque::new(), - _cuda: cuda, + cuda, timestamp: 0, codec: config.codec, })) @@ -247,42 +286,40 @@ impl Nvenc { impl Backend for Nvenc { fn encode(&mut self, frame: &Frame, cut: bool) -> Result<(), Error> { - // The published image shares its full-size conversion across renditions. - // Smaller encoders resize that NV12 buffer, so capture converts only once - // for each declared color space and never downloads its pixels. - let prepared = if let Surface::Vulkan(image) = &frame.surface { - let converter = self.converter.as_ref().ok_or_else(|| { - Error::Unsupported(format!( - "NVENC was not opened for external Vulkan {}", - image.image().device - )) - })?; - let converted = image.converted(converter.color(), || { - // Read before reserving: an exhausted pool must still signal the - // producer's timeline, or the unread frame loses its slot. - converter.import(image)?; - converter - .reserve() - .ok_or_else(|| Error::Unsupported("CUDA conversion pool exhausted".into()))? - .convert(image) - })?; - let sized = if converted.size() == self.size { - converted - } else { - converter - .reserve() - .ok_or_else(|| Error::Unsupported("CUDA resize pool exhausted".into()))? - .resize(&converted, self.size)? - }; - Some(Frame::new(Surface::Cuda(sized), frame.timestamp)) - } else { - None + // The published image shares its full-size conversion across renditions, + // so capture converts only once for each declared color space and never + // downloads its pixels; each encoder then scales it into its own target. + let converted; + let gpu = match &frame.surface { + Surface::Vulkan(image) => { + let converter = self.converter.as_ref().ok_or_else(|| { + Error::Unsupported(format!( + "NVENC was not opened for external Vulkan {}", + image.image().device + )) + })?; + converted = image.converted(converter.color(), || { + // Read before reserving: an exhausted pool must still signal the + // producer's timeline, or the unread frame loses its slot. + converter.import(image)?; + converter + .reserve() + .ok_or_else(|| Error::Unsupported("CUDA conversion pool exhausted".into()))? + .convert(image) + })?; + Some(&converted) + } + Surface::Cuda(cuda) => Some(cuda), + _ => None, + }; + + let output = match self.output.take() { + Some(output) => output, + None => self + .session + .create_output_bitstream() + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC output bitstream: {e}")))?, }; - let frame = prepared.as_ref().unwrap_or(frame); - let output = self - .session - .create_output_bitstream() - .map_err(|e| Error::codec(anyhow::anyhow!("NVENC output bitstream: {e}")))?; let params = moq_nvenc::EncodePictureParams { input_timestamp: self.timestamp, @@ -290,71 +327,71 @@ impl Backend for Nvenc { }; self.timestamp += 1; - // Each arm drains the output *before* releasing its input (the registered + // Each arm drains the output *before* releasing its input (the mapped // resource / input buffer): the synchronous bitstream lock is what // guarantees NVENC is done reading the input, so releasing the input // first would race the encoder. - let data = match &frame.surface { - // A CUDA frame is already NV12 in device memory (NVDEC output): - // register its buffer as an external NVENC resource and encode in - // place, no CPU round trip and no GPU copy. - Surface::Cuda(cuda) => { - // Registration keeps a raw pointer into the frame; the frame - // (borrowed) outlives the registration. - // SAFETY: the cloned CUDA frame owns the allocation addressed by the - // pointer and the registration retains that clone through completion. - let resource = unsafe { - self.session.register_generic_resource( - cuda.clone(), - NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR, - cuda.device_ptr() as *mut std::ffi::c_void, - cuda.pitch, - ) - } - .map_err(|e| Error::codec(anyhow::anyhow!("NVENC register CUDA frame: {e}")))?; - - let submission = self - .session - .encode_picture(resource, output, params) - .map_err(|e| Error::codec(anyhow::anyhow!("NVENC encode: {e}")))?; - drain_output(submission)? - } + let data = if let Some(gpu) = gpu { + let target = match self.target.take() { + Some(target) => target, + None => Target::new(&self.session, &self.cuda, self.size)?, + }; + let target = self.target.insert(target); + // Written only while unmapped, and finished before mapping: NVENC + // orders only against the default stream, which the target avoids. + target.buffer.fill(gpu)?; + let mapped = target + .resource + .map() + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC map CUDA input: {e}")))?; + let submission = self + .session + .encode_picture(mapped, output, params) + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC encode: {e}")))?; + let (data, _mapped, output) = drain_output(submission)?; + self.output = Some(output); + data + } else { // Everything else goes through a CPU NV12 input buffer. - frame => { - let mut input = self + let mut input = match self.input.take() { + Some(input) => input, + None => self .session .create_input_buffer() - .map_err(|e| Error::codec(anyhow::anyhow!("NVENC input buffer: {e}")))?; - - let i420 = frame.to_i420()?; - - // Interleave the I420 chroma planes into NV12's single UV plane. - let (w, h) = (i420.width as usize, i420.height as usize); - let mut uv = vec![0u8; w * h / 2]; - interleave_uv(i420.u(), i420.v(), &mut uv); - - // Write both planes honoring NVENC's chosen row stride: the - // buffer pitch can exceed the width even for aligned widths, so a - // flat write would shear the image. NV12 chroma rows are full - // width (interleaved U+V) at the luma pitch. - let mut lock = input - .lock() - .map_err(|e| Error::codec(anyhow::anyhow!("NVENC lock input: {e}")))?; - let pitch = lock.pitch() as usize; - // SAFETY: offsets stay within the pitch*height*3/2 NV12 buffer and - // each source plane is exactly row_bytes * rows. - unsafe { - lock.write_rows(0, pitch, i420.y(), w, h); - lock.write_rows(pitch * h, pitch, &uv, w, h / 2); - } - drop(lock); + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC input buffer: {e}")))?, + }; - let submission = self - .session - .encode_picture(input, output, params) - .map_err(|e| Error::codec(anyhow::anyhow!("NVENC encode: {e}")))?; - drain_output(submission)? + let i420 = frame.surface.to_i420()?; + + // Interleave the I420 chroma planes into NV12's single UV plane. + let (w, h) = (i420.width as usize, i420.height as usize); + let mut uv = vec![0u8; w * h / 2]; + interleave_uv(i420.u(), i420.v(), &mut uv); + + // Write both planes honoring NVENC's chosen row stride: the + // buffer pitch can exceed the width even for aligned widths, so a + // flat write would shear the image. NV12 chroma rows are full + // width (interleaved U+V) at the luma pitch. + let mut lock = input + .lock() + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC lock input: {e}")))?; + let pitch = lock.pitch() as usize; + // SAFETY: offsets stay within the pitch*height*3/2 NV12 buffer and + // each source plane is exactly row_bytes * rows. + unsafe { + lock.write_rows(0, pitch, i420.y(), w, h); + lock.write_rows(pitch * h, pitch, &uv, w, h / 2); } + drop(lock); + + let submission = self + .session + .encode_picture(input, output, params) + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC encode: {e}")))?; + let (data, input, output) = drain_output(submission)?; + self.input = Some(input); + self.output = Some(output); + data }; // The bitstream lock above is synchronous, so this is that frame's output. @@ -388,6 +425,10 @@ impl Backend for Nvenc { true } + fn scales(&self, surface: &Surface) -> bool { + matches!(surface, Surface::Cuda(_)) + } + fn name(&self) -> &'static str { NAME } @@ -397,14 +438,14 @@ impl Backend for Nvenc { } } -/// Block on the output bitstream and copy it out. The lock returning is also -/// what guarantees NVENC finished reading the frame's input resource, so call -/// this while that input is still alive. -fn drain_output(submission: moq_nvenc::Submission) -> Result, Error> { - let (data, _input, _output) = submission +/// Block on the output bitstream and copy it out, handing back both buffers +/// for the next frame. The lock returning is also what guarantees NVENC +/// finished reading the frame's input resource, so call this while that input +/// is still alive. +fn drain_output(submission: moq_nvenc::Submission) -> Result<(Vec, I, Bitstream), Error> { + submission .finish() - .map_err(|e| Error::codec(anyhow::anyhow!("NVENC lock output: {e}")))?; - Ok(data) + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC lock output: {e}"))) } /// Match the declared Vulkan device before allocating an NVENC session. diff --git a/rs/moq-video/src/encode/encoder.rs b/rs/moq-video/src/encode/encoder.rs index f1942ecd2f6..ec1e2c01bc2 100644 --- a/rs/moq-video/src/encode/encoder.rs +++ b/rs/moq-video/src/encode/encoder.rs @@ -555,16 +555,22 @@ impl Encoder { }; #[cfg(not(target_os = "linux"))] let external = false; + // A backend that scales a GPU surface itself writes it straight into its + // own input, rather than into a scaled copy made here. + let scaled = external + || self.resize.as_ref().is_some_and(|config| { + config.output.resolve(&frame.surface) == crate::Output::Native && self.backend.scales(&frame.surface) + }); let resized; let frame = match &self.resize { - Some(config) if !external && frame.size() != self.size => { + Some(config) if !scaled && frame.size() != self.size => { resized = frame.resize(self.size, config)?; &resized } _ => frame, }; let size = frame.size(); - if !external && size != self.size { + if !scaled && size != self.size { return Err(Error::codec(anyhow::anyhow!( "frame {size} does not match encoder {}", self.size diff --git a/rs/moq-video/src/frame.rs b/rs/moq-video/src/frame.rs index b97bf16764b..a57ffc2f387 100644 --- a/rs/moq-video/src/frame.rs +++ b/rs/moq-video/src/frame.rs @@ -453,8 +453,8 @@ pub enum Surface { /// Zero-copy GPU texture (Windows Direct3D11 NV12). #[cfg(target_os = "windows")] Texture(d3d11::Texture), - /// Zero-copy GPU buffer (Linux CUDA NV12). Produced by the NVDEC decoder or - /// the GPU color converter, consumed in place by the NVENC encoder. + /// GPU buffer (Linux CUDA NV12). Produced by the NVDEC decoder or the GPU + /// color converter, and scaled on the GPU into the NVENC encoder's input. #[cfg(all(target_os = "linux", feature = "nvidia"))] Cuda(cuda::Frame), /// External Vulkan RGBA8 / BGRA8 image with explicit GPU synchronization. @@ -3399,7 +3399,7 @@ mod tests { // Upload as pitched NV12: Y rows, then interleaved UV rows. let pitch = 512u32; - let frame = cuda::Frame::alloc(&ctx, w, h, pitch).unwrap(); + let frame = cuda::Frames::new(&ctx).alloc(Size::new(w, h), pitch).unwrap(); let mut host = vec![0u8; pitch as usize * h as usize * 3 / 2]; for row in 0..h as usize { let dst = row * pitch as usize; diff --git a/rs/moq-video/src/frame/cuda.rs b/rs/moq-video/src/frame/cuda.rs index 9e8e77020cc..b0e85744855 100644 --- a/rs/moq-video/src/frame/cuda.rs +++ b/rs/moq-video/src/frame/cuda.rs @@ -1,5 +1,6 @@ //! Linux CUDA device memory: the NV12 [`Frame`] behind `Surface::Cuda`, which -//! NVDEC or a `Converter` produces and NVENC consumes in place. +//! NVDEC or a `Converter` produces and NVENC scales into its own input on the +//! GPU. //! //! # The GPU-only path //! @@ -8,13 +9,15 @@ //! one full-size conversion per capture and color space, then scaling each //! rendition on the GPU. Those images are never downloaded. //! -//! Native `Surface::Cuda` frames from NVDEC also encode in place. Their portable +//! Native `Surface::Cuda` frames from NVDEC also stay on the GPU. Their portable //! `Surface` methods permit CPU readback for software transcoding, while an //! external Vulkan image refuses that fallback. //! -//! NVENC takes the converted frame as a registered `cuMemAlloc` NV12 buffer -//! (`NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR`), the registration the NVDEC -//! path already proved on hardware. The SDK 12.1 bindings could also register +//! NVENC reads a registered `cuMemAlloc` NV12 buffer +//! (`NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR`): each session's own `Target`, +//! which every frame is scaled into, an exact copy at the session size. The +//! one copy is what lets a session register once rather than per frame, and +//! costs microseconds of GPU time. The SDK 12.1 bindings could also register //! the imported Vulkan image itself as a CUDA array in an RGB session, but NVENC //! would then own the color matrix, and the smaller rendition would still need //! its own conversion, so a kernel that writes NV12 straight from the image is @@ -23,17 +26,24 @@ //! What the hardware says (RTX 3070 Ti, driver 595.91.07, `just rs gpu`): //! the kernels match a CPU reference exactly, and an `nsys --trace=cuda` capture //! of `tests::vulkan_cuda_three_view_workload` records no `cuMemcpy` in either -//! direction. Registering a converted buffer with NVENC costs around 70us and -//! releasing it around 150us, a tenth of a 1280x720 encode; holding one -//! registration per pool buffer would reclaim that, but a registration belongs -//! to one session while the pool is shared by all of them, so that cache has to -//! live in the encoder. +//! direction. +//! +//! # Nothing per frame takes the driver's device lock +//! +//! Allocating, freeing, and registering device memory go through the +//! driver's per-GPU lock, which every process on the device shares, and +//! `cuMemFree` also waits for the whole device. Paid per frame, those calls +//! serialize every transcode on the GPU while its engines sit idle. So every +//! frame's buffer comes from a `Pool` and goes back to it, and each NVENC +//! session scales its input into one `Target` it registered once. Scaling +//! and copies run on their owner's own stream, never the legacy default +//! stream, which would wait for every other thread's work in the process. use std::collections::HashMap; use std::num::NonZeroUsize; use std::sync::{Arc, Mutex, OnceLock}; -use cudarc::driver::{CudaContext, CudaFunction, LaunchConfig, PushKernelArg, result}; +use cudarc::driver::{CudaContext, CudaFunction, CudaStream, LaunchConfig, PushKernelArg, result}; use super::pool::{self, Pool}; use super::{I420, cuda_vulkan, vulkan}; @@ -125,21 +135,24 @@ impl pool::Alloc for Device { } } -/// A frame's allocation: a buffer leased from a `Converter`'s pool, handed -/// back for reuse on drop, or a plain one freed on drop. -enum Buffer { - Pooled(pool::Lease), - Plain(Raw), -} +/// Recycled NV12 buffers on one device for a producer that cannot refuse a +/// frame, such as a decoder: never exhausted, and holding at most as many +/// buffers as its frames ever had alive at once. Frames scaled from these draw +/// from the same buffers. +#[derive(Clone)] +pub(crate) struct Frames(Arc>); -impl std::ops::Deref for Buffer { - type Target = Raw; +impl Frames { + /// Buffers on `ctx`'s device. + pub(crate) fn new(ctx: &Arc) -> Self { + Self(Arc::new(Pool::new(Device(ctx.clone()), None))) + } - fn deref(&self) -> &Raw { - match self { - Self::Pooled(lease) => lease, - Self::Plain(raw) => raw, - } + /// An uninitialized frame of `size` (both dimensions even) at row pitch + /// `pitch`: the caller writes the full extent. + pub(crate) fn alloc(&self, size: Size, pitch: u32) -> Result { + let reservation = self.0.reserve().expect("an unbounded pool always has room"); + Frame::pooled(reservation, size, pitch, None) } } @@ -153,7 +166,7 @@ impl std::ops::Deref for Buffer { /// retains it), so a frame decoded by NVDEC is directly addressable by NVENC. #[derive(Clone)] pub struct Frame { - buf: Arc, + buf: Arc>, pub(crate) width: u32, pub(crate) height: u32, /// Row pitch in bytes of both planes (>= `width`). @@ -169,7 +182,6 @@ impl std::fmt::Debug for Frame { .field("size", &self.size()) .field("pitch", &self.pitch) .field("color", &self.color) - .field("pooled", &matches!(*self.buf, Buffer::Pooled(_))) .finish() } } @@ -200,25 +212,16 @@ fn aligned_pitch(width: u32) -> Result { } impl Frame { - /// Allocate an NV12 buffer for `width` x `height` (both even) at row - /// pitch `pitch`. Uninitialized: the caller copies the full extent in. - pub(crate) fn alloc(ctx: &Arc, width: u32, height: u32, pitch: u32) -> Result { - debug_assert!(pitch >= width && width.is_multiple_of(2) && height.is_multiple_of(2)); - let raw = Raw::alloc(ctx, nv12_len(height, pitch)?)?; + /// An uninitialized frame of `size` filling `reservation` at row pitch `pitch`. + fn pooled( + reservation: pool::Reservation, + size: Size, + pitch: u32, + color: Option, + ) -> Result { + debug_assert!(pitch >= size.width && size.width.is_multiple_of(2) && size.height.is_multiple_of(2)); Ok(Self { - buf: Arc::new(Buffer::Plain(raw)), - width, - height, - pitch, - color: None, - }) - } - - /// An uninitialized frame filling `reservation` at the aligned pitch. - fn pooled(reservation: pool::Reservation, size: Size, color: Option) -> Result { - let pitch = aligned_pitch(size.width)?; - Ok(Self { - buf: Arc::new(Buffer::Pooled(reservation.fill(nv12_len(size.height, pitch)?)?)), + buf: Arc::new(reservation.fill(nv12_len(size.height, pitch)?)?), width: size.width, height: size.height, pitch, @@ -288,38 +291,31 @@ impl Frame { /// A copy scaled to `size` (both dimensions even) on the GPU, for /// [`Surface::resize`](crate::Surface::resize), which falls back to the CPU - /// when this fails. A pooled frame draws the copy from its own pool, so a - /// full pool is one such failure; a GPU-only producer scales through - /// the encoder's pooled rendition scaler instead. + /// when this fails. The copy comes from this frame's own pool, so a full + /// bounded pool is one such failure; an NVENC session scales into its own + /// [`Target`] instead. pub(crate) fn resize(&self, size: Size) -> Result { size.validate("resize to")?; - let dst = match &*self.buf { - Buffer::Pooled(lease) => { - let pool = lease.pool(); - let reservation = pool.reserve().ok_or_else(|| { - Error::Unsupported(format!("GPU frame pool capacity {} exhausted", pool.capacity())) - })?; - Self::pooled(reservation, size, self.color)? - } - Buffer::Plain(raw) => { - let mut dst = Self::alloc(&raw.ctx, size.width, size.height, aligned_pitch(size.width)?)?; - dst.color = self.color; - dst - } - }; - self.scale_into(&dst)?; + let pool = self.buf.pool(); + let reservation = pool.reserve().ok_or_else(|| { + Error::Unsupported(format!( + "GPU frame pool of {} buffers exhausted", + pool.capacity().map_or(0, NonZeroUsize::get) + )) + })?; + let dst = Self::pooled(reservation, size, aligned_pitch(size.width)?, self.color)?; + self.scale_into(&dst, &self.buf.ctx.default_stream())?; Ok(dst) } - /// Scale this frame into `dst` with the box-filter kernels, on this frame's - /// device. - fn scale_into(&self, dst: &Self) -> Result<(), Error> { - let ctx = &self.buf.ctx; - let kernels = kernels(ctx)?; + /// Scale this frame into `dst` with the box-filter kernels on `stream`, a + /// stream of this frame's device, and wait for them. At the same size the + /// box filter is an exact copy. + fn scale_into(&self, dst: &Self, stream: &Arc) -> Result<(), Error> { + let kernels = kernels(&self.buf.ctx)?; let (width, height) = (dst.width, dst.height); let pitch = dst.pitch; - let stream = ctx.default_stream(); let block = (16u32, 16, 1); let grid = |w: u32, h: u32| (w.div_ceil(16), h.div_ceil(16), 1); let launch_err = |plane: &str, e| Error::codec(anyhow::anyhow!("CUDA resize {plane}: {e:?}")); @@ -374,8 +370,8 @@ impl Frame { } .map_err(|e| launch_err("chroma", e))?; - // The frame may head straight to NVENC (which does not order against - // our stream), so wait for the kernels rather than queueing. + // The frame may head straight to NVENC, which orders only against the + // default stream, so wait for the kernels rather than queueing. stream .synchronize() .map_err(|e| Error::codec(anyhow::anyhow!("CUDA resize sync: {e:?}"))) @@ -394,12 +390,11 @@ const CONVERT_PTX: &str = include_str!("rgba_to_nv12.ptx"); /// [`encode::Config::color`](crate::encode::Config::color) declares in the /// bitstream, and the frames it returns report that space through /// [`Frame::color`] so the two cannot silently disagree. Convert once at the -/// captured size and resize the result for smaller renditions, each into a -/// `Slot` from `reserve`; every buffer, converted or scaled, +/// captured size, into a `Slot` from `reserve`, and let each rendition's +/// encoder scale the result into its own [`Target`]. Every converted buffer /// comes from the pool sized by `capacity`, so a producer that outruns its /// encoder gets `None` from `reserve` instead of unbounded device memory. Drop -/// the frames the encoder has finished with (it retains its own clone until -/// then) and the buffers come back. +/// the frames the encoders have finished with and the buffers come back. /// /// The pixels are taken as the producer's final display-referred output: no /// transfer function is applied on the way to Y'CbCr, so an sRGB-encoded 8-bit @@ -425,8 +420,8 @@ impl std::fmt::Debug for Converter { impl Converter { /// Open CUDA device `ordinal`, load the conversion kernel, and size the - /// buffer pool. Two live frames per captured frame (the converted one and - /// one scaled copy) plus one of slack is a reasonable `capacity`. + /// buffer pool. One live frame per captured frame in flight, plus slack, is + /// a reasonable `capacity`. pub(crate) fn new(ordinal: usize, color: Color, capacity: NonZeroUsize) -> Result { let ctx = CudaContext::new(ordinal) .map_err(|e| Error::Unsupported(format!("CUDA device {ordinal} is unavailable: {e:?}")))?; @@ -436,7 +431,7 @@ impl Converter { .map_err(|e| Error::Unsupported(format!("CUDA color conversion unavailable: {e:?}")))?; Ok(Self { importer: cuda_vulkan::Importer::new(ordinal)?, - pool: Arc::new(Pool::new(Device(ctx.clone()), capacity)), + pool: Arc::new(Pool::new(Device(ctx.clone()), Some(capacity))), ctx, color, kernel, @@ -509,7 +504,7 @@ impl Slot { let size = frame.size(); size.validate("Vulkan/CUDA conversion of")?; - let dst = Frame::pooled(self.reservation, size, Some(self.color))?; + let dst = Frame::pooled(self.reservation, size, aligned_pitch(size.width)?, Some(self.color))?; let weights = self.color.coefficients(); let bgra = u32::from(frame.format() == vulkan::Format::Bgra8); let stream = frame.cuda_stream(); @@ -548,23 +543,56 @@ impl Slot { .map_err(|e| Error::codec(anyhow::anyhow!("CUDA color conversion sync: {e:?}")))?; Ok(dst) } +} - /// A copy of `frame` scaled to `size` (both dimensions even) with the - /// box-filter kernel, in the same color space. Fails without touching the - /// CPU when `frame` lives on another device or the driver refuses the - /// kernel. - pub(crate) fn resize(self, frame: &Frame, size: Size) -> Result { - if frame.buf.ctx.ordinal() != self.ctx.ordinal() { +/// One NV12 buffer that frames are scaled into, for a consumer that reads it +/// in place: an NVENC session registers it once and encodes every frame from +/// it. Scaling runs on the target's own stream, so it never waits on other +/// work in the process. +pub(crate) struct Target { + frame: Frame, + stream: Arc, +} + +impl std::fmt::Debug for Target { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Target") + .field("frame", &self.frame) + .finish_non_exhaustive() + } +} + +impl Target { + /// Allocate a `size` (both dimensions even) buffer on `ctx`'s device. + pub(crate) fn new(ctx: &Arc, size: Size) -> Result { + size.validate("CUDA target")?; + let frame = Frames::new(ctx).alloc(size, aligned_pitch(size.width)?)?; + let stream = ctx + .new_stream() + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA stream: {e:?}")))?; + Ok(Self { frame, stream }) + } + + /// The buffer, uninitialized until the first [`fill`](Self::fill). + pub(crate) fn frame(&self) -> &Frame { + &self.frame + } + + /// Scale `src` into the buffer, an exact copy at the same size, and wait + /// for it, so the consumer may read the buffer next. Fails without touching + /// the CPU when `src` lives on another device or the driver refuses the + /// kernels. + pub(crate) fn fill(&mut self, src: &Frame) -> Result<(), Error> { + if src.buf.ctx.ordinal() != self.frame.buf.ctx.ordinal() { return Err(Error::Unsupported(format!( "CUDA frame on device {} cannot be scaled on device {}", - frame.buf.ctx.ordinal(), - self.ctx.ordinal() + src.buf.ctx.ordinal(), + self.frame.buf.ctx.ordinal() ))); } - size.validate("resize to")?; - let dst = Frame::pooled(self.reservation, size, frame.color)?; - frame.scale_into(&dst)?; - Ok(dst) + src.scale_into(&self.frame, &self.stream)?; + self.frame.color = src.color; + Ok(()) } } diff --git a/rs/moq-video/src/frame/cuda_test.rs b/rs/moq-video/src/frame/cuda_test.rs index a0506bfe1bb..9e82aac26ea 100644 --- a/rs/moq-video/src/frame/cuda_test.rs +++ b/rs/moq-video/src/frame/cuda_test.rs @@ -96,6 +96,8 @@ fn nvenc(size: Size, color: Color) -> crate::encode::Encoder { config.kind = crate::encode::Kind::Named("nvenc".into()); config.color = Some(color); config.join = std::time::Duration::from_secs(1); + // A larger CUDA frame is scaled by NVENC into its own registered input. + config.resize = Some(crate::resize::Config::default()); crate::encode::Encoder::new(&config).expect("open NVENC") } @@ -153,11 +155,10 @@ async fn vulkan_cuda_convert_resize_encode() { assert!(max_diff(actual.u(), expected.u()) <= 1, "u differs from the reference"); assert!(max_diff(actual.v(), expected.v()) <= 1, "v differs from the reference"); - let scaled = converter - .reserve() - .expect("a free buffer") - .resize(&converted, sd) - .expect("GPU resize"); + let ctx = CudaContext::new(0).expect("CUDA device 0"); + let mut target = Target::new(&ctx, sd).expect("CUDA target"); + target.fill(&converted).expect("GPU resize"); + let scaled = target.frame().clone(); assert_eq!(scaled.size(), sd); assert_eq!(scaled.color(), Some(color)); let actual = scaled.download_i420().unwrap(); @@ -179,15 +180,12 @@ async fn vulkan_cuda_convert_resize_encode() { // allocated. let third = converter.reserve().expect("third buffer"); assert!(converter.reserve().is_none(), "a full pool must refuse"); - // A slot dropped unused, or consumed by a failed resize, gives its buffer - // back. + // A slot dropped unused gives its buffer back. drop(third); - let odd = converter.reserve().expect("an unfilled slot returned its buffer"); - odd.resize(&converted, Size::new(81, 49)) - .expect_err("an odd size is an error"); + Target::new(&ctx, Size::new(81, 49)).expect_err("an odd size is an error"); let third = converter .reserve() - .expect("a failed resize returned its buffer") + .expect("an unfilled slot returned its buffer") .convert(&frame) .expect("third conversion"); assert!(converter.reserve().is_none()); @@ -196,7 +194,7 @@ async fn vulkan_cuda_convert_resize_encode() { drop(third.convert(&frame).expect("convert into a reused buffer")); eprintln!("pool bound held at capacity 3"); - drop((converted, scaled, frame)); + drop((converted, scaled, target, frame)); let slot = completion.wait().await.expect("completion worker stopped"); drop(slot); @@ -235,11 +233,6 @@ async fn vulkan_cuda_convert_resize_encode() { assert!(max_diff(actual.u(), reference.u()) <= 1); assert!(max_diff(actual.v(), reference.v()) <= 1); } - let scaled = converter - .reserve() - .expect("a free buffer") - .resize(&converted, sd) - .expect("GPU resize"); drop(frame); let timestamp = moq_net::Timestamp::from_micros(i * 33_333).unwrap(); @@ -248,7 +241,7 @@ async fn vulkan_cuda_convert_resize_encode() { sd_encoder.cut().expect("NVENC cuts on request"); } let packets = hd - .encode_all(&VideoFrame::new(Surface::Cuda(converted), timestamp)) + .encode_all(&VideoFrame::new(Surface::Cuda(converted.clone()), timestamp)) .unwrap(); assert_eq!(packets.len(), 1, "one access unit per frame at {i}"); assert_eq!(packets[0].timestamp, timestamp, "timestamp preserved at {i}"); @@ -266,8 +259,9 @@ async fn vulkan_cuda_convert_resize_encode() { expected = Some(reference(&gradient(size, Format::Rgba8, i as usize * 4), size, color)); } + // The full-size buffer: the SD session scales it into its own input. let packets = sd_encoder - .encode_all(&VideoFrame::new(Surface::Cuda(scaled), timestamp)) + .encode_all(&VideoFrame::new(Surface::Cuda(converted), timestamp)) .unwrap(); assert_eq!(packets.len(), 1); assert_eq!(packets[0].timestamp, timestamp); @@ -369,9 +363,9 @@ async fn vulkan_cuda_three_view_workload() { // frozen frame no P-slice has to code. let pictures: Vec> = (0..8).map(|i| gradient(size, Format::Rgba8, i * 37)).collect(); - // Two live buffers per view (the converted frame and its scaled copy) plus - // one of slack, the sizing the `Converter` docs recommend. - let converter = Converter::new(0, color, NonZeroUsize::new(2 * VIEWS + 1).unwrap()).expect("CUDA converter"); + // One live buffer per view plus one of slack, the sizing the `Converter` + // docs recommend: each SD session scales into its own input. + let converter = Converter::new(0, color, NonZeroUsize::new(VIEWS + 1).unwrap()).expect("CUDA converter"); let mut views = Vec::new(); for _ in 0..VIEWS { @@ -383,7 +377,7 @@ async fn vulkan_cuda_three_view_workload() { } let mut upload = Stage::default(); - let (mut publish, mut convert, mut resize) = (Stage::default(), Stage::default(), Stage::default()); + let (mut publish, mut convert) = (Stage::default(), Stage::default()); let (mut encode_hd, mut encode_sd, mut complete) = (Stage::default(), Stage::default(), Stage::default()); let mut bytes = (0usize, 0usize); let mut late = 0u64; @@ -396,7 +390,6 @@ async fn vulkan_cuda_three_view_workload() { upload = Stage::default(); publish = Stage::default(); convert = Stage::default(); - resize = Stage::default(); encode_hd = Stage::default(); encode_sd = Stage::default(); complete = Stage::default(); @@ -420,16 +413,13 @@ async fn vulkan_cuda_three_view_workload() { .measure(|| converter.reserve().expect("a free buffer").convert(&frame)) .expect("convert"); drop(frame); - let scaled = resize - .measure(|| converter.reserve().expect("a free buffer").resize(&converted, sd)) - .expect("resize"); let packets = encode_hd - .measure(|| hd.encode_all(&VideoFrame::new(Surface::Cuda(converted), timestamp))) + .measure(|| hd.encode_all(&VideoFrame::new(Surface::Cuda(converted.clone()), timestamp))) .expect("encode HD"); bytes.0 += packets.iter().map(|p| p.payload.len()).sum::(); let packets = encode_sd - .measure(|| small.encode_all(&VideoFrame::new(Surface::Cuda(scaled), timestamp))) + .measure(|| small.encode_all(&VideoFrame::new(Surface::Cuda(converted), timestamp))) .expect("encode SD"); bytes.1 += packets.iter().map(|p| p.payload.len()).sum::(); @@ -457,9 +447,8 @@ async fn vulkan_cuda_three_view_workload() { let pipeline: Duration = [ ("publish", &publish), ("convert", &convert), - ("resize", &resize), ("encode hd", &encode_hd), - ("encode sd", &encode_sd), + ("encode sd (scales)", &encode_sd), ("complete", &complete), ] .iter() diff --git a/rs/moq-video/src/frame/pool.rs b/rs/moq-video/src/frame/pool.rs index 3939f34b0d9..b3d3b0325e8 100644 --- a/rs/moq-video/src/frame/pool.rs +++ b/rs/moq-video/src/frame/pool.rs @@ -1,11 +1,16 @@ -//! A bounded pool of device allocations for the GPU frame path. +//! A pool of device allocations for the GPU frame path. //! -//! Every frame the GPU conversion or resize produces draws its buffer from -//! here, so the memory a stream can hold at once is a fixed number of buffers -//! rather than however many frames an encoder falls behind by. A buffer goes -//! back when its last frame drops, and a later frame of the same or a smaller -//! size reuses it; `cuMemFree` synchronizes the whole device, so recycling -//! also keeps a free off the per-frame path. +//! Every CUDA frame draws its buffer from here. A buffer goes back when its +//! last frame drops, and a later frame of the same or a smaller size reuses +//! it: `cuMemAlloc` and `cuMemFree` go through the driver's per-GPU lock, which +//! every process on the device shares, and `cuMemFree` also synchronizes the +//! device, so neither belongs on the per-frame path. +//! +//! A bounded pool caps the buffers a stream holds at once, so a producer +//! that outruns its encoder is refused rather than handed unbounded device +//! memory. An unbounded one serves a decoder, which cannot refuse a picture: +//! its consumers bound how many frames stay alive, and the pool keeps at most +//! as many buffers as were ever alive together. use std::num::NonZeroUsize; use std::sync::{Arc, Mutex}; @@ -24,7 +29,8 @@ pub(crate) trait Alloc { pub(crate) struct Pool { alloc: A, - capacity: usize, + /// The most buffers held at once, live and idle together; `None` never refuses. + capacity: Option, state: Mutex>, } @@ -36,11 +42,12 @@ struct State { } impl Pool { - /// A pool holding at most `capacity` buffers, live and idle together. - pub(crate) fn new(alloc: A, capacity: NonZeroUsize) -> Self { + /// A pool holding at most `capacity` buffers, live and idle together, or + /// any number for `None`. + pub(crate) fn new(alloc: A, capacity: Option) -> Self { Self { alloc, - capacity: capacity.get(), + capacity, state: Mutex::new(State { live: 0, idle: Vec::new(), @@ -48,16 +55,17 @@ impl Pool { } } - /// The most buffers this pool holds at once. - pub(crate) fn capacity(&self) -> usize { + /// The most buffers this pool holds at once, or `None` when unbounded. + pub(crate) fn capacity(&self) -> Option { self.capacity } - /// Hold one of the pool's buffers, or `None` while every one is live. The - /// buffer itself is picked by [`Reservation::fill`], once its size is known. + /// Hold one of the pool's buffers, or `None` while a bounded pool's every + /// one is live. The buffer itself is picked by [`Reservation::fill`], once + /// its size is known. pub(crate) fn reserve(self: &Arc) -> Option> { let mut state = self.state.lock().expect("GPU frame pool poisoned"); - if state.live >= self.capacity { + if self.capacity.is_some_and(|capacity| state.live >= capacity.get()) { return None; } state.live += 1; @@ -73,9 +81,9 @@ pub(crate) struct Reservation(Held); impl Reservation { /// A buffer of at least `len` bytes: the smallest idle one that fits, else a - /// fresh allocation while under capacity, else an idle one too small for the - /// job freed to make room. Only a failed allocation is an error, and it - /// releases the reservation. + /// fresh allocation, freeing an idle one too small for the job when a + /// bounded pool is at capacity. Only a failed allocation is an error, and + /// it releases the reservation. pub(crate) fn fill(self, len: usize) -> Result, Error> { let mut held = self.0; let pool = &held.pool; @@ -92,7 +100,10 @@ impl Reservation { None => { // `live` counts this reservation, so an idle buffer exists // whenever the two together exceed capacity. - if state.live + state.idle.len() > pool.capacity { + if pool + .capacity + .is_some_and(|capacity| state.live + state.idle.len() > capacity.get()) + { let smallest = state .idle .iter() @@ -162,10 +173,7 @@ mod tests { } fn pool(capacity: usize) -> Arc> { - Arc::new(Pool::new( - Counting(AtomicUsize::new(0)), - NonZeroUsize::new(capacity).unwrap(), - )) + Arc::new(Pool::new(Counting(AtomicUsize::new(0)), NonZeroUsize::new(capacity))) } fn take(pool: &Arc>, len: usize) -> Lease { @@ -175,7 +183,7 @@ mod tests { #[test] fn reserve_yields_capacity_places_then_none() { let pool = pool(2); - assert_eq!(pool.capacity(), 2); + assert_eq!(pool.capacity(), NonZeroUsize::new(2)); let a = pool.reserve().unwrap(); let _b = pool.reserve().unwrap(); assert!(pool.reserve().is_none(), "a full pool refuses rather than grows"); @@ -236,6 +244,22 @@ mod tests { assert!(pool.reserve().is_none(), "both buffers are live now"); } + #[test] + fn an_unbounded_pool_recycles_instead_of_refusing() { + let pool = Arc::new(Pool::new(Counting(AtomicUsize::new(0)), None)); + let live: Vec<_> = (0..20).map(|_| take(&pool, 100)).collect(); + drop(live); + assert_eq!(pool.alloc.0.load(Ordering::Relaxed), 20); + + // A steady stream at that depth allocates nothing more. + for _ in 0..100 { + let frames: Vec<_> = (0..20).map(|_| take(&pool, 100)).collect(); + drop(frames); + } + assert_eq!(pool.alloc.0.load(Ordering::Relaxed), 20, "held buffers are reused"); + assert_eq!(pool.state.lock().unwrap().idle.len(), 20); + } + #[test] fn a_failed_allocation_releases_the_reservation() { /// Fails its first allocation, then succeeds. @@ -252,7 +276,7 @@ mod tests { } } - let pool = Arc::new(Pool::new(Flaky(AtomicBool::new(true)), NonZeroUsize::new(1).unwrap())); + let pool = Arc::new(Pool::new(Flaky(AtomicBool::new(true)), NonZeroUsize::new(1))); assert!(matches!(pool.reserve().unwrap().fill(8), Err(Error::Codec(_)))); assert_eq!(pool.state.lock().unwrap().live, 0); pool.reserve() From 6bbaca333b3f9f96bc49cc5bfbb91261aa3698b2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 15:46:38 -0800 Subject: [PATCH 2/9] test(moq-video): fill the converter pool with two conversions now that scaling bypasses it Co-Authored-By: Claude Opus 5.5 --- rs/moq-video/src/frame/cuda_test.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rs/moq-video/src/frame/cuda_test.rs b/rs/moq-video/src/frame/cuda_test.rs index 9e82aac26ea..59d164d6998 100644 --- a/rs/moq-video/src/frame/cuda_test.rs +++ b/rs/moq-video/src/frame/cuda_test.rs @@ -176,8 +176,13 @@ async fn vulkan_cuda_convert_resize_encode() { assert!(mae(actual.u(), expected_sd.u()) < 4, "scaled u disagrees with the CPU"); assert!(mae(actual.v(), expected_sd.v()) < 4, "scaled v disagrees with the CPU"); - // Three buffers live fills the pool; a fourth is not reserved, let alone - // allocated. + // Three buffers live fills the pool (the scaled copy lives in the target, + // not the pool); a fourth is not reserved, let alone allocated. + let second = converter + .reserve() + .expect("second buffer") + .convert(&frame) + .expect("second conversion"); let third = converter.reserve().expect("third buffer"); assert!(converter.reserve().is_none(), "a full pool must refuse"); // A slot dropped unused gives its buffer back. @@ -194,7 +199,7 @@ async fn vulkan_cuda_convert_resize_encode() { drop(third.convert(&frame).expect("convert into a reused buffer")); eprintln!("pool bound held at capacity 3"); - drop((converted, scaled, target, frame)); + drop((converted, second, scaled, target, frame)); let slot = completion.wait().await.expect("completion worker stopped"); drop(slot); From 000bcdf45f534fa859cd88ab522360485a6d599f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 16:03:34 -0800 Subject: [PATCH 3/9] fix(moq-video): retire a CUDA target NVENC may still hold, and bound the unbounded pool by its peak A failed completion or unmap left the target in place for the next frame to overwrite while NVENC might still read it; the encoder now owns it locally and puts it back only after a clean encode. An unbounded pool kept every idle buffer it outgrew; a fresh allocation now replaces the smallest idle one. MappedResource::unmap reports the failure a drop would swallow. Co-Authored-By: Claude Opus 5.5 --- rs/moq-nvenc/src/safe/buffer.rs | 41 +++++++++++++++++++---- rs/moq-video/src/encode/backend/nvenc.rs | 14 ++++++-- rs/moq-video/src/frame/pool.rs | 42 ++++++++++++++---------- 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/rs/moq-nvenc/src/safe/buffer.rs b/rs/moq-nvenc/src/safe/buffer.rs index 6cb9ceaf407..8b1679725bf 100644 --- a/rs/moq-nvenc/src/safe/buffer.rs +++ b/rs/moq-nvenc/src/safe/buffer.rs @@ -708,6 +708,19 @@ pub struct MappedResource<'a, T> { pitch: u32, } +impl MappedResource<'_, T> { + /// Unmap now, reporting a failure that dropping would swallow. + /// + /// # Errors + /// + /// Could error if the driver cannot unmap the resource. NVENC may then + /// still hold it, so the registration and its allocation are leaked and + /// the allocation must not be written again. + pub fn unmap(self) -> Result<(), EncodeError> { + self.mapped.unmap() + } +} + /// A registered external resource plus the owner keeping its allocation alive. #[derive(Debug)] struct Registration { @@ -773,15 +786,26 @@ impl Mapped<'_, A, T> { let registration = unsafe { ptr::read(&this.registration) }; registration.leaked = true; } + + fn unmap(self) -> Result<(), EncodeError> { + let mut this = ManuallyDrop::new(self); + this.release() + } + + /// A failed unmap leaves the resource mapped. Unregistering or freeing it + /// could release memory NVENC still holds, so leak the owner instead. + fn release(&mut self) -> Result<(), EncodeError> { + let result = self.registration.api.unmap_input_resource(self.map_ptr); + if result.is_err() { + self.registration.leaked = true; + } + result + } } impl Drop for Mapped<'_, A, T> { fn drop(&mut self) { - // A failed unmap leaves the resource mapped. Unregistering or freeing - // it could release memory NVENC still holds, so leak the owner instead. - if self.registration.api.unmap_input_resource(self.map_ptr).is_err() { - self.registration.leaked = true; - } + let _ = self.release(); } } @@ -981,7 +1005,12 @@ mod tests { api.unmap_error = Some(ErrorKind::ResourceNotMapped); let mut registration = register(&api, owner).expect("registration should succeed"); - drop(registration.map().expect("mapping should succeed")); + let error = registration + .map() + .expect("mapping should succeed") + .unmap() + .expect_err("unmapping should fail"); + assert_eq!(error.kind(), ErrorKind::ResourceNotMapped); drop(registration); assert!(alive.get(), "a possibly mapped allocation must remain owned"); diff --git a/rs/moq-video/src/encode/backend/nvenc.rs b/rs/moq-video/src/encode/backend/nvenc.rs index f207255aa43..d246e13b6d9 100644 --- a/rs/moq-video/src/encode/backend/nvenc.rs +++ b/rs/moq-video/src/encode/backend/nvenc.rs @@ -332,11 +332,15 @@ impl Backend for Nvenc { // guarantees NVENC is done reading the input, so releasing the input // first would race the encoder. let data = if let Some(gpu) = gpu { - let target = match self.target.take() { + // Held locally and put back only after a clean encode: NVENC may + // still read a target whose completion or unmap failed, so any failure + // drops it instead and it is never written again (such a registration + // leaks its allocation rather than free it). The next frame gets a new + // target. + let mut target = match self.target.take() { Some(target) => target, None => Target::new(&self.session, &self.cuda, self.size)?, }; - let target = self.target.insert(target); // Written only while unmapped, and finished before mapping: NVENC // orders only against the default stream, which the target avoids. target.buffer.fill(gpu)?; @@ -348,8 +352,12 @@ impl Backend for Nvenc { .session .encode_picture(mapped, output, params) .map_err(|e| Error::codec(anyhow::anyhow!("NVENC encode: {e}")))?; - let (data, _mapped, output) = drain_output(submission)?; + let (data, mapped, output) = drain_output(submission)?; self.output = Some(output); + mapped + .unmap() + .map_err(|e| Error::codec(anyhow::anyhow!("NVENC unmap CUDA input: {e}")))?; + self.target = Some(target); data } else { // Everything else goes through a CPU NV12 input buffer. diff --git a/rs/moq-video/src/frame/pool.rs b/rs/moq-video/src/frame/pool.rs index b3d3b0325e8..61c1d1a00d7 100644 --- a/rs/moq-video/src/frame/pool.rs +++ b/rs/moq-video/src/frame/pool.rs @@ -81,9 +81,9 @@ pub(crate) struct Reservation(Held); impl Reservation { /// A buffer of at least `len` bytes: the smallest idle one that fits, else a - /// fresh allocation, freeing an idle one too small for the job when a - /// bounded pool is at capacity. Only a failed allocation is an error, and - /// it releases the reservation. + /// fresh allocation replacing the smallest idle one, all too small for the + /// job, so the pool never holds more buffers than were ever live at once. + /// Only a failed allocation is an error, and it releases the reservation. pub(crate) fn fill(self, len: usize) -> Result, Error> { let mut held = self.0; let pool = &held.pool; @@ -98,20 +98,14 @@ impl Reservation { let buffer = match fits { Some(index) => state.idle.swap_remove(index), None => { - // `live` counts this reservation, so an idle buffer exists - // whenever the two together exceed capacity. - if pool - .capacity - .is_some_and(|capacity| state.live + state.idle.len() > capacity.get()) - { - let smallest = state - .idle - .iter() - .enumerate() - .min_by_key(|(_, (have, _))| *have) - .map(|(index, _)| index) - .expect("an idle buffer exists past capacity"); - state.idle.swap_remove(smallest); + let smallest = state + .idle + .iter() + .enumerate() + .min_by_key(|(_, (have, _))| *have) + .map(|(index, _)| index); + if let Some(index) = smallest { + state.idle.swap_remove(index); } (len, pool.alloc.alloc(len)?) } @@ -230,6 +224,20 @@ mod tests { assert_eq!(pool.alloc.0.load(Ordering::Relaxed), 3); } + #[test] + fn an_unbounded_pool_replaces_buffers_it_outgrows() { + let pool = Arc::new(Pool::new(Counting(AtomicUsize::new(0)), None)); + let _live = take(&pool, 10); + for len in 11..100 { + drop(take(&pool, len)); + } + assert_eq!( + pool.state.lock().unwrap().idle.len(), + 1, + "one buffer was ever idle at once" + ); + } + #[test] fn an_idle_buffer_too_small_is_replaced_within_capacity() { let pool = pool(2); From 1f0bb7e838c27380475624c60b29006d2fc99f9f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 19:36:36 -0800 Subject: [PATCH 4/9] fix(moq-nvenc): record an outstanding mapping on the registration, so forgetting it leaks instead of freeing A MappedResource borrows its RegisteredResource, so mem::forget on it (or on the Submission holding it) ended the borrow without marking the registration, whose drop then unregistered and freed an allocation NVENC could still read. The registration now marks itself mapped before handing out the mapping and clears it only on a successful unmap; while marked, drop leaks and map refuses. Co-Authored-By: Claude Opus 5.5 --- rs/moq-nvenc/src/safe/buffer.rs | 104 +++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 22 deletions(-) diff --git a/rs/moq-nvenc/src/safe/buffer.rs b/rs/moq-nvenc/src/safe/buffer.rs index 8b1679725bf..d40685891ef 100644 --- a/rs/moq-nvenc/src/safe/buffer.rs +++ b/rs/moq-nvenc/src/safe/buffer.rs @@ -4,7 +4,11 @@ use std::{ffi::c_void, mem::ManuallyDrop, ptr, sync::Arc}; use cudarc::driver::{DevicePtr, MappedBuffer}; -use super::{encoder::Encoder, result::EncodeError, session::Session}; +use super::{ + encoder::Encoder, + result::{EncodeError, ErrorKind}, + session::Session, +}; use crate::sys::nvEncodeAPI::{ NV_ENC_BUFFER_FORMAT, NV_ENC_CREATE_BITSTREAM_BUFFER, NV_ENC_CREATE_BITSTREAM_BUFFER_VER, NV_ENC_CREATE_INPUT_BUFFER, NV_ENC_CREATE_INPUT_BUFFER_VER, NV_ENC_INPUT_RESOURCE_TYPE, NV_ENC_LOCK_BITSTREAM, @@ -690,8 +694,11 @@ impl RegisteredResource { /// /// # Errors /// - /// Could error if the driver cannot map the resource. The registration - /// stays usable. + /// Could error if the driver cannot map the resource; the registration + /// stays usable. Errors with [`ErrorKind::InvalidCall`](super::ErrorKind::InvalidCall) + /// if an earlier mapping was never unmapped, because it was forgotten, + /// abandoned, or failed to unmap: NVENC may still hold it, so the + /// registration is retired and its allocation must not be written again. pub fn map(&mut self) -> Result, EncodeError> { let pitch = self.pitch; Ok(MappedResource { @@ -728,9 +735,10 @@ struct Registration { api: A, // Dropped only once the resource is unregistered. marker: ManuallyDrop, - /// Set when NVENC may still hold a mapping, so the registration and the - /// owner of its memory are leaked rather than released. - leaked: bool, + /// Set before a mapping is handed out and cleared only once it unmaps, so a + /// mapping that never does (forgotten, abandoned, or failed) leaves the + /// registration and the owner of its memory leaked rather than released. + mapped: bool, } impl Registration { @@ -742,12 +750,19 @@ impl Registration { reg_ptr, api, marker: ManuallyDrop::new(marker), - leaked: false, + mapped: false, }) } fn map(&mut self) -> Result, EncodeError> { + if self.mapped { + return Err(EncodeError::new( + ErrorKind::InvalidCall, + Some("an earlier mapping was never unmapped".into()), + )); + } let map_ptr = self.api.map_input_resource(self.reg_ptr)?; + self.mapped = true; Ok(Mapped { registration: self, map_ptr, @@ -758,7 +773,8 @@ impl Registration { /// Unregister the external resource when it goes out of scope. impl Drop for Registration { fn drop(&mut self) { - if self.leaked { + // NVENC may still read a mapping that never unmapped. + if self.mapped { return; } // NVENC may still refer to an allocation it failed to unregister, so @@ -778,13 +794,10 @@ struct Mapped<'a, A: ResourceApi, T> { } impl Mapped<'_, A, T> { - /// Give up a mapping the driver may still be reading: never unmap it, and - /// never unregister or release its allocation. + /// Give up a mapping the driver may still be reading: never unmap it, so the + /// registration stays marked mapped and is never unregistered or released. fn abandon(self) { - let this = ManuallyDrop::new(self); - // SAFETY: read once from a value whose destructor never runs. - let registration = unsafe { ptr::read(&this.registration) }; - registration.leaked = true; + std::mem::forget(self); } fn unmap(self) -> Result<(), EncodeError> { @@ -792,14 +805,12 @@ impl Mapped<'_, A, T> { this.release() } - /// A failed unmap leaves the resource mapped. Unregistering or freeing it - /// could release memory NVENC still holds, so leak the owner instead. + /// A failed unmap leaves the registration marked mapped: unregistering or + /// freeing it could release memory NVENC still holds. fn release(&mut self) -> Result<(), EncodeError> { - let result = self.registration.api.unmap_input_resource(self.map_ptr); - if result.is_err() { - self.registration.leaked = true; - } - result + self.registration.api.unmap_input_resource(self.map_ptr)?; + self.registration.mapped = false; + Ok(()) } } @@ -837,7 +848,6 @@ mod tests { }; use super::*; - use crate::safe::result::ErrorKind; #[derive(Debug, Default, PartialEq, Eq)] struct Calls { @@ -1046,6 +1056,56 @@ mod tests { ); } + #[test] + fn a_forgotten_mapping_retains_the_owner() { + let (owner, alive) = setup(); + let api = TestApi::new(alive.clone()); + + let mut registration = register(&api, owner).expect("registration should succeed"); + // Safe code can skip every destructor, so this must not rely on one. + std::mem::forget(registration.map().expect("mapping should succeed")); + drop(registration); + + assert!(alive.get(), "an allocation the driver may still read must remain owned"); + assert_eq!( + *api.calls.borrow(), + Calls { + register: 1, + map: 1, + unmap: 0, + unregister: 0, + } + ); + } + + #[test] + fn a_mapping_never_unmapped_refuses_the_next() { + let (owner, alive) = setup(); + let mut api = TestApi::new(alive.clone()); + api.unmap_error = Some(ErrorKind::ResourceNotMapped); + + let mut registration = register(&api, owner).expect("registration should succeed"); + std::mem::forget(registration.map().expect("mapping should succeed")); + let error = registration + .map() + .expect_err("a forgotten mapping should refuse the next"); + assert_eq!(error.kind(), ErrorKind::InvalidCall); + + let mut registration = register(&api, Owner(alive.clone())).expect("registration should succeed"); + registration.map().expect("mapping should succeed").abandon(); + let error = registration + .map() + .expect_err("an abandoned mapping should refuse the next"); + assert_eq!(error.kind(), ErrorKind::InvalidCall); + + let mut registration = register(&api, Owner(alive.clone())).expect("registration should succeed"); + let _ = registration.map().expect("mapping should succeed").unmap(); + let error = registration.map().expect_err("a failed unmap should refuse the next"); + assert_eq!(error.kind(), ErrorKind::InvalidCall); + + assert_eq!(api.calls.borrow().map, 3, "a refused mapping never reaches the driver"); + } + #[test] fn failed_unregister_on_drop_retains_the_owner() { let (owner, alive) = setup(); From 4c37d2ad785582ca96a48ead8566d6e62e1ac8d6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 20:41:06 -0800 Subject: [PATCH 5/9] docs(moq-nvenc): say a never-unmapped mapping retires its registration Co-Authored-By: Claude Opus 5.5 --- rs/moq-nvenc/CHANGELOG.md | 1 + rs/moq-nvenc/README.md | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/rs/moq-nvenc/CHANGELOG.md b/rs/moq-nvenc/CHANGELOG.md index b6f0d5ff3fe..a1352b7ffd4 100644 --- a/rs/moq-nvenc/CHANGELOG.md +++ b/rs/moq-nvenc/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - `Session::register_generic_resource` and `Session::register_cuda_resource` only register the allocation; call `RegisteredResource::map` for each encode and pass the returned `MappedResource` to `Session::encode_picture`. Registration is the expensive call, so a reused input registers once. +- `RegisteredResource::map` refuses with `ErrorKind::InvalidCall` once an earlier mapping never unmapped (it failed to unmap, or its submission was abandoned or forgotten), and such a registration never releases its allocation. ## [0.1.2](https://github.com/moq-dev/moq/compare/moq-nvenc-v0.1.1...moq-nvenc-v0.1.2) - 2026-09-26 diff --git a/rs/moq-nvenc/README.md b/rs/moq-nvenc/README.md index 2b99189e1b2..7d7f2ab3fba 100644 --- a/rs/moq-nvenc/README.md +++ b/rs/moq-nvenc/README.md @@ -29,8 +29,11 @@ alive until synchronous completion. Raw SDK structs remain available under An external input is registered once and mapped for each encode, as NVENC expects: registration is the expensive call, and the allocation may be written -only while unmapped. When an unmap or unregister fails, or a submission is -abandoned, the allocation stays owned because the driver may still refer to it. +only while unmapped. A registration marks itself mapped before handing out a +mapping and clears the mark only once that mapping unmaps, so when an unmap or +unregister fails, or a submission is abandoned or forgotten, the allocation +stays owned because the driver may still refer to it, and no later mapping is +allowed. The `sys` bindings are generated with bindgen from the vendored headers (`src/sys/headers/`); see the [upstream repo](https://github.com/ViliamVadocz/nvidia-video-codec-sdk) From 57960121fc2c5db58b49aa35f6ca3f81059fa174 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 21:43:21 -0800 Subject: [PATCH 6/9] fix(moq-video): hold a frame the backend scales to the color its size implies Delegating the resize to NVENC skipped Frame::resize, which labels an unlabelled source with Color::infer at its own size. An unlabelled 720p CUDA frame then reached a 480p encoder unchecked and was encoded under a BT.601 label instead of being refused. Co-Authored-By: Claude Opus 5.5 --- rs/moq-video/src/encode/encoder.rs | 82 +++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/rs/moq-video/src/encode/encoder.rs b/rs/moq-video/src/encode/encoder.rs index ec1e2c01bc2..035c1e40707 100644 --- a/rs/moq-video/src/encode/encoder.rs +++ b/rs/moq-video/src/encode/encoder.rs @@ -581,7 +581,14 @@ impl Encoder { // standard-definition boundary, where the pixels keep their space but the // encoder was sized into another one, and by a source whose space changes // mid-stream. - if let Some(color) = frame.surface.color() + // A frame the backend scales keeps its samples' space, and an unlabelled + // one means what `Color::infer` guesses for its own size, as + // `Frame::resize` would have labelled the copy. + let color = match frame.surface.color() { + None if scaled && !external && size != self.size => Some(Color::infer(size)), + color => color, + }; + if let Some(color) = color && color != self.color { return Err(Error::ColorMismatch { @@ -1319,6 +1326,79 @@ mod tests { } } + /// Scales every surface itself, the way NVENC scales CUDA frames into its + /// input, and records the size each frame reached it at. + struct Scaling { + sizes: std::sync::Arc>>, + } + + impl Backend for Scaling { + fn encode(&mut self, frame: &Frame, _cut: bool) -> Result<(), Error> { + self.sizes.lock().unwrap().push(frame.size()); + Ok(()) + } + + fn receive(&mut self) -> Result, Error> { + Ok(None) + } + + fn flush(&mut self) -> Result<(), Error> { + Ok(()) + } + + fn set_bitrate(&mut self, _bitrate: u64) -> Result<(), Error> { + Ok(()) + } + + fn can_cut(&self) -> bool { + true + } + + fn scales(&self, _surface: &crate::Surface) -> bool { + true + } + + fn name(&self) -> &'static str { + "scaling" + } + } + + /// A backend that scales the frame itself is handed it at its own size, but + /// the frame is held to the space it would have after `Frame::resize`: an + /// unlabelled 720p frame is BT.709, which a 480p encoder would label BT.601. + #[test] + fn a_frame_the_backend_scales_keeps_its_implied_color() { + use crate::Color; + + let unlabelled = |size: Size| { + let i420 = I420::new(size, vec![0x80; I420::len(size).unwrap()]).unwrap(); + Frame::new(Surface::I420(i420), moq_net::Timestamp::from_micros(0).unwrap()) + }; + let mut config = Config::new(640, 480, crate::FrameRate::new(30, 1).unwrap()); + config.resize = Some(crate::resize::Config::default()); + let sizes = std::sync::Arc::new(std::sync::Mutex::new(Vec::new())); + let mut encoder = encoder_with(Box::new(Scaling { sizes: sizes.clone() }), &config); + + let err = encoder + .encode(&unlabelled(Size::new(1280, 720))) + .expect_err("an implied BT.709 frame is refused"); + assert!( + matches!( + err, + Error::ColorMismatch { + frame: Color::BT709, + encoder: Color::BT601 + } + ), + "{err}" + ); + + // Within the same space, the backend gets the frame unscaled. + encoder.encode(&unlabelled(Size::new(704, 576))).unwrap(); + encoder.encode(&unlabelled(Size::new(640, 480))).unwrap(); + assert_eq!(*sizes.lock().unwrap(), [Size::new(704, 576), Size::new(640, 480)]); + } + /// Records the cut flag each frame reached the codec with, so a test can /// check what the encoder actually asked for. `cuts` is whether it claims to /// honor one, the way a V4L2 driver without the force-keyframe control does From c2cefadd9f02d32c9dcdbdc276320bf7518fd46f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 22:04:29 -0800 Subject: [PATCH 7/9] fix(moq-video): copy same-size CUDA frames without PTX, and scale in NVENC only where the kernels load Every CUDA frame went through the resize kernels into the session's target, so a driver that refuses the vendored PTX failed every CUDA encode, where it used to register a same-size frame directly and fall back to a CPU resize otherwise. A same-size fill is now a 2D device copy, and NVENC claims Backend::scales only when the kernels loaded at open, leaving Frame::resize's CPU fallback to the rest. The SD rendition of vulkan_cuda_convert_resize_encode now decodes and checks every picture's size and pixels. Co-Authored-By: Claude Opus 5.5 --- rs/moq-video/src/encode/backend/nvenc.rs | 11 +++-- rs/moq-video/src/frame/cuda.rs | 60 ++++++++++++++++++++++-- rs/moq-video/src/frame/cuda_test.rs | 21 +++++++-- 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/rs/moq-video/src/encode/backend/nvenc.rs b/rs/moq-video/src/encode/backend/nvenc.rs index d246e13b6d9..17ed8a7d3fd 100644 --- a/rs/moq-video/src/encode/backend/nvenc.rs +++ b/rs/moq-video/src/encode/backend/nvenc.rs @@ -24,8 +24,8 @@ //! //! The session's input format is NV12 (NVENC's native layout). A CPU I420 frame //! is interleaved into an NVENC input buffer; a CUDA frame ([`Surface::Cuda`], -//! NVDEC output, already NV12) is scaled on the GPU into the session's own -//! [`cuda::Target`], an exact copy at the session size, so the NVDEC -> NVENC +//! NVDEC output, already NV12) is copied, or scaled on the GPU when resizing, +//! into the session's own [`cuda::Target`], so the NVDEC -> NVENC //! transcode path never touches the CPU. The target, the input buffer, and the //! output bitstream are each created and registered once per session: those //! calls take the driver's per-GPU lock, which every session in every process @@ -96,6 +96,10 @@ pub(crate) struct Nvenc { codec: Codec, size: crate::Size, converter: Option, + /// Whether CUDA frames are scaled into the target here: only for a resizing + /// encoder on a driver that loads the resize kernels, so one that refuses + /// them keeps `Frame::resize`'s CPU fallback. + scales: bool, ready: VecDeque, } @@ -272,6 +276,7 @@ impl Nvenc { .input .map(|_| cuda::Converter::new(cuda.ordinal(), config.resolved_color(), NonZeroUsize::new(8).unwrap())) .transpose()?, + scales: config.resize.is_some() && cuda::scales(&cuda), target: None, input: None, output: None, @@ -434,7 +439,7 @@ impl Backend for Nvenc { } fn scales(&self, surface: &Surface) -> bool { - matches!(surface, Surface::Cuda(_)) + self.scales && matches!(surface, Surface::Cuda(_)) } fn name(&self) -> &'static str { diff --git a/rs/moq-video/src/frame/cuda.rs b/rs/moq-video/src/frame/cuda.rs index b0e85744855..223cdf8930f 100644 --- a/rs/moq-video/src/frame/cuda.rs +++ b/rs/moq-video/src/frame/cuda.rs @@ -89,6 +89,14 @@ fn kernels(ctx: &Arc) -> Result, Error> { .map_err(|e| Error::codec(anyhow::anyhow!("CUDA resize unavailable: {e}"))) } +/// Whether the driver loads the resize kernels on `ctx`'s device. A driver can +/// refuse the vendored PTX, so a consumer that would scale on the GPU checks +/// once and otherwise leaves scaling to [`Surface::resize`](crate::Surface::resize), +/// which falls back to the CPU. +pub(crate) fn scales(ctx: &Arc) -> bool { + kernels(ctx).is_ok() +} + /// An owned device allocation. Plain `cuMemAlloc` on purpose: NVENC's /// resource registration rejects stream-ordered pool memory /// (`cuMemAllocAsync`), which is what cudarc's `CudaSlice` uses on any GPU @@ -311,6 +319,44 @@ impl Frame { /// Scale this frame into `dst` with the box-filter kernels on `stream`, a /// stream of this frame's device, and wait for them. At the same size the /// box filter is an exact copy. + /// Copy into `dst`, the same size, on `stream` and wait for it. The copy + /// engine needs no PTX, unlike [`scale_into`](Self::scale_into). + fn copy_into(&self, dst: &Self, stream: &Arc) -> Result<(), Error> { + stream + .context() + .bind_to_thread() + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA bind: {e:?}")))?; + // NV12 keeps its chroma rows right after the luma rows at the same pitch, + // so one copy of 3/2 the height moves both planes. + let copy = cudarc::driver::sys::CUDA_MEMCPY2D { + srcXInBytes: 0, + srcY: 0, + srcMemoryType: cudarc::driver::sys::CUmemorytype::CU_MEMORYTYPE_DEVICE, + srcHost: std::ptr::null(), + srcDevice: self.buf.ptr, + srcArray: std::ptr::null_mut(), + srcPitch: self.pitch as usize, + dstXInBytes: 0, + dstY: 0, + dstMemoryType: cudarc::driver::sys::CUmemorytype::CU_MEMORYTYPE_DEVICE, + dstHost: std::ptr::null_mut(), + dstDevice: dst.buf.ptr, + dstArray: std::ptr::null_mut(), + dstPitch: dst.pitch as usize, + WidthInBytes: self.width as usize, + Height: self.height as usize * 3 / 2, + }; + // SAFETY: both buffers are live NV12 allocations of `width` x `height` at + // their own pitch, which is at least `width`, so every row stays inside. + unsafe { cudarc::driver::sys::cuMemcpy2DAsync_v2(©, stream.cu_stream()) } + .result() + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA copy: {e:?}")))?; + // As for `scale_into`: NVENC orders only against the default stream. + stream + .synchronize() + .map_err(|e| Error::codec(anyhow::anyhow!("CUDA copy sync: {e:?}"))) + } + fn scale_into(&self, dst: &Self, stream: &Arc) -> Result<(), Error> { let kernels = kernels(&self.buf.ctx)?; let (width, height) = (dst.width, dst.height); @@ -578,10 +624,10 @@ impl Target { &self.frame } - /// Scale `src` into the buffer, an exact copy at the same size, and wait - /// for it, so the consumer may read the buffer next. Fails without touching - /// the CPU when `src` lives on another device or the driver refuses the - /// kernels. + /// Copy or scale `src` into the buffer and wait for it, so the consumer may + /// read the buffer next. A copy needs no kernel; scaling fails without + /// touching the CPU when the driver refuses the kernels (see [`scales`]). + /// Either fails when `src` lives on another device. pub(crate) fn fill(&mut self, src: &Frame) -> Result<(), Error> { if src.buf.ctx.ordinal() != self.frame.buf.ctx.ordinal() { return Err(Error::Unsupported(format!( @@ -590,7 +636,11 @@ impl Target { self.frame.buf.ctx.ordinal() ))); } - src.scale_into(&self.frame, &self.stream)?; + if src.size() == self.frame.size() { + src.copy_into(&self.frame, &self.stream)?; + } else { + src.scale_into(&self.frame, &self.stream)?; + } self.frame.color = src.color; Ok(()) } diff --git a/rs/moq-video/src/frame/cuda_test.rs b/rs/moq-video/src/frame/cuda_test.rs index 59d164d6998..5c43c3ca057 100644 --- a/rs/moq-video/src/frame/cuda_test.rs +++ b/rs/moq-video/src/frame/cuda_test.rs @@ -215,8 +215,9 @@ async fn vulkan_cuda_convert_resize_encode() { ..crate::decode::Config::new() }; let mut decoder = crate::decode::backend::open(&crate::decode::backend::Codec::H264.into(), &decode).unwrap(); + let mut sd_decoder = crate::decode::backend::open(&crate::decode::backend::Codec::H264.into(), &decode).unwrap(); let mut decoded = None; - let mut sd_packets = 0; + let mut sd_decoded = 0; let mut expected = None; for i in 0..8u64 { @@ -270,11 +271,25 @@ async fn vulkan_cuda_convert_resize_encode() { .unwrap(); assert_eq!(packets.len(), 1); assert_eq!(packets[0].timestamp, timestamp); - sd_packets += packets.len(); + // Every SD picture is this frame scaled: a stale or misplaced input fails. + for out in sd_decoder + .decode_all(packets[0].payload.clone(), timestamp, i == 0) + .unwrap() + { + let actual = out.surface.to_i420().unwrap().into_owned(); + assert_eq!(actual.size(), sd, "SD picture size at {i}"); + let expected = reference(&gradient(size, Format::Rgba8, i as usize * 4), size, color) + .resize(sd) + .unwrap(); + assert!(mae(actual.y(), expected.y()) < 8, "SD luma corrupt at {i}"); + assert!(mae(actual.u(), expected.u()) < 8, "SD u corrupt at {i}"); + assert!(mae(actual.v(), expected.v()) < 8, "SD v corrupt at {i}"); + sd_decoded += 1; + } slot = completion.wait().await.expect("completion worker stopped"); } - assert_eq!(sd_packets, 8); + assert!(sd_decoded > 0, "openh264 decoded the SD stream"); // The decoded picture is the uploaded gradient: the registered NVENC input // read the converted buffer at the right pitch and planes. From 84f45fa30c9bea880992d52156e5822c542eeb39 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 22:05:29 -0800 Subject: [PATCH 8/9] test(moq-video): a same-size CUDA target fill is an exact copy Co-Authored-By: Claude Opus 5.5 --- rs/moq-video/src/frame/cuda_test.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rs/moq-video/src/frame/cuda_test.rs b/rs/moq-video/src/frame/cuda_test.rs index 5c43c3ca057..9b537012335 100644 --- a/rs/moq-video/src/frame/cuda_test.rs +++ b/rs/moq-video/src/frame/cuda_test.rs @@ -156,6 +156,16 @@ async fn vulkan_cuda_convert_resize_encode() { assert!(max_diff(actual.v(), expected.v()) <= 1, "v differs from the reference"); let ctx = CudaContext::new(0).expect("CUDA device 0"); + // At the same size the fill is a copy, which must be exact. + let mut same = Target::new(&ctx, size).expect("CUDA target"); + same.fill(&converted).expect("GPU copy"); + let copied = same.frame().download_i420().unwrap(); + assert!( + copied.y() == actual.y() && copied.u() == actual.u() && copied.v() == actual.v(), + "a same-size fill changed the samples" + ); + drop(same); + let mut target = Target::new(&ctx, sd).expect("CUDA target"); target.fill(&converted).expect("GPU resize"); let scaled = target.frame().clone(); From c3d9d9892b919a20425151b253ef9d2b172c2ae9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 13 Nov 2026 23:01:23 -0800 Subject: [PATCH 9/9] docs(moq-video): put scale_into's doc back on it, and say frames are copied or scaled into the target Co-Authored-By: Claude Opus 5.5 --- rs/moq-video/src/encode/backend/nvenc.rs | 4 ++-- rs/moq-video/src/frame/cuda.rs | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/rs/moq-video/src/encode/backend/nvenc.rs b/rs/moq-video/src/encode/backend/nvenc.rs index 17ed8a7d3fd..26b58227834 100644 --- a/rs/moq-video/src/encode/backend/nvenc.rs +++ b/rs/moq-video/src/encode/backend/nvenc.rs @@ -103,7 +103,7 @@ pub(crate) struct Nvenc { ready: VecDeque, } -/// The session's CUDA input: one buffer every CUDA frame is scaled into, +/// The session's CUDA input: one buffer every CUDA frame is copied or scaled into, /// registered with the session once and mapped for each encode. struct Target { buffer: cuda::Target, @@ -293,7 +293,7 @@ impl Backend for Nvenc { fn encode(&mut self, frame: &Frame, cut: bool) -> Result<(), Error> { // The published image shares its full-size conversion across renditions, // so capture converts only once for each declared color space and never - // downloads its pixels; each encoder then scales it into its own target. + // downloads its pixels; each encoder then copies or scales it into its own target. let converted; let gpu = match &frame.surface { Surface::Vulkan(image) => { diff --git a/rs/moq-video/src/frame/cuda.rs b/rs/moq-video/src/frame/cuda.rs index 223cdf8930f..f805244132c 100644 --- a/rs/moq-video/src/frame/cuda.rs +++ b/rs/moq-video/src/frame/cuda.rs @@ -15,7 +15,7 @@ //! //! NVENC reads a registered `cuMemAlloc` NV12 buffer //! (`NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR`): each session's own `Target`, -//! which every frame is scaled into, an exact copy at the session size. The +//! which every frame is copied into, or scaled into when its size differs. The //! one copy is what lets a session register once rather than per frame, and //! costs microseconds of GPU time. The SDK 12.1 bindings could also register //! the imported Vulkan image itself as a CUDA array in an RGB session, but NVENC @@ -35,7 +35,7 @@ //! `cuMemFree` also waits for the whole device. Paid per frame, those calls //! serialize every transcode on the GPU while its engines sit idle. So every //! frame's buffer comes from a `Pool` and goes back to it, and each NVENC -//! session scales its input into one `Target` it registered once. Scaling +//! session copies or scales its input into one `Target` it registered once. Scaling //! and copies run on their owner's own stream, never the legacy default //! stream, which would wait for every other thread's work in the process. @@ -316,9 +316,6 @@ impl Frame { Ok(dst) } - /// Scale this frame into `dst` with the box-filter kernels on `stream`, a - /// stream of this frame's device, and wait for them. At the same size the - /// box filter is an exact copy. /// Copy into `dst`, the same size, on `stream` and wait for it. The copy /// engine needs no PTX, unlike [`scale_into`](Self::scale_into). fn copy_into(&self, dst: &Self, stream: &Arc) -> Result<(), Error> { @@ -357,6 +354,8 @@ impl Frame { .map_err(|e| Error::codec(anyhow::anyhow!("CUDA copy sync: {e:?}"))) } + /// Scale this frame into `dst` with the box-filter kernels on `stream`, a + /// stream of this frame's device, and wait for them. fn scale_into(&self, dst: &Self, stream: &Arc) -> Result<(), Error> { let kernels = kernels(&self.buf.ctx)?; let (width, height) = (dst.width, dst.height); @@ -591,7 +590,7 @@ impl Slot { } } -/// One NV12 buffer that frames are scaled into, for a consumer that reads it +/// One NV12 buffer that frames are copied or scaled into, for a consumer that reads it /// in place: an NVENC session registers it once and encodes every frame from /// it. Scaling runs on the target's own stream, so it never waits on other /// work in the process.