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",
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);
134 GST_ELEMENT_REGISTER_DEFINE (autovideoconvert, "autovideoconvert",
135 GST_RANK_NONE, GST_TYPE_AUTO_VIDEO_CONVERT);
138 gst_auto_video_convert_class_init (GstAutoVideoConvertClass * klass)
140 GstElementClass *gstelement_class = (GstElementClass *) klass;
142 GST_DEBUG_CATEGORY_INIT (autovideoconvert_debug, "autovideoconvert", 0,
143 "Auto color space converter");
145 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
146 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
148 gst_element_class_set_static_metadata (gstelement_class,
149 "Select color space converter based on caps", "Generic/Bin",
150 "Selects the right color space converter based on the caps",
151 "Benjamin Gaignard <benjamin.gaignard@stericsson.com>");
153 gstelement_class->change_state =
154 GST_DEBUG_FUNCPTR (gst_auto_video_convert_change_state);
159 gst_auto_video_convert_add_autoconvert (GstAutoVideoConvert * autovideoconvert)
163 if (autovideoconvert->autoconvert)
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");
174 /* first add autoconvert in bin */
175 gst_bin_add (GST_BIN (autovideoconvert),
176 gst_object_ref (autovideoconvert->autoconvert));
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),
182 gst_object_unref (pad);
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);
193 gst_auto_video_convert_remove_autoconvert (GstAutoVideoConvert *
196 if (!autovideoconvert->autoconvert)
199 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->srcpad),
201 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->sinkpad),
204 gst_bin_remove (GST_BIN (autovideoconvert), autovideoconvert->autoconvert);
205 gst_object_unref (autovideoconvert->autoconvert);
206 autovideoconvert->autoconvert = NULL;
210 gst_auto_video_convert_init (GstAutoVideoConvert * autovideoconvert)
212 GstPadTemplate *pad_tmpl;
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);
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);
235 static GstStateChangeReturn
236 gst_auto_video_convert_change_state (GstElement * element,
237 GstStateChange transition)
239 GstAutoVideoConvert *autovideoconvert = GST_AUTO_VIDEO_CONVERT (element);
240 GstStateChangeReturn ret;
242 switch (transition) {
243 case GST_STATE_CHANGE_NULL_TO_READY:
245 /* create and add autoconvert in bin */
246 if (!gst_auto_video_convert_add_autoconvert (autovideoconvert)) {
247 ret = GST_STATE_CHANGE_FAILURE;
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",
262 ret = GST_ELEMENT_CLASS (gst_auto_video_convert_parent_class)->change_state
263 (element, transition);
264 if (ret == GST_STATE_CHANGE_FAILURE)
267 switch (transition) {
268 case GST_STATE_CHANGE_READY_TO_NULL:
270 gst_auto_video_convert_remove_autoconvert (autovideoconvert);