2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2000,2005 Wim Taymans <wim@fluendo.com>
4 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:gsttypefindhelper
26 * @short_description: Utility functions for typefinding
28 * Utility functions for elements doing typefinding:
29 * gst_type_find_helper() does typefinding in pull mode, while
30 * gst_type_find_helper_for_buffer() is useful for elements needing to do
31 * typefinding in push mode from a chain function.
41 #include "gsttypefindhelper.h"
43 /* ********************** typefinding in pull mode ************************ */
46 helper_find_suggest (gpointer data, guint probability, const GstCaps * caps);
50 GSList *buffers; /* buffer cache */
53 GstTypeFindHelperGetRangeFunction func;
54 GstTypeFindProbability best_probability;
56 GstTypeFindFactory *factory; /* for logging */
57 GstObject *obj; /* for logging */
63 * @data: helper data struct
67 * Get data pointer within a stream. Keeps a cache of read buffers (partly
68 * for performance reasons, but mostly because pointers returned by us need
69 * to stay valid until typefinding has finished)
71 * Returns: address of the data or %NULL if buffer does not cover the
75 helper_find_peek (gpointer data, gint64 offset, guint size)
77 GstTypeFindHelper *helper;
80 GSList *insert_pos = NULL;
87 helper = (GstTypeFindHelper *) data;
89 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT
90 ", %u)", GST_OBJECT_NAME (helper->factory), offset, size);
96 if (helper->size == -1 || helper->size < -offset)
99 offset += helper->size;
102 /* see if we have a matching buffer already in our list */
103 if (size > 0 && offset <= helper->last_offset) {
106 for (walk = helper->buffers; walk; walk = walk->next) {
107 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
108 guint64 buf_offset = GST_BUFFER_OFFSET (buf);
109 guint buf_size = gst_buffer_get_size (buf);
111 /* buffers are kept sorted by end offset (highest first) in the list, so
112 * at this point we save the current position and stop searching if
113 * we're after the searched end offset */
114 if (buf_offset <= offset) {
115 if ((offset + size) < (buf_offset + buf_size)) {
118 /* FIXME, unmap after usage */
119 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_READ);
121 return data + (offset - buf_offset);
123 } else if (offset + size >= buf_offset + buf_size) {
131 /* some typefinders go in 1 byte steps over 1k of data and request
132 * small buffers. It is really inefficient to pull each time, and pulling
133 * a larger chunk is almost free. Trying to pull a larger chunk at the end
134 * of the file is also not a problem here, we'll just get a truncated buffer
135 * in that case (and we'll have to double-check the size we actually get
136 * anyway, see below) */
138 helper->func (helper->obj, helper->parent, offset, MAX (size, 4096),
141 if (ret != GST_FLOW_OK)
145 caps = GST_BUFFER_CAPS (buffer);
147 if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) {
148 GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability",
151 gst_caps_replace (&helper->caps, caps);
152 helper->best_probability = GST_TYPE_FIND_MAXIMUM;
154 gst_buffer_unref (buffer);
159 /* getrange might silently return shortened buffers at the end of a file,
160 * we must, however, always return either the full requested data or NULL */
161 buf_offset = GST_BUFFER_OFFSET (buffer);
162 buf_size = gst_buffer_get_size (buffer);
164 if ((buf_offset != -1 && buf_offset != offset) || buf_size < size) {
165 GST_DEBUG ("dropping short buffer: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
166 " instead of %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
167 buf_offset, buf_offset + buf_size - 1, offset, offset + size - 1);
168 gst_buffer_unref (buffer);
174 g_slist_insert_before (helper->buffers, insert_pos, buffer);
176 /* if insert_pos is not set, our offset is bigger than the largest offset
177 * we have so far; since we keep the list sorted with highest offsets
178 * first, we need to prepend the buffer to the list */
179 helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
180 helper->buffers = g_slist_prepend (helper->buffers, buffer);
184 return gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
188 GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret));
194 * helper_find_suggest:
195 * @data: helper data struct
196 * @probability: probability of the match
197 * @caps: caps of the type
199 * If given @probability is higher, replace previously store caps.
202 helper_find_suggest (gpointer data, GstTypeFindProbability probability,
203 const GstCaps * caps)
205 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
207 GST_LOG_OBJECT (helper->obj,
208 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
209 GST_OBJECT_NAME (helper->factory), probability, caps);
211 if (probability > helper->best_probability) {
212 GstCaps *copy = gst_caps_copy (caps);
214 gst_caps_replace (&helper->caps, copy);
215 gst_caps_unref (copy);
216 helper->best_probability = probability;
221 helper_find_get_length (gpointer data)
223 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
225 GST_LOG_OBJECT (helper->obj, "'%s' called get_length, returning %"
226 G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
232 * gst_type_find_helper_get_range:
233 * @obj: A #GstObject that will be passed as first argument to @func
234 * @parent: the parent of @obj or NULL
235 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
236 * be used to access data at random offsets when doing the typefinding
237 * @size: The length in bytes
238 * @extension: extension of the media
239 * @prob: (out) (allow-none): location to store the probability of the found
242 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
243 * however, this function will use the specified function @func to obtain the
244 * data needed by the typefind functions, rather than operating on a given
245 * source pad. This is useful mostly for elements like tag demuxers which
246 * strip off data at the beginning and/or end of a file and want to typefind
247 * the stripped data stream before adding their own source pad (the specified
248 * callback can then call the upstream peer pad with offsets adjusted for the
249 * tag size, for example).
251 * When @extension is not NULL, this function will first try the typefind
252 * functions for the given extension, which might speed up the typefinding
255 * Free-function: gst_caps_unref
257 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
258 * Returns #NULL if no #GstCaps matches the data stream.
263 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
264 GstTypeFindHelperGetRangeFunction func, guint64 size,
265 const gchar * extension, GstTypeFindProbability * prob)
267 GstTypeFindHelper helper;
270 GList *l, *type_list;
271 GstCaps *result = NULL;
274 g_return_val_if_fail (GST_IS_OBJECT (obj), NULL);
275 g_return_val_if_fail (func != NULL, NULL);
277 helper.buffers = NULL;
279 helper.last_offset = 0;
281 helper.best_probability = GST_TYPE_FIND_NONE;
284 helper.parent = parent;
287 find.peek = helper_find_peek;
288 find.suggest = helper_find_suggest;
290 if (size == 0 || size == (guint64) - 1) {
291 find.get_length = NULL;
293 find.get_length = helper_find_get_length;
296 type_list = gst_type_find_factory_get_list ();
298 /* move the typefinders for the extension first in the list. The idea is that
299 * when one of them returns MAX we don't need to search further as there is a
300 * very high chance we got the right type. */
304 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head",
307 for (l = type_list; l; l = next) {
308 GstTypeFindFactory *factory;
314 factory = GST_TYPE_FIND_FACTORY (l->data);
316 ext = gst_type_find_factory_get_extensions (factory);
320 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
321 GST_OBJECT_NAME (factory), extension);
323 for (i = 0; ext[i]; i++) {
324 if (strcmp (ext[i], extension) == 0) {
325 /* found extension, move in front */
326 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
328 /* remove entry from list */
329 type_list = g_list_delete_link (type_list, l);
330 /* insert at the position */
331 type_list = g_list_insert (type_list, factory, pos);
332 /* next element will be inserted after this one */
340 for (l = type_list; l; l = l->next) {
341 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
342 gst_type_find_factory_call_function (helper.factory, &find);
343 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
346 gst_plugin_feature_list_free (type_list);
348 for (walk = helper.buffers; walk; walk = walk->next)
349 gst_buffer_unref (GST_BUFFER_CAST (walk->data));
350 g_slist_free (helper.buffers);
352 if (helper.best_probability > 0)
353 result = helper.caps;
356 *prob = helper.best_probability;
358 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
359 result, (guint) helper.best_probability);
365 * gst_type_find_helper:
366 * @src: A source #GstPad
367 * @size: The length in bytes
369 * Tries to find what type of data is flowing from the given source #GstPad.
371 * Free-function: gst_caps_unref
373 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
374 * Returns #NULL if no #GstCaps matches the data stream.
378 gst_type_find_helper (GstPad * src, guint64 size)
380 GstTypeFindHelperGetRangeFunction func;
382 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
383 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
385 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
387 return gst_type_find_helper_get_range (GST_OBJECT (src),
388 GST_OBJECT_PARENT (src), func, size, NULL, NULL);
391 /* ********************** typefinding for buffers ************************* */
395 const guint8 *data; /* buffer data */
397 GstTypeFindProbability best_probability;
399 GstTypeFindFactory *factory; /* for logging */
400 GstObject *obj; /* for logging */
401 } GstTypeFindBufHelper;
404 * buf_helper_find_peek:
405 * @data: helper data struct
406 * @off: stream offset
409 * Get data pointer within a buffer.
411 * Returns: address inside the buffer or %NULL if buffer does not cover the
414 static const guint8 *
415 buf_helper_find_peek (gpointer data, gint64 off, guint size)
417 GstTypeFindBufHelper *helper;
419 helper = (GstTypeFindBufHelper *) data;
420 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
421 GST_OBJECT_NAME (helper->factory), off, size);
427 GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
428 GST_OBJECT_NAME (helper->factory));
432 if ((off + size) <= helper->size)
433 return helper->data + off;
439 * buf_helper_find_suggest:
440 * @data: helper data struct
441 * @probability: probability of the match
442 * @caps: caps of the type
444 * If given @probability is higher, replace previously store caps.
447 buf_helper_find_suggest (gpointer data, GstTypeFindProbability probability,
448 const GstCaps * caps)
450 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
452 GST_LOG_OBJECT (helper->obj,
453 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
454 GST_OBJECT_NAME (helper->factory), probability, caps);
456 /* Note: not >= as we call typefinders in order of rank, highest first */
457 if (probability > helper->best_probability) {
458 GstCaps *copy = gst_caps_copy (caps);
460 gst_caps_replace (&helper->caps, copy);
461 gst_caps_unref (copy);
462 helper->best_probability = probability;
467 * gst_type_find_helper_for_data:
468 * @obj: object doing the typefinding, or NULL (used for logging)
469 * @data: (in) (transfer none): a pointer with data to typefind
470 * @size: (in) (transfer none): the size of @data
471 * @prob: (out) (allow-none): location to store the probability of the found
474 * Tries to find what type of data is contained in the given @data, the
475 * assumption being that the data represents the beginning of the stream or
478 * All available typefinders will be called on the data in order of rank. If
479 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
480 * typefinding is stopped immediately and the found caps will be returned
481 * right away. Otherwise, all available typefind functions will the tried,
482 * and the caps with the highest probability will be returned, or #NULL if
483 * the content of @data could not be identified.
485 * Free-function: gst_caps_unref
487 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
488 * if no type could be found. The caller should free the caps returned
489 * with gst_caps_unref().
492 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
493 GstTypeFindProbability * prob)
495 GstTypeFindBufHelper helper;
497 GList *l, *type_list;
498 GstCaps *result = NULL;
500 g_return_val_if_fail (data != NULL, NULL);
504 helper.best_probability = GST_TYPE_FIND_NONE;
508 if (helper.data == NULL || helper.size == 0)
512 find.peek = buf_helper_find_peek;
513 find.suggest = buf_helper_find_suggest;
514 find.get_length = NULL;
516 type_list = gst_type_find_factory_get_list ();
518 for (l = type_list; l; l = l->next) {
519 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
520 gst_type_find_factory_call_function (helper.factory, &find);
521 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
524 gst_plugin_feature_list_free (type_list);
526 if (helper.best_probability > 0)
527 result = helper.caps;
530 *prob = helper.best_probability;
532 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
533 result, (guint) helper.best_probability);
539 * gst_type_find_helper_for_buffer:
540 * @obj: object doing the typefinding, or NULL (used for logging)
541 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
542 * @prob: (out) (allow-none): location to store the probability of the found
545 * Tries to find what type of data is contained in the given #GstBuffer, the
546 * assumption being that the buffer represents the beginning of the stream or
549 * All available typefinders will be called on the data in order of rank. If
550 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
551 * typefinding is stopped immediately and the found caps will be returned
552 * right away. Otherwise, all available typefind functions will the tried,
553 * and the caps with the highest probability will be returned, or #NULL if
554 * the content of the buffer could not be identified.
556 * Free-function: gst_caps_unref
558 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
559 * if no type could be found. The caller should free the caps returned
560 * with gst_caps_unref().
563 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
564 GstTypeFindProbability * prob)
570 g_return_val_if_fail (buf != NULL, NULL);
571 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
572 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
573 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
575 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
576 result = gst_type_find_helper_for_data (obj, data, size, prob);
577 gst_buffer_unmap (buf, data, size);
583 * gst_type_find_helper_for_extension:
584 * @obj: (allow-none): object doing the typefinding, or NULL (used for logging)
585 * @extension: an extension
587 * Tries to find the best #GstCaps associated with @extension.
589 * All available typefinders will be checked against the extension in order
590 * of rank. The caps of the first typefinder that can handle @extension will be
593 * Free-function: gst_caps_unref
595 * Returns: (transfer full): the #GstCaps corresponding to @extension, or
596 * #NULL if no type could be found. The caller should free the caps
597 * returned with gst_caps_unref().
602 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
604 GList *l, *type_list;
605 GstCaps *result = NULL;
607 g_return_val_if_fail (extension != NULL, NULL);
609 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
611 type_list = gst_type_find_factory_get_list ();
613 for (l = type_list; l; l = g_list_next (l)) {
614 GstTypeFindFactory *factory;
618 factory = GST_TYPE_FIND_FACTORY (l->data);
620 /* we only want to check those factories without a function */
621 if (factory->function != NULL)
624 /* get the extension that this typefind factory can handle */
625 ext = gst_type_find_factory_get_extensions (factory);
629 /* there are extension, see if one of them matches the requested
631 for (i = 0; ext[i]; i++) {
632 if (strcmp (ext[i], extension) == 0) {
633 /* we found a matching extension, take the caps */
634 if ((result = gst_type_find_factory_get_caps (factory))) {
635 gst_caps_ref (result);
642 gst_plugin_feature_list_free (type_list);
644 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);