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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:gsttypefindhelper
26 * @title: GstTypeFindHelper
27 * @short_description: Utility functions for typefinding
29 * Utility functions for elements doing typefinding:
30 * gst_type_find_helper() does typefinding in pull mode, while
31 * gst_type_find_helper_for_buffer() is useful for elements needing to do
32 * typefinding in push mode from a chain function.
42 #include "gsttypefindhelper.h"
44 /* ********************** typefinding in pull mode ************************ */
47 helper_find_suggest (gpointer data, guint probability, GstCaps * caps);
57 GSList *buffers; /* buffer cache */
60 GstTypeFindHelperGetRangeFunction func;
61 GstTypeFindProbability best_probability;
63 GstTypeFindFactory *factory; /* for logging */
64 GstObject *obj; /* for logging */
70 * @data: helper data struct
74 * Get data pointer within a stream. Keeps a cache of read buffers (partly
75 * for performance reasons, but mostly because pointers returned by us need
76 * to stay valid until typefinding has finished)
78 * Returns: (nullable): address of the data or %NULL if buffer does not cover
79 * the requested range.
82 helper_find_peek (gpointer data, gint64 offset, guint size)
84 GstTypeFindHelper *helper;
87 GSList *insert_pos = NULL;
90 GstMappedBuffer *bmap;
95 helper = (GstTypeFindHelper *) data;
97 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT
98 ", %u)", GST_OBJECT_NAME (helper->factory), offset, size);
104 if (helper->size == -1 || helper->size < -offset)
107 offset += helper->size;
110 /* see if we have a matching buffer already in our list */
111 if (size > 0 && offset <= helper->last_offset) {
114 for (walk = helper->buffers; walk; walk = walk->next) {
115 GstMappedBuffer *bmp = (GstMappedBuffer *) walk->data;
116 GstBuffer *buf = GST_BUFFER_CAST (bmp->buffer);
118 buf_offset = GST_BUFFER_OFFSET (buf);
119 buf_size = bmp->map.size;
121 /* buffers are kept sorted by end offset (highest first) in the list, so
122 * at this point we save the current position and stop searching if
123 * we're after the searched end offset */
124 if (buf_offset <= offset) {
125 if ((offset + size) < (buf_offset + buf_size)) {
126 /* must already have been mapped before */
127 return (guint8 *) bmp->map.data + (offset - buf_offset);
129 } else if (offset + size >= buf_offset + buf_size) {
137 /* some typefinders go in 1 byte steps over 1k of data and request
138 * small buffers. It is really inefficient to pull each time, and pulling
139 * a larger chunk is almost free. Trying to pull a larger chunk at the end
140 * of the file is also not a problem here, we'll just get a truncated buffer
141 * in that case (and we'll have to double-check the size we actually get
142 * anyway, see below) */
144 helper->func (helper->obj, helper->parent, offset, MAX (size, 4096),
147 if (ret != GST_FLOW_OK)
151 caps = GST_BUFFER_CAPS (buffer);
153 if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) {
154 GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability",
157 gst_caps_replace (&helper->caps, caps);
158 helper->best_probability = GST_TYPE_FIND_MAXIMUM;
160 gst_buffer_unref (buffer);
165 /* getrange might silently return shortened buffers at the end of a file,
166 * we must, however, always return either the full requested data or %NULL */
167 buf_offset = GST_BUFFER_OFFSET (buffer);
168 buf_size = gst_buffer_get_size (buffer);
170 if ((buf_offset != -1 && buf_offset != offset) || buf_size < size) {
171 GST_DEBUG ("dropping short buffer: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
172 " instead of %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
173 buf_offset, buf_offset + buf_size - 1, offset, offset + size - 1);
174 gst_buffer_unref (buffer);
178 bmap = g_slice_new0 (GstMappedBuffer);
180 if (!gst_buffer_map (buffer, &bmap->map, GST_MAP_READ))
183 bmap->buffer = buffer;
186 helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, bmap);
188 /* if insert_pos is not set, our offset is bigger than the largest offset
189 * we have so far; since we keep the list sorted with highest offsets
190 * first, we need to prepend the buffer to the list */
191 helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
192 helper->buffers = g_slist_prepend (helper->buffers, bmap);
195 return bmap->map.data;
199 GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret));
204 GST_ERROR ("map failed");
205 gst_buffer_unref (buffer);
206 g_slice_free (GstMappedBuffer, bmap);
212 * helper_find_suggest:
213 * @data: helper data struct
214 * @probability: probability of the match
215 * @caps: caps of the type
217 * If given @probability is higher, replace previously store caps.
220 helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
222 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
224 GST_LOG_OBJECT (helper->obj,
225 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
226 GST_OBJECT_NAME (helper->factory), probability, caps);
228 if (probability > helper->best_probability) {
229 gst_caps_replace (&helper->caps, caps);
230 helper->best_probability = probability;
235 helper_find_get_length (gpointer data)
237 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
239 GST_LOG_OBJECT (helper->obj, "'%s' called get_length, returning %"
240 G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
246 * gst_type_find_helper_get_range:
247 * @obj: A #GstObject that will be passed as first argument to @func
248 * @parent: (allow-none): the parent of @obj or %NULL
249 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
250 * be used to access data at random offsets when doing the typefinding
251 * @size: The length in bytes
252 * @extension: extension of the media
253 * @prob: (out) (allow-none): location to store the probability of the found
256 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
257 * however, this function will use the specified function @func to obtain the
258 * data needed by the typefind functions, rather than operating on a given
259 * source pad. This is useful mostly for elements like tag demuxers which
260 * strip off data at the beginning and/or end of a file and want to typefind
261 * the stripped data stream before adding their own source pad (the specified
262 * callback can then call the upstream peer pad with offsets adjusted for the
263 * tag size, for example).
265 * When @extension is not %NULL, this function will first try the typefind
266 * functions for the given extension, which might speed up the typefinding
269 * Free-function: gst_caps_unref
271 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
272 * stream. Returns %NULL if no #GstCaps matches the data stream.
275 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
276 GstTypeFindHelperGetRangeFunction func, guint64 size,
277 const gchar * extension, GstTypeFindProbability * prob)
279 GstTypeFindHelper helper;
282 GList *l, *type_list;
283 GstCaps *result = NULL;
286 g_return_val_if_fail (GST_IS_OBJECT (obj), NULL);
287 g_return_val_if_fail (func != NULL, NULL);
289 helper.buffers = NULL;
291 helper.last_offset = 0;
293 helper.best_probability = GST_TYPE_FIND_NONE;
296 helper.parent = parent;
299 find.peek = helper_find_peek;
300 find.suggest = helper_find_suggest;
302 if (size == 0 || size == (guint64) - 1) {
303 find.get_length = NULL;
305 find.get_length = helper_find_get_length;
308 type_list = gst_type_find_factory_get_list ();
310 /* move the typefinders for the extension first in the list. The idea is that
311 * when one of them returns MAX we don't need to search further as there is a
312 * very high chance we got the right type. */
316 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head",
319 for (l = type_list; l; l = next) {
320 const gchar *const *ext;
321 GstTypeFindFactory *factory;
325 factory = GST_TYPE_FIND_FACTORY (l->data);
327 ext = gst_type_find_factory_get_extensions (factory);
331 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
332 GST_OBJECT_NAME (factory), extension);
334 while (*ext != NULL) {
335 if (strcmp (*ext, extension) == 0) {
336 /* found extension, move in front */
337 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
339 /* remove entry from list */
340 type_list = g_list_delete_link (type_list, l);
341 /* insert at the position */
342 type_list = g_list_insert (type_list, factory, pos);
343 /* next element will be inserted after this one */
352 for (l = type_list; l; l = l->next) {
353 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
354 gst_type_find_factory_call_function (helper.factory, &find);
355 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
358 gst_plugin_feature_list_free (type_list);
360 for (walk = helper.buffers; walk; walk = walk->next) {
361 GstMappedBuffer *bmap = (GstMappedBuffer *) walk->data;
363 gst_buffer_unmap (bmap->buffer, &bmap->map);
364 gst_buffer_unref (bmap->buffer);
365 g_slice_free (GstMappedBuffer, bmap);
367 g_slist_free (helper.buffers);
369 if (helper.best_probability > 0)
370 result = helper.caps;
373 *prob = helper.best_probability;
375 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
376 result, (guint) helper.best_probability);
382 * gst_type_find_helper:
383 * @src: A source #GstPad
384 * @size: The length in bytes
386 * Tries to find what type of data is flowing from the given source #GstPad.
388 * Free-function: gst_caps_unref
390 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
391 * stream. Returns %NULL if no #GstCaps matches the data stream.
395 gst_type_find_helper (GstPad * src, guint64 size)
397 GstTypeFindHelperGetRangeFunction func;
399 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
400 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
402 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
404 return gst_type_find_helper_get_range (GST_OBJECT (src),
405 GST_OBJECT_PARENT (src), func, size, NULL, NULL);
408 /* ********************** typefinding for buffers ************************* */
412 const guint8 *data; /* buffer data */
414 GstTypeFindProbability best_probability;
416 GstTypeFindFactory *factory; /* for logging */
417 GstObject *obj; /* for logging */
418 } GstTypeFindBufHelper;
421 * buf_helper_find_peek:
422 * @data: helper data struct
423 * @off: stream offset
426 * Get data pointer within a buffer.
428 * Returns: (nullable): address inside the buffer or %NULL if buffer does not
429 * cover the requested range.
431 static const guint8 *
432 buf_helper_find_peek (gpointer data, gint64 off, guint size)
434 GstTypeFindBufHelper *helper;
436 helper = (GstTypeFindBufHelper *) data;
437 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
438 GST_OBJECT_NAME (helper->factory), off, size);
444 GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
445 GST_OBJECT_NAME (helper->factory));
449 if ((off + size) <= helper->size)
450 return helper->data + off;
456 * buf_helper_find_suggest:
457 * @data: helper data struct
458 * @probability: probability of the match
459 * @caps: caps of the type
461 * If given @probability is higher, replace previously store caps.
464 buf_helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
466 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
468 GST_LOG_OBJECT (helper->obj,
469 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
470 GST_OBJECT_NAME (helper->factory), probability, caps);
472 /* Note: not >= as we call typefinders in order of rank, highest first */
473 if (probability > helper->best_probability) {
474 gst_caps_replace (&helper->caps, caps);
475 helper->best_probability = probability;
480 * gst_type_find_helper_for_data:
481 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
482 * @data: (in) (transfer none): a pointer with data to typefind
483 * @size: (in): the size of @data
484 * @prob: (out) (allow-none): location to store the probability of the found
487 * Tries to find what type of data is contained in the given @data, the
488 * assumption being that the data represents the beginning of the stream or
491 * All available typefinders will be called on the data in order of rank. If
492 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
493 * typefinding is stopped immediately and the found caps will be returned
494 * right away. Otherwise, all available typefind functions will the tried,
495 * and the caps with the highest probability will be returned, or %NULL if
496 * the content of @data could not be identified.
498 * Free-function: gst_caps_unref
500 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
501 * or %NULL if no type could be found. The caller should free the caps
502 * returned with gst_caps_unref().
505 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
506 GstTypeFindProbability * prob)
508 GstTypeFindBufHelper helper;
510 GList *l, *type_list;
511 GstCaps *result = NULL;
513 g_return_val_if_fail (data != NULL, NULL);
517 helper.best_probability = GST_TYPE_FIND_NONE;
521 if (helper.data == NULL || helper.size == 0)
525 find.peek = buf_helper_find_peek;
526 find.suggest = buf_helper_find_suggest;
527 find.get_length = NULL;
529 type_list = gst_type_find_factory_get_list ();
531 for (l = type_list; l; l = l->next) {
532 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
533 gst_type_find_factory_call_function (helper.factory, &find);
534 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
537 gst_plugin_feature_list_free (type_list);
539 if (helper.best_probability > 0)
540 result = helper.caps;
543 *prob = helper.best_probability;
545 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
546 result, (guint) helper.best_probability);
552 * gst_type_find_helper_for_buffer:
553 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
554 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
555 * @prob: (out) (allow-none): location to store the probability of the found
558 * Tries to find what type of data is contained in the given #GstBuffer, the
559 * assumption being that the buffer represents the beginning of the stream or
562 * All available typefinders will be called on the data in order of rank. If
563 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
564 * typefinding is stopped immediately and the found caps will be returned
565 * right away. Otherwise, all available typefind functions will the tried,
566 * and the caps with the highest probability will be returned, or %NULL if
567 * the content of the buffer could not be identified.
569 * Free-function: gst_caps_unref
571 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
572 * or %NULL if no type could be found. The caller should free the caps
573 * returned with gst_caps_unref().
576 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
577 GstTypeFindProbability * prob)
582 g_return_val_if_fail (buf != NULL, NULL);
583 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
584 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
585 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
587 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
589 result = gst_type_find_helper_for_data (obj, info.data, info.size, prob);
590 gst_buffer_unmap (buf, &info);
596 * gst_type_find_helper_for_extension:
597 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
598 * @extension: an extension
600 * Tries to find the best #GstCaps associated with @extension.
602 * All available typefinders will be checked against the extension in order
603 * of rank. The caps of the first typefinder that can handle @extension will be
606 * Free-function: gst_caps_unref
608 * Returns: (transfer full) (nullable): the #GstCaps corresponding to
609 * @extension, or %NULL if no type could be found. The caller should free
610 * the caps returned with gst_caps_unref().
613 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
615 GList *l, *type_list;
616 GstCaps *result = NULL;
618 g_return_val_if_fail (extension != NULL, NULL);
620 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
622 type_list = gst_type_find_factory_get_list ();
624 for (l = type_list; l; l = g_list_next (l)) {
625 GstTypeFindFactory *factory;
626 const gchar *const *ext;
628 factory = GST_TYPE_FIND_FACTORY (l->data);
630 /* we only want to check those factories without a function */
631 if (gst_type_find_factory_has_function (factory))
634 /* get the extension that this typefind factory can handle */
635 ext = gst_type_find_factory_get_extensions (factory);
639 /* there are extension, see if one of them matches the requested
641 while (*ext != NULL) {
642 if (strcmp (*ext, extension) == 0) {
643 /* we found a matching extension, take the caps */
644 if ((result = gst_type_find_factory_get_caps (factory))) {
645 gst_caps_ref (result);
653 gst_plugin_feature_list_free (type_list);
655 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);