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>
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.
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.
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.
26 #include "gstcudabasefilter.h"
27 #include "gstcudautils.h"
30 GST_DEBUG_CATEGORY_STATIC (gst_cuda_base_filter_debug);
31 #define GST_CAT_DEFAULT gst_cuda_base_filter_debug
33 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
36 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
37 (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY, GST_CUDA_CONVERTER_FORMATS))
40 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
43 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
44 (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY, GST_CUDA_CONVERTER_FORMATS))
47 #define gst_cuda_base_filter_parent_class parent_class
48 G_DEFINE_ABSTRACT_TYPE (GstCudaBaseFilter,
49 gst_cuda_base_filter, GST_TYPE_CUDA_BASE_TRANSFORM);
51 static void gst_cuda_base_filter_dispose (GObject * object);
53 gst_cuda_base_filter_transform_frame (GstCudaBaseTransform * btrans,
54 GstVideoFrame * in_frame, GstCudaMemory * in_cuda_mem,
55 GstVideoFrame * out_frame, GstCudaMemory * out_cuda_mem);
56 static gboolean gst_cuda_base_filter_set_info (GstCudaBaseTransform * btrans,
57 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
58 GstVideoInfo * out_info);
61 gst_cuda_base_filter_class_init (GstCudaBaseFilterClass * klass)
63 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
64 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
65 GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
66 GstCudaBaseTransformClass *btrans_class =
67 GST_CUDA_BASE_TRANSFORM_CLASS (klass);
69 gobject_class->dispose = gst_cuda_base_filter_dispose;
71 gst_element_class_add_static_pad_template (element_class, &sink_template);
72 gst_element_class_add_static_pad_template (element_class, &src_template);
74 trans_class->passthrough_on_same_caps = TRUE;
76 btrans_class->set_info = GST_DEBUG_FUNCPTR (gst_cuda_base_filter_set_info);
77 btrans_class->transform_frame =
78 GST_DEBUG_FUNCPTR (gst_cuda_base_filter_transform_frame);
80 GST_DEBUG_CATEGORY_INIT (gst_cuda_base_filter_debug,
81 "cudabasefilter", 0, "CUDA Base Filter");
85 gst_cuda_base_filter_init (GstCudaBaseFilter * convert)
90 gst_cuda_base_filter_dispose (GObject * object)
92 GstCudaBaseFilter *filter = GST_CUDA_BASE_FILTER (object);
94 if (filter->converter) {
95 gst_cuda_converter_free (filter->converter);
96 filter->converter = NULL;
99 if (filter->in_fallback) {
100 gst_memory_unref (GST_MEMORY_CAST (filter->in_fallback));
101 filter->in_fallback = NULL;
104 if (filter->out_fallback) {
105 gst_memory_unref (GST_MEMORY_CAST (filter->out_fallback));
106 filter->out_fallback = NULL;
109 gst_clear_object (&filter->allocator);
111 G_OBJECT_CLASS (parent_class)->dispose (object);
115 gst_cuda_base_filter_configure (GstCudaBaseFilter * filter,
116 GstVideoInfo * in_info, GstVideoInfo * out_info)
118 GstCudaBaseTransform *btrans = GST_CUDA_BASE_TRANSFORM (filter);
120 /* cleanup internal pool */
121 if (filter->in_fallback) {
122 gst_memory_unref (GST_MEMORY_CAST (filter->in_fallback));
123 filter->in_fallback = NULL;
126 if (filter->out_fallback) {
127 gst_memory_unref (GST_MEMORY_CAST (filter->out_fallback));
128 filter->out_fallback = NULL;
131 if (!filter->allocator)
132 filter->allocator = gst_cuda_allocator_new (btrans->context);
134 if (!filter->allocator) {
135 GST_ERROR_OBJECT (filter, "Failed to create CUDA allocator");
143 gst_cuda_base_filter_set_info (GstCudaBaseTransform * btrans, GstCaps * incaps,
144 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
146 GstCudaBaseFilter *filter = GST_CUDA_BASE_FILTER (btrans);
148 if (!gst_cuda_base_filter_configure (filter, in_info, out_info)) {
152 if (filter->converter)
153 gst_cuda_converter_free (filter->converter);
156 gst_cuda_converter_new (in_info, out_info, btrans->context);
158 if (filter->converter == NULL)
161 GST_DEBUG_OBJECT (filter, "reconfigured %d %d",
162 GST_VIDEO_INFO_FORMAT (in_info), GST_VIDEO_INFO_FORMAT (out_info));
168 GST_ERROR_OBJECT (filter, "could not create converter");
174 gst_cuda_base_filter_transform_frame (GstCudaBaseTransform * btrans,
175 GstVideoFrame * in_frame, GstCudaMemory * in_cuda_mem,
176 GstVideoFrame * out_frame, GstCudaMemory * out_cuda_mem)
178 GstCudaBaseFilter *filter = GST_CUDA_BASE_FILTER (btrans);
180 GstCudaMemory *in_mem;
181 GstCudaMemory *out_mem;
185 in_mem = in_cuda_mem;
187 if (!filter->in_fallback) {
188 GstCudaAllocationParams params;
190 memset (¶ms, 0, sizeof (GstCudaAllocationParams));
191 params.info = btrans->in_info;
193 filter->in_fallback =
194 (GstCudaMemory *) gst_cuda_allocator_alloc (filter->allocator,
195 GST_VIDEO_INFO_SIZE (¶ms.info), ¶ms);
198 if (!filter->in_fallback) {
199 GST_ERROR_OBJECT (filter, "Couldn't allocate fallback memory");
200 return GST_FLOW_ERROR;
203 GST_TRACE_OBJECT (filter, "use CUDA fallback memory input");
205 if (!gst_cuda_context_push (btrans->context)) {
206 GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
207 ("Cannot push CUDA context"));
211 /* upload frame to device memory */
212 for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (in_frame); i++) {
213 CUDA_MEMCPY2D param = { 0, };
216 width = GST_VIDEO_FRAME_COMP_WIDTH (in_frame, i) *
217 GST_VIDEO_FRAME_COMP_PSTRIDE (in_frame, i);
218 height = GST_VIDEO_FRAME_COMP_HEIGHT (in_frame, i);
220 param.srcMemoryType = CU_MEMORYTYPE_HOST;
221 param.srcPitch = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, i);
222 param.srcHost = GST_VIDEO_FRAME_PLANE_DATA (in_frame, i);
223 param.dstMemoryType = CU_MEMORYTYPE_DEVICE;
224 param.dstPitch = filter->in_fallback->stride;
226 filter->in_fallback->data + filter->in_fallback->offset[i];
227 param.WidthInBytes = width;
228 param.Height = height;
230 if (!gst_cuda_result (CuMemcpy2DAsync (¶m, btrans->cuda_stream))) {
231 gst_cuda_context_pop (NULL);
232 GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
233 ("Cannot upload input video frame"));
234 return GST_FLOW_ERROR;
238 gst_cuda_result (CuStreamSynchronize (btrans->cuda_stream));
239 gst_cuda_context_pop (NULL);
241 in_mem = filter->in_fallback;
245 out_mem = out_cuda_mem;
247 if (!filter->out_fallback) {
248 GstCudaAllocationParams params;
250 memset (¶ms, 0, sizeof (GstCudaAllocationParams));
251 params.info = btrans->out_info;
253 filter->out_fallback =
254 (GstCudaMemory *) gst_cuda_allocator_alloc (filter->allocator,
255 GST_VIDEO_INFO_SIZE (¶ms.info), ¶ms);
258 if (!filter->out_fallback) {
259 GST_ERROR_OBJECT (filter, "Couldn't allocate fallback memory");
260 return GST_FLOW_ERROR;
263 out_mem = filter->out_fallback;
267 gst_cuda_converter_frame (filter->converter, in_mem, &btrans->in_info,
268 out_mem, &btrans->out_info, btrans->cuda_stream);
271 GST_ERROR_OBJECT (filter, "Failed to convert frame");
272 return GST_FLOW_ERROR;
276 if (!gst_cuda_context_push (btrans->context)) {
277 GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
278 ("Cannot push CUDA context"));
282 for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (out_frame); i++) {
283 CUDA_MEMCPY2D param = { 0, };
286 width = GST_VIDEO_FRAME_COMP_WIDTH (out_frame, i) *
287 GST_VIDEO_FRAME_COMP_PSTRIDE (out_frame, i);
288 height = GST_VIDEO_FRAME_COMP_HEIGHT (out_frame, i);
290 param.srcMemoryType = CU_MEMORYTYPE_DEVICE;
291 param.srcPitch = out_mem->stride;
293 filter->out_fallback->data + filter->out_fallback->offset[i];
294 param.dstMemoryType = CU_MEMORYTYPE_HOST;
295 param.dstPitch = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, i);
296 param.dstHost = GST_VIDEO_FRAME_PLANE_DATA (out_frame, i);
297 param.WidthInBytes = width;
298 param.Height = height;
300 if (!gst_cuda_result (CuMemcpy2DAsync (¶m, btrans->cuda_stream))) {
301 gst_cuda_context_pop (NULL);
302 GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
303 ("Cannot upload input video frame"));
304 return GST_FLOW_ERROR;
308 gst_cuda_result (CuStreamSynchronize (btrans->cuda_stream));
309 gst_cuda_context_pop (NULL);