NVIDIA DeepStream SDK API Reference

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