NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-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 
26 #ifndef __GST_NVDSVISIONENCODER_H__
27 #define __GST_NVDSVISIONENCODER_H__
28 
29 #include <gst/gst.h>
30 #include <gst/base/gstbasetransform.h>
31 #include <gst/video/video.h>
32 
33 #include "nvbufsurface.h"
34 #include "nvbufsurftransform.h"
35 #include "nvdsmeta.h"
36 #include "gstnvdsmeta.h"
37 #include "nvds_tracker_meta.h"
38 #include "nvds_opticalflow_meta.h"
39 
40 #ifdef ENABLE_INSTRUMENTATION
41 #include <sys/time.h>
42 #endif
43 
44 #define DEFAULT_CACHE_REFRESH_INTERVAL 0
45 #define DEFAULT_CACHE_MAX_SIZE 1000
46 
47 /* OFA-based embedding prediction defaults */
48 #define DEFAULT_OFA_MOTION_THRESHOLD 8.0f
49 #define DEFAULT_OFA_HIGH_MOTION_THRESHOLD 25.0f
50 #define DEFAULT_OFA_PREDICT_ENABLED FALSE
51 #define OFA_MOTION_FEATURE_DIM 6
52 
53 /* DLA two-tier inference defaults */
54 #define DEFAULT_DLA_VALIDATOR_ENABLED FALSE
55 #define DEFAULT_DLA_CORE -1
56 #define DEFAULT_DLA_CHANGE_THRESHOLD 0.3f
57 
62 typedef struct {
63  float *embedding;
64  guint embedding_dim;
65  guint64 last_inferred_frame;
66 
67  /* Per-object motion history for OFA prediction */
68  float last_motion_magnitude;
69  float last_flow_dx;
70  float last_flow_dy;
71  guint64 accumulated_skip_frames;
73 
78 typedef struct {
79  void *trt_engine; /* TensorRT engine handle (ICudaEngine*) */
80  void *trt_context; /* TensorRT execution context (IExecutionContext*) */
81  float *d_input; /* Device input buffer for validator */
82  float *d_output; /* Device output: change probability [0,1] */
83  guint input_w;
84  guint input_h;
85  gboolean is_dla; /* TRUE if running on DLA, FALSE for GPU fallback */
86  gint dla_core;
88 
89 G_BEGIN_DECLS
90 
91 #define GST_TYPE_NVDSVISIONENCODER (gst_nvdsvisionencoder_get_type())
92 #define GST_NVDSVISIONENCODER(obj) \
93  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_NVDSVISIONENCODER,GstNvDsVisionEncoder))
94 #define GST_NVDSVISIONENCODER_CLASS(klass) \
95  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_NVDSVISIONENCODER,GstNvDsVisionEncoderClass))
96 #define GST_IS_NVDSVISIONENCODER(obj) \
97  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_NVDSVISIONENCODER))
98 #define GST_IS_NVDSVISIONENCODER_CLASS(klass) \
99  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_NVDSVISIONENCODER))
100 
103 
107 typedef enum {
111 
115 struct _GstNvDsVisionEncoder {
116  GstBaseTransform base_trans;
117 
118  /* Configuration properties */
119  gboolean enable;
121  gchar *backend_str; // String version for property
122  gchar *triton_url;
123  gchar *triton_model;
124  gchar *model_variant; // For compatibility with Python plugin config
125  gchar *tensorrt_engine;
126  gchar *onnx_model; // Path to ONNX model for engine building
127  guint batch_size; // TensorRT batch size (for engine building/selection)
128  guint min_crop_size;
129  gboolean verbose;
130  guint gpu_id;
131  gchar *device; // For compatibility (cuda/cpu)
132  guint skip_interval; // Frame skip interval: 0=none, 1=every other, 2=every 3rd, etc.
133  gchar *embedding_classes; // Semicolon-separated list of classes to generate embeddings for (NULL = all classes)
134  gboolean query_only; // Init model but skip per-frame inference; REST queries still work
135 
136  /* Runtime state */
137  guint frame_count;
138  gchar **allowed_classes; // Parsed array of allowed class names
139  guint num_allowed_classes; // Number of allowed classes
140  gboolean is_initialized;
141 
142  /* Triton client handle (C++ object wrapped as void*) */
143  void *triton_client;
144 
145  /* Serializes access to triton_client across streaming and REST query threads */
146  GMutex infer_mutex;
147 
148  /* CUDA stream for async operations */
149  void *cuda_stream; // cudaStream_t
150 
151  /* Model info */
152  guint input_width;
153  guint input_height;
154  guint embedding_dim;
155 
156  /* Pre-allocated buffers for preprocessing (like nvinfer SGIE) */
157  guint max_batch_size; // Maximum number of objects per transform call
158  NvBufSurfaceParams *prealloc_surf_list; // Pre-allocated surface list for src_batch
159  NvBufSurfTransformRect *prealloc_src_rect; // Pre-allocated source rectangles
160  NvBufSurfTransformRect *prealloc_dst_rect; // Pre-allocated destination rectangles
161  NvBufSurface *prealloc_output_surface; // Pre-allocated output surface (reused across calls)
162  float *d_preprocessed_pool; // Pre-allocated device tensor (max_batch_size) for GPU path
163  size_t d_preprocessed_pool_bytes; // Allocated size in bytes
164  float *embeddings_buffer; // Pre-allocated host buffer for inference output (max_batch_size * embedding_dim)
165 
166  /* Smart inference: cache embeddings by tracker object_id to avoid redundant inference */
167  gboolean smart_infer;
169  guint cache_max_size;
170  GHashTable *embedding_cache; /* guint64 object_id -> CachedEmbedding* */
171  guint64 global_frame_counter; /* Monotonic frame counter for cache eviction */
172 
173  /*
174  * OFA-based temporal embedding prediction.
175  * Uses hardware optical flow motion vectors from upstream nvof element to predict
176  * whether a cached embedding is still valid, and to transform stale embeddings
177  * using motion-derived features instead of running full inference.
178  */
179  gboolean ofa_predict_enabled;
180  gfloat ofa_motion_threshold; /* Below this: embedding is stable, skip inference */
181  gfloat ofa_high_motion_threshold; /* Above this: force re-inference (drastic change) */
182  guint64 ofa_predictions_total; /* Stats: total motion-predicted embeddings */
183  guint64 ofa_reinfers_total; /* Stats: total motion-triggered re-inferences */
184 
185  /*
186  * DLA two-tier inference.
187  * A lightweight "appearance change detector" runs on DLA (or GPU fallback) to decide
188  * whether the full vision encoder needs to re-run for a tracked object.
189  * Tier 1 (DLA/GPU): small binary classifier: "appearance changed?" on 32x32 crop
190  * Tier 2 (GPU): full vision encoder on cache-miss + validator-flagged objects
191  */
192  gboolean dla_validator_enabled;
193  gchar *dla_validator_engine; /* Path to TensorRT engine for the validator */
194  gint dla_core; /* DLA core index (-1 = GPU fallback) */
195  gfloat dla_change_threshold; /* Probability threshold for "appearance changed" */
197  guint64 dla_validations_total;
198  guint64 dla_changes_detected;
199 
200 #ifdef ENABLE_INSTRUMENTATION
201  /* Performance counters */
202  GstClockTime total_process_time;
203  guint total_objects_processed;
204 
205  /* Periodic performance reporting (every PERF_WINDOW_SEC seconds) */
206  struct timeval perf_window_start;
207  guint perf_window_objects;
208  guint perf_window_batches;
209  gboolean perf_window_started;
210 
211  /* Per-phase cumulative microseconds within current window */
212  guint64 perf_phase_preproc_us; /* NvBufSurfTransform crop+resize */
213  guint64 perf_phase_tensor_us; /* RGB surface -> CHW float tensor */
214  guint64 perf_phase_infer_us; /* TRT inference + D2H memcpy */
215  guint64 perf_phase_post_us; /* Attach embedding metadata */
216 #endif
217 };
218 
223  GstBaseTransformClass parent_class;
224 };
225 
226 GType gst_nvdsvisionencoder_get_type (void);
227 
228 G_END_DECLS
229 
230 #endif /* __GST_NVDSVISIONENCODER_H__ */
_GstNvDsVisionEncoder::ofa_high_motion_threshold
gfloat ofa_high_motion_threshold
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:181
_GstNvDsVisionEncoder::ofa_predict_enabled
gboolean ofa_predict_enabled
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:179
CachedEmbedding
Cached embedding entry for smart inference.
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:62
_GstNvDsVisionEncoder::onnx_model
gchar * onnx_model
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:126
_GstNvDsVisionEncoder::backend_str
gchar * backend_str
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:121
_GstNvDsVisionEncoder::prealloc_surf_list
NvBufSurfaceParams * prealloc_surf_list
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:158
_GstNvDsVisionEncoder::global_frame_counter
guint64 global_frame_counter
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:171
_GstNvDsVisionEncoder::dla_ctx
DlaValidatorCtx * dla_ctx
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:196
gst_nvdsvisionencoder_get_type
GType gst_nvdsvisionencoder_get_type(void)
_GstNvDsVisionEncoder::model_variant
gchar * model_variant
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:124
NvBufSurface
Holds information about batched buffers.
Definition: sources/includes/nvbufsurface.h:597
NVDS_VISION_ENCODER_BACKEND_TENSORRT
@ NVDS_VISION_ENCODER_BACKEND_TENSORRT
Definition: 9.1/sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:109
_GstNvDsVisionEncoder::smart_infer
gboolean smart_infer
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:167
_GstNvDsVisionEncoder::infer_mutex
GMutex infer_mutex
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:146
NvDsVisionEncoderBackend
NvDsVisionEncoderBackend
Encoder backend type.
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:107
_GstNvDsVisionEncoder::triton_url
gchar * triton_url
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:122
_GstNvDsVisionEncoder::is_initialized
gboolean is_initialized
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:140
_GstNvDsVisionEncoder::dla_validations_total
guint64 dla_validations_total
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:197
NvBufSurfTransformRect
Holds the coordinates of a rectangle.
Definition: sources/includes/nvbufsurftransform.h:158
_GstNvDsVisionEncoder::input_width
guint input_width
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:152
_GstNvDsVisionEncoder::backend
NvDsVisionEncoderBackend backend
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:120
_GstNvDsVisionEncoder
GstNvDsVisionEncoder structure.
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:115
_GstNvDsVisionEncoder::max_batch_size
guint max_batch_size
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:157
_GstNvDsVisionEncoder::enable
gboolean enable
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:119
_GstNvDsVisionEncoder::device
gchar * device
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:131
_GstNvDsVisionEncoder::cache_max_size
guint cache_max_size
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:169
_GstNvDsVisionEncoder::embedding_cache
GHashTable * embedding_cache
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:170
_GstNvDsVisionEncoder::d_preprocessed_pool_bytes
size_t d_preprocessed_pool_bytes
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:163
_GstNvDsVisionEncoder::allowed_classes
gchar ** allowed_classes
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:138
NvBufSurfaceParams
Hold the information of single buffer in the batch.
Definition: sources/includes/nvbufsurface.h:562
_GstNvDsVisionEncoder::triton_client
void * triton_client
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:143
_GstNvDsVisionEncoder::gpu_id
guint gpu_id
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:130
_GstNvDsVisionEncoder::dla_change_threshold
gfloat dla_change_threshold
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:195
NVDS_VISION_ENCODER_BACKEND_TRITON
@ NVDS_VISION_ENCODER_BACKEND_TRITON
Definition: 9.1/sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:108
DlaValidatorCtx
Lightweight appearance change detector state.
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:78
_GstNvDsVisionEncoder::input_height
guint input_height
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:153
_GstNvDsVisionEncoderClass::parent_class
GstBaseTransformClass parent_class
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:223
_GstNvDsVisionEncoder::batch_size
guint batch_size
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:127
_GstNvDsVisionEncoder::cache_refresh_interval
guint cache_refresh_interval
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:168
_GstNvDsVisionEncoder::d_preprocessed_pool
float * d_preprocessed_pool
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:162
_GstNvDsVisionEncoder::num_allowed_classes
guint num_allowed_classes
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:139
_GstNvDsVisionEncoder::dla_core
gint dla_core
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:194
_GstNvDsVisionEncoder::embeddings_buffer
float * embeddings_buffer
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:164
_GstNvDsVisionEncoder::dla_validator_engine
gchar * dla_validator_engine
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:193
_GstNvDsVisionEncoder::verbose
gboolean verbose
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:129
_GstNvDsVisionEncoder::ofa_reinfers_total
guint64 ofa_reinfers_total
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:183
_GstNvDsVisionEncoder::prealloc_output_surface
NvBufSurface * prealloc_output_surface
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:161
_GstNvDsVisionEncoder::query_only
gboolean query_only
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:134
_GstNvDsVisionEncoder::skip_interval
guint skip_interval
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:132
_GstNvDsVisionEncoder::embedding_dim
guint embedding_dim
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:154
_GstNvDsVisionEncoder::dla_changes_detected
guint64 dla_changes_detected
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:198
_GstNvDsVisionEncoder::cuda_stream
void * cuda_stream
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:149
_GstNvDsVisionEncoder::frame_count
guint frame_count
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:137
_GstNvDsVisionEncoder::base_trans
GstBaseTransform base_trans
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:116
_GstNvDsVisionEncoder::embedding_classes
gchar * embedding_classes
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:133
_GstNvDsVisionEncoder::prealloc_src_rect
NvBufSurfTransformRect * prealloc_src_rect
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:159
_GstNvDsVisionEncoder::triton_model
gchar * triton_model
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:123
_GstNvDsVisionEncoder::min_crop_size
guint min_crop_size
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:128
_GstNvDsVisionEncoder::dla_validator_enabled
gboolean dla_validator_enabled
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:192
_GstNvDsVisionEncoderClass
GstNvDsVisionEncoderClass structure.
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:222
_GstNvDsVisionEncoder::ofa_predictions_total
guint64 ofa_predictions_total
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:182
NvDsVisionEncoderBackend
NvDsVisionEncoderBackend
Encoder backend type.
Definition: 9.1/sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:107
_GstNvDsVisionEncoder::prealloc_dst_rect
NvBufSurfTransformRect * prealloc_dst_rect
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:160
_GstNvDsVisionEncoder::tensorrt_engine
gchar * tensorrt_engine
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:125
_GstNvDsVisionEncoder::ofa_motion_threshold
gfloat ofa_motion_threshold
Definition: sources/gst-plugins/gst-nvdsvisionencoder-c/gstnvdsvisionencoder.h:180