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 * @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: (nullable): address of the data or %NULL if buffer does not cover
78 * the requested range.
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);
117 buf_offset = GST_BUFFER_OFFSET (buf);
118 buf_size = bmp->map.size;
120 /* buffers are kept sorted by end offset (highest first) in the list, so
121 * at this point we save the current position and stop searching if
122 * we're after the searched end offset */
123 if (buf_offset <= offset) {
124 if ((offset + size) < (buf_offset + buf_size)) {
125 /* must already have been mapped before */
126 return (guint8 *) bmp->map.data + (offset - buf_offset);
128 } else if (offset + size >= buf_offset + buf_size) {
136 /* some typefinders go in 1 byte steps over 1k of data and request
137 * small buffers. It is really inefficient to pull each time, and pulling
138 * a larger chunk is almost free. Trying to pull a larger chunk at the end
139 * of the file is also not a problem here, we'll just get a truncated buffer
140 * in that case (and we'll have to double-check the size we actually get
141 * anyway, see below) */
143 helper->func (helper->obj, helper->parent, offset, MAX (size, 4096),
146 if (ret != GST_FLOW_OK)
150 caps = GST_BUFFER_CAPS (buffer);
152 if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) {
153 GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability",
156 gst_caps_replace (&helper->caps, caps);
157 helper->best_probability = GST_TYPE_FIND_MAXIMUM;
159 gst_buffer_unref (buffer);
164 /* getrange might silently return shortened buffers at the end of a file,
165 * we must, however, always return either the full requested data or %NULL */
166 buf_offset = GST_BUFFER_OFFSET (buffer);
167 buf_size = gst_buffer_get_size (buffer);
169 if ((buf_offset != -1 && buf_offset != offset) || buf_size < size) {
170 GST_DEBUG ("dropping short buffer: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
171 " instead of %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
172 buf_offset, buf_offset + buf_size - 1, offset, offset + size - 1);
173 gst_buffer_unref (buffer);
177 bmap = g_slice_new0 (GstMappedBuffer);
179 if (!gst_buffer_map (buffer, &bmap->map, GST_MAP_READ))
182 bmap->buffer = buffer;
185 helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, bmap);
187 /* if insert_pos is not set, our offset is bigger than the largest offset
188 * we have so far; since we keep the list sorted with highest offsets
189 * first, we need to prepend the buffer to the list */
190 helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
191 helper->buffers = g_slist_prepend (helper->buffers, bmap);
194 return bmap->map.data;
198 GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret));
203 GST_ERROR ("map failed");
204 gst_buffer_unref (buffer);
205 g_slice_free (GstMappedBuffer, bmap);
211 * helper_find_suggest:
212 * @data: helper data struct
213 * @probability: probability of the match
214 * @caps: caps of the type
216 * If given @probability is higher, replace previously store caps.
219 helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
221 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
223 GST_LOG_OBJECT (helper->obj,
224 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
225 GST_OBJECT_NAME (helper->factory), probability, caps);
227 if (probability > helper->best_probability) {
228 gst_caps_replace (&helper->caps, caps);
229 helper->best_probability = probability;
234 helper_find_get_length (gpointer data)
236 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
238 GST_LOG_OBJECT (helper->obj, "'%s' called get_length, returning %"
239 G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
245 * gst_type_find_helper_get_range:
246 * @obj: A #GstObject that will be passed as first argument to @func
247 * @parent: (allow-none): the parent of @obj or %NULL
248 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
249 * be used to access data at random offsets when doing the typefinding
250 * @size: The length in bytes
251 * @extension: extension of the media
252 * @prob: (out) (allow-none): location to store the probability of the found
255 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
256 * however, this function will use the specified function @func to obtain the
257 * data needed by the typefind functions, rather than operating on a given
258 * source pad. This is useful mostly for elements like tag demuxers which
259 * strip off data at the beginning and/or end of a file and want to typefind
260 * the stripped data stream before adding their own source pad (the specified
261 * callback can then call the upstream peer pad with offsets adjusted for the
262 * tag size, for example).
264 * When @extension is not %NULL, this function will first try the typefind
265 * functions for the given extension, which might speed up the typefinding
268 * Free-function: gst_caps_unref
270 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
271 * stream. Returns %NULL if no #GstCaps matches the data stream.
274 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
275 GstTypeFindHelperGetRangeFunction func, guint64 size,
276 const gchar * extension, GstTypeFindProbability * prob)
278 GstTypeFindHelper helper;
281 GList *l, *type_list;
282 GstCaps *result = NULL;
285 g_return_val_if_fail (GST_IS_OBJECT (obj), NULL);
286 g_return_val_if_fail (func != NULL, NULL);
288 helper.buffers = NULL;
290 helper.last_offset = 0;
292 helper.best_probability = GST_TYPE_FIND_NONE;
295 helper.parent = parent;
298 find.peek = helper_find_peek;
299 find.suggest = helper_find_suggest;
301 if (size == 0 || size == (guint64) - 1) {
302 find.get_length = NULL;
304 find.get_length = helper_find_get_length;
307 type_list = gst_type_find_factory_get_list ();
309 /* move the typefinders for the extension first in the list. The idea is that
310 * when one of them returns MAX we don't need to search further as there is a
311 * very high chance we got the right type. */
315 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head",
318 for (l = type_list; l; l = next) {
319 const gchar *const *ext;
320 GstTypeFindFactory *factory;
324 factory = GST_TYPE_FIND_FACTORY (l->data);
326 ext = gst_type_find_factory_get_extensions (factory);
330 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
331 GST_OBJECT_NAME (factory), extension);
333 while (*ext != NULL) {
334 if (strcmp (*ext, extension) == 0) {
335 /* found extension, move in front */
336 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
338 /* remove entry from list */
339 type_list = g_list_delete_link (type_list, l);
340 /* insert at the position */
341 type_list = g_list_insert (type_list, factory, pos);
342 /* next element will be inserted after this one */
351 for (l = type_list; l; l = l->next) {
352 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
353 gst_type_find_factory_call_function (helper.factory, &find);
354 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
357 gst_plugin_feature_list_free (type_list);
359 for (walk = helper.buffers; walk; walk = walk->next) {
360 GstMappedBuffer *bmap = (GstMappedBuffer *) walk->data;
362 gst_buffer_unmap (bmap->buffer, &bmap->map);
363 gst_buffer_unref (bmap->buffer);
364 g_slice_free (GstMappedBuffer, bmap);
366 g_slist_free (helper.buffers);
368 if (helper.best_probability > 0)
369 result = helper.caps;
372 *prob = helper.best_probability;
374 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
375 result, (guint) helper.best_probability);
381 * gst_type_find_helper:
382 * @src: A source #GstPad
383 * @size: The length in bytes
385 * Tries to find what type of data is flowing from the given source #GstPad.
387 * Free-function: gst_caps_unref
389 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
390 * stream. Returns %NULL if no #GstCaps matches the data stream.
394 gst_type_find_helper (GstPad * src, guint64 size)
396 GstTypeFindHelperGetRangeFunction func;
398 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
399 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
401 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
403 return gst_type_find_helper_get_range (GST_OBJECT (src),
404 GST_OBJECT_PARENT (src), func, size, NULL, NULL);
407 /* ********************** typefinding for buffers ************************* */
411 const guint8 *data; /* buffer data */
413 GstTypeFindProbability best_probability;
415 GstTypeFindFactory *factory; /* for logging */
416 GstObject *obj; /* for logging */
417 } GstTypeFindBufHelper;
420 * buf_helper_find_peek:
421 * @data: helper data struct
422 * @off: stream offset
425 * Get data pointer within a buffer.
427 * Returns: (nullable): address inside the buffer or %NULL if buffer does not
428 * cover the requested range.
430 static const guint8 *
431 buf_helper_find_peek (gpointer data, gint64 off, guint size)
433 GstTypeFindBufHelper *helper;
435 helper = (GstTypeFindBufHelper *) data;
436 GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
437 GST_OBJECT_NAME (helper->factory), off, size);
443 GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
444 GST_OBJECT_NAME (helper->factory));
448 if ((off + size) <= helper->size)
449 return helper->data + off;
455 * buf_helper_find_suggest:
456 * @data: helper data struct
457 * @probability: probability of the match
458 * @caps: caps of the type
460 * If given @probability is higher, replace previously store caps.
463 buf_helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
465 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
467 GST_LOG_OBJECT (helper->obj,
468 "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
469 GST_OBJECT_NAME (helper->factory), probability, caps);
471 /* Note: not >= as we call typefinders in order of rank, highest first */
472 if (probability > helper->best_probability) {
473 gst_caps_replace (&helper->caps, caps);
474 helper->best_probability = probability;
479 * gst_type_find_helper_for_data:
480 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
481 * @data: (in) (transfer none): a pointer with data to typefind
482 * @size: (in): the size of @data
483 * @prob: (out) (allow-none): location to store the probability of the found
486 * Tries to find what type of data is contained in the given @data, the
487 * assumption being that the data represents the beginning of the stream or
490 * All available typefinders will be called on the data in order of rank. If
491 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
492 * typefinding is stopped immediately and the found caps will be returned
493 * right away. Otherwise, all available typefind functions will the tried,
494 * and the caps with the highest probability will be returned, or %NULL if
495 * the content of @data could not be identified.
497 * Free-function: gst_caps_unref
499 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
500 * or %NULL if no type could be found. The caller should free the caps
501 * returned with gst_caps_unref().
504 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
505 GstTypeFindProbability * prob)
507 GstTypeFindBufHelper helper;
509 GList *l, *type_list;
510 GstCaps *result = NULL;
512 g_return_val_if_fail (data != NULL, NULL);
516 helper.best_probability = GST_TYPE_FIND_NONE;
520 if (helper.data == NULL || helper.size == 0)
524 find.peek = buf_helper_find_peek;
525 find.suggest = buf_helper_find_suggest;
526 find.get_length = NULL;
528 type_list = gst_type_find_factory_get_list ();
530 for (l = type_list; l; l = l->next) {
531 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
532 gst_type_find_factory_call_function (helper.factory, &find);
533 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
536 gst_plugin_feature_list_free (type_list);
538 if (helper.best_probability > 0)
539 result = helper.caps;
542 *prob = helper.best_probability;
544 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
545 result, (guint) helper.best_probability);
551 * gst_type_find_helper_for_buffer:
552 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
553 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
554 * @prob: (out) (allow-none): location to store the probability of the found
557 * Tries to find what type of data is contained in the given #GstBuffer, the
558 * assumption being that the buffer represents the beginning of the stream or
561 * All available typefinders will be called on the data in order of rank. If
562 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
563 * typefinding is stopped immediately and the found caps will be returned
564 * right away. Otherwise, all available typefind functions will the tried,
565 * and the caps with the highest probability will be returned, or %NULL if
566 * the content of the buffer could not be identified.
568 * Free-function: gst_caps_unref
570 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
571 * or %NULL if no type could be found. The caller should free the caps
572 * returned with gst_caps_unref().
575 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
576 GstTypeFindProbability * prob)
581 g_return_val_if_fail (buf != NULL, NULL);
582 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
583 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
584 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
586 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
588 result = gst_type_find_helper_for_data (obj, info.data, info.size, prob);
589 gst_buffer_unmap (buf, &info);
595 * gst_type_find_helper_for_extension:
596 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
597 * @extension: an extension
599 * Tries to find the best #GstCaps associated with @extension.
601 * All available typefinders will be checked against the extension in order
602 * of rank. The caps of the first typefinder that can handle @extension will be
605 * Free-function: gst_caps_unref
607 * Returns: (transfer full) (nullable): the #GstCaps corresponding to
608 * @extension, or %NULL if no type could be found. The caller should free
609 * the caps returned with gst_caps_unref().
612 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
614 GList *l, *type_list;
615 GstCaps *result = NULL;
617 g_return_val_if_fail (extension != NULL, NULL);
619 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
621 type_list = gst_type_find_factory_get_list ();
623 for (l = type_list; l; l = g_list_next (l)) {
624 GstTypeFindFactory *factory;
625 const gchar *const *ext;
627 factory = GST_TYPE_FIND_FACTORY (l->data);
629 /* we only want to check those factories without a function */
630 if (gst_type_find_factory_has_function (factory))
633 /* get the extension that this typefind factory can handle */
634 ext = gst_type_find_factory_get_extensions (factory);
638 /* there are extension, see if one of them matches the requested
640 while (*ext != NULL) {
641 if (strcmp (*ext, extension) == 0) {
642 /* we found a matching extension, take the caps */
643 if ((result = gst_type_find_factory_get_caps (factory))) {
644 gst_caps_ref (result);
652 gst_plugin_feature_list_free (type_list);
654 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);