NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-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 __GSTNVINFER_IMPL_H__
19 #define __GSTNVINFER_IMPL_H__
20 
21 #include <string.h>
22 #include <sys/time.h>
23 #include <glib.h>
24 #include <gst/gst.h>
25 
26 #include <vector>
27 #include <list>
28 #include <condition_variable>
29 #include <memory>
30 #include <mutex>
31 #include <thread>
32 
33 #include "nvbufsurftransform.h"
34 #include "nvdsinfer_context.h"
35 #include "nvdsinfer_func_utils.h"
36 #include "nvdsmeta.h"
37 #include "nvdspreprocess_meta.h"
38 #include "nvtx3/nvToolsExt.h"
39 
40 G_BEGIN_DECLS
41 typedef struct _GstNvInfer GstNvInfer;
42 
43 void gst_nvinfer_logger(NvDsInferContextHandle handle, unsigned int unique_id,
44  NvDsInferLogLevel log_level, const char* log_message, void* user_ctx);
45 
46 G_END_DECLS
47 
48 using NvDsInferContextInitParamsPtr = std::unique_ptr<NvDsInferContextInitParams>;
49 using NvDsInferContextPtr = std::shared_ptr<INvDsInferContext>;
50 
52 
56 typedef struct {
60  gdouble scale_ratio_x = 0;
64  gdouble scale_ratio_y = 0;
67  guint offset_left = 0;
68  guint offset_top = 0;
70  guint roi_left = 0;
71  guint roi_top = 0;
73  NvDsObjectMeta *obj_meta = nullptr;
74  NvDsFrameMeta *frame_meta = nullptr;
75  NvDsRoiMeta *roi_meta = nullptr;
78  guint batch_index = 0;
80  gulong frame_num = 0;
82  NvBufSurfaceParams *input_surf_params = nullptr;
86  gpointer converted_frame_ptr = nullptr;
89  std::weak_ptr<GstNvInferObjectHistory> history;
90 
92 
94  std::pair<std::weak_ptr<GstNvInferObjectHistory>, NvDsObjectMeta *>;
95 
99 typedef struct {
101  std::vector<GstNvInferFrame> frames;
103  GstBuffer *inbuf = nullptr;
105  gulong inbuf_batch_num = 0;
110  gboolean push_buffer = FALSE;
114  gboolean event_marker = FALSE;
116  GstBuffer *conv_buf = nullptr;
117  nvtxRangeId_t nvtx_complete_buf_range = 0;
118 
121  NvBufSurfTransformSyncObj_t sync_obj = NULL;
122 
125  std::vector <GstNvInferObjHistory_MetaPair> objs_pending_meta_attach;
127 
128 
138 typedef struct
139 {
143  GstMiniObject mini_object;
145  NvDsInferContextPtr infer_context;
149  NvDsInferContextBatchOutput batch_output;
151 
152 namespace gstnvinfer {
153 
155 struct ModelStatus
156 {
160  std::string cfg_file;
161  /* Error message string if any. */
162  std::string message;
163 };
164 
166 class LockGMutex
167 {
168 public:
169  LockGMutex (GMutex &mutex)
170  :m (mutex) {
171  lock ();
172  }
174  if (locked)
175  unlock();
176  }
177  void lock ();
178  void unlock ();
179  void wait (GCond &cond);
180 
181 private:
182  GMutex &m;
183  bool locked = false;
184 };
185 
188 {
199 };
200 
201 /* Helper class to manage the NvDsInferContext and runtime model update. The
202  * model can be updated at runtime by setting "config-file-path" and/or
203  * "model-engine-file" properties with the new config file/model engine file.
204  *
205  * The runtime update implementation would basically create and initialize a
206  * new NvDsInferContext with new parameters and if successful will replace the
207  * current NvDsInferContext instance with the new instance while taking care of
208  * processing synchronization.
209  *
210  * Constraints of runtime model update:
211  * - Model input resolution and channels should not change
212  * - Batch-size of new model engine should be equal or greater than
213  * gst-nvinfer's batch-size
214  * - Type of the model (Detection/Classification/Segmentation) should not
215  * change.
216  *
217  * Check deepstream-test5-app README for more details on OTA and runtime model
218  * update and sample test steps.*/
219 class DsNvInferImpl
220 {
221 public:
222  using ContextReplacementPtr =
223  std::unique_ptr<std::tuple<NvDsInferContextPtr, NvDsInferContextInitParamsPtr, std::string>>;
224 
225  DsNvInferImpl (GstNvInfer *infer);
226  ~DsNvInferImpl ();
227  /* Start the model load thread. */
229  /* Stop the model load thread. Release the NvDsInferContext. */
230  void stop ();
231 
232  bool isContextReady () const { return m_InferCtx.get(); }
233 
235  bool triggerNewModel (const std::string &modelPath, ModelLoadType loadType);
236 
239  void notifyLoadModelStatus (const ModelStatus &res);
240 
243 
246 
247 private:
249  class ModelLoadThread
250  {
251  public:
252  using ModelItem = std::tuple<std::string, ModelLoadType> ;
253 
254  ModelLoadThread (DsNvInferImpl &impl);
255  ~ModelLoadThread ();
256  void queueModel (const std::string &modelPath, ModelLoadType type) {
257  m_PendingModels.push (ModelItem(modelPath, type));
258  }
259  private:
260  void Run();
261 
262  DsNvInferImpl &m_Impl;
263  std::thread m_Thread;
265  };
266 
267  bool initNewInferModelParams (
268  NvDsInferContextInitParams &newParams,
269  const std::string &newModelPath, ModelLoadType loadType,
270  const NvDsInferContextInitParams &oldParams);
271  bool isNewContextValid (
272  INvDsInferContext &newCtx, NvDsInferContextInitParams &newParam);
273  bool triggerContextReplace (
275  const std::string &path);
276  void loadModel (const std::string &path, ModelLoadType loadType);
277 
278  ContextReplacementPtr getNextReplacementUnlock ();
279  NvDsInferStatus flushDataUnlock (LockGMutex &lock);
280  NvDsInferStatus resetContextUnlock (
282  const std::string &path);
283 
284  GstNvInfer *m_GstInfer = nullptr;
286  std::unique_ptr<ModelLoadThread> m_ModelLoadThread;
287  ContextReplacementPtr m_NextContextReplacement;
288 };
289 
290 }
291 
292 #endif
gstnvinfer::DsNvInferImpl::m_InitParams
NvDsInferContextInitParamsPtr m_InitParams
NvDsInferContext initialization params.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:245
gstnvinfer::LockGMutex::LockGMutex
LockGMutex(GMutex &mutex)
Definition: 9.1/sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:169
nvdsinfer::GuardQueue
Definition: sources/libs/nvdsinfer/nvdsinfer_func_utils.h:174
gstnvinfer::LockGMutex::~LockGMutex
~LockGMutex()
Definition: 9.1/sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:173
_GstNvInferObjectHistory
Holds the inference information/history for one object based on it's tracking id.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer.h:148
NvBufSurfTransformSyncObj_t
struct NvBufSurfTransformSyncObj * NvBufSurfTransformSyncObj_t
Holds the information about synchronization objects for asynchronous transform/composite APIs.
Definition: sources/includes/nvbufsurftransform.h:301
gstnvinfer
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:152
gstnvinfer::DsNvInferImpl::ensureReplaceNextContext
NvDsInferStatus ensureReplaceNextContext()
replace context, action in submit_input_buffer
gstnvinfer::LockGMutex::lock
void lock()
gstnvinfer::ModelLoadType
ModelLoadType
Enum for type of model update required.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:187
gstnvinfer::ModelStatus::status
NvDsInferStatus status
Status of the model update.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:158
NvDsInferLogLevel
NvDsInferLogLevel
Enum for the log levels of NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:262
gstnvinfer::DsNvInferImpl
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:219
gstnvinfer::DsNvInferImpl::stop
void stop()
gstnvinfer::DsNvInferImpl::m_InferCtx
NvDsInferContextPtr m_InferCtx
NvDsInferContext to be used for inferencing.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:242
gstnvinfer::LockGMutex::unlock
void unlock()
gstnvinfer::DsNvInferImpl::notifyLoadModelStatus
void notifyLoadModelStatus(const ModelStatus &res)
NvBufSurfaceParams
Hold the information of single buffer in the batch.
Definition: sources/includes/nvbufsurface.h:562
gstnvinfer::DsNvInferImpl::DsNvInferImpl
DsNvInferImpl(GstNvInfer *infer)
gstnvinfer::DsNvInferImpl::triggerNewModel
bool triggerNewModel(const std::string &modelPath, ModelLoadType loadType)
Load new model in separate thread.
gstnvinfer::DsNvInferImpl::~DsNvInferImpl
~DsNvInferImpl()
NvDsInferContextHandle
struct INvDsInferContext * NvDsInferContextHandle
An opaque pointer type to be used as a handle for a context instance.
Definition: sources/includes/nvdsinfer_context.h:669
GstNvInferBatch
Holds information about the batch of frames to be inferred.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:99
gstnvinfer::ModelStatus::message
std::string message
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:162
gstnvinfer::MODEL_LOAD_FROM_ENGINE
@ MODEL_LOAD_FROM_ENGINE
Load a new model by just replacing the model engine assuming no network architecture changes.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:191
_GstNvInfer::unique_id
guint unique_id
Unique ID of the element.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer.h:201
GstNvInferTensorOutputObject
Data type used for the refcounting and managing the usage of NvDsInferContext's batch output and the ...
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:138
gstnvinfer::ModelStatus::cfg_file
std::string cfg_file
Config file used for model update.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:160
_NvDsInferContextInitParams
Holds the initialization parameters required for the NvDsInferContext interface.
Definition: sources/includes/nvdsinfer_context.h:246
gstnvinfer::DsNvInferImpl::start
NvDsInferStatus start()
gstnvinfer::MODEL_LOAD_FROM_CONFIG
@ MODEL_LOAD_FROM_CONFIG
Load a new model with other configuration changes.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:196
gst_nvinfer_logger
void gst_nvinfer_logger(NvDsInferContextHandle handle, unsigned int unique_id, NvDsInferLogLevel log_level, const char *log_message, void *user_ctx)
NvDsRoiMeta
Holds Information about ROI Metadata.
Definition: sources/includes/nvds_roi_meta.h:95
gstnvinfer::DsNvInferImpl::isContextReady
bool isContextReady() const
Definition: 9.1/sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:232
GstNvInfer
typedefG_BEGIN_DECLS struct _GstNvInfer GstNvInfer
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer.h:50
NvDsInferContextBatchOutput
Holds the output for all of the frames in a batch (an array of frame), and related buffer information...
Definition: sources/includes/nvdsinfer_context.h:645
NvDsInferContextPtr
std::shared_ptr< INvDsInferContext > NvDsInferContextPtr
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:49
_NvDsFrameMeta
Holds metadata for a frame in a batch.
Definition: sources/includes/nvdsmeta.h:306
_GstNvInfer
GstNvInfer element structure.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer.h:183
GstNvInferFrame
Holds info about one frame in a batch for inferencing.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:56
GstNvInferObjHistory_MetaPair
std::pair< std::weak_ptr< GstNvInferObjectHistory >, NvDsObjectMeta * > GstNvInferObjHistory_MetaPair
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:94
gstnvinfer::LockGMutex::wait
void wait(GCond &cond)
gstnvinfer::MODEL_LOAD_STOP
@ MODEL_LOAD_STOP
Request the model load thread to stop.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:198
NvDsInferContextInitParamsPtr
std::unique_ptr< NvDsInferContextInitParams > NvDsInferContextInitParamsPtr
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:48
gstnvinfer::ModelStatus
Holds runtime model update status along with the error message if any.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:155
GstBuffer
struct _GstBuffer GstBuffer
Definition: sources/includes/ds3d/common/idatatype.h:24
gstnvinfer::DsNvInferImpl::ContextReplacementPtr
std::unique_ptr< std::tuple< NvDsInferContextPtr, NvDsInferContextInitParamsPtr, std::string > > ContextReplacementPtr
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:223
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:231
_NvDsObjectMeta
Holds metadata for an object in the frame.
Definition: sources/includes/nvdsmeta.h:365