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;
88 helper = (GstTypeFindHelper *) data;
90 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT
91 ", %u)", GST_OBJECT_NAME (helper->factory), offset, size);
97 if (helper->size == -1 || helper->size < -offset)
100 offset += helper->size;
103 /* see if we have a matching buffer already in our list */
104 if (size > 0 && offset <= helper->last_offset) {
107 for (walk = helper->buffers; walk; walk = walk->next) {
108 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
109 guint64 buf_offset = GST_BUFFER_OFFSET (buf);
110 guint buf_size = gst_buffer_get_size (buf);
112 /* buffers are kept sorted by end offset (highest first) in the list, so
113 * at this point we save the current position and stop searching if
114 * we're after the searched end offset */
115 if (buf_offset <= offset) {
116 if ((offset + size) < (buf_offset + buf_size)) {
117 /* FIXME, unmap after usage */
118 g_assert (gst_buffer_map (buf, &info, GST_MAP_READ));
120 return (guint8 *) info.data + (offset - buf_offset);
122 } else if (offset + size >= buf_offset + buf_size) {
130 /* some typefinders go in 1 byte steps over 1k of data and request
131 * small buffers. It is really inefficient to pull each time, and pulling
132 * a larger chunk is almost free. Trying to pull a larger chunk at the end
133 * of the file is also not a problem here, we'll just get a truncated buffer
134 * in that case (and we'll have to double-check the size we actually get
135 * anyway, see below) */
137 helper->func (helper->obj, helper->parent, offset, MAX (size, 4096),
140 if (ret != GST_FLOW_OK)
144 caps = GST_BUFFER_CAPS (buffer);
146 if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) {
147 GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability",
150 gst_caps_replace (&helper->caps, caps);
151 helper->best_probability = GST_TYPE_FIND_MAXIMUM;
153 gst_buffer_unref (buffer);
158 /* getrange might silently return shortened buffers at the end of a file,
159 * we must, however, always return either the full requested data or NULL */
160 buf_offset = GST_BUFFER_OFFSET (buffer);
161 buf_size = gst_buffer_get_size (buffer);
163 if ((buf_offset != -1 && buf_offset != offset) || buf_size < size) {
164 GST_DEBUG ("dropping short buffer: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
165 " instead of %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
166 buf_offset, buf_offset + buf_size - 1, offset, offset + size - 1);
167 gst_buffer_unref (buffer);
173 g_slist_insert_before (helper->buffers, insert_pos, buffer);
175 /* if insert_pos is not set, our offset is bigger than the largest offset
176 * we have so far; since we keep the list sorted with highest offsets
177 * first, we need to prepend the buffer to the list */
178 helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
179 helper->buffers = g_slist_prepend (helper->buffers, buffer);
183 g_assert (gst_buffer_map (buffer, &info, 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_take (&helper->caps, copy);
215 helper->best_probability = probability;
220 helper_find_get_length (gpointer data)
222 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
224 GST_LOG_OBJECT (helper->obj, "'%s' called get_length, returning %"
225 G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
231 * gst_type_find_helper_get_range:
232 * @obj: A #GstObject that will be passed as first argument to @func
233 * @parent: the parent of @obj or NULL
234 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
235 * be used to access data at random offsets when doing the typefinding
236 * @size: The length in bytes
237 * @extension: extension of the media
238 * @prob: (out) (allow-none): location to store the probability of the found
241 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
242 * however, this function will use the specified function @func to obtain the
243 * data needed by the typefind functions, rather than operating on a given
244 * source pad. This is useful mostly for elements like tag demuxers which
245 * strip off data at the beginning and/or end of a file and want to typefind
246 * the stripped data stream before adding their own source pad (the specified
247 * callback can then call the upstream peer pad with offsets adjusted for the
248 * tag size, for example).
250 * When @extension is not NULL, this function will first try the typefind
251 * functions for the given extension, which might speed up the typefinding
254 * Free-function: gst_caps_unref
256 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
257 * Returns #NULL if no #GstCaps matches the data stream.
262 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
263 GstTypeFindHelperGetRangeFunction func, guint64 size,
264 const gchar * extension, GstTypeFindProbability * prob)
266 GstTypeFindHelper helper;
269 GList *l, *type_list;
270 GstCaps *result = NULL;
273 g_return_val_if_fail (GST_IS_OBJECT (obj), NULL);
274 g_return_val_if_fail (func != NULL, NULL);
276 helper.buffers = NULL;
278 helper.last_offset = 0;
280 helper.best_probability = GST_TYPE_FIND_NONE;
283 helper.parent = parent;
286 find.peek = helper_find_peek;
287 find.suggest = helper_find_suggest;
289 if (size == 0 || size == (guint64) - 1) {
290 find.get_length = NULL;
292 find.get_length = helper_find_get_length;
295 type_list = gst_type_find_factory_get_list ();
297 /* move the typefinders for the extension first in the list. The idea is that
298 * when one of them returns MAX we don't need to search further as there is a
299 * very high chance we got the right type. */
303 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head",
306 for (l = type_list; l; l = next) {
307 GstTypeFindFactory *factory;
313 factory = GST_TYPE_FIND_FACTORY (l->data);
315 ext = gst_type_find_factory_get_extensions (factory);
319 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
320 GST_OBJECT_NAME (factory), extension);
322 for (i = 0; ext[i]; i++) {
323 if (strcmp (ext[i], extension) == 0) {
324 /* found extension, move in front */
325 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
327 /* remove entry from list */
328 type_list = g_list_delete_link (type_list, l);
329 /* insert at the position */
330 type_list = g_list_insert (type_list, factory, pos);
331 /* next element will be inserted after this one */
339 for (l = type_list; l; l = l->next) {
340 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
341 gst_type_find_factory_call_function (helper.factory, &find);
342 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
345 gst_plugin_feature_list_free (type_list);
347 for (walk = helper.buffers; walk; walk = walk->next)
348 gst_buffer_unref (GST_BUFFER_CAST (walk->data));
349 g_slist_free (helper.buffers);
351 if (helper.best_probability > 0)
352 result = helper.caps;
355 *prob = helper.best_probability;
357 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
358 result, (guint) helper.best_probability);
364 * gst_type_find_helper:
365 * @src: A source #GstPad
366 * @size: The length in bytes
368 * Tries to find what type of data is flowing from the given source #GstPad.
370 * Free-function: gst_caps_unref
372 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
373 * Returns #NULL if no #GstCaps matches the data stream.
377 gst_type_find_helper (GstPad * src, guint64 size)
379 GstTypeFindHelperGetRangeFunction func;
381 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
382 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
384 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
386 return gst_type_find_helper_get_range (GST_OBJECT (src),
387 GST_OBJECT_PARENT (src), func, size, NULL, NULL);
390 /* ********************** typefinding for buffers ************************* */
394 const guint8 *data; /* buffer data */
396 GstTypeFindProbability best_probability;
398 GstTypeFindFactory *factory; /* for logging */
399 GstObject *obj; /* for logging */
400 } GstTypeFindBufHelper;
403 * buf_helper_find_peek:
404 * @data: helper data struct
405 * @off: stream offset
408 * Get data pointer within a buffer.
410 * Returns: address inside the buffer or %NULL if buffer does not cover the
413 static const guint8 *
414 buf_helper_find_peek (gpointer data, gint64 off, guint size)
416 GstTypeFindBufHelper *helper;
418 helper = (GstTypeFindBufHelper *) data;
419 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
420 GST_OBJECT_NAME (helper->factory), off, size);
426 GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
427 GST_OBJECT_NAME (helper->factory));
431 if ((off + size) <= helper->size)
432 return helper->data + off;
438 * buf_helper_find_suggest:
439 * @data: helper data struct
440 * @probability: probability of the match
441 * @caps: caps of the type
443 * If given @probability is higher, replace previously store caps.
446 buf_helper_find_suggest (gpointer data, GstTypeFindProbability probability,
447 const GstCaps * caps)
449 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
451 GST_LOG_OBJECT (helper->obj,
452 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
453 GST_OBJECT_NAME (helper->factory), probability, caps);
455 /* Note: not >= as we call typefinders in order of rank, highest first */
456 if (probability > helper->best_probability) {
457 GstCaps *copy = gst_caps_copy (caps);
459 gst_caps_replace (&helper->caps, copy);
460 gst_caps_unref (copy);
461 helper->best_probability = probability;
466 * gst_type_find_helper_for_data:
467 * @obj: object doing the typefinding, or NULL (used for logging)
468 * @data: (in) (transfer none): a pointer with data to typefind
469 * @size: (in) (transfer none): the size of @data
470 * @prob: (out) (allow-none): location to store the probability of the found
473 * Tries to find what type of data is contained in the given @data, the
474 * assumption being that the data represents the beginning of the stream or
477 * All available typefinders will be called on the data in order of rank. If
478 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
479 * typefinding is stopped immediately and the found caps will be returned
480 * right away. Otherwise, all available typefind functions will the tried,
481 * and the caps with the highest probability will be returned, or #NULL if
482 * the content of @data could not be identified.
484 * Free-function: gst_caps_unref
486 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
487 * if no type could be found. The caller should free the caps returned
488 * with gst_caps_unref().
491 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
492 GstTypeFindProbability * prob)
494 GstTypeFindBufHelper helper;
496 GList *l, *type_list;
497 GstCaps *result = NULL;
499 g_return_val_if_fail (data != NULL, NULL);
503 helper.best_probability = GST_TYPE_FIND_NONE;
507 if (helper.data == NULL || helper.size == 0)
511 find.peek = buf_helper_find_peek;
512 find.suggest = buf_helper_find_suggest;
513 find.get_length = NULL;
515 type_list = gst_type_find_factory_get_list ();
517 for (l = type_list; l; l = l->next) {
518 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
519 gst_type_find_factory_call_function (helper.factory, &find);
520 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
523 gst_plugin_feature_list_free (type_list);
525 if (helper.best_probability > 0)
526 result = helper.caps;
529 *prob = helper.best_probability;
531 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
532 result, (guint) helper.best_probability);
538 * gst_type_find_helper_for_buffer:
539 * @obj: object doing the typefinding, or NULL (used for logging)
540 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
541 * @prob: (out) (allow-none): location to store the probability of the found
544 * Tries to find what type of data is contained in the given #GstBuffer, the
545 * assumption being that the buffer represents the beginning of the stream or
548 * All available typefinders will be called on the data in order of rank. If
549 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
550 * typefinding is stopped immediately and the found caps will be returned
551 * right away. Otherwise, all available typefind functions will the tried,
552 * and the caps with the highest probability will be returned, or #NULL if
553 * the content of the buffer could not be identified.
555 * Free-function: gst_caps_unref
557 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
558 * if no type could be found. The caller should free the caps returned
559 * with gst_caps_unref().
562 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
563 GstTypeFindProbability * prob)
568 g_return_val_if_fail (buf != NULL, NULL);
569 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
570 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
571 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
573 g_assert (gst_buffer_map (buf, &info, GST_MAP_READ));
574 result = gst_type_find_helper_for_data (obj, info.data, info.size, prob);
575 gst_buffer_unmap (buf, &info);
581 * gst_type_find_helper_for_extension:
582 * @obj: (allow-none): object doing the typefinding, or NULL (used for logging)
583 * @extension: an extension
585 * Tries to find the best #GstCaps associated with @extension.
587 * All available typefinders will be checked against the extension in order
588 * of rank. The caps of the first typefinder that can handle @extension will be
591 * Free-function: gst_caps_unref
593 * Returns: (transfer full): the #GstCaps corresponding to @extension, or
594 * #NULL if no type could be found. The caller should free the caps
595 * returned with gst_caps_unref().
600 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
602 GList *l, *type_list;
603 GstCaps *result = NULL;
605 g_return_val_if_fail (extension != NULL, NULL);
607 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
609 type_list = gst_type_find_factory_get_list ();
611 for (l = type_list; l; l = g_list_next (l)) {
612 GstTypeFindFactory *factory;
616 factory = GST_TYPE_FIND_FACTORY (l->data);
618 /* we only want to check those factories without a function */
619 if (factory->function != NULL)
622 /* get the extension that this typefind factory can handle */
623 ext = gst_type_find_factory_get_extensions (factory);
627 /* there are extension, see if one of them matches the requested
629 for (i = 0; ext[i]; i++) {
630 if (strcmp (ext[i], extension) == 0) {
631 /* we found a matching extension, take the caps */
632 if ((result = gst_type_find_factory_get_caps (factory))) {
633 gst_caps_ref (result);
640 gst_plugin_feature_list_free (type_list);
642 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);