Skip to content

Commit

Permalink
#659 move NvBufSurfaceGenerator to a separate lib (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomskikh authored May 28, 2024
1 parent 3a90eea commit 4d48d00
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 77 deletions.
14 changes: 14 additions & 0 deletions docker/Dockerfile.deepstream
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ COPY libs/gstsavantframemeta .
RUN python3 -m build --wheel --no-isolation && rm -rf _skbuild


# Package builder for NvBufSurfaceGenerator
FROM builder AS nv-buf-surface-generator-builder

WORKDIR /libs/nvbufsurfacegenerator
COPY libs/nvbufsurfacegenerator/requirements.txt .
RUN python3 -m pip install --no-cache-dir -r requirements.txt

COPY libs/nvbufsurfacegenerator .
RUN python3 -m build --wheel --no-isolation && rm -rf _skbuild


# Savant boost(cuda) library builder
FROM builder AS savant-boost-builder
# TODO: Determine JetPack release (replace `r36.2`)
Expand Down Expand Up @@ -149,6 +160,9 @@ RUN python -m pip install --no-cache-dir /libs/savanboost/dist/*.whl
COPY --from=savant-meta-builder /libs/gstsavantframemeta/dist /libs/gstsavantframemeta/dist
RUN python -m pip install --no-cache-dir /libs/gstsavantframemeta/dist/*.whl

COPY --from=nv-buf-surface-generator-builder /libs/nvbufsurfacegenerator/dist /libs/nvbufsurfacegenerator/dist
RUN python -m pip install --no-cache-dir /libs/nvbufsurfacegenerator/dist/*.whl

COPY --from=savant-package-builder /tmp/build/dist /libs/savant/dist
RUN python -m pip install --no-cache-dir /libs/savant/dist/*.whl

Expand Down
3 changes: 0 additions & 3 deletions libs/gstsavantframemeta/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ if (SAVANT_NVDS_ENABLED)
set(SOURCE_FILES
${SOURCE_FILES}
"gstsavantframemeta/src/gstsavantbatchmeta.cpp"
"gstsavantframemeta/src/nvbufsurface_generator.cpp"
"gstsavantframemeta/src/nvdssavantframemeta.cpp"
"gstsavantframemeta/src/savantrsprobes.cpp"
"gstsavantframemeta/src/savantnvprobes.cpp"
Expand All @@ -114,7 +113,6 @@ if (SAVANT_NVDS_ENABLED)
set(HEADER_FILES
${HEADER_FILES}
"gstsavantframemeta/include/gstsavantbatchmeta.h"
"gstsavantframemeta/include/nvbufsurface_generator.h"
"gstsavantframemeta/include/nvdssavantframemeta.h"
"gstsavantframemeta/include/savantrsprobes.h"
"gstsavantframemeta/include/savantnvprobes.h"
Expand Down Expand Up @@ -152,7 +150,6 @@ if (SAVANT_NVDS_ENABLED)
${CUDA_LIBRARIES}
nvds_meta
nvdsgst_meta
nvdsbufferpool
)
endif()

Expand Down
3 changes: 0 additions & 3 deletions libs/gstsavantframemeta/gstsavantframemeta/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ if (SAVANT_NVDS_ENABLED)
${SOURCE_FILES}
${PROJECT_INCLUDE_DIR}/gstsavantbatchmeta.h
${PROJECT_SOURCE_DIR}/gstsavantbatchmeta.cpp
${PROJECT_INCLUDE_DIR}/nvbufsurface_generator.h
${PROJECT_SOURCE_DIR}/nvbufsurface_generator.cpp
${PROJECT_INCLUDE_DIR}/nvdssavantframemeta.h
${PROJECT_SOURCE_DIR}/nvdssavantframemeta.cpp
${PROJECT_INCLUDE_DIR}/savantrsprobes.h
Expand Down Expand Up @@ -122,7 +120,6 @@ if (SAVANT_NVDS_ENABLED)
${CUDA_LIBRARIES}
nvds_meta
nvdsgst_meta
nvdsbufferpool
)
endif()

Expand Down
21 changes: 0 additions & 21 deletions libs/gstsavantframemeta/pygstsavantframemeta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,3 @@ def add_pad_probe_to_remove_tracker_objs(pad: Gst.Pad):
:param pad: nvtracker src pad.
"""
pygstsavantframemeta.add_pad_probe_to_remove_tracker_objs(hash(pad))


class NvBufSurfaceGenerator:
"""Generates GStreamer buffers with NvBufSurface memory allocated.
:param caps: Caps for the generated buffers.
:param gpu_id: ID of the GPU to allocate NvBufSurface.
:param mem_type: Memory type for the NvBufSurface.
"""

def __init__(self, caps: Gst.Caps, gpu_id: int, mem_type: int):
self._nested = pygstsavantframemeta.NvBufSurfaceGenerator(
hash(caps), gpu_id, mem_type
)

def create_surface(self, buffer: Gst.Buffer):
"""Create a new NvBufSurface and attach it to the given buffer.
:param buffer: Buffer to attach the NvBufSurface to.
"""
return self._nested.create_surface(hash(buffer))
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#include "gstsavantframemeta.h"


#ifdef SAVANT_NVDS_ENABLED

#include "gstsavantbatchmeta.h"
#include "nvbufsurface_generator.h"
#include "nvdssavantframemeta.h"
#include "savantrsprobes.h"
#include "savantnvprobes.h"


#endif // SAVANT_NVDS_ENABLED

#include <pybind11/pybind11.h>


namespace py = pybind11;

PYBIND11_MODULE(pygstsavantframemeta, m) {
Expand Down Expand Up @@ -86,10 +80,8 @@ PYBIND11_MODULE(pygstsavantframemeta, m) {
},
py::return_value_policy::reference);

m.def("add_convert_savant_frame_meta_pad_probe", [](
size_t gst_pad,
bool to_nvds
) {
m.def("add_convert_savant_frame_meta_pad_probe", [](size_t gst_pad,
bool to_nvds) {
auto *pad = reinterpret_cast<GstPad *>(gst_pad);
gpointer conv_func;
if (to_nvds) {
Expand All @@ -101,11 +93,9 @@ PYBIND11_MODULE(pygstsavantframemeta, m) {
convert_savant_frame_meta_pad_probe, conv_func, NULL);
});

m.def("add_pad_probe_to_move_frame", [](
size_t gst_pad,
uintptr_t video_pipeline,
const char *stage
) {
m.def("add_pad_probe_to_move_frame", [](size_t gst_pad,
uintptr_t video_pipeline,
const char *stage) {
auto *pad = reinterpret_cast<GstPad *>(gst_pad);
SavantRsPadProbeData *data = create_savant_rs_pad_probe_data(
video_pipeline, stage);
Expand All @@ -117,11 +107,9 @@ PYBIND11_MODULE(pygstsavantframemeta, m) {
(GDestroyNotify)release_savant_rs_pad_probe_data);
});

m.def("add_pad_probe_to_pack_and_move_frames", [](
size_t gst_pad,
uintptr_t video_pipeline,
const char *stage
) {
m.def("add_pad_probe_to_pack_and_move_frames", [](size_t gst_pad,
uintptr_t video_pipeline,
const char *stage) {
auto *pad = reinterpret_cast<GstPad *>(gst_pad);
SavantRsPadProbeData *data = create_savant_rs_pad_probe_data(
video_pipeline, stage);
Expand All @@ -133,11 +121,9 @@ PYBIND11_MODULE(pygstsavantframemeta, m) {
(GDestroyNotify)release_savant_rs_pad_probe_data);
});

m.def("add_pad_probe_to_move_batch", [](
size_t gst_pad,
uintptr_t video_pipeline,
const char *stage
) {
m.def("add_pad_probe_to_move_batch", [](size_t gst_pad,
uintptr_t video_pipeline,
const char *stage) {
auto *pad = reinterpret_cast<GstPad *>(gst_pad);
SavantRsPadProbeData *data = create_savant_rs_pad_probe_data(
video_pipeline, stage);
Expand All @@ -149,11 +135,9 @@ PYBIND11_MODULE(pygstsavantframemeta, m) {
(GDestroyNotify)release_savant_rs_pad_probe_data);
});

m.def("add_pad_probe_to_unpack_and_move_batch", [](
size_t gst_pad,
uintptr_t video_pipeline,
const char *stage
) {
m.def("add_pad_probe_to_unpack_and_move_batch", [](size_t gst_pad,
uintptr_t video_pipeline,
const char *stage) {
auto *pad = reinterpret_cast<GstPad *>(gst_pad);
SavantRsPadProbeData *data = create_savant_rs_pad_probe_data(
video_pipeline, stage);
Expand All @@ -174,24 +158,5 @@ PYBIND11_MODULE(pygstsavantframemeta, m) {
NULL,
NULL);
});

py::class_<NvBufSurfaceGenerator>(m, "NvBufSurfaceGenerator")
.def(py::init([](
size_t gst_caps,
uint32_t gpuId,
uint32_t memType
) {
auto *caps = reinterpret_cast<GstCaps *>(gst_caps);
return new NvBufSurfaceGenerator(caps, gpuId, memType);
}))
.def("create_surface", [](
NvBufSurfaceGenerator &self,
size_t gst_buffer_dest
) {
auto *buffer_dest = reinterpret_cast<GstBuffer *>(gst_buffer_dest);
py::gil_scoped_release release;
self.create_surface(buffer_dest);
});

#endif // SAVANT_NVDS_ENABLED
}
81 changes: 81 additions & 0 deletions libs/nvbufsurfacegenerator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
cmake_minimum_required(VERSION 3.12)

project(pynvbufsurfacegenerator VERSION "0.0.1" LANGUAGES CXX CUDA)

set(python_module_name pynvbufsurfacegenerator)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined" )
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-undefined" )

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS "-O3")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

find_package(PkgConfig REQUIRED)
find_package(CUDA REQUIRED)
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
find_package(pybind11 CONFIG REQUIRED)

if(NOT DEFINED DeepStream_DIR)
set(DeepStream_DIR /opt/nvidia/deepstream/deepstream)
endif()

pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0)

include_directories(
${Python3_INCLUDE_DIRS}
${GLIB_INCLUDE_DIRS}
${GSTREAMER_INCLUDE_DIRS}
${CUDA_INCLUDE_DIRS}
${DeepStream_DIR}/sources/includes
${CMAKE_SOURCE_DIR}/nvbufsurfacegenerator/include
${CMAKE_SOURCE_DIR}/pynvbufsurfacegenerator
)

link_directories(
${Python3_LIBRARY_DIRS}
${GLIB_LIBRARY_DIRS}
${GSTREAMER_LIBRARY_DIRS}
${CUDA_LIBRARY_DIRS}
${DeepStream_DIR}/lib
)

add_subdirectory(nvbufsurfacegenerator ../build/nvbufsurfacegenerator)

set(SOURCE_FILES
"nvbufsurfacegenerator/src/nvbufsurface_generator.cpp"
)
set(HEADER_FILES
"nvbufsurfacegenerator/include/nvbufsurface_generator.h"
)

file (GLOB PYTHON_FILES "pynvbufsurfacegenerator/*.cpp" "pynvbufsurfacegenerator/*.h")

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_FILES} ${HEADER_FILES} ${PYTHON_FILES} )

pybind11_add_module(${python_module_name} SHARED
${SOURCE_FILES}
${HEADER_FILES}
${PYTHON_FILES}
)

target_link_libraries(${python_module_name} PRIVATE
${Python3_LIBRARIES}
${GLIB_LIBRARIES}
${GSTREAMER_LIBRARIES}
${CUDA_LIBRARIES}
nvdsbufferpool
nvbufsurfacegenerator
pybind11::module
pybind11::lto
)

pybind11_extension(${python_module_name})
pybind11_strip(${python_module_name})

install(TARGETS ${python_module_name} DESTINATION .)
57 changes: 57 additions & 0 deletions libs/nvbufsurfacegenerator/nvbufsurfacegenerator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
cmake_minimum_required(VERSION 3.12)

project(nvbufsurfacegenerator VERSION "0.0.1" LANGUAGES CXX CUDA)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined" )
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-undefined" )

find_package(PkgConfig REQUIRED)
find_package(CUDA REQUIRED)
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)

if (NOT DEFINED DeepStream_DIR)
set(DeepStream_DIR /opt/nvidia/deepstream/deepstream)
endif()

pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0)

include_directories(
${GLIB_INCLUDE_DIRS}
${GSTREAMER_INCLUDE_DIRS}
${CUDA_INCLUDE_DIRS}
${DeepStream_DIR}/sources/includes
)

link_directories(
${GLIB_LIBRARY_DIRS}
${GSTREAMER_LIBRARY_DIRS}
${CUDA_LIBRARY_DIRS}
${DeepStream_DIR}/lib
)

AUX_SOURCE_DIRECTORY(src SRC_FILES)

set(PROJECT_SOURCE_DIR "src")
set(PROJECT_INCLUDE_DIR "include")

set(SOURCE_FILES
${PROJECT_INCLUDE_DIR}/nvbufsurface_generator.h
${PROJECT_SOURCE_DIR}/nvbufsurface_generator.cpp
)

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_FILES})

add_library(nvbufsurfacegenerator SHARED ${SOURCE_FILES})
target_link_libraries(
nvbufsurfacegenerator
${GLIB_LIBRARIES}
${GSTREAMER_LIBRARIES}
${CUDA_LIBRARIES}
nvdsbufferpool
)

target_include_directories(nvbufsurfacegenerator PUBLIC include)

install(TARGETS nvbufsurfacegenerator LIBRARY PUBLIC_HEADER)
27 changes: 27 additions & 0 deletions libs/nvbufsurfacegenerator/pynvbufsurfacegenerator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import gi

from . import pynvbufsurfacegenerator

gi.require_version('Gst', '1.0')
from gi.repository import Gst


class NvBufSurfaceGenerator:
"""Generates GStreamer buffers with NvBufSurface memory allocated.
:param caps: Caps for the generated buffers.
:param gpu_id: ID of the GPU to allocate NvBufSurface.
:param mem_type: Memory type for the NvBufSurface.
"""

def __init__(self, caps: Gst.Caps, gpu_id: int, mem_type: int):
self._nested = pynvbufsurfacegenerator.NvBufSurfaceGenerator(
hash(caps), gpu_id, mem_type
)

def create_surface(self, buffer: Gst.Buffer):
"""Create a new NvBufSurface and attach it to the given buffer.
:param buffer: Buffer to attach the NvBufSurface to.
"""
return self._nested.create_surface(hash(buffer))
Loading

0 comments on commit 4d48d00

Please sign in to comment.