NVIDIA DeepStream SDK API Reference

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