NVIDIA DeepStream SDK API Reference

6.4 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: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef __NVSTREAMMUX_BATCH__H
25 #define __NVSTREAMMUX_BATCH__H
26 
27 
28 #include <iostream>
29 #include <ctime>
30 #include <ratio>
31 #include <chrono>
32 #include <unordered_map>
33 #include <map>
34 #include <list>
35 #include "nvbufsurface.h"
36 #include <string.h>
37 #include "nvstreammux_pads.h"
38 #include <nvdsmeta.h>
39 #if 0
40 #ifdef NEW_METADATA
41 #include "gstnvdsmeta.h"
42 #else
43 #include "gstnvstreammeta.h"
44 #include "gstnvdsmeta_int.h"
45 #endif
46 #endif
47 typedef std::chrono::steady_clock Clock;
48 using TimePoint = std::chrono::time_point<std::chrono::steady_clock, NanoSecondsType>;
49 
50 typedef struct
51 {
52  unsigned int source_max_fps_n;
53  unsigned int source_max_fps_d;
54  unsigned int source_min_fps_n;
55  unsigned int source_min_fps_d;
56  unsigned int priority;
59 
60 typedef enum
61 {
66 
67 typedef struct BatchPolicyConfig
68 {
71  unsigned int batch_size;
72  unsigned int overall_max_fps_n;
73  unsigned int overall_max_fps_d;
74  unsigned int overall_min_fps_n;
75  unsigned int overall_min_fps_d;
76  unsigned int max_same_source_frames;
81  std::unordered_map< unsigned int, NvStreammuxSourceProps> source_props;
82 
84  {
85  batch_size = 0;
86  }
87 
89 
90 typedef struct
91 {
95 
96 //class BatchBufferWrapper : public BufferWrapper
98 {
99  public :
100  //BatchBufferWrapper(void * b) : batch(b)
102  {
103 
104 
105  }
106  virtual ~BatchBufferWrapper() = default;
107  virtual unsigned int copy(void * buf, unsigned int pos, unsigned int num_surfaces)
108  {
109  return 0;
110  }
111  virtual bool push(SourcePad * pad, TimePoint play_start, NanoSecondsType accum_dur){ return false; };
112  virtual bool push(SourcePad * pad, unsigned long pts){ return false; };
113  virtual void unref(){};
114  void * batch;
115 // unsigned int num_filled;
116 
117 };
118 
120 {
121  public :
122  NvDsBatchBufferWrapper(unsigned int size);
123  virtual ~NvDsBatchBufferWrapper();
124 
131  virtual unsigned int copy_buf(std::shared_ptr<BufferWrapper> buf, unsigned int pos) = 0;
132 
133  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){};
134  virtual void unref(){};
135  unsigned int batch_size;
136 
137 };
138 
139 class Batch
140 {
141 
142 
143  public :
144 
145  Batch(unsigned int size)
146  {
147  acc_batch = 0;
148  batch_size = size;
149  }
150  //void form_batch();
151 
152  void check_source(unsigned int source_id);
153 
154  void reset_batch();
155 
156  void set_size(unsigned int size);
157 
158 
159 
160  /*
161  * @brief map of number of sources for sources in batch
162  * key=source_id
163  * value=number of surfaces to copy from this source
164  */
165  std::unordered_map<int, int> num_sources;
166 
170  unsigned int acc_batch;
173  unsigned int batch_size;
174  unsigned int num_surfaces_in_batch;
175 
176 
177 };
178 
179 
181 {
182  public :
183  void sorted_insert(unsigned int);
184  std::list<unsigned int>::iterator get_next_pos(std::list<unsigned int>::iterator pos);
185  std::list<unsigned int>::iterator get_max_pos();
186  unsigned int get_least();
187  std::list<unsigned int>::iterator get_least_pos();
188  unsigned int get_at(std::list<unsigned int>::iterator pos);
189  int size();
190  private :
191  std::list<unsigned int> store;
192 };
193 
195 {
196  public :
198  BatchPolicy(BatchPolicyConfig policy, std::unordered_map<unsigned int, SinkPad *> * ins, INvStreammuxDebug* a_debug_iface);
199 
200  unsigned int check_repeats_per_batch();
201 
202  unsigned int check_repeats_per_batch(unsigned int source_id);
203 
204  /*
205  * @brief function to try to form a batch per current priorities
206  * and set algorithm
207  * @param b [IN] the batch to update
208  */
209  Batch * form_batch( Batch * b, unsigned int batch_size);
210 
211  unsigned int get_batch_size();
212 
213  unsigned int get_config_batch_size();
214 
215  void set_batch_size(unsigned int);
216 
217  void set_batch_push_timeout(unsigned int);
221  void set_num_surfaces(unsigned int);
222 
223  /*
224  * @brief function to update a batch with buffers from a source
225  * If Timestamp synchronization user API is set,
226  * only on-time buffers will be batched.
227  * late buffers will be discarded.
228  * early buffers will not be batched with the current call.
229  * @param source_id [IN] the id of pad
230  * @param batch [IN] the batch to update
231  */
232  void update_with_source(Batch * batch, unsigned int source_id);
233 
234  /*
235  * @brief function to check amount of time to wait for batch data
236  * based on current time and min frame rate based duration
237  */
239 
240  //NanoSecondsType update_last_batch_time(NanoSecondsType last_batch_time);
241  void update_last_batch_time();
242 
243  /*
244  * @brief function to get delay for next batch
245  * based on max frame rate based duration
246  */
248 
249  /*
250  * @brief function to calculate delay for next batch
251  * based on current time and max frame rate based duration
252  */
254 
255  /*
256  * @brief function to check if a batch is ready to be pushed
257  * @param b [IN] the batch object pointer
258  */
259  bool is_ready(Batch * b);
260 
261  bool is_ready_or_due(Batch * b);
262 
263  /*
264  * @brief function to check if defaults exist for this source id
265  * from config, (TBD) this mechanism needs to replaced with something
266  * that is at initialization time
267  * @param source_id [IN] the id of pad
268  */
269  void check_and_update_defaults(unsigned int source_id);
270 
271  //void reset_batch(Batch * b);
272  unsigned int total_buf_available;
273 
274  bool check_past_min();
275 
276  void update_idle_sources(unsigned int idle_count);
277 
278  void update_eos_sources(unsigned int eos_count);
279 
280  unsigned int get_eos_sources();
281 
282  void update_push_stats(unsigned int source_id, unsigned int num_pushed);
283 
285 
286  void set_synch_buffer_iface(ISynchronizeBuffer* synch_buffer_iface);
287 
292  unsigned int synchronize_buffers_in_pad(SinkPad* pad,
293  unsigned int allowed_buffers);
294 
296 
298 
299  private :
300  unsigned int get_allowed(unsigned int source_id, float fps, unsigned int available);
301 
302  BatchPolicyConfig config;
303  NanoSecondsType max_fps_dur;
304  NanoSecondsType min_fps_dur;
306  NanoSecondsType min_early_buffer_dur;
307  TimePoint max_dur_time;
308  TimePoint min_dur_time;
309  std::unordered_map<unsigned int, float> src_max_fps;
310  std::unordered_map<unsigned int, NanoSecondsType> min_src_fps_dur;
311  std::unordered_map<unsigned int, TimePoint> src_push_times;
312  std::unordered_map<unsigned int, unsigned int> src_num_pushed;
313  SortedList priority_list;
314  /*
315  * @brief map of sources for a priority
316  * key = priority
317  * value = source_id
318  */
319  std::multimap<int, int> sources;
320  std::unordered_map<unsigned int, SinkPad *> * inputs;
321  std::chrono::time_point<std::chrono::steady_clock> last_batch_time;
322  unsigned int batch_size;
323  LastBatchState last_batch_state;
331  unsigned int num_sources_idle;
336  unsigned int num_sources_eos;
337  unsigned int num_surfaces_per_frame;
338  ISynchronizeBuffer* synch_buffer;
339  INvStreammuxDebug* debug_iface;
340 
341 
342 };
343 
344 
345 #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:112
NvStreammuxSourceProps::source_max_fps_d
unsigned int source_max_fps_d
Definition: nvstreammux_batch.h:53
NvDsBatchBufferWrapper::batch_size
unsigned int batch_size
Definition: nvstreammux_batch.h:134
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:133
NvDsBatchBufferWrapper
Definition: nvstreammux_batch.h:119
BatchPolicy::set_batch_size
void set_batch_size(unsigned int)
Batch::Batch
Batch(unsigned int size)
Definition: nvstreammux_batch.h:145
BatchPolicy::total_buf_available
unsigned int total_buf_available
Definition: nvstreammux_batch.h:272
Batch::num_sources
std::unordered_map< int, int > num_sources
Definition: nvstreammux_batch.h:165
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:71
SortedList::get_least
unsigned int get_least()
SinkPad
Definition: nvstreammux_pads.h:178
BatchPolicy::get_config_batch_size
unsigned int get_config_batch_size()
BATCH_METHOD_NONE
@ BATCH_METHOD_NONE
Definition: nvstreammux_batch.h:62
BatchPolicyConfig::BatchPolicyConfig
BatchPolicyConfig()
Definition: nvstreammux_batch.h:83
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:80
Batch
Definition: nvstreammux_batch.h:139
NvStreammuxSourceProps::max_num_frames_per_batch
unsigned int max_num_frames_per_batch
Definition: nvstreammux_batch.h:57
LastBatchState::priority_list_position
int priority_list_position
Definition: nvstreammux_batch.h:92
BatchPolicyConfig::overall_max_fps_n
unsigned int overall_max_fps_n
Definition: nvstreammux_batch.h:72
Clock
std::chrono::steady_clock Clock
Definition: nvstreammux_batch.h:47
NvDsBatchBufferWrapper::~NvDsBatchBufferWrapper
virtual ~NvDsBatchBufferWrapper()
BatchPolicyConfig::source_props
std::unordered_map< unsigned int, NvStreammuxSourceProps > source_props
Definition: nvstreammux_batch.h:81
SortedList::get_next_pos
std::list< unsigned int >::iterator get_next_pos(std::list< unsigned int >::iterator pos)
SourcePad
Definition: nvstreammux_pads.h:151
BatchPolicyConfig::overall_min_fps_d
unsigned int overall_min_fps_d
Definition: nvstreammux_batch.h:75
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:113
NvDsBatchBufferWrapper::unref
virtual void unref()
Definition: nvstreammux_batch.h:134
BatchBufferWrapper::copy
virtual unsigned int copy(void *buf, unsigned int pos, unsigned int num_surfaces)
Definition: nvstreammux_batch.h:107
BatchBufferWrapper::push
virtual bool push(SourcePad *pad, TimePoint play_start, NanoSecondsType accum_dur)
Definition: nvstreammux_batch.h:111
BatchBufferWrapper::BatchBufferWrapper
BatchBufferWrapper()
Definition: nvstreammux_batch.h:101
BatchBufferWrapper::unref
virtual void unref()
Definition: nvstreammux_batch.h:113
NvStreammuxBatchMethod
NvStreammuxBatchMethod
Definition: nvstreammux_batch.h:60
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: nvdsmeta.h:240
gstnvdsmeta.h
BatchPolicyConfig::adaptive_batching
bool adaptive_batching
Definition: nvstreammux_batch.h:70
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:180
BatchBufferWrapper
Definition: nvstreammux_batch.h:97
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:173
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:54
BatchPolicyConfig::max_same_source_frames
unsigned int max_same_source_frames
Definition: nvstreammux_batch.h:76
BatchPolicy::get_config
BatchPolicyConfig get_config()
BatchPolicy::form_batch
Batch * form_batch(Batch *b, unsigned int batch_size)
NvStreammuxSourceProps
Definition: nvstreammux_batch.h:50
Batch::reset_batch
void reset_batch()
BATCH_METHOD_PRIORITY
@ BATCH_METHOD_PRIORITY
Definition: nvstreammux_batch.h:64
BatchPolicyConfig::type
NvStreammuxBatchMethod type
Definition: nvstreammux_batch.h:69
BatchPolicy::get_max_duration_delay
NanoSecondsType get_max_duration_delay()
NanoSecondsType
std::chrono::duration< double, std::nano > NanoSecondsType
Definition: nvstreammux_pads.h:33
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:74
LastBatchState
Definition: nvstreammux_batch.h:90
Batch::num_surfaces_in_batch
unsigned int num_surfaces_in_batch
Definition: nvstreammux_batch.h:174
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:73
NvStreammuxSourceProps::source_min_fps_d
unsigned int source_min_fps_d
Definition: nvstreammux_batch.h:55
BATCH_METHOD_ROUND_ROBIN
@ BATCH_METHOD_ROUND_ROBIN
Definition: nvstreammux_batch.h:63
nvdsmeta.h
ISynchronizeBuffer
Definition: nvstreammux_pads.h:353
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:170
nvbufsurface.h
BatchPolicy
Definition: nvstreammux_batch.h:194
BatchPolicyConfig
Definition: nvstreammux_batch.h:67
BatchPolicy::BatchPolicy
BatchPolicy()
Definition: nvstreammux_batch.h:197
LastBatchState::source_map_position
int source_map_position
Definition: nvstreammux_batch.h:93
NvStreammuxSourceProps::source_max_fps_n
unsigned int source_max_fps_n
Definition: nvstreammux_batch.h:52
INvStreammuxDebug
Definition: nvstreammux_debug.h:27
SortedList::size
int size()
nvstreammux_pads.h
BatchPolicyConfig::enable_source_rate_control
bool enable_source_rate_control
Definition: nvstreammux_batch.h:77
TimePoint
std::chrono::time_point< std::chrono::steady_clock, NanoSecondsType > TimePoint
Definition: nvstreammux_batch.h:48
NvStreammuxSourceProps::priority
unsigned int priority
Definition: nvstreammux_batch.h:56