NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/sources/libs/gst-nvdsinferbase/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 __GSTNVINFERBASE_IMPL_H__
19 #define __GSTNVINFERBASE_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 "nvds_audio_meta.h"
38 #include "nvtx3/nvToolsExt.h"
39 
40 G_BEGIN_DECLS
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;
66  NvDsObjectMeta *obj_meta = nullptr;
67  NvDsFrameMeta *frame_meta = nullptr;
68  NvDsAudioFrameMeta *audio_frame_meta = nullptr;
71  guint batch_index = 0;
73  gulong frame_num = 0;
75  NvBufSurfaceParams *input_surf_params = nullptr;
79  gpointer converted_frame_ptr = nullptr;
82  std::weak_ptr<GstNvInferObjectHistory> history;
83 
85 
87  std::pair<std::weak_ptr<GstNvInferObjectHistory>, NvDsObjectMeta *>;
88 
92 typedef struct {
94  std::vector<GstNvInferFrame> frames;
96  GstBuffer *inbuf = nullptr;
98  gulong inbuf_batch_num = 0;
103  gboolean push_buffer = FALSE;
107  gboolean event_marker = FALSE;
109  GstBuffer *conv_buf = nullptr;
110  nvtxRangeId_t nvtx_complete_buf_range = 0;
111 
114  std::vector <GstNvInferObjHistory_MetaPair> objs_pending_meta_attach;
116 
117 
127 typedef struct
128 {
132  GstMiniObject mini_object;
134  NvDsInferContextPtr infer_context;
138  NvDsInferContextBatchOutput batch_output;
140 
141 namespace gstnvinfer {
142 
144 struct ModelStatus
145 {
149  std::string cfg_file;
150  /* Error message string if any. */
151  std::string message;
152 };
153 
155 class LockGMutex
156 {
157 public:
158  LockGMutex (GMutex &mutex)
159  :m (mutex) {
160  lock ();
161  }
163  if (locked)
164  unlock();
165  }
166  void lock ();
167  void unlock ();
168  void wait (GCond &cond);
169 
170 private:
171  GMutex &m;
172  bool locked = false;
173 };
174 
177 {
188 };
189 
190 /* Helper class to manage the NvDsInferContext and runtime model update. The
191  * model can be updated at runtime by setting "config-file-path" and/or
192  * "model-engine-file" properties with the new config file/model engine file.
193  *
194  * The runtime update implementation would basically create and initialize a
195  * new NvDsInferContext with new parameters and if successful will replace the
196  * current NvDsInferContext instance with the new instance while taking care of
197  * processing synchronization.
198  *
199  * Constraints of runtime model update:
200  * - Model input resolution and channels should not change
201  * - Batch-size of new model engine should be equal or greater than
202  * gst-nvinfer's batch-size
203  * - Type of the model (Detection/Classification/Segmentation) should not
204  * change.
205  *
206  * Check deepstream-test5-app README for more details on OTA and runtime model
207  * update and sample test steps.*/
208 class DsNvInferImpl
209 {
210 public:
211  using ContextReplacementPtr =
212  std::unique_ptr<std::tuple<NvDsInferContextPtr, NvDsInferContextInitParamsPtr, std::string>>;
213 
214  DsNvInferImpl (GstNvInferBase *infer);
215  ~DsNvInferImpl ();
216  /* Start the model load thread. */
218  /* Stop the model load thread. Release the NvDsInferContext. */
219  void stop ();
220 
221  bool isContextReady () const { return m_InferCtx.get(); }
222 
224  bool triggerNewModel (const std::string &modelPath, ModelLoadType loadType);
225 
228  void notifyLoadModelStatus (const ModelStatus &res);
229 
232 
235 
236 private:
238  class ModelLoadThread
239  {
240  public:
241  using ModelItem = std::tuple<std::string, ModelLoadType> ;
242 
243  ModelLoadThread (DsNvInferImpl &impl);
244  ~ModelLoadThread ();
245  void queueModel (const std::string &modelPath, ModelLoadType type) {
246  m_PendingModels.push (ModelItem(modelPath, type));
247  }
248  private:
249  void Run();
250 
251  DsNvInferImpl &m_Impl;
252  std::thread m_Thread;
254  };
255 
256  bool initNewInferModelParams (
257  NvDsInferContextInitParams &newParams,
258  const std::string &newModelPath, ModelLoadType loadType,
259  const NvDsInferContextInitParams &oldParams);
260  bool isNewContextValid (
261  INvDsInferContext &newCtx, NvDsInferContextInitParams &newParam);
262  bool triggerContextReplace (
264  const std::string &path);
265  void loadModel (const std::string &path, ModelLoadType loadType);
266 
267  ContextReplacementPtr getNextReplacementUnlock ();
268  NvDsInferStatus flushDataUnlock (LockGMutex &lock);
269  NvDsInferStatus resetContextUnlock (
271  const std::string &path);
272 
273  GstNvInferBase *m_GstInfer = nullptr;
275  std::unique_ptr<ModelLoadThread> m_ModelLoadThread;
276  ContextReplacementPtr m_NextContextReplacement;
277 };
278 
279 }
280 
281 #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.2/sources/libs/gst-nvdsinferbase/gstnvinfer_impl.h:158
FALSE
#define FALSE
Definition: sources/libs/nvll_batch_jpeg_encode/oss/jmorecfg.h:247
nvdsinfer::GuardQueue
Definition: sources/libs/nvdsinfer/nvdsinfer_func_utils.h:174
gstnvinfer::LockGMutex::~LockGMutex
~LockGMutex()
Definition: 9.2/sources/libs/gst-nvdsinferbase/gstnvinfer_impl.h:162
_GstNvInferObjectHistory
Holds the inference information/history for one object based on it's tracking id.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer.h:148
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
_NvDsAudioFrameMeta
Holds metadata for a audio frame in a batch.
Definition: includes/nvds_audio_meta.h:50
_GstNvInferBase::unique_id
guint unique_id
Unique ID of the element.
Definition: sources/libs/gst-nvdsinferbase/gstnvinferbase.h:113
ModelStatus
ModelStatus
ModelBin lifecycle (applies to EVERY ModelBin, any role).
Definition: sources/gst-plugins/gst-nvmodelmux/gstnvmodelmux_priv.h:445
gstnvinfer::ModelStatus::status
NvDsInferStatus status
Status of the model update.
Definition: sources/gst-plugins/gst-nvinfer/gstnvinfer_impl.h:158
GstNvInferBase
typedefG_BEGIN_DECLS struct _GstNvInferBase GstNvInferBase
Definition: sources/libs/gst-nvdsinferbase/gstnvinferbase.h:42
_GstNvInferBase
GstNvInferBase element structure.
Definition: sources/libs/gst-nvdsinferbase/gstnvinferbase.h:95
NvDsInferLogLevel
NvDsInferLogLevel
Enum for the log levels of NvDsInferContext.
Definition: 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()
GstBuffer
struct _GstBuffer GstBuffer
Definition: includes/ds3d/common/idatatype.h:24
gstnvinfer::DsNvInferImpl::notifyLoadModelStatus
void notifyLoadModelStatus(const ModelStatus &res)
NvBufSurfaceParams
Hold the information of single buffer in the batch.
Definition: includes/nvbufsurface.h:567
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: 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
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
gst_nvinfer_logger
void gst_nvinfer_logger(NvDsInferContextHandle handle, unsigned int unique_id, NvDsInferLogLevel log_level, const char *log_message, void *user_ctx)
_NvDsInferContextInitParams
Holds the initialization parameters required for the NvDsInferContext interface.
Definition: 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
gstnvinfer::DsNvInferImpl::isContextReady
bool isContextReady() const
Definition: 9.2/sources/libs/gst-nvdsinferbase/gstnvinfer_impl.h:221
NvDsInferContextBatchOutput
Holds the output for all of the frames in a batch (an array of frame), and related buffer information...
Definition: 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: includes/nvdsmeta.h:308
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
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: includes/nvdsinfer.h:231
_NvDsObjectMeta
Holds metadata for an object in the frame.
Definition: includes/nvdsmeta.h:367