NVIDIA DeepStream SDK API Reference

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