Blame libs/gst/base/gsttypefindhelper.c

Packit a6ee4b
/* GStreamer
Packit a6ee4b
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
Packit a6ee4b
 * Copyright (C) 2000,2005 Wim Taymans <wim@fluendo.com>
Packit a6ee4b
 * Copyright (C) 2006      Tim-Philipp Müller <tim centricular net>
Packit a6ee4b
 *
Packit a6ee4b
 * gsttypefindhelper.c:
Packit a6ee4b
 *
Packit a6ee4b
 * This library is free software; you can redistribute it and/or
Packit a6ee4b
 * modify it under the terms of the GNU Library General Public
Packit a6ee4b
 * License as published by the Free Software Foundation; either
Packit a6ee4b
 * version 2 of the License, or (at your option) any later version.
Packit a6ee4b
 *
Packit a6ee4b
 * This library is distributed in the hope that it will be useful,
Packit a6ee4b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a6ee4b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a6ee4b
 * Library General Public License for more details.
Packit a6ee4b
 *
Packit a6ee4b
 * You should have received a copy of the GNU Library General Public
Packit a6ee4b
 * License along with this library; if not, write to the
Packit a6ee4b
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit a6ee4b
 * Boston, MA 02110-1301, USA.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * SECTION:gsttypefindhelper
Packit a6ee4b
 * @title: GstTypeFindHelper
Packit a6ee4b
 * @short_description: Utility functions for typefinding
Packit a6ee4b
 *
Packit a6ee4b
 * Utility functions for elements doing typefinding:
Packit a6ee4b
 * gst_type_find_helper() does typefinding in pull mode, while
Packit a6ee4b
 * gst_type_find_helper_for_buffer() is useful for elements needing to do
Packit a6ee4b
 * typefinding in push mode from a chain function.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
#ifdef HAVE_CONFIG_H
Packit a6ee4b
#  include "config.h"
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
#include <stdlib.h>
Packit a6ee4b
#include <string.h>
Packit a6ee4b
Packit a6ee4b
#include "gsttypefindhelper.h"
Packit a6ee4b
Packit a6ee4b
/* ********************** typefinding in pull mode ************************ */
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
helper_find_suggest (gpointer data, guint probability, GstCaps * caps);
Packit a6ee4b
Packit a6ee4b
typedef struct
Packit a6ee4b
{
Packit a6ee4b
  GstBuffer *buffer;
Packit a6ee4b
  GstMapInfo map;
Packit a6ee4b
} GstMappedBuffer;
Packit a6ee4b
Packit a6ee4b
typedef struct
Packit a6ee4b
{
Packit a6ee4b
  GSList *buffers;              /* buffer cache */
Packit a6ee4b
  guint64 size;
Packit a6ee4b
  guint64 last_offset;
Packit a6ee4b
  GstTypeFindHelperGetRangeFunction func;
Packit a6ee4b
  GstTypeFindProbability best_probability;
Packit a6ee4b
  GstCaps *caps;
Packit a6ee4b
  GstTypeFindFactory *factory;  /* for logging */
Packit a6ee4b
  GstObject *obj;               /* for logging */
Packit a6ee4b
  GstObject *parent;
Packit a6ee4b
  GstFlowReturn flow_ret;
Packit a6ee4b
} GstTypeFindHelper;
Packit a6ee4b
Packit a6ee4b
/*
Packit a6ee4b
 * helper_find_peek:
Packit a6ee4b
 * @data: helper data struct
Packit a6ee4b
 * @off: stream offset
Packit a6ee4b
 * @size: block size
Packit a6ee4b
 *
Packit a6ee4b
 * Get data pointer within a stream. Keeps a cache of read buffers (partly
Packit a6ee4b
 * for performance reasons, but mostly because pointers returned by us need
Packit a6ee4b
 * to stay valid until typefinding has finished)
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (nullable): address of the data or %NULL if buffer does not cover
Packit a6ee4b
 * the requested range.
Packit a6ee4b
 */
Packit a6ee4b
static const guint8 *
Packit a6ee4b
helper_find_peek (gpointer data, gint64 offset, guint size)
Packit a6ee4b
{
Packit a6ee4b
  GstTypeFindHelper *helper;
Packit a6ee4b
  GstBuffer *buffer;
Packit a6ee4b
  GSList *insert_pos = NULL;
Packit a6ee4b
  gsize buf_size;
Packit a6ee4b
  guint64 buf_offset;
Packit a6ee4b
  GstMappedBuffer *bmap;
Packit a6ee4b
#if 0
Packit a6ee4b
  GstCaps *caps;
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
  helper = (GstTypeFindHelper *) data;
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT
Packit a6ee4b
      ", %u)", GST_OBJECT_NAME (helper->factory), offset, size);
Packit a6ee4b
Packit a6ee4b
  if (size == 0)
Packit a6ee4b
    return NULL;
Packit a6ee4b
Packit a6ee4b
  if (offset < 0) {
Packit a6ee4b
    if (helper->size == -1 || helper->size < -offset)
Packit a6ee4b
      return NULL;
Packit a6ee4b
Packit a6ee4b
    offset += helper->size;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* see if we have a matching buffer already in our list */
Packit a6ee4b
  if (size > 0 && offset <= helper->last_offset) {
Packit a6ee4b
    GSList *walk;
Packit a6ee4b
Packit a6ee4b
    for (walk = helper->buffers; walk; walk = walk->next) {
Packit a6ee4b
      GstMappedBuffer *bmp = (GstMappedBuffer *) walk->data;
Packit a6ee4b
      GstBuffer *buf = GST_BUFFER_CAST (bmp->buffer);
Packit a6ee4b
Packit a6ee4b
      buf_offset = GST_BUFFER_OFFSET (buf);
Packit a6ee4b
      buf_size = bmp->map.size;
Packit a6ee4b
Packit a6ee4b
      /* buffers are kept sorted by end offset (highest first) in the list, so
Packit a6ee4b
       * at this point we save the current position and stop searching if
Packit a6ee4b
       * we're after the searched end offset */
Packit a6ee4b
      if (buf_offset <= offset) {
Packit a6ee4b
        if ((offset + size) < (buf_offset + buf_size)) {
Packit a6ee4b
          /* must already have been mapped before */
Packit a6ee4b
          return (guint8 *) bmp->map.data + (offset - buf_offset);
Packit a6ee4b
        }
Packit a6ee4b
      } else if (offset + size >= buf_offset + buf_size) {
Packit a6ee4b
        insert_pos = walk;
Packit a6ee4b
        break;
Packit a6ee4b
      }
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  buffer = NULL;
Packit a6ee4b
  /* some typefinders go in 1 byte steps over 1k of data and request
Packit a6ee4b
   * small buffers. It is really inefficient to pull each time, and pulling
Packit a6ee4b
   * a larger chunk is almost free. Trying to pull a larger chunk at the end
Packit a6ee4b
   * of the file is also not a problem here, we'll just get a truncated buffer
Packit a6ee4b
   * in that case (and we'll have to double-check the size we actually get
Packit a6ee4b
   * anyway, see below) */
Packit a6ee4b
  helper->flow_ret =
Packit a6ee4b
      helper->func (helper->obj, helper->parent, offset, MAX (size, 4096),
Packit a6ee4b
      &buffer);
Packit a6ee4b
Packit a6ee4b
  if (helper->flow_ret != GST_FLOW_OK)
Packit a6ee4b
    goto error;
Packit a6ee4b
Packit a6ee4b
#if 0
Packit a6ee4b
  caps = GST_BUFFER_CAPS (buffer);
Packit a6ee4b
Packit a6ee4b
  if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) {
Packit a6ee4b
    GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability",
Packit a6ee4b
        caps);
Packit a6ee4b
Packit a6ee4b
    gst_caps_replace (&helper->caps, caps);
Packit a6ee4b
    helper->best_probability = GST_TYPE_FIND_MAXIMUM;
Packit a6ee4b
Packit a6ee4b
    gst_buffer_unref (buffer);
Packit a6ee4b
    return NULL;
Packit a6ee4b
  }
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
  /* getrange might silently return shortened buffers at the end of a file,
Packit a6ee4b
   * we must, however, always return either the full requested data or %NULL */
Packit a6ee4b
  buf_offset = GST_BUFFER_OFFSET (buffer);
Packit a6ee4b
  buf_size = gst_buffer_get_size (buffer);
Packit a6ee4b
Packit a6ee4b
  if (buf_size < size) {
Packit a6ee4b
    GST_DEBUG ("dropping short buffer of size %" G_GSIZE_FORMAT ","
Packit a6ee4b
        "requested size was %u", buf_size, size);
Packit a6ee4b
    gst_buffer_unref (buffer);
Packit a6ee4b
    return NULL;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (buf_offset != -1 && buf_offset != offset) {
Packit a6ee4b
    GST_DEBUG ("dropping buffer with unexpected offset %" G_GUINT64_FORMAT ", "
Packit a6ee4b
        "expected offset was %" G_GUINT64_FORMAT, buf_offset, offset);
Packit a6ee4b
    gst_buffer_unref (buffer);
Packit a6ee4b
    return NULL;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  bmap = g_slice_new0 (GstMappedBuffer);
Packit a6ee4b
Packit a6ee4b
  if (!gst_buffer_map (buffer, &bmap->map, GST_MAP_READ))
Packit a6ee4b
    goto map_failed;
Packit a6ee4b
Packit a6ee4b
  bmap->buffer = buffer;
Packit a6ee4b
Packit a6ee4b
  if (insert_pos) {
Packit a6ee4b
    helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, bmap);
Packit a6ee4b
  } else {
Packit a6ee4b
    /* if insert_pos is not set, our offset is bigger than the largest offset
Packit a6ee4b
     * we have so far; since we keep the list sorted with highest offsets
Packit a6ee4b
     * first, we need to prepend the buffer to the list */
Packit a6ee4b
    helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
Packit a6ee4b
    helper->buffers = g_slist_prepend (helper->buffers, bmap);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return bmap->map.data;
Packit a6ee4b
Packit a6ee4b
error:
Packit a6ee4b
  {
Packit a6ee4b
    GST_INFO ("typefind function returned: %s",
Packit a6ee4b
        gst_flow_get_name (helper->flow_ret));
Packit a6ee4b
    return NULL;
Packit a6ee4b
  }
Packit a6ee4b
map_failed:
Packit a6ee4b
  {
Packit a6ee4b
    GST_ERROR ("map failed");
Packit a6ee4b
    gst_buffer_unref (buffer);
Packit a6ee4b
    g_slice_free (GstMappedBuffer, bmap);
Packit a6ee4b
    return NULL;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/*
Packit a6ee4b
 * helper_find_suggest:
Packit a6ee4b
 * @data: helper data struct
Packit a6ee4b
 * @probability: probability of the match
Packit a6ee4b
 * @caps: caps of the type
Packit a6ee4b
 *
Packit a6ee4b
 * If given @probability is higher, replace previously store caps.
Packit a6ee4b
 */
Packit a6ee4b
static void
Packit a6ee4b
helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
Packit a6ee4b
{
Packit a6ee4b
  GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (helper->obj,
Packit a6ee4b
      "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
Packit a6ee4b
      GST_OBJECT_NAME (helper->factory), probability, caps);
Packit a6ee4b
Packit a6ee4b
  if (probability > helper->best_probability) {
Packit a6ee4b
    gst_caps_replace (&helper->caps, caps);
Packit a6ee4b
    helper->best_probability = probability;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static guint64
Packit a6ee4b
helper_find_get_length (gpointer data)
Packit a6ee4b
{
Packit a6ee4b
  GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (helper->obj, "'%s' called get_length, returning %"
Packit a6ee4b
      G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
Packit a6ee4b
Packit a6ee4b
  return helper->size;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static GList *
Packit a6ee4b
prioritize_extension (GstObject * obj, GList * type_list,
Packit a6ee4b
    const gchar * extension)
Packit a6ee4b
{
Packit a6ee4b
  gint pos = 0;
Packit a6ee4b
  GList *next, *l;
Packit a6ee4b
Packit a6ee4b
  if (!extension)
Packit a6ee4b
    return type_list;
Packit a6ee4b
Packit a6ee4b
  /* move the typefinders for the extension first in the list. The idea is that
Packit a6ee4b
   * when one of them returns MAX we don't need to search further as there is a
Packit a6ee4b
   * very high chance we got the right type. */
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head", extension);
Packit a6ee4b
Packit a6ee4b
  for (l = type_list; l; l = next) {
Packit a6ee4b
    const gchar *const *ext;
Packit a6ee4b
    GstTypeFindFactory *factory;
Packit a6ee4b
Packit a6ee4b
    next = l->next;
Packit a6ee4b
Packit a6ee4b
    factory = GST_TYPE_FIND_FACTORY (l->data);
Packit a6ee4b
Packit a6ee4b
    ext = gst_type_find_factory_get_extensions (factory);
Packit a6ee4b
    if (ext == NULL)
Packit a6ee4b
      continue;
Packit a6ee4b
Packit a6ee4b
    GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
Packit a6ee4b
        GST_OBJECT_NAME (factory), extension);
Packit a6ee4b
Packit a6ee4b
    while (*ext != NULL) {
Packit a6ee4b
      if (strcmp (*ext, extension) == 0) {
Packit a6ee4b
        /* found extension, move in front */
Packit a6ee4b
        GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
Packit a6ee4b
            extension);
Packit a6ee4b
        /* remove entry from list */
Packit a6ee4b
        type_list = g_list_delete_link (type_list, l);
Packit a6ee4b
        /* insert at the position */
Packit a6ee4b
        type_list = g_list_insert (type_list, factory, pos);
Packit a6ee4b
        /* next element will be inserted after this one */
Packit a6ee4b
        pos++;
Packit a6ee4b
        break;
Packit a6ee4b
      }
Packit a6ee4b
      ++ext;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return type_list;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_type_find_helper_get_range:
Packit a6ee4b
 * @obj: A #GstObject that will be passed as first argument to @func
Packit a6ee4b
 * @parent: (allow-none): the parent of @obj or %NULL
Packit a6ee4b
 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
Packit a6ee4b
 *        be used to access data at random offsets when doing the typefinding
Packit a6ee4b
 * @size: The length in bytes
Packit a6ee4b
 * @extension: (allow-none): extension of the media, or %NULL
Packit a6ee4b
 * @prob: (out) (allow-none): location to store the probability of the found
Packit a6ee4b
 *     caps, or %NULL
Packit a6ee4b
 *
Packit a6ee4b
 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
Packit a6ee4b
 * however, this function will use the specified function @func to obtain the
Packit a6ee4b
 * data needed by the typefind functions, rather than operating on a given
Packit a6ee4b
 * source pad. This is useful mostly for elements like tag demuxers which
Packit a6ee4b
 * strip off data at the beginning and/or end of a file and want to typefind
Packit a6ee4b
 * the stripped data stream before adding their own source pad (the specified
Packit a6ee4b
 * callback can then call the upstream peer pad with offsets adjusted for the
Packit a6ee4b
 * tag size, for example).
Packit a6ee4b
 *
Packit a6ee4b
 * When @extension is not %NULL, this function will first try the typefind
Packit a6ee4b
 * functions for the given extension, which might speed up the typefinding
Packit a6ee4b
 * in many cases.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_caps_unref
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
Packit a6ee4b
 *     stream.  Returns %NULL if no #GstCaps matches the data stream.
Packit a6ee4b
 */
Packit a6ee4b
GstCaps *
Packit a6ee4b
gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
Packit a6ee4b
    GstTypeFindHelperGetRangeFunction func, guint64 size,
Packit a6ee4b
    const gchar * extension, GstTypeFindProbability * prob)
Packit a6ee4b
{
Packit a6ee4b
  GstCaps *caps = NULL;
Packit a6ee4b
Packit a6ee4b
  gst_type_find_helper_get_range_full (obj, parent, func, size, extension,
Packit a6ee4b
      &caps, prob);
Packit a6ee4b
Packit a6ee4b
  return caps;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_type_find_helper_get_range_full:
Packit a6ee4b
 * @obj: A #GstObject that will be passed as first argument to @func
Packit a6ee4b
 * @parent: (allow-none): the parent of @obj or %NULL
Packit a6ee4b
 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
Packit a6ee4b
 *        be used to access data at random offsets when doing the typefinding
Packit a6ee4b
 * @size: The length in bytes
Packit a6ee4b
 * @extension: (allow-none): extension of the media, or %NULL
Packit a6ee4b
 * @caps: (out) (transfer full): returned caps
Packit a6ee4b
 * @prob: (out) (allow-none): location to store the probability of the found
Packit a6ee4b
 *     caps, or %NULL
Packit a6ee4b
 *
Packit a6ee4b
 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
Packit a6ee4b
 * however, this function will use the specified function @func to obtain the
Packit a6ee4b
 * data needed by the typefind functions, rather than operating on a given
Packit a6ee4b
 * source pad. This is useful mostly for elements like tag demuxers which
Packit a6ee4b
 * strip off data at the beginning and/or end of a file and want to typefind
Packit a6ee4b
 * the stripped data stream before adding their own source pad (the specified
Packit a6ee4b
 * callback can then call the upstream peer pad with offsets adjusted for the
Packit a6ee4b
 * tag size, for example).
Packit a6ee4b
 *
Packit a6ee4b
 * When @extension is not %NULL, this function will first try the typefind
Packit a6ee4b
 * functions for the given extension, which might speed up the typefinding
Packit a6ee4b
 * in many cases.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: the last %GstFlowReturn from pulling a buffer or %GST_FLOW_OK if
Packit a6ee4b
 *          typefinding was successful.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.14.3
Packit a6ee4b
 */
Packit a6ee4b
GstFlowReturn
Packit a6ee4b
gst_type_find_helper_get_range_full (GstObject * obj, GstObject * parent,
Packit a6ee4b
    GstTypeFindHelperGetRangeFunction func, guint64 size,
Packit a6ee4b
    const gchar * extension, GstCaps ** caps, GstTypeFindProbability * prob)
Packit a6ee4b
{
Packit a6ee4b
  GstTypeFindHelper helper;
Packit a6ee4b
  GstTypeFind find;
Packit a6ee4b
  GSList *walk;
Packit a6ee4b
  GList *l, *type_list;
Packit a6ee4b
  GstCaps *result = NULL;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_OBJECT (obj), GST_FLOW_ERROR);
Packit a6ee4b
  g_return_val_if_fail (func != NULL, GST_FLOW_ERROR);
Packit a6ee4b
  g_return_val_if_fail (caps != NULL, GST_FLOW_ERROR);
Packit a6ee4b
Packit a6ee4b
  *caps = NULL;
Packit a6ee4b
Packit a6ee4b
  helper.buffers = NULL;
Packit a6ee4b
  helper.size = size;
Packit a6ee4b
  helper.last_offset = 0;
Packit a6ee4b
  helper.func = func;
Packit a6ee4b
  helper.best_probability = GST_TYPE_FIND_NONE;
Packit a6ee4b
  helper.caps = NULL;
Packit a6ee4b
  helper.obj = obj;
Packit a6ee4b
  helper.parent = parent;
Packit a6ee4b
  helper.flow_ret = GST_FLOW_OK;
Packit a6ee4b
Packit a6ee4b
  find.data = &helper;
Packit a6ee4b
  find.peek = helper_find_peek;
Packit a6ee4b
  find.suggest = helper_find_suggest;
Packit a6ee4b
Packit a6ee4b
  if (size == 0 || size == (guint64) - 1) {
Packit a6ee4b
    find.get_length = NULL;
Packit a6ee4b
  } else {
Packit a6ee4b
    find.get_length = helper_find_get_length;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  type_list = gst_type_find_factory_get_list ();
Packit a6ee4b
  type_list = prioritize_extension (obj, type_list, extension);
Packit a6ee4b
Packit a6ee4b
  for (l = type_list; l; l = l->next) {
Packit a6ee4b
    helper.factory = GST_TYPE_FIND_FACTORY (l->data);
Packit a6ee4b
    gst_type_find_factory_call_function (helper.factory, &find);
Packit a6ee4b
    if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM) {
Packit a6ee4b
      /* Any other flow return can be ignored here, we found
Packit a6ee4b
       * something before any error with highest probability */
Packit a6ee4b
      helper.flow_ret = GST_FLOW_OK;
Packit a6ee4b
      break;
Packit a6ee4b
    } else if (helper.flow_ret != GST_FLOW_OK
Packit a6ee4b
        && helper.flow_ret != GST_FLOW_EOS) {
Packit a6ee4b
      /* We had less than maximum probability and an error, don't return
Packit a6ee4b
       * any caps as they might be with a lower probability than what
Packit a6ee4b
       * we would've gotten when continuing if there was no error */
Packit a6ee4b
      gst_caps_replace (&helper.caps, NULL);
Packit a6ee4b
      break;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
  gst_plugin_feature_list_free (type_list);
Packit a6ee4b
Packit a6ee4b
  for (walk = helper.buffers; walk; walk = walk->next) {
Packit a6ee4b
    GstMappedBuffer *bmap = (GstMappedBuffer *) walk->data;
Packit a6ee4b
Packit a6ee4b
    gst_buffer_unmap (bmap->buffer, &bmap->map);
Packit a6ee4b
    gst_buffer_unref (bmap->buffer);
Packit a6ee4b
    g_slice_free (GstMappedBuffer, bmap);
Packit a6ee4b
  }
Packit a6ee4b
  g_slist_free (helper.buffers);
Packit a6ee4b
Packit a6ee4b
  if (helper.best_probability > 0)
Packit a6ee4b
    result = helper.caps;
Packit a6ee4b
Packit a6ee4b
  if (prob)
Packit a6ee4b
    *prob = helper.best_probability;
Packit a6ee4b
Packit a6ee4b
  *caps = result;
Packit a6ee4b
  if (helper.flow_ret == GST_FLOW_EOS) {
Packit a6ee4b
    /* Some typefinder might've tried to read too much, if we
Packit a6ee4b
     * didn't get any meaningful caps because of that this is
Packit a6ee4b
     * just a normal error */
Packit a6ee4b
    helper.flow_ret = GST_FLOW_ERROR;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
Packit a6ee4b
      result, (guint) helper.best_probability);
Packit a6ee4b
Packit a6ee4b
  return helper.flow_ret;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_type_find_helper:
Packit a6ee4b
 * @src: A source #GstPad
Packit a6ee4b
 * @size: The length in bytes
Packit a6ee4b
 *
Packit a6ee4b
 * Tries to find what type of data is flowing from the given source #GstPad.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_caps_unref
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
Packit a6ee4b
 *     stream.  Returns %NULL if no #GstCaps matches the data stream.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
GstCaps *
Packit a6ee4b
gst_type_find_helper (GstPad * src, guint64 size)
Packit a6ee4b
{
Packit a6ee4b
  GstTypeFindHelperGetRangeFunction func;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
Packit a6ee4b
  g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
Packit a6ee4b
Packit a6ee4b
  func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
Packit a6ee4b
Packit a6ee4b
  return gst_type_find_helper_get_range (GST_OBJECT (src),
Packit a6ee4b
      GST_OBJECT_PARENT (src), func, size, NULL, NULL);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/* ********************** typefinding for buffers ************************* */
Packit a6ee4b
Packit a6ee4b
typedef struct
Packit a6ee4b
{
Packit a6ee4b
  const guint8 *data;           /* buffer data */
Packit a6ee4b
  gsize size;
Packit a6ee4b
  GstTypeFindProbability best_probability;
Packit a6ee4b
  GstCaps *caps;
Packit a6ee4b
  GstTypeFindFactory *factory;  /* for logging */
Packit a6ee4b
  GstObject *obj;               /* for logging */
Packit a6ee4b
} GstTypeFindBufHelper;
Packit a6ee4b
Packit a6ee4b
/*
Packit a6ee4b
 * buf_helper_find_peek:
Packit a6ee4b
 * @data: helper data struct
Packit a6ee4b
 * @off: stream offset
Packit a6ee4b
 * @size: block size
Packit a6ee4b
 *
Packit a6ee4b
 * Get data pointer within a buffer.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (nullable): address inside the buffer or %NULL if buffer does not
Packit a6ee4b
 * cover the requested range.
Packit a6ee4b
 */
Packit a6ee4b
static const guint8 *
Packit a6ee4b
buf_helper_find_peek (gpointer data, gint64 off, guint size)
Packit a6ee4b
{
Packit a6ee4b
  GstTypeFindBufHelper *helper;
Packit a6ee4b
Packit a6ee4b
  helper = (GstTypeFindBufHelper *) data;
Packit a6ee4b
  GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
Packit a6ee4b
      GST_OBJECT_NAME (helper->factory), off, size);
Packit a6ee4b
Packit a6ee4b
  if (size == 0)
Packit a6ee4b
    return NULL;
Packit a6ee4b
Packit a6ee4b
  if (off < 0) {
Packit a6ee4b
    GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
Packit a6ee4b
        GST_OBJECT_NAME (helper->factory));
Packit a6ee4b
    return NULL;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* If we request beyond the available size, we're sure we can't return
Packit a6ee4b
   * anything regardless of the requested offset */
Packit a6ee4b
  if (size > helper->size)
Packit a6ee4b
    return NULL;
Packit a6ee4b
Packit a6ee4b
  /* Only return data if there's enough room left for the given offset.
Packit a6ee4b
   * This is the same as "if (off + size <= helper->size)" except that
Packit a6ee4b
   * it doesn't exceed type limits */
Packit a6ee4b
  if (off <= helper->size - size)
Packit a6ee4b
    return helper->data + off;
Packit a6ee4b
Packit a6ee4b
  return NULL;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/*
Packit a6ee4b
 * buf_helper_find_suggest:
Packit a6ee4b
 * @data: helper data struct
Packit a6ee4b
 * @probability: probability of the match
Packit a6ee4b
 * @caps: caps of the type
Packit a6ee4b
 *
Packit a6ee4b
 * If given @probability is higher, replace previously store caps.
Packit a6ee4b
 */
Packit a6ee4b
static void
Packit a6ee4b
buf_helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
Packit a6ee4b
{
Packit a6ee4b
  GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (helper->obj,
Packit a6ee4b
      "'%s' called suggest (%u, %" GST_PTR_FORMAT ")",
Packit a6ee4b
      GST_OBJECT_NAME (helper->factory), probability, caps);
Packit a6ee4b
Packit a6ee4b
  /* Note: not >= as we call typefinders in order of rank, highest first */
Packit a6ee4b
  if (probability > helper->best_probability) {
Packit a6ee4b
    gst_caps_replace (&helper->caps, caps);
Packit a6ee4b
    helper->best_probability = probability;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_type_find_helper_for_data:
Packit a6ee4b
 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
Packit a6ee4b
 * @data: (transfer none) (array length=size): * a pointer with data to typefind
Packit a6ee4b
 * @size: the size of @data
Packit a6ee4b
 * @prob: (out) (allow-none): location to store the probability of the found
Packit a6ee4b
 *     caps, or %NULL
Packit a6ee4b
 *
Packit a6ee4b
 * Tries to find what type of data is contained in the given @data, the
Packit a6ee4b
 * assumption being that the data represents the beginning of the stream or
Packit a6ee4b
 * file.
Packit a6ee4b
 *
Packit a6ee4b
 * All available typefinders will be called on the data in order of rank. If
Packit a6ee4b
 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
Packit a6ee4b
 * typefinding is stopped immediately and the found caps will be returned
Packit a6ee4b
 * right away. Otherwise, all available typefind functions will the tried,
Packit a6ee4b
 * and the caps with the highest probability will be returned, or %NULL if
Packit a6ee4b
 * the content of @data could not be identified.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_caps_unref
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
Packit a6ee4b
 *     or %NULL if no type could be found. The caller should free the caps
Packit a6ee4b
 *     returned with gst_caps_unref().
Packit a6ee4b
 */
Packit a6ee4b
GstCaps *
Packit a6ee4b
gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
Packit a6ee4b
    GstTypeFindProbability * prob)
Packit a6ee4b
{
Packit a6ee4b
  return gst_type_find_helper_for_data_with_extension (obj, data, size, NULL,
Packit a6ee4b
      prob);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_type_find_helper_for_data_with_extension:
Packit a6ee4b
 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
Packit a6ee4b
 * @data: (transfer none) (array length=size): * a pointer with data to typefind
Packit a6ee4b
 * @size: the size of @data
Packit a6ee4b
 * @extension: (allow-none): extension of the media, or %NULL
Packit a6ee4b
 * @prob: (out) (allow-none): location to store the probability of the found
Packit a6ee4b
 *     caps, or %NULL
Packit a6ee4b
 *
Packit a6ee4b
 * Tries to find what type of data is contained in the given @data, the
Packit a6ee4b
 * assumption being that the data represents the beginning of the stream or
Packit a6ee4b
 * file.
Packit a6ee4b
 *
Packit a6ee4b
 * All available typefinders will be called on the data in order of rank. If
Packit a6ee4b
 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
Packit a6ee4b
 * typefinding is stopped immediately and the found caps will be returned
Packit a6ee4b
 * right away. Otherwise, all available typefind functions will the tried,
Packit a6ee4b
 * and the caps with the highest probability will be returned, or %NULL if
Packit a6ee4b
 * the content of @data could not be identified.
Packit a6ee4b
 *
Packit a6ee4b
 * When @extension is not %NULL, this function will first try the typefind
Packit a6ee4b
 * functions for the given extension, which might speed up the typefinding
Packit a6ee4b
 * in many cases.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_caps_unref
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
Packit a6ee4b
 *     or %NULL if no type could be found. The caller should free the caps
Packit a6ee4b
 *     returned with gst_caps_unref().
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.16
Packit a6ee4b
 *
Packit a6ee4b
 */
Packit a6ee4b
GstCaps *
Packit a6ee4b
gst_type_find_helper_for_data_with_extension (GstObject * obj,
Packit a6ee4b
    const guint8 * data, gsize size, const gchar * extension,
Packit a6ee4b
    GstTypeFindProbability * prob)
Packit a6ee4b
{
Packit a6ee4b
  GstTypeFindBufHelper helper;
Packit a6ee4b
  GstTypeFind find;
Packit a6ee4b
  GList *l, *type_list;
Packit a6ee4b
  GstCaps *result = NULL;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (data != NULL, NULL);
Packit a6ee4b
Packit a6ee4b
  helper.data = data;
Packit a6ee4b
  helper.size = size;
Packit a6ee4b
  helper.best_probability = GST_TYPE_FIND_NONE;
Packit a6ee4b
  helper.caps = NULL;
Packit a6ee4b
  helper.obj = obj;
Packit a6ee4b
Packit a6ee4b
  if (helper.data == NULL || helper.size == 0)
Packit a6ee4b
    return NULL;
Packit a6ee4b
Packit a6ee4b
  find.data = &helper;
Packit a6ee4b
  find.peek = buf_helper_find_peek;
Packit a6ee4b
  find.suggest = buf_helper_find_suggest;
Packit a6ee4b
  find.get_length = NULL;
Packit a6ee4b
Packit a6ee4b
  type_list = gst_type_find_factory_get_list ();
Packit a6ee4b
  type_list = prioritize_extension (obj, type_list, extension);
Packit a6ee4b
Packit a6ee4b
  for (l = type_list; l; l = l->next) {
Packit a6ee4b
    helper.factory = GST_TYPE_FIND_FACTORY (l->data);
Packit a6ee4b
    gst_type_find_factory_call_function (helper.factory, &find);
Packit a6ee4b
    if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
Packit a6ee4b
      break;
Packit a6ee4b
  }
Packit a6ee4b
  gst_plugin_feature_list_free (type_list);
Packit a6ee4b
Packit a6ee4b
  if (helper.best_probability > 0)
Packit a6ee4b
    result = helper.caps;
Packit a6ee4b
Packit a6ee4b
  if (prob)
Packit a6ee4b
    *prob = helper.best_probability;
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
Packit a6ee4b
      result, (guint) helper.best_probability);
Packit a6ee4b
Packit a6ee4b
  return result;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_type_find_helper_for_buffer:
Packit a6ee4b
 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
Packit a6ee4b
 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
Packit a6ee4b
 * @prob: (out) (allow-none): location to store the probability of the found
Packit a6ee4b
 *     caps, or %NULL
Packit a6ee4b
 *
Packit a6ee4b
 * Tries to find what type of data is contained in the given #GstBuffer, the
Packit a6ee4b
 * assumption being that the buffer represents the beginning of the stream or
Packit a6ee4b
 * file.
Packit a6ee4b
 *
Packit a6ee4b
 * All available typefinders will be called on the data in order of rank. If
Packit a6ee4b
 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
Packit a6ee4b
 * typefinding is stopped immediately and the found caps will be returned
Packit a6ee4b
 * right away. Otherwise, all available typefind functions will the tried,
Packit a6ee4b
 * and the caps with the highest probability will be returned, or %NULL if
Packit a6ee4b
 * the content of the buffer could not be identified.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_caps_unref
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
Packit a6ee4b
 *     or %NULL if no type could be found. The caller should free the caps
Packit a6ee4b
 *     returned with gst_caps_unref().
Packit a6ee4b
 */
Packit a6ee4b
GstCaps *
Packit a6ee4b
gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
Packit a6ee4b
    GstTypeFindProbability * prob)
Packit a6ee4b
{
Packit a6ee4b
  return gst_type_find_helper_for_buffer_with_extension (obj, buf, NULL, prob);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_type_find_helper_for_buffer_with_extension:
Packit a6ee4b
 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
Packit a6ee4b
 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
Packit a6ee4b
 * @extension: (allow-none): extension of the media, or %NULL
Packit a6ee4b
 * @prob: (out) (allow-none): location to store the probability of the found
Packit a6ee4b
 *     caps, or %NULL
Packit a6ee4b
 *
Packit a6ee4b
 * Tries to find what type of data is contained in the given #GstBuffer, the
Packit a6ee4b
 * assumption being that the buffer represents the beginning of the stream or
Packit a6ee4b
 * file.
Packit a6ee4b
 *
Packit a6ee4b
 * All available typefinders will be called on the data in order of rank. If
Packit a6ee4b
 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
Packit a6ee4b
 * typefinding is stopped immediately and the found caps will be returned
Packit a6ee4b
 * right away. Otherwise, all available typefind functions will the tried,
Packit a6ee4b
 * and the caps with the highest probability will be returned, or %NULL if
Packit a6ee4b
 * the content of the buffer could not be identified.
Packit a6ee4b
 *
Packit a6ee4b
 * When @extension is not %NULL, this function will first try the typefind
Packit a6ee4b
 * functions for the given extension, which might speed up the typefinding
Packit a6ee4b
 * in many cases.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_caps_unref
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
Packit a6ee4b
 *     or %NULL if no type could be found. The caller should free the caps
Packit a6ee4b
 *     returned with gst_caps_unref().
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.16
Packit a6ee4b
 *
Packit a6ee4b
 */
Packit a6ee4b
GstCaps *
Packit a6ee4b
gst_type_find_helper_for_buffer_with_extension (GstObject * obj,
Packit a6ee4b
    GstBuffer * buf, const gchar * extension, GstTypeFindProbability * prob)
Packit a6ee4b
{
Packit a6ee4b
  GstCaps *result;
Packit a6ee4b
  GstMapInfo info;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (buf != NULL, NULL);
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
Packit a6ee4b
  g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
Packit a6ee4b
      GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
Packit a6ee4b
Packit a6ee4b
  if (!gst_buffer_map (buf, &info, GST_MAP_READ))
Packit a6ee4b
    return NULL;
Packit a6ee4b
  result =
Packit a6ee4b
      gst_type_find_helper_for_data_with_extension (obj, info.data, info.size,
Packit a6ee4b
      extension, prob);
Packit a6ee4b
  gst_buffer_unmap (buf, &info;;
Packit a6ee4b
Packit a6ee4b
  return result;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_type_find_helper_for_extension:
Packit a6ee4b
 * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
Packit a6ee4b
 * @extension: an extension
Packit a6ee4b
 *
Packit a6ee4b
 * Tries to find the best #GstCaps associated with @extension.
Packit a6ee4b
 *
Packit a6ee4b
 * All available typefinders will be checked against the extension in order
Packit a6ee4b
 * of rank. The caps of the first typefinder that can handle @extension will be
Packit a6ee4b
 * returned.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_caps_unref
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstCaps corresponding to
Packit a6ee4b
 *     @extension, or %NULL if no type could be found. The caller should free
Packit a6ee4b
 *     the caps returned with gst_caps_unref().
Packit a6ee4b
 */
Packit a6ee4b
GstCaps *
Packit a6ee4b
gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
Packit a6ee4b
{
Packit a6ee4b
  GList *l, *type_list;
Packit a6ee4b
  GstCaps *result = NULL;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (extension != NULL, NULL);
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
Packit a6ee4b
Packit a6ee4b
  type_list = gst_type_find_factory_get_list ();
Packit a6ee4b
Packit a6ee4b
  for (l = type_list; l; l = g_list_next (l)) {
Packit a6ee4b
    GstTypeFindFactory *factory;
Packit a6ee4b
    const gchar *const *ext;
Packit a6ee4b
Packit a6ee4b
    factory = GST_TYPE_FIND_FACTORY (l->data);
Packit a6ee4b
Packit a6ee4b
    /* we only want to check those factories without a function */
Packit a6ee4b
    if (gst_type_find_factory_has_function (factory))
Packit a6ee4b
      continue;
Packit a6ee4b
Packit a6ee4b
    /* get the extension that this typefind factory can handle */
Packit a6ee4b
    ext = gst_type_find_factory_get_extensions (factory);
Packit a6ee4b
    if (ext == NULL)
Packit a6ee4b
      continue;
Packit a6ee4b
Packit a6ee4b
    /* there are extension, see if one of them matches the requested
Packit a6ee4b
     * extension */
Packit a6ee4b
    while (*ext != NULL) {
Packit a6ee4b
      if (strcmp (*ext, extension) == 0) {
Packit a6ee4b
        /* we found a matching extension, take the caps */
Packit a6ee4b
        if ((result = gst_type_find_factory_get_caps (factory))) {
Packit a6ee4b
          gst_caps_ref (result);
Packit a6ee4b
          goto done;
Packit a6ee4b
        }
Packit a6ee4b
      }
Packit a6ee4b
      ++ext;
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
done:
Packit a6ee4b
  gst_plugin_feature_list_free (type_list);
Packit a6ee4b
Packit a6ee4b
  GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);
Packit a6ee4b
Packit a6ee4b
  return result;
Packit a6ee4b
}