NVIDIA DeepStream SDK API Reference

6.4 Release
gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp
Go to the documentation of this file.
1 
23 #ifndef __NVDSCUSTOMLIB_BASE_HPP__
24 #define __NVDSCUSTOMLIB_BASE_HPP__
25 
26 #include <gst/base/gstbasetransform.h>
27 #include <gst/video/video.h>
28 
29 #include "gstnvdsbufferpool.h"
31 
32 /* Buffer Pool Configuration Parameters */
33 struct BufferPoolConfig {
34  gint cuda_mem_type;
35  guint gpu_id;
36  guint max_buffers;
37  gint batch_size;
38 };
39 
41 {
42 public:
43  explicit DSCustomLibraryBase(GstBaseTransform* btrans = nullptr);
44 
45  /* Set Init Parameters */
46  virtual bool SetInitParams(DSCustom_CreateParams *params);
47 
48  virtual ~DSCustomLibraryBase();
49 
50  /* Set Custom Properties of the library */
51  virtual bool SetProperty(Property &prop) = 0;
52 
53  virtual bool HandleEvent (GstEvent *event) = 0;
54  // TODO: Add getProperty as well
55 
56  virtual char* QueryProperties () = 0;
57 
58  /* Get GetCompatibleOutputCaps */
59  virtual GstCaps* GetCompatibleCaps (GstPadDirection direction,
60  GstCaps* in_caps, GstCaps* othercaps);
61 
62  /* Process Incoming Buffer */
63  virtual BufferResult ProcessBuffer(GstBuffer *inbuf) = 0;
64 
65  /* Helped function to get the NvBufSurface from the GstBuffer */
67 
68  /* Helper function to create the custom buffer pool */
69  GstBufferPool* CreateBufferPool (BufferPoolConfig *pool_config, GstCaps *outcaps);
70 
71 public:
72  /* Gstreamer dsexaple2 plugin's base class reference */
73  GstBaseTransform *m_element;
74 
76  guint m_gpuId;
77 
79 
81 
82  /* Video Information */
83  GstVideoInfo m_inVideoInfo;
84  GstVideoInfo m_outVideoInfo;
85 
86  /* Video Format Information */
87  GstVideoFormat m_inVideoFmt;
88  GstVideoFormat m_outVideoFmt;
89 
90  /* Gst Caps Information */
91  GstCaps *m_inCaps;
92  GstCaps *m_outCaps;
93 };
94 
95 
96 DSCustomLibraryBase::DSCustomLibraryBase(GstBaseTransform* btrans) : m_element(btrans)
97 {
98  m_inCaps = NULL;
99  m_outCaps = NULL;
100  m_gpuId = 0;
101  m_dummyMetaInsert = false;
102  m_fillDummyBatchMeta = false;
103 }
104 
106  m_element = params->m_element;
107  m_inCaps = params->m_inCaps;
108  m_outCaps = params->m_outCaps;
109  m_gpuId = params->m_gpuId;
112 
113  gst_video_info_from_caps(&m_inVideoInfo, m_inCaps);
114  gst_video_info_from_caps(&m_outVideoInfo, m_outCaps);
115 
116  m_inVideoFmt = GST_VIDEO_FORMAT_INFO_FORMAT (m_inVideoInfo.finfo);
117  m_outVideoFmt = GST_VIDEO_FORMAT_INFO_FORMAT (m_outVideoInfo.finfo);
118 
119  return true;
120 }
121 
123 }
124 
125 GstCaps* DSCustomLibraryBase::GetCompatibleCaps (GstPadDirection direction,
126  GstCaps* in_caps, GstCaps* othercaps)
127 {
128  GstCaps* result = NULL;
129  GstStructure *s1, *s2;
130  gint width = 0, height = 0;
131  gint num = 0, denom = 0;
132  const gchar *inputFmt = NULL;
133 
134  GST_INFO_OBJECT (m_element, "\n----------\ndirection = %d (1=Src, 2=Sink) -> %s:\nCAPS = %s\n",
135  direction, __func__, gst_caps_to_string(in_caps));
136  GST_INFO_OBJECT (m_element, "%s : OTHERCAPS = %s\n", __func__, gst_caps_to_string(othercaps));
137 
138 #if 0
139  GST_INFO_OBJECT (nvdsvideotemplate, "%s : CAPS = %" GST_PTR_FORMAT "\n\n", __func__, in_caps);
140  GST_INFO_OBJECT (nvdsvideotemplate, "%s : OTHER CAPS = %" GST_PTR_FORMAT "\n\n", __func__, othercaps);
141 #endif
142 
143  othercaps = gst_caps_truncate(othercaps);
144  othercaps = gst_caps_make_writable(othercaps);
145 
146  // TODO:
147  // Currently selecting only first caps structure
148  {
149  s1 = gst_caps_get_structure(in_caps, 0);
150  s2 = gst_caps_get_structure(othercaps, 0);
151 
152  inputFmt = gst_structure_get_string (s1, "format");
153  gst_structure_get_int (s1, "width", &width);
154  gst_structure_get_int (s1, "height", &height);
155 
156  if (0)
157  g_print ("InputFMT = %s \n\n", inputFmt);
158 
159  /* otherwise the dimension of the output heatmap needs to be fixated */
160 
161  // Here change the width and height on output caps based on the information provided
162  // byt the custom library
163  gst_structure_fixate_field_nearest_int(s2, "width", width);
164  gst_structure_fixate_field_nearest_int(s2, "height", height);
165  if (gst_structure_get_fraction(s1, "framerate", &num, &denom))
166  {
167  gst_structure_fixate_field_nearest_fraction(s2, "framerate", num, denom);
168  }
169 
170  gst_structure_remove_fields (s2, "width", "height", "format", NULL);
171 
172  // TODO: Get width, height, coloutformat, and framerate from customlibrary API
173  // set the new properties accordingly
174  gst_structure_set (s2, "width", G_TYPE_INT, width,
175  "height", G_TYPE_INT, height,
176  "format", G_TYPE_STRING, "NV12",
177  NULL);
178 
179  result = gst_caps_ref(othercaps);
180  }
181 
182  gst_caps_unref(othercaps);
183 
184  GST_INFO_OBJECT (m_element, "%s : Updated OTHERCAPS = %s \n\n", __func__, gst_caps_to_string(othercaps));
185 #if 0
186  GST_INFO_OBJECT (nvdsvideotemplate, "%s : CAPS = %" GST_PTR_FORMAT "\n\n", __func__, othercaps);
187  GST_INFO_OBJECT(nvdsvideotemplate, "CAPS fixate: %" GST_PTR_FORMAT ", direction %d",
188  result, direction);
189 #endif
190  return result;
191 }
192 
194  (BufferPoolConfig *pool_config, GstCaps *outcaps)
195 {
196  GstBufferPool *m_buf_pool= NULL;
197  GstStructure *config = NULL;
198 
199  m_buf_pool = gst_nvds_buffer_pool_new ();
200 
201  config = gst_buffer_pool_get_config (m_buf_pool);
202 
203  GST_INFO_OBJECT (m_element, "in videoconvert caps = %" GST_PTR_FORMAT "\n", outcaps);
204  gst_buffer_pool_config_set_params (config, outcaps, sizeof (NvBufSurface), pool_config->max_buffers, pool_config->max_buffers+4);
205 
206  gst_structure_set (config,
207  "memtype", G_TYPE_UINT, pool_config->cuda_mem_type,
208  "gpu-id", G_TYPE_UINT, pool_config->gpu_id,
209  "batch-size", G_TYPE_UINT, pool_config->batch_size, NULL);
210 
211  GST_INFO_OBJECT (m_element, " %s Allocating Buffers in NVM Buffer Pool for Max_Views=%d\n",
212  __func__, pool_config->batch_size);
213 
214  /* set config for the created buffer pool */
215  if (!gst_buffer_pool_set_config (m_buf_pool, config)) {
216  GST_WARNING ("bufferpool configuration failed");
217  return NULL;
218  }
219 
220  gboolean is_active = gst_buffer_pool_set_active (m_buf_pool, TRUE);
221  if (!is_active) {
222  GST_WARNING (" Failed to allocate the buffers inside the output pool");
223  return NULL;
224  } else {
225  GST_DEBUG (" Output buffer pool (%p) successfully created with %d buffers",
226  m_buf_pool, pool_config->max_buffers);
227  }
228  return m_buf_pool;
229 }
230 
231 /* Helped function to get the NvBufSurface from the GstBuffer */
233 {
234  GstMapInfo in_map_info;
235  NvBufSurface *nvbuf_surface = NULL;
236 
237  /* Map the buffer contents and get the pointer to NvBufSurface. */
238  if (!gst_buffer_map (inbuf, &in_map_info, GST_MAP_READ)) {
239  GST_ELEMENT_ERROR (m_element, STREAM, FAILED,
240  ("%s:gst buffer map to get pointer to NvBufSurface failed", __func__), (NULL));
241  return NULL;
242  }
243 
244  // Assuming that the plugin uses DS NvBufSurface data structure
245  nvbuf_surface = (NvBufSurface *) in_map_info.data;
246 
247  gst_buffer_unmap(inbuf, &in_map_info);
248  return nvbuf_surface;
249 }
250 
251 #endif
DSCustomLibraryBase::ProcessBuffer
virtual BufferResult ProcessBuffer(GstBuffer *inbuf)=0
DSCustom_CreateParams
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:39
DSCustomLibraryBase::m_outCaps
GstCaps * m_outCaps
Definition: nvdscustomlib_base.h:80
NvBufSurface
Holds information about batched buffers.
Definition: nvbufsurface.h:503
BufferPoolConfig::batch_size
gint batch_size
Definition: nvdscustomlib_base.h:36
BufferResult
BufferResult
Copyright (c) 2023, NVIDIA CORPORATION.
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:32
DSCustomLibraryBase::m_element
GstBaseTransform * m_element
Definition: nvdscustomlib_base.h:65
DSCustom_CreateParams::m_element
GstElement * m_element
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:40
DSCustomLibraryBase::CreateBufferPool
GstBufferPool * CreateBufferPool(BufferPoolConfig *pool_config, GstCaps *outcaps)
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:194
DSCustomLibraryBase::SetProperty
virtual bool SetProperty(Property &prop)=0
DSCustomLibraryBase::QueryProperties
virtual char * QueryProperties()=0
DSCustomLibraryBase::m_inCaps
GstCaps * m_inCaps
Definition: nvdscustomlib_base.h:79
IDSCustomLibrary
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:53
DSCustom_CreateParams::m_inCaps
GstCaps * m_inCaps
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_interface.hpp:38
BufferPoolConfig
Copyright (c) 2021, NVIDIA CORPORATION.
Definition: nvdscustomlib_base.h:32
DSCustomLibraryBase::~DSCustomLibraryBase
virtual ~DSCustomLibraryBase()
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_base.hpp:75
DSCustomLibraryBase::GetCompatibleCaps
virtual GstCaps * GetCompatibleCaps(GstPadDirection direction, GstCaps *in_caps, GstCaps *othercaps)
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:125
Property
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:43
DSCustom_CreateParams::m_outCaps
GstCaps * m_outCaps
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_interface.hpp:39
DSCustom_CreateParams::m_gpuId
guint m_gpuId
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_interface.hpp:40
gst_nvds_buffer_pool_new
GstBufferPool * gst_nvds_buffer_pool_new(void)
DSCustomLibraryBase::DSCustomLibraryBase
DSCustomLibraryBase(GstBaseTransform *btrans=nullptr)
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:96
DSCustomLibraryBase::m_outVideoInfo
GstVideoInfo m_outVideoInfo
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:84
BufferPoolConfig::max_buffers
guint max_buffers
Definition: nvdscustomlib_base.h:35
BufferPoolConfig::cuda_mem_type
gint cuda_mem_type
Definition: nvdscustomlib_base.h:33
DSCustomLibraryBase::getNvBufSurface
NvBufSurface * getNvBufSurface(GstBuffer *inbuf)
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:232
DSCustom_CreateParams::m_fillDummyBatchMeta
gboolean m_fillDummyBatchMeta
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_interface.hpp:43
BufferPoolConfig::gpu_id
guint gpu_id
Definition: nvdscustomlib_base.h:34
DSCustomLibraryBase::m_dummyMetaInsert
gboolean m_dummyMetaInsert
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:78
DSCustomLibraryBase::HandleEvent
virtual bool HandleEvent(GstEvent *event)=0
DSCustomLibraryBase::m_gpuId
guint m_gpuId
GPU ID on which we expect to execute the algorithm.
Definition: nvdscustomlib_base.h:68
DSCustomLibraryBase::m_inVideoInfo
GstVideoInfo m_inVideoInfo
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:83
GstBuffer
struct _GstBuffer GstBuffer
Definition: idatatype.h:19
nvdscustomlib_interface.hpp
DSCustomLibraryBase::m_outVideoFmt
GstVideoFormat m_outVideoFmt
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:88
DSCustomLibraryBase::SetInitParams
virtual bool SetInitParams(DSCustom_CreateParams *params)
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_base.hpp:69
DSCustomLibraryBase::m_inVideoFmt
GstVideoFormat m_inVideoFmt
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:87
gstnvdsbufferpool.h
DSCustomLibraryBase::m_fillDummyBatchMeta
gboolean m_fillDummyBatchMeta
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:80
DSCustom_CreateParams::m_dummyMetaInsert
gboolean m_dummyMetaInsert
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_interface.hpp:42
DSCustomLibraryBase
Definition: nvdscustomlib_base.h:39