NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-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 __NVSTREAMMUX_BATCH__H
19 #define __NVSTREAMMUX_BATCH__H
20 
21 
22 #include <iostream>
23 #include <ctime>
24 #include <ratio>
25 #include <chrono>
26 #include <unordered_map>
27 #include <map>
28 #include <list>
29 #include "nvbufsurface.h"
30 #include <string.h>
31 #include "nvstreammux_pads.h"
32 #include <nvdsmeta.h>
33 #if 0
34 #ifdef NEW_METADATA
35 #include "gstnvdsmeta.h"
36 #else
37 #include "gstnvstreammeta.h"
38 #include "gstnvdsmeta_int.h"
39 #endif
40 #endif
41 typedef std::chrono::steady_clock Clock;
42 using TimePoint = std::chrono::time_point<std::chrono::steady_clock, NanoSecondsType>;
43 
44 typedef struct
45 {
46  unsigned int source_max_fps_n;
47  unsigned int source_max_fps_d;
48  unsigned int source_min_fps_n;
49  unsigned int source_min_fps_d;
50  unsigned int priority;
51  unsigned int max_num_frames_per_batch;
53 
54 typedef enum
55 {
60 
61 typedef struct BatchPolicyConfig
62 {
64  bool adaptive_batching;
65  unsigned int batch_size;
66  unsigned int overall_max_fps_n;
67  unsigned int overall_max_fps_d;
68  unsigned int overall_min_fps_n;
69  unsigned int overall_min_fps_d;
70  unsigned int max_same_source_frames;
75  std::unordered_map< unsigned int, NvStreammuxSourceProps> source_props;
76 
78  {
79  batch_size = 0;
80  }
81 
83 
84 typedef struct
85 {
86  int priority_list_position;
87  int source_map_position;
89 
90 //class BatchBufferWrapper : public BufferWrapper
92 {
93  public :
94  //BatchBufferWrapper(void * b) : batch(b)
96  {
97 
98 
99  }
100  virtual ~BatchBufferWrapper() = default;
101  virtual unsigned int copy(void * buf, unsigned int pos, unsigned int num_surfaces)
102  {
103  return 0;
104  }
105  virtual bool push(SourcePad * pad, TimePoint play_start, NanoSecondsType accum_dur){ return false; };
106  virtual bool push(SourcePad * pad, unsigned long pts){ return false; };
107  virtual void unref(){};
108  void * batch;
109 // unsigned int num_filled;
110 
111 };
112 
114 {
115  public :
116  NvDsBatchBufferWrapper(unsigned int size);
117  virtual ~NvDsBatchBufferWrapper();
118 
125  virtual unsigned int copy_buf(std::shared_ptr<BufferWrapper> buf, unsigned int pos) = 0;
126 
127  virtual void copy_meta(unsigned int id, std::shared_ptr<BufferWrapper> src_buffer, unsigned int batch_id, unsigned int frame_number, unsigned int num_surfaces_per_frame, NvDsBatchMeta * dest_batch_meta, unsigned int source_id){};
128  virtual void unref(){};
129  unsigned int batch_size;
130 
131 };
132 
133 class Batch
134 {
135 
136 
137  public :
138 
139  Batch(unsigned int size)
140  {
141  acc_batch = 0;
142  batch_size = size;
143  }
144  //void form_batch();
145 
146  void check_source(unsigned int source_id);
147 
148  void reset_batch();
149 
150  void set_size(unsigned int size);
151 
152 
153 
154  /*
155  * @brief map of number of sources for sources in batch
156  * key=source_id
157  * value=number of surfaces to copy from this source
158  */
159  std::unordered_map<int, int> num_sources;
160 
164  unsigned int acc_batch;
167  unsigned int batch_size;
168  unsigned int num_surfaces_in_batch;
169 
170 
171 };
172 
173 
174 class SortedList
175 {
176  public :
177  void sorted_insert(unsigned int);
178  std::list<unsigned int>::iterator get_next_pos(std::list<unsigned int>::iterator pos);
179  std::list<unsigned int>::iterator get_max_pos();
180  unsigned int get_least();
181  std::list<unsigned int>::iterator get_least_pos();
182  unsigned int get_at(std::list<unsigned int>::iterator pos);
183  int size();
184  private :
185  std::list<unsigned int> store;
186 };
187 
188 class BatchPolicy
189 {
190  public :
192  BatchPolicy(BatchPolicyConfig policy, std::unordered_map<unsigned int, SinkPad *> * ins, INvStreammuxDebug* a_debug_iface);
193 
194  unsigned int check_repeats_per_batch();
195 
196  unsigned int check_repeats_per_batch(unsigned int source_id);
197 
198  /*
199  * @brief function to try to form a batch per current priorities
200  * and set algorithm
201  * @param b [IN] the batch to update
202  */
203  Batch * form_batch( Batch * b, unsigned int batch_size);
204 
205  unsigned int get_batch_size();
206 
207  unsigned int get_config_batch_size();
208 
209  void set_batch_size(unsigned int);
210 
211  void set_batch_push_timeout(unsigned int);
215  void set_num_surfaces(unsigned int);
216 
217  /*
218  * @brief function to update a batch with buffers from a source
219  * If Timestamp synchronization user API is set,
220  * only on-time buffers will be batched.
221  * late buffers will be discarded.
222  * early buffers will not be batched with the current call.
223  * @param source_id [IN] the id of pad
224  * @param batch [IN] the batch to update
225  */
226  void update_with_source(Batch * batch, unsigned int source_id);
227 
228  /*
229  * @brief function to check amount of time to wait for batch data
230  * based on current time and min frame rate based duration
231  */
233 
234  //NanoSecondsType update_last_batch_time(NanoSecondsType last_batch_time);
235  void update_last_batch_time();
236 
237  /*
238  * @brief function to get delay for next batch
239  * based on max frame rate based duration
240  */
242 
243  /*
244  * @brief function to calculate delay for next batch
245  * based on current time and max frame rate based duration
246  */
248 
249  /*
250  * @brief function to check if a batch is ready to be pushed
251  * @param b [IN] the batch object pointer
252  */
253  bool is_ready(Batch * b);
254 
255  bool is_ready_or_due(Batch * b);
256 
257  /*
258  * @brief function to check if defaults exist for this source id
259  * from config, (TBD) this mechanism needs to replaced with something
260  * that is at initialization time
261  * @param source_id [IN] the id of pad
262  */
263  void check_and_update_defaults(unsigned int source_id);
264 
265  //void reset_batch(Batch * b);
266  unsigned int total_buf_available;
267 
268  bool check_past_min();
269 
270  void update_idle_sources(unsigned int idle_count);
271 
272  void update_eos_sources(unsigned int eos_count);
273 
274  unsigned int get_eos_sources();
275 
276  void update_push_stats(unsigned int source_id, unsigned int num_pushed);
277 
279 
280  void set_synch_buffer_iface(ISynchronizeBuffer* synch_buffer_iface);
281 
286  unsigned int synchronize_buffers_in_pad(SinkPad* pad,
287  unsigned int allowed_buffers);
288 
290 
292 
293  private :
294  unsigned int get_allowed(unsigned int source_id, float fps, unsigned int available);
295 
296  BatchPolicyConfig config;
297  NanoSecondsType max_fps_dur;
298  NanoSecondsType min_fps_dur;
300  NanoSecondsType min_early_buffer_dur;
301  TimePoint max_dur_time;
302  TimePoint min_dur_time;
303  std::unordered_map<unsigned int, float> src_max_fps;
304  std::unordered_map<unsigned int, NanoSecondsType> min_src_fps_dur;
305  std::unordered_map<unsigned int, TimePoint> src_push_times;
306  std::unordered_map<unsigned int, unsigned int> src_num_pushed;
307  SortedList priority_list;
308  /*
309  * @brief map of sources for a priority
310  * key = priority
311  * value = source_id
312  */
313  std::multimap<int, int> sources;
314  std::unordered_map<unsigned int, SinkPad *> * inputs;
315  std::chrono::time_point<std::chrono::steady_clock> last_batch_time;
316  unsigned int batch_size;
317  LastBatchState last_batch_state;
325  unsigned int num_sources_idle;
330  unsigned int num_sources_eos;
331  unsigned int num_surfaces_per_frame;
332  ISynchronizeBuffer* synch_buffer;
333  INvStreammuxDebug* debug_iface;
334 
335 
336 };
337 
338 
339 #endif
Batch::set_size
void set_size(unsigned int size)
SortedList::sorted_insert
void sorted_insert(unsigned int)
BatchBufferWrapper::push
virtual bool push(SourcePad *pad, unsigned long pts)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:106
NvDsBatchBufferWrapper::batch_size
unsigned int batch_size
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:128
BatchPolicy::is_ready
bool is_ready(Batch *b)
BatchPolicy::update_last_batch_time
void update_last_batch_time()
NvDsBatchBufferWrapper::copy_meta
virtual void copy_meta(unsigned int id, std::shared_ptr< BufferWrapper > src_buffer, unsigned int batch_id, unsigned int frame_number, unsigned int num_surfaces_per_frame, NvDsBatchMeta *dest_batch_meta, unsigned int source_id)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:127
BATCH_METHOD_NONE
@ BATCH_METHOD_NONE
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:56
NvDsBatchBufferWrapper
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:113
BatchPolicy::set_batch_size
void set_batch_size(unsigned int)
Batch::Batch
Batch(unsigned int size)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:139
BatchPolicy::total_buf_available
unsigned int total_buf_available
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:266
NvDsBatchBufferWrapper::copy_buf
virtual unsigned int copy_buf(std::shared_ptr< BufferWrapper > buf, unsigned int pos)=0
Copy input buffer (buf) to this NvDsBatchBufferWrapper.
BatchPolicy::check_repeats_per_batch
unsigned int check_repeats_per_batch()
SortedList::get_least_pos
std::list< unsigned int >::iterator get_least_pos()
BatchPolicy::is_ready_or_due
bool is_ready_or_due(Batch *b)
NvStreammuxBatchMethod
NvStreammuxBatchMethod
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:54
BatchPolicyConfig::batch_size
unsigned int batch_size
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:65
TimePoint
std::chrono::time_point< std::chrono::steady_clock, NanoSecondsType > TimePoint
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:42
SortedList::get_least
unsigned int get_least()
SinkPad
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:172
BatchPolicy::get_config_batch_size
unsigned int get_config_batch_size()
BatchPolicyConfig::BatchPolicyConfig
BatchPolicyConfig()
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:77
BatchPolicyConfig::enable_max_fps_control
bool enable_max_fps_control
enables or disables the throttling control in push_loop() implementing max_fps configuration support
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:74
Batch
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:133
BatchPolicyConfig::overall_max_fps_n
unsigned int overall_max_fps_n
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:66
NvDsBatchBufferWrapper::~NvDsBatchBufferWrapper
virtual ~NvDsBatchBufferWrapper()
SortedList::get_next_pos
std::list< unsigned int >::iterator get_next_pos(std::list< unsigned int >::iterator pos)
SourcePad
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:145
BatchPolicyConfig::overall_min_fps_d
unsigned int overall_min_fps_d
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:69
BatchPolicy::update_eos_sources
void update_eos_sources(unsigned int eos_count)
BatchPolicy::update_push_stats
void update_push_stats(unsigned int source_id, unsigned int num_pushed)
BatchPolicyConfig
struct BatchPolicyConfig BatchPolicyConfig
NvDsBatchBufferWrapper::unref
virtual void unref()
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:128
BatchBufferWrapper::copy
virtual unsigned int copy(void *buf, unsigned int pos, unsigned int num_surfaces)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:101
BatchBufferWrapper::push
virtual bool push(SourcePad *pad, TimePoint play_start, NanoSecondsType accum_dur)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:105
BatchBufferWrapper::BatchBufferWrapper
BatchBufferWrapper()
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:95
Clock
std::chrono::steady_clock Clock
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:41
BATCH_METHOD_ROUND_ROBIN
@ BATCH_METHOD_ROUND_ROBIN
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:57
BatchBufferWrapper::unref
virtual void unref()
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:107
Batch::num_sources
std::unordered_map< int, int > num_sources
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:159
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: sources/includes/nvdsmeta.h:262
BatchPolicyConfig::adaptive_batching
bool adaptive_batching
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:64
BatchPolicy::get_batch_size
unsigned int get_batch_size()
SortedList::get_at
unsigned int get_at(std::list< unsigned int >::iterator pos)
SortedList
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:174
BatchBufferWrapper
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:91
BatchPolicy::synchronize_buffers_in_pad
unsigned int synchronize_buffers_in_pad(SinkPad *pad, unsigned int allowed_buffers)
Synchronize the buffers in queue for provided pad.
BatchBufferWrapper::~BatchBufferWrapper
virtual ~BatchBufferWrapper()=default
BatchPolicyConfig::source_props
std::unordered_map< unsigned int, NvStreammuxSourceProps > source_props
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:75
Batch::batch_size
unsigned int batch_size
number of surfaces in the batch this will be one NvDsFrameMeta (one frame) in the batch meta
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:167
BatchPolicy::check_past_min
bool check_past_min()
BatchPolicy::update_with_source
void update_with_source(Batch *batch, unsigned int source_id)
BatchPolicy::calculate_wait
NanoSecondsType calculate_wait()
BatchPolicy::set_batch_push_timeout
void set_batch_push_timeout(unsigned int)
NanoSecondsType
std::chrono::duration< double, std::nano > NanoSecondsType
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:27
BatchPolicyConfig::max_same_source_frames
unsigned int max_same_source_frames
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:70
BatchPolicy::get_config
BatchPolicyConfig get_config()
BatchPolicy::form_batch
Batch * form_batch(Batch *b, unsigned int batch_size)
NvStreammuxSourceProps
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:44
Batch::reset_batch
void reset_batch()
BatchPolicyConfig::type
NvStreammuxBatchMethod type
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:63
BatchPolicy::get_max_duration_delay
NanoSecondsType get_max_duration_delay()
BatchPolicy::set_num_surfaces
void set_num_surfaces(unsigned int)
Set num_surfaces_per_frame.
Batch::check_source
void check_source(unsigned int source_id)
BatchPolicy::set_synch_buffer_iface
void set_synch_buffer_iface(ISynchronizeBuffer *synch_buffer_iface)
BatchPolicy::update_idle_sources
void update_idle_sources(unsigned int idle_count)
BatchPolicy::is_max_fps_control_enabled
bool is_max_fps_control_enabled()
BatchPolicy::get_eos_sources
unsigned int get_eos_sources()
BatchPolicyConfig::overall_min_fps_n
unsigned int overall_min_fps_n
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:68
LastBatchState
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:84
Batch::num_surfaces_in_batch
unsigned int num_surfaces_in_batch
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:168
NvStreammuxBatchMethod
NvStreammuxBatchMethod
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:54
BatchPolicy::get_min_fps_duration
NanoSecondsType get_min_fps_duration()
BatchPolicyConfig::overall_max_fps_d
unsigned int overall_max_fps_d
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:67
ISynchronizeBuffer
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:347
NvDsBatchBufferWrapper::NvDsBatchBufferWrapper
NvDsBatchBufferWrapper(unsigned int size)
SortedList::get_max_pos
std::list< unsigned int >::iterator get_max_pos()
BatchPolicy::calculate_delay
NanoSecondsType calculate_delay()
BatchPolicy::check_and_update_defaults
void check_and_update_defaults(unsigned int source_id)
Batch::acc_batch
unsigned int acc_batch
number of buffers already accumulated in this batch Still not copied; yet info available in @num_sour...
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:164
BatchBufferWrapper::batch
void * batch
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:107
BatchPolicy
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:188
BatchPolicyConfig
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:61
BatchPolicy::BatchPolicy
BatchPolicy()
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:191
BATCH_METHOD_PRIORITY
@ BATCH_METHOD_PRIORITY
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux_batch.h:58
nvstreammux_pads.h
INvStreammuxDebug
Definition: sources/libs/nvstreammux/include/nvstreammux_debug.h:21
SortedList::size
int size()
BatchPolicyConfig::enable_source_rate_control
bool enable_source_rate_control
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:71