• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2005-2012 David Schleef <ds@schleef.org>
4  * Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 /**
23  * GstCudaBaseFilter:
24  *
25  * Base class for CUDA filters
26  *
27  * Since: 1.20
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #  include <config.h>
32 #endif
33 
34 #include "gstcudabasefilter.h"
35 #include "gstcudautils.h"
36 #include <string.h>
37 
38 GST_DEBUG_CATEGORY_STATIC (gst_cuda_base_filter_debug);
39 #define GST_CAT_DEFAULT gst_cuda_base_filter_debug
40 
41 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
45         (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY, GST_CUDA_CONVERTER_FORMATS))
46     );
47 
48 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
52         (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY, GST_CUDA_CONVERTER_FORMATS))
53     );
54 
55 #define gst_cuda_base_filter_parent_class parent_class
56 G_DEFINE_ABSTRACT_TYPE (GstCudaBaseFilter,
57     gst_cuda_base_filter, GST_TYPE_CUDA_BASE_TRANSFORM);
58 
59 static void gst_cuda_base_filter_dispose (GObject * object);
60 static GstFlowReturn
61 gst_cuda_base_filter_transform_frame (GstCudaBaseTransform * btrans,
62     GstVideoFrame * in_frame, GstCudaMemory * in_cuda_mem,
63     GstVideoFrame * out_frame, GstCudaMemory * out_cuda_mem);
64 static gboolean gst_cuda_base_filter_set_info (GstCudaBaseTransform * btrans,
65     GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
66     GstVideoInfo * out_info);
67 
68 static void
gst_cuda_base_filter_class_init(GstCudaBaseFilterClass * klass)69 gst_cuda_base_filter_class_init (GstCudaBaseFilterClass * klass)
70 {
71   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
72   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
73   GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
74   GstCudaBaseTransformClass *btrans_class =
75       GST_CUDA_BASE_TRANSFORM_CLASS (klass);
76 
77   gobject_class->dispose = gst_cuda_base_filter_dispose;
78 
79   gst_element_class_add_static_pad_template (element_class, &sink_template);
80   gst_element_class_add_static_pad_template (element_class, &src_template);
81 
82   trans_class->passthrough_on_same_caps = TRUE;
83 
84   btrans_class->set_info = GST_DEBUG_FUNCPTR (gst_cuda_base_filter_set_info);
85   btrans_class->transform_frame =
86       GST_DEBUG_FUNCPTR (gst_cuda_base_filter_transform_frame);
87 
88   GST_DEBUG_CATEGORY_INIT (gst_cuda_base_filter_debug,
89       "cudabasefilter", 0, "CUDA Base Filter");
90 }
91 
92 static void
gst_cuda_base_filter_init(GstCudaBaseFilter * convert)93 gst_cuda_base_filter_init (GstCudaBaseFilter * convert)
94 {
95 }
96 
97 static void
gst_cuda_base_filter_dispose(GObject * object)98 gst_cuda_base_filter_dispose (GObject * object)
99 {
100   GstCudaBaseFilter *filter = GST_CUDA_BASE_FILTER (object);
101 
102   if (filter->converter) {
103     gst_cuda_converter_free (filter->converter);
104     filter->converter = NULL;
105   }
106 
107   if (filter->in_fallback) {
108     gst_memory_unref (GST_MEMORY_CAST (filter->in_fallback));
109     filter->in_fallback = NULL;
110   }
111 
112   if (filter->out_fallback) {
113     gst_memory_unref (GST_MEMORY_CAST (filter->out_fallback));
114     filter->out_fallback = NULL;
115   }
116 
117   gst_clear_object (&filter->allocator);
118 
119   G_OBJECT_CLASS (parent_class)->dispose (object);
120 }
121 
122 static gboolean
gst_cuda_base_filter_configure(GstCudaBaseFilter * filter,GstVideoInfo * in_info,GstVideoInfo * out_info)123 gst_cuda_base_filter_configure (GstCudaBaseFilter * filter,
124     GstVideoInfo * in_info, GstVideoInfo * out_info)
125 {
126   GstCudaBaseTransform *btrans = GST_CUDA_BASE_TRANSFORM (filter);
127 
128   /* cleanup internal pool */
129   if (filter->in_fallback) {
130     gst_memory_unref (GST_MEMORY_CAST (filter->in_fallback));
131     filter->in_fallback = NULL;
132   }
133 
134   if (filter->out_fallback) {
135     gst_memory_unref (GST_MEMORY_CAST (filter->out_fallback));
136     filter->out_fallback = NULL;
137   }
138 
139   if (!filter->allocator)
140     filter->allocator = gst_cuda_allocator_new (btrans->context);
141 
142   if (!filter->allocator) {
143     GST_ERROR_OBJECT (filter, "Failed to create CUDA allocator");
144     return FALSE;
145   }
146 
147   return TRUE;
148 }
149 
150 static gboolean
gst_cuda_base_filter_set_info(GstCudaBaseTransform * btrans,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)151 gst_cuda_base_filter_set_info (GstCudaBaseTransform * btrans, GstCaps * incaps,
152     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
153 {
154   GstCudaBaseFilter *filter = GST_CUDA_BASE_FILTER (btrans);
155 
156   if (!gst_cuda_base_filter_configure (filter, in_info, out_info)) {
157     return FALSE;
158   }
159 
160   if (filter->converter)
161     gst_cuda_converter_free (filter->converter);
162 
163   filter->converter =
164       gst_cuda_converter_new (in_info, out_info, btrans->context);
165 
166   if (filter->converter == NULL)
167     goto no_converter;
168 
169   GST_DEBUG_OBJECT (filter, "reconfigured %d %d",
170       GST_VIDEO_INFO_FORMAT (in_info), GST_VIDEO_INFO_FORMAT (out_info));
171 
172   return TRUE;
173 
174 no_converter:
175   {
176     GST_ERROR_OBJECT (filter, "could not create converter");
177     return FALSE;
178   }
179 }
180 
181 static GstFlowReturn
gst_cuda_base_filter_transform_frame(GstCudaBaseTransform * btrans,GstVideoFrame * in_frame,GstCudaMemory * in_cuda_mem,GstVideoFrame * out_frame,GstCudaMemory * out_cuda_mem)182 gst_cuda_base_filter_transform_frame (GstCudaBaseTransform * btrans,
183     GstVideoFrame * in_frame, GstCudaMemory * in_cuda_mem,
184     GstVideoFrame * out_frame, GstCudaMemory * out_cuda_mem)
185 {
186   GstCudaBaseFilter *filter = GST_CUDA_BASE_FILTER (btrans);
187   gboolean conv_ret;
188   GstCudaMemory *in_mem;
189   GstCudaMemory *out_mem;
190   gint i;
191 
192   if (in_cuda_mem) {
193     in_mem = in_cuda_mem;
194   } else {
195     if (!filter->in_fallback) {
196       GstCudaAllocationParams params;
197 
198       memset (&params, 0, sizeof (GstCudaAllocationParams));
199       params.info = btrans->in_info;
200 
201       filter->in_fallback =
202           (GstCudaMemory *) gst_cuda_allocator_alloc (filter->allocator,
203           GST_VIDEO_INFO_SIZE (&params.info), &params);
204     }
205 
206     if (!filter->in_fallback) {
207       GST_ERROR_OBJECT (filter, "Couldn't allocate fallback memory");
208       return GST_FLOW_ERROR;
209     }
210 
211     GST_TRACE_OBJECT (filter, "use CUDA fallback memory input");
212 
213     if (!gst_cuda_context_push (btrans->context)) {
214       GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
215           ("Cannot push CUDA context"));
216       return FALSE;
217     }
218 
219     /* upload frame to device memory */
220     for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (in_frame); i++) {
221       CUDA_MEMCPY2D param = { 0, };
222       guint width, height;
223 
224       width = GST_VIDEO_FRAME_COMP_WIDTH (in_frame, i) *
225           GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, i);
226       height = GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, i);
227 
228       param.srcMemoryType = CU_MEMORYTYPE_HOST;
229       param.srcPitch = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, i);
230       param.srcHost = GST_VIDEO_FRAME_PLANE_DATA (in_frame, i);
231       param.dstMemoryType = CU_MEMORYTYPE_DEVICE;
232       param.dstPitch = filter->in_fallback->stride;
233       param.dstDevice =
234           filter->in_fallback->data + filter->in_fallback->offset[i];
235       param.WidthInBytes = width;
236       param.Height = height;
237 
238       if (!gst_cuda_result (CuMemcpy2DAsync (&param, btrans->cuda_stream))) {
239         gst_cuda_context_pop (NULL);
240         GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
241             ("Cannot upload input video frame"));
242         return GST_FLOW_ERROR;
243       }
244     }
245 
246     gst_cuda_result (CuStreamSynchronize (btrans->cuda_stream));
247     gst_cuda_context_pop (NULL);
248 
249     in_mem = filter->in_fallback;
250   }
251 
252   if (out_cuda_mem) {
253     out_mem = out_cuda_mem;
254   } else {
255     if (!filter->out_fallback) {
256       GstCudaAllocationParams params;
257 
258       memset (&params, 0, sizeof (GstCudaAllocationParams));
259       params.info = btrans->out_info;
260 
261       filter->out_fallback =
262           (GstCudaMemory *) gst_cuda_allocator_alloc (filter->allocator,
263           GST_VIDEO_INFO_SIZE (&params.info), &params);
264     }
265 
266     if (!filter->out_fallback) {
267       GST_ERROR_OBJECT (filter, "Couldn't allocate fallback memory");
268       return GST_FLOW_ERROR;
269     }
270 
271     out_mem = filter->out_fallback;
272   }
273 
274   conv_ret =
275       gst_cuda_converter_frame (filter->converter, in_mem, &btrans->in_info,
276       out_mem, &btrans->out_info, btrans->cuda_stream);
277 
278   if (!conv_ret) {
279     GST_ERROR_OBJECT (filter, "Failed to convert frame");
280     return GST_FLOW_ERROR;
281   }
282 
283   if (!out_cuda_mem) {
284     if (!gst_cuda_context_push (btrans->context)) {
285       GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
286           ("Cannot push CUDA context"));
287       return FALSE;
288     }
289 
290     for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (out_frame); i++) {
291       CUDA_MEMCPY2D param = { 0, };
292       guint width, height;
293 
294       width = GST_VIDEO_FRAME_COMP_WIDTH (out_frame, i) *
295           GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, i);
296       height = GST_VIDEO_FRAME_COMP_HEIGHT (out_frame, i);
297 
298       param.srcMemoryType = CU_MEMORYTYPE_DEVICE;
299       param.srcPitch = out_mem->stride;
300       param.srcDevice =
301           filter->out_fallback->data + filter->out_fallback->offset[i];
302       param.dstMemoryType = CU_MEMORYTYPE_HOST;
303       param.dstPitch = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, i);
304       param.dstHost = GST_VIDEO_FRAME_PLANE_DATA (out_frame, i);
305       param.WidthInBytes = width;
306       param.Height = height;
307 
308       if (!gst_cuda_result (CuMemcpy2DAsync (&param, btrans->cuda_stream))) {
309         gst_cuda_context_pop (NULL);
310         GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
311             ("Cannot upload input video frame"));
312         return GST_FLOW_ERROR;
313       }
314     }
315 
316     gst_cuda_result (CuStreamSynchronize (btrans->cuda_stream));
317     gst_cuda_context_pop (NULL);
318   }
319 
320   return GST_FLOW_OK;
321 }
322