NVIDIA DeepStream SDK API Reference

9.1 Release
sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-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 __NVDSPREPROCESS_IMPL_H__
19 #define __NVDSPREPROCESS_IMPL_H__
20 
21 #include <stdarg.h>
22 #include <condition_variable>
23 #include <functional>
24 #include <list>
25 #include <memory>
26 #include <mutex>
27 #include <queue>
28 
29 #include <cuda_runtime_api.h>
30 
31 #include "nvdspreprocess_interface.h"
32 
33 #include <stdio.h>
34 #include <assert.h>
35 
38 #define _MAX_CHANNELS 4
39 
41 #define DISABLE_CLASS_COPY(NoCopyClass) \
42  NoCopyClass(const NoCopyClass &) = delete; \
43  void operator=(const NoCopyClass &) = delete
44 
46 #define SIMPLE_MOVE_COPY(Cls) \
47  Cls &operator=(Cls &&o) \
48  { \
49  move_copy(std::move(o)); \
50  return *this; \
51  } \
52  Cls(Cls &&o) { move_copy(std::move(o)); }
53 
55 inline const char *safeStr(const std::string &str)
56 {
57  return str.c_str();
58 }
59 
61 inline bool file_accessible(const char* path)
62 {
63  assert(path);
64  return (access(path, F_OK) != -1);
65 }
66 
68 inline bool file_accessible(const std::string &path)
69 {
70  return (!path.empty()) && file_accessible(path.c_str());
71 }
72 
76 typedef struct
77 {
81  std::string labelsFilePath;
82 
85  std::string meanImageFilePath;
86 
90  std::vector<float> offsets;
91 
93  float pixel_normalization_factor = 1.0;
94 
98 
103 {
104 public:
105  explicit CudaStream(uint flag = cudaStreamDefault, int priority = 0);
106  ~CudaStream();
108  operator cudaStream_t() { return m_Stream; }
110  cudaStream_t& ptr() { return m_Stream; }
113 
114 private:
115  void move_copy(CudaStream&& o)
116  {
117  m_Stream = o.m_Stream;
118  o.m_Stream = nullptr;
119  }
120  DISABLE_CLASS_COPY(CudaStream);
121 
122  cudaStream_t m_Stream = nullptr;
123 };
124 
129 {
130 public:
131  virtual ~CudaBuffer() = default;
133  size_t bytes() const { return m_Size; }
135  template <typename T>
136  T* ptr()
137  {
138  return (T*)m_Buf;
139  }
141  void* ptr() { return m_Buf; }
144 
145 protected:
146  explicit CudaBuffer(size_t s) : m_Size(s) {}
149  {
150  m_Buf = o.m_Buf;
151  o.m_Buf = nullptr;
152  m_Size = o.m_Size;
153  o.m_Size = 0;
154  }
158  void* m_Buf = nullptr;
160  size_t m_Size = 0;
161 };
162 
167 {
168 public:
170  explicit CudaDeviceBuffer(size_t size);
173 };
174 
179 {
180 public:
183  int id = 0);
184  virtual ~NvDsPreProcessTensorImpl() = default;
185 
187  bool setScaleOffsets(float scale, const std::vector<float>& offsets = {});
189  bool setMeanFile(const std::string& file);
198  CustomTensorParams &tensorParam, void*& devBuf);
199 
200 private:
201  NvDsPreProcessStatus readMeanImageFile();
202  DISABLE_CLASS_COPY(NvDsPreProcessTensorImpl);
203 
204 private:
205  int m_UniqueID = 0;
206 
207  /* Network input information. */
208  NvDsPreProcessNetworkSize m_NetworkSize = {0};
209  NvDsPreProcessFormat m_NetworkInputFormat = NvDsPreProcessFormat_RGB;
211 
212  float m_Scale = 1.0f;
213  std::vector<float> m_ChannelMeans; // same as channels
214  std::string m_MeanFile;
215 
216  std::unique_ptr<CudaStream> m_PreProcessStream;
217  std::unique_ptr<CudaDeviceBuffer> m_MeanDataBuffer;
218 };
219 
223 extern "C"
226  NvDsPreProcessTensorParams *tensor_params,
227  std::unique_ptr<NvDsPreProcessTensorImpl> & m_Preprocessor, int unique_id);
228 
229 #endif
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: sources/includes/nvbufsurftransform.h:35
CudaDeviceBuffer::~CudaDeviceBuffer
~CudaDeviceBuffer()
destructor
CudaBuffer::ptr
T * ptr()
template to return cuda buffer
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:136
CudaBuffer::CudaBuffer
CudaBuffer(size_t s)
helper move copy functionality
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:146
normalization_mean_subtraction_impl_initialize
NvDsPreProcessStatus normalization_mean_subtraction_impl_initialize(CustomMeanSubandNormParams *custom_params, NvDsPreProcessTensorParams *tensor_params, std::unique_ptr< NvDsPreProcessTensorImpl > &m_Preprocessor, int unique_id)
Initialize for pixel normalization and mean subtraction.
CustomMeanSubandNormParams
Custom parameters for normalization and mean subtractions.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:76
NvDsPreProcessFormat_RGB
@ NvDsPreProcessFormat_RGB
Specifies 24-bit interleaved R-G-B format.
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:102
NvDsPreProcessNetworkSize
Holds information about the model network.
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:156
NvDsPreProcessTensorImpl::setMeanFile
bool setMeanFile(const std::string &file)
method to set mean file
NvDsPreProcessBatch
Holds information about the batch of frames to be inferred.
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:252
NvDsPreProcessTensorImpl::allocateResource
NvDsPreProcessStatus allocateResource()
allocate resources for tensor preparation
NvDsPreProcessTensorImpl::setInputOrder
bool setInputOrder(const NvDsPreProcessNetworkInputOrder order)
method to set network input order
NvDsPreProcessStatus
NvDsPreProcessStatus
Enum for the status codes returned by NvDsPreProcessImpl.
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:54
safeStr
const char * safeStr(const std::string &str)
helper check safe string in C
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:55
NvDsPreProcessTensorImpl
Provides pre-processing functionality like mean subtraction and normalization.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:178
CudaStream
Helper class for managing Cuda Streams.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:102
CustomMeanSubandNormParams::labelsFilePath
std::string labelsFilePath
Holds the pathname of the labels file containing strings for the class labels.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:81
NvDsPreProcessTensorImpl::NvDsPreProcessTensorImpl
NvDsPreProcessTensorImpl(const NvDsPreProcessNetworkSize &size, NvDsPreProcessFormat format, int id=0)
constructor for tensor preparation implementation
CudaBuffer
Helper base class for managing Cuda allocated buffers.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:128
CudaStream::~CudaStream
~CudaStream()
NvDsPreProcessTensorImpl::syncStream
NvDsPreProcessStatus syncStream()
synchronize cuda stream
CustomTensorParams
Tensor params passed to custom library for tensor preparation.
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:178
CudaBuffer::move_copy
void move_copy(CudaBuffer &&o)
move_copy cuda buffer
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:148
CudaBuffer::~CudaBuffer
virtual ~CudaBuffer()=default
NvDsPreProcessTensorImpl::~NvDsPreProcessTensorImpl
virtual ~NvDsPreProcessTensorImpl()=default
CudaBuffer::m_Size
size_t m_Size
buffer size
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:160
NvDsPreProcessTensorParams
Holds model parameters for tensor preparation.
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:134
file_accessible
bool file_accessible(const char *path)
helper check if file accessible in C
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:61
CudaBuffer::m_Buf
void * m_Buf
pointer to cuda buffer
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:158
NvDsPreProcessTensorImpl::setScaleOffsets
bool setScaleOffsets(float scale, const std::vector< float > &offsets={})
method to set offsets values
CudaBuffer::bytes
size_t bytes() const
size of cuda buffer in bytes
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:133
CudaDeviceBuffer
CUDA device buffers.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:166
CustomMeanSubandNormParams::meanImageFilePath
std::string meanImageFilePath
Holds the pathname of the mean image file (PPM format).
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:85
NvDsPreProcessNetworkInputOrder
NvDsPreProcessNetworkInputOrder
Enum for the network input order according to which network shape will be provided to prepare raw ten...
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:86
CustomMeanSubandNormParams::networkSize
NvDsPreProcessNetworkSize networkSize
width, height, channels size of Network
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:96
SIMPLE_MOVE_COPY
#define SIMPLE_MOVE_COPY(Cls)
helper move function
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:46
CudaDeviceBuffer::CudaDeviceBuffer
CudaDeviceBuffer(size_t size)
constructor
NvDsPreProcessNetworkInputOrder_kNCHW
@ NvDsPreProcessNetworkInputOrder_kNCHW
Specifies NCHW network input order.
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:89
CudaBuffer::DISABLE_CLASS_COPY
DISABLE_CLASS_COPY(CudaBuffer)
disable class copy
CudaBuffer::ptr
void * ptr()
pointer to cuda buffer
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:141
NvDsPreProcessTensorImpl::prepare_tensor
NvDsPreProcessStatus prepare_tensor(NvDsPreProcessBatch *batch, CustomTensorParams &tensorParam, void *&devBuf)
method to prepare tensor using cuda kernels
NvDsPreProcessFormat
NvDsPreProcessFormat
Defines model color formats.
Definition: sources/gst-plugins/gst-nvdspreprocess/include/nvdspreprocess_interface.h:99
CustomMeanSubandNormParams::offsets
std::vector< float > offsets
Holds the per-channel offsets for mean subtraction.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:90
CudaStream::ptr
cudaStream_t & ptr()
pointer to cuda stream
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:110
ds3d::v2xinfer::format
static std::string format(const char *fmt,...)
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:31
CudaStream::CudaStream
CudaStream(uint flag=cudaStreamDefault, int priority=0)