NVIDIA DeepStream SDK API Reference

7.0 Release
infer_options.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-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 __NVDSINFERSERVER_OPTIONS_H__
14 #define __NVDSINFERSERVER_OPTIONS_H__
15 
16 #include <infer_datatypes.h>
17 
18 #include <optional>
19 #include <string>
20 #include <unordered_map>
21 
22 #ifdef FOR_PRIVATE
23 #include "infer_common.h"
24 #include "infer_utils.h"
25 #else
26 inline void
27 dsInferLogPrint__(NvDsInferLogLevel level, const char* fmt, ...)
28 {
29  va_list args;
30  va_start(args, fmt);
31  vprintf(fmt, args);
32  va_end(args);
33 }
34 #define safeStr(str) str.c_str()
35 
36 #endif
37 
38 namespace nvdsinferserver {
39 
40 class BufOptions;
41 using SharedBufOptions = std::shared_ptr<BufOptions>;
42 
43 class BufOptions : public IOptions {
44 private:
45  struct D {
46  struct BasicValue {
47  union {
48  int64_t vInt64;
49  uint64_t vUint64;
50  double vDouble;
51  bool vBool;
52  void* vPtr;
53  } value;
55  std::string vStr;
56  template <typename V>
57  inline void setV(const V& v, OptionType t)
58  {
59  *((V*)(void*)&value) = v;
60  this->type = t;
61  }
62  } vHead;
63  std::vector<BasicValue> vArray;
64  };
65 
66 public:
67  OptionType getType(const std::string& key) const override
68  {
69  const auto i = m_Fields.find(key);
70  return (i == m_Fields.end() ? OptionType::oNone : i->second.vHead.type);
71  }
72  bool hasValue(const std::string& key) const override
73  {
74  const auto i = m_Fields.find(key);
75  return (i == m_Fields.end() ? false : true);
76  }
77  uint32_t getCount() const final { return (uint32_t)m_Fields.size(); }
78  std::string getKey(uint32_t idx) const final
79  {
80  assert(idx < m_Fields.size());
81  auto i = m_Fields.cbegin();
82  std::advance(i, idx);
83  return i->first;
84  }
85 
86 private:
87  NvDsInferStatus getValuePtr(const std::string& key, OptionType t, void*& ptr) const override
88  {
89  assert(t != OptionType::oNone && t != OptionType::oArray);
90  auto d = getValueD(key, t);
92  d, NVDSINFER_INVALID_PARAMS, "failed to get pointer value:%s", safeStr(key));
93  if (t == OptionType::oString) {
94  ptr = (void*)&(d->vHead.vStr);
95  } else {
96  ptr = (void*)&(d->vHead.value);
97  }
98  return NVDSINFER_SUCCESS;
99  }
100 
101  NvDsInferStatus getArraySize(const std::string& key, uint32_t& size) const override
102  {
103  auto d = getValueD(key, OptionType::oArray);
104  RETURN_IF_FAILED(d, NVDSINFER_INVALID_PARAMS, "failed to get array value:%s", safeStr(key));
105  size = d->vArray.size();
106  return NVDSINFER_SUCCESS;
107  }
108 
109  NvDsInferStatus getRawPtrArray(
110  const std::string& key, OptionType ot, void** ptrBase, uint32_t size) const override
111  {
112  auto d = getValueD(key, OptionType::oArray);
114  d, NVDSINFER_INVALID_PARAMS, "failed to get pointer array value:%s", safeStr(key));
115  assert(size <= d->vArray.size());
116  for (uint32_t i = 0; i < size; ++i) {
117  auto& each = d->vArray[i];
118  assert(each.type != OptionType::oArray && each.type != OptionType::oNone);
120  each.type == ot, NVDSINFER_INVALID_PARAMS,
121  "query value type:%d doesn't match exact type:%d in array.", (int)ot,
122  (int)each.type);
123  if (ot == OptionType::oString) {
124  ptrBase[i] = (void*)&(each.vStr);
125  } else {
126  ptrBase[i] = (void*)&(each.value);
127  }
128  }
129  return NVDSINFER_SUCCESS;
130  }
131 
132  template <typename In>
133  struct convertType {
134  };
135 
136 public:
137  template <typename T>
138  inline void setValue(const std::string& key, const T& v)
139  {
140  using t = typename convertType<std::remove_const_t<std::remove_reference_t<T>>>::t;
141  auto& field = m_Fields[key];
142  field.vHead.setV<t>(v, oType<t>::v);
143  }
144 
145  template <typename T>
146  inline void setValueArray(const std::string& key, const std::vector<T>& values)
147  {
148  if (values.empty()) {
149  return;
150  }
151  using t = typename convertType<std::remove_const_t<std::remove_reference_t<T>>>::t;
152  auto& field = m_Fields[key];
153  field.vHead.type = OptionType::oArray;
154  field.vArray = std::vector<D::BasicValue>(values.size());
155  for (size_t i = 0; i < values.size(); ++i) {
156  auto& data = field.vArray[i];
157  data.setV<t>(t(values[i]), oType<t>::v);
158  }
159  }
160 
161 private:
162  const D* getValueD(const std::string& key, OptionType t) const
163  {
164  const auto i = m_Fields.find(key);
165  if (i == m_Fields.end()) {
166  InferError("BufOptions: No option:%s found.", safeStr(key));
167  return nullptr;
168  }
169  if (i->second.vHead.type != t) {
170  InferError(
171  "BufOptions: get option:%s but type is not matched.",
172  safeStr(key));
173  return nullptr;
174  }
175  return &(i->second);
176  }
177 
178  std::unordered_map<std::string, D> m_Fields;
179 };
180 
181 template <>
182 inline void
183 BufOptions::D::BasicValue::setV<std::string>(const std::string& v, OptionType t)
184 {
185  this->vStr = v;
186  assert(t == OptionType::oString);
187  this->type = t;
188 }
189 
190 template <typename T>
191 struct BufOptions::convertType<T*> {
192  typedef std::remove_const_t<T>* t;
193 };
194 template <>
195 struct BufOptions::convertType<int64_t> {
196  typedef int64_t t;
197 };
198 template <>
199 struct BufOptions::convertType<int32_t> {
200  typedef int64_t t;
201 };
202 template <>
203 struct BufOptions::convertType<int16_t> {
204  typedef int64_t t;
205 };
206 template <>
207 struct BufOptions::convertType<int8_t> {
208  typedef int64_t t;
209 };
210 template <>
211 struct BufOptions::convertType<uint64_t> {
212  typedef uint64_t t;
213 };
214 template <>
215 struct BufOptions::convertType<uint32_t> {
216  typedef uint64_t t;
217 };
218 template <>
219 struct BufOptions::convertType<uint16_t> {
220  typedef uint64_t t;
221 };
222 template <>
223 struct BufOptions::convertType<uint8_t> {
224  typedef uint64_t t;
225 };
226 template <>
227 struct BufOptions::convertType<double> {
228  typedef double t;
229 };
230 template <>
231 struct BufOptions::convertType<float> {
232  typedef double t;
233 };
234 template <>
235 struct BufOptions::convertType<bool> {
236  typedef bool t;
237 };
238 template <>
239 struct BufOptions::convertType<std::string> {
240  typedef std::string t;
241 };
242 
243 template <typename T> // not supported
244 struct BufOptions::convertType<std::vector<T>> {
245 };
246 
247 template <typename T> // not supported
248 struct BufOptions::convertType<std::vector<T*>> {
249 };
250 
251 } // namespace nvdsinferserver
252 
253 #endif //__NVDSINFERSERVER_OPTIONS_H__
nvdsinferserver
This is a header file for pre-processing cuda kernels with normalization and mean subtraction require...
Definition: infer_custom_process.h:24
nvdsinferserver::BufOptions::D::BasicValue::vInt64
int64_t vInt64
Definition: infer_options.h:48
nvdsinferserver::OptionType::oString
@ oString
nvdsinferserver::OptionType::oNone
@ oNone
nvdsinferserver::BufOptions::D::BasicValue::vPtr
void * vPtr
Definition: infer_options.h:52
nvdsinferserver::BufOptions::convertType< uint64_t >::t
uint64_t t
Definition: infer_options.h:212
nvdsinferserver::IOptions::oType
Definition: infer_ioptions.h:75
nvdsinferserver::BufOptions::D::BasicValue::vDouble
double vDouble
Definition: infer_options.h:50
infer_datatypes.h
Header file for the data types used in the inference processing.
nvdsinferserver::BufOptions::D::BasicValue::vUint64
uint64_t vUint64
Definition: infer_options.h:49
nvdsinferserver::BufOptions::getType
OptionType getType(const std::string &key) const override
Definition: infer_options.h:67
nvdsinferserver::OptionType::oArray
@ oArray
nvdsinferserver::BufOptions::D::BasicValue::type
OptionType type
Definition: infer_options.h:54
nvdsinferserver::BufOptions::convertType< uint8_t >::t
uint64_t t
Definition: infer_options.h:224
NVDSINFER_SUCCESS
@ NVDSINFER_SUCCESS
NvDsInferContext operation succeeded.
Definition: nvdsinfer.h:220
nvdsinferserver::BufOptions::setValue
void setValue(const std::string &key, const T &v)
Definition: infer_options.h:138
nvdsinferserver::BufOptions::convertType< int32_t >::t
int64_t t
Definition: infer_options.h:200
nvdsinferserver::BufOptions::convertType< float >::t
double t
Definition: infer_options.h:232
NvDsInferLogLevel
NvDsInferLogLevel
Enum for the log levels of NvDsInferContext.
Definition: nvdsinfer.h:249
nvdsinferserver::BufOptions::getKey
std::string getKey(uint32_t idx) const final
Definition: infer_options.h:78
infer_utils.h
Header file containing utility functions and classes used by the nvinferserver low level library.
nvdsinferserver::BufOptions::D::BasicValue
Definition: infer_options.h:46
infer_common.h
Header file of the common declarations for the nvinferserver library.
dsInferLogPrint__
void dsInferLogPrint__(NvDsInferLogLevel level, const char *fmt,...)
Print the nvinferserver log messages as per the configured log level.
Definition: infer_options.h:27
nvdsinferserver::BufOptions::convertType< uint16_t >::t
uint64_t t
Definition: infer_options.h:220
nvdsinferserver::SharedBufOptions
std::shared_ptr< BufOptions > SharedBufOptions
Definition: infer_options.h:41
nvdsinferserver::BufOptions::D::BasicValue::setV
void setV(const V &v, OptionType t)
Definition: infer_options.h:57
nvdsinferserver::BufOptions::convertType< T * >::t
std::remove_const_t< T > * t
Definition: infer_options.h:192
nvdsinferserver::BufOptions::setValueArray
void setValueArray(const std::string &key, const std::vector< T > &values)
Definition: infer_options.h:146
nvdsinferserver::BufOptions::convertType< bool >::t
bool t
Definition: infer_options.h:236
nvdsinferserver::BufOptions::convertType< std::string >::t
std::string t
Definition: infer_options.h:240
nvdsinferserver::BufOptions::D::BasicValue::value
union nvdsinferserver::BufOptions::D::BasicValue::@0 value
nvdsinferserver::BufOptions::convertType< int16_t >::t
int64_t t
Definition: infer_options.h:204
RETURN_IF_FAILED
#define RETURN_IF_FAILED(condition, ret, fmt,...)
Definition: infer_defines.h:70
nvdsinferserver::BufOptions::D::BasicValue::vStr
std::string vStr
Definition: infer_options.h:55
InferError
#define InferError(fmt,...)
Definition: infer_defines.h:46
nvdsinferserver::BufOptions::D::BasicValue::vBool
bool vBool
Definition: infer_options.h:51
nvdsinferserver::BufOptions::convertType< int8_t >::t
int64_t t
Definition: infer_options.h:208
NVDSINFER_INVALID_PARAMS
@ NVDSINFER_INVALID_PARAMS
Invalid parameters were supplied.
Definition: nvdsinfer.h:227
nvdsinferserver::IOptions
Definition: infer_ioptions.h:54
nvdsinferserver::BufOptions::hasValue
bool hasValue(const std::string &key) const override
Definition: infer_options.h:72
nvdsinferserver::BufOptions::getCount
uint32_t getCount() const final
Definition: infer_options.h:77
safeStr
#define safeStr(str)
Definition: infer_options.h:34
nvdsinferserver::BufOptions
Definition: infer_options.h:43
nvdsinferserver::OptionType
OptionType
Definition: infer_ioptions.h:28
nvdsinferserver::BufOptions::convertType< double >::t
double t
Definition: infer_options.h:228
nvdsinferserver::BufOptions::convertType< int64_t >::t
int64_t t
Definition: infer_options.h:196
nvdsinferserver::BufOptions::convertType< uint32_t >::t
uint64_t t
Definition: infer_options.h:216
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: nvdsinfer.h:218