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);
147 gst_caps_debug (GstCaps *caps)
149 GST_DEBUG_ENTER ("caps debug");
151 GST_DEBUG (GST_CAT_CAPS, "caps: %p %s %s\n", caps, caps->name, gst_caps_get_mime (caps));
153 if (caps->properties) {
154 gst_props_debug (caps->properties);
157 GST_DEBUG (GST_CAT_CAPS, "no properties\n");
162 GST_DEBUG_LEAVE ("caps debug");
167 * @caps: the caps to unref
169 * Decrease the refcount of this caps structure,
170 * destroying it when the refcount is 0
172 * Returns: caps or NULL if the refcount reached 0
175 gst_caps_unref (GstCaps *caps)
183 g_return_val_if_fail (caps->refcount > 0, NULL);
185 GST_CAPS_LOCK (caps);
187 zero = (caps->refcount == 0);
189 GST_CAPS_UNLOCK (caps);
192 *next = gst_caps_unref (*next);
195 gst_caps_destroy (caps);
203 * @caps: the caps to ref
205 * Increase the refcount of this caps structure
207 * Returns: the caps with the refcount incremented
210 gst_caps_ref (GstCaps *caps)
212 g_return_val_if_fail (caps != NULL, NULL);
214 GST_CAPS_LOCK (caps);
216 GST_CAPS_UNLOCK (caps);
223 * @caps: the caps to copy
227 * Returns: a copy of the GstCaps structure.
230 gst_caps_copy (GstCaps *caps)
232 GstCaps *new = NULL, *walk = NULL;
237 newcaps = gst_caps_new_id (
240 gst_props_copy (caps->properties));
243 new = walk = newcaps;
246 walk = walk->next = newcaps;
255 * gst_caps_copy_on_write:
256 * @caps: the caps to copy
258 * Copies the caps if the refcount is greater than 1
260 * Returns: a pointer to a GstCaps strcuture that can
261 * be safely written to
264 gst_caps_copy_on_write (GstCaps *caps)
269 g_return_val_if_fail (caps != NULL, NULL);
271 GST_CAPS_LOCK (caps);
272 needcopy = (caps->refcount > 1);
273 GST_CAPS_UNLOCK (caps);
276 new = gst_caps_copy (caps);
277 gst_caps_unref (caps);
285 * @caps: the caps to get the name from
287 * Get the name of a GstCaps structure.
289 * Returns: the name of the caps
292 gst_caps_get_name (GstCaps *caps)
294 g_return_val_if_fail (caps != NULL, NULL);
296 return (const gchar *)caps->name;
301 * @caps: the caps to set the name to
302 * @name: the name to set
304 * Set the name of a caps.
307 gst_caps_set_name (GstCaps *caps, const gchar *name)
309 g_return_if_fail (caps != NULL);
314 caps->name = g_strdup (name);
319 * @caps: the caps to get the mime type from
321 * Get the mime type of the caps as a string.
323 * Returns: the mime type of the caps
326 gst_caps_get_mime (GstCaps *caps)
330 g_return_val_if_fail (caps != NULL, NULL);
332 type = gst_type_find_by_id (caps->id);
337 return "unknown/unknown";
342 * @caps: the caps to set the mime type to
343 * @mime: the mime type to attach to the caps
345 * Set the mime type of the caps as a string.
348 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
350 g_return_if_fail (caps != NULL);
351 g_return_if_fail (mime != NULL);
353 caps->id = get_type_for_mime (mime);
357 * gst_caps_get_type_id:
358 * @caps: the caps to get the type id from
360 * Get the type id of the caps.
362 * Returns: the type id of the caps
365 gst_caps_get_type_id (GstCaps *caps)
367 g_return_val_if_fail (caps != NULL, 0);
373 * gst_caps_set_type_id:
374 * @caps: the caps to set the type id to
375 * @type_id: the type id to set
377 * Set the type id of the caps.
380 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
382 g_return_if_fail (caps != NULL);
388 * gst_caps_set_props:
389 * @caps: the caps to attach the properties to
390 * @props: the properties to attach
392 * Set the properties to the given caps.
394 * Returns: the new caps structure
397 gst_caps_set_props (GstCaps *caps, GstProps *props)
399 g_return_val_if_fail (caps != NULL, caps);
400 g_return_val_if_fail (props != NULL, caps);
401 g_return_val_if_fail (caps->properties == NULL, caps);
403 caps->properties = props;
409 * gst_caps_get_props:
410 * @caps: the caps to get the properties from
412 * Get the properties of the given caps.
414 * Returns: the properties of the caps
417 gst_caps_get_props (GstCaps *caps)
419 g_return_val_if_fail (caps != NULL, NULL);
421 return caps->properties;
427 * @...: more capabilities
429 * chains the given capabilities
431 * Returns: the new capability
434 gst_caps_chain (GstCaps *caps, ...)
436 GstCaps *orig = caps;
439 va_start (var_args, caps);
444 toadd = va_arg (var_args, GstCaps*);
445 gst_caps_append (caps, toadd);
457 * @capstoadd: the capability to append
459 * Appends a capability to the existing capability.
461 * Returns: the new capability
464 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
466 GstCaps *orig = caps;
468 g_return_val_if_fail (caps != capstoadd, caps);
476 caps->next = capstoadd;
484 * @capstoadd: a capabilty to prepend
486 * prepend the capability to the list of capabilities
488 * Returns: the new capability
491 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
493 GstCaps *orig = capstoadd;
495 if (capstoadd == NULL)
498 g_return_val_if_fail (caps != capstoadd, caps);
500 while (capstoadd->next) {
501 capstoadd = capstoadd->next;
503 capstoadd->next = caps;
509 * gst_caps_get_by_name:
511 * @name: the name of the capability to get
513 * Get the capability with the given name from this
514 * chain of capabilities.
516 * Returns: the first capability in the chain with the
520 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
522 g_return_val_if_fail (caps != NULL, NULL);
523 g_return_val_if_fail (name != NULL, NULL);
526 if (!strcmp (caps->name, name))
535 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
537 if (fromcaps->id != tocaps->id) {
538 GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)\n",
539 gst_type_find_by_id (fromcaps->id)->mime,
540 gst_type_find_by_id (tocaps->id)->mime);
544 if (tocaps->properties) {
545 if (fromcaps->properties) {
546 return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
549 GST_DEBUG (GST_CAT_CAPS,"no source caps\n");
554 /* assume it accepts everything */
555 GST_DEBUG (GST_CAT_CAPS,"no caps\n");
561 * gst_caps_check_compatibility:
562 * @fromcaps: a capabilty
563 * @tocaps: a capabilty
565 * Checks whether two capabilities are compatible.
567 * Returns: TRUE if compatible, FALSE otherwise
570 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
572 if (fromcaps == NULL) {
573 if (tocaps == NULL) {
574 GST_DEBUG (GST_CAT_CAPS,"no caps\n");
578 GST_DEBUG (GST_CAT_CAPS,"no source but destination caps\n");
583 if (tocaps == NULL) {
584 GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps\n");
590 GstCaps *destcaps = tocaps;
593 if (gst_caps_check_compatibility_func (fromcaps, destcaps))
596 destcaps = destcaps->next;
598 fromcaps = fromcaps->next;
604 gst_caps_intersect_func (GstCaps *caps1, GstCaps *caps2)
606 GstCaps *result = NULL;
609 if (caps1->id != caps2->id) {
610 GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)\n",
611 gst_type_find_by_id (caps1->id)->mime,
612 gst_type_find_by_id (caps2->id)->mime);
616 if (caps1->properties == NULL) {
617 return gst_caps_ref (caps2);
619 if (caps2->properties == NULL) {
620 return gst_caps_ref (caps1);
623 props = gst_props_intersect (caps1->properties, caps2->properties);
625 result = gst_caps_new_id ("intersect", caps1->id, props);
632 * gst_caps_intersect:
633 * @caps1: a capabilty
634 * @caps2: a capabilty
636 * Make the intersection between two caps.
638 * Returns: The intersection of the two caps or NULL if the intersection
642 gst_caps_intersect (GstCaps *caps1, GstCaps *caps2)
644 GstCaps *result = NULL, *walk = NULL;
647 GST_DEBUG (GST_CAT_CAPS, "first caps is NULL, return other caps\n");
648 return gst_caps_copy (caps2);
651 GST_DEBUG (GST_CAT_CAPS, "second caps is NULL, return other caps\n");
652 return gst_caps_copy (caps1);
656 GstCaps *othercaps = caps2;
659 GstCaps *intersection = gst_caps_intersect_func (caps1, othercaps);
663 walk = result = intersection;
666 walk = walk->next = intersection;
669 othercaps = othercaps->next;
678 gst_caps_normalize (GstCaps *caps)
680 GstCaps *result = NULL, *walk = caps;
688 proplist = gst_props_normalize (caps->properties);
689 if (proplist && g_list_next (proplist) == NULL) {
691 walk = result = caps;
693 walk = walk->next = caps;
699 GstProps *props = (GstProps *) proplist->data;
700 GstCaps *newcaps = gst_caps_new_id (caps->name, caps->id, props);
703 walk = result = newcaps;
705 walk = walk->next = newcaps;
707 proplist = g_list_next (proplist);
715 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
717 * gst_caps_save_thyself:
718 * @caps: a capabilty to save
719 * @parent: the parent XML node pointer
721 * Save the capability into an XML representation.
723 * Returns: a new XML node pointer
726 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
729 xmlNodePtr subsubtree;
732 subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
734 xmlNewChild (subtree, NULL, "name", caps->name);
735 xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
736 if (caps->properties) {
737 subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
739 gst_props_save_thyself (caps->properties, subsubtree);
749 * gst_caps_load_thyself:
750 * @parent: the parent XML node pointer
752 * Load a new caps from the XML representation.
754 * Returns: a new capability
757 gst_caps_load_thyself (xmlNodePtr parent)
759 GstCaps *result = NULL;
760 xmlNodePtr field = parent->xmlChildrenNode;
763 if (!strcmp (field->name, "capscomp")) {
764 xmlNodePtr subfield = field->xmlChildrenNode;
768 g_mutex_lock (_gst_caps_chunk_lock);
769 caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
770 g_mutex_unlock (_gst_caps_chunk_lock);
773 caps->lock = g_mutex_new ();
778 if (!strcmp (subfield->name, "name")) {
779 caps->name = xmlNodeGetContent (subfield);
781 if (!strcmp (subfield->name, "type")) {
782 content = xmlNodeGetContent (subfield);
783 caps->id = get_type_for_mime (content);
786 else if (!strcmp (subfield->name, "properties")) {
787 caps->properties = gst_props_load_thyself (subfield);
790 subfield = subfield->next;
792 result = gst_caps_append (result, caps);
800 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */