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.
25 * Base class for CUDA filters
34 #include "gstcudabasefilter.h"
35 #include "gstcudaformat.h"
36 #include <gst/cuda/gstcudautils.h>
40 GST_DEBUG_CATEGORY_STATIC (gst_cuda_base_filter_debug);
41 #define GST_CAT_DEFAULT gst_cuda_base_filter_debug
43 #define GST_CUDA_FILTER_FORMATS \
44 "{ I420, YV12, NV12, NV21, P010_10LE, P016_LE, I420_10LE, Y444, Y444_16LE, " \
45 "BGRA, RGBA, RGBx, BGRx, ARGB, ABGR, RGB, BGR, BGR10A2_LE, RGB10A2_LE, Y42B, I422_10LE, I422_12LE }"
47 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
50 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
51 (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY, GST_CUDA_FILTER_FORMATS))
54 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
58 (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY, GST_CUDA_FILTER_FORMATS))
61 #define gst_cuda_base_filter_parent_class parent_class
62 G_DEFINE_ABSTRACT_TYPE (GstCudaBaseFilter,
63 gst_cuda_base_filter, GST_TYPE_CUDA_BASE_TRANSFORM);
65 static void gst_cuda_base_filter_dispose (GObject * object);
67 gst_cuda_base_filter_propose_allocation (GstBaseTransform * trans,
68 GstQuery * decide_query, GstQuery * query);
69 static gboolean gst_cuda_base_filter_decide_allocation (GstBaseTransform *
70 trans, GstQuery * query);
71 static GstFlowReturn gst_cuda_base_filter_transform (GstBaseTransform * trans,
72 GstBuffer * inbuf, GstBuffer * outbuf);
73 static gboolean gst_cuda_base_filter_set_info (GstCudaBaseTransform * btrans,
74 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
75 GstVideoInfo * out_info);
78 gst_cuda_base_filter_class_init (GstCudaBaseFilterClass * klass)
80 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
81 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
82 GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
83 GstCudaBaseTransformClass *btrans_class =
84 GST_CUDA_BASE_TRANSFORM_CLASS (klass);
86 gobject_class->dispose = gst_cuda_base_filter_dispose;
88 gst_element_class_add_static_pad_template (element_class, &sink_template);
89 gst_element_class_add_static_pad_template (element_class, &src_template);
91 trans_class->passthrough_on_same_caps = TRUE;
93 trans_class->propose_allocation =
94 GST_DEBUG_FUNCPTR (gst_cuda_base_filter_propose_allocation);
95 trans_class->decide_allocation =
96 GST_DEBUG_FUNCPTR (gst_cuda_base_filter_decide_allocation);
97 trans_class->transform = GST_DEBUG_FUNCPTR (gst_cuda_base_filter_transform);
99 btrans_class->set_info = GST_DEBUG_FUNCPTR (gst_cuda_base_filter_set_info);
101 GST_DEBUG_CATEGORY_INIT (gst_cuda_base_filter_debug,
102 "cudabasefilter", 0, "CUDA Base Filter");
104 gst_type_mark_as_plugin_api (GST_TYPE_CUDA_BASE_FILTER, 0);
108 gst_cuda_base_filter_init (GstCudaBaseFilter * convert)
113 gst_cuda_base_filter_dispose (GObject * object)
115 GstCudaBaseFilter *filter = GST_CUDA_BASE_FILTER (object);
117 if (filter->converter) {
118 gst_cuda_converter_free (filter->converter);
119 filter->converter = NULL;
122 G_OBJECT_CLASS (parent_class)->dispose (object);
126 gst_cuda_base_filter_set_info (GstCudaBaseTransform * btrans, GstCaps * incaps,
127 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
129 GstCudaBaseFilter *filter = GST_CUDA_BASE_FILTER (btrans);
131 if (filter->converter)
132 gst_cuda_converter_free (filter->converter);
135 gst_cuda_converter_new (in_info, out_info, btrans->context);
137 if (!filter->converter) {
138 GST_ERROR_OBJECT (filter, "could not create converter");
142 GST_DEBUG_OBJECT (filter, "reconfigured %d %d",
143 GST_VIDEO_INFO_FORMAT (in_info), GST_VIDEO_INFO_FORMAT (out_info));
149 gst_cuda_base_filter_propose_allocation (GstBaseTransform * trans,
150 GstQuery * decide_query, GstQuery * query)
152 GstCudaBaseTransform *ctrans = GST_CUDA_BASE_TRANSFORM (trans);
158 if (!GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
159 decide_query, query))
162 /* passthrough, we're done */
163 if (decide_query == NULL)
166 gst_query_parse_allocation (query, &caps, NULL);
171 if (!gst_video_info_from_caps (&info, caps))
174 if (gst_query_get_n_allocation_pools (query) == 0) {
175 GstStructure *config;
177 pool = gst_cuda_buffer_pool_new (ctrans->context);
179 config = gst_buffer_pool_get_config (pool);
181 gst_buffer_pool_config_add_option (config,
182 GST_BUFFER_POOL_OPTION_VIDEO_META);
184 size = GST_VIDEO_INFO_SIZE (&info);
185 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
187 if (!gst_buffer_pool_set_config (pool, config)) {
188 GST_ERROR_OBJECT (ctrans, "failed to set config");
189 gst_object_unref (pool);
193 /* Get updated size by cuda buffer pool */
194 config = gst_buffer_pool_get_config (pool);
195 gst_buffer_pool_config_get_params (config, NULL, &size, NULL, NULL);
196 gst_structure_free (config);
198 gst_query_add_allocation_pool (query, pool, size, 0, 0);
200 gst_object_unref (pool);
203 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
209 gst_cuda_base_filter_decide_allocation (GstBaseTransform * trans,
212 GstCudaBaseTransform *ctrans = GST_CUDA_BASE_TRANSFORM (trans);
213 GstCaps *outcaps = NULL;
214 GstBufferPool *pool = NULL;
215 guint size, min, max;
216 GstStructure *config;
217 gboolean update_pool = FALSE;
219 gst_query_parse_allocation (query, &outcaps, NULL);
224 if (gst_query_get_n_allocation_pools (query) > 0) {
225 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
227 if (!GST_IS_CUDA_BUFFER_POOL (pool)) {
228 gst_clear_object (&pool);
230 GstCudaBufferPool *cpool = GST_CUDA_BUFFER_POOL (pool);
232 if (cpool->context != ctrans->context) {
233 gst_clear_object (&pool);
241 gst_video_info_from_caps (&vinfo, outcaps);
242 size = GST_VIDEO_INFO_SIZE (&vinfo);
247 GST_DEBUG_OBJECT (ctrans, "create our pool");
249 pool = gst_cuda_buffer_pool_new (ctrans->context);
252 config = gst_buffer_pool_get_config (pool);
253 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
254 gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
255 gst_buffer_pool_set_config (pool, config);
257 /* Get updated size by cuda buffer pool */
258 config = gst_buffer_pool_get_config (pool);
259 gst_buffer_pool_config_get_params (config, NULL, &size, NULL, NULL);
260 gst_structure_free (config);
263 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
265 gst_query_add_allocation_pool (query, pool, size, min, max);
267 gst_object_unref (pool);
269 return GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
274 gst_cuda_base_filter_transform (GstBaseTransform * trans,
275 GstBuffer * inbuf, GstBuffer * outbuf)
277 GstCudaBaseFilter *self = GST_CUDA_BASE_FILTER (trans);
278 GstCudaBaseTransform *ctrans = GST_CUDA_BASE_TRANSFORM (trans);
279 GstVideoFrame in_frame, out_frame;
280 GstFlowReturn ret = GST_FLOW_OK;
283 if (gst_buffer_n_memory (inbuf) != 1) {
284 GST_ERROR_OBJECT (self, "Invalid input buffer");
285 return GST_FLOW_ERROR;
288 mem = gst_buffer_peek_memory (inbuf, 0);
289 if (!gst_is_cuda_memory (mem)) {
290 GST_ERROR_OBJECT (self, "Input buffer is not CUDA");
291 return GST_FLOW_ERROR;
294 if (gst_buffer_n_memory (outbuf) != 1) {
295 GST_ERROR_OBJECT (self, "Invalid output buffer");
296 return GST_FLOW_ERROR;
299 mem = gst_buffer_peek_memory (outbuf, 0);
300 if (!gst_is_cuda_memory (mem)) {
301 GST_ERROR_OBJECT (self, "Input buffer is not CUDA");
302 return GST_FLOW_ERROR;
305 if (!gst_video_frame_map (&in_frame, &ctrans->in_info, inbuf,
306 GST_MAP_READ | GST_MAP_CUDA)) {
307 GST_ERROR_OBJECT (self, "Failed to map input buffer");
308 return GST_FLOW_ERROR;
311 if (!gst_video_frame_map (&out_frame, &ctrans->out_info, outbuf,
312 GST_MAP_WRITE | GST_MAP_CUDA)) {
313 gst_video_frame_unmap (&in_frame);
314 GST_ERROR_OBJECT (self, "Failed to map output buffer");
315 return GST_FLOW_ERROR;
318 if (!gst_cuda_converter_convert_frame (self->converter, &in_frame, &out_frame,
319 ctrans->cuda_stream)) {
320 GST_ERROR_OBJECT (self, "Failed to convert frame");
321 ret = GST_FLOW_ERROR;
324 gst_video_frame_unmap (&out_frame);
325 gst_video_frame_unmap (&in_frame);