blob: 271d6f540471d24c94e18a521c27fa85356c209d [file] [log] [blame]
Olivier Naudanb28313f2012-04-16 08:10:18 -04001/* GStreamer
2 * Copyright 2010 ST-Ericsson SA
3 * @author: Benjamin Gaignard <benjamin.gaignard@stericsson.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
Sebastian Drögee6513c12013-07-14 12:12:42 +020017 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
Olivier Naudanb28313f2012-04-16 08:10:18 -040019 */
20/*
21 * test autovideoconvert:
22 * if rgb2bayer is present
Sebastian Dröge8aae4522012-09-18 10:21:31 +020023 * gst-launch videotestsrc num-buffers=2 ! "video/x-raw,width=100,height=100,framerate=10/1" ! autovideoconvert ! "video/x-raw-bayer,width=100,height=100,format=bggr,framerate=10/1" ! fakesink -v
Olivier Naudanb28313f2012-04-16 08:10:18 -040024 * if bayer2rgb is present
Sebastian Dröge8aae4522012-09-18 10:21:31 +020025 * gst-launch videotestsrc num-buffers=2 ! "video/x-raw-bayer,width=100,height=100,format=bggr,framerate=10/1" ! autovideoconvert ! "video/x-raw,width=100,height=100,framerate=10/1" ! fakesink -v
26 * test with videoconvert
27 * gst-launch videotestsrc num-buffers=2 ! "video/x-raw,format=RGBx,width=100,height=100,framerate=10/1" ! autovideoconvert ! "video/x-raw,format=RGB16,width=100,height=100,framerate=10/1" ! fakesink -v
Olivier Naudanb28313f2012-04-16 08:10:18 -040028 */
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include <string.h>
34
35#include "gstautovideoconvert.h"
36
37GST_DEBUG_CATEGORY (autovideoconvert_debug);
38#define GST_CAT_DEFAULT (autovideoconvert_debug)
39
Sebastian Drögee6513c12013-07-14 12:12:42 +020040static GMutex factories_mutex;
41static guint32 factories_cookie = 0; /* Cookie from last time when factories was updated */
42static GList *factories = NULL; /* factories we can use for selecting elements */
Olivier Naudanb28313f2012-04-16 08:10:18 -040043
44/* element factory information */
45static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_PAD_SINK,
47 GST_PAD_ALWAYS,
48 GST_STATIC_CAPS_ANY);
49
50static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
51 GST_PAD_SRC,
52 GST_PAD_ALWAYS,
53 GST_STATIC_CAPS_ANY);
54
55
56static GstStateChangeReturn gst_auto_video_convert_change_state (GstElement *
57 element, GstStateChange transition);
58
59void gst_auto_video_convert_update_factory_list (GstAutoVideoConvert *
60 autovideoconvert);
61
62static gboolean
63gst_auto_video_convert_element_filter (GstPluginFeature * feature,
64 GstAutoVideoConvert * autovideoconvert)
65{
66 const gchar *klass;
67
68 /* we only care about element factories */
69 if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
70 return FALSE;
71
Sebastian Dröge8aae4522012-09-18 10:21:31 +020072 klass = gst_element_factory_get_metadata (GST_ELEMENT_FACTORY_CAST (feature),
73 GST_ELEMENT_METADATA_KLASS);
Olivier Naudanb28313f2012-04-16 08:10:18 -040074 /* only select color space converter */
75 if (strstr (klass, "Filter") &&
76 strstr (klass, "Converter") && strstr (klass, "Video")) {
77 GST_DEBUG_OBJECT (autovideoconvert,
78 "gst_auto_video_convert_element_filter found %s\n",
79 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (feature)));
80 return TRUE;
81 }
82 return FALSE;
83}
84
85
86static GList *
87gst_auto_video_convert_create_factory_list (GstAutoVideoConvert *
88 autovideoconvert)
89{
90 GList *result = NULL;
91
92 /* get the feature list using the filter */
93 result = gst_registry_feature_filter (gst_registry_get (),
94 (GstPluginFeatureFilter) gst_auto_video_convert_element_filter,
95 FALSE, autovideoconvert);
96
97 /* sort on rank and name */
98 result = g_list_sort (result, gst_plugin_feature_rank_compare_func);
99
100 return result;
101}
102
103void
104gst_auto_video_convert_update_factory_list (GstAutoVideoConvert *
105 autovideoconvert)
106{
107 /* use a static mutex to protect factories list and factories cookie */
Sebastian Drögee6513c12013-07-14 12:12:42 +0200108 g_mutex_lock (&factories_mutex);
Olivier Naudanb28313f2012-04-16 08:10:18 -0400109
110 /* test if a factories list already exist or not */
111 if (!factories) {
112 /* no factories list create it */
113 factories_cookie =
114 gst_registry_get_feature_list_cookie (gst_registry_get ());
115 factories = gst_auto_video_convert_create_factory_list (autovideoconvert);
116 } else {
117 /* a factories list exist but is it up to date? */
118 if (factories_cookie !=
119 gst_registry_get_feature_list_cookie (gst_registry_get ())) {
120 /* we need to update the factories list */
121 /* first free the old one */
122 gst_plugin_feature_list_free (factories);
123 /* then create an updated one */
124 factories_cookie =
125 gst_registry_get_feature_list_cookie (gst_registry_get ());
126 factories = gst_auto_video_convert_create_factory_list (autovideoconvert);
127 }
128 }
129
Sebastian Drögee6513c12013-07-14 12:12:42 +0200130 g_mutex_unlock (&factories_mutex);
Olivier Naudanb28313f2012-04-16 08:10:18 -0400131}
132
133G_DEFINE_TYPE (GstAutoVideoConvert, gst_auto_video_convert, GST_TYPE_BIN);
134
135static void
136gst_auto_video_convert_class_init (GstAutoVideoConvertClass * klass)
137{
138 GstElementClass *gstelement_class = (GstElementClass *) klass;
139
140 GST_DEBUG_CATEGORY_INIT (autovideoconvert_debug, "autovideoconvert", 0,
141 "Auto color space converter");
142
143 gst_element_class_add_pad_template (gstelement_class,
144 gst_static_pad_template_get (&srctemplate));
145 gst_element_class_add_pad_template (gstelement_class,
146 gst_static_pad_template_get (&sinktemplate));
147
Sebastian Dröge6a5ec012012-10-25 14:10:09 +0200148 gst_element_class_set_static_metadata (gstelement_class,
Olivier Naudanb28313f2012-04-16 08:10:18 -0400149 "Select color space convertor based on caps", "Generic/Bin",
150 "Selects the right color space convertor based on the caps",
151 "Benjamin Gaignard <benjamin.gaignard@stericsson.com>");
152
153 gstelement_class->change_state =
154 GST_DEBUG_FUNCPTR (gst_auto_video_convert_change_state);
155
156}
157
158static gboolean
159gst_auto_video_convert_add_autoconvert (GstAutoVideoConvert * autovideoconvert)
160{
161 GstPad *pad;
162
163 if (autovideoconvert->autoconvert)
164 return TRUE;
165
166 autovideoconvert->autoconvert =
167 gst_element_factory_make ("autoconvert", "autoconvertchild");
168 if (!autovideoconvert->autoconvert) {
169 GST_ERROR_OBJECT (autovideoconvert,
170 "Could not create autoconvert instance");
171 return FALSE;
172 }
173
174 /* first add autoconvert in bin */
175 gst_bin_add (GST_BIN (autovideoconvert),
176 gst_object_ref (autovideoconvert->autoconvert));
177
178 /* get sinkpad and link it to ghost sink pad */
179 pad = gst_element_get_static_pad (autovideoconvert->autoconvert, "sink");
180 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->sinkpad),
181 pad);
182 gst_object_unref (pad);
183
184 /* get srcpad and link it to ghost src pad */
185 pad = gst_element_get_static_pad (autovideoconvert->autoconvert, "src");
186 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->srcpad), pad);
187 gst_object_unref (pad);
188
189 return TRUE;
190}
191
192static void
193gst_auto_video_convert_remove_autoconvert (GstAutoVideoConvert *
194 autovideoconvert)
195{
196 if (!autovideoconvert->autoconvert)
197 return;
198
199 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->srcpad),
200 NULL);
201 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->sinkpad),
202 NULL);
203
204 gst_bin_remove (GST_BIN (autovideoconvert), autovideoconvert->autoconvert);
205 gst_object_unref (autovideoconvert->autoconvert);
206 autovideoconvert->autoconvert = NULL;
207}
208
209static void
210gst_auto_video_convert_init (GstAutoVideoConvert * autovideoconvert)
211{
212 GstPadTemplate *pad_tmpl;
213
214 /* get sink pad template */
215 pad_tmpl = gst_static_pad_template_get (&sinktemplate);
216 autovideoconvert->sinkpad =
217 gst_ghost_pad_new_no_target_from_template ("sink", pad_tmpl);
218 /* add sink ghost pad */
219 gst_element_add_pad (GST_ELEMENT (autovideoconvert),
220 autovideoconvert->sinkpad);
221 gst_object_unref (pad_tmpl);
222
223 /* get src pad template */
224 pad_tmpl = gst_static_pad_template_get (&srctemplate);
225 autovideoconvert->srcpad =
226 gst_ghost_pad_new_no_target_from_template ("src", pad_tmpl);
227 /* add src ghost pad */
228 gst_element_add_pad (GST_ELEMENT (autovideoconvert),
229 autovideoconvert->srcpad);
230 gst_object_unref (pad_tmpl);
231
232 return;
233}
234
235static GstStateChangeReturn
236gst_auto_video_convert_change_state (GstElement * element,
237 GstStateChange transition)
238{
239 GstAutoVideoConvert *autovideoconvert = GST_AUTO_VIDEO_CONVERT (element);
240 GstStateChangeReturn ret;
241
242 switch (transition) {
243 case GST_STATE_CHANGE_NULL_TO_READY:
244 {
245 /* create and add autoconvert in bin */
246 if (!gst_auto_video_convert_add_autoconvert (autovideoconvert)) {
247 ret = GST_STATE_CHANGE_FAILURE;
248 return ret;
249 }
250 /* get an updated list of factories */
251 gst_auto_video_convert_update_factory_list (autovideoconvert);
252 GST_DEBUG_OBJECT (autovideoconvert, "set factories list");
253 /* give factory list to autoconvert */
254 g_object_set (GST_ELEMENT (autovideoconvert->autoconvert), "factories",
255 factories, NULL);
256 break;
257 }
258 default:
259 break;
260 }
261
262 ret = GST_ELEMENT_CLASS (gst_auto_video_convert_parent_class)->change_state
263 (element, transition);
264 if (ret == GST_STATE_CHANGE_FAILURE)
265 return ret;
266
267 switch (transition) {
268 case GST_STATE_CHANGE_READY_TO_NULL:
269 {
270 gst_auto_video_convert_remove_autoconvert (autovideoconvert);
271 break;
272 }
273 default:
274 break;
275 }
276
277 return ret;
278}