- Added value transform functions for caps and props boxed types
[platform/upstream/gstreamer.git] / gst / gstcaps.c
index 91755b8..f7ed767 100644 (file)
@@ -1,5 +1,8 @@
-/* Gnome-Streamer
- * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
+/* GStreamer
+ * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
+ *                    2000 Wim Taymans <wtay@chello.be>
+ *
+ * gstcaps.c: Element capabilities 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.
  */
 
-#include <stdarg.h>
-#include <gst/gstcapsprivate.h>
+/* #define GST_DEBUG_ENABLED */
+#include "gst_private.h"
+
+#include "gstcaps.h"
+#include "gsttype.h"
+#include "gstlog.h"
+
+static GMemChunk *_gst_caps_chunk;
+static GMutex *_gst_caps_chunk_lock;
 
-void 
-_gst_caps_initialize (void) 
+GType _gst_caps_type;
+
+static void
+transform_func (const GValue *src_value,
+               GValue *dest_value)
 {
+  GstCaps *caps = g_value_peek_pointer (src_value);
+  GString *result = g_string_new (""); 
+
+  g_string_append_printf (result, "(GstCaps *) ");
+                 
+  while (caps) {
+    gchar *props;
+    GValue value = { 0, }; /* the important thing is that value.type = 0 */
+    
+    g_string_append_printf (result,
+                 "{ %s; ", gst_caps_get_mime (caps));
+
+    g_value_init (&value, GST_TYPE_PROPS);
+    g_value_set_boxed  (&value, caps->properties);
+    props = g_strdup_value_contents (&value);
+
+    g_string_append (result, props);
+    g_free (props);
+
+    caps = caps->next;
+    g_string_append_printf (result, " }%s", caps?", ":"");
+  }
+  dest_value->data[0].v_pointer = result->str;
 }
 
