2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstcaps.c: Element capabilities subsystem
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 /* #define GST_DEBUG_ENABLED */
24 #include "gst_private.h"
30 static GMemChunk *_gst_caps_chunk;
31 static GMutex *_gst_caps_chunk_lock;
36 _gst_caps_initialize (void)
38 _gst_caps_chunk = g_mem_chunk_new ("GstCaps",
39 sizeof (GstCaps), sizeof (GstCaps) * 256,
41 _gst_caps_chunk_lock = g_mutex_new ();
43 _gst_caps_type = g_boxed_type_register_static ("GstCaps",
44 (GBoxedCopyFunc) gst_caps_ref,
45 (GBoxedFreeFunc) gst_caps_unref);
50 get_type_for_mime (const gchar *mime)
54 typeid = gst_type_find_by_mime (mime);
56 GstTypeDefinition definition;
57 GstTypeFactory *factory;
59 definition.name = "capstype";
60 definition.mime = g_strdup (mime);
61 definition.exts = NULL;
62 definition.typefindfunc = NULL;
64 factory = gst_type_factory_new (&definition);
66 typeid = gst_type_register (factory);
73 * @name: the name of this capability
74 * @mime: the mime type to attach to the capability
75 * @props: the properties to add to this capability
77 * Create a new capability with the given mime typei and properties.
79 * Returns: a new capability
82 gst_caps_new (const gchar *name, const gchar *mime, GstProps *props)
84 g_return_val_if_fail (mime != NULL, NULL);
86 return gst_caps_new_id (name, get_type_for_mime (mime), props);
91 * @name: the name of this capability
92 * @id: the id of the mime type
93 * @props: the properties to add to this capability
95 * Create a new capability with the given mime typeid and properties.
97 * Returns: a new capability
100 gst_caps_new_id (const gchar *name, const guint16 id, GstProps *props)
104 g_mutex_lock (_gst_caps_chunk_lock);
105 caps = g_mem_chunk_alloc (_gst_caps_chunk);
106 g_mutex_unlock (_gst_caps_chunk_lock);
108 caps->name = g_strdup (name);
110 caps->properties = props;
114 caps->fixed = props->fixed;
123 * @caps: the caps to destroy
125 * Frees the memory used by this caps structure and all
126 * the chained caps and properties.
129 gst_caps_destroy (GstCaps *caps)
138 gst_props_unref (caps->properties);
140 g_mutex_lock (_gst_caps_chunk_lock);
141 g_mem_chunk_free (_gst_caps_chunk, caps);
142 g_mutex_unlock (_gst_caps_chunk_lock);
145 gst_caps_unref (next);
150 * @caps: the caps to print out
151 * @label: a label to put on the printout, or NULL
153 * Print out the contents of the caps structure. Useful for debugging.
156 gst_caps_debug (GstCaps *caps, const gchar *label)
158 GST_DEBUG_ENTER ("caps debug: %s", label);
160 GST_DEBUG (GST_CAT_CAPS, "caps: %p %s %s (%sfixed)", caps, caps->name, gst_caps_get_mime (caps),
161 caps->fixed ? "" : "NOT ");
163 if (caps->properties) {
164 gst_props_debug (caps->properties);
167 GST_DEBUG (GST_CAT_CAPS, "no properties");
172 GST_DEBUG_LEAVE ("caps debug");
177 * @caps: the caps to unref
179 * Decrease the refcount of this caps structure,
180 * destroying it when the refcount is 0
182 * Returns: caps or NULL if the refcount reached 0
185 gst_caps_unref (GstCaps *caps)
193 g_return_val_if_fail (caps->refcount > 0, NULL);
196 zero = (caps->refcount == 0);
200 *next = gst_caps_unref (*next);
203 gst_caps_destroy (caps);
211 * @caps: the caps to ref
213 * Increase the refcount of this caps structure
215 * Returns: the caps with the refcount incremented
218 gst_caps_ref (GstCaps *caps)
220 g_return_val_if_fail (caps != NULL, NULL);
229 * @caps: the caps to copy
231 * Copies the caps, not copying any chained caps.
233 * Returns: a copy of the GstCaps structure.
236 gst_caps_copy_1 (GstCaps *caps)
243 newcaps = gst_caps_new_id (
246 gst_props_copy (caps->properties));
253 * @caps: the caps to copy
257 * Returns: a copy of the GstCaps structure.
260 gst_caps_copy (GstCaps *caps)
262 GstCaps *new = NULL, *walk = NULL;
267 newcaps = gst_caps_copy_1 (caps);
270 new = walk = newcaps;
273 walk = walk->next = newcaps;
282 * gst_caps_copy_on_write:
283 * @caps: the caps to copy
285 * Copies the caps if the refcount is greater than 1
287 * Returns: a pointer to a GstCaps strcuture that can
288 * be safely written to
291 gst_caps_copy_on_write (GstCaps *caps)
296 g_return_val_if_fail (caps != NULL, NULL);
298 needcopy = (caps->refcount > 1);
301 new = gst_caps_copy (caps);
302 gst_caps_unref (caps);
310 * @caps: the caps to get the name from
312 * Get the name of a GstCaps structure.
314 * Returns: the name of the caps
317 gst_caps_get_name (GstCaps *caps)
319 g_return_val_if_fail (caps != NULL, NULL);
321 return (const gchar *)caps->name;
326 * @caps: the caps to set the name to
327 * @name: the name to set
329 * Set the name of a caps.
332 gst_caps_set_name (GstCaps *caps, const gchar *name)
334 g_return_if_fail (caps != NULL);
339 caps->name = g_strdup (name);
344 * @caps: the caps to get the mime type from
346 * Get the mime type of the caps as a string.
348 * Returns: the mime type of the caps
351 gst_caps_get_mime (GstCaps *caps)
355 g_return_val_if_fail (caps != NULL, NULL);
357 type = gst_type_find_by_id (caps->id);
362 return "unknown/unknown";
367 * @caps: the caps to set the mime type to
368 * @mime: the mime type to attach to the caps
370 * Set the mime type of the caps as a string.
373 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
375 g_return_if_fail (caps != NULL);
376 g_return_if_fail (mime != NULL);
378 caps->id = get_type_for_mime (mime);
382 * gst_caps_get_type_id:
383 * @caps: the caps to get the type id from
385 * Get the type id of the caps.
387 * Returns: the type id of the caps
390 gst_caps_get_type_id (GstCaps *caps)
392 g_return_val_if_fail (caps != NULL, 0);
398 * gst_caps_set_type_id:
399 * @caps: the caps to set the type id to
400 * @type_id: the type id to set
402 * Set the type id of the caps.
405 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
407 g_return_if_fail (caps != NULL);
413 * gst_caps_set_props:
414 * @caps: the caps to attach the properties to
415 * @props: the properties to attach
417 * Set the properties to the given caps.
419 * Returns: the new caps structure
422 gst_caps_set_props (GstCaps *caps, GstProps *props)
424 g_return_val_if_fail (caps != NULL, caps);
425 g_return_val_if_fail (props != NULL, caps);
426 g_return_val_if_fail (caps->properties == NULL, caps);
428 caps->properties = props;
434 * gst_caps_get_props:
435 * @caps: the caps to get the properties from
437 * Get the properties of the given caps.
439 * Returns: the properties of the caps
442 gst_caps_get_props (GstCaps *caps)
444 g_return_val_if_fail (caps != NULL, NULL);
446 return caps->properties;
452 * @...: more capabilities
454 * chains the given capabilities
456 * Returns: the new capability
459 gst_caps_chain (GstCaps *caps, ...)
461 GstCaps *orig = caps;
464 va_start (var_args, caps);
469 toadd = va_arg (var_args, GstCaps*);
470 gst_caps_append (caps, toadd);
482 * @capstoadd: the capability to append
484 * Appends a capability to the existing capability.
486 * Returns: the new capability
489 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
491 GstCaps *orig = caps;
493 if (caps == NULL || caps == capstoadd)
499 caps->next = capstoadd;
507 * @capstoadd: a capabilty to prepend
509 * prepend the capability to the list of capabilities
511 * Returns: the new capability
514 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
516 GstCaps *orig = capstoadd;
518 if (capstoadd == NULL)
521 g_return_val_if_fail (caps != capstoadd, caps);
523 while (capstoadd->next) {
524 capstoadd = capstoadd->next;
526 capstoadd->next = caps;
532 * gst_caps_get_by_name:
534 * @name: the name of the capability to get
536 * Get the capability with the given name from this
537 * chain of capabilities.
539 * Returns: the first capability in the chain with the
543 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
545 g_return_val_if_fail (caps != NULL, NULL);
546 g_return_val_if_fail (name != NULL, NULL);
549 if (!strcmp (caps->name, name))
558 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
560 if (fromcaps->id != tocaps->id) {
561 GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)",
562 gst_type_find_by_id (fromcaps->id)->mime,
563 gst_type_find_by_id (tocaps->id)->mime);
567 if (tocaps->properties) {
568 if (fromcaps->properties) {
569 return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
572 GST_DEBUG (GST_CAT_CAPS,"no source caps");
577 /* assume it accepts everything */
578 GST_DEBUG (GST_CAT_CAPS,"no caps");
584 * gst_caps_check_compatibility:
585 * @fromcaps: a capabilty
586 * @tocaps: a capabilty
588 * Checks whether two capabilities are compatible.
590 * Returns: TRUE if compatible, FALSE otherwise
593 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
595 if (fromcaps == NULL) {
596 if (tocaps == NULL) {
597 GST_DEBUG (GST_CAT_CAPS,"no caps");
601 GST_DEBUG (GST_CAT_CAPS,"no source but destination caps");
606 if (tocaps == NULL) {
607 GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps");
613 GstCaps *destcaps = tocaps;
616 if (gst_caps_check_compatibility_func (fromcaps, destcaps))
619 destcaps = destcaps->next;
621 fromcaps = fromcaps->next;
627 gst_caps_intersect_func (GstCaps *caps1, GstCaps *caps2)
629 GstCaps *result = NULL;
632 if (caps1->id != caps2->id) {
633 GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)",
634 gst_type_find_by_id (caps1->id)->mime,
635 gst_type_find_by_id (caps2->id)->mime);
639 if (caps1->properties == NULL) {
640 return gst_caps_ref (caps2);
642 if (caps2->properties == NULL) {
643 return gst_caps_ref (caps1);
646 props = gst_props_intersect (caps1->properties, caps2->properties);
648 result = gst_caps_new_id ("intersect", caps1->id, props);
655 * gst_caps_intersect:
656 * @caps1: a capabilty
657 * @caps2: a capabilty
659 * Make the intersection between two caps.
661 * Returns: The intersection of the two caps or NULL if the intersection
665 gst_caps_intersect (GstCaps *caps1, GstCaps *caps2)
667 GstCaps *result = NULL, *walk = NULL;
670 GST_DEBUG (GST_CAT_CAPS, "first caps is NULL, return other caps");
671 return gst_caps_copy (caps2);
674 GST_DEBUG (GST_CAT_CAPS, "second caps is NULL, return other caps");
675 return gst_caps_copy (caps1);
679 GstCaps *othercaps = caps2;
682 GstCaps *intersection;
684 intersection = gst_caps_intersect_func (caps1, othercaps);
688 walk = result = intersection;
691 walk = walk->next = intersection;
694 othercaps = othercaps->next;
703 * gst_caps_normalize:
706 * Make the normalisation of the caps. This will return a new caps
707 * that is equivalent to the input caps with the exception that all
708 * lists are unrolled. This function is useful when you want to iterate
711 * Returns: The normalisation of the caps.
714 gst_caps_normalize (GstCaps *caps)
716 GstCaps *result = NULL, *walk = caps;
724 proplist = gst_props_normalize (caps->properties);
725 if (proplist && g_list_next (proplist) == NULL) {
727 walk = result = caps;
729 walk = walk->next = caps;
735 GstProps *props = (GstProps *) proplist->data;
736 GstCaps *newcaps = gst_caps_new_id (caps->name, caps->id, props);
739 walk = result = newcaps;
741 walk = walk->next = newcaps;
743 proplist = g_list_next (proplist);
751 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
753 * gst_caps_save_thyself:
754 * @caps: a capabilty to save
755 * @parent: the parent XML node pointer
757 * Save the capability into an XML representation.
759 * Returns: a new XML node pointer
762 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
765 xmlNodePtr subsubtree;
768 subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
770 xmlNewChild (subtree, NULL, "name", caps->name);
771 xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
772 if (caps->properties) {
773 subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
775 gst_props_save_thyself (caps->properties, subsubtree);
785 * gst_caps_load_thyself:
786 * @parent: the parent XML node pointer
788 * Load a new caps from the XML representation.
790 * Returns: a new capability
793 gst_caps_load_thyself (xmlNodePtr parent)
795 GstCaps *result = NULL;
796 xmlNodePtr field = parent->xmlChildrenNode;
799 if (!strcmp (field->name, "capscomp")) {
800 xmlNodePtr subfield = field->xmlChildrenNode;
804 g_mutex_lock (_gst_caps_chunk_lock);
805 caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
806 g_mutex_unlock (_gst_caps_chunk_lock);
813 if (!strcmp (subfield->name, "name")) {
814 caps->name = xmlNodeGetContent (subfield);
816 if (!strcmp (subfield->name, "type")) {
817 content = xmlNodeGetContent (subfield);
818 caps->id = get_type_for_mime (content);
821 else if (!strcmp (subfield->name, "properties")) {
822 caps->properties = gst_props_load_thyself (subfield);
825 subfield = subfield->next;
827 result = gst_caps_append (result, caps);
835 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */