NVDEC H.264 packet submission audit

Snapshot: this page records this audit of the NVDEC packet submission, as it was when it was written. The code has changed since, and the page is not kept up to date.

Green video can result from damaged decoding or unwritten/stale YUV surfaces. This change fixes concrete submission defects in src/nx/gpu/video/nvdec.c; without a failing game's capture it does not establish which defect caused that game's symptom.

Findings and changes

These rules follow FFmpeg's send/receive API contract and packet API.

Tests

nvdec_packets uses the production implementation with a small, isolated FFmpeg API double. It covers packet bytes/padding, shorter consecutive packets, ambiguous lengths, malformed inputs, allocation failures, backpressure, bounded retry, receive errors/EOF, header-cache recovery, stale callbacks/fields and surface failure status. The double's headers are private to that target, never used by the runtime.

cmake -S . -B build/packet-tests -DSNX_TOOLS_ONLY=ON -DSNX_TEST_SANITIZERS=ON
cmake --build build/packet-tests --target snx-test-nvdec-packets
ctest --test-dir build/packet-tests -R '^nvdec_packets$' --output-on-failure

nvdec_decode builds against real FFmpeg when SNX_TOOLS_ONLY=OFF. It decodes synthetic 16x16 I_PCM and reference-dependent P-skip pictures, checks exact luma/interleaved chroma values, reuses SPS/PPS and exercises Annex B, four-byte AVCC and bare-NAL submission. No copyrighted video or encoder dependency is required.

cmake --build build --target snx-test-nvdec-decode
ctest --test-dir build -R '^nvdec_(packets|decode)$' --output-on-failure

Validation here: 16 tools-only CTests passed with Clang 21.1.5 and ASan/UBSan; the real-decoder harness passed 2,290 checks against the pinned FFmpeg 9.0.2. The locally built FFmpeg library itself was not sanitizer-instrumented. A -no-pie executable link flag was needed for ASan's address-space reservation in the sandbox.

nvdec_vp9_* (one test a vector, with SNX_TOOLS_ONLY=OFF as nvdec_decode) feeds nvdec_execute the VP9 jobs of 16 of libvpx's test vectors (BSD-3-Clause, downloaded at configure time into CPM_SOURCE_CACHE, pinned by SHA-256 in tests/CMakeLists.txt) and checks every picture on its surface right after its own job, a hidden one too, against the MD5 libvpx lists for the image (the reference decoder's picture for a hidden frame). FFmpeg's decoder, as released, works each job out of the vector (tests/nvdec_vp9_jobs.c): the picture setup from its parsed headers and the probability table from its state after the frame, which a game's player works out itself before it hands NVDEC the job. That part reads FFmpeg's private VP9Context, so it builds against the patched source and the config.h cmake/ffmpeg.cmake keeps with the install, and follows FFmpeg's struct when the pin moves. The vectors cover hidden frames, contexts that adapt, frame parallel mode, superframes, frames shown again, intra-only frames, spatial layers, tiles, size changes and segmentation; taking any of the three hunks out of cmake/ffmpeg-vp9-nvdec.patch fails several of them.

ctest --test-dir build -R '^nvdec_vp9_' --output-on-failure

Limits

The tests substitute linear surface addressing; they verify publication and NV12 interleaving, not actual GOB layout, VIC conversion or GPU presentation. Actual interlaced/B-frame game streams, alternate SPS/PPS IDs and unsupported chroma formats still need capture-based coverage. This patch does not change SPS/PPS reconstruction, the decode-order get_buffer2 strategy, or recover reference pictures already lost to malformed input. Framing autodetection remains a heuristic without explicit container metadata. A game-level green-screen fix still needs in-game verification.


Made from docs/history/nvdec-audit.md at 69237645142c. GPL-3.0-or-later.