- Added value transform functions for caps and props boxed types
[platform/upstream/gstreamer.git] / gst / gstcaps.c
index 0ef1c55..f7ed767 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
-//#define GST_DEBUG_ENABLED
+/* #define GST_DEBUG_ENABLED */
 #include "gst_private.h"
 
 #include "gstcaps.h"
 #include "gsttype.h"
-
-#include "gstpropsprivate.h"
+#include "gstlog.h"
 
 static GMemChunk *_gst_caps_chunk;
 static GMutex *_gst_caps_chunk_lock;
 
+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;
+}
+
 void
 _gst_caps_initialize (void)
 {
@@ -38,6 +68,14 @@ _gst_caps_initialize (void)
                   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
@@ -47,13 +85,17 @@ get_type_for_mime (const gchar *mime)
 
   typeid = gst_type_find_by_mime (mime);
   if (typeid == 0) {
-     GstTypeFactory factory; // = g_new0 (GstTypeFactory, 1);
+     GstTypeDefinition definition;
+     GstTypeFactory *factory;
 
-     factory.mime = g_strdup (mime);
-     factory.exts = NULL;
-     factory.typefindfunc = NULL;
+     definition.name = "capstype";
+     definition.mime = g_strdup (mime);
+     definition.exts = NULL;
+     definition.typefindfunc = NULL;
 
-     typeid = gst_type_register (&factory);
+     factory = gst_type_factory_new (&definition);
+
+     typeid = gst_type_register (factory);
   }
   return typeid;
 }
@@ -71,20 +113,39 @@ get_type_for_mime (const gchar *mime)
 GstCaps*
 gst_caps_new (const gchar *name, const gchar *mime, GstProps *props)
 {
-  GstCaps *caps;
-
   g_return_val_if_fail (mime != NULL, NULL);
 
+  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 = get_type_for_mime (mime);
+  caps->id = id;
   caps->properties = props;
   caps->next = NULL;
   caps->refcount = 1;
-  caps->lock = g_mutex_new ();
+  if (props)
+    caps->fixed = props->fixed;
+  else
+    caps->fixed = TRUE;
 
   return caps;
 }
