7e0ea9eae95cc645227153533bcb094842687241
[platform/upstream/gstreamer.git] / gst / autoconvert / gstautovideoconvert.c
1 /* 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
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 /*
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
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <string.h>
34
35 #include "gstautovideoconvert.h"
36
37 GST_DEBUG_CATEGORY (autovideoconvert_debug);
38 #define GST_CAT_DEFAULT (autovideoconvert_debug)
39
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 */
43
44 /* element factory information */
45 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS_ANY);
49
50 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS_ANY);
54
55
56 static GstStateChangeReturn gst_auto_video_convert_change_state (GstElement *
57     element, GstStateChange transition);
58
59 void gst_auto_video_convert_update_factory_list (GstAutoVideoConvert *
60     autovideoconvert);
61
62 static gboolean
63 gst_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
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)));
80     return TRUE;
81   }
82   return FALSE;
83 }
84
85
86 static GList *
87 gst_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
103 void
104 gst_auto_video_convert_update_factory_list (GstAutoVideoConvert *
105     autovideoconvert)
106 {
107   /* use a static mutex to protect factories list and factories cookie */
108   g_mutex_lock (&factories_mutex);
109
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
130   g_mutex_unlock (&factories_mutex);
131 }
132
133 G_DEFINE_TYPE (GstAutoVideoConvert, gst_auto_video_convert, GST_TYPE_BIN);
134
135 static void
136 gst_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_static_pad_template (gstelement_class, &srctemplate);
144   gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
145
146   gst_element_class_set_static_metadata (gstelement_class,
147       "Select color space convertor based on caps", "Generic/Bin",
148       "Selects the right color space convertor based on the caps",
149       "Benjamin Gaignard <benjamin.gaignard@stericsson.com>");
150
151   gstelement_class->change_state =
152       GST_DEBUG_FUNCPTR (gst_auto_video_convert_change_state);
153
154 }
155
156 static gboolean
157 gst_auto_video_convert_add_autoconvert (GstAutoVideoConvert * autovideoconvert)
158 {
159   GstPad *pad;
160
161   if (autovideoconvert->autoconvert)
162     return TRUE;
163
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");
169     return FALSE;
170   }
171
172   /* first add autoconvert in bin */
173   gst_bin_add (GST_BIN (autovideoconvert),
174       gst_object_ref (autovideoconvert->autoconvert));
175
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),
179       pad);
180   gst_object_unref (pad);
181
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);
186
187   return TRUE;
188 }
189
190 static void
191 gst_auto_video_convert_remove_autoconvert (GstAutoVideoConvert *
192     autovideoconvert)
193 {
194   if (!autovideoconvert->autoconvert)
195     return;
196
197   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->srcpad),
198       NULL);
199   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (autovideoconvert->sinkpad),
200       NULL);
201
202   gst_bin_remove (GST_BIN (autovideoconvert), autovideoconvert->autoconvert);
203   gst_object_unref (autovideoconvert->autoconvert);
204   autovideoconvert->autoconvert = NULL;
205 }
206
207 static void
208 gst_auto_video_convert_init (GstAutoVideoConvert * autovideoconvert)
209 {
210   GstPadTemplate *pad_tmpl;
211
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);
220
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);
229
230   return;
231 }
232
233 static GstStateChangeReturn
234 gst_auto_video_convert_change_state (GstElement * element,
235     GstStateChange transition)
236 {
237   GstAutoVideoConvert *autovideoconvert = GST_AUTO_VIDEO_CONVERT (element);
238   GstStateChangeReturn ret;
239
240   switch (transition) {
241     case GST_STATE_CHANGE_NULL_TO_READY:
242     {
243       /* create and add autoconvert in bin */
244       if (!gst_auto_video_convert_add_autoconvert (autovideoconvert)) {
245         ret = GST_STATE_CHANGE_FAILURE;
246         return ret;
247       }
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",
253           factories, NULL);
254       break;
255     }
256     default:
257       break;
258   }
259
260   ret = GST_ELEMENT_CLASS (gst_auto_video_convert_parent_class)->change_state
261       (element, transition);
262   if (ret == GST_STATE_CHANGE_FAILURE)
263     return ret;
264
265   switch (transition) {
266     case GST_STATE_CHANGE_READY_TO_NULL:
267     {
268       gst_auto_video_convert_remove_autoconvert (autovideoconvert);
269       break;
270     }
271     default:
272       break;
273   }
274
275   return ret;
276 }