blob: bd4fbebcd3ffbfe5fba0675164319d563ca2a77e [file] [log] [blame]
Matthew Waters35146002015-02-12 17:59:27 +11001/*
2 * GStreamer
3 * Copyright (C) 2012-2014 Matthew Waters <ystree00@gmail.com>
4 * Copyright (C) 2015 Matthew Waters <matthew@centricular.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#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include <gst/gl/gl.h>
27#include "gstglcolorconvertelement.h"
28
29GST_DEBUG_CATEGORY_STATIC (gst_gl_color_convert_element_debug);
30#define GST_CAT_DEFAULT gst_gl_color_convert_element_debug
31
32G_DEFINE_TYPE_WITH_CODE (GstGLColorConvertElement, gst_gl_color_convert_element,
33 GST_TYPE_GL_BASE_FILTER,
34 GST_DEBUG_CATEGORY_INIT (gst_gl_color_convert_element_debug,
Sebastian Dröge53c79cb2015-05-06 15:46:49 +020035 "glconvertelement", 0, "convert");
36 );
Matthew Waters35146002015-02-12 17:59:27 +110037
Matthew Waters35146002015-02-12 17:59:27 +110038static gboolean gst_gl_color_convert_element_set_caps (GstBaseTransform * bt,
39 GstCaps * in_caps, GstCaps * out_caps);
40static GstCaps *gst_gl_color_convert_element_transform_caps (GstBaseTransform *
41 bt, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
42static gboolean gst_gl_color_convert_element_get_unit_size (GstBaseTransform *
43 trans, GstCaps * caps, gsize * size);
Nicolas Dufresne206638c2015-08-20 17:27:34 -070044static gboolean
45gst_gl_color_convert_element_filter_meta (GstBaseTransform * trans,
46 GstQuery * query, GType api, const GstStructure * params);
Matthew Waters35146002015-02-12 17:59:27 +110047static gboolean gst_gl_color_convert_element_decide_allocation (GstBaseTransform
48 * trans, GstQuery * query);
49static GstFlowReturn
50gst_gl_color_convert_element_prepare_output_buffer (GstBaseTransform * bt,
51 GstBuffer * inbuf, GstBuffer ** outbuf);
52static GstFlowReturn gst_gl_color_convert_element_transform (GstBaseTransform *
53 bt, GstBuffer * inbuf, GstBuffer * outbuf);
Matthieu Bouron76b2cef2015-04-19 19:16:55 +020054static GstCaps *gst_gl_color_convert_element_fixate_caps (GstBaseTransform *
55 bt, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
Matthew Waters35146002015-02-12 17:59:27 +110056
57static GstStaticPadTemplate gst_gl_color_convert_element_src_pad_template =
Sebastian Dröge53c79cb2015-05-06 15:46:49 +020058GST_STATIC_PAD_TEMPLATE ("src",
Matthew Waters35146002015-02-12 17:59:27 +110059 GST_PAD_SRC,
60 GST_PAD_ALWAYS,
Matthew Waterse61d5042015-10-29 00:44:26 +110061 GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS));
Sebastian Dröge53c79cb2015-05-06 15:46:49 +020062
63static GstStaticPadTemplate gst_gl_color_convert_element_sink_pad_template =
64GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_PAD_SINK,
66 GST_PAD_ALWAYS,
Matthew Waterse61d5042015-10-29 00:44:26 +110067 GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS));
Matthew Waters35146002015-02-12 17:59:27 +110068
Guillaume Desmottes17446a42015-04-15 14:49:02 +020069static gboolean
70gst_gl_color_convert_element_stop (GstBaseTransform * bt)
71{
72 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
73
74 if (convert->convert) {
75 gst_object_unref (convert->convert);
76 convert->convert = NULL;
77 }
78
Sebastian Dröge53ed7012015-04-17 14:03:21 +020079 gst_caps_replace (&convert->in_caps, NULL);
80 gst_caps_replace (&convert->out_caps, NULL);
81
Guillaume Desmottes17446a42015-04-15 14:49:02 +020082 return
83 GST_BASE_TRANSFORM_CLASS (gst_gl_color_convert_element_parent_class)->stop
84 (bt);
85}
86
Matthew Waters35146002015-02-12 17:59:27 +110087static void
88gst_gl_color_convert_element_class_init (GstGLColorConvertElementClass * klass)
89{
90 GstBaseTransformClass *bt_class = GST_BASE_TRANSFORM_CLASS (klass);
91 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
92
93 bt_class->transform_caps = gst_gl_color_convert_element_transform_caps;
94 bt_class->set_caps = gst_gl_color_convert_element_set_caps;
95 bt_class->get_unit_size = gst_gl_color_convert_element_get_unit_size;
Nicolas Dufresne206638c2015-08-20 17:27:34 -070096 bt_class->filter_meta = gst_gl_color_convert_element_filter_meta;
Matthew Waters35146002015-02-12 17:59:27 +110097 bt_class->decide_allocation = gst_gl_color_convert_element_decide_allocation;
98 bt_class->prepare_output_buffer =
99 gst_gl_color_convert_element_prepare_output_buffer;
100 bt_class->transform = gst_gl_color_convert_element_transform;
Guillaume Desmottes17446a42015-04-15 14:49:02 +0200101 bt_class->stop = gst_gl_color_convert_element_stop;
Matthieu Bouron76b2cef2015-04-19 19:16:55 +0200102 bt_class->fixate_caps = gst_gl_color_convert_element_fixate_caps;
Matthew Waters35146002015-02-12 17:59:27 +1100103
104 bt_class->passthrough_on_same_caps = TRUE;
105
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get
108 (&gst_gl_color_convert_element_src_pad_template));
109 gst_element_class_add_pad_template (element_class,
Sebastian Dröge53c79cb2015-05-06 15:46:49 +0200110 gst_static_pad_template_get
111 (&gst_gl_color_convert_element_sink_pad_template));
Matthew Waters35146002015-02-12 17:59:27 +1100112
113 gst_element_class_set_metadata (element_class,
114 "OpenGL color converter", "Filter/Converter/Video",
115 "Converts between color spaces using OpenGL shaders",
116 "Matthew Waters <matthew@centricular.com>");
Matthew Waters35146002015-02-12 17:59:27 +1100117}
118
119static void
120gst_gl_color_convert_element_init (GstGLColorConvertElement * convert)
121{
122 gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (convert),
123 TRUE);
124}
125
Matthew Waters35146002015-02-12 17:59:27 +1100126static gboolean
127gst_gl_color_convert_element_set_caps (GstBaseTransform * bt,
128 GstCaps * in_caps, GstCaps * out_caps)
129{
130 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
131
132 gst_caps_replace (&convert->in_caps, in_caps);
133 gst_caps_replace (&convert->out_caps, out_caps);
134
Sebastian Dröge53c79cb2015-05-06 15:46:49 +0200135 if (convert->convert)
136 gst_gl_color_convert_set_caps (convert->convert, in_caps, out_caps);
137
Matthew Waters35146002015-02-12 17:59:27 +1100138 return TRUE;
139}
140
141static GstCaps *
142gst_gl_color_convert_element_transform_caps (GstBaseTransform * bt,
143 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
144{
145 GstGLContext *context = GST_GL_BASE_FILTER (bt)->context;
146
Sebastian Dröge53c79cb2015-05-06 15:46:49 +0200147 return gst_gl_color_convert_transform_caps (context, direction, caps, filter);
Matthew Waters35146002015-02-12 17:59:27 +1100148}
149
150static gboolean
151gst_gl_color_convert_element_get_unit_size (GstBaseTransform * trans,
152 GstCaps * caps, gsize * size)
153{
154 gboolean ret = FALSE;
155 GstVideoInfo info;
156
157 ret = gst_video_info_from_caps (&info, caps);
158 if (ret)
159 *size = GST_VIDEO_INFO_SIZE (&info);
160
161 return TRUE;
162}
163
164static gboolean
Nicolas Dufresne206638c2015-08-20 17:27:34 -0700165gst_gl_color_convert_element_filter_meta (GstBaseTransform * trans,
166 GstQuery * query, GType api, const GstStructure * params)
167{
168 /* propose all metadata upstream */
169 return TRUE;
170}
171
172static gboolean
Matthew Waters35146002015-02-12 17:59:27 +1100173gst_gl_color_convert_element_decide_allocation (GstBaseTransform * trans,
174 GstQuery * query)
175{
176 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (trans);
177 GstGLContext *context;
178
179 /* get gl context */
180 if (!GST_BASE_TRANSFORM_CLASS
181 (gst_gl_color_convert_element_parent_class)->decide_allocation (trans,
182 query))
183 return FALSE;
184
185 context = GST_GL_BASE_FILTER (trans)->context;
186
187 if (!convert->convert)
188 convert->convert = gst_gl_color_convert_new (context);
189
Sebastian Dröge53c79cb2015-05-06 15:46:49 +0200190 if (!gst_gl_color_convert_set_caps (convert->convert, convert->in_caps,
191 convert->out_caps))
Matthew Waters35146002015-02-12 17:59:27 +1100192 return FALSE;
193
Matthew Waters51928022016-01-06 16:25:38 +1100194 if (!gst_gl_color_convert_decide_allocation (convert->convert, query))
195 return FALSE;
196
Matthew Waters35146002015-02-12 17:59:27 +1100197 return TRUE;
198}
199
200static GstFlowReturn
201gst_gl_color_convert_element_prepare_output_buffer (GstBaseTransform * bt,
202 GstBuffer * inbuf, GstBuffer ** outbuf)
203{
204 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
Nicolas Dufresneb06fc672015-08-24 19:28:10 -0400205 GstBaseTransformClass *bclass;
206
207 bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
Matthew Waters35146002015-02-12 17:59:27 +1100208
209 if (gst_base_transform_is_passthrough (bt)) {
210 *outbuf = inbuf;
211 return GST_FLOW_OK;
212 }
213
Sebastian Dröge53c79cb2015-05-06 15:46:49 +0200214 if (!convert->convert)
Matthew Waters35146002015-02-12 17:59:27 +1100215 return GST_FLOW_NOT_NEGOTIATED;
216
Sebastian Dröge53c79cb2015-05-06 15:46:49 +0200217 *outbuf = gst_gl_color_convert_perform (convert->convert, inbuf);
Matthew Waters0aaf9db2015-07-17 17:47:37 +1000218 if (!*outbuf) {
219 GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND,
220 ("%s", "Failed to convert video buffer"), (NULL));
221 return GST_FLOW_ERROR;
222 }
Matthew Waters35146002015-02-12 17:59:27 +1100223
Matthew Waters0c8ede82015-03-14 10:39:06 +0000224 /* basetransform doesn't unref if they're the same */
225 if (inbuf == *outbuf)
226 gst_buffer_unref (*outbuf);
Nicolas Dufresneb06fc672015-08-24 19:28:10 -0400227 else
228 bclass->copy_metadata (bt, inbuf, *outbuf);
Matthew Waters35146002015-02-12 17:59:27 +1100229
230 return GST_FLOW_OK;
231}
232
233static GstFlowReturn
234gst_gl_color_convert_element_transform (GstBaseTransform * bt,
235 GstBuffer * inbuf, GstBuffer * outbuf)
236{
237 return GST_FLOW_OK;
238}
Matthieu Bouron76b2cef2015-04-19 19:16:55 +0200239
240static GstCaps *
241gst_gl_color_convert_element_fixate_caps (GstBaseTransform *
242 bt, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
243{
Matthew Watersccce2172015-10-29 13:04:31 +1100244 GstGLContext *context = GST_GL_BASE_FILTER (bt)->context;
Matthieu Bouron76b2cef2015-04-19 19:16:55 +0200245
Matthew Watersccce2172015-10-29 13:04:31 +1100246 return gst_gl_color_convert_fixate_caps (context, direction, caps, othercaps);
Matthieu Bouron76b2cef2015-04-19 19:16:55 +0200247}