buffer: fix resize function some more
[platform/upstream/gstreamer.git] / gst / gsttypefind.c
index c5d9d7b..1f0b16d 100644 (file)
@@ -1,8 +1,7 @@
 /* GStreamer
- * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
- *                    2000 Wim Taymans <wtay@chello.be>
+ * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
  *
- * gsttypefind.c: 
+ * gsttypefind.c: typefinding subsystem
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * Boston, MA 02111-1307, USA.
  */
 
+/**
+ * SECTION:gsttypefind
+ * @short_description: Stream type detection
+ *
+ * The following functions allow you to detect the media type of an unknown
+ * stream.
+ *
+ * Last reviewed on 2005-11-09 (0.9.4)
+ */
 
 #include "gst_private.h"
-#include "gsttype.h"
-#include "gstlog.h"
+#include "gstinfo.h"
 #include "gsttypefind.h"
+#include "gstregistry.h"
+#include "gsttypefindfactory.h"
 
-#define DEFAULT_MAX_BUFFERS    1
-
-GstElementDetails gst_type_find_details = {
-  "TypeFind",
-  "Generic",
-  "LGPL",
-  "Finds the media type of a stream",
-  VERSION,
-  "Erik Walthinsen <omega@cse.ogi.edu>,"
-  "Wim Taymans <wim.taymans@chello.be>",
-  "(C) 1999",
-};
-
-
-/* TypeFind signals and args */
-enum {
-  HAVE_TYPE,
-  LAST_SIGNAL
-};
-
-enum {
-  ARG_0,
-  ARG_CAPS,
-  ARG_MAX_BUFFERS,
-};
-
-
-static void    gst_type_find_class_init        (GstTypeFindClass *klass);
-static void    gst_type_find_init              (GstTypeFind *typefind);
-
-static void    gst_type_find_set_property      (GObject *object, guint prop_id,
-                                                const GValue *value, 
-                                                GParamSpec *pspec);
-static void    gst_type_find_get_property      (GObject *object, guint prop_id,
-                                                GValue *value, 
-                                                GParamSpec *pspec);
-
-static void    gst_type_find_chain             (GstPad *pad, GstBuffer *buf);
-static GstElementStateReturn
-               gst_type_find_change_state      (GstElement *element);
-
-static GstElementClass *parent_class = NULL;
-static guint gst_type_find_signals[LAST_SIGNAL] = { 0 };
+GST_DEBUG_CATEGORY_EXTERN (type_find_debug);
+#define GST_CAT_DEFAULT type_find_debug
 
 GType
 gst_type_find_get_type (void)
 {
   static GType typefind_type = 0;
 
-  if (!typefind_type) {
-    static const GTypeInfo typefind_info = {
-      sizeof(GstTypeFindClass),
-      NULL,
-      NULL,
-      (GClassInitFunc)gst_type_find_class_init,
-      NULL,
-      NULL,
-      sizeof(GstTypeFind),
-      0,
-      (GInstanceInitFunc)gst_type_find_init,
-      NULL
-    };
-    typefind_type = g_type_register_static (GST_TYPE_ELEMENT, "GstTypeFind", &typefind_info, 0);
+  if (G_UNLIKELY (typefind_type == 0)) {
+    typefind_type = g_pointer_type_register_static ("GstTypeFind");
   }
   return typefind_type;
 }
 
-static void
-gst_type_find_class_init (GstTypeFindClass *klass)
+/**
+ * gst_type_find_register:
+ * @plugin: A #GstPlugin, or NULL for a static typefind function (note that
+ *    passing NULL only works in GStreamer 0.10.16 and later)
+ * @name: The name for registering
+ * @rank: The rank (or importance) of this typefind function
+ * @func: The #GstTypeFindFunction to use
+ * @extensions: (transfer none) (array zero-terminated=1) (element-type utf8):
+ *     Optional extensions that could belong to this type
+ * @possible_caps: Optionally the caps that could be returned when typefinding
+ *                 succeeds
+ * @data: Optional user data. This user data must be available until the plugin
+ *        is unloaded.
+ * @data_notify: a #GDestroyNotify that will be called on @data when the plugin
+ *        is unloaded.
+ *
+ * Registers a new typefind function to be used for typefinding. After
+ * registering this function will be available for typefinding.
+ * This function is typically called during an element's plugin initialization.
+ *
+ * Returns: TRUE on success, FALSE otherwise
+ */
+gboolean
+gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
+    GstTypeFindFunction func, gchar ** extensions,
+    const GstCaps * possible_caps, gpointer data, GDestroyNotify data_notify)
 {
-  GObjectClass *gobject_class;
-  GstElementClass *gstelement_class;
-
-  gobject_class = (GObjectClass*)klass;
-  gstelement_class = (GstElementClass*)klass;
-
-  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
-
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CAPS,
-    g_param_spec_pointer ("caps", "Caps", "Found capabilities", G_PARAM_READABLE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_BUFFERS,
-    g_param_spec_int ("max_buffers", 
-                     "Max Buffers", 
-                     "Maximal amount of buffers before giving en error (0 == unlimited)", 
-                     0, G_MAXINT, DEFAULT_MAX_BUFFERS, G_PARAM_READWRITE));
-
-  gst_type_find_signals[HAVE_TYPE] =
-      g_signal_new ("have_type", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
-                     G_STRUCT_OFFSET (GstTypeFindClass, have_type), NULL, NULL,
-                     g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
-                     G_TYPE_POINTER);
+  GstTypeFindFactory *factory;
+
+  g_return_val_if_fail (name != NULL, FALSE);
+
+  GST_INFO ("registering typefind function for %s", name);
+
+  factory = g_object_newv (GST_TYPE_TYPE_FIND_FACTORY, 0, NULL);
+  GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
+  g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
+
+  gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
+  gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
+
+  if (factory->extensions)
+    g_strfreev (factory->extensions);
+  factory->extensions = g_strdupv (extensions);
+
+  gst_caps_replace (&factory->caps, (GstCaps *) possible_caps);
+  factory->function = func;
+  factory->user_data = data;
+  factory->user_data_notify = data_notify;
+  if (plugin && plugin->desc.name) {
+    GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
+    GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
+    g_object_add_weak_pointer ((GObject *) plugin,
+        (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
+  } else {
+    GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
+    GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
+  }
+  GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
 
-  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_type_find_set_property);
-  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_type_find_get_property);
+  gst_registry_add_feature (gst_registry_get_default (),
+      GST_PLUGIN_FEATURE_CAST (factory));
 
-  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_type_find_change_state);
+  return TRUE;
 }
 
