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 #include "gst_private.h"
27 #include "gstmemchunk.h"
30 #ifndef GST_DISABLE_TRACE
31 /* #define GST_WITH_ALLOC_TRACE */
34 static GstAllocTrace *_gst_caps_trace;
37 static GstMemChunk *_gst_caps_chunk;
41 extern GstProps * __gst_props_from_string_func (gchar *s, gchar **end, gboolean caps);
42 extern gboolean __gst_props_parse_string (gchar *r, gchar **end, gchar **next);
44 /* transform functions */
45 static void gst_caps_transform_to_string (const GValue *src_value, GValue *dest_value);
47 static void gst_caps_destroy (GstCaps *caps);
51 gst_caps_transform_to_string (const GValue *src_value, GValue *dest_value)
53 GstCaps *caps = g_value_peek_pointer (src_value);
54 dest_value->data[0].v_pointer = gst_caps_to_string (caps);
58 * caps: the caps to convert to a string
60 * Converts a #GstCaps into a readable format. This is mainly intended for
61 * debugging purposes. You have to free the string using g_free.
62 * A string converted with #gst_caps_to_string can always be converted back to
63 * its caps representation using #gst_caps_from_string.
65 * Returns: A newly allocated string
68 gst_caps_to_string (GstCaps *caps)
73 g_return_val_if_fail (caps != NULL, NULL);
75 result = g_string_new ("");
79 GValue value = { 0, }; /* the important thing is that value.type = 0 */
81 g_string_append_printf (result, "\"%s\"", gst_caps_get_mime (caps));
83 if (caps->properties) {
84 g_value_init (&value, GST_TYPE_PROPS);
85 g_value_set_boxed (&value, caps->properties);
86 props = g_strdup_value_contents (&value);
88 g_value_unset (&value);
89 g_string_append (result, ", ");
90 g_string_append (result, props);
96 g_string_append (result, "; ");
99 g_string_free (result, FALSE);
104 gst_caps_from_string_func (gchar *r)
107 GstCaps *caps, *append;
108 GstProps *props = NULL;
111 if (!__gst_props_parse_string (r, &w, &r)) goto error;
113 if (*r == '\0') goto found;
114 if (*r++ != ',') goto error;
115 while (g_ascii_isspace (*r)) r++;
117 props = __gst_props_from_string_func (r, &r, TRUE);
118 if (!props) goto error;
123 gst_props_unref (props);
126 caps = gst_caps_new ("parsed caps", mime, props);
130 while (g_ascii_isspace (*r)) r++;
133 while (g_ascii_isspace (*r)) r++;
134 append = gst_caps_from_string_func (r);
136 gst_caps_unref (caps);
139 gst_caps_append (caps, append);
148 * gst_caps_from_string:
149 * str: the str to convert into caps
151 * Tries to convert a string into a #GstCaps. This is mainly intended for
152 * debugging purposes. The returned caps are floating.
154 * Returns: A floating caps or NULL if the string couldn't be converted
157 gst_caps_from_string (gchar *str)
161 g_return_val_if_fail (str != NULL, NULL);
164 caps = gst_caps_from_string_func (s);
170 _gst_caps_initialize (void)
172 _gst_caps_chunk = gst_mem_chunk_new ("GstCaps",
173 sizeof (GstCaps), sizeof (GstCaps) * 256,
176 _gst_caps_type = g_boxed_type_register_static ("GstCaps",
177 (GBoxedCopyFunc) gst_caps_ref,
178 (GBoxedFreeFunc) gst_caps_unref);
180 g_value_register_transform_func (_gst_caps_type, G_TYPE_STRING,
181 gst_caps_transform_to_string);
183 #ifndef GST_DISABLE_TRACE
184 _gst_caps_trace = gst_alloc_trace_register (GST_CAPS_TRACE_NAME);
189 gst_caps_get_type (void)
191 return _gst_caps_type;
195 get_type_for_mime (const gchar *mime)
199 typeid = gst_type_find_by_mime (mime);
201 GstTypeDefinition definition;
202 GstTypeFactory *factory;
204 definition.name = "capstype";
205 definition.mime = g_strdup (mime);
206 definition.exts = NULL;
207 definition.typefindfunc = NULL;
209 factory = gst_type_factory_new (&definition);
211 typeid = gst_type_register (factory);
218 * @name: the name of this capability
219 * @mime: the mime type to attach to the capability
220 * @props: the properties to add to this capability
222 * Create a new capability with the given mime type and properties.
224 * Returns: a new capability
227 gst_caps_new (const gchar *name, const gchar *mime, GstProps *props)
229 g_return_val_if_fail (mime != NULL, NULL);
231 return gst_caps_new_id (name, get_type_for_mime (mime), props);
236 * @name: the name of this capability
237 * @id: the id of the mime type
238 * @props: the properties to add to this capability
240 * Create a new capability with the given mime typeid and properties.
242 * Returns: a new capability
245 gst_caps_new_id (const gchar *name, const guint16 id, GstProps *props)
249 caps = gst_mem_chunk_alloc (_gst_caps_chunk);
250 #ifndef GST_DISABLE_TRACE
251 gst_alloc_trace_new (_gst_caps_trace, caps);
254 GST_DEBUG (GST_CAT_CAPS, "new %p", caps);
256 gst_props_ref (props);
257 gst_props_sink (props);
259 caps->name = g_strdup (name);
261 caps->properties = props;
264 GST_CAPS_FLAG_SET (caps, GST_CAPS_FLOATING);
266 if (props && !GST_PROPS_IS_FIXED (props))
267 GST_CAPS_FLAG_UNSET (caps, GST_CAPS_FIXED);
269 GST_CAPS_FLAG_SET (caps, GST_CAPS_FIXED);
276 * @oldcaps: the caps to take replace
277 * @newcaps: the caps to take replace
279 * Replace the pointer to the caps, doing proper
283 gst_caps_replace (GstCaps **oldcaps, GstCaps *newcaps)
285 if (*oldcaps != newcaps) {
286 if (newcaps) gst_caps_ref (newcaps);
287 if (*oldcaps) gst_caps_unref (*oldcaps);
294 * gst_caps_replace_sink:
295 * @oldcaps: the caps to take replace
296 * @newcaps: the caps to take replace
298 * Replace the pointer to the caps and take ownership.
301 gst_caps_replace_sink (GstCaps **oldcaps, GstCaps *newcaps)
303 gst_caps_replace (oldcaps, newcaps);
304 gst_caps_sink (newcaps);
309 * @caps: the caps to destroy
311 * Frees the memory used by this caps structure and all
312 * the chained caps and properties.
315 gst_caps_destroy (GstCaps *caps)
324 GST_DEBUG (GST_CAT_CAPS, "destroy %p", caps);
326 gst_props_unref (caps->properties);
329 #ifndef GST_DISABLE_TRACE
330 gst_alloc_trace_free (_gst_caps_trace, caps);
332 gst_mem_chunk_free (_gst_caps_chunk, caps);
335 gst_caps_unref (next);
340 * @caps: the caps to print out
341 * @label: a label to put on the printout, or NULL
343 * Print out the contents of the caps structure. Useful for debugging.
346 gst_caps_debug (GstCaps *caps, const gchar *label)
348 GST_DEBUG_ENTER ("caps debug: %s", label);
349 if (caps && caps->refcount == 0) {
350 g_warning ("Warning: refcount of caps %s is 0", label);
354 GST_DEBUG (GST_CAT_CAPS, "caps: %p %s %s (%sfixed) (refcount %d) %s",
355 caps, caps->name, gst_caps_get_mime (caps),
356 GST_CAPS_IS_FIXED (caps) ? "" : "NOT ", caps->refcount,
357 GST_CAPS_IS_FLOATING (caps) ? "FLOATING" : "");
359 if (caps->properties) {
360 gst_props_debug (caps->properties);
363 GST_DEBUG (GST_CAT_CAPS, "no properties");
368 GST_DEBUG_LEAVE ("caps debug");
373 * @caps: the caps to unref
375 * Decrease the refcount of this caps structure,
376 * destroying it when the refcount is 0
378 * Returns: caps or NULL if the refcount reached 0
381 gst_caps_unref (GstCaps *caps)
388 g_return_val_if_fail (caps->refcount > 0, NULL);
390 GST_DEBUG (GST_CAT_CAPS, "unref %p (%d->%d) %d",
391 caps, caps->refcount, caps->refcount-1, GST_CAPS_FLAGS (caps));
394 zero = (caps->refcount == 0);
397 gst_caps_destroy (caps);
405 * @caps: the caps to ref
407 * Increase the refcount of this caps structure
409 * Returns: the caps with the refcount incremented
412 gst_caps_ref (GstCaps *caps)
417 g_return_val_if_fail (caps->refcount > 0, NULL);
419 GST_DEBUG (GST_CAT_CAPS, "ref %p (%d->%d) %d",
420 caps, caps->refcount, caps->refcount+1, GST_CAPS_FLAGS (caps));
429 * @caps: the caps to take ownership of
431 * Take ownership of a GstCaps
434 gst_caps_sink (GstCaps *caps)
439 if (GST_CAPS_IS_FLOATING (caps)) {
440 GST_DEBUG (GST_CAT_CAPS, "sink %p", caps);
442 GST_CAPS_FLAG_UNSET (caps, GST_CAPS_FLOATING);
443 gst_caps_unref (caps);
449 * @caps: the caps to copy
451 * Copies the caps, not copying any chained caps.
453 * Returns: a floating copy of the GstCaps structure.
456 gst_caps_copy_1 (GstCaps *caps)
463 newcaps = gst_caps_new_id (
466 gst_props_copy (caps->properties));
473 * @caps: the caps to copy
477 * Returns: a floating copy of the GstCaps structure.
480 gst_caps_copy (GstCaps *caps)
482 GstCaps *new = NULL, *walk = NULL;
487 newcaps = gst_caps_copy_1 (caps);
490 new = walk = newcaps;
493 walk = walk->next = newcaps;
502 * gst_caps_copy_on_write:
503 * @caps: the caps to copy
505 * Copies the caps if the refcount is greater than 1
507 * Returns: a pointer to a GstCaps strcuture that can
508 * be safely written to.
511 gst_caps_copy_on_write (GstCaps *caps)
516 g_return_val_if_fail (caps != NULL, NULL);
518 needcopy = (caps->refcount > 1);
521 new = gst_caps_copy (caps);
522 gst_caps_unref (caps);
530 * @caps: the caps to get the name from
532 * Get the name of a GstCaps structure.
534 * Returns: the name of the caps
537 gst_caps_get_name (GstCaps *caps)
539 g_return_val_if_fail (caps != NULL, NULL);
541 return (const gchar *)caps->name;
546 * @caps: the caps to set the name to
547 * @name: the name to set
549 * Set the name of a caps.
552 gst_caps_set_name (GstCaps *caps, const gchar *name)
554 g_return_if_fail (caps != NULL);
557 caps->name = g_strdup (name);
562 * @caps: the caps to get the mime type from
564 * Get the mime type of the caps as a string.
566 * Returns: the mime type of the caps
569 gst_caps_get_mime (GstCaps *caps)
573 g_return_val_if_fail (caps != NULL, NULL);
575 type = gst_type_find_by_id (caps->id);
580 return "unknown/unknown";
585 * @caps: the caps to set the mime type to
586 * @mime: the mime type to attach to the caps
588 * Set the mime type of the caps as a string.
591 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
593 g_return_if_fail (caps != NULL);
594 g_return_if_fail (mime != NULL);
596 caps->id = get_type_for_mime (mime);
600 * gst_caps_get_type_id:
601 * @caps: the caps to get the type id from
603 * Get the type id of the caps.
605 * Returns: the type id of the caps
608 gst_caps_get_type_id (GstCaps *caps)
610 g_return_val_if_fail (caps != NULL, 0);
616 * gst_caps_set_type_id:
617 * @caps: the caps to set the type id to
618 * @type_id: the type id to set
620 * Set the type id of the caps.
623 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
625 g_return_if_fail (caps != NULL);
631 * gst_caps_set_props:
632 * @caps: the caps to attach the properties to
633 * @props: the properties to attach
635 * Set the properties to the given caps.
637 * Returns: the new caps structure
640 gst_caps_set_props (GstCaps *caps, GstProps *props)
642 g_return_val_if_fail (caps != NULL, caps);
644 gst_props_replace_sink (&caps->properties, props);
646 if (props && !GST_PROPS_IS_FIXED (props))
647 GST_CAPS_FLAG_UNSET (caps, GST_CAPS_FIXED);
649 GST_CAPS_FLAG_SET (caps, GST_CAPS_FIXED);
655 * gst_caps_get_props:
656 * @caps: the caps to get the properties from
658 * Get the properties of the given caps.
660 * Returns: the properties of the caps
663 gst_caps_get_props (GstCaps *caps)
665 g_return_val_if_fail (caps != NULL, NULL);
667 return caps->properties;
672 * @caps: the caps to query
674 * Get the next caps of this chained caps.
676 * Returns: the next caps or NULL if the chain ended.
679 gst_caps_next (GstCaps *caps)
690 * @...: more capabilities
692 * chains the given capabilities
694 * Returns: the new capability
697 gst_caps_chain (GstCaps *caps, ...)
699 GstCaps *orig = caps;
702 va_start (var_args, caps);
707 toadd = va_arg (var_args, GstCaps*);
708 gst_caps_append (caps, toadd);
720 * @capstoadd: the capability to append
722 * Appends a capability to the existing capability.
724 * Returns: the new capability
727 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
729 GstCaps *orig = caps;
731 if (caps == NULL || caps == capstoadd)
737 gst_caps_replace_sink (&caps->next, capstoadd);
745 * @capstoadd: a capabilty to prepend
747 * prepend the capability to the list of capabilities
749 * Returns: the new capability
752 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
754 GstCaps *orig = capstoadd;
756 if (capstoadd == NULL)
759 g_return_val_if_fail (caps != capstoadd, caps);
761 while (capstoadd->next) {
762 capstoadd = capstoadd->next;
764 gst_caps_replace_sink (&capstoadd->next, caps);
770 * gst_caps_get_by_name:
772 * @name: the name of the capability to get
774 * Get the capability with the given name from this
775 * chain of capabilities.
777 * Returns: the first capability in the chain with the
781 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
783 g_return_val_if_fail (caps != NULL, NULL);
784 g_return_val_if_fail (name != NULL, NULL);
787 if (!strcmp (caps->name, name))
796 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
798 if (fromcaps->id != tocaps->id) {
799 GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)",
800 gst_type_find_by_id (fromcaps->id)->mime,
801 gst_type_find_by_id (tocaps->id)->mime);
805 if (tocaps->properties) {
806 if (fromcaps->properties) {
807 return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
810 GST_DEBUG (GST_CAT_CAPS,"no source caps");
815 /* assume it accepts everything */
816 GST_DEBUG (GST_CAT_CAPS,"no caps");
822 * gst_caps_is_always_compatible:
823 * @fromcaps: a #GstCaps capability to check compatibility of.
824 * @tocaps: the #GstCaps capability to check compatibility with.
826 * Checks if a link is always possible from fromcaps to tocaps, for all
827 * possible capabilities.
829 * Returns: TRUE if compatible under all circumstances, FALSE otherwise.
832 gst_caps_is_always_compatible (GstCaps *fromcaps, GstCaps *tocaps)
834 if (fromcaps == NULL) {
835 if (tocaps == NULL) {
836 /* if both are NULL, they can always link. Think filesrc ! filesink */
837 GST_DEBUG (GST_CAT_CAPS, "both caps NULL, compatible");
841 /* if source caps are NULL, it could be sending anything, so the
842 * destination can't know if it can accept this. Think filesrc ! mad */
843 GST_DEBUG (GST_CAT_CAPS, "source caps NULL, not guaranteed compatible");
848 if (tocaps == NULL) {
849 /* if the dest caps are NULL, the element can accept anything, always,
850 * so they're compatible by definition. Think mad ! filesink */
851 GST_DEBUG (GST_CAT_CAPS,"destination caps NULL");
857 GstCaps *destcaps = tocaps;
858 /* assume caps is incompatible */
859 gboolean compat = FALSE;
861 while (destcaps && !compat) {
862 if (gst_caps_check_compatibility_func (fromcaps, destcaps)) {
865 destcaps = destcaps->next;
870 fromcaps = fromcaps->next;
876 gst_caps_intersect_func (GstCaps *caps1, GstCaps *caps2)
878 GstCaps *result = NULL;
881 if (caps1->id != caps2->id) {
882 GST_DEBUG (GST_CAT_CAPS, "mime types differ (%s to %s)",
883 gst_type_find_by_id (caps1->id)->mime,
884 gst_type_find_by_id (caps2->id)->mime);
888 if (caps1->properties == NULL) {
889 return gst_caps_ref (caps2);
891 if (caps2->properties == NULL) {
892 return gst_caps_ref (caps1);
895 props = gst_props_intersect (caps1->properties, caps2->properties);
897 result = gst_caps_new_id ("intersect", caps1->id, props);
898 gst_caps_ref (result);
899 gst_caps_sink (result);
906 * gst_caps_intersect:
907 * @caps1: a capability
908 * @caps2: a capability
910 * Make the intersection between two caps.
912 * Returns: The intersection of the two caps or NULL if the intersection
913 * is empty. unref the caps after use.
916 gst_caps_intersect (GstCaps *caps1, GstCaps *caps2)
918 GstCaps *result = NULL, *walk = NULL;
920 /* printing the name is not useful here since caps can be chained */
921 GST_DEBUG (GST_CAT_CAPS, "intersecting caps %p and %p", caps1, caps2);
924 GST_DEBUG (GST_CAT_CAPS, "first caps is NULL, return other caps");
925 return gst_caps_ref (caps2);
928 GST_DEBUG (GST_CAT_CAPS, "second caps is NULL, return other caps");
929 return gst_caps_ref (caps1);
933 if (caps1 == caps2) {
934 return gst_caps_ref (caps1);
938 GstCaps *othercaps = caps2;
941 GstCaps *intersection;
943 intersection = gst_caps_intersect_func (caps1, othercaps);
947 walk = result = intersection;
950 walk = walk->next = intersection;
953 othercaps = othercaps->next;
962 gst_caps_union (GstCaps *caps1, GstCaps *caps2)
964 GstCaps *result = NULL;
966 /* printing the name is not useful here since caps can be chained */
967 GST_DEBUG (GST_CAT_CAPS, "making union of caps %p and %p", caps1, caps2);
970 GST_DEBUG (GST_CAT_CAPS, "first caps is NULL, return other caps");
971 return gst_caps_ref (caps2);
974 GST_DEBUG (GST_CAT_CAPS, "second caps is NULL, return other caps");
975 return gst_caps_ref (caps1);
982 * gst_caps_normalize:
985 * Make the normalisation of the caps. This will return a new caps
986 * that is equivalent to the input caps with the exception that all
987 * lists are unrolled. This function is useful when you want to iterate
988 * the caps. unref the caps after use.
990 * Returns: The normalisation of the caps. Unref after usage.
993 gst_caps_normalize (GstCaps *caps)
995 GstCaps *result = NULL, *walk;
1000 GST_DEBUG (GST_CAT_CAPS, "normalizing caps %p ", caps);
1007 proplist = gst_props_normalize (caps->properties);
1009 GstProps *props = (GstProps *) proplist->data;
1010 GstCaps *newcaps = gst_caps_new_id (caps->name, caps->id, props);
1012 gst_caps_ref (newcaps);
1013 gst_caps_sink (newcaps);
1016 walk = result = newcaps;
1018 walk = walk->next = newcaps;
1020 proplist = g_list_next (proplist);
1027 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
1029 * gst_caps_save_thyself:
1030 * @caps: a capabilty to save
1031 * @parent: the parent XML node pointer
1033 * Save the capability into an XML representation.
1035 * Returns: a new XML node pointer
1038 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
1041 xmlNodePtr subsubtree;
1044 subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
1046 xmlNewChild (subtree, NULL, "name", caps->name);
1047 xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
1048 if (caps->properties) {
1049 subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
1051 gst_props_save_thyself (caps->properties, subsubtree);
1061 * gst_caps_load_thyself:
1062 * @parent: the parent XML node pointer
1064 * Load a new caps from the XML representation.
1066 * Returns: a new capability
1069 gst_caps_load_thyself (xmlNodePtr parent)
1071 GstCaps *result = NULL;
1072 xmlNodePtr field = parent->xmlChildrenNode;
1075 if (!strcmp (field->name, "capscomp")) {
1076 xmlNodePtr subfield = field->xmlChildrenNode;
1079 GstCapsFlags fixed = GST_CAPS_FIXED;
1081 caps = gst_mem_chunk_alloc0 (_gst_caps_chunk);
1082 #ifndef GST_DISABLE_TRACE
1083 gst_alloc_trace_new (_gst_caps_trace, caps);
1087 GST_CAPS_FLAG_SET (caps, GST_CAPS_FLOATING);
1091 if (!strcmp (subfield->name, "name")) {
1092 caps->name = xmlNodeGetContent (subfield);
1094 if (!strcmp (subfield->name, "type")) {
1095 content = xmlNodeGetContent (subfield);
1096 caps->id = get_type_for_mime (content);
1099 else if (!strcmp (subfield->name, "properties")) {
1100 GstProps *props = gst_props_load_thyself (subfield);
1102 gst_props_ref (props);
1103 gst_props_sink (props);
1104 caps->properties = props;
1106 fixed &= (GST_PROPS_IS_FIXED (caps->properties) ? GST_CAPS_FIXED : 0 );
1109 subfield = subfield->next;
1111 GST_CAPS_FLAG_SET (caps, fixed);
1113 result = gst_caps_append (result, caps);
1115 field = field->next;
1121 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */