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 */
66 GstFlowReturn flow_ret;
71 * @data: helper data struct
75 * Get data pointer within a stream. Keeps a cache of read buffers (partly
76 * for performance reasons, but mostly because pointers returned by us need
77 * to stay valid until typefinding has finished)
79 * Returns: (nullable): address of the data or %NULL if buffer does not cover
80 * the requested range.
83 helper_find_peek (gpointer data, gint64 offset, guint size)
85 GstTypeFindHelper *helper;
87 GSList *insert_pos = NULL;
90 GstMappedBuffer *bmap;
95 helper = (GstTypeFindHelper *) data;
97 GST_LOG_OBJECT (helper->obj, "Typefind factory called peek (%" G_GINT64_FORMAT
98 ", %u)", 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 (helper->flow_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_size < size) {
171 GST_DEBUG ("dropping short buffer of size %" G_GSIZE_FORMAT ","
172 "requested size was %u", buf_size, size);
173 gst_buffer_unref (buffer);
177 if (buf_offset != -1 && buf_offset != offset) {
178 GST_DEBUG ("dropping buffer with unexpected offset %" G_GUINT64_FORMAT ", "
179 "expected offset was %" G_GUINT64_FORMAT, buf_offset, offset);
180 gst_buffer_unref (buffer);
184 bmap = g_slice_new0 (GstMappedBuffer);
186 if (!gst_buffer_map (buffer, &bmap->map, GST_MAP_READ))
189 bmap->buffer = buffer;
192 helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, bmap);
194 /* if insert_pos is not set, our offset is bigger than the largest offset
195 * we have so far; since we keep the list sorted with highest offsets
196 * first, we need to prepend the buffer to the list */
197 helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
198 helper->buffers = g_slist_prepend (helper->buffers, bmap);
201 return bmap->map.data;
205 GST_INFO ("typefind function returned: %s",
206 gst_flow_get_name (helper->flow_ret));
211 GST_ERROR ("map failed");
212 gst_buffer_unref (buffer);
213 g_slice_free (GstMappedBuffer, bmap);
219 * helper_find_suggest:
220 * @data: helper data struct
221 * @probability: probability of the match
222 * @caps: caps of the type
224 * If given @probability is higher, replace previously store caps.
227 helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
229 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
231 GST_LOG_OBJECT (helper->obj,
232 "Typefind factory called suggest (%u, %" GST_PTR_FORMAT ")",
235 if (probability > helper->best_probability) {
236 gst_caps_replace (&helper->caps, caps);
237 helper->best_probability = probability;
242 helper_find_get_length (gpointer data)
244 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
246 GST_LOG_OBJECT (helper->obj, "Typefind factory called get_length, returning %"
247 G_GUINT64_FORMAT, helper->size);
253 prioritize_extension (GstObject * obj, GList * type_list,
254 const gchar * extension)
262 /* move the typefinders for the extension first in the list. The idea is that
263 * when one of them returns MAX we don't need to search further as there is a
264 * very high chance we got the right type. */
266 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head", extension);
268 for (l = type_list; l; l = next) {
269 const gchar *const *ext;
270 GstTypeFindFactory *factory;
274 factory = GST_TYPE_FIND_FACTORY (l->data);
276 ext = gst_type_find_factory_get_extensions (factory);
280 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
281 GST_OBJECT_NAME (factory), extension);
283 while (*ext != NULL) {
284 if (strcmp (*ext, extension) == 0) {
285 /* found extension, move in front */
286 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
288 /* remove entry from list */
289 type_list = g_list_delete_link (type_list, l);
290 /* insert at the position */
291 type_list = g_list_insert (type_list, factory, pos);
292 /* next element will be inserted after this one */
304 * gst_type_find_helper_get_range:
305 * @obj: A #GstObject that will be passed as first argument to @func
306 * @parent: (nullable): the parent of @obj or %NULL
307 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
308 * be used to access data at random offsets when doing the typefinding
309 * @size: The length in bytes
310 * @extension: (nullable): extension of the media, or %NULL
311 * @prob: (out) (optional): location to store the probability of the found
314 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
315 * however, this function will use the specified function @func to obtain the
316 * data needed by the typefind functions, rather than operating on a given
317 * source pad. This is useful mostly for elements like tag demuxers which
318 * strip off data at the beginning and/or end of a file and want to typefind
319 * the stripped data stream before adding their own source pad (the specified
320 * callback can then call the upstream peer pad with offsets adjusted for the
321 * tag size, for example).
323 * When @extension is not %NULL, this function will first try the typefind
324 * functions for the given extension, which might speed up the typefinding
327 * Free-function: gst_caps_unref
329 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
330 * stream. Returns %NULL if no #GstCaps matches the data stream.
333 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
334 GstTypeFindHelperGetRangeFunction func, guint64 size,
335 const gchar * extension, GstTypeFindProbability * prob)
337 GstCaps *caps = NULL;
339 gst_type_find_helper_get_range_full (obj, parent, func, size, extension,
346 * gst_type_find_helper_get_range_full:
347 * @obj: A #GstObject that will be passed as first argument to @func
348 * @parent: (nullable): the parent of @obj or %NULL
349 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
350 * be used to access data at random offsets when doing the typefinding
351 * @size: The length in bytes
352 * @extension: (nullable): extension of the media, or %NULL
353 * @caps: (out) (transfer full): returned caps
354 * @prob: (out) (optional): location to store the probability of the found
357 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
358 * however, this function will use the specified function @func to obtain the
359 * data needed by the typefind functions, rather than operating on a given
360 * source pad. This is useful mostly for elements like tag demuxers which
361 * strip off data at the beginning and/or end of a file and want to typefind
362 * the stripped data stream before adding their own source pad (the specified
363 * callback can then call the upstream peer pad with offsets adjusted for the
364 * tag size, for example).
366 * When @extension is not %NULL, this function will first try the typefind
367 * functions for the given extension, which might speed up the typefinding
370 * Returns: the last %GstFlowReturn from pulling a buffer or %GST_FLOW_OK if
371 * typefinding was successful.
376 gst_type_find_helper_get_range_full (GstObject * obj, GstObject * parent,
377 GstTypeFindHelperGetRangeFunction func, guint64 size,
378 const gchar * extension, GstCaps ** caps, GstTypeFindProbability * prob)
380 GstTypeFindHelper helper;
383 GList *l, *type_list;
384 GstCaps *result = NULL;
386 g_return_val_if_fail (GST_IS_OBJECT (obj), GST_FLOW_ERROR);
387 g_return_val_if_fail (func != NULL, GST_FLOW_ERROR);
388 g_return_val_if_fail (caps != NULL, GST_FLOW_ERROR);
392 helper.buffers = NULL;
394 helper.last_offset = 0;
396 helper.best_probability = GST_TYPE_FIND_NONE;
399 helper.parent = parent;
400 helper.flow_ret = GST_FLOW_OK;
403 find.peek = helper_find_peek;
404 find.suggest = helper_find_suggest;
406 if (size == 0 || size == (guint64) - 1) {
407 find.get_length = NULL;
409 find.get_length = helper_find_get_length;
412 type_list = gst_type_find_factory_get_list ();
413 type_list = prioritize_extension (obj, type_list, extension);
415 for (l = type_list; l; l = l->next) {
416 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
417 gst_type_find_factory_call_function (helper.factory, &find);
418 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM) {
419 /* Any other flow return can be ignored here, we found
420 * something before any error with highest probability */
421 helper.flow_ret = GST_FLOW_OK;
423 } else if (helper.flow_ret != GST_FLOW_OK
424 && helper.flow_ret != GST_FLOW_EOS) {
425 /* We had less than maximum probability and an error, don't return
426 * any caps as they might be with a lower probability than what
427 * we would've gotten when continuing if there was no error */
428 gst_caps_replace (&helper.caps, NULL);
432 gst_plugin_feature_list_free (type_list);
434 for (walk = helper.buffers; walk; walk = walk->next) {
435 GstMappedBuffer *bmap = (GstMappedBuffer *) walk->data;
437 gst_buffer_unmap (bmap->buffer, &bmap->map);
438 gst_buffer_unref (bmap->buffer);
439 g_slice_free (GstMappedBuffer, bmap);
441 g_slist_free (helper.buffers);
443 if (helper.best_probability > 0)
444 result = helper.caps;
447 *prob = helper.best_probability;
450 if (helper.flow_ret == GST_FLOW_EOS) {
451 /* Some typefinder might've tried to read too much, if we
452 * didn't get any meaningful caps because of that this is
453 * just a normal error */
454 helper.flow_ret = GST_FLOW_ERROR;
457 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
458 result, (guint) helper.best_probability);
460 return helper.flow_ret;
464 * gst_type_find_helper:
465 * @src: A source #GstPad
466 * @size: The length in bytes
468 * Tries to find what type of data is flowing from the given source #GstPad.
470 * Free-function: gst_caps_unref
472 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
473 * stream. Returns %NULL if no #GstCaps matches the data stream.
477 gst_type_find_helper (GstPad * src, guint64 size)
479 GstTypeFindHelperGetRangeFunction func;
481 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
482 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
484 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
486 return gst_type_find_helper_get_range (GST_OBJECT (src),
487 GST_OBJECT_PARENT (src), func, size, NULL, NULL);
490 /* ********************** typefinding for buffers ************************* */
494 const guint8 *data; /* buffer data */
496 GstTypeFindProbability best_probability;
498 GstObject *obj; /* for logging */
499 } GstTypeFindBufHelper;
504 * The opaque #GstTypeFindData structure.
509 struct _GstTypeFindData
512 GstTypeFindBufHelper helper;
516 * buf_helper_find_peek:
517 * @data: helper data struct
518 * @off: stream offset
521 * Get data pointer within a buffer.
523 * Returns: (nullable): address inside the buffer or %NULL if buffer does not
524 * cover the requested range.
526 static const guint8 *
527 buf_helper_find_peek (gpointer data, gint64 off, guint size)
529 GstTypeFindBufHelper *helper;
531 helper = (GstTypeFindBufHelper *) data;
532 GST_LOG_OBJECT (helper->obj,
533 "Typefind factory called peek (%" G_GINT64_FORMAT ", %u)", off, size);
539 GST_LOG_OBJECT (helper->obj,
540 "Typefind factory wanted to peek at end; not supported");
544 /* If we request beyond the available size, we're sure we can't return
545 * anything regardless of the requested offset */
546 if (size > helper->size)
549 /* Only return data if there's enough room left for the given offset.
550 * This is the same as "if (off + size <= helper->size)" except that
551 * it doesn't exceed type limits */
552 if (off <= helper->size - size)
553 return helper->data + off;
559 * buf_helper_find_suggest:
560 * @data: helper data struct
561 * @probability: probability of the match
562 * @caps: caps of the type
564 * If given @probability is higher, replace previously store caps.
567 buf_helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
569 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
571 GST_LOG_OBJECT (helper->obj,
572 "Typefind factory called suggest (%u, %" GST_PTR_FORMAT ")",
575 /* Note: not >= as we call typefinders in order of rank, highest first */
576 if (probability > helper->best_probability) {
577 gst_caps_replace (&helper->caps, caps);
578 helper->best_probability = probability;
583 * gst_type_find_helper_for_data:
584 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
585 * @data: (transfer none) (array length=size): * a pointer with data to typefind
586 * @size: the size of @data
587 * @prob: (out) (optional): location to store the probability of the found
590 * Tries to find what type of data is contained in the given @data, the
591 * assumption being that the data represents the beginning of the stream or
594 * All available typefinders will be called on the data in order of rank. If
595 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
596 * typefinding is stopped immediately and the found caps will be returned
597 * right away. Otherwise, all available typefind functions will the tried,
598 * and the caps with the highest probability will be returned, or %NULL if
599 * the content of @data could not be identified.
601 * Free-function: gst_caps_unref
603 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
604 * or %NULL if no type could be found. The caller should free the caps
605 * returned with gst_caps_unref().
608 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
609 GstTypeFindProbability * prob)
611 return gst_type_find_helper_for_data_with_extension (obj, data, size, NULL,
616 * gst_type_find_helper_for_data_with_extension:
617 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
618 * @data: (transfer none) (array length=size): * a pointer with data to typefind
619 * @size: the size of @data
620 * @extension: (nullable): extension of the media, or %NULL
621 * @prob: (out) (optional): location to store the probability of the found
624 * Tries to find what type of data is contained in the given @data, the
625 * assumption being that the data represents the beginning of the stream or
628 * All available typefinders will be called on the data in order of rank. If
629 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
630 * typefinding is stopped immediately and the found caps will be returned
631 * right away. Otherwise, all available typefind functions will the tried,
632 * and the caps with the highest probability will be returned, or %NULL if
633 * the content of @data could not be identified.
635 * When @extension is not %NULL, this function will first try the typefind
636 * functions for the given extension, which might speed up the typefinding
639 * Free-function: gst_caps_unref
641 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
642 * or %NULL if no type could be found. The caller should free the caps
643 * returned with gst_caps_unref().
649 gst_type_find_helper_for_data_with_extension (GstObject * obj,
650 const guint8 * data, gsize size, const gchar * extension,
651 GstTypeFindProbability * prob)
653 GstTypeFindBufHelper helper;
654 GstTypeFindFactory *factory;
656 GList *l, *type_list;
657 GstCaps *result = NULL;
659 g_return_val_if_fail (data != NULL, NULL);
663 helper.best_probability = GST_TYPE_FIND_NONE;
667 if (helper.data == NULL || helper.size == 0)
671 find.peek = buf_helper_find_peek;
672 find.suggest = buf_helper_find_suggest;
673 find.get_length = NULL;
675 type_list = gst_type_find_factory_get_list ();
676 type_list = prioritize_extension (obj, type_list, extension);
678 for (l = type_list; l; l = l->next) {
679 factory = GST_TYPE_FIND_FACTORY (l->data);
680 gst_type_find_factory_call_function (factory, &find);
681 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
684 gst_plugin_feature_list_free (type_list);
686 if (helper.best_probability > 0)
687 result = helper.caps;
690 *prob = helper.best_probability;
692 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
693 result, (guint) helper.best_probability);
699 * gst_type_find_helper_for_data_with_caps:
700 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
701 * @data: (transfer none) (array length=size): a pointer with data to typefind
702 * @size: the size of @data
703 * @caps: caps of the media
704 * @prob: (out) (optional): location to store the probability of the found
707 * Tries to find if type of media contained in the given @data, matches the
708 * @caps specified, assumption being that the data represents the beginning
709 * of the stream or file.
711 * Only the typefinder matching the given caps will be called, if found. The
712 * caps with the highest probability will be returned, or %NULL if the content
713 * of the @data could not be identified.
715 * Free-function: gst_caps_unref
717 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
718 * or %NULL if no type could be found. The caller should free the caps
719 * returned with gst_caps_unref().
725 gst_type_find_helper_for_data_with_caps (GstObject * obj,
726 const guint8 * data, gsize size, GstCaps * caps,
727 GstTypeFindProbability * prob)
730 GstTypeFindData *find_data;
731 GstTypeFindFactory *factory;
732 GList *l, *factories = NULL;
733 GstCaps *result = NULL;
734 GstTypeFindProbability found_probability, last_found_probability;
736 g_return_val_if_fail (data != NULL, NULL);
737 g_return_val_if_fail (caps != NULL, NULL);
738 g_return_val_if_fail (size != 0, NULL);
740 find_data = gst_type_find_data_new (obj, data, size);
741 find = gst_type_find_data_get_typefind (find_data);
743 factories = gst_type_find_list_factories_for_caps (obj, caps);
745 GST_ERROR_OBJECT (obj, "Failed to typefind for caps: %" GST_PTR_FORMAT,
750 found_probability = GST_TYPE_FIND_NONE;
751 last_found_probability = GST_TYPE_FIND_NONE;
753 for (l = factories; l; l = l->next) {
754 factory = GST_TYPE_FIND_FACTORY (l->data);
756 gst_type_find_factory_call_function (factory, find);
758 found_probability = gst_type_find_data_get_probability (find_data);
760 if (found_probability > last_found_probability) {
761 last_found_probability = found_probability;
762 result = gst_type_find_data_get_caps (find_data);
764 GST_DEBUG_OBJECT (obj, "Found %" GST_PTR_FORMAT " (probability = %u)",
765 result, (guint) last_found_probability);
766 if (last_found_probability >= GST_TYPE_FIND_MAXIMUM)
772 *prob = last_found_probability;
774 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
775 result, (guint) last_found_probability);
778 g_list_free_full (factories, (GDestroyNotify) gst_object_unref);
780 gst_type_find_data_free (find_data);
786 * gst_type_find_helper_for_buffer:
787 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
788 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
789 * @prob: (out) (optional): location to store the probability of the found
792 * Tries to find what type of data is contained in the given #GstBuffer, the
793 * assumption being that the buffer represents the beginning of the stream or
796 * All available typefinders will be called on the data in order of rank. If
797 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
798 * typefinding is stopped immediately and the found caps will be returned
799 * right away. Otherwise, all available typefind functions will the tried,
800 * and the caps with the highest probability will be returned, or %NULL if
801 * the content of the buffer could not be identified.
803 * Free-function: gst_caps_unref
805 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
806 * or %NULL if no type could be found. The caller should free the caps
807 * returned with gst_caps_unref().
810 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
811 GstTypeFindProbability * prob)
813 return gst_type_find_helper_for_buffer_with_extension (obj, buf, NULL, prob);
817 * gst_type_find_helper_for_buffer_with_extension:
818 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
819 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
820 * @extension: (nullable): extension of the media, or %NULL
821 * @prob: (out) (optional): location to store the probability of the found
824 * Tries to find what type of data is contained in the given #GstBuffer, the
825 * assumption being that the buffer represents the beginning of the stream or
828 * All available typefinders will be called on the data in order of rank. If
829 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
830 * typefinding is stopped immediately and the found caps will be returned
831 * right away. Otherwise, all available typefind functions will the tried,
832 * and the caps with the highest probability will be returned, or %NULL if
833 * the content of the buffer could not be identified.
835 * When @extension is not %NULL, this function will first try the typefind
836 * functions for the given extension, which might speed up the typefinding
839 * Free-function: gst_caps_unref
841 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
842 * or %NULL if no type could be found. The caller should free the caps
843 * returned with gst_caps_unref().
849 gst_type_find_helper_for_buffer_with_extension (GstObject * obj,
850 GstBuffer * buf, const gchar * extension, GstTypeFindProbability * prob)
855 g_return_val_if_fail (buf != NULL, NULL);
856 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
857 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
858 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
860 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
863 gst_type_find_helper_for_data_with_extension (obj, info.data, info.size,
865 gst_buffer_unmap (buf, &info);
871 * gst_type_find_helper_for_buffer_with_caps:
872 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
873 * @buf: (transfer none): a #GstBuffer with data to typefind
874 * @caps: caps of the media
875 * @prob: (out) (optional): location to store the probability of the found
878 * Tries to find if type of media contained in the given #GstBuffer, matches
879 * @caps specified, assumption being that the buffer represents the beginning
880 * of the stream or file.
882 * Tries to find what type of data is contained in the given @data, the
883 * assumption being that the data represents the beginning of the stream or
886 * Only the typefinder matching the given caps will be called, if found. The
887 * caps with the highest probability will be returned, or %NULL if the content
888 * of the @data could not be identified.
890 * Free-function: gst_caps_unref
892 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
893 * or %NULL if no type could be found. The caller should free the caps
894 * returned with gst_caps_unref().
900 gst_type_find_helper_for_buffer_with_caps (GstObject * obj,
901 GstBuffer * buf, GstCaps * caps, GstTypeFindProbability * prob)
906 g_return_val_if_fail (caps != NULL, NULL);
907 g_return_val_if_fail (buf != NULL, NULL);
908 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
909 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
910 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
912 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
916 gst_type_find_helper_for_data_with_caps (obj, info.data, info.size,
919 gst_buffer_unmap (buf, &info);
925 * gst_type_find_helper_for_extension:
926 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
927 * @extension: an extension
929 * Tries to find the best #GstCaps associated with @extension.
931 * All available typefinders will be checked against the extension in order
932 * of rank. The caps of the first typefinder that can handle @extension will be
935 * Free-function: gst_caps_unref
937 * Returns: (transfer full) (nullable): the #GstCaps corresponding to
938 * @extension, or %NULL if no type could be found. The caller should free
939 * the caps returned with gst_caps_unref().
942 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
944 GList *l, *type_list;
945 GstCaps *result = NULL;
947 g_return_val_if_fail (extension != NULL, NULL);
949 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
951 type_list = gst_type_find_factory_get_list ();
953 for (l = type_list; l; l = g_list_next (l)) {
954 GstTypeFindFactory *factory;
955 const gchar *const *ext;
957 factory = GST_TYPE_FIND_FACTORY (l->data);
959 /* we only want to check those factories without a function */
960 if (gst_type_find_factory_has_function (factory))
963 /* get the extension that this typefind factory can handle */
964 ext = gst_type_find_factory_get_extensions (factory);
968 /* there are extension, see if one of them matches the requested
970 while (*ext != NULL) {
971 if (strcmp (*ext, extension) == 0) {
972 /* we found a matching extension, take the caps */
973 if ((result = gst_type_find_factory_get_caps (factory))) {
974 gst_caps_ref (result);
982 gst_plugin_feature_list_free (type_list);
984 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);
990 * gst_type_find_list_factories_for_caps:
991 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
992 * @caps: caps of the media
994 * Tries to find the best #GstTypeFindFactory associated with @caps.
996 * The typefinder that can handle @caps will be returned.
998 * Free-function: g_list_free
1000 * Returns: (transfer full) (nullable) (element-type Gst.TypeFindFactory): the list of #GstTypeFindFactory
1001 * corresponding to @caps, or %NULL if no typefinder could be
1002 * found. Caller should free the returned list with g_list_free()
1003 * and list elements with gst_object_unref().
1009 gst_type_find_list_factories_for_caps (GstObject * obj, GstCaps * caps)
1011 GList *l, *type_list, *factories = NULL;
1013 g_return_val_if_fail (caps != NULL, NULL);
1015 GST_LOG_OBJECT (obj, "finding factory for caps %" GST_PTR_FORMAT, caps);
1017 type_list = gst_type_find_factory_get_list ();
1019 for (l = type_list; l; l = g_list_next (l)) {
1020 GstTypeFindFactory *factory;
1021 GstCaps *factory_caps;
1023 factory = GST_TYPE_FIND_FACTORY (l->data);
1025 /* We only want to check those factories without a function */
1026 if (gst_type_find_factory_has_function (factory))
1029 /* Get the caps that this typefind factory can handle */
1030 factory_caps = gst_type_find_factory_get_caps (factory);
1034 if (gst_caps_can_intersect (factory_caps, caps)) {
1035 factory = gst_object_ref (factory);
1036 factories = g_list_prepend (factories, factory);
1040 gst_plugin_feature_list_free (type_list);
1042 return g_list_reverse (factories);
1046 * gst_type_find_data_new: (skip)
1047 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
1048 * @data: (transfer none) (array length=size): a pointer with data to typefind
1049 * @size: the size of @data
1051 * Free-function: gst_type_find_data_free
1053 * Returns: (transfer full): the #GstTypeFindData. The caller should free
1054 * the returned #GstTypeFindData with gst_type_find_data_free().
1060 gst_type_find_data_new (GstObject * obj, const guint8 * data, gsize size)
1062 GstTypeFindData *find_data;
1064 g_return_val_if_fail (data != NULL, NULL);
1065 g_return_val_if_fail (size != 0, NULL);
1067 find_data = g_new0 (GstTypeFindData, 1);
1069 find_data->helper.data = data;
1070 find_data->helper.size = size;
1071 find_data->helper.best_probability = GST_TYPE_FIND_NONE;
1072 find_data->helper.caps = NULL;
1073 find_data->helper.obj = obj;
1075 find_data->find.data = (gpointer) (&find_data->helper);
1076 find_data->find.peek = buf_helper_find_peek;
1077 find_data->find.suggest = buf_helper_find_suggest;
1078 find_data->find.get_length = NULL;
1084 * gst_type_find_data_get_caps: (skip)
1085 * @data: GstTypeFindData *
1087 * Returns #GstCaps associated with #GstTypeFindData
1089 * Returns: (transfer full) (nullable): #GstCaps.
1095 gst_type_find_data_get_caps (GstTypeFindData * data)
1097 g_return_val_if_fail (data != NULL, NULL);
1099 return gst_caps_ref (data->helper.caps);
1103 * gst_type_find_data_get_probability: (skip)
1104 * @data: GstTypeFindData *
1106 * Returns #GstTypeFindProbability associated with #GstTypeFindData
1108 * Returns: #GstTypeFindProbability.
1113 GstTypeFindProbability
1114 gst_type_find_data_get_probability (GstTypeFindData * data)
1116 g_return_val_if_fail (data != NULL, GST_TYPE_FIND_NONE);
1118 return data->helper.best_probability;
1122 * gst_type_find_data_get_typefind: (skip)
1123 * @data: GstTypeFindData *
1125 * Returns #GstTypeFind associated with #GstTypeFindData
1127 * Returns: #GstTypeFind.
1133 gst_type_find_data_get_typefind (GstTypeFindData * data)
1135 g_return_val_if_fail (data != NULL, NULL);
1141 * gst_type_find_data_free: (skip)
1142 * @data: GstTypeFindData * to free
1148 gst_type_find_data_free (GstTypeFindData * data)
1150 if (data && data->helper.caps)
1151 gst_caps_unref (data->helper.caps);