-static void
-gst_type_find_init (GstTypeFind *typefind)
-{
-  typefind->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
-  gst_element_add_pad (GST_ELEMENT (typefind), typefind->sinkpad);
-  gst_pad_set_chain_function (typefind->sinkpad, gst_type_find_chain);
+/*** typefind function interface **********************************************/
 
-  typefind->num_buffer = 0;
-  typefind->max_buffers = DEFAULT_MAX_BUFFERS;
-  typefind->caps = NULL;
-}
-
-static void
-gst_type_find_set_property (GObject *object, guint prop_id, 
-                           const GValue *value, GParamSpec *pspec)
+/**
+ * gst_type_find_peek:
+ * @find: The #GstTypeFind object the function was called with
+ * @offset: The offset
+ * @size: The number of bytes to return
+ *
+ * Returns the @size bytes of the stream to identify beginning at offset. If
+ * offset is a positive number, the offset is relative to the beginning of the
+ * stream, if offset is a negative number the offset is relative to the end of
+ * the stream. The returned memory is valid until the typefinding function
+ * returns and must not be freed.
+ *
+ * Returns: (transfer none) (array length=size): the requested data, or NULL
+ *     if that data is not available.
+ */
+const guint8 *
+gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
 {
-  GstTypeFind *typefind;
-
-  g_return_if_fail (GST_IS_TYPE_FIND (object));
+  g_return_val_if_fail (find->peek != NULL, NULL);
 
-  typefind = GST_TYPE_FIND (object);
-
-  switch (prop_id) {
-    case ARG_MAX_BUFFERS:
-      typefind->max_buffers = g_value_get_int (value);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-  }
+  return find->peek (find->data, offset, size);
 }
 
-static void
-gst_type_find_get_property (GObject *object, guint prop_id, 
-                           GValue *value, GParamSpec *pspec)
+/**
+ * gst_type_find_suggest:
+ * @find: The #GstTypeFind object the function was called with
+ * @probability: The probability in percent that the suggestion is right
+ * @caps: The fixed #GstCaps to suggest
+ *
+ * If a #GstTypeFindFunction calls this function it suggests the caps with the
+ * given probability. A #GstTypeFindFunction may supply different suggestions
+ * in one call.
+ * It is up to the caller of the #GstTypeFindFunction to interpret these values.
+ */
+void
+gst_type_find_suggest (GstTypeFind * find, guint probability,
+    const GstCaps * caps)
 {
-  GstTypeFind *typefind;
+  g_return_if_fail (find->suggest != NULL);
+  g_return_if_fail (probability <= 100);
+  g_return_if_fail (caps != NULL);
+  g_return_if_fail (gst_caps_is_fixed (caps));
 
-  g_return_if_fail (GST_IS_TYPE_FIND (object));
-
-  typefind = GST_TYPE_FIND (object);
-
-  switch (prop_id) {
-    case ARG_CAPS:
-      g_value_set_pointer (value, typefind->caps);
-      break;
-    case ARG_MAX_BUFFERS:
-      g_value_set_int (value, typefind->max_buffers);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-  }
+  find->suggest (find->data, probability, caps);
 }
 