@@ -101,19 +162,49 @@ gst_caps_destroy (GstCaps *caps)
 {
   GstCaps *next;
 
-  g_return_if_fail (caps != NULL);
+  if (caps == NULL)
+    return;
 
-  GST_CAPS_LOCK (caps);
   next = caps->next;
+
+  gst_props_unref (caps->properties);
   g_free (caps->name);
-  g_free (caps);
-  GST_CAPS_UNLOCK (caps);
+  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)
+{
+  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");
+    }
+
+    caps = caps->next;
+  }
+  GST_DEBUG_LEAVE ("caps debug");
+}
+
+/**
  * gst_caps_unref:
  * @caps: the caps to unref
  *
@@ -128,14 +219,14 @@ gst_caps_unref (GstCaps *caps)
   gboolean zero;
   GstCaps **next;
 
-  g_return_val_if_fail (caps != NULL, NULL);
+  if (caps == NULL)
+    return NULL;
+
   g_return_val_if_fail (caps->refcount > 0, NULL);
 
-  GST_CAPS_LOCK (caps);
   caps->refcount--;
   zero = (caps->refcount == 0);
   next = &caps->next;
-  GST_CAPS_UNLOCK (caps);
 
   if (*next)
     *next = gst_caps_unref (*next);
@@ -160,14 +251,36 @@ gst_caps_ref (GstCaps *caps)
 {
   g_return_val_if_fail (caps != NULL, NULL);
 
-  GST_CAPS_LOCK (caps);
   caps->refcount++;
-  GST_CAPS_UNLOCK (caps);
 
   return caps;
 }
 
 /**
+ * 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;
+  
+  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
  *
@@ -178,16 +291,21 @@ gst_caps_ref (GstCaps *caps)
 GstCaps*
 gst_caps_copy (GstCaps *caps)
 {
-  GstCaps *new = caps;;
+  GstCaps *new = NULL, *walk = NULL;
 
-  g_return_val_if_fail (caps != NULL, NULL);
+  while (caps) {
+    GstCaps *newcaps;
 
-  GST_CAPS_LOCK (caps);
-  new = gst_caps_new (
-                 caps->name,
-                 (gst_type_find_by_id (caps->id))->mime,
-                 gst_props_copy (caps->properties));
-  GST_CAPS_UNLOCK (caps);
+    newcaps = gst_caps_copy_1 (caps);
+
+    if (new == NULL) {
+      new = walk = newcaps;
+    }
+    else {
+      walk = walk->next = newcaps;
+    }
+    caps = caps->next;
+  }
 
   return new;
 }
@@ -209,9 +327,7 @@ gst_caps_copy_on_write (GstCaps *caps)
 
   g_return_val_if_fail (caps != NULL, NULL);
 
-  GST_CAPS_LOCK (caps);
   needcopy = (caps->refcount > 1);
-  GST_CAPS_UNLOCK (caps);
 
   if (needcopy) {
     new = gst_caps_copy (caps);
@@ -406,9 +522,7 @@ gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
 {
   GstCaps *orig = caps;
   
-  g_return_val_if_fail (caps != capstoadd, caps);
-
-  if (caps == NULL)
+  if (caps == NULL || caps == capstoadd)
     return capstoadd;
   
   while (caps->next) {
@@ -433,11 +547,11 @@ gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
 {
   GstCaps *orig = capstoadd;
   
-  g_return_val_if_fail (caps != capstoadd, caps);
-
   if (capstoadd == NULL)
     return caps;
 
+  g_return_val_if_fail (caps != capstoadd, caps);
+
   while (capstoadd->next) {
     capstoadd = capstoadd->next;
   }
@@ -476,7 +590,7 @@ static gboolean
 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
 {
   if (fromcaps->id != tocaps->id) {
-    GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)\n",
+    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;
@@ -487,13 +601,13 @@ gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
       return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
     }
     else {
-      GST_DEBUG (GST_CAT_CAPS,"no source caps\n");
+      GST_DEBUG (GST_CAT_CAPS,"no source caps");
       return FALSE;
     }
   }
   else {
-    // assume it accepts everything
-    GST_DEBUG (GST_CAT_CAPS,"no caps\n");
+    /* assume it accepts everything */
+    GST_DEBUG (GST_CAT_CAPS,"no caps");
     return TRUE;
   }
 }
@@ -512,17 +626,17 @@ gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
 {
   if (fromcaps == NULL) {
     if (tocaps == NULL) {
-      GST_DEBUG (GST_CAT_CAPS,"no caps\n");
+      GST_DEBUG (GST_CAT_CAPS,"no caps");
       return TRUE;
     }
     else {
-      GST_DEBUG (GST_CAT_CAPS,"no source but destination caps\n");
+      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\n");
+      GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps");
       return TRUE;
     }
   }
@@ -541,6 +655,132 @@ gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
   return FALSE;
 }
 
+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;
+  }
+
+  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_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;
+
+  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);
+  }
+
+  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 result;
+}
+
+/**
+ * gst_caps_normalize:
+ * @caps: a capabilty
+ *
+ * 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: The normalisation of the caps.
+ */
+GstCaps*
+gst_caps_normalize (GstCaps *caps)
+{
+  GstCaps *result = NULL, *walk = caps;
+
+  if (caps == NULL)
+    return caps;
+
+  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;
+  }
+  return result;
+}
+
+#ifndef GST_DISABLE_LOADSAVE_REGISTRY
 /**
  * gst_caps_save_thyself:
  * @caps: a capabilty to save
@@ -598,8 +838,8 @@ gst_caps_load_thyself (xmlNodePtr parent)
       g_mutex_unlock (_gst_caps_chunk_lock);
 
       caps->refcount = 1;
-      caps->lock = g_mutex_new ();
       caps->next = NULL;
+      caps->fixed = TRUE;
        
       while (subfield) {
         if (!strcmp (subfield->name, "name")) {
@@ -624,5 +864,4 @@ gst_caps_load_thyself (xmlNodePtr parent)
   return result;
 }
 
-
-
+#endif /* GST_DISABLE_LOADSAVE_REGISTRY */