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"
29 #include "gstpropsprivate.h"
31 static GMemChunk *_gst_caps_chunk;
32 static GMutex *_gst_caps_chunk_lock;
35 _gst_caps_initialize (void)
37 _gst_caps_chunk = g_mem_chunk_new ("GstCaps",
38 sizeof (GstCaps), sizeof (GstCaps) * 256,
40 _gst_caps_chunk_lock = g_mutex_new ();
44 get_type_for_mime (const gchar *mime)
48 typeid = gst_type_find_by_mime (mime);
50 GstTypeDefinition definition;
51 GstTypeFactory *factory;
53 definition.name = "capstype";
54 definition.mime = g_strdup (mime);
55 definition.exts = NULL;
56 definition.typefindfunc = NULL;
58 factory = gst_typefactory_new (&definition);
60 typeid = gst_type_register (factory);
67 * @name: the name of this capability
68 * @mime: the mime type to attach to the capability
69 * @props: the properties to add to this capability
71 * Create a new capability with the given mime typei and properties.
73 * Returns: a new capability
76 gst_caps_new (const gchar *name, const gchar *mime, GstProps *props)
78 g_return_val_if_fail (mime != NULL, NULL);
80 return gst_caps_new_id (name, get_type_for_mime (mime), props);
85 * @name: the name of this capability
86 * @id: the id of the mime type
87 * @props: the properties to add to this capability
89 * Create a new capability with the given mime typeid and properties.
91 * Returns: a new capability
94 gst_caps_new_id (const gchar *name, const guint16 id, GstProps *props)
98 g_mutex_lock (_gst_caps_chunk_lock);
99 caps = g_mem_chunk_alloc (_gst_caps_chunk);
100 g_mutex_unlock (_gst_caps_chunk_lock);
102 caps->name = g_strdup (name);
104 caps->properties = props;
107 caps->lock = g_mutex_new ();
109 caps->fixed = props->fixed;
118 * @caps: the caps to destroy
120 * Frees the memory used by this caps structure and all
121 * the chained caps and properties.
124 gst_caps_destroy (GstCaps *caps)
131 GST_CAPS_LOCK (caps);
133 GST_CAPS_UNLOCK (caps);
135 g_mutex_free (caps->lock);
136 gst_props_unref (caps->properties);
138 g_mutex_lock (_gst_caps_chunk_lock);
139 g_mem_chunk_free (_gst_caps_chunk, caps);
140 g_mutex_unlock (_gst_caps_chunk_lock);
143 gst_caps_unref (next);
148 * @caps: the caps to print out
149 * @label: a label to put on the printout, or NULL
151 * Print out the contents of the caps structure. Useful for debugging.
154 gst_caps_debug (GstCaps *caps, const gchar *label)
156 GST_DEBUG_ENTER ("caps debug: %s", label);
158 GST_DEBUG (GST_CAT_CAPS, "caps: %p %s %s\n", caps, caps->name, gst_caps_get_mime (caps));
160 if (caps->properties) {
161 gst_props_debug (caps->properties);
164 GST_DEBUG (GST_CAT_CAPS, "no properties\n");
169 GST_DEBUG_LEAVE ("caps debug");
174 * @caps: the caps to unref
176 * Decrease the refcount of this caps structure,
177 * destroying it when the refcount is 0
179 * Returns: caps or NULL if the refcount reached 0
182 gst_caps_unref (GstCaps *caps)
190 g_return_val_if_fail (caps->refcount > 0, NULL);
192 GST_CAPS_LOCK (caps);
194 zero = (caps->refcount == 0);
196 GST_CAPS_UNLOCK (caps);
199 *next = gst_caps_unref (*next);
202 gst_caps_destroy (caps);
210 * @caps: the caps to ref
212 * Increase the refcount of this caps structure
214 * Returns: the caps with the refcount incremented
217 gst_caps_ref (GstCaps *caps)
219 g_return_val_if_fail (caps != NULL, NULL);
221 GST_CAPS_LOCK (caps);
223 GST_CAPS_UNLOCK (caps);
230 * @caps: the caps to copy
232 * Copies the caps, not copying any chained caps.
234 * Returns: a copy of the GstCaps structure.
237 gst_caps_copy_1 (GstCaps *caps)
244 newcaps = gst_caps_new_id (
247 gst_props_copy (caps->properties));
254 * @caps: the caps to copy
258 * Returns: a copy of the GstCaps structure.
261 gst_caps_copy (GstCaps *caps)
263 GstCaps *new = NULL, *walk = NULL;
268 newcaps = gst_caps_copy_1 (caps);
271 new = walk = newcaps;
274 walk = walk->next = newcaps;
283 * gst_caps_copy_on_write:
284 * @caps: the caps to copy
286 * Copies the caps if the refcount is greater than 1
288 * Returns: a pointer to a GstCaps strcuture that can
289 * be safely written to
292 gst_caps_copy_on_write (GstCaps *caps)
297 g_return_val_if_fail (caps != NULL, NULL);
299 GST_CAPS_LOCK (caps);
300 needcopy = (caps->refcount > 1);
301 GST_CAPS_UNLOCK (caps);
304 new = gst_caps_copy (caps);
305 gst_caps_unref (caps);
313 * @caps: the caps to get the name from
315 * Get the name of a GstCaps structure.
317 * Returns: the name of the caps
320 gst_caps_get_name (GstCaps *caps)
322 g_return_val_if_fail (caps != NULL, NULL);
324 return (const gchar *)caps->name;
329 * @caps: the caps to set the name to
330 * @name: the name to set
332 * Set the name of a caps.
335 gst_caps_set_name (GstCaps *caps, const gchar *name)
337 g_return_if_fail (caps != NULL);
342 caps->name = g_strdup (name);
347 * @caps: the caps to get the mime type from
349 * Get the mime type of the caps as a string.
351 * Returns: the mime type of the caps
354 gst_caps_get_mime (GstCaps *caps)
358 g_return_val_if_fail (caps != NULL, NULL);
360 type = gst_type_find_by_id (caps->id);
365 return "unknown/unknown";
370 * @caps: the caps to set the mime type to
371 * @mime: the mime type to attach to the caps
373 * Set the mime type of the caps as a string.
376 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
378 g_return_if_fail (caps != NULL);
379 g_return_if_fail (mime != NULL);
381 caps->id = get_type_for_mime (mime);
385 * gst_caps_get_type_id:
386 * @caps: the caps to get the type id from
388 * Get the type id of the caps.
390 * Returns: the type id of the caps
393 gst_caps_get_type_id (GstCaps *caps)
395 g_return_val_if_fail (caps != NULL, 0);
401 * gst_caps_set_type_id:
402 * @caps: the caps to set the type id to
403 * @type_id: the type id to set
405 * Set the type id of the caps.
408 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
410 g_return_if_fail (caps != NULL);
416 * gst_caps_set_props:
417 * @caps: the caps to attach the properties to
418 * @props: the properties to attach
420 * Set the properties to the given caps.
422 * Returns: the new caps structure
425 gst_caps_set_props (GstCaps *caps, GstProps *props)
427 g_return_val_if_fail (caps != NULL, caps);
428 g_return_val_if_fail (props != NULL, caps);
429 g_return_val_if_fail (caps->properties == NULL, caps);
431 caps->properties = props;
437 * gst_caps_get_props:
438 * @caps: the caps to get the properties from
440 * Get the properties of the given caps.
442 * Returns: the properties of the caps
445 gst_caps_get_props (GstCaps *caps)
447 g_return_val_if_fail (caps != NULL, NULL);
449 return caps->properties;
455 * @...: more capabilities
457 * chains the given capabilities
459 * Returns: the new capability
462 gst_caps_chain (GstCaps *caps, ...)
464 GstCaps *orig = caps;
467 va_start (var_args, caps);
472 toadd = va_arg (var_args, GstCaps*);
473 gst_caps_append (caps, toadd);
485 * @capstoadd: the capability to append
487 * Appends a capability to the existing capability.
489 * Returns: the new capability
492 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
494 GstCaps *orig = caps;
496 if (caps == NULL || caps == capstoadd)
502 caps->next = capstoadd;
510 * @capstoadd: a capabilty to prepend
512 * prepend the capability to the list of capabilities
514 * Returns: the new capability
517 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
519 GstCaps *orig = capstoadd;
521 if (capstoadd == NULL)
524 g_return_val_if_fail (caps != capstoadd, caps);
526 while (capstoadd->next) {
527 capstoadd = capstoadd->next;
529 capstoadd->next = caps;
535 * gst_caps_get_by_name:
537 * @name: the name of the capability to get
539 * Get the capability with the given name from this
540 * chain of capabilities.
542 * Returns: the first capability in the chain with the
546 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
548 g_return_val_if_fail (caps != NULL, NULL);
549 g_return_val_if_fail (name != NULL, NULL);
552 if (!strcmp (caps->name, name))
561 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
563 if (fromcaps->id != tocaps->id) {
564 GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)\n",
565 gst_type_find_by_id (fromcaps->id)->mime,
566 gst_type_find_by_id (tocaps->id)->mime);
570 if (tocaps->properties) {
571 if (fromcaps->properties) {
572 return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
575 GST_DEBUG (GST_CAT_CAPS,"no source caps\n");
580 /* assume it accepts everything */
581 GST_DEBUG (GST_CAT_CAPS,"no caps\n");
587 * gst_caps_check_compatibility:
588 * @fromcaps: a capabilty
589 * @tocaps: a capabilty
591 * Checks whether two capabilities are compatible.
593 * Returns: TRUE if compatible, FALSE otherwise
596 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
598 if (fromcaps == NULL) {
599 if (tocaps == NULL) {
600 GST_DEBUG (GST_CAT_CAPS,"no caps\n");
604 GST_DEBUG (GST_CAT_CAPS,"no source but destination caps\n");
609 if (tocaps == NULL) {
610 GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps\n");
616 GstCaps *destcaps = tocaps;
619 if (gst_caps_check_compatibility_func (fromcaps, destcaps))
622 destcaps = destcaps->next;
624 fromcaps = fromcaps->next;
630 gst_caps_intersect_func (GstCaps *caps1, GstCaps *caps2)
632 GstCaps *result = NULL;
635 if (caps1->id != caps2->id) {
636 GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)\n",
637 gst_type_find_by_id (caps1->id)->mime,
638 gst_type_find_by_id (caps2->id)->mime);
642 if (caps1->properties == NULL) {
643 return gst_caps_ref (caps2);
645 if (caps2->properties == NULL) {
646 return gst_caps_ref (caps1);
649 props = gst_props_intersect (caps1->properties, caps2->properties);
651 result = gst_caps_new_id ("intersect", caps1->id, props);
658 * gst_caps_intersect:
659 * @caps1: a capabilty
660 * @caps2: a capabilty
662 * Make the intersection between two caps.
664 * Returns: The intersection of the two caps or NULL if the intersection
668 gst_caps_intersect (GstCaps *caps1, GstCaps *caps2)
670 GstCaps *result = NULL, *walk = NULL;
673 GST_DEBUG (GST_CAT_CAPS, "first caps is NULL, return other caps\n");
674 return gst_caps_copy (caps2);
677 GST_DEBUG (GST_CAT_CAPS, "second caps is NULL, return other caps\n");
678 return gst_caps_copy (caps1);
682 GstCaps *othercaps = caps2;
685 GstCaps *intersection;
687 intersection = gst_caps_intersect_func (caps1, othercaps);
691 walk = result = intersection;
694 walk = walk->next = intersection;
697 othercaps = othercaps->next;
706 gst_caps_normalize (GstCaps *caps)
708 GstCaps *result = NULL, *walk = caps;
716 proplist = gst_props_normalize (caps->properties);
717 if (proplist && g_list_next (proplist) == NULL) {
719 walk = result = caps;
721 walk = walk->next = caps;
727 GstProps *props = (GstProps *) proplist->data;
728 GstCaps *newcaps = gst_caps_new_id (caps->name, caps->id, props);
731 walk = result = newcaps;
733 walk = walk->next = newcaps;
735 proplist = g_list_next (proplist);
743 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
745 * gst_caps_save_thyself:
746 * @caps: a capabilty to save
747 * @parent: the parent XML node pointer
749 * Save the capability into an XML representation.
751 * Returns: a new XML node pointer
754 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
757 xmlNodePtr subsubtree;
760 subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
762 xmlNewChild (subtree, NULL, "name", caps->name);
763 xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
764 if (caps->properties) {
765 subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
767 gst_props_save_thyself (caps->properties, subsubtree);
777 * gst_caps_load_thyself:
778 * @parent: the parent XML node pointer
780 * Load a new caps from the XML representation.
782 * Returns: a new capability
785 gst_caps_load_thyself (xmlNodePtr parent)
787 GstCaps *result = NULL;
788 xmlNodePtr field = parent->xmlChildrenNode;
791 if (!strcmp (field->name, "capscomp")) {
792 xmlNodePtr subfield = field->xmlChildrenNode;
796 g_mutex_lock (_gst_caps_chunk_lock);
797 caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
798 g_mutex_unlock (_gst_caps_chunk_lock);
801 caps->lock = g_mutex_new ();
806 if (!strcmp (subfield->name, "name")) {
807 caps->name = xmlNodeGetContent (subfield);
809 if (!strcmp (subfield->name, "type")) {
810 content = xmlNodeGetContent (subfield);
811 caps->id = get_type_for_mime (content);
814 else if (!strcmp (subfield->name, "properties")) {
815 caps->properties = gst_props_load_thyself (subfield);
818 subfield = subfield->next;
820 result = gst_caps_append (result, caps);
828 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */