NVIDIA DeepStream SDK API Reference

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