846de4da5c94d05095f7097c576f78f0afa43d9d
[platform/upstream/gstreamer.git] / sys / nvcodec / gstcudadownload.c
1
2 /* GStreamer
3  * Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include "gstcudadownload.h"
26
27 GST_DEBUG_CATEGORY_STATIC (gst_cuda_download_debug);
28 #define GST_CAT_DEFAULT gst_cuda_download_debug
29
30 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
31     GST_PAD_SINK,
32     GST_PAD_ALWAYS,
33     GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY
34         "); video/x-raw"));
35
36 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("video/x-raw"));
40
41 G_DEFINE_TYPE (GstCudaDownload, gst_cuda_download,
42     GST_TYPE_CUDA_BASE_TRANSFORM);
43
44 static GstCaps *gst_cuda_download_transform_caps (GstBaseTransform * trans,
45     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
46
47 static void
48 gst_cuda_download_class_init (GstCudaDownloadClass * klass)
49 {
50   GstElementClass *element_class;
51   GstBaseTransformClass *trans_class;
52
53   element_class = GST_ELEMENT_CLASS (klass);
54   trans_class = GST_BASE_TRANSFORM_CLASS (klass);
55
56   gst_element_class_add_static_pad_template (element_class, &sink_template);
57   gst_element_class_add_static_pad_template (element_class, &src_template);
58
59   gst_element_class_set_static_metadata (element_class,
60       "CUDA downloader", "Filter/Video",
61       "Uploads data into NVIDA GPU via CUDA APIs",
62       "Seungha Yang <seungha.yang@navercorp.com>");
63
64   trans_class->passthrough_on_same_caps = TRUE;
65
66   trans_class->transform_caps =
67       GST_DEBUG_FUNCPTR (gst_cuda_download_transform_caps);
68
69   GST_DEBUG_CATEGORY_INIT (gst_cuda_download_debug,
70       "cudadownload", 0, "cudadownload Element");
71 }
72
73 static void
74 gst_cuda_download_init (GstCudaDownload * download)
75 {
76 }
77
78 static GstCaps *
79 _set_caps_features (const GstCaps * caps, const gchar * feature_name)
80 {
81   GstCaps *tmp = gst_caps_copy (caps);
82   guint n = gst_caps_get_size (tmp);
83   guint i = 0;
84
85   for (i = 0; i < n; i++)
86     gst_caps_set_features (tmp, i,
87         gst_caps_features_from_string (feature_name));
88
89   return tmp;
90 }
91
92 static GstCaps *
93 gst_cuda_download_transform_caps (GstBaseTransform * trans,
94     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
95 {
96   GstCaps *result, *tmp;
97
98   GST_DEBUG_OBJECT (trans,
99       "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps,
100       (direction == GST_PAD_SINK) ? "sink" : "src");
101
102   if (direction == GST_PAD_SINK) {
103     tmp = _set_caps_features (caps, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY);
104     tmp = gst_caps_merge (gst_caps_ref (caps), tmp);
105   } else {
106     GstCaps *newcaps;
107     tmp = gst_caps_ref (caps);
108
109     newcaps = _set_caps_features (caps, GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY);
110     tmp = gst_caps_merge (tmp, newcaps);
111   }
112
113   if (filter) {
114     result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
115     gst_caps_unref (tmp);
116   } else {
117     result = tmp;
118   }
119
120   GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, result);
121
122   return result;
123 }