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);
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 if (!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 gst_buffer_map (buffer, &info, GST_MAP_READ);
189 GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret));
195 * helper_find_suggest:
196 * @data: helper data struct
197 * @probability: probability of the match
198 * @caps: caps of the type
200 * If given @probability is higher, replace previously store caps.
203 helper_find_suggest (gpointer data, GstTypeFindProbability probability,
206 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
208 GST_LOG_OBJECT (helper->obj,
209 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
210 GST_OBJECT_NAME (helper->factory), probability, caps);
212 if (probability > helper->best_probability) {
213 gst_caps_replace (&helper->caps, caps);
214 helper->best_probability = probability;
219 helper_find_get_length (gpointer data)
221 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
223 GST_LOG_OBJECT (helper->obj, "'%s' called get_length, returning %"
224 G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
230 * gst_type_find_helper_get_range:
231 * @obj: A #GstObject that will be passed as first argument to @func
232 * @parent: the parent of @obj or NULL
233 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
234 * be used to access data at random offsets when doing the typefinding
235 * @size: The length in bytes
236 * @extension: extension of the media
237 * @prob: (out) (allow-none): location to store the probability of the found
240 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
241 * however, this function will use the specified function @func to obtain the
242 * data needed by the typefind functions, rather than operating on a given
243 * source pad. This is useful mostly for elements like tag demuxers which
244 * strip off data at the beginning and/or end of a file and want to typefind
245 * the stripped data stream before adding their own source pad (the specified
246 * callback can then call the upstream peer pad with offsets adjusted for the
247 * tag size, for example).
249 * When @extension is not NULL, this function will first try the typefind
250 * functions for the given extension, which might speed up the typefinding
253 * Free-function: gst_caps_unref
255 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
256 * Returns #NULL if no #GstCaps matches the data stream.
261 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
262 GstTypeFindHelperGetRangeFunction func, guint64 size,
263 const gchar * extension, GstTypeFindProbability * prob)
265 GstTypeFindHelper helper;
268 GList *l, *type_list;
269 GstCaps *result = NULL;
272 g_return_val_if_fail (GST_IS_OBJECT (obj), NULL);
273 g_return_val_if_fail (func != NULL, NULL);
275 helper.buffers = NULL;
277 helper.last_offset = 0;
279 helper.best_probability = GST_TYPE_FIND_NONE;
282 helper.parent = parent;
285 find.peek = helper_find_peek;
286 find.suggest = helper_find_suggest;
288 if (size == 0 || size == (guint64) - 1) {
289 find.get_length = NULL;
291 find.get_length = helper_find_get_length;
294 type_list = gst_type_find_factory_get_list ();
296 /* move the typefinders for the extension first in the list. The idea is that
297 * when one of them returns MAX we don't need to search further as there is a
298 * very high chance we got the right type. */
302 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head",
305 for (l = type_list; l; l = next) {
306 GstTypeFindFactory *factory;
312 factory = GST_TYPE_FIND_FACTORY (l->data);
314 ext = gst_type_find_factory_get_extensions (factory);
318 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
319 GST_OBJECT_NAME (factory), extension);
321 for (i = 0; ext[i]; i++) {
322 if (strcmp (ext[i], extension) == 0) {
323 /* found extension, move in front */
324 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
326 /* remove entry from list */
327 type_list = g_list_delete_link (type_list, l);
328 /* insert at the position */
329 type_list = g_list_insert (type_list, factory, pos);
330 /* next element will be inserted after this one */
338 for (l = type_list; l; l = l->next) {
339 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
340 gst_type_find_factory_call_function (helper.factory, &find);
341 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
344 gst_plugin_feature_list_free (type_list);
346 for (walk = helper.buffers; walk; walk = walk->next)
347 gst_buffer_unref (GST_BUFFER_CAST (walk->data));
348 g_slist_free (helper.buffers);
350 if (helper.best_probability > 0)
351 result = helper.caps;
354 *prob = helper.best_probability;
356 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
357 result, (guint) helper.best_probability);
363 * gst_type_find_helper:
364 * @src: A source #GstPad
365 * @size: The length in bytes
367 * Tries to find what type of data is flowing from the given source #GstPad.
369 * Free-function: gst_caps_unref
371 * Returns: (transfer full): the #GstCaps corresponding to the data stream.
372 * Returns #NULL if no #GstCaps matches the data stream.
376 gst_type_find_helper (GstPad * src, guint64 size)
378 GstTypeFindHelperGetRangeFunction func;
380 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
381 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
383 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
385 return gst_type_find_helper_get_range (GST_OBJECT (src),
386 GST_OBJECT_PARENT (src), func, size, NULL, NULL);
389 /* ********************** typefinding for buffers ************************* */
393 const guint8 *data; /* buffer data */
395 GstTypeFindProbability best_probability;
397 GstTypeFindFactory *factory; /* for logging */
398 GstObject *obj; /* for logging */
399 } GstTypeFindBufHelper;
402 * buf_helper_find_peek:
403 * @data: helper data struct
404 * @off: stream offset
407 * Get data pointer within a buffer.
409 * Returns: address inside the buffer or %NULL if buffer does not cover the
412 static const guint8 *
413 buf_helper_find_peek (gpointer data, gint64 off, guint size)
415 GstTypeFindBufHelper *helper;
417 helper = (GstTypeFindBufHelper *) data;
418 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
419 GST_OBJECT_NAME (helper->factory), off, size);
425 GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
426 GST_OBJECT_NAME (helper->factory));
430 if ((off + size) <= helper->size)
431 return helper->data + off;
437 * buf_helper_find_suggest:
438 * @data: helper data struct
439 * @probability: probability of the match
440 * @caps: caps of the type
442 * If given @probability is higher, replace previously store caps.
445 buf_helper_find_suggest (gpointer data, GstTypeFindProbability probability,
448 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
450 GST_LOG_OBJECT (helper->obj,
451 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
452 GST_OBJECT_NAME (helper->factory), probability, caps);
454 /* Note: not >= as we call typefinders in order of rank, highest first */
455 if (probability > helper->best_probability) {
456 gst_caps_replace (&helper->caps, caps);
457 helper->best_probability = probability;
462 * gst_type_find_helper_for_data:
463 * @obj: object doing the typefinding, or NULL (used for logging)
464 * @data: (in) (transfer none): a pointer with data to typefind
465 * @size: (in) (transfer none): the size of @data
466 * @prob: (out) (allow-none): location to store the probability of the found
469 * Tries to find what type of data is contained in the given @data, the
470 * assumption being that the data represents the beginning of the stream or
473 * All available typefinders will be called on the data in order of rank. If
474 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
475 * typefinding is stopped immediately and the found caps will be returned
476 * right away. Otherwise, all available typefind functions will the tried,
477 * and the caps with the highest probability will be returned, or #NULL if
478 * the content of @data could not be identified.
480 * Free-function: gst_caps_unref
482 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
483 * if no type could be found. The caller should free the caps returned
484 * with gst_caps_unref().
487 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
488 GstTypeFindProbability * prob)
490 GstTypeFindBufHelper helper;
492 GList *l, *type_list;
493 GstCaps *result = NULL;
495 g_return_val_if_fail (data != NULL, NULL);
499 helper.best_probability = GST_TYPE_FIND_NONE;
503 if (helper.data == NULL || helper.size == 0)
507 find.peek = buf_helper_find_peek;
508 find.suggest = buf_helper_find_suggest;
509 find.get_length = NULL;
511 type_list = gst_type_find_factory_get_list ();
513 for (l = type_list; l; l = l->next) {
514 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
515 gst_type_find_factory_call_function (helper.factory, &find);
516 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
519 gst_plugin_feature_list_free (type_list);
521 if (helper.best_probability > 0)
522 result = helper.caps;
525 *prob = helper.best_probability;
527 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
528 result, (guint) helper.best_probability);
534 * gst_type_find_helper_for_buffer:
535 * @obj: object doing the typefinding, or NULL (used for logging)
536 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
537 * @prob: (out) (allow-none): location to store the probability of the found
540 * Tries to find what type of data is contained in the given #GstBuffer, the
541 * assumption being that the buffer represents the beginning of the stream or
544 * All available typefinders will be called on the data in order of rank. If
545 * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
546 * typefinding is stopped immediately and the found caps will be returned
547 * right away. Otherwise, all available typefind functions will the tried,
548 * and the caps with the highest probability will be returned, or #NULL if
549 * the content of the buffer could not be identified.
551 * Free-function: gst_caps_unref
553 * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
554 * if no type could be found. The caller should free the caps returned
555 * with gst_caps_unref().
558 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
559 GstTypeFindProbability * prob)
564 g_return_val_if_fail (buf != NULL, NULL);
565 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
566 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
567 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
569 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
571 result = gst_type_find_helper_for_data (obj, info.data, info.size, prob);
572 gst_buffer_unmap (buf, &info);
578 * gst_type_find_helper_for_extension:
579 * @obj: (allow-none): object doing the typefinding, or NULL (used for logging)
580 * @extension: an extension
582 * Tries to find the best #GstCaps associated with @extension.
584 * All available typefinders will be checked against the extension in order
585 * of rank. The caps of the first typefinder that can handle @extension will be
588 * Free-function: gst_caps_unref
590 * Returns: (transfer full): the #GstCaps corresponding to @extension, or
591 * #NULL if no type could be found. The caller should free the caps
592 * returned with gst_caps_unref().
597 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
599 GList *l, *type_list;
600 GstCaps *result = NULL;
602 g_return_val_if_fail (extension != NULL, NULL);
604 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
606 type_list = gst_type_find_factory_get_list ();
608 for (l = type_list; l; l = g_list_next (l)) {
609 GstTypeFindFactory *factory;
613 factory = GST_TYPE_FIND_FACTORY (l->data);
615 /* we only want to check those factories without a function */
616 if (factory->function != NULL)
619 /* get the extension that this typefind factory can handle */
620 ext = gst_type_find_factory_get_extensions (factory);
624 /* there are extension, see if one of them matches the requested
626 for (i = 0; ext[i]; i++) {
627 if (strcmp (ext[i], extension) == 0) {
628 /* we found a matching extension, take the caps */
629 if ((result = gst_type_find_factory_get_caps (factory))) {
630 gst_caps_ref (result);
637 gst_plugin_feature_list_free (type_list);
639 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);