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, GstCaps * caps);
56 GSList *buffers; /* buffer cache */
59 GstTypeFindHelperGetRangeFunction func;
60 GstTypeFindProbability best_probability;
62 GstTypeFindFactory *factory; /* for logging */
63 GstObject *obj; /* for logging */
69 * @data: helper data struct
73 * Get data pointer within a stream. Keeps a cache of read buffers (partly
74 * for performance reasons, but mostly because pointers returned by us need
75 * to stay valid until typefinding has finished)
77 * Returns: address of the data or %NULL if buffer does not cover the
81 helper_find_peek (gpointer data, gint64 offset, guint size)
83 GstTypeFindHelper *helper;
86 GSList *insert_pos = NULL;
89 GstMappedBuffer *bmap;
94 helper = (GstTypeFindHelper *) data;
96 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT
97 ", %u)", GST_OBJECT_NAME (helper->factory), offset, size);
103 if (helper->size == -1 || helper->size < -offset)
106 offset += helper->size;
109 /* see if we have a matching buffer already in our list */
110 if (size > 0 && offset <= helper->last_offset) {
113 for (walk = helper->buffers; walk; walk = walk->next) {
114 GstMappedBuffer *bmp = (GstMappedBuffer *) walk->data;
115 GstBuffer *buf = GST_BUFFER_CAST (bmp->buffer);
116 guint64 buf_offset = GST_BUFFER_OFFSET (buf);
117 guint buf_size = gst_buffer_get_size (buf);
119 /* buffers are kept sorted by end offset (highest first) in the list, so
120 * at this point we save the current position and stop searching if
121 * we're after the searched end offset */
122 if (buf_offset <= offset) {
123 if ((offset + size) < (buf_offset + buf_size)) {
124 /* must already have been mapped before */
125 return (guint8 *) bmp->map.data + (offset - buf_offset);
127 } else if (offset + size >= buf_offset + buf_size) {
135 /* some typefinders go in 1 byte steps over 1k of data and request
136 * small buffers. It is really inefficient to pull each time, and pulling
137 * a larger chunk is almost free. Trying to pull a larger chunk at the end
138 * of the file is also not a problem here, we'll just get a truncated buffer
139 * in that case (and we'll have to double-check the size we actually get
140 * anyway, see below) */
142 helper->func (helper->obj, helper->parent, offset, MAX (size, 4096),
145 if (ret != GST_FLOW_OK)
149 caps = GST_BUFFER_CAPS (buffer);
151 if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) {
152 GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability",
155 gst_caps_replace (&helper->caps, caps);
156 helper->best_probability = GST_TYPE_FIND_MAXIMUM;
158 gst_buffer_unref (buffer);
163 /* getrange might silently return shortened buffers at the end of a file,
164 * we must, however, always return either the full requested data or NULL */
165 buf_offset = GST_BUFFER_OFFSET (buffer);
166 buf_size = gst_buffer_get_size (buffer);
168 if ((buf_offset != -1 && buf_offset != offset) || buf_size < size) {
169 GST_DEBUG ("dropping short buffer: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
170 " instead of %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
171 buf_offset, buf_offset + buf_size - 1, offset, offset + size - 1);
172 gst_buffer_unref (buffer);
176 bmap = g_slice_new0 (GstMappedBuffer);
177 bmap->buffer = buffer;
179 helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, bmap);
181 /* if insert_pos is not set, our offset is bigger than the largest offset
182 * we have so far; since we keep the list sorted with highest offsets
183 * first, we need to prepend the buffer to the list */
184 helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
185 helper->buffers = g_slist_prepend (helper->buffers, bmap);
188 gst_buffer_map (buffer, &bmap->map, GST_MAP_READ);
190 return bmap->map.data;
194 GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret));
200 * helper_find_suggest:
201 * @data: helper data struct
202 * @probability: probability of the match
203 * @caps: caps of the type
205 * If given @probability is higher, replace previously store caps.
208 helper_find_suggest (gpointer data, GstTypeFindProbability probability,
211 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
213 GST_LOG_OBJECT (helper->obj,
214 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
215 GST_OBJECT_NAME (helper->factory), probability, caps);
217 if (probability > helper->best_probability) {
218 gst_caps_replace (&helper->caps, caps);
219 helper->best_probability = probability;
224 helper_find_get_length (gpointer data)
226 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
228 GST_LOG_OBJECT (helper->obj, "'%s' called get_length, returning %"
229 G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
235 * gst_type_find_helper_get_range:
236 * @obj: A #GstObject that will be passed as first argument to @func
237 * @parent: the parent of @obj or NULL
238 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
239 * be used to access data at random offsets when doing the typefinding
240 * @size: The length in bytes
241 * @extension: extension of the media
242 * @prob: (out) (allow-none): location to store the probability of the found
245 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
246 * however, this function will use the specified function @func to obtain the
247 * data needed by the typefind functions, rather than operating on a given
248 * source pad. This is useful mostly for elements like tag demuxers which
249 * strip off data at the beginning and/or end of a file and want to typefind
250 * the stripped data stream before adding their own source pad (the specified
251 * callback can then call the upstream peer pad with offsets adjusted for the
252 * tag size, for example).
254 * When @extension is not NULL, this function will first try the typefind
255 * functions for the given extension, which might speed up the typefinding
258 * Free-function: gst_caps_unref
260 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
261 * Returns #NULL if no #GstCaps matches the data stream.
266 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
267 GstTypeFindHelperGetRangeFunction func, guint64 size,
268 const gchar * extension, GstTypeFindProbability * prob)
270 GstTypeFindHelper helper;
273 GList *l, *type_list;
274 GstCaps *result = NULL;
277 g_return_val_if_fail (GST_IS_OBJECT (obj), NULL);
278 g_return_val_if_fail (func != NULL, NULL);
280 helper.buffers = NULL;
282 helper.last_offset = 0;
284 helper.best_probability = GST_TYPE_FIND_NONE;
287 helper.parent = parent;
290 find.peek = helper_find_peek;
291 find.suggest = helper_find_suggest;
293 if (size == 0 || size == (guint64) - 1) {
294 find.get_length = NULL;
296 find.get_length = helper_find_get_length;
299 type_list = gst_type_find_factory_get_list ();
301 /* move the typefinders for the extension first in the list. The idea is that
302 * when one of them returns MAX we don't need to search further as there is a
303 * very high chance we got the right type. */
307 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head",
310 for (l = type_list; l; l = next) {
311 GstTypeFindFactory *factory;
317 factory = GST_TYPE_FIND_FACTORY (l->data);
319 ext = gst_type_find_factory_get_extensions (factory);
323 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
324 GST_OBJECT_NAME (factory), extension);
326 for (i = 0; ext[i]; i++) {
327 if (strcmp (ext[i], extension) == 0) {
328 /* found extension, move in front */
329 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
331 /* remove entry from list */
332 type_list = g_list_delete_link (type_list, l);
333 /* insert at the position */
334 type_list = g_list_insert (type_list, factory, pos);
335 /* next element will be inserted after this one */
343 for (l = type_list; l; l = l->next) {
344 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
345 gst_type_find_factory_call_function (helper.factory, &find);
346 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
349 gst_plugin_feature_list_free (type_list);
351 for (walk = helper.buffers; walk; walk = walk->next) {
352 GstMappedBuffer *bmap = (GstMappedBuffer *) walk->data;
354 gst_buffer_unmap (bmap->buffer, &bmap->map);
355 gst_buffer_unref (bmap->buffer);
356 g_slice_free (GstMappedBuffer, bmap);
358 g_slist_free (helper.buffers);
360 if (helper.best_probability > 0)
361 result = helper.caps;
364 *prob = helper.best_probability;
366 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
367 result, (guint) helper.best_probability);
373 * gst_type_find_helper:
374 * @src: A source #GstPad
375 * @size: The length in bytes
377 * Tries to find what type of data is flowing from the given source #GstPad.
379 * Free-function: gst_caps_unref
381 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
382 * Returns #NULL if no #GstCaps matches the data stream.
386 gst_type_find_helper (GstPad * src, guint64 size)
388 GstTypeFindHelperGetRangeFunction func;
390 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
391 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
393 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
395 return gst_type_find_helper_get_range (GST_OBJECT (src),
396 GST_OBJECT_PARENT (src), func, size, NULL, NULL);
399 /* ********************** typefinding for buffers ************************* */
403 const guint8 *data; /* buffer data */
405 GstTypeFindProbability best_probability;
407 GstTypeFindFactory *factory; /* for logging */
408 GstObject *obj; /* for logging */
409 } GstTypeFindBufHelper;
412 * buf_helper_find_peek:
413 * @data: helper data struct
414 * @off: stream offset
417 * Get data pointer within a buffer.
419 * Returns: address inside the buffer or %NULL if buffer does not cover the
422 static const guint8 *
423 buf_helper_find_peek (gpointer data, gint64 off, guint size)
425 GstTypeFindBufHelper *helper;
427 helper = (GstTypeFindBufHelper *) data;
428 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
429 GST_OBJECT_NAME (helper->factory), off, size);
435 GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
436 GST_OBJECT_NAME (helper->factory));
440 if ((off + size) <= helper->size)
441 return helper->data + off;
447 * buf_helper_find_suggest:
448 * @data: helper data struct
449 * @probability: probability of the match
450 * @caps: caps of the type
452 * If given @probability is higher, replace previously store caps.
455 buf_helper_find_suggest (gpointer data, GstTypeFindProbability probability,
458 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
460 GST_LOG_OBJECT (helper->obj,
461 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
462 GST_OBJECT_NAME (helper->factory), probability, caps);
464 /* Note: not >= as we call typefinders in order of rank, highest first */
465 if (probability > helper->best_probability) {
466 gst_caps_replace (&helper->caps, caps);
467 helper->best_probability = probability;
472 * gst_type_find_helper_for_data:
473 * @obj: object doing the typefinding, or NULL (used for logging)
474 * @data: (in) (transfer none): a pointer with data to typefind
475 * @size: (in) (transfer none): the size of @data
476 * @prob: (out) (allow-none): location to store the probability of the found
479 * Tries to find what type of data is contained in the given @data, the
480 * assumption being that the data represents the beginning of the stream or
483 * All available typefinders will be called on the data in order of rank. If
484 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
485 * typefinding is stopped immediately and the found caps will be returned
486 * right away. Otherwise, all available typefind functions will the tried,
487 * and the caps with the highest probability will be returned, or #NULL if
488 * the content of @data could not be identified.
490 * Free-function: gst_caps_unref
492 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
493 * if no type could be found. The caller should free the caps returned
494 * with gst_caps_unref().
497 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
498 GstTypeFindProbability * prob)
500 GstTypeFindBufHelper helper;
502 GList *l, *type_list;
503 GstCaps *result = NULL;
505 g_return_val_if_fail (data != NULL, NULL);
509 helper.best_probability = GST_TYPE_FIND_NONE;
513 if (helper.data == NULL || helper.size == 0)
517 find.peek = buf_helper_find_peek;
518 find.suggest = buf_helper_find_suggest;
519 find.get_length = NULL;
521 type_list = gst_type_find_factory_get_list ();
523 for (l = type_list; l; l = l->next) {
524 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
525 gst_type_find_factory_call_function (helper.factory, &find);
526 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
529 gst_plugin_feature_list_free (type_list);
531 if (helper.best_probability > 0)
532 result = helper.caps;
535 *prob = helper.best_probability;
537 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
538 result, (guint) helper.best_probability);
544 * gst_type_find_helper_for_buffer:
545 * @obj: object doing the typefinding, or NULL (used for logging)
546 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
547 * @prob: (out) (allow-none): location to store the probability of the found
550 * Tries to find what type of data is contained in the given #GstBuffer, the
551 * assumption being that the buffer represents the beginning of the stream or
554 * All available typefinders will be called on the data in order of rank. If
555 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
556 * typefinding is stopped immediately and the found caps will be returned
557 * right away. Otherwise, all available typefind functions will the tried,
558 * and the caps with the highest probability will be returned, or #NULL if
559 * the content of the buffer could not be identified.
561 * Free-function: gst_caps_unref
563 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
564 * if no type could be found. The caller should free the caps returned
565 * with gst_caps_unref().
568 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
569 GstTypeFindProbability * prob)
574 g_return_val_if_fail (buf != NULL, NULL);
575 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
576 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
577 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
579 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
581 result = gst_type_find_helper_for_data (obj, info.data, info.size, prob);
582 gst_buffer_unmap (buf, &info);
588 * gst_type_find_helper_for_extension:
589 * @obj: (allow-none): object doing the typefinding, or NULL (used for logging)
590 * @extension: an extension
592 * Tries to find the best #GstCaps associated with @extension.
594 * All available typefinders will be checked against the extension in order
595 * of rank. The caps of the first typefinder that can handle @extension will be
598 * Free-function: gst_caps_unref
600 * Returns: (transfer full): the #GstCaps corresponding to @extension, or
601 * #NULL if no type could be found. The caller should free the caps
602 * returned with gst_caps_unref().
607 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
609 GList *l, *type_list;
610 GstCaps *result = NULL;
612 g_return_val_if_fail (extension != NULL, NULL);
614 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
616 type_list = gst_type_find_factory_get_list ();
618 for (l = type_list; l; l = g_list_next (l)) {
619 GstTypeFindFactory *factory;
623 factory = GST_TYPE_FIND_FACTORY (l->data);
625 /* we only want to check those factories without a function */
626 if (gst_type_find_factory_has_function (factory))
629 /* get the extension that this typefind factory can handle */
630 ext = gst_type_find_factory_get_extensions (factory);
634 /* there are extension, see if one of them matches the requested
636 for (i = 0; ext[i]; i++) {
637 if (strcmp (ext[i], extension) == 0) {
638 /* we found a matching extension, take the caps */
639 if ((result = gst_type_find_factory_get_caps (factory))) {
640 gst_caps_ref (result);
647 gst_plugin_feature_list_free (type_list);
649 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);