Repository navigation
[Camera streamer 3/5] Standalone NVDEC CUDA decoder #1391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a0db28e
Add standalone NVDEC CUDA decoder extension
jsepulveda-nvidia 1de7863
Protect decoder lifecycle and recover resources after CUDA failures
jsepulveda-nvidia 4225865
Validate decoded surface format and writable byte output buffers
jsepulveda-nvidia c750769
Validate output dimensions against the native decoder configuration
jsepulveda-nvidia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # Python environment and caches | ||
| .venv/ | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
| .pytest_cache/ | ||
| .mypy_cache/ | ||
| .ruff_cache/ | ||
| .coverage | ||
| htmlcov/ | ||
|
|
||
| # Native codec build outputs | ||
| codec/build/ | ||
| codec/_camera_receiver_codec*.so | ||
|
|
||
| # Local logs and runtime artifacts | ||
| *.log | ||
| *.pid | ||
| *.sock | ||
| *.avs1 | ||
| *.hvs2 | ||
| *.nv12 | ||
| *.h265 | ||
| *.hevc |
123 changes: 123 additions & 0 deletions
123
examples/camera_streamer/camera_receiver/codec/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Native H.264/HEVC NVDEC pybind11 module for camera_receiver. The .so | ||
| # lands next to __init__.py so the package imports without any | ||
| # install step. | ||
|
|
||
| cmake_minimum_required(VERSION 3.20) | ||
|
|
||
| # Ampere / Ada / Hopper / Orin / Thor. Override with | ||
| # CMAKE_CUDA_ARCHITECTURES on the configure line for fast iteration. | ||
| if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES) | ||
| set(CMAKE_CUDA_ARCHITECTURES "80;86;87;89;90") | ||
| endif() | ||
|
|
||
| project(camera_receiver_codec LANGUAGES C CXX CUDA) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 17) | ||
| set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
| set(CMAKE_CUDA_STANDARD 17) | ||
| set(CMAKE_CUDA_STANDARD_REQUIRED ON) | ||
| set(CMAKE_POSITION_INDEPENDENT_CODE ON) | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Dependencies | ||
| # --------------------------------------------------------------------------- | ||
| find_package(CUDAToolkit REQUIRED) | ||
| find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module) | ||
|
|
||
| # pybind11 is pip-installed into the active camera_receiver Python environment. | ||
| execute_process( | ||
| COMMAND ${Python3_EXECUTABLE} -c "import pybind11; print(pybind11.get_cmake_dir())" | ||
| OUTPUT_VARIABLE pybind11_DIR | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE | ||
| RESULT_VARIABLE _pybind11_rc | ||
| ) | ||
| if(NOT _pybind11_rc EQUAL 0) | ||
| message(FATAL_ERROR "pybind11 not found. `uv pip install pybind11` into the active venv.") | ||
| endif() | ||
| find_package(pybind11 REQUIRED CONFIG) | ||
|
|
||
| # NVDEC entry-point library (ships with the NVIDIA driver). | ||
| if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64") | ||
| set(_lib_arch_dir "/usr/lib/aarch64-linux-gnu/nvidia") | ||
| else() | ||
| set(_lib_arch_dir "/usr/lib/x86_64-linux-gnu") | ||
| endif() | ||
|
|
||
| find_library(NVCUVID_LIBRARY | ||
| NAMES libnvcuvid.so.1 libnvcuvid.so nvcuvid | ||
| HINTS ${CUDA_TOOLKIT_ROOT_DIR}/lib64 | ||
| PATHS "${_lib_arch_dir}" | ||
| ) | ||
| if(NOT NVCUVID_LIBRARY) | ||
| message(FATAL_ERROR "libnvcuvid not found. Install the NVIDIA driver.") | ||
| endif() | ||
| message(STATUS "camera_receiver codec: NVCUVID=${NVCUVID_LIBRARY}") | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # NVIDIA Video Codec SDK headers + reference decoder sources. | ||
| # --------------------------------------------------------------------------- | ||
| set(_nvc_sdk_url | ||
| "https://edge.urm.nvidia.com/artifactory/sw-holoscan-thirdparty-generic-local/holohub/operators/holohub_nvc_13.0.19.zip" | ||
| ) | ||
| set(_nvc_sdk_zip "${CMAKE_BINARY_DIR}/holohub_nvc_13.0.19.zip") | ||
| set(_nvc_sdk_vendor_zip "${CMAKE_CURRENT_SOURCE_DIR}/third_party/holohub_nvc_13.0.19.zip") | ||
| set(_nvc_sdk_dir "${CMAKE_BINARY_DIR}/_deps/nvc_sdk") | ||
|
|
||
| if(EXISTS ${_nvc_sdk_vendor_zip} AND NOT EXISTS ${_nvc_sdk_zip}) | ||
| message(STATUS "Using vendored NVIDIA Video Codec SDK zip...") | ||
| file(COPY ${_nvc_sdk_vendor_zip} DESTINATION ${CMAKE_BINARY_DIR}) | ||
| endif() | ||
| if(NOT EXISTS ${_nvc_sdk_zip}) | ||
| message(STATUS "Downloading NVIDIA Video Codec SDK...") | ||
| file(DOWNLOAD ${_nvc_sdk_url} ${_nvc_sdk_zip} | ||
| SHOW_PROGRESS | ||
| EXPECTED_MD5 "a08c33c282bc92469ab8cf863aca9087" | ||
| ) | ||
| endif() | ||
| if(NOT EXISTS ${_nvc_sdk_dir}/NvDecoder) | ||
| message(STATUS "Extracting NVIDIA Video Codec SDK...") | ||
| file(MAKE_DIRECTORY ${_nvc_sdk_dir}) | ||
| execute_process( | ||
| COMMAND ${CMAKE_COMMAND} -E tar xzf ${_nvc_sdk_zip} | ||
| WORKING_DIRECTORY ${_nvc_sdk_dir} | ||
| ) | ||
| endif() | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # pybind11 module | ||
| # --------------------------------------------------------------------------- | ||
| pybind11_add_module(_camera_receiver_codec MODULE | ||
| codec_bindings.cpp | ||
| nv_video_decoder.cpp | ||
| nv12_to_rgba.cu | ||
| ${_nvc_sdk_dir}/NvDecoder/NvDecoder.cpp | ||
| ${_nvc_sdk_dir}/Logger.cpp | ||
| ) | ||
|
|
||
| target_include_directories(_camera_receiver_codec PRIVATE | ||
| ${_nvc_sdk_dir} | ||
| ${_nvc_sdk_dir}/NvDecoder | ||
| ) | ||
|
|
||
| target_compile_definitions(_camera_receiver_codec PRIVATE NV_WINDOWS=0 NV_LINUX=1) | ||
|
|
||
| # NVCODEC SDK reference sources carry legacy warnings. | ||
| target_compile_options(_camera_receiver_codec PRIVATE | ||
| $<$<COMPILE_LANGUAGE:CXX>:-Wno-reorder -Wno-uninitialized -Wno-unused-variable | ||
| -Wno-unused-parameter -Wno-missing-field-initializers -Wno-sign-compare> | ||
| ) | ||
|
|
||
| target_link_libraries(_camera_receiver_codec PRIVATE | ||
| CUDA::cudart | ||
| CUDA::cuda_driver | ||
| ${NVCUVID_LIBRARY} | ||
| ) | ||
|
|
||
| # Drop the .so next to __init__.py so the package imports straight | ||
| # from the source tree. | ||
| set_target_properties(_camera_receiver_codec PROPERTIES | ||
| LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
| ) |
23 changes: 23 additions & 0 deletions
23
examples/camera_streamer/camera_receiver/codec/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Native NVDEC video decoder for camera_receiver.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| try: | ||
| from ._camera_receiver_codec import ( # type: ignore[import-not-found] | ||
| Codec, | ||
| DecoderConfig, | ||
| NvVideoDecoder, | ||
| ) | ||
| except ImportError as e: | ||
| raise ImportError( | ||
| "camera_receiver native codec not built. Follow " | ||
| "the host receiver build instructions in examples/camera_streamer/README.md." | ||
| ) from e | ||
|
|
||
| __all__ = [ | ||
| "Codec", | ||
| "DecoderConfig", | ||
| "NvVideoDecoder", | ||
| ] |
95 changes: 95 additions & 0 deletions
95
examples/camera_streamer/camera_receiver/codec/codec_bindings.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // pybind11 bindings — public surface is ``camera_receiver.codec``. | ||
|
|
||
| #include "cuda_buffers.hpp" | ||
| #include "nv_video_decoder.hpp" | ||
|
|
||
| #include <pybind11/pybind11.h> | ||
| #include <pybind11/stl.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| namespace py = pybind11; | ||
| using camera_receiver::codec::Codec; | ||
| using camera_receiver::codec::DecoderConfig; | ||
| using camera_receiver::codec::NvVideoDecoder; | ||
|
|
||
| PYBIND11_MODULE(_camera_receiver_codec, m) | ||
| { | ||
| m.doc() = "Native NVDEC H.264/HEVC decoder for camera_receiver."; | ||
|
|
||
| py::enum_<Codec>(m, "Codec").value("H264", Codec::H264).value("HEVC", Codec::HEVC); | ||
|
|
||
| py::class_<DecoderConfig>(m, "DecoderConfig") | ||
| .def(py::init<>()) | ||
| .def_readwrite("width", &DecoderConfig::width) | ||
| .def_readwrite("height", &DecoderConfig::height) | ||
| .def_readwrite("full_range", &DecoderConfig::full_range) | ||
| .def_readwrite("gpu_id", &DecoderConfig::gpu_id) | ||
| .def_readwrite("codec", &DecoderConfig::codec); | ||
|
|
||
| py::class_<NvVideoDecoder>(m, "NvVideoDecoder") | ||
| .def(py::init<const DecoderConfig&>(), py::arg("config")) | ||
| .def( | ||
| "decode", | ||
| [](NvVideoDecoder& self, const py::buffer& packet, const py::object& rgba_out, std::uint32_t width, | ||
| std::uint32_t height, int rotation) -> bool | ||
| { | ||
| py::buffer_info pkt = packet.request(); | ||
| if (pkt.ndim != 1) | ||
| { | ||
| throw std::runtime_error("packet must be a 1D byte buffer"); | ||
| } | ||
| if (pkt.itemsize != 1) | ||
| { | ||
| throw std::runtime_error("packet must be a uint8 buffer"); | ||
| } | ||
| // Destination capacity must match the decoder, not caller-supplied dimensions. | ||
| if (width != self.width() || height != self.height()) | ||
| throw std::runtime_error("width/height do not match the configured decoder size"); | ||
| const CudaBuf buf = extract_cuda_buffer(rgba_out, width, height); | ||
| bool produced = false; | ||
| { | ||
| py::gil_scoped_release release; | ||
| produced = self.decode(static_cast<const std::uint8_t*>(pkt.ptr), | ||
| static_cast<std::size_t>(pkt.size), buf.ptr, buf.row_pitch_bytes, rotation); | ||
|
jsepulveda-nvidia marked this conversation as resolved.
Resolved
|
||
| } | ||
| return produced; | ||
| }, | ||
| py::arg("packet"), py::arg("rgba_out"), py::arg("width"), py::arg("height"), py::arg("rotation") = 0, | ||
| "Feed one Annex-B AU. Returns True iff a frame was written to rgba_out.") | ||
| .def( | ||
| "decode_nv12", | ||
| [](NvVideoDecoder& self, const py::buffer& packet, const py::object& nv12_out, std::uint32_t width, | ||
| std::uint32_t height, int rotation) -> bool | ||
| { | ||
| py::buffer_info pkt = packet.request(); | ||
| if (pkt.ndim != 1) | ||
| { | ||
| throw std::runtime_error("packet must be a 1D byte buffer"); | ||
| } | ||
| if (pkt.itemsize != 1) | ||
| { | ||
| throw std::runtime_error("packet must be a uint8 buffer"); | ||
| } | ||
| // Destination capacity must match the decoder, not caller-supplied dimensions. | ||
| if (width != self.width() || height != self.height()) | ||
| throw std::runtime_error("width/height do not match the configured decoder size"); | ||
| const CudaBuf buf = extract_nv12_buffer(nv12_out, width, height); | ||
| bool produced = false; | ||
| { | ||
| py::gil_scoped_release release; | ||
| produced = | ||
| self.decode_nv12(static_cast<const std::uint8_t*>(pkt.ptr), static_cast<std::size_t>(pkt.size), | ||
| buf.ptr, buf.row_pitch_bytes, rotation); | ||
| } | ||
| return produced; | ||
| }, | ||
| py::arg("packet"), py::arg("nv12_out"), py::arg("width"), py::arg("height"), py::arg("rotation") = 0, | ||
| "Feed one Annex-B AU. Returns True iff a frame was copied to a tight NV12 GPU buffer.") | ||
| .def("reset", &NvVideoDecoder::reset, "Tear down NVDEC state. Use after long stream silence."); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.