-static GstCapsEntry *
-gst_caps_create_entry (GstCapsFactory factory, gint *skipped)
+void
+_gst_caps_initialize (void)
 {
-  GstCapsFactoryEntry tag;
-  GstCapsEntry *entry;
-  guint i=0;
-
-  entry = g_new0 (GstCapsEntry, 1);
-
-  tag = factory[i++];
-  switch (GPOINTER_TO_INT (tag)) {
-    case GST_CAPS_INT_ID:
-      entry->capstype = GST_CAPS_INT_ID_NUM;
-      entry->data.int_data = GPOINTER_TO_INT (factory[i++]);
-      break;
-    case GST_CAPS_INT_RANGE_ID:
-      entry->capstype = GST_CAPS_INT_RANGE_ID_NUM;
-      entry->data.int_range_data.min = GPOINTER_TO_INT (factory[i++]);
-      entry->data.int_range_data.max = GPOINTER_TO_INT (factory[i++]);
-      break;
-    case GST_CAPS_LIST_ID:
-      g_print("gstcaps: list not allowed in list\n");
-      break;
-    default:
-      g_print("gstcaps: unknown caps id found\n");
-      g_free (entry);
-      entry = NULL;
-      break;
+  _gst_caps_chunk = g_mem_chunk_new ("GstCaps",
+                  sizeof (GstCaps), sizeof (GstCaps) * 256,
+                  G_ALLOC_AND_FREE);
+  _gst_caps_chunk_lock = g_mutex_new ();
+
+  _gst_caps_type = g_boxed_type_register_static ("GstCaps",
+                                       (GBoxedCopyFunc) gst_caps_ref,
+                                       (GBoxedFreeFunc) gst_caps_unref);
+
+  g_value_register_transform_func (_gst_caps_type,
+                                  G_TYPE_STRING,
+                                  transform_func);
+}
+
+static guint16
+get_type_for_mime (const gchar *mime)
+{
+  guint16 typeid;
+
+  typeid = gst_type_find_by_mime (mime);
+  if (typeid == 0) {
+     GstTypeDefinition definition;
+     GstTypeFactory *factory;
+
+     definition.name = "capstype";
+     definition.mime = g_strdup (mime);
+     definition.exts = NULL;
+     definition.typefindfunc = NULL;
+
+     factory = gst_type_factory_new (&definition);
+
+     typeid = gst_type_register (factory);
   }
+  return typeid;
+}
 
-  *skipped = i;
+/**
+ * gst_caps_new:
+ * @name: the name of this capability
+ * @mime: the mime type to attach to the capability
+ * @props: the properties to add to this capability
+ *
+ * Create a new capability with the given mime typei and properties.
+ *
+ * Returns: a new capability
+ */
+GstCaps*
+gst_caps_new (const gchar *name, const gchar *mime, GstProps *props)
+{
+  g_return_val_if_fail (mime != NULL, NULL);
 
-  return entry;
+  return gst_caps_new_id (name, get_type_for_mime (mime), props);
 }
 
+/**
+ * gst_caps_new_id:
+ * @name: the name of this capability
+ * @id: the id of the mime type 
+ * @props: the properties to add to this capability
+ *
+ * Create a new capability with the given mime typeid and properties.
+ *
+ * Returns: a new capability
+ */
+GstCaps*
+gst_caps_new_id (const gchar *name, const guint16 id, GstProps *props)
+{
+  GstCaps *caps;
+
+  g_mutex_lock (_gst_caps_chunk_lock);
+  caps = g_mem_chunk_alloc (_gst_caps_chunk);
+  g_mutex_unlock (_gst_caps_chunk_lock);
+
+  caps->name = g_strdup (name);
+  caps->id = id;
+  caps->properties = props;
+  caps->next = NULL;
+  caps->refcount = 1;
+  if (props)
+    caps->fixed = props->fixed;
+  else
+    caps->fixed = TRUE;
 
-static gint 
-caps_compare_func (gconstpointer a,
-                  gconstpointer b) 
+  return caps;
+}
+
+/**
+ * gst_caps_destroy:
+ * @caps: the caps to destroy
+ *
+ * Frees the memory used by this caps structure and all
+ * the chained caps and properties.
+ */
+void
+gst_caps_destroy (GstCaps *caps)
+{
+  GstCaps *next;
+
+  if (caps == NULL)
+    return;
+
+  next = caps->next;
+
+  gst_props_unref (caps->properties);
+  g_free (caps->name);
+  g_mutex_lock (_gst_caps_chunk_lock);
+  g_mem_chunk_free (_gst_caps_chunk, caps);
+  g_mutex_unlock (_gst_caps_chunk_lock);
+
+  if (next) 
+    gst_caps_unref (next);
+}
+
+/**
+ * gst_caps_debug:
+ * @caps: the caps to print out
+ * @label: a label to put on the printout, or NULL
+ *
+ * Print out the contents of the caps structure. Useful for debugging.
+ */
+void
+gst_caps_debug (GstCaps *caps, const gchar *label)
 {
-  GstCapsEntry *entry1 = (GstCapsEntry *)a;
-  GstCapsEntry *entry2 = (GstCapsEntry *)b;
+  GST_DEBUG_ENTER ("caps debug: %s", label);
+  while (caps) {
+    GST_DEBUG (GST_CAT_CAPS, "caps: %p %s %s (%sfixed)", caps, caps->name, gst_caps_get_mime (caps), 
+               caps->fixed ? "" : "NOT ");
+
+    if (caps->properties) {
+      gst_props_debug (caps->properties);
+    }
+    else {
+      GST_DEBUG (GST_CAT_CAPS, "no properties");
+    }
 
-  return (entry1->propid - entry2->propid);
+    caps = caps->next;
+  }
+  GST_DEBUG_LEAVE ("caps debug");
 }
 
 /**
- * gst_caps_register:
- * @factory: the factory to register
+ * gst_caps_unref:
+ * @caps: the caps to unref
  *
- * Register the factory. 
+ * Decrease the refcount of this caps structure, 
+ * destroying it when the refcount is 0
  *
- * Returns: The registered capability
+ * Returns: caps or NULL if the refcount reached 0
  */
-GstCaps *
-gst_caps_register (GstCapsFactory factory)
+GstCaps*
+gst_caps_unref (GstCaps *caps)
 {
-  GstCapsFactoryEntry tag;
-  gint i = 0;
-  guint16 typeid;
-  GstCaps *caps;
-  gint skipped;
-  
-  g_return_val_if_fail (factory != NULL, NULL);
+  gboolean zero;
+  GstCaps **next;
 
-  tag = factory[i++];
+  if (caps == NULL)
+    return NULL;
 
-  g_return_val_if_fail (tag != NULL, NULL);
-  
-  typeid = gst_type_find_by_mime ((gchar *)tag);
-  if (typeid == 0) {
-     GstTypeFactory *factory = g_new0 (GstTypeFactory, 1);
+  g_return_val_if_fail (caps->refcount > 0, NULL);
 
-     factory->mime = g_strdup ((gchar *)tag);
-     factory->exts = NULL;
-     factory->typefindfunc = NULL;
+  caps->refcount--;
+  zero = (caps->refcount == 0);
+  next = &caps->next;
 
-     typeid = gst_type_register (factory);
+  if (*next)
+    *next = gst_caps_unref (*next);
+
+  if (zero) {
+    gst_caps_destroy (caps);
+    caps = NULL;
   }
+  return caps;
+}
 
-  caps = g_new0 (GstCaps, 1);
+/**
+ * gst_caps_ref:
+ * @caps: the caps to ref
+ *
+ * Increase the refcount of this caps structure
+ *
+ * Returns: the caps with the refcount incremented
+ */
+GstCaps*
+gst_caps_ref (GstCaps *caps)
+{
   g_return_val_if_fail (caps != NULL, NULL);
 
-  caps->id = typeid;
-  caps->properties = NULL;
+  caps->refcount++;
+
+  return caps;
+}
 
-  tag = factory[i++];
+/**
+ * gst_caps_copy_1:
+ * @caps: the caps to copy
+ *
+ * Copies the caps, not copying any chained caps.
+ *
+ * Returns: a copy of the GstCaps structure.
+ */
+GstCaps*
+gst_caps_copy_1 (GstCaps *caps)
+{
+  GstCaps *newcaps;
   
-  while (tag) {
-    GQuark quark;
-    GstCapsEntry *entry;
-    
-    quark = g_quark_from_string ((gchar *)tag);
-
-    tag = factory[i];
-    switch (GPOINTER_TO_INT (tag)) {
-      case GST_CAPS_LIST_ID: 
-      {
-        GstCapsEntry *list_entry;
-
-        entry = g_new0 (GstCapsEntry, 1);
-       entry->propid = quark;
-       entry->capstype = GST_CAPS_LIST_ID_NUM;
-       entry->data.list_data.entries = NULL;
-
-       i++; // skip list tag
-        tag = factory[i];
-       while (tag) {
-         list_entry = gst_caps_create_entry (&factory[i], &skipped);
-         i += skipped;
-          tag = factory[i];
-         entry->data.list_data.entries = g_list_prepend (entry->data.list_data.entries, list_entry);
-       }
-       entry->data.list_data.entries = g_list_reverse (entry->data.list_data.entries);
-       i++; //skip NULL (list end)
-       break;
-      }
-      default:
-      {
-       entry = gst_caps_create_entry (&factory[i], &skipped);
-       entry->propid = quark;
-       i += skipped;
-       break;
-      }
+  if (!caps)
+    return NULL;
+
+  newcaps = gst_caps_new_id (
+                 caps->name,
+                 caps->id,
+                 gst_props_copy (caps->properties));
+
+  return newcaps;
+}
+
+/**
+ * gst_caps_copy:
+ * @caps: the caps to copy
+ *
+ * Copies the caps.
+ *
+ * Returns: a copy of the GstCaps structure.
+ */
+GstCaps*
+gst_caps_copy (GstCaps *caps)
+{
+  GstCaps *new = NULL, *walk = NULL;
+
+  while (caps) {
+    GstCaps *newcaps;
+
+    newcaps = gst_caps_copy_1 (caps);
+
+    if (new == NULL) {
+      new = walk = newcaps;
+    }
+    else {
+      walk = walk->next = newcaps;
     }
-    caps->properties = g_slist_insert_sorted (caps->properties, entry, caps_compare_func);
-     
-    tag = factory[i++];
+    caps = caps->next;
   }
 
-  return caps;
+  return new;
 }
 
-static void
-gst_caps_dump_entry_func (GstCapsEntry *entry)
+/**
+ * gst_caps_copy_on_write:
+ * @caps: the caps to copy
+ *
+ * Copies the caps if the refcount is greater than 1
+ *
+ * Returns: a pointer to a GstCaps strcuture that can
+ * be safely written to
+ */
+GstCaps*
+gst_caps_copy_on_write (GstCaps *caps)
 {
-  switch (entry->capstype) {
-    case GST_CAPS_INT_ID_NUM: 
-      g_print("gstcaps:    int %d\n", entry->data.int_data);
-      break;
-    case GST_CAPS_INT_RANGE_ID_NUM: 
-      g_print("gstcaps:    int range %d %d\n", 
-                     entry->data.int_range_data.min,
-                     entry->data.int_range_data.max);
-      break;
-    case GST_CAPS_INT32_ID_NUM: 
-      g_print("gstcaps:    int32 %d\n", entry->data.int_data);
-      break;
-    case GST_CAPS_BOOL_ID_NUM: 
-      g_print("gstcaps:    boolean %d\n", entry->data.bool_data);
-      break;
-    default:
-      g_print("gstcaps:    **illegal entry**\n");
-      break;
+  GstCaps *new = caps;
+  gboolean needcopy;
+
+  g_return_val_if_fail (caps != NULL, NULL);
+
+  needcopy = (caps->refcount > 1);
+
+  if (needcopy) {
+    new = gst_caps_copy (caps);
+    gst_caps_unref (caps);
   }
+
+  return new;
 }
 
-static void
-gst_caps_dump_list_func (gpointer entry,
-                        gpointer list_entry)
+/**
+ * gst_caps_get_name:
+ * @caps: the caps to get the name from
+ *
+ * Get the name of a GstCaps structure.
+ *
+ * Returns: the name of the caps
+ */
+const gchar*
+gst_caps_get_name (GstCaps *caps)
 {
-  gst_caps_dump_entry_func ((GstCapsEntry *)entry);
+  g_return_val_if_fail (caps != NULL, NULL);
+
+  return (const gchar *)caps->name;
 }
 
-static void
-gst_caps_dump_func (gpointer data,
-                   gpointer user_data)
+/**
+ * gst_caps_set_name:
+ * @caps: the caps to set the name to
+ * @name: the name to set
+ *
+ * Set the name of a caps.
+ */
+void
+gst_caps_set_name (GstCaps *caps, const gchar *name)
 {
-  GstCapsEntry *entry;
+  g_return_if_fail (caps != NULL);
 
-  entry = (GstCapsEntry *)data;
+  if (caps->name)
+    g_free (caps->name);
 
-  g_print("gstcaps:  property type \"%s\"\n", g_quark_to_string (entry->propid));
+  caps->name = g_strdup (name);
+}
 
-  switch (entry->capstype) {
-    case GST_CAPS_LIST_ID_NUM: 
-    {
-      g_print("gstcaps:   list type (\n");
-      g_list_foreach (entry->data.list_data.entries, gst_caps_dump_list_func, entry);
-      g_print("gstcaps:   )\n");
-      break;
-    }
-    default:
-      gst_caps_dump_entry_func (entry);
-      break;
-  }
+/**
+ * gst_caps_get_mime:
+ * @caps: the caps to get the mime type from
+ *
+ * Get the mime type of the caps as a string.
+ *
+ * Returns: the mime type of the caps
+ */
+const gchar*
+gst_caps_get_mime (GstCaps *caps)
+{
+  GstType *type;
+
+  g_return_val_if_fail (caps != NULL, NULL);
+
+  type = gst_type_find_by_id (caps->id);
+
+  if (type)
+    return type->mime;
+  else
+    return "unknown/unknown";
 }
 
 /**
- * gst_caps_dump:
- * @caps: the capability to dump
+ * gst_caps_set_mime:
+ * @caps: the caps to set the mime type to
+ * @mime: the mime type to attach to the caps
  *
- * Dumps the contents of the capabilty one the console
+ * Set the mime type of the caps as a string.
  */
 void
-gst_caps_dump (GstCaps *caps)
+gst_caps_set_mime (GstCaps *caps, const gchar *mime)
 {
   g_return_if_fail (caps != NULL);
+  g_return_if_fail (mime != NULL);
+
+  caps->id = get_type_for_mime (mime);
+}
 
-  g_print("gstcaps: {\ngstcaps:  mime type \"%d\"\n", caps->id);
+/**
+ * gst_caps_get_type_id:
+ * @caps: the caps to get the type id from
+ *
+ * Get the type id of the caps.
+ *
+ * Returns: the type id of the caps
+ */
+guint16
+gst_caps_get_type_id (GstCaps *caps)
+{
+  g_return_val_if_fail (caps != NULL, 0);
 
-  g_slist_foreach (caps->properties, gst_caps_dump_func, caps);
-  g_print("gstcaps: }\n");
+  return caps->id;
 }
-       
+
+/**
+ * gst_caps_set_type_id:
+ * @caps: the caps to set the type id to
+ * @type_id: the type id to set
+ *
+ * Set the type id of the caps.
+ */
+void
+gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
+{
+  g_return_if_fail (caps != NULL);
+
+  caps->id = type_id;
+}
+
+/**
+ * gst_caps_set_props:
+ * @caps: the caps to attach the properties to
+ * @props: the properties to attach
+ *
+ * Set the properties to the given caps.
+ *
+ * Returns: the new caps structure
+ */
+GstCaps*
+gst_caps_set_props (GstCaps *caps, GstProps *props)
+{
+  g_return_val_if_fail (caps != NULL, caps);
+  g_return_val_if_fail (props != NULL, caps);
+  g_return_val_if_fail (caps->properties == NULL, caps);
+
+  caps->properties = props;
+
+  return caps;
+}
+
+/**
+ * gst_caps_get_props:
+ * @caps: the caps to get the properties from
+ *
+ * Get the properties of the given caps.
+ *
+ * Returns: the properties of the caps
+ */
+GstProps*
+gst_caps_get_props (GstCaps *caps)
+{
+  g_return_val_if_fail (caps != NULL, NULL);
+
+  return caps->properties;
+}
+
+/**
+ * gst_caps_chain:
+ * @caps: a capabilty
+ * @...: more capabilities
+ *
+ * chains the given capabilities
+ *
+ * Returns: the new capability
+ */
+GstCaps*
+gst_caps_chain (GstCaps *caps, ...)
+{
+  GstCaps *orig = caps;
+  va_list var_args;
+
+  va_start (var_args, caps);
+
+  while (caps) {
+    GstCaps *toadd;
+    
+    toadd = va_arg (var_args, GstCaps*);
+    gst_caps_append (caps, toadd);
+    
+    caps = toadd;
+  }
+  va_end (var_args);
+  
+  return orig;
+}
+
+/**
+ * gst_caps_append:
+ * @caps: a capabilty
+ * @capstoadd: the capability to append
+ *
+ * Appends a capability to the existing capability.
+ *
+ * Returns: the new capability
+ */
+GstCaps*
+gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
+{
+  GstCaps *orig = caps;
+  
+  if (caps == NULL || caps == capstoadd)
+    return capstoadd;
+  
+  while (caps->next) {
+    caps = caps->next;
+  }
+  caps->next = capstoadd;
+
+  return orig;
+}
+
+/**
+ * gst_caps_prepend:
+ * @caps: a capabilty
+ * @capstoadd: a capabilty to prepend
+ *
+ * prepend the capability to the list of capabilities
+ *
+ * Returns: the new capability
+ */
+GstCaps*
+gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
+{
+  GstCaps *orig = capstoadd;
+  
+  if (capstoadd == NULL)
+    return caps;
+
+  g_return_val_if_fail (caps != capstoadd, caps);
+
+  while (capstoadd->next) {
+    capstoadd = capstoadd->next;
+  }
+  capstoadd->next = caps;
+
+  return orig;
+}
+
+/**
+ * gst_caps_get_by_name:
+ * @caps: a capabilty
+ * @name: the name of the capability to get
+ *
+ * Get the capability with the given name from this
+ * chain of capabilities.
+ *
+ * Returns: the first capability in the chain with the 
+ * given name
+ */
+GstCaps*
+gst_caps_get_by_name (GstCaps *caps, const gchar *name)
+{
+  g_return_val_if_fail (caps != NULL, NULL);
+  g_return_val_if_fail (name != NULL, NULL);
+   
+  while (caps) {
+    if (!strcmp (caps->name, name)) 
+      return caps;
+    caps = caps->next;
+  }
+
+  return NULL;
+}
+                                                                                                                   
 static gboolean
-gst_caps_entry_check_compatibility (GstCapsEntry *entry1, GstCapsEntry *entry2)
+gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
 {
-  g_print ("compare: %s %s\n", g_quark_to_string (entry1->propid),
-                              g_quark_to_string (entry2->propid));
+  if (fromcaps->id != tocaps->id) {
+    GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)",
+              gst_type_find_by_id (fromcaps->id)->mime, 
+              gst_type_find_by_id (tocaps->id)->mime);
+    return FALSE;
+  }
+
+  if (tocaps->properties) {
+    if (fromcaps->properties) {
+      return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
+    }
+    else {
+      GST_DEBUG (GST_CAT_CAPS,"no source caps");
+      return FALSE;
+    }
+  }
+  else {
+    /* assume it accepts everything */
+    GST_DEBUG (GST_CAT_CAPS,"no caps");
+    return TRUE;
+  }
 }
 
 /**
@@ -242,87 +617,251 @@ gst_caps_entry_check_compatibility (GstCapsEntry *entry1, GstCapsEntry *entry2)
  * @fromcaps: a capabilty
  * @tocaps: a capabilty
  *
- * Checks whether two capabilities are compatible
+ * Checks whether two capabilities are compatible.
  *
- * Returns: true if compatible, false otherwise
+ * Returns: TRUE if compatible, FALSE otherwise
  */
 gboolean
 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
 {
-  GSList *sourcelist;
-  GSList *sinklist;
-  gint missing = 0;
-  gint more = 0;
+  if (fromcaps == NULL) {
+    if (tocaps == NULL) {
+      GST_DEBUG (GST_CAT_CAPS,"no caps");
+      return TRUE;
+    }
+    else {
+      GST_DEBUG (GST_CAT_CAPS,"no source but destination caps");
+      return FALSE;
+    }
+  }
+  else {
+    if (tocaps == NULL) {
+      GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps");
+      return TRUE;
+    }
+  }
 
-  g_return_val_if_fail (fromcaps != NULL, FALSE);
-  g_return_val_if_fail (tocaps != NULL, FALSE);
-       
-  if (fromcaps->id != tocaps->id)
-    return FALSE;
+  while (fromcaps) {
+    GstCaps *destcaps = tocaps;
 
-  sourcelist = fromcaps->properties;
-  sinklist   = tocaps->properties;
+    while (destcaps) {
+      if (gst_caps_check_compatibility_func (fromcaps, destcaps))
+       return TRUE;
 
-  while (sourcelist && sinklist) {
-    GstCapsEntry *entry1;
-    GstCapsEntry *entry2;
+      destcaps =  destcaps->next;
+    }
+    fromcaps =  fromcaps->next;
+  }
+  return FALSE;
+}
 
-    entry1 = (GstCapsEntry *)sourcelist->data;
-    entry2 = (GstCapsEntry *)sinklist->data;
+static GstCaps*
+gst_caps_intersect_func (GstCaps *caps1, GstCaps *caps2)
+{
+  GstCaps *result = NULL;
+  GstProps *props;
+
+  if (caps1->id != caps2->id) {
+    GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)",
+              gst_type_find_by_id (caps1->id)->mime, 
+              gst_type_find_by_id (caps2->id)->mime);
+    return NULL;
+  }
 
-    while (entry1->propid < entry2->propid) {
-      g_print ("source is more specific in \"%s\"\n", g_quark_to_string (entry1->propid));
-      more++;
-      sourcelist = g_slist_next (sourcelist);
-      if (sourcelist) entry1 = (GstCapsEntry *)sourcelist->data;
-      else goto end;
-    }
-    while (entry1->propid > entry2->propid) {
-      g_print ("source has missing property \"%s\"\n", g_quark_to_string (entry2->propid));
-      missing++;
-      sinklist = g_slist_next (sinklist);
-      if (sinklist) entry2 = (GstCapsEntry *)sinklist->data;
-      else goto end;
-    }
+  if (caps1->properties == NULL) {
+    return gst_caps_ref (caps2);
+  }
+  if (caps2->properties == NULL) {
+    return gst_caps_ref (caps1);
+  }
+  
+  props = gst_props_intersect (caps1->properties, caps2->properties);
+  if (props) {
+    result = gst_caps_new_id ("intersect", caps1->id, props);
+  }
+
+  return result;
+}
 
-    gst_caps_entry_check_compatibility (entry1, entry2);
+/**
+ * gst_caps_intersect:
+ * @caps1: a capabilty
+ * @caps2: a capabilty
+ *
+ * Make the intersection between two caps.
+ *
+ * Returns: The intersection of the two caps or NULL if the intersection
+ * is empty.
+ */
+GstCaps*
+gst_caps_intersect (GstCaps *caps1, GstCaps *caps2)
+{
+  GstCaps *result = NULL, *walk = NULL;
 
-    sourcelist = g_slist_next (sourcelist);
-    sinklist = g_slist_next (sinklist);
+  if (caps1 == NULL) {
+    GST_DEBUG (GST_CAT_CAPS, "first caps is NULL, return other caps");
+    return gst_caps_copy (caps2);
+  }
+  if (caps2 == NULL) {
+    GST_DEBUG (GST_CAT_CAPS, "second caps is NULL, return other caps");
+    return gst_caps_copy (caps1);
   }
-end:
 
-  if (missing)
-    return FALSE;
+  while (caps1) {
+    GstCaps *othercaps = caps2;
+
+    while (othercaps) {
+      GstCaps *intersection;
+      
+      intersection = gst_caps_intersect_func (caps1, othercaps);
+
+      if (intersection) {
+        if (!result) {
+         walk = result = intersection;
+        }
+        else {
+         walk = walk->next = intersection;
+        }
+      }
+      othercaps = othercaps->next;
+    }
+    caps1 =  caps1->next;
+  }
 
-  return TRUE;
+  return result;
 }
 
 /**
- * gst_caps_register_va:
- * @factory: the factories to register
+ * gst_caps_normalize:
+ * @caps: a capabilty
  *
- * Register the given factories. 
+ * Make the normalisation of the caps. This will return a new caps
+ * that is equivalent to the input caps with the exception that all
+ * lists are unrolled. This function is useful when you want to iterate
+ * the caps.
  *
- * Returns: A list of the registered factories
+ * Returns: The normalisation of the caps.
  */
-GList *
-gst_caps_register_va (GstCapsFactory factory, ...)
+GstCaps*
+gst_caps_normalize (GstCaps *caps)
 {
-  va_list var_args;
-  GstCapsFactoryEntry *current_factory;
+  GstCaps *result = NULL, *walk = caps;
 
-  va_start (var_args, factory);
+  if (caps == NULL)
+    return caps;
 
-  current_factory = (GstCapsFactoryEntry *) factory;
-  
-  while (current_factory) {
-    gst_caps_register (current_factory);
-    
-    current_factory = va_arg (var_args, GstCapsFactoryEntry *);
+  while (caps) {
+    GList *proplist;
+
+    proplist = gst_props_normalize (caps->properties);
+    if (proplist && g_list_next (proplist) == NULL) {
+      if (result == NULL)
+       walk = result = caps;
+      else {
+       walk = walk->next = caps;
+      }
+      goto next;
+    }
+
+    while (proplist) {
+      GstProps *props = (GstProps *) proplist->data;
+      GstCaps *newcaps = gst_caps_new_id (caps->name, caps->id, props);
+
+      if (result == NULL)
+       walk = result = newcaps;
+      else {
+       walk = walk->next = newcaps;
+      }
+      proplist = g_list_next (proplist);  
+    }
+next:
+    caps = caps->next;
   }
-  
-  va_end(var_args);
+  return result;
+}
 
-  return NULL;
+#ifndef GST_DISABLE_LOADSAVE_REGISTRY
+/**
+ * gst_caps_save_thyself:
+ * @caps: a capabilty to save
+ * @parent: the parent XML node pointer
+ *
+ * Save the capability into an XML representation.
+ *
+ * Returns: a new XML node pointer
+ */
+xmlNodePtr
+gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
+{
+  xmlNodePtr subtree;
+  xmlNodePtr subsubtree;
+
+  while (caps) {
+    subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
+
+    xmlNewChild (subtree, NULL, "name", caps->name);
+    xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
+    if (caps->properties) {
+      subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
+
+      gst_props_save_thyself (caps->properties, subsubtree);
+    }
+
+    caps = caps->next;
+  }
+
+  return parent;
+}
+
+/**
+ * gst_caps_load_thyself:
+ * @parent: the parent XML node pointer
+ *
+ * Load a new caps from the XML representation.
+ *
+ * Returns: a new capability
+ */
+GstCaps*
+gst_caps_load_thyself (xmlNodePtr parent)
+{
+  GstCaps *result = NULL;
+  xmlNodePtr field = parent->xmlChildrenNode;
+
+  while (field) {
+    if (!strcmp (field->name, "capscomp")) {
+      xmlNodePtr subfield = field->xmlChildrenNode;
+      GstCaps *caps;
+      gchar *content;
+
+      g_mutex_lock (_gst_caps_chunk_lock);
+      caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
+      g_mutex_unlock (_gst_caps_chunk_lock);
+
+      caps->refcount = 1;
+      caps->next = NULL;
+      caps->fixed = TRUE;
+       
+      while (subfield) {
+        if (!strcmp (subfield->name, "name")) {
+          caps->name = xmlNodeGetContent (subfield);
+        }
+        if (!strcmp (subfield->name, "type")) {
+          content = xmlNodeGetContent (subfield);
+          caps->id = get_type_for_mime (content);
+          g_free (content);
+        }
+        else if (!strcmp (subfield->name, "properties")) {
+          caps->properties = gst_props_load_thyself (subfield);
+        }
+       
+        subfield = subfield->next;
+      }
+      result = gst_caps_append (result, caps);
+    }
+    field = field->next;
+  }
+
+  return result;
 }
+
+#endif /* GST_DISABLE_LOADSAVE_REGISTRY */