2bf7faa9468c771198fcd8aab781511578120b23
[platform/upstream/gstreamer.git] / gst / gsttypefind.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gsttypefind.c: 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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gsttypefind
24  * @short_description: Stream type detection
25  *
26  * The following functions allow you to detect the media type of an unknown
27  * stream.
28  *
29  * Last reviewed on 2005-11-09 (0.9.4)
30  */
31
32 #include "gst_private.h"
33 #include "gstinfo.h"
34 #include "gsttypefind.h"
35 #include "gstregistry.h"
36 #include "gsttypefindfactory.h"
37
38 GST_DEBUG_CATEGORY_EXTERN (type_find_debug);
39 #define GST_CAT_DEFAULT type_find_debug
40
41 G_DEFINE_POINTER_TYPE (GstTypeFind, gst_type_find);
42
43 /**
44  * gst_type_find_register:
45  * @plugin: A #GstPlugin, or NULL for a static typefind function
46  * @name: The name for registering
47  * @rank: The rank (or importance) of this typefind function
48  * @func: The #GstTypeFindFunction to use
49  * @extensions: (allow-none): Optional comma-separated list of extensions
50  *     that could belong to this type
51  * @possible_caps: Optionally the caps that could be returned when typefinding
52  *                 succeeds
53  * @data: Optional user data. This user data must be available until the plugin
54  *        is unloaded.
55  * @data_notify: a #GDestroyNotify that will be called on @data when the plugin
56  *        is unloaded.
57  *
58  * Registers a new typefind function to be used for typefinding. After
59  * registering this function will be available for typefinding.
60  * This function is typically called during an element's plugin initialization.
61  *
62  * Returns: TRUE on success, FALSE otherwise
63  */
64 gboolean
65 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
66     GstTypeFindFunction func, const gchar * extensions,
67     GstCaps * possible_caps, gpointer data, GDestroyNotify data_notify)
68 {
69   GstTypeFindFactory *factory;
70
71   g_return_val_if_fail (name != NULL, FALSE);
72
73   GST_INFO ("registering typefind function for %s", name);
74
75   factory = g_object_newv (GST_TYPE_TYPE_FIND_FACTORY, 0, NULL);
76   GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
77   g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
78
79   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
80   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
81
82   if (factory->extensions) {
83     g_strfreev (factory->extensions);
84     factory->extensions = NULL;
85   }
86   if (extensions)
87     factory->extensions = g_strsplit (extensions, ",", -1);
88
89   gst_caps_replace (&factory->caps, possible_caps);
90   factory->function = func;
91   factory->user_data = data;
92   factory->user_data_notify = data_notify;
93   if (plugin && plugin->desc.name) {
94     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
95     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
96     g_object_add_weak_pointer ((GObject *) plugin,
97         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
98   } else {
99     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
100     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
101   }
102   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
103
104   gst_registry_add_feature (gst_registry_get (),
105       GST_PLUGIN_FEATURE_CAST (factory));
106
107   return TRUE;
108 }
109
110 /*** typefind function interface **********************************************/
111
112 /**
113  * gst_type_find_peek:
114  * @find: The #GstTypeFind object the function was called with
115  * @offset: The offset
116  * @size: The number of bytes to return
117  *
118  * Returns the @size bytes of the stream to identify beginning at offset. If
119  * offset is a positive number, the offset is relative to the beginning of the
120  * stream, if offset is a negative number the offset is relative to the end of
121  * the stream. The returned memory is valid until the typefinding function
122  * returns and must not be freed.
123  *
124  * Returns: (transfer none) (array length=size): the requested data, or NULL
125  *     if that data is not available.
126  */
127 const guint8 *
128 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
129 {
130   g_return_val_if_fail (find->peek != NULL, NULL);
131
132   return find->peek (find->data, offset, size);
133 }
134
135 /**
136  * gst_type_find_suggest:
137  * @find: The #GstTypeFind object the function was called with
138  * @probability: The probability in percent that the suggestion is right
139  * @caps: The fixed #GstCaps to suggest
140  *
141  * If a #GstTypeFindFunction calls this function it suggests the caps with the
142  * given probability. A #GstTypeFindFunction may supply different suggestions
143  * in one call.
144  * It is up to the caller of the #GstTypeFindFunction to interpret these values.
145  */
146 void
147 gst_type_find_suggest (GstTypeFind * find, guint probability, GstCaps * caps)
148 {
149   g_return_if_fail (find->suggest != NULL);
150   g_return_if_fail (probability <= 100);
151   g_return_if_fail (caps != NULL);
152   g_return_if_fail (gst_caps_is_fixed (caps));
153
154   find->suggest (find->data, probability, caps);
155 }
156
157 /**
158  * gst_type_find_suggest_simple:
159  * @find: The #GstTypeFind object the function was called with
160  * @probability: The probability in percent that the suggestion is right
161  * @media_type: the media type of the suggested caps
162  * @fieldname: first field of the suggested caps, or NULL
163  * @...: additional arguments to the suggested caps in the same format as the
164  *     arguments passed to gst_structure_new() (ie. triplets of field name,
165  *     field GType and field value)
166  *
167  * If a #GstTypeFindFunction calls this function it suggests the caps with the
168  * given probability. A #GstTypeFindFunction may supply different suggestions
169  * in one call. It is up to the caller of the #GstTypeFindFunction to interpret
170  * these values.
171  *
172  * This function is similar to gst_type_find_suggest(), only that instead of
173  * passing a #GstCaps argument you can create the caps on the fly in the same
174  * way as you can with gst_caps_new_simple().
175  *
176  * Make sure you terminate the list of arguments with a NULL argument and that
177  * the values passed have the correct type (in terms of width in bytes when
178  * passed to the vararg function - this applies particularly to gdouble and
179  * guint64 arguments).
180  */
181 void
182 gst_type_find_suggest_simple (GstTypeFind * find, guint probability,
183     const char *media_type, const char *fieldname, ...)
184 {
185   GstStructure *structure;
186   va_list var_args;
187   GstCaps *caps;
188
189   g_return_if_fail (find->suggest != NULL);
190   g_return_if_fail (probability <= 100);
191   g_return_if_fail (media_type != NULL);
192
193   caps = gst_caps_new_empty ();
194
195   va_start (var_args, fieldname);
196   structure = gst_structure_new_valist (media_type, fieldname, var_args);
197   va_end (var_args);
198
199   gst_caps_append_structure (caps, structure);
200   g_return_if_fail (gst_caps_is_fixed (caps));
201
202   find->suggest (find->data, probability, caps);
203   gst_caps_unref (caps);
204 }
205
206 /**
207  * gst_type_find_get_length:
208  * @find: The #GstTypeFind the function was called with
209  *
210  * Get the length of the data stream.
211  *
212  * Returns: The length of the data stream, or 0 if it is not available.
213  */
214 guint64
215 gst_type_find_get_length (GstTypeFind * find)
216 {
217   if (find->get_length == NULL)
218     return 0;
219
220   return find->get_length (find->data);
221 }