Field notes/Mediarusty_nvencNVENCGPU

rusty_nvenc: Pure Rust NVENC Encoding, Byte-Identical to FFmpeg

rusty_nvenc drives NVIDIA's hardware encoder from Rust with no C headers, no bindgen and no build script. H.264 and HEVC, checked byte for byte against FFmpeg.

Signed by M·
Painted glassworks at dusk where a craftsman passes a glowing gather on a long iron pipe through a small furnace door, warm orange light and a few drifting motes
What does pure Rust NVENC mean if the encoder is NVIDIA hardware?

It means the layer above NVIDIA's driver is Rust. rusty_nvenc writes the NVENC API structs by hand, proves their layout against the real C header at compile time, and loads libnvidia-encode.so.1 and libcuda.so.1 at run time. There is no bindgen, no -sys crate and no C toolchain. The silicon and the closed driver stay NVIDIA's.

rusty_nvenc is a pure Rust NVENC front end. It drives NVIDIA's hardware video encoder without C headers, without bindgen, without a -sys crate and without a build script. Version 0.1.0 shipped on 2 December 2026 with H.264, 0.2.0 followed that night with overlapped uploads, and 0.3.0 added HEVC Main and Main 10 the next morning.

Everything below comes from the three pull requests, the crate README and the remade_ffmpeg_rs workspace readme. Every number is the project's own, measured on one machine. We read them; we didn't rerun them.

What Pure Rust NVENC Means For A Hardware Encoder

NVENC is silicon. You reach it through NVIDIA's closed driver, and nothing here replaces that driver. What rusty_nvenc replaces is the C layer people usually put above it: the vendor header, a generated binding, a link-time loader and the CUDA toolkit.

The usual route takes nvEncodeAPI.h, runs bindgen, wraps the result in a -sys crate, and needs a C compiler and libclang to build. Here, the interface types are #[repr(C)] structs written by hand in src/abi/, libnvidia-encode.so.1 and libcuda.so.1 are opened at run time, and the crate has no normal dependencies at all. A machine without an NVIDIA GPU builds and runs it; loading simply returns a typed error, and nothing falls back to software.

The interface definitions come from NVENC API 12.1 in FFmpeg's nv-codec-headers, which are MIT licensed and not vendored. That means a driver of 530.41.03 or newer on Linux. NVIDIA's own Video Codec SDK is the reference for what the hardware can do.

How The ABI Is Kept Honest

A wrong struct layout doesn't crash loudly. The driver reads garbage and may still report success. So the first pull request treats the hand-written definitions as claims that need proof.

A C Program That Checks The Rust

A small C tool in the repo, which cargo never runs, includes the real header and prints every struct's size and alignment, every field offset, every bitfield, enum value and GUID. Its output for Linux (clang, cross-checked with gcc) and Windows (MSVC) is committed and turned into const assertions. The PR counts 36 structs and unions, 35 enums with 330 constants, 22 GUIDs and 17 version constants, plus 16 poison cases that must fail the build. A mistyped GUID byte is a compile error. The README adds a useful warning: the driver's own version check is not a layout check, because it accepted a struct revision one off in either direction on the development machine.

Ownership That Matches The Driver

One hardware test crashed inside nvEncDestroyEncoder after passing twice. The cause was B-frames: the driver holds a frame back and keeps writing into its buffers at flush or destroy, and the code had already freed them. The fix moved the rule into the types. A submitted input lives in its output buffer until the picture is read, and a buffer the driver may still hold is released only after a flush. The project reports the worst case passing 500 times in 500 fresh processes, and 400 randomised drop rounds without a crash.

Leaks, Threads And The Unsafe Line

Opening and closing an encoder 1,000 times at 1280x720 left process memory, GPU memory and file descriptors flat in three modes, while a deliberate leak moved every number. Driver and CudaContext are Send + Sync; Session and Encoder are Send only, and the CUDA context is pushed and popped around each call. The crate root denies unsafe; four private modules allow it behind safe APIs, each block with a SAFETY comment.

What Ships In 0.3, And What Doesn't Yet

Three releases in about thirteen hours is quick, so it's worth being exact about the edges.

Byte For Byte Against FFmpeg

NVENC is deterministic, so the check can be exact. The workspace readme reports 42 of 42 H.264 cells byte-identical to ffmpeg -c:v h264_nvenc across presets, profiles, B-frames, QPs and GOPs, and 46 of 46 HEVC cells identical to hevc_nvenc, plus 11 of 11 in an independent run on a fresh tree. Each stream was also decoded by the house decoders, rusty_h264 and rusty_h265, with FFmpeg's decode matching frame for frame. Matching FFmpeg took settings ffmpeg -h doesn't show, such as its default picture timing SEI and its P-frame-only -qp; the gate scripts set them on both sides.

Speed, With The Caveats Attached

Pull request 274 added an output delay, so the next frame uploads while the GPU encodes the last one. At 1080p preset P1 the README reports 0.80 of FFmpeg's wall time and 0.67 of its CPU time, faster in 8 of 8 rounds. With the delay set to zero, as 0.1.0 always ran, the same cells were 1.08 to 1.40 times FFmpeg's wall time. All of it is one RTX 4060 Laptop under WSL 2. Native Linux and other GPUs are unmeasured, and the HEVC speed runs were on a loaded machine, so read those as "not slower".

Limits, Then How To Try It

What 0.3 doesn't do: VBR or CBR rate control, 4:4:4 input, lossless, AV1 encode, or any Windows encode (the loader is a stub). The API is 0.x and pull request 276 already made Frame generic for 10-bit P010 input. It is a library, not yet a remade_ffmpeg_rs CLI flag. If you have Linux and an NVIDIA card, trying pure Rust NVENC means adding the crate, running the hardware tests the README lists, and reading the plan doc before relying on it. The pure Rust FFmpeg alternative page shows where it fits, and the rusty_h265 0.8 notes cover the decoder that checked every HEVC stream. The wider project sits on mata.network.

FAQ

Quick answers for builders evaluating this technology.

Which codecs and platforms does rusty_nvenc 0.3 support?

H.264 8-bit and HEVC Main and Main 10, all 4:2:0, at constant QP, on Linux x86-64 with an NVIDIA driver of 530.41.03 or newer. The Windows loader is a stub that returns Error::Unsupported, and AV1 definitions are present but nothing has encoded with them yet.

How was byte identity with FFmpeg checked?

By encoding the same clips at matched settings with rusty_nvenc and with FFmpeg's h264_nvenc or hevc_nvenc, then comparing the streams. The project reports 42 of 42 H.264 cells and 46 of 46 HEVC cells identical, plus 11 of 11 in an independent HEVC run. NVENC is deterministic, so this is an exact check.

Is rusty_nvenc faster than FFmpeg's NVENC encoder?

On one machine it measured a bit faster. At 1080p preset P1 the project reports 0.80 of FFmpeg's wall time and 0.67 of its CPU time for H.264, on an RTX 4060 Laptop under WSL 2. Native Linux and other GPUs haven't been measured, and the HEVC runs were on a loaded machine.

Does rusty_nvenc use unsafe code?

Yes, because talking to a driver is FFI. The crate root denies unsafe code, and four private modules allow it behind safe public APIs, each block with a SAFETY comment. One public function, Driver::load_from, is unsafe because it loads a library the caller names.

Can I use rusty_nvenc from the remade_ffmpeg_rs command line?

Not yet. The workspace readme lists it as library only, not yet in the CLI. The encoder already has the send_frame, receive_packet and finish shape of the rff-codec encoder trait, and the plan lists the wrapper as a later brick.