2 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * @short_description: Structure describing sets of media formats
22 * @see_also: #GstStructure
32 #include "gst_private.h"
35 /* #define DEBUG_REFCOUNT */
37 #define CAPS_POISON(caps) G_STMT_START{ \
39 GstCaps *_newcaps = gst_caps_copy (caps); \
40 gst_caps_unref(caps); \
44 #define STRUCTURE_POISON(structure) G_STMT_START{ \
46 GstStructure *_newstruct = gst_structure_copy (structure); \
47 gst_structure_free(structure); \
48 structure = _newstruct; \
51 #define IS_WRITABLE(caps) \
52 (g_atomic_int_get (&(caps)->refcount) == 1)
55 static void gst_caps_transform_to_string (const GValue * src_value,
57 static gboolean gst_caps_from_string_inplace (GstCaps * caps,
58 const gchar * string);
59 static GstCaps *gst_caps_copy_conditional (GstCaps * src);
62 gst_caps_get_type (void)
64 static GType gst_caps_type = 0;
67 gst_caps_type = g_boxed_type_register_static ("GstCaps",
68 (GBoxedCopyFunc) gst_caps_copy_conditional,
69 (GBoxedFreeFunc) gst_caps_unref);
71 g_value_register_transform_func (gst_caps_type,
72 G_TYPE_STRING, gst_caps_transform_to_string);
78 /* creation/deletion */
83 * Creates a new #GstCaps that is empty. That is, the returned
84 * #GstCaps contains no media formats.
85 * Caller is responsible for unreffing the returned caps.
87 * Returns: the new #GstCaps
90 gst_caps_new_empty (void)
92 GstCaps *caps = g_new0 (GstCaps, 1);
94 g_atomic_int_inc (&caps->refcount);
95 caps->type = GST_TYPE_CAPS;
96 caps->structs = g_ptr_array_new ();
99 GST_CAT_LOG (GST_CAT_CAPS, "created caps %p", caps);
108 * Creates a new #GstCaps that indicates that it is compatible with
111 * Returns: the new #GstCaps
114 gst_caps_new_any (void)
116 GstCaps *caps = gst_caps_new_empty ();
118 caps->flags = GST_CAPS_FLAGS_ANY;
124 * gst_caps_new_simple:
125 * @media_type: the media type of the structure
126 * @fieldname: first field to set
127 * @...: additional arguments
129 * Creates a new #GstCaps that contains one #GstStructure. The
130 * structure is defined by the arguments, which have the same format
131 * as @gst_structure_new().
132 * Caller is responsible for unreffing the returned caps.
134 * Returns: the new #GstCaps
137 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
140 GstStructure *structure;
143 caps = gst_caps_new_empty ();
145 va_start (var_args, fieldname);
146 structure = gst_structure_new_valist (media_type, fieldname, var_args);
149 gst_caps_append_structure (caps, structure);
156 * @struct1: the first structure to add
157 * @...: additional structures to add
159 * Creates a new #GstCaps and adds all the structures listed as
160 * arguments. The list must be NULL-terminated. The structures
161 * are not copied; the returned #GstCaps owns the structures.
163 * Returns: the new #GstCaps
166 gst_caps_new_full (GstStructure * struct1, ...)
171 va_start (var_args, struct1);
172 caps = gst_caps_new_full_valist (struct1, var_args);
179 * gst_caps_new_full_valist:
180 * @structure: the first structure to add
181 * @var_args: additional structures to add
183 * Creates a new #GstCaps and adds all the structures listed as
184 * arguments. The list must be NULL-terminated. The structures
185 * are not copied; the returned #GstCaps owns the structures.
187 * Returns: the new #GstCaps
190 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
194 caps = gst_caps_new_empty ();
197 gst_caps_append_structure (caps, structure);
198 structure = va_arg (var_args, GstStructure *);
206 * @caps: the #GstCaps to copy
208 * Creates a new #GstCaps as a copy of the old @caps. The new caps will have a
209 * refcount of 1, owned by the caller. The structures are copied as well.
211 * Note that this function is the semantic equivalent of a gst_caps_ref()
212 * followed by a gst_caps_make_writable(). If you only want to hold on to a
213 * reference to the data, you should use gst_caps_ref().
215 * When you are finished with the caps, call gst_caps_unref() on it.
217 * Returns: the new #GstCaps
220 gst_caps_copy (const GstCaps * caps)
223 GstStructure *structure;
226 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
228 newcaps = gst_caps_new_empty ();
229 newcaps->flags = caps->flags;
231 for (i = 0; i < caps->structs->len; i++) {
232 structure = gst_caps_get_structure (caps, i);
233 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
240 _gst_caps_free (GstCaps * caps)
242 GstStructure *structure;
245 /* The refcount must be 0, but since we're only called by gst_caps_unref,
246 * don't bother testing. */
248 for (i = 0; i < caps->structs->len; i++) {
249 structure = (GstStructure *) gst_caps_get_structure (caps, i);
250 gst_structure_set_parent_refcount (structure, NULL);
251 gst_structure_free (structure);
253 g_ptr_array_free (caps->structs, TRUE);
255 memset (caps, 0xff, sizeof (GstCaps));
261 * gst_caps_make_writable:
262 * @caps: the #GstCaps to make writable
264 * Returns a writable copy of @caps.
266 * If there is only one reference count on @caps, the caller must be the owner,
267 * and so this function will return the caps object unchanged. If on the other
268 * hand there is more than one reference on the object, a new caps object will
269 * be returned. The caller's reference on @caps will be removed, and instead the
270 * caller will own a reference to the returned object.
272 * In short, this function unrefs the caps in the argument and refs the caps
273 * that it returns. Don't access the argument after calling this function. See
274 * also: gst_caps_ref().
276 * Returns: the same #GstCaps object.
279 gst_caps_make_writable (GstCaps * caps)
283 g_return_val_if_fail (caps != NULL, NULL);
285 /* we are the only instance reffing this caps */
286 if (g_atomic_int_get (&caps->refcount) == 1)
290 copy = gst_caps_copy (caps);
291 gst_caps_unref (caps);
298 * @caps: the #GstCaps to reference
300 * Add a reference to a #GstCaps object.
302 * From this point on, until the caller calls gst_caps_unref() or
303 * gst_caps_make_writable(), it is guaranteed that the caps object will not
304 * change. This means its structures won't change, etc. To use a #GstCaps
305 * object, you must always have a refcount on it -- either the one made
306 * implicitly by gst_caps_new(), or via taking one explicitly with this
309 * Returns: the same #GstCaps object.
312 gst_caps_ref (GstCaps * caps)
314 g_return_val_if_fail (caps != NULL, NULL);
316 #ifdef DEBUG_REFCOUNT
317 GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
318 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) + 1);
320 g_return_val_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0, NULL);
322 g_atomic_int_inc (&caps->refcount);
329 * @caps: the #GstCaps to unref
331 * Unref a #GstCaps and and free all its structures and the
332 * structures' values when the refcount reaches 0.
335 gst_caps_unref (GstCaps * caps)
337 g_return_if_fail (caps != NULL);
339 #ifdef DEBUG_REFCOUNT
340 GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
341 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) - 1);
344 g_return_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0);
346 /* if we ended up with the refcount at zero, free the caps */
347 if (g_atomic_int_dec_and_test (&caps->refcount)) {
348 _gst_caps_free (caps);
353 * gst_static_caps_get:
354 * @static_caps: the #GstStaticCaps to convert
356 * Converts a #GstStaticCaps to a #GstCaps.
358 * Returns: A pointer to the #GstCaps. Unref after usage. Since the
359 * core holds an additional ref to the returned caps,
360 * use gst_caps_make_writable() on the returned caps to modify it.
363 gst_static_caps_get (GstStaticCaps * static_caps)
365 GstCaps *caps = (GstCaps *) static_caps;
368 if (caps->type == 0) {
369 if (static_caps->string == NULL) {
370 g_warning ("static caps is NULL");
374 caps->type = GST_TYPE_CAPS;
375 /* initialize the caps to a refcount of 1 so the caps can be writable... */
376 gst_atomic_int_set (&caps->refcount, 1);
377 caps->structs = g_ptr_array_new ();
378 ret = gst_caps_from_string_inplace (caps, static_caps->string);
381 g_critical ("Could not convert static caps \"%s\"", static_caps->string);
384 /* ref the caps, makes it not writable */
392 static GstStructure *
393 gst_caps_remove_and_get_structure (GstCaps * caps, guint idx)
395 /* don't use index_fast, gst_caps_simplify relies on the order */
396 GstStructure *s = g_ptr_array_remove_index (caps->structs, idx);
398 gst_structure_set_parent_refcount (s, NULL);
404 * @caps1: the #GstCaps that will be appended to
405 * @caps2: the #GstCaps to append
407 * Appends the structures contained in @caps2 to @caps1. The structures in
408 * @caps2 are not copied -- they are transferred to @caps1, and then @caps2 is
409 * freed. If either caps is ANY, the resulting caps will be ANY.
412 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
414 GstStructure *structure;
417 g_return_if_fail (GST_IS_CAPS (caps1));
418 g_return_if_fail (GST_IS_CAPS (caps2));
419 g_return_if_fail (IS_WRITABLE (caps1));
420 g_return_if_fail (IS_WRITABLE (caps2));
425 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2)) {
426 /* FIXME: this leaks */
427 caps1->flags |= GST_CAPS_FLAGS_ANY;
428 for (i = caps2->structs->len - 1; i >= 0; i--) {
429 structure = gst_caps_remove_and_get_structure (caps2, i);
430 gst_structure_free (structure);
433 int len = caps2->structs->len;
435 for (i = 0; i < len; i++) {
436 structure = gst_caps_remove_and_get_structure (caps2, 0);
437 gst_caps_append_structure (caps1, structure);
440 gst_caps_unref (caps2); /* guaranteed to free it */
444 * gst_caps_append_structure:
445 * @caps: the #GstCaps that will be appended to
446 * @structure: the #GstStructure to append
448 * Appends @structure to @caps. The structure is not copied; @caps
449 * becomes the owner of @structure.
452 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
454 g_return_if_fail (GST_IS_CAPS (caps));
455 g_return_if_fail (IS_WRITABLE (caps));
458 g_return_if_fail (structure->parent_refcount == NULL);
461 STRUCTURE_POISON (structure);
464 gst_structure_set_parent_refcount (structure, &caps->refcount);
465 g_ptr_array_add (caps->structs, structure);
470 * gst_caps_remove_structure:
471 * @caps: the #GstCaps to remove from
472 * @idx: Index of the structure to remove
474 * removes the stucture with the given index from the list of structures
475 * contained in @caps.
478 gst_caps_remove_structure (GstCaps * caps, guint idx)
480 GstStructure *structure;
482 g_return_if_fail (caps != NULL);
483 g_return_if_fail (idx <= gst_caps_get_size (caps));
484 g_return_if_fail (IS_WRITABLE (caps));
486 structure = gst_caps_remove_and_get_structure (caps, idx);
487 gst_structure_free (structure);
491 * gst_caps_split_one:
494 * This function is not implemented.
499 gst_caps_split_one (GstCaps * caps)
502 g_critical ("unimplemented");
511 * Gets the number of structures contained in @caps.
513 * Returns: the number of structures that @caps contains
516 gst_caps_get_size (const GstCaps * caps)
518 g_return_val_if_fail (GST_IS_CAPS (caps), 0);
520 return caps->structs->len;
524 * gst_caps_get_structure:
526 * @index: the index of the structure
528 * Finds the structure in @caps that has the index @index, and
531 * WARNING: This function takes a const GstCaps *, but returns a
532 * non-const GstStructure *. This is for programming convenience --
533 * the caller should be aware that structures inside a constant
534 * @GstCaps should not be modified.
536 * Returns: a pointer to the #GstStructure corresponding to @index
539 gst_caps_get_structure (const GstCaps * caps, int index)
541 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
542 g_return_val_if_fail (index >= 0, NULL);
543 g_return_val_if_fail (index < caps->structs->len, NULL);
545 return g_ptr_array_index (caps->structs, index);
550 * @caps: the @GstCaps to copy
551 * @nth: the nth structure to copy
553 * Creates a new @GstCaps and appends a copy of the nth structure
554 * contained in @caps.
556 * Returns: the new @GstCaps
559 gst_caps_copy_nth (const GstCaps * caps, gint nth)
562 GstStructure *structure;
564 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
566 newcaps = gst_caps_new_empty ();
567 newcaps->flags = caps->flags;
569 if (caps->structs->len > nth) {
570 structure = gst_caps_get_structure (caps, nth);
571 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
579 * @caps: the @GstCaps to truncate
581 * Destructively discard all but the first structure from @caps. Useful when
582 * fixating. @caps must be writable.
585 gst_caps_truncate (GstCaps * caps)
589 g_return_if_fail (GST_IS_CAPS (caps));
590 g_return_if_fail (IS_WRITABLE (caps));
592 i = caps->structs->len - 1;
595 gst_caps_remove_structure (caps, i--);
599 * gst_caps_set_simple:
600 * @caps: the @GstCaps to set
601 * @field: first field to set
602 * @...: additional parameters
604 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
605 * only has one structure. The arguments must be passed in the same
606 * manner as @gst_structure_set(), and be NULL-terminated.
609 gst_caps_set_simple (GstCaps * caps, char *field, ...)
611 GstStructure *structure;
614 g_return_if_fail (GST_IS_CAPS (caps));
615 g_return_if_fail (caps->structs->len == 1);
616 g_return_if_fail (IS_WRITABLE (caps));
618 structure = gst_caps_get_structure (caps, 0);
620 va_start (var_args, field);
621 gst_structure_set_valist (structure, field, var_args);
626 * gst_caps_set_simple_valist:
627 * @caps: the @GstCaps to copy
628 * @field: first field to set
629 * @varargs: additional parameters
631 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
632 * only has one structure. The arguments must be passed in the same
633 * manner as @gst_structure_set(), and be NULL-terminated.
636 gst_caps_set_simple_valist (GstCaps * caps, char *field, va_list varargs)
638 GstStructure *structure;
640 g_return_if_fail (GST_IS_CAPS (caps));
641 g_return_if_fail (caps->structs->len != 1);
642 g_return_if_fail (IS_WRITABLE (caps));
644 structure = gst_caps_get_structure (caps, 0);
646 gst_structure_set_valist (structure, field, varargs);
653 * @caps: the @GstCaps to test
655 * Determines if @caps represents any media format.
657 * Returns: TRUE if @caps represents any format.
660 gst_caps_is_any (const GstCaps * caps)
662 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
664 return (caps->flags & GST_CAPS_FLAGS_ANY);
669 * @caps: the @GstCaps to test
671 * Determines if @caps represents no media formats.
673 * Returns: TRUE if @caps represents no formats.
676 gst_caps_is_empty (const GstCaps * caps)
678 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
680 if (caps->flags & GST_CAPS_FLAGS_ANY)
683 return (caps->structs == NULL) || (caps->structs->len == 0);
687 gst_caps_is_fixed_foreach (GQuark field_id, const GValue * value,
690 return gst_value_is_fixed (value);
695 * @caps: the @GstCaps to test
697 * Fixed @GstCaps describe exactly one format, that is, they have exactly
698 * one structure, and each field in the structure describes a fixed type.
699 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
701 * Returns: TRUE if @caps is fixed
704 gst_caps_is_fixed (const GstCaps * caps)
706 GstStructure *structure;
708 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
710 if (caps->structs->len != 1)
713 structure = gst_caps_get_structure (caps, 0);
715 return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
719 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
722 GstStructure *struct1 = (GstStructure *) data;
723 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
727 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
735 * gst_caps_is_equal_fixed:
736 * @caps1: the #GstCaps to test
737 * @caps2: the #GstCaps to test
739 * Tests if two #GstCaps are equal. This function only works on fixed
742 * Returns: TRUE if the arguments represent the same format
745 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
747 GstStructure *struct1, *struct2;
749 g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
750 g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
752 struct1 = gst_caps_get_structure (caps1, 0);
753 struct2 = gst_caps_get_structure (caps2, 0);
755 if (struct1->name != struct2->name) {
758 if (struct1->fields->len != struct2->fields->len) {
762 return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
767 * gst_caps_is_always_compatible:
768 * @caps1: the #GstCaps to test
769 * @caps2: the #GstCaps to test
771 * A given #GstCaps structure is always compatible with another if
772 * every media format that is in the first is also contained in the
773 * second. That is, @caps1 is a subset of @caps2.
775 * Returns: TRUE if @caps1 is a subset of @caps2.
778 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
780 g_return_val_if_fail (GST_IS_CAPS (caps1), FALSE);
781 g_return_val_if_fail (GST_IS_CAPS (caps2), FALSE);
783 return gst_caps_is_subset (caps1, caps2);
787 * gst_caps_is_subset:
788 * @subset: a #GstCaps
789 * @superset: a potentially greater #GstCaps
791 * Checks if all caps represented by @subset are also represented by @superset
792 * <note>This function does not work reliably if optional properties for caps
793 * are included on one caps and omitted on the other.</note>
795 * Returns: TRUE if @subset is a subset of @superset
798 gst_caps_is_subset (const GstCaps * subset, const GstCaps * superset)
803 g_return_val_if_fail (subset != NULL, FALSE);
804 g_return_val_if_fail (superset != NULL, FALSE);
806 if (gst_caps_is_empty (subset) || gst_caps_is_any (superset))
808 if (gst_caps_is_any (subset) || gst_caps_is_empty (superset))
811 caps = gst_caps_subtract (subset, superset);
812 ret = gst_caps_is_empty (caps);
813 gst_caps_unref (caps);
820 * @caps2: another #GstCaps
822 * Checks if the given caps represent the same set of caps.
823 * <note>This function does not work reliably if optional properties for caps
824 * are included on one caps and omitted on the other.</note>
826 * Returns: TRUE if both caps are equal
829 gst_caps_is_equal (const GstCaps * caps1, const GstCaps * caps2)
831 g_return_val_if_fail (caps1 != NULL, FALSE);
832 g_return_val_if_fail (caps2 != NULL, FALSE);
834 if (gst_caps_is_fixed (caps1) && gst_caps_is_fixed (caps2))
835 return gst_caps_is_equal_fixed (caps1, caps2);
837 return gst_caps_is_subset (caps1, caps2) && gst_caps_is_subset (caps2, caps1);
843 const GstStructure *intersect;
849 gst_caps_structure_intersect_field (GQuark id, const GValue * val1,
852 IntersectData *idata = (IntersectData *) data;
853 GValue dest_value = { 0 };
854 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
857 gst_structure_id_set_value (idata->dest, id, val1);
858 } else if (idata->first_run) {
859 if (gst_value_intersect (&dest_value, val1, val2)) {
860 gst_structure_id_set_value (idata->dest, id, &dest_value);
861 g_value_unset (&dest_value);
870 static GstStructure *
871 gst_caps_structure_intersect (const GstStructure * struct1,
872 const GstStructure * struct2)
876 g_return_val_if_fail (struct1 != NULL, NULL);
877 g_return_val_if_fail (struct2 != NULL, NULL);
879 if (struct1->name != struct2->name)
882 data.dest = gst_structure_id_empty_new (struct1->name);
883 data.intersect = struct2;
884 data.first_run = TRUE;
885 if (!gst_structure_foreach ((GstStructure *) struct1,
886 gst_caps_structure_intersect_field, &data))
889 data.intersect = struct1;
890 data.first_run = FALSE;
891 if (!gst_structure_foreach ((GstStructure *) struct2,
892 gst_caps_structure_intersect_field, &data))
898 gst_structure_free (data.dest);
903 static GstStructure *
904 gst_caps_structure_union (const GstStructure * struct1,
905 const GstStructure * struct2)
909 const GstStructureField *field1;
910 const GstStructureField *field2;
913 /* FIXME this doesn't actually work */
915 if (struct1->name != struct2->name)
918 dest = gst_structure_id_empty_new (struct1->name);
920 for (i = 0; i < struct1->fields->len; i++) {
921 GValue dest_value = { 0 };
923 field1 = GST_STRUCTURE_FIELD (struct1, i);
924 field2 = gst_structure_id_get_field (struct2, field1->name);
926 if (field2 == NULL) {
929 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
930 gst_structure_set_value (dest, g_quark_to_string (field1->name),
933 ret = gst_value_compare (&field1->value, &field2->value);
945 * gst_caps_intersect:
946 * @caps1: a #GstCaps to intersect
947 * @caps2: a #GstCaps to intersect
949 * Creates a new #GstCaps that contains all the formats that are common
950 * to both @caps1 and @caps2.
952 * Returns: the new #GstCaps
955 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
958 GstStructure *struct1;
959 GstStructure *struct2;
961 GstStructure *istruct;
963 g_return_val_if_fail (GST_IS_CAPS (caps1), NULL);
964 g_return_val_if_fail (GST_IS_CAPS (caps2), NULL);
966 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)) {
967 return gst_caps_new_empty ();
969 if (gst_caps_is_any (caps1))
970 return gst_caps_copy (caps2);
971 if (gst_caps_is_any (caps2))
972 return gst_caps_copy (caps1);
974 dest = gst_caps_new_empty ();
976 /* run zigzag on top line then right line, this preserves the caps order
977 * much better than a simple loop.
979 * This algorithm zigzags over the caps structures as demonstrated in
980 * the folowing matrix:
988 * First we iterate over the caps1 structures (top line) intersecting
989 * the structures diagonally down, then we iterate over the caps2
992 for (i = 0; i < caps1->structs->len + caps2->structs->len - 1; i++) {
993 /* caps1 index goes from 0 to caps1->structs->len-1 */
994 j = MIN (i, caps1->structs->len - 1);
995 /* caps2 index stays 0 until i reaches caps1->structs->len, then it counts
996 * up from 1 to caps2->structs->len - 1 */
999 /* now run the diagonal line, end condition is the left or bottom
1001 while (k < caps2->structs->len && j >= 0) {
1002 struct1 = gst_caps_get_structure (caps1, j);
1003 struct2 = gst_caps_get_structure (caps2, k);
1005 istruct = gst_caps_structure_intersect (struct1, struct2);
1007 gst_caps_append_structure (dest, istruct);
1008 /* move down left */
1013 gst_caps_do_simplify (dest);
1019 const GstStructure *subtract_from;
1026 gst_caps_structure_subtract_field (GQuark field_id, const GValue * value,
1029 SubtractionEntry *e = user_data;
1030 GValue subtraction = { 0, };
1031 const GValue *other;
1032 GstStructure *structure;
1034 other = gst_structure_id_get_value (e->subtract_from, field_id);
1038 if (!gst_value_subtract (&subtraction, other, value))
1040 if (gst_value_compare (&subtraction, other) == GST_VALUE_EQUAL) {
1041 g_value_unset (&subtraction);
1044 structure = gst_structure_copy (e->subtract_from);
1045 gst_structure_id_set_value (structure, field_id, &subtraction);
1046 g_value_unset (&subtraction);
1047 e->put_into = g_slist_prepend (e->put_into, structure);
1053 gst_caps_structure_subtract (GSList ** into, const GstStructure * minuend,
1054 const GstStructure * subtrahend)
1059 e.subtract_from = minuend;
1062 ret = gst_structure_foreach ((GstStructure *) subtrahend,
1063 gst_caps_structure_subtract_field, &e);
1069 for (walk = e.put_into; walk; walk = g_slist_next (walk)) {
1070 gst_structure_free (walk->data);
1072 g_slist_free (e.put_into);
1078 * gst_caps_subtract:
1079 * @minuend: #GstCaps to substract from
1080 * @subtrahend: #GstCaps to substract
1082 * Subtracts the @subtrahend from the @minuend.
1083 * <note>This function does not work reliably if optional properties for caps
1084 * are included on one caps and omitted on the other.</note>
1086 * Returns: the resulting caps
1089 gst_caps_subtract (const GstCaps * minuend, const GstCaps * subtrahend)
1094 GstCaps *dest = NULL, *src;
1096 g_return_val_if_fail (minuend != NULL, NULL);
1097 g_return_val_if_fail (subtrahend != NULL, NULL);
1099 if (gst_caps_is_empty (minuend) || gst_caps_is_any (subtrahend)) {
1100 return gst_caps_new_empty ();
1102 if (gst_caps_is_empty (subtrahend))
1103 return gst_caps_copy (minuend);
1105 /* FIXME: Do we want this here or above?
1106 The reason we need this is that there is no definition about what
1107 ANY means for specific types, so it's not possible to reduce ANY partially
1108 You can only remove everything or nothing and that is done above.
1109 Note: there's a test that checks this behaviour. */
1110 g_return_val_if_fail (!gst_caps_is_any (minuend), NULL);
1111 g_assert (subtrahend->structs->len > 0);
1113 src = gst_caps_copy (minuend);
1114 for (i = 0; i < subtrahend->structs->len; i++) {
1115 sub = gst_caps_get_structure (subtrahend, i);
1117 gst_caps_unref (src);
1120 dest = gst_caps_new_empty ();
1121 for (j = 0; j < src->structs->len; j++) {
1122 min = gst_caps_get_structure (src, j);
1123 if (gst_structure_get_name_id (min) == gst_structure_get_name_id (sub)) {
1126 if (gst_caps_structure_subtract (&list, min, sub)) {
1129 for (walk = list; walk; walk = g_slist_next (walk)) {
1130 gst_caps_append_structure (dest, (GstStructure *) walk->data);
1132 g_slist_free (list);
1134 gst_caps_append_structure (dest, gst_structure_copy (min));
1137 gst_caps_append_structure (dest, gst_structure_copy (min));
1140 if (gst_caps_is_empty (dest)) {
1141 gst_caps_unref (src);
1146 gst_caps_unref (src);
1147 gst_caps_do_simplify (dest);
1153 * @caps1: a #GstCaps to union
1154 * @caps2: a #GstCaps to union
1156 * Creates a new #GstCaps that contains all the formats that are in
1157 * either @caps1 and @caps2.
1159 * Returns: the new #GstCaps
1162 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
1167 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2))
1168 return gst_caps_new_any ();
1170 dest1 = gst_caps_copy (caps1);
1171 dest2 = gst_caps_copy (caps2);
1172 gst_caps_append (dest1, dest2);
1174 gst_caps_do_simplify (dest1);
1178 typedef struct _NormalizeForeach
1181 GstStructure *structure;
1186 gst_caps_normalize_foreach (GQuark field_id, const GValue * value, gpointer ptr)
1188 NormalizeForeach *nf = (NormalizeForeach *) ptr;
1192 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1193 for (i = 1; i < gst_value_list_get_size (value); i++) {
1194 const GValue *v = gst_value_list_get_value (value, i);
1195 GstStructure *structure = gst_structure_copy (nf->structure);
1197 gst_structure_id_set_value (structure, field_id, v);
1198 gst_caps_append_structure (nf->caps, structure);
1201 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
1202 gst_structure_id_set_value (nf->structure, field_id, &val);
1203 g_value_unset (&val);
1211 * gst_caps_normalize:
1212 * @caps: a #GstCaps to normalize
1214 * Creates a new #GstCaps that represents the same set of formats as
1215 * @caps, but contains no lists. Each list is expanded into separate
1218 * Returns: the new #GstCaps
1221 gst_caps_normalize (const GstCaps * caps)
1223 NormalizeForeach nf;
1227 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
1229 newcaps = gst_caps_copy (caps);
1232 for (i = 0; i < newcaps->structs->len; i++) {
1233 nf.structure = gst_caps_get_structure (newcaps, i);
1235 while (!gst_structure_foreach (nf.structure,
1236 gst_caps_normalize_foreach, &nf));
1243 gst_caps_compare_structures (gconstpointer one, gconstpointer two)
1246 const GstStructure *struct1 = *((const GstStructure **) one);
1247 const GstStructure *struct2 = *((const GstStructure **) two);
1249 /* FIXME: this orders aphabetically, but ordering the quarks might be faster
1250 So what's the best way? */
1251 ret = strcmp (gst_structure_get_name (struct1),
1252 gst_structure_get_name (struct2));
1256 return gst_structure_n_fields (struct2) - gst_structure_n_fields (struct1);
1260 * gst_caps_simplify:
1261 * @caps: a #GstCaps to simplify
1263 * Creates a new #GstCaps that represents the same set of formats as
1264 * @caps, but simpler. Component structures that are identical are
1265 * merged. Component structures that have ranges or lists that can
1266 * be merged are also merged.
1268 * Returns: the new #GstCaps
1271 gst_caps_simplify (const GstCaps * caps)
1275 g_return_val_if_fail (caps != NULL, NULL);
1277 ret = gst_caps_copy (caps);
1278 gst_caps_do_simplify (ret);
1287 GstStructure *compare;
1292 gst_caps_structure_figure_out_union (GQuark field_id, const GValue * value,
1295 UnionField *u = user_data;
1296 const GValue *val = gst_structure_id_get_value (u->compare, field_id);
1300 g_value_unset (&u->value);
1303 if (gst_value_compare (val, value) == GST_VALUE_EQUAL)
1306 g_value_unset (&u->value);
1310 gst_value_union (&u->value, val, value);
1315 gst_caps_structure_simplify (GstStructure ** result,
1316 const GstStructure * simplify, GstStructure * compare)
1319 UnionField field = { 0, {0,}, NULL };
1321 /* try to subtract to get a real subset */
1322 if (gst_caps_structure_subtract (&list, simplify, compare)) {
1323 switch (g_slist_length (list)) {
1328 *result = list->data;
1329 g_slist_free (list);
1335 for (walk = list; walk; walk = g_slist_next (walk)) {
1336 gst_structure_free (walk->data);
1338 g_slist_free (list);
1344 /* try to union both structs */
1345 field.compare = compare;
1346 if (gst_structure_foreach ((GstStructure *) simplify,
1347 gst_caps_structure_figure_out_union, &field)) {
1348 gboolean ret = FALSE;
1350 /* now we know all of simplify's fields are the same in compare
1351 * but at most one field: field.name */
1352 if (G_IS_VALUE (&field.value)) {
1353 if (gst_structure_n_fields (simplify) == gst_structure_n_fields (compare)) {
1354 gst_structure_id_set_value (compare, field.name, &field.value);
1358 g_value_unset (&field.value);
1359 } else if (gst_structure_n_fields (simplify) <=
1360 gst_structure_n_fields (compare)) {
1361 /* compare is just more specific, will be optimized away later */
1362 /* FIXME: do this here? */
1363 GST_LOG ("found a case that will be optimized later.");
1365 gchar *one = gst_structure_to_string (simplify);
1366 gchar *two = gst_structure_to_string (compare);
1369 ("caps mismatch: structures %s and %s claim to be possible to unify, but aren't",
1381 gst_caps_switch_structures (GstCaps * caps, GstStructure * old,
1382 GstStructure * new, gint i)
1384 gst_structure_set_parent_refcount (old, NULL);
1385 gst_structure_free (old);
1386 gst_structure_set_parent_refcount (new, &caps->refcount);
1387 g_ptr_array_index (caps->structs, i) = new;
1391 * gst_caps_do_simplify:
1392 * @caps: a #GstCaps to simplify
1394 * Modifies the given @caps inplace into a representation that represents the
1395 * same set of formats, but in a simpler form. Component structures that are
1396 * identical are merged. Component structures that have values that can be
1397 * merged are also merged.
1399 * Returns: TRUE, if the caps could be simplified
1402 gst_caps_do_simplify (GstCaps * caps)
1404 GstStructure *simplify, *compare, *result = NULL;
1406 gboolean changed = FALSE;
1408 g_return_val_if_fail (caps != NULL, FALSE);
1409 g_return_val_if_fail (IS_WRITABLE (caps), FALSE);
1411 if (gst_caps_get_size (caps) < 2)
1414 g_ptr_array_sort (caps->structs, gst_caps_compare_structures);
1416 start = caps->structs->len - 1;
1417 for (i = caps->structs->len - 1; i >= 0; i--) {
1418 simplify = gst_caps_get_structure (caps, i);
1419 if (gst_structure_get_name_id (simplify) !=
1420 gst_structure_get_name_id (gst_caps_get_structure (caps, start)))
1422 for (j = start; j >= 0; j--) {
1425 compare = gst_caps_get_structure (caps, j);
1426 if (gst_structure_get_name_id (simplify) !=
1427 gst_structure_get_name_id (compare)) {
1430 if (gst_caps_structure_simplify (&result, simplify, compare)) {
1432 gst_caps_switch_structures (caps, simplify, result, i);
1435 gst_caps_remove_structure (caps, i);
1447 /* gst_caps_do_simplify (caps); */
1451 #ifndef GST_DISABLE_LOADSAVE
1453 * gst_caps_save_thyself:
1454 * @caps: a #GstCaps structure
1455 * @parent: a XML parent node
1457 * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1459 * Returns: a XML node pointer
1462 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1464 char *s = gst_caps_to_string (caps);
1466 xmlNewChild (parent, NULL, (xmlChar *) "caps", (xmlChar *) s);
1472 * gst_caps_load_thyself:
1473 * @parent: a XML node
1475 * Creates a #GstCaps from its XML serialization.
1477 * Returns: a new #GstCaps structure
1480 gst_caps_load_thyself (xmlNodePtr parent)
1482 if (strcmp ("caps", (char *) parent->name) == 0) {
1483 return gst_caps_from_string ((gchar *) xmlNodeGetContent (parent));
1494 * @caps: a pointer to #GstCaps
1495 * @newcaps: a #GstCaps to replace *caps
1497 * Replaces *caps with @newcaps. Frees the #GstCaps in the location
1498 * pointed to by @caps, if applicable, then modifies @caps to point to
1502 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1506 #if 0 /* disable this, since too many plugins rely on undefined behavior */
1507 #ifdef USE_POISONING
1508 //if (newcaps) CAPS_POISON (newcaps);
1514 gst_caps_ref (newcaps);
1519 gst_caps_unref (oldcaps);
1523 * gst_caps_to_string:
1526 * Converts @caps to a string representation. This string representation
1527 * can be converted back to a #GstCaps by #gst_caps_from_string.
1529 * Returns: a newly allocated string representing @caps.
1532 gst_caps_to_string (const GstCaps * caps)
1537 /* NOTE: This function is potentially called by the debug system,
1538 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1539 * should be careful to avoid recursion. This includes any functions
1540 * called by gst_caps_to_string. In particular, calls should
1541 * not use the GST_PTR_FORMAT extension. */
1544 return g_strdup ("NULL");
1546 if (gst_caps_is_any (caps)) {
1547 return g_strdup ("ANY");
1549 if (gst_caps_is_empty (caps)) {
1550 return g_strdup ("EMPTY");
1553 s = g_string_new ("");
1554 for (i = 0; i < caps->structs->len; i++) {
1555 GstStructure *structure;
1559 g_string_append (s, "; ");
1561 structure = gst_caps_get_structure (caps, i);
1562 sstr = gst_structure_to_string (structure);
1563 g_string_append (s, sstr);
1567 return g_string_free (s, FALSE);
1571 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1573 GstStructure *structure;
1576 g_return_val_if_fail (string, FALSE);
1577 if (strcmp ("ANY", string) == 0) {
1578 caps->flags = GST_CAPS_FLAGS_ANY;
1581 if (strcmp ("EMPTY", string) == 0) {
1585 structure = gst_structure_from_string (string, &s);
1586 if (structure == NULL) {
1589 gst_caps_append_structure (caps, structure);
1593 while (g_ascii_isspace (*s))
1595 structure = gst_structure_from_string (s, &s);
1596 if (structure == NULL) {
1599 gst_caps_append_structure (caps, structure);
1600 while (g_ascii_isspace (*s))
1612 * gst_caps_from_string:
1613 * @string: a string to convert to #GstCaps
1615 * Converts @caps from a string representation.
1617 * Returns: a newly allocated #GstCaps
1620 gst_caps_from_string (const gchar * string)
1624 caps = gst_caps_new_empty ();
1625 if (gst_caps_from_string_inplace (caps, string)) {
1628 gst_caps_unref (caps);
1634 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1636 g_return_if_fail (G_IS_VALUE (src_value));
1637 g_return_if_fail (G_IS_VALUE (dest_value));
1638 g_return_if_fail (G_VALUE_HOLDS (src_value, GST_TYPE_CAPS));
1639 g_return_if_fail (G_VALUE_HOLDS (dest_value, G_TYPE_STRING)
1640 || G_VALUE_HOLDS (dest_value, G_TYPE_POINTER));
1642 dest_value->data[0].v_pointer =
1643 gst_caps_to_string (src_value->data[0].v_pointer);
1647 gst_caps_copy_conditional (GstCaps * src)
1650 return gst_caps_ref (src);