NVIDIA DeepStream SDK API Reference

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