NVIDIA DeepStream SDK API Reference

8.0 Release
buffer.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2025 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 
21 #ifndef DEEPSTREAM_BUFFER_HPP
22 #define DEEPSTREAM_BUFFER_HPP
23 
24 #include <vector>
25 #include <functional>
26 
27 #include "object.hpp"
28 #include "metadata.hpp"
29 #include "nvbufsurface.h"
30 
31 typedef struct _GstBuffer OpaqueBuffer;
32 
33 namespace deepstream {
34 
35 class Tensor;
36 
46 class Buffer {
47  public:
49  typedef struct _Latency {
51  unsigned int source_id;
53  unsigned int frame_num;
55  double latency;
56  } Latency;
57 
59  using FreeFunction = void(*)(void*);
60 
62  Buffer();
63 
74  Buffer(size_t length, void* data=nullptr, FreeFunction=nullptr);
75 
84  Buffer(const std::vector<uint8_t>);
85 
94  Buffer(OpaqueBuffer* buffer);
95 
97  Buffer(const Buffer&);
98 
100  Buffer(Buffer&&);
101 
103  Buffer& operator=(const Buffer&);
104 
106  Buffer& operator=(Buffer&&);
107 
109  virtual ~Buffer();
110 
112  operator bool() const;
113 
115  size_t size() const;
116 
118  uint64_t timestamp() const;
119 
121  int32_t chunkId(unsigned int batchId) const;
122 
124  std::vector<Latency> measureLatency() const;
125 
132  virtual size_t read(std::function<size_t(const void* data, size_t len)> callable);
133 
140  virtual size_t write(std::function<size_t(void* data, size_t len)> callable);
141 
143  OpaqueBuffer* give();
144 
149  virtual size_t batchSize();
150 
155  virtual Tensor* extract(unsigned int batchId);
156 
160  static void wrap(Tensor* );
161 
162  protected:
166  std::vector<int> chunk_ids_ = { 0 };
167 };
168 
169 class VideoBuffer : public Buffer {
170  public:
171  // NvSurface Buffer
172  VideoBuffer(size_t width, size_t height, NvBufSurfaceColorFormat video_format,
173  NvBufSurfaceMemType memtype, void* mem=nullptr, int gpu_id=0);
174 
175  // cast from a Buffer Object
176  VideoBuffer(const Buffer&);
177 
178  virtual size_t read(std::function<size_t(const void* data, size_t len)>);
179 
180  virtual size_t write(std::function<size_t(void* data, size_t len)>);
181 
183 
184  size_t width() const { return width_; }
185  size_t height() const { return height_; }
186  const NvBufSurfaceColorFormat format() const { return format_; }
187 
188  VideoBuffer clone() const;
189 
190  protected:
191  size_t width_ = 0;
192  size_t height_ = 0;
194 };
195 
196 typedef struct _NvDsSourceMeta
197 {
198  int chunk_id;
200 
201 } // namespace deepstream
202 
203 #endif
deepstream::VideoBuffer::getBatchMetadata
BatchMetadata getBatchMetadata()
deepstream::VideoBuffer::read
virtual size_t read(std::function< size_t(const void *data, size_t len)>)
deepstream::_NvDsSourceMeta
Definition: buffer.hpp:196
deepstream::Buffer::read
virtual size_t read(std::function< size_t(const void *data, size_t len)> callable)
Read data from the buffer.
deepstream::Buffer::Buffer
Buffer()
empty buffer constructor
deepstream::Buffer::operator=
Buffer & operator=(const Buffer &)
Copy assignment.
deepstream::VideoBuffer::width_
size_t width_
Definition: buffer.hpp:191
deepstream::Buffer::measureLatency
std::vector< Latency > measureLatency() const
Return latency data of the buffer.
deepstream::VideoBuffer::height
size_t height() const
Definition: buffer.hpp:185
deepstream::VideoBuffer::clone
VideoBuffer clone() const
deepstream::VideoBuffer::write
virtual size_t write(std::function< size_t(void *data, size_t len)>)
deepstream::NvDsSourceMeta
struct deepstream::_NvDsSourceMeta NvDsSourceMeta
deepstream::Buffer::_Latency::source_id
unsigned int source_id
Indicating the source from which the buffer is generated.
Definition: buffer.hpp:51
deepstream
Definition: buffer.hpp:33
deepstream::Buffer::size
size_t size() const
Size of the buffer in bytes.
deepstream::Buffer::~Buffer
virtual ~Buffer()
Destructor.
deepstream::BatchMetadata
Holds information about a formed batch containingframes from different sources.
Definition: metadata.hpp:652
deepstream::VideoBuffer::height_
size_t height_
Definition: buffer.hpp:192
OpaqueBuffer
struct _GstBuffer OpaqueBuffer
Definition: buffer.hpp:31
NvBufSurfaceMemType
NvBufSurfaceMemType
Specifies memory types for NvBufSurface.
Definition: nvbufsurface.h:332
deepstream::Buffer
Base class of a buffer.
Definition: buffer.hpp:46
deepstream::Buffer::wrap
static void wrap(Tensor *)
wrap the tensor to a new buffer
deepstream::Buffer::chunk_ids_
std::vector< int > chunk_ids_
chunk ids, optional
Definition: buffer.hpp:166
deepstream::VideoBuffer::format_
NvBufSurfaceColorFormat format_
Definition: buffer.hpp:193
deepstream::Buffer::_Latency
Unified definition of latency for a buffer.
Definition: buffer.hpp:49
deepstream::Buffer::chunkId
int32_t chunkId(unsigned int batchId) const
Chunk id of the buffer.
metadata.hpp
deepstream::Buffer::write
virtual size_t write(std::function< size_t(void *data, size_t len)> callable)
Write data to the buffer.
deepstream::Buffer::buffer_
OpaqueBuffer * buffer_
opaque buffer pointer
Definition: buffer.hpp:164
deepstream::Buffer::Latency
struct deepstream::Buffer::_Latency Latency
Unified definition of latency for a buffer.
deepstream::Buffer::extract
virtual Tensor * extract(unsigned int batchId)
create a tensor object with the buffer data
deepstream::Buffer::batchSize
virtual size_t batchSize()
get the batch size of the buffer, 1 for un-batched buffer
deepstream::Tensor
Definition: service-maker/includes/tensor.hpp:49
deepstream::_NvDsSourceMeta::chunk_id
int chunk_id
Definition: buffer.hpp:198
deepstream::Buffer::_Latency::frame_num
unsigned int frame_num
Indicating the frame to which the buffer belongs.
Definition: buffer.hpp:53
deepstream::Buffer::_Latency::latency
double latency
Latency data.
Definition: buffer.hpp:55
deepstream::VideoBuffer::format
const NvBufSurfaceColorFormat format() const
Definition: buffer.hpp:186
deepstream::VideoBuffer::VideoBuffer
VideoBuffer(size_t width, size_t height, NvBufSurfaceColorFormat video_format, NvBufSurfaceMemType memtype, void *mem=nullptr, int gpu_id=0)
deepstream::Buffer::FreeFunction
void(*)(void *) FreeFunction
Signature of customized function for freeing data in a buffer.
Definition: buffer.hpp:59
deepstream::VideoBuffer
Definition: buffer.hpp:169
object.hpp
nvbufsurface.h
deepstream::Buffer::give
OpaqueBuffer * give()
Give up the ownership of this buffer and return the opaque buffer pointer.
deepstream::VideoBuffer::width
size_t width() const
Definition: buffer.hpp:184
NvBufSurfaceColorFormat
NvBufSurfaceColorFormat
Defines color formats for NvBufSurface.
Definition: nvbufsurface.h:105
deepstream::Buffer::timestamp
uint64_t timestamp() const
Timestamp of the buffer.