don't mix tabs and spaces
[platform/upstream/gstreamer.git] / gst / gsttypefind.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gsttypefind.h: typefinding subsystem
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "gstinfo.h"
23 #include "gsttypefind.h"
24 #include "gstregistrypool.h"
25
26 GST_DEBUG_CATEGORY_STATIC (gst_type_find_debug);
27 #define GST_CAT_DEFAULT gst_type_find_debug
28
29 static void gst_type_find_factory_class_init (gpointer g_class,
30     gpointer class_data);
31 static void gst_type_find_factory_init (GTypeInstance * instance,
32     gpointer g_class);
33 static void gst_type_find_factory_dispose (GObject * object);
34
35 static void gst_type_find_factory_unload_thyself (GstPluginFeature * feature);
36
37 static void gst_type_find_load_plugin (GstTypeFind * find, gpointer data);
38
39 static GstPluginFeatureClass *parent_class = NULL;
40
41 GType
42 gst_type_find_factory_get_type (void)
43 {
44   static GType typefind_type = 0;
45
46   if (!typefind_type) {
47     static const GTypeInfo typefind_info = {
48       sizeof (GstTypeFindFactoryClass),
49       NULL,
50       NULL,
51       gst_type_find_factory_class_init,
52       NULL,
53       NULL,
54       sizeof (GstTypeFindFactory),
55       0,
56       gst_type_find_factory_init,
57       NULL
58     };
59
60     typefind_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
61         "GstTypeFindFactory", &typefind_info, 0);
62     GST_DEBUG_CATEGORY_INIT (gst_type_find_debug, "GST_TYPEFIND",
63         GST_DEBUG_FG_GREEN, "typefinding subsystem");
64   }
65
66   return typefind_type;
67 }
68 static void
69 gst_type_find_factory_class_init (gpointer g_class, gpointer class_data)
70 {
71   GstPluginFeatureClass *gstpluginfeature_class =
72       GST_PLUGIN_FEATURE_CLASS (g_class);
73   GObjectClass *object_class = G_OBJECT_CLASS (g_class);
74
75   parent_class = g_type_class_peek_parent (g_class);
76
77   object_class->dispose = gst_type_find_factory_dispose;
78
79   gstpluginfeature_class->unload_thyself =
80       GST_DEBUG_FUNCPTR (gst_type_find_factory_unload_thyself);
81 }
82 static void
83 gst_type_find_factory_init (GTypeInstance * instance, gpointer g_class)
84 {
85   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (instance);
86
87   factory->user_data = factory;
88   factory->function = gst_type_find_load_plugin;
89 }
90 static void
91 gst_type_find_factory_dispose (GObject * object)
92 {
93   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (object);
94
95   if (factory->caps) {
96     gst_caps_free (factory->caps);
97     factory->caps = NULL;
98   }
99   if (factory->extensions) {
100     g_strfreev (factory->extensions);
101     factory->extensions = NULL;
102   }
103 }
104 static void
105 gst_type_find_factory_unload_thyself (GstPluginFeature * feature)
106 {
107   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
108
109   factory->function = gst_type_find_load_plugin;
110   factory->user_data = factory;
111 }
112 static void
113 gst_type_find_load_plugin (GstTypeFind * find, gpointer data)
114 {
115   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (data);
116
117   GST_DEBUG_OBJECT (factory, "need to load typefind function %s",
118       GST_PLUGIN_FEATURE_NAME (factory));
119
120   if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
121     if (factory->function == gst_type_find_load_plugin) {
122       /* looks like we didn't get a real typefind function */
123       g_warning ("could not load valid typefind function for feature '%s'\n",
124           GST_PLUGIN_FEATURE_NAME (factory));
125     } else {
126       g_assert (factory->function);
127       gst_type_find_factory_call_function (factory, find);
128     }
129   }
130 }
131
132 /**
133  * gst_type_find_factory_get_list:
134  *
135  * Gets the list of all registered typefind factories. You must free the
136  * list using g_list_free.
137  * 
138  * Returns: the list of all registered typefind factories
139  */
140 GList *
141 gst_type_find_factory_get_list (void)
142 {
143   return gst_registry_pool_feature_list (GST_TYPE_TYPE_FIND_FACTORY);
144 }
145
146 /**
147  * gst_type_find_factory_get_caps:
148  * @factory: a factory
149  * 
150  * Gets the caps associated with a typefind factory.
151  *
152  * Returns: the #GstCaps associated with this factory
153  */
154 const GstCaps *
155 gst_type_find_factory_get_caps (const GstTypeFindFactory * factory)
156 {
157   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
158
159   return factory->caps;
160 }
161
162 /**
163  * gst_type_find_factory_get_extensions:
164  * @factory: a factory
165  * 
166  * Gets the extensions associated with a typefind factory. The returned
167  * array should not be changed. If you need to change stuff in it, you should
168  * copy it using g_stdupv().  This function may return NULL to indicate
169  * a 0-length list.
170  *
171  * Returns: a NULL-terminated array of extensions associated with this factory
172  */
173 gchar **
174 gst_type_find_factory_get_extensions (const GstTypeFindFactory * factory)
175 {
176   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
177
178   return factory->extensions;
179 }
180
181 /**
182  * gst_type_find_factory_call_function:
183  * @factory: a factory
184  * @find: a properly setup #GstTypeFind entry. The get_data and suggest_type
185  *        members must be set.
186  * 
187  * Calls the typefinding function associated with this factory.
188  */
189 void
190 gst_type_find_factory_call_function (const GstTypeFindFactory * factory,
191     GstTypeFind * find)
192 {
193   g_return_if_fail (GST_IS_TYPE_FIND_FACTORY (factory));
194   g_return_if_fail (find != NULL);
195   g_return_if_fail (find->peek != NULL);
196   g_return_if_fail (find->suggest != NULL);
197
198   /* should never happen */
199   g_assert (factory->function != NULL);
200
201   factory->function (find, factory->user_data);
202 }
203
204 /**
205  * gst_type_find_register:
206  * @plugin: the GstPlugin to register with
207  * @name: the name for registering
208  * @rank: rank (or importance) of this typefind function
209  * @func: the function to use for typefinding
210  * @extensions: optional extensions that could belong to this type
211  * @possible_caps: optionally the caps that could be returned when typefinding succeeds
212  * @data: optional user data. This user data must be available until the plugin 
213  *        is unloaded.
214  *
215  * Registers a new typefind function to be used for typefinding. After 
216  * registering this function will be available for typefinding.
217  * This function is typically called during an element's plugin initialization.
218  *
219  * Returns: TRUE on success, FALSE otherwise
220  */
221 gboolean
222 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
223     GstTypeFindFunction func, gchar ** extensions,
224     const GstCaps * possible_caps, gpointer data)
225 {
226   GstTypeFindFactory *factory;
227
228   g_return_val_if_fail (plugin != NULL, FALSE);
229   g_return_val_if_fail (name != NULL, FALSE);
230   g_return_val_if_fail (func != NULL, FALSE);
231
232   GST_INFO ("registering typefind function for %s", name);
233   factory =
234       GST_TYPE_FIND_FACTORY (gst_registry_pool_find_feature (name,
235           GST_TYPE_TYPE_FIND_FACTORY));
236   if (!factory) {
237     factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
238     GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
239     g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
240     gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
241   } else {
242     GST_DEBUG_OBJECT (factory, "using old typefind factory for %s", name);
243   }
244
245   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
246   if (factory->extensions)
247     g_strfreev (factory->extensions);
248
249   factory->extensions = g_strdupv (extensions);
250   gst_caps_replace (&factory->caps, gst_caps_copy (possible_caps));
251   factory->function = func;
252   factory->user_data = data;
253
254   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
255
256   return TRUE;
257 }
258
259 /*** typefind function interface **********************************************/
260
261 /**
262  * gst_type_find_peek:
263  * @find: the find object the function was called with
264  * @offset: the offset
265  * @size: the number of bytes to return
266  *
267  * Returns size bytes of the stream to identify beginning at offset. If offset 
268  * is a positive number, the offset is relative to the beginning of the stream,
269  * if offset is a negative number the offset is relative to the end of the 
270  * stream. The returned memory is valid until the typefinding function returns
271  * and must not be freed.
272  * If NULL is returned, that data is not available.
273  *
274  * Returns: the requested data or NULL if that data is not available.
275  */
276 guint8 *
277 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
278 {
279   g_return_val_if_fail (find->peek != NULL, NULL);
280
281   return find->peek (find->data, offset, size);
282 }
283
284 /**
285  * gst_type_find_suggest:
286  * @find: the find object the function was called with
287  * @probability: the probability in percent that the suggestion is right
288  * @caps: the fixed caps to suggest
289  *
290  * If a typefind function calls this function it suggests the caps with the
291  * given probability. A typefind function may supply different suggestions
292  * in one call.
293  * It is up to the caller of the typefind function to interpret these values.
294  */
295 void
296 gst_type_find_suggest (GstTypeFind * find, guint probability,
297     const GstCaps * caps)
298 {
299   g_return_if_fail (find->suggest != NULL);
300   g_return_if_fail (probability <= 100);
301   g_return_if_fail (caps != NULL);
302   g_return_if_fail (gst_caps_is_fixed (caps));
303
304   find->suggest (find->data, probability, caps);
305 }
306
307 /**
308  * gst_type_find_get_length:
309  * @find: the find object the function was called with
310  *
311  * Get the length of the data stream.
312  * 
313  * Returns: the length of the data stream or 0 if it is not available.
314  */
315 guint64
316 gst_type_find_get_length (GstTypeFind * find)
317 {
318   if (find->get_length == NULL)
319     return 0;
320
321   return find->get_length (find->data);
322 }