Files
reims-vgpu/docs/video/nvidia.md
T
Anees Iqbal 221d6933e6 🆕 Decode VP9 profile 2 at 12 bits on NVDEC where its caps list it
The owner's order is to offer what the host lists. An RTX 3090's NVDEC
lists VP9 at 12 bits, 4:2:0 (m3-max-77186), but the NVDEC arm declined
every 12-bit profile 2 stream at its paravirtual session's start, so an
arm64 guest decoded none on an NVIDIA host, while iHD's already land in
`tv20`.

What changes for the guest: on an NVIDIA host, an arm64 guest's
paravirtual VP9 session of profile 2 at 12 bits, 4:2:0, is now taken
where NVDEC's caps of VP9 at 12 bits, asked at the session's start as
HEVC's range extensions are, list it at the stream's size. Its frames
decode into P016 (the samples at the top of their words) and land in
`tv20` as HEVC's 12-bit 4:2:0 pictures do. Elsewhere the session is
refused at its start as before, its reason named.

vp9_s_caps_are_nvdec_s_answers_and_decline_what_they_lack held the old
answer (12 bits declined) and now holds the new one: taken where the
caps list it; declined where they do not, write no P016, or lack the
size. It fails with the 12-bit branch cut off (m3-max-78250, msi). The
new ignored NVIDIA-host test a_12_bit_profile_2_stream_decodes_to_libvpx_frames
checks the codec module's 12-bit libvpx stream against vpxdec's MD5s, to
run on an RTX 3090. docs/video/formats.md's cell reads "listed, not run"
until it does.
2026-12-02 07:18:17 -08:00

491 lines
30 KiB
Markdown

