1 /*
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
29 GST_DEBUG_CATEGORY_STATIC (gst_gl_color_convert_element_debug);
30 #define gst_gl_color_convert_element_parent_class parent_class
31 #define GST_CAT_DEFAULT gst_gl_color_convert_element_debug
32
33 G_DEFINE_TYPE_WITH_CODE (GstGLColorConvertElement, gst_gl_color_convert_element,
34 GST_TYPE_GL_BASE_FILTER,
35 GST_DEBUG_CATEGORY_INIT (gst_gl_color_convert_element_debug,
36 "glconvertelement", 0, "convert");
37 );
38
39 static gboolean gst_gl_color_convert_element_set_caps (GstBaseTransform * bt,
40 GstCaps * in_caps, GstCaps * out_caps);
41 static GstCaps *gst_gl_color_convert_element_transform_caps (GstBaseTransform *
42 bt, GstPadDirection direction, GstCaps * caps, GstCaps * filter);
43 static gboolean gst_gl_color_convert_element_get_unit_size (GstBaseTransform *
44 trans, GstCaps * caps, gsize * size);
45 static gboolean
46 gst_gl_color_convert_element_filter_meta (GstBaseTransform * trans,
47 GstQuery * query, GType api, const GstStructure * params);
48 static gboolean gst_gl_color_convert_element_decide_allocation (GstBaseTransform
49 * trans, GstQuery * query);
50 static GstFlowReturn
51 gst_gl_color_convert_element_prepare_output_buffer (GstBaseTransform * bt,
52 GstBuffer * inbuf, GstBuffer ** outbuf);
53 static GstFlowReturn gst_gl_color_convert_element_transform (GstBaseTransform *
54 bt, GstBuffer * inbuf, GstBuffer * outbuf);
55 static GstCaps *gst_gl_color_convert_element_fixate_caps (GstBaseTransform *
56 bt, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
57 static GstStateChangeReturn
58 gst_gl_color_convert_element_change_state (GstElement * element,
59 GstStateChange transition);
60
61 static GstStaticPadTemplate gst_gl_color_convert_element_src_pad_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
63 GST_PAD_SRC,
64 GST_PAD_ALWAYS,
65 GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS));
66
67 static GstStaticPadTemplate gst_gl_color_convert_element_sink_pad_template =
68 GST_STATIC_PAD_TEMPLATE ("sink",
69 GST_PAD_SINK,
70 GST_PAD_ALWAYS,
71 GST_STATIC_CAPS (GST_GL_COLOR_CONVERT_VIDEO_CAPS));
72
73 static gboolean
gst_gl_color_convert_element_stop(GstBaseTransform * bt)74 gst_gl_color_convert_element_stop (GstBaseTransform * bt)
75 {
76 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
77
78 if (convert->convert) {
79 gst_object_unref (convert->convert);
80 convert->convert = NULL;
81 }
82
83 return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (bt);
84 }
85
86 static void
gst_gl_color_convert_element_class_init(GstGLColorConvertElementClass * klass)87 gst_gl_color_convert_element_class_init (GstGLColorConvertElementClass * klass)
88 {
89 GstBaseTransformClass *bt_class = GST_BASE_TRANSFORM_CLASS (klass);
90 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
91
92 bt_class->transform_caps = gst_gl_color_convert_element_transform_caps;
93 bt_class->set_caps = gst_gl_color_convert_element_set_caps;
94 bt_class->get_unit_size = gst_gl_color_convert_element_get_unit_size;
95 bt_class->filter_meta = gst_gl_color_convert_element_filter_meta;
96 bt_class->decide_allocation = gst_gl_color_convert_element_decide_allocation;
97 bt_class->prepare_output_buffer =
98 gst_gl_color_convert_element_prepare_output_buffer;
99 bt_class->transform = gst_gl_color_convert_element_transform;
100 bt_class->stop = gst_gl_color_convert_element_stop;
101 bt_class->fixate_caps = gst_gl_color_convert_element_fixate_caps;
102
103 bt_class->passthrough_on_same_caps = TRUE;
104
105 element_class->change_state = gst_gl_color_convert_element_change_state;
106
107 gst_element_class_add_static_pad_template (element_class,
108 &gst_gl_color_convert_element_src_pad_template);
109 gst_element_class_add_static_pad_template (element_class,
110 &gst_gl_color_convert_element_sink_pad_template);
111
112 gst_element_class_set_metadata (element_class,
113 "OpenGL color converter", "Filter/Converter/Video",
114 "Converts between color spaces using OpenGL shaders",
115 "Matthew Waters <matthew@centricular.com>");
116 }
117
118 static void
gst_gl_color_convert_element_init(GstGLColorConvertElement * convert)119 gst_gl_color_convert_element_init (GstGLColorConvertElement * convert)
120 {
121 gst_base_transform_set_prefer_passthrough (GST_BASE_TRANSFORM (convert),
122 TRUE);
123 }
124
125 static gboolean
gst_gl_color_convert_element_set_caps(GstBaseTransform * bt,GstCaps * in_caps,GstCaps * out_caps)126 gst_gl_color_convert_element_set_caps (GstBaseTransform * bt,
127 GstCaps * in_caps, GstCaps * out_caps)
128 {
129 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
130
131 if (!gst_gl_color_convert_set_caps (convert->convert, in_caps, out_caps))
132 return FALSE;
133
134 return TRUE;
135 }
136
137 static GstCaps *
gst_gl_color_convert_element_transform_caps(GstBaseTransform * bt,GstPadDirection direction,GstCaps * caps,GstCaps * filter)138 gst_gl_color_convert_element_transform_caps (GstBaseTransform * bt,
139 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
140 {
141 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
142 GstGLBaseFilter *base_filter = GST_GL_BASE_FILTER (bt);
143 GstGLContext *context;
144
145 if (base_filter->display && !gst_gl_base_filter_find_gl_context (base_filter))
146 return NULL;
147
148 context = GST_GL_BASE_FILTER (bt)->context;
149
150 if (!convert->convert && context)
151 convert->convert = gst_gl_color_convert_new (context);
152
153 return gst_gl_color_convert_transform_caps (context, direction, caps, filter);
154 }
155
156 static gboolean
gst_gl_color_convert_element_get_unit_size(GstBaseTransform * trans,GstCaps * caps,gsize * size)157 gst_gl_color_convert_element_get_unit_size (GstBaseTransform * trans,
158 GstCaps * caps, gsize * size)
159 {
160 gboolean ret = FALSE;
161 GstVideoInfo info;
162
163 ret = gst_video_info_from_caps (&info, caps);
164 if (ret)
165 *size = GST_VIDEO_INFO_SIZE (&info);
166
167 return TRUE;
168 }
169
170 static gboolean
gst_gl_color_convert_element_filter_meta(GstBaseTransform * trans,GstQuery * query,GType api,const GstStructure * params)171 gst_gl_color_convert_element_filter_meta (GstBaseTransform * trans,
172 GstQuery * query, GType api, const GstStructure * params)
173 {
174 /* propose all metadata upstream */
175 return TRUE;
176 }
177
178 static gboolean
gst_gl_color_convert_element_decide_allocation(GstBaseTransform * trans,GstQuery * query)179 gst_gl_color_convert_element_decide_allocation (GstBaseTransform * trans,
180 GstQuery * query)
181 {
182 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (trans);
183
184 /* get gl context */
185 if (!GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
186 query))
187 return FALSE;
188
189 if (!gst_gl_color_convert_decide_allocation (convert->convert, query))
190 return FALSE;
191
192 return TRUE;
193 }
194
195 static GstFlowReturn
gst_gl_color_convert_element_prepare_output_buffer(GstBaseTransform * bt,GstBuffer * inbuf,GstBuffer ** outbuf)196 gst_gl_color_convert_element_prepare_output_buffer (GstBaseTransform * bt,
197 GstBuffer * inbuf, GstBuffer ** outbuf)
198 {
199 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (bt);
200 GstBaseTransformClass *bclass;
201
202 bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
203
204 if (gst_base_transform_is_passthrough (bt)) {
205 *outbuf = inbuf;
206 return GST_FLOW_OK;
207 }
208
209 if (!convert->convert)
210 return GST_FLOW_NOT_NEGOTIATED;
211
212 *outbuf = gst_gl_color_convert_perform (convert->convert, inbuf);
213 if (!*outbuf) {
214 GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND,
215 ("%s", "Failed to convert video buffer"), (NULL));
216 return GST_FLOW_ERROR;
217 }
218
219 /* basetransform doesn't unref if they're the same */
220 if (inbuf == *outbuf)
221 gst_buffer_unref (*outbuf);
222 else
223 bclass->copy_metadata (bt, inbuf, *outbuf);
224
225 return GST_FLOW_OK;
226 }
227
228 static GstFlowReturn
gst_gl_color_convert_element_transform(GstBaseTransform * bt,GstBuffer * inbuf,GstBuffer * outbuf)229 gst_gl_color_convert_element_transform (GstBaseTransform * bt,
230 GstBuffer * inbuf, GstBuffer * outbuf)
231 {
232 return GST_FLOW_OK;
233 }
234
235 static GstCaps *
gst_gl_color_convert_element_fixate_caps(GstBaseTransform * bt,GstPadDirection direction,GstCaps * caps,GstCaps * othercaps)236 gst_gl_color_convert_element_fixate_caps (GstBaseTransform *
237 bt, GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
238 {
239 GstGLContext *context = GST_GL_BASE_FILTER (bt)->context;
240
241 return gst_gl_color_convert_fixate_caps (context, direction, caps, othercaps);
242 }
243
244 static GstStateChangeReturn
gst_gl_color_convert_element_change_state(GstElement * element,GstStateChange transition)245 gst_gl_color_convert_element_change_state (GstElement * element,
246 GstStateChange transition)
247 {
248 GstGLColorConvertElement *convert = GST_GL_COLOR_CONVERT_ELEMENT (element);
249 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
250
251 GST_DEBUG_OBJECT (convert, "changing state: %s => %s",
252 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
253 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
254
255 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
256 if (ret == GST_STATE_CHANGE_FAILURE)
257 return ret;
258
259 switch (transition) {
260 case GST_STATE_CHANGE_READY_TO_NULL:
261 if (convert->convert) {
262 gst_object_unref (convert->convert);
263 convert->convert = NULL;
264 }
265 break;
266 default:
267 break;
268 }
269
270 return ret;
271 }
272