2 * Copyright 2010 ST-Ericsson SA
3 * @author: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
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.
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.
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.
21 * test autovideoconvert:
22 * if rgb2bayer is present
23 * gst-launch-1.0 videotestsrc num-buffers=2 ! "video/x-raw,width=100,height=100,framerate=10/1" ! autovideoconvert ! "video/x-bayer,width=100,height=100,format=bggr,framerate=10/1" ! fakesink -v
24 * if bayer2rgb is present
25 * gst-launch-1.0 videotestsrc num-buffers=2 ! "video/x-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-1.0 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
35 #include "gstautovideoconvert.h"
37 GST_DEBUG_CATEGORY (autovideoconvert_debug);
38 #define GST_CAT_DEFAULT (autovideoconvert_debug)
40 static GMutex factories_mutex;
41 static guint32 factories_cookie = 0; /* Cookie from last time when factories was updated */
42 static GList *factories = NULL; /* factories we can use for selecting elements */
44 /* element factory information */
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
50 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
56 static GstStateChangeReturn gst_auto_video_convert_change_state (GstElement *
57 element, GstStateChange transition);
59 void gst_auto_video_convert_update_factory_list (GstAutoVideoConvert *
63 gst_auto_video_convert_element_filter (GstPluginFeature * feature,
64 GstAutoVideoConvert * autovideoconvert)
68 /* we only care about element factories */
69 if (G_UNLIKELY (!GST_IS_ELEMENT_FACTORY (feature)))
72 klass = gst_element_factory_get_metadata (GST_ELEMENT_FACTORY_CAST (feature),
73 GST_ELEMENT_METADATA_KLASS);
74 /* 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)));
87 gst_auto_video_convert_create_factory_list (GstAutoVideoConvert *
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);
97 /* sort on rank and name */
98 result = g_list_sort (result, gst_plugin_feature_rank_compare_func);
104 gst_auto_video_convert_update_factory_list (GstAutoVideoConvert *
107 /* use a static mutex to protect factories list and factories cookie */
108 g_mutex_lock (&factories_mutex);
110 /* test if a factories list already exist or not */
112 /* no factories list create it */
114 gst_registry_get_feature_list_cookie (gst_registry_get ());
115 factories = gst_auto_video_convert_create_factory_list (autovideoconvert);
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 */
125 gst_registry_get_feature_list_cookie (gst_registry_get ());
126 factories = gst_auto_video_convert_create_factory_list (autovideoconvert);
130 g_mutex_unlock (&factories_mutex);
133 G_DEFINE_TYPE (GstAutoVideoConvert, gst_auto_video_convert, GST_TYPE_BIN);
136 gst_auto_video_convert_class_init (GstAutoVideoConvertClass * klass)
138 GstElementClass *gstelement_class = (GstElementClass *) klass;
140 GST_DEBUG_CATEGORY_INIT (autovideoconvert_debug, "autovideoconvert", 0,
141 "Auto color space converter");
143 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
144 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
146 gst_element_class_set_static_metadata (gstelement_class,
147 "Select color space converter based on caps", "Generic/Bin",
148 "Selects the right color space converter based on the caps",
149 "Benjamin Gaignard <benjamin.gaignard@stericsson.com>");
151 gstelement_class->change_state =
152 GST_DEBUG_FUNCPTR (gst_auto_video_convert_change_state);
157 gst_auto_video_convert_add_autoconvert (GstAutoVideoConvert * autovideoconvert)
161 if (autovideoconvert->autoconvert)
164 autovideoconvert->autoconvert =
165 gst_element_factory_make ("autoconvert", "autoconvertchild");
166 if (!autovideoconvert->autoconvert) {
167 GST_ERROR_OBJECT (autovideoconvert,
168 "Could not create autoconvert instance");
172 /* first add autoconvert in bin */
173 gst_bin_add (GST_BIN (autovideoconvert),
174 gst_object_ref (autovideoconvert->autoconvert));
176 /* get sinkpad and link it to ghost sink pad */
177 pad = gst_element_get_static_pad (autovideoconvert->autoconvert, "sink");
178 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->sinkpad),
180 gst_object_unref (pad);
182 /* get srcpad and link it to ghost src pad */
183 pad = gst_element_get_static_pad (autovideoconvert->autoconvert, "src");
184 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->srcpad), pad);
185 gst_object_unref (pad);
191 gst_auto_video_convert_remove_autoconvert (GstAutoVideoConvert *
194 if (!autovideoconvert->autoconvert)
197 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->srcpad),
199 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->sinkpad),
202 gst_bin_remove (GST_BIN (autovideoconvert), autovideoconvert->autoconvert);
203 gst_object_unref (autovideoconvert->autoconvert);
204 autovideoconvert->autoconvert = NULL;
208 gst_auto_video_convert_init (GstAutoVideoConvert * autovideoconvert)
210 GstPadTemplate *pad_tmpl;
212 /* get sink pad template */
213 pad_tmpl = gst_static_pad_template_get (&sinktemplate);
214 autovideoconvert->sinkpad =
215 gst_ghost_pad_new_no_target_from_template ("sink", pad_tmpl);
216 /* add sink ghost pad */
217 gst_element_add_pad (GST_ELEMENT (autovideoconvert),
218 autovideoconvert->sinkpad);
219 gst_object_unref (pad_tmpl);
221 /* get src pad template */
222 pad_tmpl = gst_static_pad_template_get (&srctemplate);
223 autovideoconvert->srcpad =
224 gst_ghost_pad_new_no_target_from_template ("src", pad_tmpl);
225 /* add src ghost pad */
226 gst_element_add_pad (GST_ELEMENT (autovideoconvert),
227 autovideoconvert->srcpad);
228 gst_object_unref (pad_tmpl);
233 static GstStateChangeReturn
234 gst_auto_video_convert_change_state (GstElement * element,
235 GstStateChange transition)
237 GstAutoVideoConvert *autovideoconvert = GST_AUTO_VIDEO_CONVERT (element);
238 GstStateChangeReturn ret;
240 switch (transition) {
241 case GST_STATE_CHANGE_NULL_TO_READY:
243 /* create and add autoconvert in bin */
244 if (!gst_auto_video_convert_add_autoconvert (autovideoconvert)) {
245 ret = GST_STATE_CHANGE_FAILURE;
248 /* get an updated list of factories */
249 gst_auto_video_convert_update_factory_list (autovideoconvert);
250 GST_DEBUG_OBJECT (autovideoconvert, "set factories list");
251 /* give factory list to autoconvert */
252 g_object_set (GST_ELEMENT (autovideoconvert->autoconvert), "factories",
260 ret = GST_ELEMENT_CLASS (gst_auto_video_convert_parent_class)->change_state
261 (element, transition);
262 if (ret == GST_STATE_CHANGE_FAILURE)
265 switch (transition) {
266 case GST_STATE_CHANGE_READY_TO_NULL:
268 gst_auto_video_convert_remove_autoconvert (autovideoconvert);