-static void
-gst_type_find_chain (GstPad *pad, GstBuffer *buf)
+/**
+ * gst_type_find_suggest_simple:
+ * @find: The #GstTypeFind object the function was called with
+ * @probability: The probability in percent that the suggestion is right
+ * @media_type: the media type of the suggested caps
+ * @fieldname: first field of the suggested caps, or NULL
+ * @...: additional arguments to the suggested caps in the same format as the
+ *     arguments passed to gst_structure_new() (ie. triplets of field name,
+ *     field GType and field value)
+ *
+ * If a #GstTypeFindFunction calls this function it suggests the caps with the
+ * given probability. A #GstTypeFindFunction may supply different suggestions
+ * in one call. It is up to the caller of the #GstTypeFindFunction to interpret
+ * these values.
+ *
+ * This function is similar to gst_type_find_suggest(), only that instead of
+ * passing a #GstCaps argument you can create the caps on the fly in the same
+ * way as you can with gst_caps_new_simple().
+ *
+ * Make sure you terminate the list of arguments with a NULL argument and that
+ * the values passed have the correct type (in terms of width in bytes when
+ * passed to the vararg function - this applies particularly to gdouble and
+ * guint64 arguments).
+ *
+ * Since: 0.10.20
+ */
+void
+gst_type_find_suggest_simple (GstTypeFind * find, guint probability,
+    const char *media_type, const char *fieldname, ...)
 {
-  GstTypeFind *typefind;
-  const GList *type_list;
-  GstType *type;
-
-  g_return_if_fail (GST_IS_PAD (pad));
-
-  typefind = GST_TYPE_FIND (GST_OBJECT_PARENT (pad));
+  GstStructure *structure;
+  va_list var_args;
+  GstCaps *caps;
 
-  GST_DEBUG (0,"got buffer of %d bytes in '%s'",
-        GST_BUFFER_SIZE (buf), GST_OBJECT_NAME (typefind));
+  g_return_if_fail (find->suggest != NULL);
+  g_return_if_fail (probability <= 100);
+  g_return_if_fail (media_type != NULL);
 
-  type_list = gst_type_get_list ();
+  caps = gst_caps_new_empty ();
 
-  while (type_list) {
-    GSList *factories;
-    type = (GstType *) type_list->data;
+  va_start (var_args, fieldname);
+  structure = gst_structure_new_valist (media_type, fieldname, var_args);
+  va_end (var_args);
 
-    factories = type->factories;
+  gst_caps_append_structure (caps, structure);
+  g_return_if_fail (gst_caps_is_fixed (caps));
 
-    while (factories) {
-      GstTypeFactory *factory = GST_TYPE_FACTORY (factories->data);
-      GstTypeFindFunc typefindfunc = (GstTypeFindFunc)factory->typefindfunc;
-      GstCaps *caps;
-
-      GST_DEBUG (GST_CAT_TYPES, "try type (%p) :%d \"%s\" %p", 
-                factory, type->id, type->mime, typefindfunc);
-      if (typefindfunc && (caps = typefindfunc (buf, factory))) {
-        GST_DEBUG (GST_CAT_TYPES, "found type: %d \"%s\" \"%s\"", 
-                  caps->id, type->mime, gst_caps_get_name (caps));
-       typefind->caps = caps;
-
-       if (gst_pad_try_set_caps (pad, caps) <= 0) {
-          g_warning ("typefind: found type but peer didn't accept it");
-       }
-
-       {
-         gst_object_ref (GST_OBJECT (typefind));
-          g_signal_emit (G_OBJECT (typefind), gst_type_find_signals[HAVE_TYPE], 0,
-                             typefind->caps);
-         gst_object_unref (GST_OBJECT (typefind));
-          goto end;
-       }
-      }
-      factories = g_slist_next (factories);
-    }
-    type_list = g_list_next (type_list);
-  }
-
-  typefind->num_buffer++;
-
-end:
-  gst_buffer_unref (buf);
-
-  if (typefind->num_buffer >= typefind->max_buffers) {
-    gst_element_error (GST_ELEMENT (typefind), 
-                   "typefind could not determine type after %d buffers", typefind->num_buffer);
-  }
+  find->suggest (find->data, probability, caps);
+  gst_caps_unref (caps);
 }
 
-static GstElementStateReturn
-gst_type_find_change_state (GstElement *element)
+/**
+ * gst_type_find_get_length:
+ * @find: The #GstTypeFind the function was called with
+ *
+ * Get the length of the data stream.
+ *
+ * Returns: The length of the data stream, or 0 if it is not available.
+ */
+guint64
+gst_type_find_get_length (GstTypeFind * find)
 {
-  GstTypeFind *typefind;
-  GstElementStateReturn ret;
-
-  typefind = GST_TYPE_FIND (element);
+  if (find->get_length == NULL)
+    return 0;
 
-  switch (GST_STATE_TRANSITION (element)) {
-    case GST_STATE_NULL_TO_READY:
-      break;
-    case GST_STATE_READY_TO_PAUSED:
-      typefind->num_buffer = 0;
-      gst_caps_unref (typefind->caps);
-      typefind->caps = NULL;
-      break;
-    case GST_STATE_PAUSED_TO_PLAYING:
-    case GST_STATE_PLAYING_TO_PAUSED:
-    case GST_STATE_PAUSED_TO_READY:
-    case GST_STATE_READY_TO_NULL:
-      break;
-  }
-  
-  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
-  
-  return ret;
+  return find->get_length (find->data);
 }