NVIDIA DeepStream SDK API Reference

7.0 Release
seq_process_common.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021 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 __NVDS_SEQ_PREPROCESS_COMMON_H__
14 #define __NVDS_SEQ_PREPROCESS_COMMON_H__
15 
16 #include <inttypes.h>
17 #include <stdint.h>
18 #include <string.h>
19 
20 #include <atomic>
21 #include <cassert>
22 #include <condition_variable>
23 #include <fstream>
24 #include <functional>
25 #include <iostream>
26 #include <map>
27 #include <memory>
28 #include <mutex>
29 #include <numeric>
30 #include <queue>
31 #include <stdexcept>
32 #include <thread>
33 #include <tuple>
34 #include <unordered_map>
35 
36 #include "nvbufsurface.h"
37 #include "nvbufsurftransform.h"
38 #include "nvdspreprocess_interface.h"
39 
40 // custom settings in [user-config]
41 #define CUSTOM_CONFIG_SUBSAMPLE "subsample"
42 #define CUSTOM_CONFIG_STRIDE "stride"
43 #define CUSTOM_CONFIG_CHANNEL_SCALE_FACTORS "channel-scale-factors"
44 #define CUSTOM_CONFIG_CHANNEL_MEANS "channel-mean-offsets"
45 
46 // Misc macro definitions
47 #define DSASSERT assert
48 
49 #ifndef UNUSED
50 #define UNUSED(var) (void)(var)
51 #endif
52 
53 // log information definition for log print, debug, info, error.
54 
55 #define LOG_PRINT_(out, level, fmt, ...) \
56  fprintf(out, "%s:%d, [%s: CUSTOM_LIB] " fmt "\n", __FILE__, __LINE__, #level, ##__VA_ARGS__)
57 
58 #define LOG_DEBUG(fmt, ...) \
59  if (kEnableDebug) { \
60  LOG_PRINT_(stdout, DEBUG, fmt, ##__VA_ARGS__); \
61  }
62 
63 #define LOG_INFO(fmt, ...) LOG_PRINT_(stdout, INFO, fmt, ##__VA_ARGS__)
64 
65 #define LOG_ERROR(fmt, ...) LOG_PRINT_(stderr, ERROR, fmt, ##__VA_ARGS__)
66 
67 // check preprocess errors and return error no.
68 #define CHECK_PROCESS_ERROR(err, fmt, ...) \
69  do { \
70  NvDsPreProcessStatus _sno_ = (err); \
71  if (_sno_ != NVDSPREPROCESS_SUCCESS) { \
72  LOG_ERROR(fmt ", seq_process error: %d", ##__VA_ARGS__, (int)_sno_); \
73  return _sno_; \
74  } \
75  } while (0)
76 
77 // check CUDA errors errors and return process error
78 #define CHECK_CUDA_ERR(err, fmt, ...) \
79  do { \
80  cudaError_t _errnum_ = (err); \
81  if (_errnum_ != cudaSuccess) { \
82  LOG_ERROR( \
83  fmt ", cuda err_no: %d, err_str: %s", ##__VA_ARGS__, (int)_errnum_, \
84  cudaGetErrorName(_errnum_)); \
85  return NVDSPREPROCESS_CUDA_ERROR; \
86  } \
87  } while (0)
88 
89 // declare export APIs
90 #define PROCESS_EXPORT_API __attribute__((__visibility__("default")))
91 
92 // skip frame: extend values of NvDsPreProcessStatus
93 #define NVDSPREPROCESS_SKIP_FRAME (NvDsPreProcessStatus)255
94 
95 // default frame number value if not set
96 #define FRAME_NO_NOT_SET UINT64_MAX
97 
98 // tuple<stream_id, roi.xy0, roi.xy1>
99 using SourceKey = std::tuple<uint64_t, int64_t, int64_t>;
100 
101 // enable debug log
102 extern bool kEnableDebug;
103 
104 // Block descriptions to store multiple sequenced buffers
106  NvDsPreProcessCustomBuf* buf = nullptr; // allocated from NvDsPreProcessAcquirer
107  // parameters description of allocated `buf`
110  uint32_t maxBatchSize = 0; // parsed from param.network_input_shape[0]
111  uint32_t inUseBatchSize = 0;
112 };
113 
114 // ROI based sequence processing buffer
116  // pointer to batch buffer block
117  FullBatchBlock* block = nullptr;
118  // offsets in bytes for current sequence in batched block
119  uint64_t blockOffset = 0;
120 
121  // starting sequence index of this buffer.
122  // index loops between [0, seqence-size]
123  uint32_t startSeqIdx = 0;
124  // accumulation count of processed buffers.
125  // accumulation is sliding by stride in user-config
126  uint32_t processedSeq = 0;
127 
128  // ROI information of this sequence
130  // latest processed buffer information
132  // lastest updated frame number
134  // frame number to indicate next sequence is ready
135  uint64_t nextReadyFrameNo = 0;
136 };
137 
138 // results storing batches of sequence processed buffers
139 struct ReadyResult {
140  // allocated batches of processed sequence buffers
142  // ROI list of processed buffers
143  std::vector<NvDsRoiMeta> rois;
144  // updated parameter desciptions of `readyBuf`
147 };
148 
149 // mapping source/roi to specific sequence processing buffer
150 using SrcDestBufMap = std::map<SourceKey, std::unique_ptr<RoiProcessedBuf>>;
151 
152 // definition of smart pointer
153 template <class T>
154 using UniqPtr = std::unique_ptr<T, std::function<void(T*)>>;
155 
156 #endif // __NVDS_SEQ_PREPROCESS_COMMON_H__
RoiProcessedBuf
Definition: seq_process_common.h:115
ReadyResult
Definition: seq_process_common.h:139
NvDsPreProcessFormat_RGB
@ NvDsPreProcessFormat_RGB
Specifies 24-bit interleaved R-G-B format.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:97
FullBatchBlock::maxBatchSize
uint32_t maxBatchSize
Definition: seq_process_common.h:110
RoiProcessedBuf::roiInfo
NvDsRoiMeta roiInfo
Definition: seq_process_common.h:129
FRAME_NO_NOT_SET
#define FRAME_NO_NOT_SET
Definition: seq_process_common.h:96
UniqPtr
std::unique_ptr< T, std::function< void(T *)> > UniqPtr
Definition: seq_process_common.h:154
RoiProcessedBuf::latestFrameNo
uint64_t latestFrameNo
Definition: seq_process_common.h:133
ReadyResult::param
NvDsPreProcessTensorParams param
Definition: seq_process_common.h:145
FullBatchBlock
Definition: seq_process_common.h:105
RoiProcessedBuf::latestUnit
NvDsPreProcessUnit latestUnit
Definition: seq_process_common.h:131
NvDsPreProcessUnit
A preprocess unit for processing which can be Frame/ROI.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:221
FullBatchBlock::param
NvDsPreProcessTensorParams param
Definition: seq_process_common.h:108
RoiProcessedBuf::blockOffset
uint64_t blockOffset
Definition: seq_process_common.h:119
ReadyResult::readyBuf
NvDsPreProcessCustomBuf * readyBuf
Definition: seq_process_common.h:141
FullBatchBlock::buf
NvDsPreProcessCustomBuf * buf
Definition: seq_process_common.h:106
FullBatchBlock::inUseBatchSize
uint32_t inUseBatchSize
Definition: seq_process_common.h:111
NvDsPreProcessCustomBuf
Custom Buffer passed to the custom lib for preparing tensor.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:199
RoiProcessedBuf::nextReadyFrameNo
uint64_t nextReadyFrameNo
Definition: seq_process_common.h:135
NvDsPreProcessTensorParams
Holds model parameters for tensor preparation.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:129
kEnableDebug
bool kEnableDebug
SourceKey
std::tuple< uint64_t, int64_t, int64_t > SourceKey
Definition: seq_process_common.h:99
RoiProcessedBuf::startSeqIdx
uint32_t startSeqIdx
Definition: seq_process_common.h:123
SrcDestBufMap
std::map< SourceKey, std::unique_ptr< RoiProcessedBuf > > SrcDestBufMap
Definition: seq_process_common.h:150
RoiProcessedBuf::block
FullBatchBlock * block
Definition: seq_process_common.h:117
ReadyResult::rois
std::vector< NvDsRoiMeta > rois
Definition: seq_process_common.h:143
NvDsRoiMeta
Holds Information about ROI Metadata.
Definition: nvds_roi_meta.h:86
RoiProcessedBuf::processedSeq
uint32_t processedSeq
Definition: seq_process_common.h:126
NvDsPreProcessNetworkInputOrder_CUSTOM
@ NvDsPreProcessNetworkInputOrder_CUSTOM
Specifies any other custom input order handled by custom lib.
Definition: gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:88
nvbufsurftransform.h
nvbufsurface.h