# NVDEC and NVENC
`crates/reims-vgpu-video`'s `nvidia` module: decode on NVDEC and encode on
NVENC, over the CUDA driver API, with nothing above them. The reims-graphics device's
video decodes on its decoder of whole access units on an NVIDIA GPU
(reims-vgpu's `graphics/nvidia.rs`, [devices.md](devices.md)). Its NVENC
encoder is driven by its example and by reims-vgpu's tests, which encode
the streams they decode. This doc says how the module binds the driver, what
an RTX 5080 Laptop GPU answers, how each part works, and how it is tested.
Runs are on an NVIDIA GeForce RTX 5080 Laptop GPU (driver 615.71.09, CUDA
driver 13.4, on CachyOS) unless they say otherwise.
## What is here
| what | where |
|---|---|
| Loading the driver's libraries, and the function tables | `src/nvidia/dl.rs` |
| The CUDA driver API: a GPU, its primary context, copies | `src/nvidia/cuda.rs`, `src/nvidia/cuda/ffi.rs` |
| NVDEC's binding: caps, decoder, pictures, mapping, status, reconfiguration | `src/nvidia/nvdec.rs`, `src/nvidia/nvdec/ffi.rs`, `src/nvidia/nvdec/ffi/vp9.rs` |
| An NVDEC decoder and its pictures: surfaces, stand-ins, the copy out | `src/nvidia/nvdec/decoder.rs` |
| The codec module's accelerators on NVDEC, one a codec | `src/nvidia/nvdec/h264.rs`, `hevc.rs`, and AV1's, for VideoToolbox's decoders alone ([devices.md](devices.md), "AV1 on arm64 guests"), `av1.rs` with its binding `src/nvidia/nvdec/ffi/av1.rs`, and VP9's, likewise, `vp9.rs` |
| The decoder of whole Annex B access units, which the reims-graphics device's video drives | `src/nvidia/annex_b.rs` |
| The failures NVDEC reports, which fail the pictures after them | `src/nvidia/decode.rs` |
| Zero copy: guest memory registered with CUDA ("Zero copy") | `src/nvidia/zero_copy.rs` |
| Another API's memory imported into CUDA, for a copy within the GPU | `src/nvidia/external.rs` |
| The waits on the GPU, each bounded ("Waiting for the GPU") | `src/nvidia/wait.rs`, `src/nvidia/nvenc/locks.rs` |
| NVENC's binding, and an encoder of raw frames | `src/nvidia/nvenc.rs`, `src/nvidia/nvenc/ffi.rs`, `src/nvidia/nvenc/encoder.rs` |
| What the GPU answers, as `vainfo` would | `src/nvidia/probe.rs`, `examples/nvidia-probe.rs` |
| Raw NVENC from the command line | `examples/nvidia-encode.rs` |
| The binding checked against NVIDIA's headers | `src/nvidia/layout.rs` (an ignored test) |
## The binding
libcuda.so.1, libnvcuvid.so.1 and libnvidia-encode.so.1 are opened with
`dlopen` when a GPU is first opened, so a host with no NVIDIA driver runs
without them: opening a GPU there fails, and nothing else does. No
crate binds them; the declarations are ours, for the subset used, and no
NVIDIA parser is used: the codec module parses every stream, as for VA-API.
- **Checked against NVIDIA's headers.** `src/nvidia/layout.rs`'s test
(`cargo test -p reims-vgpu-video nvidia::layout -- --ignored`) writes a C
program from the binding's tables, builds it against ffnvcodec-headers
13.1.15.0 in `/usr/include/ffnvcodec`, and compares what C sees with
what Rust declares: every struct's size and offsets, every bit-field,
constant, GUID and function signature. It last passed on 2026-10-24,
with `CUVIDRECONFIGUREDECODERINFO` and `cuvidReconfigureDecoder` among
its checks. The C bit-fields are words in Rust, with each field's shift
and width (`nvdec::ffi::bits`), in GCC's x86-64 order, which the test
checks too.
- **NVENC API 13.1.** `NvEncodeAPIGetMaxSupportedVersion` answers 13.1;
every versioned struct is declared at 13.1, and NVENC refuses a wrong
size or version.
- **Threads.** The decoder is made with `cudaVideoCreate_PreferCUVID` and
no `vidLock`: each call on a decoder holds that decoder's mutex with the
GPU's primary context pushed, so a guest's copy on one thread and its
decodes on another do not race. Each push is popped by a guard on the
thread that pushed it, which cannot leave that thread (`cuda::Current`).
- **Handles.** Each is released once, on every path, a panic's unwinding
included: a mapped picture is unmapped by a guard (`MappedFrame`,
`nvdec/decoder.rs`), and an NVENC buffer's lock undone by one (`Locked`,
`nvenc.rs`). An NVENC session keeps the buffers it makes, which their
owner names by index (`nvenc::Buffer`), and destroys them before itself;
every call that passes one to the driver is the session's, from the
buffer's own handle, size and format. Its initialisation and
configuration go after it, the configuration kept at the one address
NVENC was given, which a reconfiguration changes in place. An NVDEC
decode is given its
bitstream and slice offsets by borrow, each offset checked within the
bitstream, so that no other memory reaches the driver as either. Memory
the driver hands over
uninitialised is zeroed before it is read (the staging memory), or only
written (NVENC's input buffers). Nothing of the driver's calls back into
Rust: no parser, no NVENC events.
- **Not in the headers.** `cuMemHostRegister_v2` and `cuMemHostUnregister`,
for zero copy, are not in ffnvcodec's `dynlink_cuda.h`, so the layout
test does not check them; their signatures are the CUDA driver API's
(`void *`, a size, flags; `void *`), and libcuda 615.71.09 exports both
(`nm -D`, 2026-10-25). A driver without them is lent no guest buffer,
and the log says so once (`src/nvidia/cuda.rs`). Nor does it check
`cuImportExternalMemory`, `cuExternalMemoryGetMappedBuffer` and
`cuDestroyExternalMemory`, for imports of another API's memory
(`src/nvidia/external.rs`).
## What the RTX 5080 Laptop GPU answers
`nvidia-probe` (2026-10-24), the parts the module uses:
| codec | chroma, depth | sizes | output |
|---|---|---|---|
| H.264 | 4:2:0 8-bit | 48x64 to 8192x8192 | NV12 |
| HEVC | 4:2:0 8, 10, 12-bit | 144x144 to 8192x8192 | NV12; P016 above 8 |
| HEVC | 4:2:2 8, 10, 12-bit | 144x144 to 8192x8192 | NV16; P216 above 8, each alone |
The 4:2:2 row is `nvidia-probe`'s of 2026-11-27 (615.71.09, potluck job
m3-max-77563), 262,144 macroblocks. Each has 2 decode engines. NVDEC also
lists H.264 at 10 bits and 4:2:2, and HEVC 4:4:4, which the device's caps
do not offer; HEVC's range extensions are decoded for VideoToolbox's
paravirtual decoders ([devices.md](devices.md)). A
paravirtual session of a stream outside the sizes of its codec, chroma
format and depth is refused at its start, and the guest decodes it in
software ([devices.md](devices.md), "A stream outside the host decoder's
sizes").
The RTX 3090 (620.70.06, `nvidia-probe`, 2026-11-22) answers HEVC 4:4:4 at
8 bits from 144x144 to 8192x8192, 262,144 macroblocks, into `Yuv444`
alone, and at 10 and 12 bits into `Yuv444_16Bit` alone; HEVC 4:2:0 at 8
bits into NV12 alone, and at 10 and 12 into NV12 and P016.
NVENC (`NvEncGetEncodeCaps`): H.264 Baseline, Main, High and High 10 among
others, 145x49 to 4096x4096, 7 B-frames; HEVC Main, Main 10 and the range
extensions, 129x33 to 8192x8192, 5 B-frames. Presets P1 to P7, 2 encode
engines, CBR, VBR and constant QP, lookahead (`SUPPORT_LOOKAHEAD`).
## The reims-graphics device
The reims-graphics device's video, on an NVIDIA GPU, runs on this module's
NVDEC through `AnnexBDecoder` (`src/nvidia/annex_b.rs`), in a host video session of
reims-vgpu's (`crates/reims-vgpu/src/graphics/video_host.rs`, `nvidia.rs`): the
guest's video driver hands each decode a whole Annex B access unit, one
picture, in decode order, and the planes of the output surface it lands in.
The device's arm chooses the runtime device's own GPU, by its PCI address,
where it is NVIDIA's and CUDA opens it (`graphics/video_choice.rs`).
- **The access unit, in place.** The codec module's decoder parses it where it
lies, in the host bytes of the guest's bitstream buffer, and hands NVDEC each
picture's slices, units of the access unit's own bytes. Where they lie in it
as one run (after start codes, only zeros between them), NVDEC is handed that
run as it is, with no copy (`nvdec::decoder::one_run`); otherwise the slices
are gathered into one buffer. NVDEC copies the bitstream within
`cuvidDecodePicture`, so the guest's buffer is read there alone. A session
logs at its end how many of its pictures went each way.
- **The picture, by one GPU copy**, `cuMemcpy2D` a plane from the mapped
surface into the plane at its pitch, its crop's rows alone, waited for
before the session signals the decode's value. Any pitch is taken, so the
device asks no layout of the guest.
- **Into the output's device caches**, where CUDA imports the runtime
device's exportable buffers, which the arm learns once as the video opens
(a buffer of a 4K picture's bytes made exportable and imported, logged
with the make's time). The caps then ask the guest for cached outputs
(`RGFX_VIDEO_CAPS_OUTPUT_CACHED`), the device makes each cache of a surface
of two planes (NV12, P010) exportable as it makes its memory (in the
per-shape form, at the memory's first use), the others as any buffer, and
the picture is copied within the GPU into the cache's
memory, imported once a cache (`src/nvidia/external.rs`) and let go of as the
cache goes.
- **Into the output's pages**, where its planes lie in pages a device
reaches in place (the surface's view of the guest's RAM, or a copy block
of the device's region): their view is registered with CUDA once a
mapping, as "Zero copy" registers a guest buffer's, and written by DMA.
Elsewhere the picture is read back and copied by the CPU.
- **A picture that does not land is a decode error.** A picture whose planes
the session refuses still signals the decode's value, so its command
buffer completes, failed with Metal's internal error. Where its write into
the device caches failed (a decode NVDEC failed, before the copies), the
caches are acquired first (`video_host`'s `Landing::failed_in_caches`).
Where the work ran past its deadline and may still write, the landing
first waits for the work it kept, with the imports, to be let go, up to
`video_host::STALL_HOLD` (10 s), and the process dies past it. For HEVC
the guest's video driver's schedule of the picture waits for that verdict
and fails
(`kVTVideoDecoderMalfunctionErr`),
which AppleGVA reports from that frame on, so no frame of the picture
reaches the guest. AppleGVA's H.264 decoder reports no failure a driver can
answer, so an H.264 picture that does not land reaches the guest as a
decoded frame (`guest/bundle/src/RgfxVideoStatus.hpp`, bugs.md D25).
- **A picture NVDEC failed lands as NVDEC left it**, H.264's and HEVC's. The
device's sessions have their decoders copy such a picture
(`AnnexBDecoder::copy_failed_pictures`): it lands as any, its command
buffer completing as any, and the pictures after it decode from it, as a
Mac's hardware decoders hand a picture they decoded with an error on, with
no error, and decode on until the next IDR picture (**measured**, a MacBook
Pro of 2019 under macOS 26.7.1: on 2026-11-16, an H.264 P picture cut to
half its slice, or with 64 bytes of it changed, and those after it came
back with status 0, 26.5 to 29.7 dB from the undamaged stream's luma, none
all zeros; on 2026-11-20, on the UHD 630, an HEVC P picture at 1080p whose
last slice segment was cut to half, and those after it, came back with
status 0 and an image each). A failed HEVC command buffer would instead
end the guest's session: AppleGVA's HEVC path answers
`kVTVideoDecoderMalfunctionErr` from that picture on. The session counts
each, and logs the first ones ("a picture the hardware failed landed as it
left it").
- **Surfaces.** A picture holds its decode surface until it lands; the decoder
has `AnnexBDecoder::SPARE_PICTURES` (2) beyond its references and the
picture being decoded, and the session holds no more pictures unlanded than
that. Pictures land on the session's landing thread, in decode order, while
the next is decoded: a copy out holds the decoder's handle only for its map,
copies and unmap, and each ask after its decode's status, so NVDEC decodes
the next picture meanwhile (`nvdec::decoder`).
## The decoders
Each codec is the codec module's decoder over an accelerator of ours that
fills `CUVIDPICPARAMS` from what the codec module parsed, as the `vaapi`
module fills libva's buffers (`src/nvidia/nvdec/h264.rs`, `hevc.rs`). ffmpeg's
`nvdec_*.c` are the model for the field mapping, and NVIDIA's own parser
the judge where the two differ; every mapping was confirmed by bit-exact
runs against ffmpeg's software decode ([bugs.md](bugs.md), T16), not by
the model.
- **Output.** 8 bits decode into NV12, 10 into P016, which holds each
sample in the high bits as P010 does. A picture is mapped
(`cuvidMapVideoFrame64`) once its decode is done, and copied out of the
mapped surface by the GPU ("The reims-graphics device", above), or read
back with two `cuMemcpy2DAsync` on the decoder's own stream into the
decoder's staging memory, page-locked by `cuMemHostAlloc` (pageable where
CUDA gives none), for the CPU to copy.
- **A failed decode fails the pictures after it.** NVDEC's status of a
picture's decode (`cuvidGetDecodeStatus`) is read at its copy, and a
failed one is bad data (`Error::DecodeFailed`). NVDEC reports a slice
cut short failed (status 9, `cuvidDecodeStatus_Error_Concealed`). The
failure is noted (`Failures`, `src/nvidia/decode.rs`). In an HEVC stream
the decoder's next decode sets the codec module's damaged state, so that
each picture after it is refused until an IRAP picture; an H.264 stream's
pictures after it decode on from it, as after any picture that does not
decode (the codec module's H.264 decoder). A picture of a decoder that
copies failed pictures (above) is copied instead, its status kept
(`AnnexBPicture::copied_failed`), and noted as no failure. A
picture whose copy is never made has its status read by no one: an HEVC
picture with `pic_output_flag` 0, for one ([bugs.md](bugs.md), B41).
- **H.264.** Slices as Annex B, each after a three-byte start code, with
`pSliceDataOffsets`; the scaling lists in raster order; the DPB by
surface index with `FrameIdx` and both fields' order counts.
- **HEVC's range extensions** (the codec module's streams of theirs at
4:2:0, 4:2:2 and 4:4:4, 8, 10 and 12 bits) decode at their chroma format
and depth: 4:2:0 into NV12 at 8 bits and P016 deeper, as Main 10's, the 12
bits at the top of each sample; 4:2:2 at `cudaVideoChromaFormat_422` into
`cudaVideoSurfaceFormat_NV16` at 8 bits and `P216` deeper, laid out as
NV12 and P016 with the chroma as tall as the luma, its rows from the
crop's first (`Mapped::of`); 4:4:4 at `cudaVideoChromaFormat_444` into
`cudaVideoSurfaceFormat_YUV444` at 8 bits and `YUV444_16Bit` deeper,
three planes, luma, Cb and Cr, each the picture's size, the second after
the luma's rows rounded up to even, as the 4:2:0 chroma lies, and the
third as far after the second (`Mapped::of`). The range extensions'
fields of `CUVIDHEVCPICPARAMS` take
the SPS's and PPS's range extensions, the chroma QP offset lists'
entries among them. A picture of three planes reads back with three
copies (`AnnexBPicture::read_back`), and is written within the GPU into
three planes of imported buffers by three
(`AnnexBPicture::write_4_4_4_into_device`); a write of two planes refuses
it. VideoToolbox's paravirtual decoders write it so into a scratch of the
runtime device's, whose kernel then writes the guest's surface's two
planes (reims-vgpu's `graphics::nvidia` and `graphics::gather`).
- **VP9**, for VideoToolbox's paravirtual decoders alone (`src/nvidia/nvdec/vp9.rs`):
`CUVIDVP9PICPARAMS` from the codec module's uncompressed header and slots, as ffmpeg's
`nvdec_vp9.c` fills it: the last, golden and altref frames by surface index (255 in an intra
frame), their slots in `activeRefIdx` and their sign biases from `refFrameSignBias[1]`, the
filter in libvpx's numbering, the loop filter's deltas as `int`s in its `unsigned int`s, each
segment's features as coded with the skip feature's data 0, and `frameTagSize` and
`offsetToDctParts` the two headers' sizes. The frame goes whole, where it lies in the sample,
as one slice at its start; NVDEC reads its compressed header and tiles. Profile 0 decodes into
NV12 and profile 2 at 10 and 12 bits into P016, the samples at the top of their words, on a
decoder made at the frame's size (`cudaVideoCodec_VP9`, 10); the caps' entry lists each
profile where NVDEC answers VP9 at 8 or 10 bits, and a 12-bit stream's session is taken where
NVDEC's caps of VP9 at 12 bits list it at its size (reims-vgpu's `declines_vp9`).
- **HEVC.** The references by surface index with their order counts and
long-term flags; `RefPicSetStCurrBefore`, `StCurrAfter` and `LtCurr` as
indices into them, in the order 8.3.2 derives them, which the codec
module hands on, since NVDEC builds each slice's lists itself; the
scaling lists in coding (diagonal) order, as dynlink_cuviddec.h says
("scaling lists (diag order)"), 32x32's from `matrixId` 0 and 3;
`NumDeltaPocsOfRefRpsIdx` from the slice header's own predicted set.
- **AV1**, for VideoToolbox's paravirtual decoders alone ([devices.md](devices.md), "AV1 on
arm64 guests"): `CUVIDAV1PICPARAMS` from the codec module's sequence and frame headers
(`src/nvidia/nvdec/av1.rs`), ffmpeg's `nvdec_av1.c` the model. A frame's tile data goes in
one call, each tile's start and end in `pSliceDataOffsets`. The references go by surface:
`ref_frame_map` and each `ref_frame[i].index` name the surface of the slot they name, 0xff
for an empty one, for every frame, as ffmpeg fills them; `primary_ref_frame` names the
surface of the primary reference's slot, not the slot's number. With the slot's number there,
and `ref_frame[]` left zero on key frames, libnvcuvid faulted (SIGSEGV, and SIGBUS under gdb)
inside `cuvidDecodePicture` at the first frame (**measured** on the RTX 3090, driver
620.70.06, 2026-11-27, m3-max-77319 and m3-max-77356). A frame naming a slot that holds no
frame is refused before NVDEC sees it (`picture_parameters`). After the fix, every
`CUVIDPICPARAMS` of our superres stream equalled ffmpeg's byte for byte but for its two
pointers, call by call under gdb (m3-max-77392). NVDEC decodes into `decodePicIdx`, which
later frames predict from, and writes the frame with film grain into `CurrPicIdx`, which the
guest gets: its grain is the specification's, each frame of our grain stream equal to
libdav1d's (m3-max-77377). On the RTX 3090 (Ampere, driver 620.70.06), the caps answer AV1 at
4:2:0 from 128x128 up to 8192x8192, at 8 and 10 bits (m3-max-77421), and NVDEC decodes a superres stream's
inter frames wrongly: of our `aom-superres`, the key frame matched libdav1d's and each of the
5 inter frames differed, each with status 9 (`cuvidDecodeStatus_Error_Concealed`), and
ffmpeg's own NVDEC decode gave the same 5 wrong frames (**measured**, 2026-11-27,
m3-max-77377 and m3-max-77392; [bugs.md](bugs.md), U36). VideoToolbox's decoders copy no
failed picture, so each such frame fails to the guest as a decode error.
### Concealment
A frame lost from an H.264 stream whose SPS allows no gap in
`frame_num` is stood in for, so that the frames predicting from it
decode, as on VA-API.
NVDEC's surfaces are written by a decode alone, so `NvdecH264::conceal`
gives the last reference's own surface under a picture of its own
(`Picture::stand_in`, `nvdec/decoder.rs`), held until both go. How the
DPB names it follows NVIDIA's parser, measured on 2026-10-24:
- **NVIDIA's parser**, fed a stream through `cuvidParseVideoData` by a
scratch program that printed each `CUVIDPICPARAMS`'s DPB and decoded
nothing, was given `crates/reims-vgpu-video/tools/h264-check.sh`'s
drop20-ref1 and a four-reference drop20-ref4 (x264 `--ref 4`, frame 20
of 60 left out, made by `h264-craft --drop 20`). With four references it
named the lost frame `not_existing` 1, `PicIdx` -1, `FrameIdx` 4, beside
the frames before it. With one, it kept the last reference's surface,
`FrameIdx` 3, and named no lost frame.
- **One surface named twice fails.** With the stand-in beside its source
in the DPB (drop20-ref4, frames 20 to 22), NVDEC reported each of those
pictures failed, status 9 (`cuvidDecodeStatus_Error_Concealed`), and the
first picture without the two (frame 23) decoded.
- **So a stand-in whose surface another entry names goes as
`not_existing`, `PicIdx` -1** (`dpb_entries`, `nvdec/h264.rs`), and one
whose surface no other entry names keeps it. Then drop20-ref1 decoded
59 of 59 frames, the 50 the loss does not reach equal to the source's
ffmpeg decode, and drop20-ref4 59 of 59 with no failure, (**measured**,
2026-10-24). The concealed frames came to a
luma PSNR of 21.3 to 26.3 dB against the source for drop20-ref1, which
is a copy of the last reference as VA-API's, and 27.7 to 30.0 dB for
drop20-ref4.
### What it refuses
- What the codec module refuses (interlaced H.264, and in HEVC's range
extensions monochrome, depths past 12 bits and the rest of
[vaapi.md](vaapi.md), "What it refuses"), as unsupported.
- An HEVC range extensions stream, as unsupported, where the decoder's
caller lands Main and Main 10 pictures alone ("HEVC range extensions: its
caller lands Main and Main 10 pictures alone"), as a host video session's
decoders do; VideoToolbox's paravirtual decoders land them
(`AnnexBDecoder::lands_range_extensions`).
- A stream of a chroma format and depth NVDEC's caps do not list, or not
into the output the decoder makes of it, as unsupported, at the
decoder's make: the RTX 3090 lists no HEVC 4:2:2 decode (`nvidia-probe`,
2026-11-22), and a VideoToolbox session of such a stream is refused at
its start ([devices.md](devices.md)).
- A stream past NVDEC's sizes, as unsupported, or under them, as too small
(`Error::TooSmall`). The device's caps give the guest NVDEC's own sizes
([bugs.md](bugs.md), D14).
- A VP9 frame whose references lie in another decoder's surfaces, as a
picture's error: a decoder is made for each run of frames of one size,
and NVDEC names references by their surfaces, so a frame of another
size than the frames it predicts from (a resize between key frames,
which VP9 allows) does not decode, nor do the frames after it until an
intra frame fills the slots ([bugs.md](bugs.md), B96). VP9 past profile 0
and profile 2 at 10 and 12 bits, 4:2:0 (profiles 1 and 3), as
unsupported, at the decoder's configure, before NVDEC sees a frame; the
arm's `dsta` refuses such a stream at its start
([devices.md](devices.md), "VP9's profiles 1 and 3 on arm64 guests").
- An HEVC picture with more than 8 pictures in one of its current
reference sets, as unsupported: `CUVIDHEVCPICPARAMS` holds 8 a set
(`current_set`, `src/nvidia/nvdec/hevc.rs`, "NVDEC takes 8"). The caps answer 8 current references
in all, which every picture decodes with.
## The encoder
`NvencEncoder` (`src/nvidia/nvenc/encoder.rs`) is an NVENC session in NVENC's
synchronous mode, the only one on Linux: raw 4:2:0 frames in, NV12 or P010,
H.264 or HEVC access units out, in decode order.
- **Pictures in and out.** Each frame is copied into an NVENC input buffer
(`encode`), or read where another API's GPU wrote it, in memory CUDA
imported (`encode_device`, a `DeviceFrame`): registered with the session
at its pitch on its first frame (`nvEncRegisterResource`, a CUDA device
pointer), mapped for each frame (`nvEncMapInputResource`) and unmapped
once its output is locked, and unregistered as the session goes, before
the import, which holds the exporter's buffer (`ImportedBuffer::holding`).
Each is encoded with `frameIdx` counting from 0 in submission order;
`NV_ENC_ERR_NEED_MORE_INPUT` means nothing yet, and after a success the
pending output buffers are locked in submission order, each its picture
in coding order. A flush sends a picture with the end-of-stream flag
alone, and the encoder flushes before it is destroyed, since destroying
it with frames in flight crashed in the driver (in libnvcuvid, under
`nvEncDestroyEncoder`, with lookahead on).
- **Configuration.** The preset asked (P1 to P7) and its tuning, then:
lookahead off (below); `frameIntervalP` the B-frames plus 1, B-frames
never references; NVENC decides each picture's type (`enablePTD`) by its
GOP length, and an IDR picture is forced where the caller asks for one.
NVENC writes its own parameter sets, before each IDR picture, and slice
headers, and keeps its own references. The VBV holds the time asked at
the peak rate; the VUI's colour and range are the caller's.
- **Its waits are bounded.** A bitstream buffer's lock, which waits for
the picture, is made on a thread of the session's own and waited for
until the deadline ("Waiting for the GPU"; [bugs.md](bugs.md), U19).
- **Lookahead is off.** With P6 and P7 at high quality, which turn it on,
after `NV_ENC_SUCCESS` only the oldest pending output locked; the next
lock gave `NV_ENC_ERR_INVALID_PARAM`, and frames were lost even after the
end-of-stream picture ([bugs.md](bugs.md), U6 and D12).
## Zero copy
A picture is copied by the GPU straight into memory the host's device
reaches in place, and the CPU copies nothing (`src/nvidia/zero_copy.rs`). The host
lends the memory as a view of its pages, contiguous in its address space:
reims-vgpu's view of a surface over the guest's RAM. The view's whole pages
are registered with CUDA once per mapping, `cuMemHostRegister_v2` with
`CU_MEMHOSTREGISTER_PORTABLE`, which pins them for every context, and the
registration is kept by the mapping's identity in core's `DeviceImports`,
process-wide, since a range is registered once in a process whichever GPU
registered it: at most 96, the one used longest ago dropped first. It is
unregistered, with its GPU's context pushed, when the mapping goes, before
the hold that keeps the view mapped is let go.
CUDA takes any pitch, so every layout is written by the GPU, and no
dma-buf is made, so udmabuf's limits do not bear on it. What declines, the
picture then read back and copied by the CPU with the reason logged once a
session: a registration CUDA refuses, a copy that fails, or a driver
without `cuMemHostRegister_v2`.
A picture lands in the GPU's own memory through `src/nvidia/external.rs`: a Vulkan
buffer whose memory its device exported as an opaque file descriptor,
imported with `cuImportExternalMemory` on the CUDA device of the same GPU
(CUDA refuses another's) and mapped, so that the copy is within the GPU.
The mapping starts at the allocation's first byte and the buffer's address
is its offset into the mapping: CUDA maps a buffer of a dedicated
allocation's import only from offset 0 (measured on an RTX 2080 and an
RTX 3090, the module's docs), so a buffer at any offset of any allocation
maps. The import is freed as the `ImportedBuffer` drops, and the exporter's
buffer must outlive it.
## Waiting for the GPU
Every wait of the module's on the GPU is bounded by core's `DEVICE_WAIT`,
5 s ([core.md](core.md), "Bounded waits"), asked after every 100 µs for its
first 50 ms and every millisecond after (`src/nvidia/wait.rs`):
- a picture's decode, with `cuvidGetDecodeStatus` until it is no longer
`cuvidDecodeStatus_InProgress`, in place of `cuvidMapVideoFrame64`'s own
wait;
- its map and copies, with `cuStreamQuery` of the decoder's own stream, in
place of `cuMemcpy2D`, which returns once its copy is done; copies into
pageable staging memory, which return only once copied, are made on the
decoder's call thread, then the stream is asked;
- an encode, with the waiting `nvEncLockBitstream` made on the session's
call thread and its answer waited for (`src/nvidia/nvenc/locks.rs`, which says
why `doNotWait` does not serve).
Past the deadline, that picture fails as a malfunction
(`Error::Stalled`), and each later call on the decoder or encoder fails at
once the same way. NVDEC's and NVENC's handles are kept until the GPU is
done with the work, and destroyed then; a call on a call thread that has
not returned is left to return there, and nothing it holds is used,
unmapped, freed or destroyed until it has.
## Tests
- **Unit tests**, on any host (`cargo nextest run -p
reims-vgpu-video -E 'test(/^nvidia::/)'`), with no driver: the binding's bit-fields and
statuses, the slices' runs and the copies' rows, the failures, the waits
and their call threads, NVENC's configuration and its buffers' locks, and
HEVC's current sets.
- **On the NVIDIA host**, the ignored tests (`cargo nextest run -p
reims-vgpu-video --run-ignored only -E 'test(/^nvidia::/)'`):
- `capabilities_are_asked_once_a_codec_chroma_format_and_bit_depth`
(`src/nvidia/nvdec.rs`): NVDEC's capabilities kept from the first ask,
which the device's arm makes as it opens, for its sessions' decoders;
- `layouts_match_nvidia_headers` (`src/nvidia/layout.rs`): the binding against
ffnvcodec-headers 13.1.15.0 in `/usr/include/ffnvcodec`, built with the
host's C compiler ("The binding");
- `access_units_decode_in_place_and_land_by_the_gpu_as_they_read_back`
(`src/nvidia/annex_b/tests.rs`): whole access units decoded in place, each
picture written by the GPU equal to its read back;
- `libvpx_streams_decode_to_libvpx_frames` and
`a_stream_that_starts_after_its_key_frame_decodes_from_the_key_frame`
(`src/nvidia/annex_b/vp9_tests.rs`): the codec module's two libvpx
streams, every frame equal to vpxdec's MD5, each frame sent in place,
the hidden ones too (an RTX 3090, potluck job m3-max-77353, 2026-11-27);
and `a_12_bit_profile_2_stream_decodes_to_libvpx_frames`, its stream of
profile 2 at 12 bits likewise, into P016;
- `what_each_lock_returns` and
`a_lock_past_its_deadline_stalls_the_session_until_nvenc_is_done`
(`src/nvidia/nvenc/encoder.rs`): NVENC's locks ([bugs.md](bugs.md), U19).
- **reims-vgpu's NVDEC arm**, on the NVIDIA host
(`crates/reims-vgpu/src/graphics/nvidia/tests.rs`, ignored):
`a_picture_lands_in_an_imported_device_cache_as_it_reads_back`, a picture
copied within the GPU into a device cache CUDA imported, read through the
runtime as NVDEC's own read back of it; and
`an_encode_first_processs_waits_run_on_the_arms_runtime`, an encode made
before any decode waiting on the runtime the arm opened with, which the open
hands the module (`use_runtime`).
The module has no conformance suite of its own: the `vaapi` module's
([backends.md](backends.md), "The conformance suite").
## Limits
- NVDEC fails valid decodes in every process while one of our VMs boots on
its GPU, under contended atomics in imported host memory
([bugs.md](bugs.md), B37, U21).
- A truncated slice is found by NVDEC's status, but not in a picture whose
copy is never made ([bugs.md](bugs.md), B41).
- NVENC runs without lookahead ([bugs.md](bugs.md), D12).
- The smallest frames are NVDEC's and NVENC's own ([bugs.md](bugs.md),
D14).