NVIDIA DeepStream SDK API Reference

7.0 Release
gstnvinfer_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2020 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #ifndef __GSTNVINFER_IMPL_H__
14 #define __GSTNVINFER_IMPL_H__
15 
16 #include <string.h>
17 #include <sys/time.h>
18 #include <glib.h>
19 #include <gst/gst.h>
20 
21 #include <vector>
22 #include <list>
23 #include <condition_variable>
24 #include <memory>
25 #include <mutex>
26 #include <thread>
27 
28 #include "nvbufsurftransform.h"
29 #include "nvdsinfer_context.h"
30 #include "nvdsinfer_func_utils.h"
31 #include "nvdsmeta.h"
32 #include "nvdspreprocess_meta.h"
33 #include "nvtx3/nvToolsExt.h"
34 
35 G_BEGIN_DECLS
36 typedef struct _GstNvInfer GstNvInfer;
37 
38 void gst_nvinfer_logger(NvDsInferContextHandle handle, unsigned int unique_id,
39  NvDsInferLogLevel log_level, const char* log_message, void* user_ctx);
40 
41 G_END_DECLS
42 
43 using NvDsInferContextInitParamsPtr = std::unique_ptr<NvDsInferContextInitParams>;
44 using NvDsInferContextPtr = std::shared_ptr<INvDsInferContext>;
45 
47 
51 typedef struct {
55  gdouble scale_ratio_x = 0;
59  gdouble scale_ratio_y = 0;
62  guint offset_left = 0;
63  guint offset_top = 0;
65  guint roi_left = 0;
66  guint roi_top = 0;
68  NvDsObjectMeta *obj_meta = nullptr;
69  NvDsFrameMeta *frame_meta = nullptr;
70  NvDsRoiMeta *roi_meta = nullptr;
73  guint batch_index = 0;
75  gulong frame_num = 0;
77  NvBufSurfaceParams *input_surf_params = nullptr;
81  gpointer converted_frame_ptr = nullptr;
84  std::weak_ptr<GstNvInferObjectHistory> history;
85 
87 
89  std::pair<std::weak_ptr<GstNvInferObjectHistory>, NvDsObjectMeta *>;
90 
94 typedef struct {
96  std::vector<GstNvInferFrame> frames;
98  GstBuffer *inbuf = nullptr;
100  gulong inbuf_batch_num = 0;
105  gboolean push_buffer = FALSE;
109  gboolean event_marker = FALSE;
111  GstBuffer *conv_buf = nullptr;
112  nvtxRangeId_t nvtx_complete_buf_range = 0;
113 
117 
120  std::vector <GstNvInferObjHistory_MetaPair> objs_pending_meta_attach;
122 
123 
133 typedef struct
134 {
138  GstMiniObject mini_object;
146 
147 namespace gstnvinfer {
148 
151 {
155  std::string cfg_file;
156  /* Error message string if any. */
157  std::string message;
158 };
159 
162 {
163 public:
164  LockGMutex (GMutex &mutex)
165  :m (mutex) {
166  lock ();
167  }
169  if (locked)
170  unlock();
171  }
172  void lock ();
173  void unlock ();
174  void wait (GCond &cond);
175 
176 private:
177  GMutex &m;
178  bool locked = false;
179 };
180 
183 {
194 };
195 
196 /* Helper class to manage the NvDsInferContext and runtime model update. The
197  * model can be updated at runtime by setting "config-file-path" and/or
198  * "model-engine-file" properties with the new config file/model engine file.
199  *
200  * The runtime update implementation would basically create and initialize a
201  * new NvDsInferContext with new parameters and if successful will replace the
202  * current NvDsInferContext instance with the new instance while taking care of
203  * processing synchronization.
204  *
205  * Constraints of runtime model update:
206  * - Model input resolution and channels should not change
207  * - Batch-size of new model engine should be equal or greater than
208  * gst-nvinfer's batch-size
209  * - Type of the model (Detection/Classification/Segmentation) should not
210  * change.
211  *
212  * Check deepstream-test5-app README for more details on OTA and runtime model
213  * update and sample test steps.*/
215 {
216 public:
217  using ContextReplacementPtr =
218  std::unique_ptr<std::tuple<NvDsInferContextPtr, NvDsInferContextInitParamsPtr, std::string>>;
219 
220  DsNvInferImpl (GstNvInfer *infer);
221  ~DsNvInferImpl ();
222  /* Start the model load thread. */
224  /* Stop the model load thread. Release the NvDsInferContext. */
225  void stop ();
226 
227  bool isContextReady () const { return m_InferCtx.get(); }
228 
230  bool triggerNewModel (const std::string &modelPath, ModelLoadType loadType);
231 
234  void notifyLoadModelStatus (const ModelStatus &res);
235 
238 
241 
242 private:
244  class ModelLoadThread
245  {
246  public:
247  using ModelItem = std::tuple<std::string, ModelLoadType> ;
248 
249  ModelLoadThread (DsNvInferImpl &impl);
250  ~ModelLoadThread ();
251  void queueModel (const std::string &modelPath, ModelLoadType type) {
252  m_PendingModels.push (ModelItem(modelPath, type));
253  }
254  private:
255  void Run();
256 
257  DsNvInferImpl &m_Impl;
258  std::thread m_Thread;
260  };
261 
262  bool initNewInferModelParams (
263  NvDsInferContextInitParams &newParams,
264  const std::string &newModelPath, ModelLoadType loadType,
265  const NvDsInferContextInitParams &oldParams);
266  bool isNewContextValid (
267  INvDsInferContext &newCtx, NvDsInferContextInitParams &newParam);
268  bool triggerContextReplace (
270  const std::string &path);
271  void loadModel (const std::string &path, ModelLoadType loadType);
272 
273  ContextReplacementPtr getNextReplacementUnlock ();
274  NvDsInferStatus flushDataUnlock (LockGMutex &lock);
275  NvDsInferStatus resetContextUnlock (
277  const std::string &path);
278 
279  GstNvInfer *m_GstInfer = nullptr;
281  std::unique_ptr<ModelLoadThread> m_ModelLoadThread;
282  ContextReplacementPtr m_NextContextReplacement;
283 };
284 
285 }
286 
287 #endif
gstnvinfer::DsNvInferImpl::m_InitParams
NvDsInferContextInitParamsPtr m_InitParams
NvDsInferContext initialization params.
Definition: gstnvinfer_impl.h:240
gstnvinfer::LockGMutex::LockGMutex
LockGMutex(GMutex &mutex)
Definition: gstnvinfer_impl.h:164
nvdsinfer::GuardQueue
Definition: nvdsinfer_func_utils.h:169
gstnvinfer::LockGMutex::~LockGMutex
~LockGMutex()
Definition: gstnvinfer_impl.h:168
_GstNvInferObjectHistory
Holds the inference information/history for one object based on it's tracking id.
Definition: gstnvinfer.h:141
NvBufSurfTransformSyncObj_t
struct NvBufSurfTransformSyncObj * NvBufSurfTransformSyncObj_t
Holds the information about synchronization objects for asynchronous transform/composite APIs.
Definition: nvbufsurftransform.h:287
gstnvinfer
Definition: gstnvinfer_impl.h:147
GstNvInferFrame::history
std::weak_ptr< GstNvInferObjectHistory > history
Pointer to the structure holding inference history for the object.
Definition: gstnvinfer_impl.h:84
gst_nvinfer_logger
void gst_nvinfer_logger(NvDsInferContextHandle handle, unsigned int unique_id, NvDsInferLogLevel log_level, const char *log_message, void *user_ctx)
gstnvinfer::DsNvInferImpl::ensureReplaceNextContext
NvDsInferStatus ensureReplaceNextContext()
replace context, action in submit_input_buffer
GstNvInfer
typedefG_BEGIN_DECLS struct _GstNvInfer GstNvInfer
Definition: gstnvinfer.h:45
gstnvinfer::LockGMutex::lock
void lock()
gstnvinfer::ModelLoadType
ModelLoadType
Enum for type of model update required.
Definition: gstnvinfer_impl.h:182
GstNvInferObjHistory_MetaPair
std::pair< std::weak_ptr< GstNvInferObjectHistory >, NvDsObjectMeta * > GstNvInferObjHistory_MetaPair
Definition: gstnvinfer_impl.h:89
NvDsInferContextPtr
std::shared_ptr< INvDsInferContext > NvDsInferContextPtr
Definition: gstnvinfer_impl.h:44
NvDsInferContextInitParamsPtr
std::unique_ptr< NvDsInferContextInitParams > NvDsInferContextInitParamsPtr
Definition: gstnvinfer_impl.h:43
gstnvinfer::ModelStatus::status
NvDsInferStatus status
Status of the model update.
Definition: gstnvinfer_impl.h:153
NvDsInferLogLevel
NvDsInferLogLevel
Enum for the log levels of NvDsInferContext.
Definition: nvdsinfer.h:249
gstnvinfer::DsNvInferImpl
Definition: gstnvinfer_impl.h:214
gstnvinfer::DsNvInferImpl::stop
void stop()
gstnvinfer::DsNvInferImpl::m_InferCtx
NvDsInferContextPtr m_InferCtx
NvDsInferContext to be used for inferencing.
Definition: gstnvinfer_impl.h:237
gstnvinfer::LockGMutex::unlock
void unlock()
gstnvinfer::DsNvInferImpl::notifyLoadModelStatus
void notifyLoadModelStatus(const ModelStatus &res)
NvBufSurfaceParams
Hold the information of single buffer in the batch.
Definition: nvbufsurface.h:476
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()
GstNvInferTensorOutputObject::mini_object
GstMiniObject mini_object
Parent type.
Definition: gstnvinfer_impl.h:138
GstNvInferBatch::objs_pending_meta_attach
std::vector< GstNvInferObjHistory_MetaPair > objs_pending_meta_attach
List of objects not inferred on in the current batch but pending attachment of lastest available clas...
Definition: gstnvinfer_impl.h:120
nvdsinfer_context.h
NvDsInferContextHandle
struct INvDsInferContext * NvDsInferContextHandle
An opaque pointer type to be used as a handle for a context instance.
Definition: nvdsinfer_context.h:629
GstNvInferTensorOutputObject::infer_context
NvDsInferContextPtr infer_context
NvDsInferContext pointer which hold the resource.
Definition: gstnvinfer_impl.h:140
GstNvInferBatch
Holds information about the batch of frames to be inferred.
Definition: gstnvinfer_impl.h:94
gstnvinfer::ModelStatus::message
std::string message
Definition: gstnvinfer_impl.h:157
_GstNvInfer::unique_id
guint unique_id
Unique ID of the element.
Definition: gstnvinfer.h:194
GstNvInferTensorOutputObject
Data type used for the refcounting and managing the usage of NvDsInferContext's batch output and the ...
Definition: gstnvinfer_impl.h:133
gstnvinfer::ModelStatus::cfg_file
std::string cfg_file
Config file used for model update.
Definition: gstnvinfer_impl.h:155
_NvDsInferContextInitParams
Holds the initialization parameters required for the NvDsInferContext interface.
Definition: nvdsinfer_context.h:239
gstnvinfer::DsNvInferImpl::start
NvDsInferStatus start()
NvDsRoiMeta
Holds Information about ROI Metadata.
Definition: nvds_roi_meta.h:86
GstBuffer
struct _GstBuffer GstBuffer
Definition: idatatype.h:19
gstnvinfer::DsNvInferImpl::isContextReady
bool isContextReady() const
Definition: gstnvinfer_impl.h:227
GstNvInferTensorOutputObject::batch_output
NvDsInferContextBatchOutput batch_output
NvDsInferContextBatchOutput instance whose output tensor buffers are being sent as meta data.
Definition: gstnvinfer_impl.h:144
GstNvInferBatch::frames
std::vector< GstNvInferFrame > frames
Vector of frames in the batch.
Definition: gstnvinfer_impl.h:96
gstnvinfer::MODEL_LOAD_STOP
@ MODEL_LOAD_STOP
Request the model load thread to stop.
Definition: gstnvinfer_impl.h:193
NvDsInferContextBatchOutput
Holds the output for all of the frames in a batch (an array of frame), and related buffer information...
Definition: nvdsinfer_context.h:605
_NvDsFrameMeta
Holds metadata for a frame in a batch.
Definition: nvdsmeta.h:285
_GstNvInfer
GstNvInfer element structure.
Definition: gstnvinfer.h:176
nvdsmeta.h
nvbufsurftransform.h
GstNvInferFrame
Holds info about one frame in a batch for inferencing.
Definition: gstnvinfer_impl.h:51
gstnvinfer::LockGMutex::wait
void wait(GCond &cond)
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: gstnvinfer_impl.h:186
gstnvinfer::ModelStatus
Holds runtime model update status along with the error message if any.
Definition: gstnvinfer_impl.h:150
gstnvinfer::LockGMutex
C++ helper class written on top of GMutex/GCond.
Definition: gstnvinfer_impl.h:161
gstnvinfer::DsNvInferImpl::ContextReplacementPtr
std::unique_ptr< std::tuple< NvDsInferContextPtr, NvDsInferContextInitParamsPtr, std::string > > ContextReplacementPtr
Definition: gstnvinfer_impl.h:218
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: nvdsinfer.h:218
nvdsinfer_func_utils.h
gstnvinfer::MODEL_LOAD_FROM_CONFIG
@ MODEL_LOAD_FROM_CONFIG
Load a new model with other configuration changes.
Definition: gstnvinfer_impl.h:191
_NvDsObjectMeta
Holds metadata for an object in the frame.
Definition: nvdsmeta.h:343