NVIDIA DeepStream SDK API Reference

7.0 Release
gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef __NVDSCUSTOMLIB_BASE_HPP__
25 #define __NVDSCUSTOMLIB_BASE_HPP__
26 
27 #include <gst/base/gstbasetransform.h>
28 #include <gst/video/video.h>
29 
30 #include "gstnvdsbufferpool.h"
32 
33 /* Buffer Pool Configuration Parameters */
34 struct BufferPoolConfig {
35  gint cuda_mem_type;
36  guint gpu_id;
37  guint max_buffers;
38  gint batch_size;
39 };
40 
42 {
43 public:
44  explicit DSCustomLibraryBase(GstBaseTransform* btrans = nullptr);
45 
46  /* Set Init Parameters */
47  virtual bool SetInitParams(DSCustom_CreateParams *params);
48 
49  virtual ~DSCustomLibraryBase();
50 
51  /* Set Custom Properties of the library */
52  virtual bool SetProperty(Property &prop) = 0;
53 
54  virtual bool HandleEvent (GstEvent *event) = 0;
55  // TODO: Add getProperty as well
56 
57  virtual char* QueryProperties () = 0;
58 
59  /* Get GetCompatibleOutputCaps */
60  virtual GstCaps* GetCompatibleCaps (GstPadDirection direction,
61  GstCaps* in_caps, GstCaps* othercaps);
62 
63  /* Process Incoming Buffer */
64  virtual BufferResult ProcessBuffer(GstBuffer *inbuf) = 0;
65 
66  /* Helped function to get the NvBufSurface from the GstBuffer */
68 
69  /* Helper function to create the custom buffer pool */
70  GstBufferPool* CreateBufferPool (BufferPoolConfig *pool_config, GstCaps *outcaps);
71 
72 public:
73  /* Gstreamer dsexaple2 plugin's base class reference */
74  GstBaseTransform *m_element;
75 
77  guint m_gpuId;
78 
80 
82 
83  /* Video Information */
84  GstVideoInfo m_inVideoInfo;
85  GstVideoInfo m_outVideoInfo;
86 
87  /* Video Format Information */
88  GstVideoFormat m_inVideoFmt;
89  GstVideoFormat m_outVideoFmt;
90 
91  /* Gst Caps Information */
92  GstCaps *m_inCaps;
93  GstCaps *m_outCaps;
94 };
95 
96 
97 DSCustomLibraryBase::DSCustomLibraryBase(GstBaseTransform* btrans) : m_element(btrans)
98 {
99  m_inCaps = NULL;
100  m_outCaps = NULL;
101  m_gpuId = 0;
102  m_dummyMetaInsert = false;
103  m_fillDummyBatchMeta = false;
104 }
105 
107  m_element = params->m_element;
108  m_inCaps = params->m_inCaps;
109  m_outCaps = params->m_outCaps;
110  m_gpuId = params->m_gpuId;
113 
114  gst_video_info_from_caps(&m_inVideoInfo, m_inCaps);
115  gst_video_info_from_caps(&m_outVideoInfo, m_outCaps);
116 
117  m_inVideoFmt = GST_VIDEO_FORMAT_INFO_FORMAT (m_inVideoInfo.finfo);
118  m_outVideoFmt = GST_VIDEO_FORMAT_INFO_FORMAT (m_outVideoInfo.finfo);
119 
120  return true;
121 }
122 
124 }
125 
126 GstCaps* DSCustomLibraryBase::GetCompatibleCaps (GstPadDirection direction,
127  GstCaps* in_caps, GstCaps* othercaps)
128 {
129  GstCaps* result = NULL;
130  GstStructure *s1, *s2;
131  gint width = 0, height = 0;
132  gint num = 0, denom = 0;
133  const gchar *inputFmt = NULL;
134 
135  GST_INFO_OBJECT (m_element, "\n----------\ndirection = %d (1=Src, 2=Sink) -> %s:\nCAPS = %s\n",
136  direction, __func__, gst_caps_to_string(in_caps));
137  GST_INFO_OBJECT (m_element, "%s : OTHERCAPS = %s\n", __func__, gst_caps_to_string(othercaps));
138 
139 #if 0
140  GST_INFO_OBJECT (nvdsvideotemplate, "%s : CAPS = %" GST_PTR_FORMAT "\n\n", __func__, in_caps);
141  GST_INFO_OBJECT (nvdsvideotemplate, "%s : OTHER CAPS = %" GST_PTR_FORMAT "\n\n", __func__, othercaps);
142 #endif
143 
144  othercaps = gst_caps_truncate(othercaps);
145  othercaps = gst_caps_make_writable(othercaps);
146 
147  // TODO:
148  // Currently selecting only first caps structure
149  {
150  s1 = gst_caps_get_structure(in_caps, 0);
151  s2 = gst_caps_get_structure(othercaps, 0);
152 
153  inputFmt = gst_structure_get_string (s1, "format");
154  gst_structure_get_int (s1, "width", &width);
155  gst_structure_get_int (s1, "height", &height);
156 
157  if (0)
158  g_print ("InputFMT = %s \n\n", inputFmt);
159 
160  /* otherwise the dimension of the output heatmap needs to be fixated */
161 
162  // Here change the width and height on output caps based on the information provided
163  // byt the custom library
164  gst_structure_fixate_field_nearest_int(s2, "width", width);
165  gst_structure_fixate_field_nearest_int(s2, "height", height);
166  if (gst_structure_get_fraction(s1, "framerate", &num, &denom))
167  {
168  gst_structure_fixate_field_nearest_fraction(s2, "framerate", num, denom);
169  }
170 
171  gst_structure_remove_fields (s2, "width", "height", "format", NULL);
172 
173  // TODO: Get width, height, coloutformat, and framerate from customlibrary API
174  // set the new properties accordingly
175  gst_structure_set (s2, "width", G_TYPE_INT, width,
176  "height", G_TYPE_INT, height,
177  "format", G_TYPE_STRING, "NV12",
178  NULL);
179 
180  result = gst_caps_ref(othercaps);
181  }
182 
183  gst_caps_unref(othercaps);
184 
185  GST_INFO_OBJECT (m_element, "%s : Updated OTHERCAPS = %s \n\n", __func__, gst_caps_to_string(othercaps));
186 #if 0
187  GST_INFO_OBJECT (nvdsvideotemplate, "%s : CAPS = %" GST_PTR_FORMAT "\n\n", __func__, othercaps);
188  GST_INFO_OBJECT(nvdsvideotemplate, "CAPS fixate: %" GST_PTR_FORMAT ", direction %d",
189  result, direction);
190 #endif
191  return result;
192 }
193 
195  (BufferPoolConfig *pool_config, GstCaps *outcaps)
196 {
197  GstBufferPool *m_buf_pool= NULL;
198  GstStructure *config = NULL;
199 
200  m_buf_pool = gst_nvds_buffer_pool_new ();
201 
202  config = gst_buffer_pool_get_config (m_buf_pool);
203 
204  GST_INFO_OBJECT (m_element, "in videoconvert caps = %" GST_PTR_FORMAT "\n", outcaps);
205  gst_buffer_pool_config_set_params (config, outcaps, sizeof (NvBufSurface), pool_config->max_buffers, pool_config->max_buffers+4);
206 
207  gst_structure_set (config,
208  "memtype", G_TYPE_UINT, pool_config->cuda_mem_type,
209  "gpu-id", G_TYPE_UINT, pool_config->gpu_id,
210  "batch-size", G_TYPE_UINT, pool_config->batch_size, NULL);
211 
212  GST_INFO_OBJECT (m_element, " %s Allocating Buffers in NVM Buffer Pool for Max_Views=%d\n",
213  __func__, pool_config->batch_size);
214 
215  /* set config for the created buffer pool */
216  if (!gst_buffer_pool_set_config (m_buf_pool, config)) {
217  GST_WARNING ("bufferpool configuration failed");
218  return NULL;
219  }
220 
221  gboolean is_active = gst_buffer_pool_set_active (m_buf_pool, TRUE);
222  if (!is_active) {
223  GST_WARNING (" Failed to allocate the buffers inside the output pool");
224  return NULL;
225  } else {
226  GST_DEBUG (" Output buffer pool (%p) successfully created with %d buffers",
227  m_buf_pool, pool_config->max_buffers);
228  }
229  return m_buf_pool;
230 }
231 
232 /* Helped function to get the NvBufSurface from the GstBuffer */
234 {
235  GstMapInfo in_map_info;
236  NvBufSurface *nvbuf_surface = NULL;
237 
238  /* Map the buffer contents and get the pointer to NvBufSurface. */
239  if (!gst_buffer_map (inbuf, &in_map_info, GST_MAP_READ)) {
240  GST_ELEMENT_ERROR (m_element, STREAM, FAILED,
241  ("%s:gst buffer map to get pointer to NvBufSurface failed", __func__), (NULL));
242  return NULL;
243  }
244 
245  // Assuming that the plugin uses DS NvBufSurface data structure
246  nvbuf_surface = (NvBufSurface *) in_map_info.data;
247 
248  gst_buffer_unmap(inbuf, &in_map_info);
249  return nvbuf_surface;
250 }
251 
252 #endif
DSCustomLibraryBase::ProcessBuffer
virtual BufferResult ProcessBuffer(GstBuffer *inbuf)=0
DSCustom_CreateParams
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:29
DSCustomLibraryBase::m_outCaps
GstCaps * m_outCaps
Definition: nvdscustomlib_base.h:81
NvBufSurface
Holds information about batched buffers.
Definition: nvbufsurface.h:509
BufferPoolConfig::batch_size
gint batch_size
Definition: nvdscustomlib_base.h:37
BufferResult
BufferResult
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:22
DSCustomLibraryBase::m_element
GstBaseTransform * m_element
Definition: nvdscustomlib_base.h:66
DSCustom_CreateParams::m_element
GstElement * m_element
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:30
DSCustomLibraryBase::CreateBufferPool
GstBufferPool * CreateBufferPool(BufferPoolConfig *pool_config, GstCaps *outcaps)
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:195
DSCustomLibraryBase::SetProperty
virtual bool SetProperty(Property &prop)=0
DSCustomLibraryBase::QueryProperties
virtual char * QueryProperties()=0
DSCustomLibraryBase::m_inCaps
GstCaps * m_inCaps
Definition: nvdscustomlib_base.h:80
IDSCustomLibrary
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:43
DSCustom_CreateParams::m_inCaps
GstCaps * m_inCaps
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_interface.hpp:39
BufferPoolConfig
Definition: nvdscustomlib_base.h:33
DSCustomLibraryBase::~DSCustomLibraryBase
virtual ~DSCustomLibraryBase()
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_base.hpp:65
DSCustomLibraryBase::GetCompatibleCaps
virtual GstCaps * GetCompatibleCaps(GstPadDirection direction, GstCaps *in_caps, GstCaps *othercaps)
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:126
Property
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:33
DSCustom_CreateParams::m_outCaps
GstCaps * m_outCaps
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_interface.hpp:40
DSCustom_CreateParams::m_gpuId
guint m_gpuId
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_interface.hpp:41
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:97
DSCustomLibraryBase::m_outVideoInfo
GstVideoInfo m_outVideoInfo
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:85
BufferPoolConfig::max_buffers
guint max_buffers
Definition: nvdscustomlib_base.h:36
BufferPoolConfig::cuda_mem_type
gint cuda_mem_type
Definition: nvdscustomlib_base.h:34
DSCustomLibraryBase::getNvBufSurface
NvBufSurface * getNvBufSurface(GstBuffer *inbuf)
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:233
DSCustom_CreateParams::m_fillDummyBatchMeta
gboolean m_fillDummyBatchMeta
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_interface.hpp:44
BufferPoolConfig::gpu_id
guint gpu_id
Definition: nvdscustomlib_base.h:35
DSCustomLibraryBase::m_dummyMetaInsert
gboolean m_dummyMetaInsert
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:79
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:69
DSCustomLibraryBase::m_inVideoInfo
GstVideoInfo m_inVideoInfo
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:84
GstBuffer
struct _GstBuffer GstBuffer
Definition: idatatype.h:19
nvdscustomlib_interface.hpp
DSCustomLibraryBase::m_outVideoFmt
GstVideoFormat m_outVideoFmt
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:89
DSCustomLibraryBase::SetInitParams
virtual bool SetInitParams(DSCustom_CreateParams *params)
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_base.hpp:59
DSCustomLibraryBase::m_inVideoFmt
GstVideoFormat m_inVideoFmt
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:88
gstnvdsbufferpool.h
DSCustomLibraryBase::m_fillDummyBatchMeta
gboolean m_fillDummyBatchMeta
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_base.hpp:81
DSCustom_CreateParams::m_dummyMetaInsert
gboolean m_dummyMetaInsert
Definition: gst-nvdsvideotemplate/includes/nvdscustomlib_interface.hpp:43
DSCustomLibraryBase
Definition: nvdscustomlib_base.h:40