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 guint best_probability;
56 GstTypeFindFactory *factory; /* for logging */
57 GstObject *obj; /* for logging */
62 * @data: helper data struct
66 * Get data pointer within a stream. Keeps a cache of read buffers (partly
67 * for performance reasons, but mostly because pointers returned by us need
68 * to stay valid until typefinding has finished)
70 * Returns: address of the data or %NULL if buffer does not cover the
74 helper_find_peek (gpointer data, gint64 offset, guint size)
76 GstTypeFindHelper *helper;
79 GSList *insert_pos = NULL;
86 helper = (GstTypeFindHelper *) data;
88 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT
89 ", %u)", GST_OBJECT_NAME (helper->factory), offset, size);
95 if (helper->size == -1 || helper->size < -offset)
98 offset += helper->size;
101 /* see if we have a matching buffer already in our list */
102 if (size > 0 && offset <= helper->last_offset) {
105 for (walk = helper->buffers; walk; walk = walk->next) {
106 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
107 guint64 buf_offset = GST_BUFFER_OFFSET (buf);
108 guint buf_size = gst_buffer_get_size (buf);
110 /* buffers are kept sorted by end offset (highest first) in the list, so
111 * at this point we save the current position and stop searching if
112 * we're after the searched end offset */
113 if (buf_offset <= offset) {
114 if ((offset + size) < (buf_offset + buf_size)) {
117 /* FIXME, unmap after usage */
118 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_READ);
120 return 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) */
136 ret = helper->func (helper->obj, offset, MAX (size, 4096), &buffer);
138 if (ret != GST_FLOW_OK)
142 caps = GST_BUFFER_CAPS (buffer);
144 if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) {
145 GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability",
148 gst_caps_replace (&helper->caps, caps);
149 helper->best_probability = GST_TYPE_FIND_MAXIMUM;
151 gst_buffer_unref (buffer);
156 /* getrange might silently return shortened buffers at the end of a file,
157 * we must, however, always return either the full requested data or NULL */
158 buf_offset = GST_BUFFER_OFFSET (buffer);
159 buf_size = gst_buffer_get_size (buffer);
161 if ((buf_offset != -1 && buf_offset != offset) || buf_size < size) {
162 GST_DEBUG ("droping short buffer: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
163 " instead of %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
164 buf_offset, buf_offset + buf_size - 1, offset, offset + size - 1);
165 gst_buffer_unref (buffer);
171 g_slist_insert_before (helper->buffers, insert_pos, buffer);
173 /* if insert_pos is not set, our offset is bigger than the largest offset
174 * we have so far; since we keep the list sorted with highest offsets
175 * first, we need to prepend the buffer to the list */
176 helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
177 helper->buffers = g_slist_prepend (helper->buffers, buffer);
181 return gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
185 GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret));
191 * helper_find_suggest:
192 * @data: helper data struct
193 * @probability: probability of the match
194 * @caps: caps of the type
196 * If given @probability is higher, replace previously store caps.
199 helper_find_suggest (gpointer data, guint probability, const GstCaps * caps)
201 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
203 GST_LOG_OBJECT (helper->obj,
204 "'%s' called called suggest (%u, %" GST_PTR_FORMAT ")",
205 GST_OBJECT_NAME (helper->factory), probability, caps);
207 if (probability > helper->best_probability) {
208 GstCaps *copy = gst_caps_copy (caps);
210 gst_caps_replace (&helper->caps, copy);
211 gst_caps_unref (copy);
212 helper->best_probability = probability;
217 helper_find_get_length (gpointer data)
219 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
221 GST_LOG_OBJECT (helper->obj, "'%s' called called get_length, returning %"
222 G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
228 * gst_type_find_helper_get_range_ext:
229 * @obj: A #GstObject that will be passed as first argument to @func
230 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
231 * be used to access data at random offsets when doing the typefinding
232 * @size: The length in bytes
233 * @extension: extension of the media
234 * @prob: (out) (allow-none): location to store the probability of the found
237 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
238 * however, this function will use the specified function @func to obtain the
239 * data needed by the typefind functions, rather than operating on a given
240 * source pad. This is useful mostly for elements like tag demuxers which
241 * strip off data at the beginning and/or end of a file and want to typefind
242 * the stripped data stream before adding their own source pad (the specified
243 * callback can then call the upstream peer pad with offsets adjusted for the
244 * tag size, for example).
246 * When @extension is not NULL, this function will first try the typefind
247 * functions for the given extension, which might speed up the typefinding
250 * Free-function: gst_caps_unref
252 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
253 * Returns #NULL if no #GstCaps matches the data stream.
258 gst_type_find_helper_get_range_ext (GstObject * obj,
259 GstTypeFindHelperGetRangeFunction func, guint64 size,
260 const gchar * extension, GstTypeFindProbability * prob)
262 GstTypeFindHelper helper;
265 GList *l, *type_list;
266 GstCaps *result = NULL;
269 g_return_val_if_fail (GST_IS_OBJECT (obj), NULL);
270 g_return_val_if_fail (func != NULL, NULL);
272 helper.buffers = NULL;
274 helper.last_offset = 0;
276 helper.best_probability = 0;
281 find.peek = helper_find_peek;
282 find.suggest = helper_find_suggest;
284 if (size == 0 || size == (guint64) - 1) {
285 find.get_length = NULL;
287 find.get_length = helper_find_get_length;
290 type_list = gst_type_find_factory_get_list ();
292 /* move the typefinders for the extension first in the list. The idea is that
293 * when one of them returns MAX we don't need to search further as there is a
294 * very high chance we got the right type. */
298 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head",
301 for (l = type_list; l; l = next) {
302 GstTypeFindFactory *factory;
308 factory = GST_TYPE_FIND_FACTORY (l->data);
310 ext = gst_type_find_factory_get_extensions (factory);
314 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
315 GST_OBJECT_NAME (factory), extension);
317 for (i = 0; ext[i]; i++) {
318 if (strcmp (ext[i], extension) == 0) {
319 /* found extension, move in front */
320 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
322 /* remove entry from list */
323 type_list = g_list_delete_link (type_list, l);
324 /* insert at the position */
325 type_list = g_list_insert (type_list, factory, pos);
326 /* next element will be inserted after this one */
334 for (l = type_list; l; l = l->next) {
335 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
336 gst_type_find_factory_call_function (helper.factory, &find);
337 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
340 gst_plugin_feature_list_free (type_list);
342 for (walk = helper.buffers; walk; walk = walk->next)
343 gst_buffer_unref (GST_BUFFER_CAST (walk->data));
344 g_slist_free (helper.buffers);
346 if (helper.best_probability > 0)
347 result = helper.caps;
350 *prob = helper.best_probability;
352 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
353 result, (guint) helper.best_probability);
359 * gst_type_find_helper_get_range:
360 * @obj: A #GstObject that will be passed as first argument to @func
361 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
362 * be used to access data at random offsets when doing the typefinding
363 * @size: The length in bytes
364 * @prob: (out) (allow-none): location to store the probability of the found
367 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
368 * however, this function will use the specified function @func to obtain the
369 * data needed by the typefind functions, rather than operating on a given
370 * source pad. This is useful mostly for elements like tag demuxers which
371 * strip off data at the beginning and/or end of a file and want to typefind
372 * the stripped data stream before adding their own source pad (the specified
373 * callback can then call the upstream peer pad with offsets adjusted for the
374 * tag size, for example).
376 * Free-function: gst_caps_unref
378 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
379 * Returns #NULL if no #GstCaps matches the data stream.
382 gst_type_find_helper_get_range (GstObject * obj,
383 GstTypeFindHelperGetRangeFunction func, guint64 size,
384 GstTypeFindProbability * prob)
386 return gst_type_find_helper_get_range_ext (obj, func, size, NULL, prob);
390 * gst_type_find_helper:
391 * @src: A source #GstPad
392 * @size: The length in bytes
394 * Tries to find what type of data is flowing from the given source #GstPad.
396 * Free-function: gst_caps_unref
398 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
399 * Returns #NULL if no #GstCaps matches the data stream.
403 gst_type_find_helper (GstPad * src, guint64 size)
405 GstTypeFindHelperGetRangeFunction func;
407 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
408 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
410 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
412 return gst_type_find_helper_get_range (GST_OBJECT (src), func, size, NULL);
415 /* ********************** typefinding for buffers ************************* */
419 const guint8 *data; /* buffer data */
421 guint best_probability;
423 GstTypeFindFactory *factory; /* for logging */
424 GstObject *obj; /* for logging */
425 } GstTypeFindBufHelper;
428 * buf_helper_find_peek:
429 * @data: helper data struct
430 * @off: stream offset
433 * Get data pointer within a buffer.
435 * Returns: address inside the buffer or %NULL if buffer does not cover the
438 static const guint8 *
439 buf_helper_find_peek (gpointer data, gint64 off, guint size)
441 GstTypeFindBufHelper *helper;
443 helper = (GstTypeFindBufHelper *) data;
444 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
445 GST_OBJECT_NAME (helper->factory), off, size);
451 GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
452 GST_OBJECT_NAME (helper->factory));
456 if ((off + size) <= helper->size)
457 return helper->data + off;
463 * buf_helper_find_suggest:
464 * @data: helper data struct
465 * @probability: probability of the match
466 * @caps: caps of the type
468 * If given @probability is higher, replace previously store caps.
471 buf_helper_find_suggest (gpointer data, guint probability, const GstCaps * caps)
473 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
475 GST_LOG_OBJECT (helper->obj,
476 "'%s' called called suggest (%u, %" GST_PTR_FORMAT ")",
477 GST_OBJECT_NAME (helper->factory), probability, caps);
479 /* Note: not >= as we call typefinders in order of rank, highest first */
480 if (probability > helper->best_probability) {
481 GstCaps *copy = gst_caps_copy (caps);
483 gst_caps_replace (&helper->caps, copy);
484 gst_caps_unref (copy);
485 helper->best_probability = probability;
490 * gst_type_find_helper_for_data:
491 * @obj: object doing the typefinding, or NULL (used for logging)
492 * @data: (in) (transfer none): a pointer with data to typefind
493 * @size: (in) (transfer none): the size of @data
494 * @prob: (out) (allow-none): location to store the probability of the found
497 * Tries to find what type of data is contained in the given @data, the
498 * assumption being that the data represents the beginning of the stream or
501 * All available typefinders will be called on the data in order of rank. If
502 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
503 * typefinding is stopped immediately and the found caps will be returned
504 * right away. Otherwise, all available typefind functions will the tried,
505 * and the caps with the highest probability will be returned, or #NULL if
506 * the content of @data could not be identified.
508 * Free-function: gst_caps_unref
510 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
511 * if no type could be found. The caller should free the caps returned
512 * with gst_caps_unref().
515 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
516 GstTypeFindProbability * prob)
518 GstTypeFindBufHelper helper;
520 GList *l, *type_list;
521 GstCaps *result = NULL;
523 g_return_val_if_fail (data != NULL, NULL);
527 helper.best_probability = 0;
531 if (helper.data == NULL || helper.size == 0)
535 find.peek = buf_helper_find_peek;
536 find.suggest = buf_helper_find_suggest;
537 find.get_length = NULL;
539 type_list = gst_type_find_factory_get_list ();
541 for (l = type_list; l; l = l->next) {
542 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
543 gst_type_find_factory_call_function (helper.factory, &find);
544 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
547 gst_plugin_feature_list_free (type_list);
549 if (helper.best_probability > 0)
550 result = helper.caps;
553 *prob = helper.best_probability;
555 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
556 result, (guint) helper.best_probability);
562 * gst_type_find_helper_for_buffer:
563 * @obj: object doing the typefinding, or NULL (used for logging)
564 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
565 * @prob: (out) (allow-none): location to store the probability of the found
568 * Tries to find what type of data is contained in the given #GstBuffer, the
569 * assumption being that the buffer represents the beginning of the stream or
572 * All available typefinders will be called on the data in order of rank. If
573 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
574 * typefinding is stopped immediately and the found caps will be returned
575 * right away. Otherwise, all available typefind functions will the tried,
576 * and the caps with the highest probability will be returned, or #NULL if
577 * the content of the buffer could not be identified.
579 * Free-function: gst_caps_unref
581 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
582 * if no type could be found. The caller should free the caps returned
583 * with gst_caps_unref().
586 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
587 GstTypeFindProbability * prob)
593 g_return_val_if_fail (buf != NULL, NULL);
594 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
595 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
596 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
598 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
599 result = gst_type_find_helper_for_data (obj, data, size, prob);
600 gst_buffer_unmap (buf, data, size);
606 * gst_type_find_helper_for_extension:
607 * @obj: (allow-none): object doing the typefinding, or NULL (used for logging)
608 * @extension: an extension
610 * Tries to find the best #GstCaps associated with @extension.
612 * All available typefinders will be checked against the extension in order
613 * of rank. The caps of the first typefinder that can handle @extension will be
616 * Free-function: gst_caps_unref
618 * Returns: (transfer full): the #GstCaps corresponding to @extension, or
619 * #NULL if no type could be found. The caller should free the caps
620 * returned with gst_caps_unref().
625 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
627 GList *l, *type_list;
628 GstCaps *result = NULL;
630 g_return_val_if_fail (extension != NULL, NULL);
632 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
634 type_list = gst_type_find_factory_get_list ();
636 for (l = type_list; l; l = g_list_next (l)) {
637 GstTypeFindFactory *factory;
641 factory = GST_TYPE_FIND_FACTORY (l->data);
643 /* we only want to check those factories without a function */
644 if (factory->function != NULL)
647 /* get the extension that this typefind factory can handle */
648 ext = gst_type_find_factory_get_extensions (factory);
652 /* there are extension, see if one of them matches the requested
654 for (i = 0; ext[i]; i++) {
655 if (strcmp (ext[i], extension) == 0) {
656 /* we found a matching extension, take the caps */
657 if ((result = gst_type_find_factory_get_caps (factory))) {
658 gst_caps_ref (result);
665 gst_plugin_feature_list_free (type_list);
667 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);