NVIDIA DeepStream SDK API Reference

9.2 Release
sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef CUSTOMLIB_IMPL_H
19 #define CUSTOMLIB_IMPL_H
20 
21 // PyTorch Library
22 #include <torch/torch.h>
23 
24 // NVIDIA DeepStream Library
25 #include "nvbufsurface.h"
26 #include "nvbufsurftransform.h"
27 #include "gst-nvquery.h"
28 #include "gstnvdsmeta.h"
29 #include "gst-nvevent.h"
30 #include "nvdscustomusermeta.h"
31 #include "nvdsdummyusermeta.h"
32 #include "nvdscustomlib_base.hpp"
33 
34 // Preprocess Library
35 #include "preprocess/calibration_parser.h"
36 
37 // Sparse4D Utils Library
38 #include "utils/tensor.h"
39 #include "utils/config_parser.h"
40 #include "utils/log_utils.h"
41 
42 // Sparse4D Infer Library
43 #include "infer/engine_builder.h"
44 #include "infer/tensor_rt_engine.h"
45 
46 // Format definitions
47 #define FORMAT_NV12 "NV12"
48 #define FORMAT_RGBA "RGBA"
49 #define FORMAT_I420 "I420"
50 #define GST_CAPS_FEATURE_MEMORY_NVMM "memory:NVMM"
51 
52 /* This quark is required to identify NvDsMeta when iterating through the buffer metadatas */
53 extern GQuark _dsmeta_quark;
54 
55 // Type definitions for Eigen matrices
56 using Matrix4f = Eigen::Matrix<float, 4, 4>;
57 
58 // Function declarations
59 void *set_metadata_ptr(void);
61 
66 {
67 public:
72  m_VectorProperty.clear();
73  m_CustomConfigLoaded = false;
74  m_SensorNames.clear();
75 
76  // Initialize logger pointer
77  m_Logger = new PrettyLogger();
78 
79  // Initialize tensor cache
80  m_TensorCache = new TensorCache();
81 
82  Initialize();
83  }
84 
88  void Initialize();
89 
96  virtual bool SetInitParams(DSCustom_CreateParams *params);
97 
104  virtual bool SetProperty(Property &prop);
105 
112  virtual bool HandleEvent(GstEvent *event);
113 
119  virtual char *QueryProperties();
120 
127  virtual BufferResult ProcessBuffer(GstBuffer *inbuf);
128 
137  virtual GstCaps *GetCompatibleCaps(GstPadDirection direction,
138  GstCaps* in_caps, GstCaps* othercaps);
139 
144 
145  gboolean hw_caps;
147 private:
148  std::string errorMessage;
150  std::string skipMessage;
158  NvDsBatchMeta *GetNVDS_BatchMeta(GstBuffer *buffer);
159 
160 
161 public:
162  guint source_id = 0;
163  guint m_FrameNum = 0;
165  guint m_DumpedFrameNum = 0;
166  bool m_TransformMode = false;
168  /* Custom Library Bufferpool */
169  GstBufferPool *m_dsBufferPool = NULL;
170  GstBufferPool *m_swbufpool = NULL;
171  guint swbuffersize;
175  void *m_scratchNvBufSurface = NULL;
177  // Currently dumps first 5 input video frame into file for demonstration purpose
178  // Use vooya or similar player to view NV12 / RGBA video raw frame
179  int dump_max_frames = 5;
181  /* Additional flags for metadata insertion */
182  bool m_dummyMetaInsert = false;
183  bool m_fillDummyBatchMeta = false;
185  /* Config file parser properties */
187  std::unique_ptr<ConfigParser> m_configParser;
188  bool m_CustomConfigLoaded = false;
189  std::vector<Property> m_VectorProperty;
191  /* Logger instance */
193 
194  /* Vector containing sensor names */
195  std::vector<std::string> m_SensorNames;
196 
197  /* Tensor cache */
199 
200  // TensorRT Engine instance for inference
201  std::unique_ptr<TensorRTEngine> m_TensorRTEngine;
202 
203  /* Map to store projection matrices by camera name */
204  std::map<std::string, Matrix4f> m_CameraProjectionMatrices;
205 
206  /* Map to store transformed projection matrices by camera name */
207  std::map<std::string, Matrix4f> m_CameraTransformedProjectionMatrices;
208 
209  /* Flag to track if EOS has been received */
210  bool m_eosReceived = false;
211 };
212 
220 
221 #endif /* CUSTOMLIB_IMPL_H */
DSCustom_CreateParams
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_interface.hpp:32
SampleAlgorithm::m_scratchNvBufSurface
void * m_scratchNvBufSurface
Scratch NvBufSurface.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:175
fill_dummy_batch_meta_on_buffer
void fill_dummy_batch_meta_on_buffer(NvDsBatchMeta *batch_meta)
SampleAlgorithm
Sample implementation of the custom library.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:65
SampleAlgorithm::GetCompatibleCaps
virtual GstCaps * GetCompatibleCaps(GstPadDirection direction, GstCaps *in_caps, GstCaps *othercaps)
Get compatible caps.
SampleAlgorithm::m_eosReceived
bool m_eosReceived
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:210
PrettyLogger
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/log_utils.h:57
SampleAlgorithm::m_Logger
PrettyLogger * m_Logger
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:192
SampleAlgorithm::m_dummyMetaInsert
bool m_dummyMetaInsert
Dummy metadata insertion flag.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:182
TensorCache
Class to manage multiple tensors with string-based lookup.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/tensor.h:511
SampleAlgorithm::SetInitParams
virtual bool SetInitParams(DSCustom_CreateParams *params)
Set initialization parameters.
SampleAlgorithm::swbuffersize
guint swbuffersize
Software buffer size.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:171
SampleAlgorithm::m_FrameNum
guint m_FrameNum
Frame number (increments for all frames including skipped)
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:163
SampleAlgorithm::m_fillDummyBatchMeta
bool m_fillDummyBatchMeta
Fill dummy batch metadata flag.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:183
SampleAlgorithm::m_configParser
std::unique_ptr< ConfigParser > m_configParser
Config parser.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:187
SampleAlgorithm::hw_caps
gboolean hw_caps
Hardware capabilities flag.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:145
SampleAlgorithm::m_swbufpool
GstBufferPool * m_swbufpool
Software buffer pool.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:170
SampleAlgorithm::m_CameraProjectionMatrices
std::map< std::string, Matrix4f > m_CameraProjectionMatrices
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:204
SampleAlgorithm::m_VectorProperty
std::vector< Property > m_VectorProperty
Vector containing key:value pair of custom lib properties.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:189
SampleAlgorithm::m_CustomConfigLoaded
bool m_CustomConfigLoaded
Custom config loaded flag.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:188
SampleAlgorithm::dump_max_frames
int dump_max_frames
Maximum frames to dump.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:179
SampleAlgorithm::m_ProcessedFrameNum
guint m_ProcessedFrameNum
Processed frame number (only increments for actually processed frames)
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:164
SampleAlgorithm::source_id
guint source_id
Source ID.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:162
SampleAlgorithm::ProcessBuffer
virtual BufferResult ProcessBuffer(GstBuffer *inbuf)
Process incoming buffer.
ConfigProperties
Structure containing all configurable properties from the YAML config file.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/config_properties.h:32
IDSCustomLibrary
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_interface.hpp:52
set_metadata_ptr
void * set_metadata_ptr(void)
SampleAlgorithm::m_TensorCache
TensorCache * m_TensorCache
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:198
GstBuffer
struct _GstBuffer GstBuffer
Definition: includes/ds3d/common/idatatype.h:24
_NvBufSurfTransformConfigParams
Holds configuration parameters for a transform/composite session.
Definition: includes/nvbufsurftransform.h:180
SampleAlgorithm::~SampleAlgorithm
~SampleAlgorithm()
Destructor.
Property
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:38
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: includes/nvdsmeta.h:264
SampleAlgorithm::m_TransformMode
bool m_TransformMode
Transform mode flag.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:166
SampleAlgorithm::Initialize
void Initialize()
Initialize the custom library.
SampleAlgorithm::m_dsBufferPool
GstBufferPool * m_dsBufferPool
DeepStream buffer pool.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:169
SampleAlgorithm::m_SensorNames
std::vector< std::string > m_SensorNames
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:195
SampleAlgorithm::SetProperty
virtual bool SetProperty(Property &prop)
Set custom properties of the library.
SampleAlgorithm::m_ConfigParams
NvBufSurfTransformConfigParams m_ConfigParams
Transform config parameters.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:173
Matrix4f
Eigen::Matrix< float, 4, 4 > Matrix4f
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:56
SampleAlgorithm::m_TensorRTEngine
std::unique_ptr< TensorRTEngine > m_TensorRTEngine
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:201
SampleAlgorithm::m_ConfigProperties
ConfigProperties m_ConfigProperties
Structure to store all configuration properties loaded from YAML config file.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:186
SampleAlgorithm::QueryProperties
virtual char * QueryProperties()
Query properties.
CreateCustomAlgoCtx
IDSCustomLibrary * CreateCustomAlgoCtx(DSCustom_CreateParams *params)
Create a custom algorithm context.
_dsmeta_quark
GQuark _dsmeta_quark
SampleAlgorithm::m_DumpedFrameNum
guint m_DumpedFrameNum
Dumped frame number.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:165
SampleAlgorithm::HandleEvent
virtual bool HandleEvent(GstEvent *event)
Handle GStreamer events.
DSCustomLibraryBase
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:36
SampleAlgorithm::SampleAlgorithm
SampleAlgorithm()
Constructor.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:71
SampleAlgorithm::m_CameraTransformedProjectionMatrices
std::map< std::string, Matrix4f > m_CameraTransformedProjectionMatrices
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:207
BufferResult
BufferResult
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:24