NVIDIA DeepStream SDK API Reference

9.1 Release
sources/includes/ds3d/common/impl/impl_frames.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 _DS3D_COMMON_IMPL_FRAMES__H
19 #define _DS3D_COMMON_IMPL_FRAMES__H
20 
21 #include <ds3d/common/abi_frame.h>
22 #include <ds3d/common/common.h>
23 #include <ds3d/common/func_utils.h>
24 #include <ds3d/common/hpp/frame.hpp>
25 
26 namespace ds3d { namespace impl {
27 
28 template <typename DataTypeTP, FrameType ft, class abiBase>
29 class BaseFrame : public abiBase {
30 public:
31  DataType dataType() const final { return _dataType(); }
32  FrameType frameType() const final { return _frameType(); }
33  MemType memType() const final { return _memType; }
34 
35  size_t bytes() const override { return _bytes; }
36  void* base() const final { return (void*)(_data); }
37  const Shape& shape() const final { return _shape; }
38  int64_t devId() const final { return _devId; }
39  bool isValid() const { return base() && bytes() > 0; }
40 
41  // Implementation public functions
42  using Deleter = std::function<void(void*)>;
43  void resetShape(const Shape& s) { _shape = s; }
44  // function reset is not virtual
45  void reset()
46  {
47  if (_data && _del) {
48  _del(_data);
49  }
50  _data = nullptr;
51  _bytes = 0;
52  _shape = {0, {0}};
53  _memType = MemType::kNone;
54  _devId = 0;
55  _del = nullptr;
56  }
57 
58  void resetData(void* data, size_t bytes, Deleter del = nullptr)
59  {
60  if (_data && _del) {
61  _del(_data);
62  }
63  _data = data;
64  _bytes = bytes;
65  _del = std::move(del);
66  }
67 
69  void* data, size_t bytes, const Shape& shape, MemType memType, uint64_t devId,
70  Deleter&& deleter = nullptr)
71  : _data(data), _bytes(bytes), _memType(memType), _devId(devId), _shape(shape),
72  _del(std::move(deleter))
73  {
74  }
75  ~BaseFrame() override { reset(); }
76 
77  template <class EleT>
78  EleT& at(size_t idx)
79  {
80  DS_ASSERT(sizeof(EleT) * idx < _bytes);
81  return static_cast<EleT*>(_data)[idx];
82  }
83 
84 protected:
85  template <typename F>
86  void setDeleter(F&& f)
87  {
88  _del = std::move(f);
89  }
90 
91 private:
92  // internal
93  static constexpr DataType _dataType() { return __DataTypeTrait<DataTypeTP>::_data_type(); }
94  static constexpr FrameType _frameType() { return ft; }
95 
96 private:
97  void* _data = nullptr;
98  size_t _bytes = 0;
99  MemType _memType = MemType::kNone;
100  int64_t _devId = 0;
101  Shape _shape = {0, {0}};
102  Deleter _del = nullptr;
103 };
104 
105 template <typename DataTypeTP, FrameType ft>
107 
108 template <typename DataTypeTP, FrameType ft>
109 class Frame2DBaseImpl : public BaseFrame<DataTypeTP, ft, abi2DFrame> {
110 public:
113  void* data, size_t bytes, const Shape& shape, MemType memType, uint64_t devId,
114  Deleter&& deleter)
115  : BaseFrame<DataTypeTP, ft, abi2DFrame>(
116  data, bytes, shape, memType, devId, std::move(deleter))
117  {
118  }
119 
120  uint32_t planes() const final { return _planes.size(); }
121  const Frame2DPlane& getPlane(uint32_t idx) const final
122  {
123  if (idx > _planes.size()) {
124  DS_ASSERT(false);
125  throwError(ErrCode::kParam, "idx is larger than planes number");
126  }
127  return _planes[idx];
128  }
129  void setPlanes(const std::vector<Frame2DPlane>& p)
130  {
131  _planes = p;
132  if (p.empty()) {
133  this->resetShape(Shape{0, {0}});
134  } else {
135  this->resetShape(Shape{2, {(int)p[0].height, (int)p[0].width}});
136  }
137  }
138 
140  void* data, const std::vector<Frame2DPlane>& planes, size_t bytes, MemType memType,
141  uint64_t devId, Deleter&& deleter)
142  : BaseFrame<DataTypeTP, ft, abi2DFrame>(
143  data, bytes, {0, {0}}, memType, devId, std::move(deleter))
144  {
145  setPlanes(planes);
146  }
148 
149  void reset()
150  {
151  _planes.clear();
153  }
154 
155  template <class EleT>
156  EleT& at(size_t row, size_t column, uint32_t plane = 0)
157  {
158  DS_ASSERT(plane < _planes.size());
159  DS_ASSERT(row < _planes[plane].height && column < _planes[plane].width);
160  uint8_t* buf =
161  (uint8_t*)this->base() + _planes[plane].offset + row * _planes[plane].pitchInBytes;
162  return reinterpret_cast<EleT*>(buf + _planes[plane].bytesPerPixel * column);
163  }
164 
165 private:
166  std::vector<Frame2DPlane> _planes;
167 };
168 
169 // template<typename TP>
170 // using DepthFrameImpl = FrameImpl<vec1<TP>, TP, FrameType::kDepth>;
171 
172 // template<typename TP>
173 // using RGBFrameImpl = FrameImpl<vec3<TP>, TP, FrameType::kColorRGB>;
174 
175 // template<typename TP>
176 // using RGBAFrameImpl = FrameImpl<vec4<TP>, TP, FrameType::kColorRGBA>;
177 
178 template <typename DataTypeTP, FrameType ft>
181  void* data, size_t bytes, const Shape& shape, MemType memType, uint64_t devId,
182  std::function<void(void*)>&& deleter)
183 {
184  abiRefFrame* f = NewAbiRef<abiFrame>(
185  new FrameBaseImpl<DataTypeTP, ft>(data, bytes, shape, memType, devId, std::move(deleter)));
186  FrameGuard frame(f, true);
187  return frame;
188 }
189 
190 template <typename DataTypeTP, FrameType ft>
193  void* data, const std::vector<Frame2DPlane>& planes, size_t bytes, MemType memType,
194  uint64_t devId, std::function<void(void*)> deleter)
195 {
196  abiRef2DFrame* f = NewAbiRef<abi2DFrame>(new Frame2DBaseImpl<DataTypeTP, ft>(
197  data, planes, bytes, memType, devId, std::move(deleter)));
198  Frame2DGuard frame(f, true);
199  return frame;
200 }
201 
202 template <typename DataTypeTP>
205  void* data, uint32_t points, MemType memType, uint64_t devId,
206  std::function<void(void*)>&& deleter)
207 {
208  Shape shape{2, {(int)points, 3}};
209  return WrapFrame<DataTypeTP, FrameType::kPointXYZ>(
210  data, sizeof(DataTypeTP) * ShapeSize(shape), shape, memType, devId, std::move(deleter));
211 }
212 
213 template <typename DataTypeTP>
216  void* data, uint32_t points, MemType memType, uint64_t devId,
217  std::function<void(void*)>&& deleter)
218 {
219  Shape shape{2, {(int)points, 4}};
220  return WrapFrame<DataTypeTP, FrameType::kLidarXYZI>(
221  data, sizeof(DataTypeTP) * ShapeSize(shape), shape, memType, devId, std::move(deleter));
222 }
223 
224 template <typename DataTypeTP>
227  void* data, uint32_t points, MemType memType, uint64_t devId,
228  std::function<void(void*)>&& deleter)
229 {
230  Shape shape{2U, {(int)points, 2}};
231  return WrapFrame<DataTypeTP, FrameType::kPointCoordUV>(
232  data, sizeof(DataTypeTP) * ShapeSize(shape), shape, memType, devId, std::move(deleter));
233 }
234 
235 }} // namespace ds3d::impl
236 
237 #endif // _DS3D_COMMON_IMPL_FRAMES__H
ds3d::impl::BaseFrame::resetData
void resetData(void *data, size_t bytes, Deleter del=nullptr)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:58
ds3d::impl::BaseFrame::bytes
size_t bytes() const override
Definition: sources/includes/ds3d/common/impl/impl_frames.h:35
ds3d::impl::Frame2DBaseImpl
Definition: sources/includes/ds3d/common/impl/impl_frames.h:109
ds3d::impl::BaseFrame::~BaseFrame
~BaseFrame() override
Definition: sources/includes/ds3d/common/impl/impl_frames.h:75
ds3d::impl::wrapPointXYZFrame
FrameGuard wrapPointXYZFrame(void *data, uint32_t points, MemType memType, uint64_t devId, std::function< void(void *)> &&deleter)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:204
ds3d::ShapeSize
size_t ShapeSize(const Shape &shape)
Definition: sources/includes/ds3d/common/func_utils.h:170
ds3d::DataType
DataType
Definition: sources/includes/ds3d/common/idatatype.h:82
ds3d::impl::BaseFrame::at
EleT & at(size_t idx)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:78
ds3d::Frame2DPlane
Definition: sources/includes/ds3d/common/abi_frame.h:47
ds3d::impl::BaseFrame::dataType
DataType dataType() const final
Definition: sources/includes/ds3d/common/impl/impl_frames.h:31
ds3d::impl::BaseFrame::frameType
FrameType frameType() const final
Definition: sources/includes/ds3d/common/impl/impl_frames.h:32
ds3d::ErrCode::kParam
@ kParam
ds3d::impl::Frame2DBaseImpl::reset
void reset()
Definition: sources/includes/ds3d/common/impl/impl_frames.h:149
ds3d::impl::BaseFrame::isValid
bool isValid() const
Definition: sources/includes/ds3d/common/impl/impl_frames.h:39
ds3d::impl::BaseFrame< DataTypeTP, ft, abi2DFrame >::Deleter
std::function< void(void *)> Deleter
Definition: sources/includes/ds3d/common/impl/impl_frames.h:42
ds3d::impl::Frame2DBaseImpl::getPlane
const Frame2DPlane & getPlane(uint32_t idx) const final
Definition: sources/includes/ds3d/common/impl/impl_frames.h:121
ds3d::abiRefT
Definition: sources/includes/ds3d/common/abi_obj.h:43
ds3d::impl::Wrap2DFrame
Frame2DGuard Wrap2DFrame(void *data, const std::vector< Frame2DPlane > &planes, size_t bytes, MemType memType, uint64_t devId, std::function< void(void *)> deleter)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:192
DS_ASSERT
#define DS_ASSERT(...)
Definition: sources/includes/ds3d/common/defines.h:36
ds3d::impl::WrapFrame
FrameGuard WrapFrame(void *data, size_t bytes, const Shape &shape, MemType memType, uint64_t devId, std::function< void(void *)> &&deleter)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:180
ds3d::impl::BaseFrame::devId
int64_t devId() const final
Definition: sources/includes/ds3d/common/impl/impl_frames.h:38
ds3d::impl::wrapPointCoordUVFrame
FrameGuard wrapPointCoordUVFrame(void *data, uint32_t points, MemType memType, uint64_t devId, std::function< void(void *)> &&deleter)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:226
ds3d::GuardDataT
Definition: sources/includes/ds3d/common/hpp/obj.hpp:335
ds3d::__DataTypeTrait::_data_type
static constexpr DataType _data_type()
Definition: sources/includes/ds3d/common/type_trait.h:82
ds3d::impl::Frame2DBaseImpl::~Frame2DBaseImpl
~Frame2DBaseImpl()
Definition: sources/includes/ds3d/common/impl/impl_frames.h:147
ds3d::impl::Frame2DBaseImpl::planes
uint32_t planes() const final
Definition: sources/includes/ds3d/common/impl/impl_frames.h:120
ds3d::abi2DFrame
Definition: sources/includes/ds3d/common/abi_frame.h:56
ds3d::impl::BaseFrame
Definition: sources/includes/ds3d/common/impl/impl_frames.h:29
ds3d::impl::BaseFrame::base
void * base() const final
Definition: sources/includes/ds3d/common/impl/impl_frames.h:36
ds3d::impl::BaseFrame::shape
const Shape & shape() const final
Definition: sources/includes/ds3d/common/impl/impl_frames.h:37
ds3d::impl::Frame2DBaseImpl::setPlanes
void setPlanes(const std::vector< Frame2DPlane > &p)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:129
ds3d::impl::BaseFrame::BaseFrame
BaseFrame(void *data, size_t bytes, const Shape &shape, MemType memType, uint64_t devId, Deleter &&deleter=nullptr)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:68
ds3d::impl::BaseFrame::reset
void reset()
Definition: sources/includes/ds3d/common/impl/impl_frames.h:45
ds3d::impl::BaseFrame::setDeleter
void setDeleter(F &&f)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:86
ds3d::FrameType
FrameType
Definition: sources/includes/ds3d/common/idatatype.h:95
ds3d::impl::BaseFrame::resetShape
void resetShape(const Shape &s)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:43
ds3d::Shape
Definition: sources/includes/ds3d/common/idatatype.h:122
ds3d::impl::Frame2DBaseImpl::Frame2DBaseImpl
Frame2DBaseImpl(void *data, size_t bytes, const Shape &shape, MemType memType, uint64_t devId, Deleter &&deleter)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:112
ds3d::impl::BaseFrame::memType
MemType memType() const final
Definition: sources/includes/ds3d/common/impl/impl_frames.h:33
ds3d::impl::Frame2DBaseImpl::Frame2DBaseImpl
Frame2DBaseImpl(void *data, const std::vector< Frame2DPlane > &planes, size_t bytes, MemType memType, uint64_t devId, Deleter &&deleter)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:139
ds3d
Definition: sources/includes/ds3d/common/abi_dataprocess.h:25
ds3d::impl::Frame2DBaseImpl::at
EleT & at(size_t row, size_t column, uint32_t plane=0)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:156
ds3d::MemType
MemType
Definition: sources/includes/ds3d/common/idatatype.h:106
ds3d::throwError
void throwError(ErrCode code, const std::string &msg)
Definition: sources/includes/ds3d/common/func_utils.h:81
ds3d::impl::wrapLidarXYZIFrame
FrameGuard wrapLidarXYZIFrame(void *data, uint32_t points, MemType memType, uint64_t devId, std::function< void(void *)> &&deleter)
Definition: sources/includes/ds3d/common/impl/impl_frames.h:215
ds3d::MemType::kNone
@ kNone