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.
86 * Returns: the new #GstCaps
89 gst_caps_new_empty (void)
91 GstCaps *caps = g_new0 (GstCaps, 1);
93 g_atomic_int_inc (&caps->refcount);
94 caps->type = GST_TYPE_CAPS;
95 caps->structs = g_ptr_array_new ();
98 GST_CAT_LOG (GST_CAT_CAPS, "created caps %p", caps);
107 * Creates a new #GstCaps that indicates that it is compatible with
110 * Returns: the new #GstCaps
113 gst_caps_new_any (void)
115 GstCaps *caps = gst_caps_new_empty ();
117 caps->flags = GST_CAPS_FLAGS_ANY;
123 * gst_caps_new_simple:
124 * @media_type: the media type of the structure
125 * @fieldname: first field to set
126 * @...: additional arguments
128 * Creates a new #GstCaps that contains one #GstStructure. The
129 * structure is defined by the arguments, which have the same format
130 * as @gst_structure_new().
132 * Returns: the new #GstCaps
135 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
138 GstStructure *structure;
141 caps = gst_caps_new_empty ();
143 va_start (var_args, fieldname);
144 structure = gst_structure_new_valist (media_type, fieldname, var_args);
147 gst_caps_append_structure (caps, structure);
154 * @struct1: the first structure to add
155 * @...: additional structures to add
157 * Creates a new #GstCaps and adds all the structures listed as
158 * arguments. The list must be NULL-terminated. The structures
159 * are not copied; the returned #GstCaps owns the structures.
161 * Returns: the new #GstCaps
164 gst_caps_new_full (GstStructure * struct1, ...)
169 va_start (var_args, struct1);
170 caps = gst_caps_new_full_valist (struct1, var_args);
177 * gst_caps_new_full_valist:
178 * @structure: the first structure to add
179 * @var_args: additional structures to add
181 * Creates a new #GstCaps and adds all the structures listed as
182 * arguments. The list must be NULL-terminated. The structures
183 * are not copied; the returned #GstCaps owns the structures.
185 * Returns: the new #GstCaps
188 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
192 caps = gst_caps_new_empty ();
195 gst_caps_append_structure (caps, structure);
196 structure = va_arg (var_args, GstStructure *);
204 * @caps: the #GstCaps to copy
206 * Creates a new #GstCaps as a copy of the old @caps. The new caps will have a
207 * refcount of 1, owned by the caller. The structures are copied as well.
209 * Note that this function is the semantic equivalent of a gst_caps_ref()
210 * followed by a gst_caps_make_writable(). If you only want to hold on to a
211 * reference to the data, you should use gst_caps_ref().
213 * When you are finished with the caps, call gst_caps_unref() on it.
215 * Returns: the new #GstCaps
218 gst_caps_copy (const GstCaps * caps)
221 GstStructure *structure;
224 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
226 newcaps = gst_caps_new_empty ();
227 newcaps->flags = caps->flags;
229 for (i = 0; i < caps->structs->len; i++) {
230 structure = gst_caps_get_structure (caps, i);
231 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
238 _gst_caps_free (GstCaps * caps)
240 GstStructure *structure;
243 /* The refcount must be 0, but since we're only called by gst_caps_unref,
244 * don't bother testing. */
246 for (i = 0; i < caps->structs->len; i++) {
247 structure = (GstStructure *) gst_caps_get_structure (caps, i);
248 gst_structure_set_parent_refcount (structure, NULL);
249 gst_structure_free (structure);
251 g_ptr_array_free (caps->structs, TRUE);
253 memset (caps, 0xff, sizeof (GstCaps));
259 * gst_caps_make_writable:
260 * @caps: the #GstCaps to make writable
262 * Returns a writable copy of @caps.
264 * If there is only one reference count on @caps, the caller must be the owner,
265 * and so this function will return the caps object unchanged. If on the other
266 * hand there is more than one reference on the object, a new caps object will
267 * be returned. The caller's reference on @caps will be removed, and instead the
268 * caller will own a reference to the returned object.
270 * In short, this function unrefs the caps in the argument and refs the caps
271 * that it returns. Don't access the argument after calling this function. See
272 * also: gst_caps_ref().
274 * Returns: the same #GstCaps object.
277 gst_caps_make_writable (GstCaps * caps)
281 g_return_val_if_fail (caps != NULL, NULL);
283 /* we are the only instance reffing this caps */
284 if (g_atomic_int_get (&caps->refcount) == 1)
288 copy = gst_caps_copy (caps);
289 gst_caps_unref (caps);
296 * @caps: the #GstCaps to reference
298 * Add a reference to a #GstCaps object.
300 * From this point on, until the caller calls gst_caps_unref() or
301 * gst_caps_make_writable(), it is guaranteed that the caps object will not
302 * change. This means its structures won't change, etc. To use a #GstCaps
303 * object, you must always have a refcount on it -- either the one made
304 * implicitly by gst_caps_new(), or via taking one explicitly with this
307 * Returns: the same #GstCaps object.
310 gst_caps_ref (GstCaps * caps)
312 g_return_val_if_fail (caps != NULL, NULL);
314 #ifdef DEBUG_REFCOUNT
315 GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
316 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) + 1);
318 g_return_val_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0, NULL);
320 g_atomic_int_inc (&caps->refcount);
327 * @caps: the #GstCaps to unref
329 * Unref a #GstCaps and and free all its structures and the
330 * structures' values when the refcount reaches 0.
333 gst_caps_unref (GstCaps * caps)
335 g_return_if_fail (caps != NULL);
337 #ifdef DEBUG_REFCOUNT
338 GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
339 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) - 1);
342 g_return_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0);
344 /* if we ended up with the refcount at zero, free the caps */
345 if (g_atomic_int_dec_and_test (&caps->refcount)) {
346 _gst_caps_free (caps);
351 * gst_static_caps_get:
352 * @static_caps: the #GstStaticCaps to convert
354 * Converts a #GstStaticCaps to a #GstCaps.
356 * Returns: A pointer to the #GstCaps. Unref after usage. Since the
357 * core holds an additional ref to the returned caps,
358 * use gst_caps_make_writable() on the returned caps to modify it.
361 gst_static_caps_get (GstStaticCaps * static_caps)
363 GstCaps *caps = (GstCaps *) static_caps;
366 if (caps->type == 0) {
367 if (static_caps->string == NULL) {
368 g_warning ("static caps is NULL");
372 caps->type = GST_TYPE_CAPS;
373 /* initialize the caps to a refcount of 1 so the caps can be writable... */
374 gst_atomic_int_set (&caps->refcount, 1);
375 caps->structs = g_ptr_array_new ();
376 ret = gst_caps_from_string_inplace (caps, static_caps->string);
379 g_critical ("Could not convert static caps \"%s\"", static_caps->string);
382 /* ref the caps, makes it not writable */
390 static GstStructure *
391 gst_caps_remove_and_get_structure (GstCaps * caps, guint idx)
393 /* don't use index_fast, gst_caps_simplify relies on the order */
394 GstStructure *s = g_ptr_array_remove_index (caps->structs, idx);
396 gst_structure_set_parent_refcount (s, NULL);
402 * @caps1: the #GstCaps that will be appended to
403 * @caps2: the #GstCaps to append
405 * Appends the structures contained in @caps2 to @caps1. The structures in
406 * @caps2 are not copied -- they are transferred to @caps1, and then @caps2 is
407 * freed. If either caps is ANY, the resulting caps will be ANY.
410 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
412 GstStructure *structure;
415 g_return_if_fail (GST_IS_CAPS (caps1));
416 g_return_if_fail (GST_IS_CAPS (caps2));
417 g_return_if_fail (IS_WRITABLE (caps1));
418 g_return_if_fail (IS_WRITABLE (caps2));
423 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2)) {
424 /* FIXME: this leaks */
425 caps1->flags |= GST_CAPS_FLAGS_ANY;
426 for (i = caps2->structs->len - 1; i >= 0; i--) {
427 structure = gst_caps_remove_and_get_structure (caps2, i);
428 gst_structure_free (structure);
431 int len = caps2->structs->len;
433 for (i = 0; i < len; i++) {
434 structure = gst_caps_remove_and_get_structure (caps2, 0);
435 gst_caps_append_structure (caps1, structure);
438 gst_caps_unref (caps2); /* guaranteed to free it */
442 * gst_caps_append_structure:
443 * @caps: the #GstCaps that will be appended to
444 * @structure: the #GstStructure to append
446 * Appends @structure to @caps. The structure is not copied; @caps
447 * becomes the owner of @structure.
450 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
452 g_return_if_fail (GST_IS_CAPS (caps));
453 g_return_if_fail (IS_WRITABLE (caps));
456 g_return_if_fail (structure->parent_refcount == NULL);
459 STRUCTURE_POISON (structure);
462 gst_structure_set_parent_refcount (structure, &caps->refcount);
463 g_ptr_array_add (caps->structs, structure);
468 * gst_caps_remove_structure:
469 * @caps: the #GstCaps to remove from
470 * @idx: Index of the structure to remove
472 * removes the stucture with the given index from the list of structures
473 * contained in @caps.
476 gst_caps_remove_structure (GstCaps * caps, guint idx)
478 GstStructure *structure;
480 g_return_if_fail (caps != NULL);
481 g_return_if_fail (idx <= gst_caps_get_size (caps));
482 g_return_if_fail (IS_WRITABLE (caps));
484 structure = gst_caps_remove_and_get_structure (caps, idx);
485 gst_structure_free (structure);
489 * gst_caps_split_one:
492 * This function is not implemented.
497 gst_caps_split_one (GstCaps * caps)
500 g_critical ("unimplemented");
509 * Gets the number of structures contained in @caps.
511 * Returns: the number of structures that @caps contains
514 gst_caps_get_size (const GstCaps * caps)
516 g_return_val_if_fail (GST_IS_CAPS (caps), 0);
518 return caps->structs->len;
522 * gst_caps_get_structure:
524 * @index: the index of the structure
526 * Finds the structure in @caps that has the index @index, and
529 * WARNING: This function takes a const GstCaps *, but returns a
530 * non-const GstStructure *. This is for programming convenience --
531 * the caller should be aware that structures inside a constant
532 * @GstCaps should not be modified.
534 * Returns: a pointer to the #GstStructure corresponding to @index
537 gst_caps_get_structure (const GstCaps * caps, int index)
539 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
540 g_return_val_if_fail (index >= 0, NULL);
541 g_return_val_if_fail (index < caps->structs->len, NULL);
543 return g_ptr_array_index (caps->structs, index);
548 * @caps: the @GstCaps to copy
549 * @nth: the nth structure to copy
551 * Creates a new @GstCaps and appends a copy of the nth structure
552 * contained in @caps.
554 * Returns: the new @GstCaps
557 gst_caps_copy_nth (const GstCaps * caps, gint nth)
560 GstStructure *structure;
562 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
564 newcaps = gst_caps_new_empty ();
565 newcaps->flags = caps->flags;
567 if (caps->structs->len > nth) {
568 structure = gst_caps_get_structure (caps, nth);
569 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
577 * @caps: the @GstCaps to truncate
579 * Destructively discard all but the first structure from @caps. Useful when
580 * fixating. @caps must be writable.
583 gst_caps_truncate (GstCaps * caps)
587 g_return_if_fail (GST_IS_CAPS (caps));
588 g_return_if_fail (IS_WRITABLE (caps));
590 i = caps->structs->len - 1;
593 gst_caps_remove_structure (caps, i--);
597 * gst_caps_set_simple:
598 * @caps: the @GstCaps to set
599 * @field: first field to set
600 * @...: additional parameters
602 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
603 * only has one structure. The arguments must be passed in the same
604 * manner as @gst_structure_set(), and be NULL-terminated.
607 gst_caps_set_simple (GstCaps * caps, char *field, ...)
609 GstStructure *structure;
612 g_return_if_fail (GST_IS_CAPS (caps));
613 g_return_if_fail (caps->structs->len == 1);
614 g_return_if_fail (IS_WRITABLE (caps));
616 structure = gst_caps_get_structure (caps, 0);
618 va_start (var_args, field);
619 gst_structure_set_valist (structure, field, var_args);
624 * gst_caps_set_simple_valist:
625 * @caps: the @GstCaps to copy
626 * @field: first field to set
627 * @varargs: additional parameters
629 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
630 * only has one structure. The arguments must be passed in the same
631 * manner as @gst_structure_set(), and be NULL-terminated.
634 gst_caps_set_simple_valist (GstCaps * caps, char *field, va_list varargs)
636 GstStructure *structure;
638 g_return_if_fail (GST_IS_CAPS (caps));
639 g_return_if_fail (caps->structs->len != 1);
640 g_return_if_fail (IS_WRITABLE (caps));
642 structure = gst_caps_get_structure (caps, 0);
644 gst_structure_set_valist (structure, field, varargs);
651 * @caps: the @GstCaps to test
653 * Determines if @caps represents any media format.
655 * Returns: TRUE if @caps represents any format.
658 gst_caps_is_any (const GstCaps * caps)
660 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
662 return (caps->flags & GST_CAPS_FLAGS_ANY);
667 * @caps: the @GstCaps to test
669 * Determines if @caps represents no media formats.
671 * Returns: TRUE if @caps represents no formats.
674 gst_caps_is_empty (const GstCaps * caps)
676 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
678 if (caps->flags & GST_CAPS_FLAGS_ANY)
681 return (caps->structs == NULL) || (caps->structs->len == 0);
685 gst_caps_is_fixed_foreach (GQuark field_id, const GValue * value,
688 return gst_value_is_fixed (value);
693 * @caps: the @GstCaps to test
695 * Fixed @GstCaps describe exactly one format, that is, they have exactly
696 * one structure, and each field in the structure describes a fixed type.
697 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
699 * Returns: TRUE if @caps is fixed
702 gst_caps_is_fixed (const GstCaps * caps)
704 GstStructure *structure;
706 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
708 if (caps->structs->len != 1)
711 structure = gst_caps_get_structure (caps, 0);
713 return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
717 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
720 GstStructure *struct1 = (GstStructure *) data;
721 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
725 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
733 * gst_caps_is_equal_fixed:
734 * @caps1: the #GstCaps to test
735 * @caps2: the #GstCaps to test
737 * Tests if two #GstCaps are equal. This function only works on fixed
740 * Returns: TRUE if the arguments represent the same format
743 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
745 GstStructure *struct1, *struct2;
747 g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
748 g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
750 struct1 = gst_caps_get_structure (caps1, 0);
751 struct2 = gst_caps_get_structure (caps2, 0);
753 if (struct1->name != struct2->name) {
756 if (struct1->fields->len != struct2->fields->len) {
760 return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
765 * gst_caps_is_always_compatible:
766 * @caps1: the #GstCaps to test
767 * @caps2: the #GstCaps to test
769 * A given #GstCaps structure is always compatible with another if
770 * every media format that is in the first is also contained in the
771 * second. That is, @caps1 is a subset of @caps2.
773 * Returns: TRUE if @caps1 is a subset of @caps2.
776 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
778 g_return_val_if_fail (GST_IS_CAPS (caps1), FALSE);
779 g_return_val_if_fail (GST_IS_CAPS (caps2), FALSE);
781 return gst_caps_is_subset (caps1, caps2);
785 * gst_caps_is_subset:
786 * @subset: a #GstCaps
787 * @superset: a potentially greater #GstCaps
789 * Checks if all caps represented by @subset are also represented by @superset
790 * <note>This function does not work reliably if optional properties for caps
791 * are included on one caps and omitted on the other.</note>
793 * Returns: TRUE if @subset is a subset of @superset
796 gst_caps_is_subset (const GstCaps * subset, const GstCaps * superset)
801 g_return_val_if_fail (subset != NULL, FALSE);
802 g_return_val_if_fail (superset != NULL, FALSE);
804 if (gst_caps_is_empty (subset) || gst_caps_is_any (superset))
806 if (gst_caps_is_any (subset) || gst_caps_is_empty (superset))
809 caps = gst_caps_subtract (subset, superset);
810 ret = gst_caps_is_empty (caps);
811 gst_caps_unref (caps);
818 * @caps2: another #GstCaps
820 * Checks if the given caps represent the same set of caps.
821 * <note>This function does not work reliably if optional properties for caps
822 * are included on one caps and omitted on the other.</note>
824 * Returns: TRUE if both caps are equal
827 gst_caps_is_equal (const GstCaps * caps1, const GstCaps * caps2)
829 g_return_val_if_fail (caps1 != NULL, FALSE);
830 g_return_val_if_fail (caps2 != NULL, FALSE);
832 if (gst_caps_is_fixed (caps1) && gst_caps_is_fixed (caps2))
833 return gst_caps_is_equal_fixed (caps1, caps2);
835 return gst_caps_is_subset (caps1, caps2) && gst_caps_is_subset (caps2, caps1);
841 const GstStructure *intersect;
847 gst_caps_structure_intersect_field (GQuark id, const GValue * val1,
850 IntersectData *idata = (IntersectData *) data;
851 GValue dest_value = { 0 };
852 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
855 gst_structure_id_set_value (idata->dest, id, val1);
856 } else if (idata->first_run) {
857 if (gst_value_intersect (&dest_value, val1, val2)) {
858 gst_structure_id_set_value (idata->dest, id, &dest_value);
859 g_value_unset (&dest_value);
868 static GstStructure *
869 gst_caps_structure_intersect (const GstStructure * struct1,
870 const GstStructure * struct2)
874 g_return_val_if_fail (struct1 != NULL, NULL);
875 g_return_val_if_fail (struct2 != NULL, NULL);
877 if (struct1->name != struct2->name)
880 data.dest = gst_structure_id_empty_new (struct1->name);
881 data.intersect = struct2;
882 data.first_run = TRUE;
883 if (!gst_structure_foreach ((GstStructure *) struct1,
884 gst_caps_structure_intersect_field, &data))
887 data.intersect = struct1;
888 data.first_run = FALSE;
889 if (!gst_structure_foreach ((GstStructure *) struct2,
890 gst_caps_structure_intersect_field, &data))
896 gst_structure_free (data.dest);
901 static GstStructure *
902 gst_caps_structure_union (const GstStructure * struct1,
903 const GstStructure * struct2)
907 const GstStructureField *field1;
908 const GstStructureField *field2;
911 /* FIXME this doesn't actually work */
913 if (struct1->name != struct2->name)
916 dest = gst_structure_id_empty_new (struct1->name);
918 for (i = 0; i < struct1->fields->len; i++) {
919 GValue dest_value = { 0 };
921 field1 = GST_STRUCTURE_FIELD (struct1, i);
922 field2 = gst_structure_id_get_field (struct2, field1->name);
924 if (field2 == NULL) {
927 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
928 gst_structure_set_value (dest, g_quark_to_string (field1->name),
931 ret = gst_value_compare (&field1->value, &field2->value);
943 * gst_caps_intersect:
944 * @caps1: a #GstCaps to intersect
945 * @caps2: a #GstCaps to intersect
947 * Creates a new #GstCaps that contains all the formats that are common
948 * to both @caps1 and @caps2.
950 * Returns: the new #GstCaps
953 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
956 GstStructure *struct1;
957 GstStructure *struct2;
959 GstStructure *istruct;
961 g_return_val_if_fail (GST_IS_CAPS (caps1), NULL);
962 g_return_val_if_fail (GST_IS_CAPS (caps2), NULL);
964 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)) {
965 return gst_caps_new_empty ();
967 if (gst_caps_is_any (caps1))
968 return gst_caps_copy (caps2);
969 if (gst_caps_is_any (caps2))
970 return gst_caps_copy (caps1);
972 dest = gst_caps_new_empty ();
974 /* run zigzag on top line then right line, this preserves the caps order
975 * much better than a simple loop.
977 * This algorithm zigzags over the caps structures as demonstrated in
978 * the folowing matrix:
986 * First we iterate over the caps1 structures (top line) intersecting
987 * the structures diagonally down, then we iterate over the caps2
990 for (i = 0; i < caps1->structs->len + caps2->structs->len - 1; i++) {
991 /* caps1 index goes from 0 to caps1->structs->len-1 */
992 j = MIN (i, caps1->structs->len - 1);
993 /* caps2 index stays 0 until i reaches caps1->structs->len, then it counts
994 * up from 1 to caps2->structs->len - 1 */
997 /* now run the diagonal line, end condition is the left or bottom
999 while (k < caps2->structs->len && j >= 0) {
1000 struct1 = gst_caps_get_structure (caps1, j);
1001 struct2 = gst_caps_get_structure (caps2, k);
1003 istruct = gst_caps_structure_intersect (struct1, struct2);
1005 gst_caps_append_structure (dest, istruct);
1006 /* move down left */
1011 gst_caps_do_simplify (dest);
1017 const GstStructure *subtract_from;
1024 gst_caps_structure_subtract_field (GQuark field_id, const GValue * value,
1027 SubtractionEntry *e = user_data;
1028 GValue subtraction = { 0, };
1029 const GValue *other;
1030 GstStructure *structure;
1032 other = gst_structure_id_get_value (e->subtract_from, field_id);
1036 if (!gst_value_subtract (&subtraction, other, value))
1038 if (gst_value_compare (&subtraction, other) == GST_VALUE_EQUAL) {
1039 g_value_unset (&subtraction);
1042 structure = gst_structure_copy (e->subtract_from);
1043 gst_structure_id_set_value (structure, field_id, &subtraction);
1044 g_value_unset (&subtraction);
1045 e->put_into = g_slist_prepend (e->put_into, structure);
1051 gst_caps_structure_subtract (GSList ** into, const GstStructure * minuend,
1052 const GstStructure * subtrahend)
1057 e.subtract_from = minuend;
1060 ret = gst_structure_foreach ((GstStructure *) subtrahend,
1061 gst_caps_structure_subtract_field, &e);
1067 for (walk = e.put_into; walk; walk = g_slist_next (walk)) {
1068 gst_structure_free (walk->data);
1070 g_slist_free (e.put_into);
1076 * gst_caps_subtract:
1077 * @minuend: #GstCaps to substract from
1078 * @subtrahend: #GstCaps to substract
1080 * Subtracts the @subtrahend from the @minuend.
1081 * <note>This function does not work reliably if optional properties for caps
1082 * are included on one caps and omitted on the other.</note>
1084 * Returns: the resulting caps
1087 gst_caps_subtract (const GstCaps * minuend, const GstCaps * subtrahend)
1092 GstCaps *dest = NULL, *src;
1094 g_return_val_if_fail (minuend != NULL, NULL);
1095 g_return_val_if_fail (subtrahend != NULL, NULL);
1097 if (gst_caps_is_empty (minuend) || gst_caps_is_any (subtrahend)) {
1098 return gst_caps_new_empty ();
1100 if (gst_caps_is_empty (subtrahend))
1101 return gst_caps_copy (minuend);
1103 /* FIXME: Do we want this here or above?
1104 The reason we need this is that there is no definition about what
1105 ANY means for specific types, so it's not possible to reduce ANY partially
1106 You can only remove everything or nothing and that is done above.
1107 Note: there's a test that checks this behaviour. */
1108 g_return_val_if_fail (!gst_caps_is_any (minuend), NULL);
1109 g_assert (subtrahend->structs->len > 0);
1111 src = gst_caps_copy (minuend);
1112 for (i = 0; i < subtrahend->structs->len; i++) {
1113 sub = gst_caps_get_structure (subtrahend, i);
1115 gst_caps_unref (src);
1118 dest = gst_caps_new_empty ();
1119 for (j = 0; j < src->structs->len; j++) {
1120 min = gst_caps_get_structure (src, j);
1121 if (gst_structure_get_name_id (min) == gst_structure_get_name_id (sub)) {
1124 if (gst_caps_structure_subtract (&list, min, sub)) {
1127 for (walk = list; walk; walk = g_slist_next (walk)) {
1128 gst_caps_append_structure (dest, (GstStructure *) walk->data);
1130 g_slist_free (list);
1132 gst_caps_append_structure (dest, gst_structure_copy (min));
1135 gst_caps_append_structure (dest, gst_structure_copy (min));
1138 if (gst_caps_is_empty (dest)) {
1139 gst_caps_unref (src);
1144 gst_caps_unref (src);
1145 gst_caps_do_simplify (dest);
1151 * @caps1: a #GstCaps to union
1152 * @caps2: a #GstCaps to union
1154 * Creates a new #GstCaps that contains all the formats that are in
1155 * either @caps1 and @caps2.
1157 * Returns: the new #GstCaps
1160 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
1165 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2))
1166 return gst_caps_new_any ();
1168 dest1 = gst_caps_copy (caps1);
1169 dest2 = gst_caps_copy (caps2);
1170 gst_caps_append (dest1, dest2);
1172 gst_caps_do_simplify (dest1);
1176 typedef struct _NormalizeForeach
1179 GstStructure *structure;
1184 gst_caps_normalize_foreach (GQuark field_id, const GValue * value, gpointer ptr)
1186 NormalizeForeach *nf = (NormalizeForeach *) ptr;
1190 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1191 for (i = 1; i < gst_value_list_get_size (value); i++) {
1192 const GValue *v = gst_value_list_get_value (value, i);
1193 GstStructure *structure = gst_structure_copy (nf->structure);
1195 gst_structure_id_set_value (structure, field_id, v);
1196 gst_caps_append_structure (nf->caps, structure);
1199 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
1200 gst_structure_id_set_value (nf->structure, field_id, &val);
1201 g_value_unset (&val);
1209 * gst_caps_normalize:
1210 * @caps: a #GstCaps to normalize
1212 * Creates a new #GstCaps that represents the same set of formats as
1213 * @caps, but contains no lists. Each list is expanded into separate
1216 * Returns: the new #GstCaps
1219 gst_caps_normalize (const GstCaps * caps)
1221 NormalizeForeach nf;
1225 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
1227 newcaps = gst_caps_copy (caps);
1230 for (i = 0; i < newcaps->structs->len; i++) {
1231 nf.structure = gst_caps_get_structure (newcaps, i);
1233 while (!gst_structure_foreach (nf.structure,
1234 gst_caps_normalize_foreach, &nf));
1241 gst_caps_compare_structures (gconstpointer one, gconstpointer two)
1244 const GstStructure *struct1 = *((const GstStructure **) one);
1245 const GstStructure *struct2 = *((const GstStructure **) two);
1247 /* FIXME: this orders aphabetically, but ordering the quarks might be faster
1248 So what's the best way? */
1249 ret = strcmp (gst_structure_get_name (struct1),
1250 gst_structure_get_name (struct2));
1254 return gst_structure_n_fields (struct2) - gst_structure_n_fields (struct1);
1258 * gst_caps_simplify:
1259 * @caps: a #GstCaps to simplify
1261 * Creates a new #GstCaps that represents the same set of formats as
1262 * @caps, but simpler. Component structures that are identical are
1263 * merged. Component structures that have ranges or lists that can
1264 * be merged are also merged.
1266 * Returns: the new #GstCaps
1269 gst_caps_simplify (const GstCaps * caps)
1273 g_return_val_if_fail (caps != NULL, NULL);
1275 ret = gst_caps_copy (caps);
1276 gst_caps_do_simplify (ret);
1285 GstStructure *compare;
1290 gst_caps_structure_figure_out_union (GQuark field_id, const GValue * value,
1293 UnionField *u = user_data;
1294 const GValue *val = gst_structure_id_get_value (u->compare, field_id);
1298 g_value_unset (&u->value);
1301 if (gst_value_compare (val, value) == GST_VALUE_EQUAL)
1304 g_value_unset (&u->value);
1308 gst_value_union (&u->value, val, value);
1313 gst_caps_structure_simplify (GstStructure ** result,
1314 const GstStructure * simplify, GstStructure * compare)
1317 UnionField field = { 0, {0,}, NULL };
1319 /* try to subtract to get a real subset */
1320 if (gst_caps_structure_subtract (&list, simplify, compare)) {
1321 switch (g_slist_length (list)) {
1326 *result = list->data;
1327 g_slist_free (list);
1333 for (walk = list; walk; walk = g_slist_next (walk)) {
1334 gst_structure_free (walk->data);
1336 g_slist_free (list);
1342 /* try to union both structs */
1343 field.compare = compare;
1344 if (gst_structure_foreach ((GstStructure *) simplify,
1345 gst_caps_structure_figure_out_union, &field)) {
1346 gboolean ret = FALSE;
1348 /* now we know all of simplify's fields are the same in compare
1349 * but at most one field: field.name */
1350 if (G_IS_VALUE (&field.value)) {
1351 if (gst_structure_n_fields (simplify) == gst_structure_n_fields (compare)) {
1352 gst_structure_id_set_value (compare, field.name, &field.value);
1356 g_value_unset (&field.value);
1357 } else if (gst_structure_n_fields (simplify) <=
1358 gst_structure_n_fields (compare)) {
1359 /* compare is just more specific, will be optimized away later */
1360 /* FIXME: do this here? */
1361 GST_LOG ("found a case that will be optimized later.");
1363 gchar *one = gst_structure_to_string (simplify);
1364 gchar *two = gst_structure_to_string (compare);
1367 ("caps mismatch: structures %s and %s claim to be possible to unify, but aren't",
1379 gst_caps_switch_structures (GstCaps * caps, GstStructure * old,
1380 GstStructure * new, gint i)
1382 gst_structure_set_parent_refcount (old, NULL);
1383 gst_structure_free (old);
1384 gst_structure_set_parent_refcount (new, &caps->refcount);
1385 g_ptr_array_index (caps->structs, i) = new;
1389 * gst_caps_do_simplify:
1390 * @caps: a #GstCaps to simplify
1392 * Modifies the given @caps inplace into a representation that represents the
1393 * same set of formats, but in a simpler form. Component structures that are
1394 * identical are merged. Component structures that have values that can be
1395 * merged are also merged.
1397 * Returns: TRUE, if the caps could be simplified
1400 gst_caps_do_simplify (GstCaps * caps)
1402 GstStructure *simplify, *compare, *result = NULL;
1404 gboolean changed = FALSE;
1406 g_return_val_if_fail (caps != NULL, FALSE);
1407 g_return_val_if_fail (IS_WRITABLE (caps), FALSE);
1409 if (gst_caps_get_size (caps) < 2)
1412 g_ptr_array_sort (caps->structs, gst_caps_compare_structures);
1414 start = caps->structs->len - 1;
1415 for (i = caps->structs->len - 1; i >= 0; i--) {
1416 simplify = gst_caps_get_structure (caps, i);
1417 if (gst_structure_get_name_id (simplify) !=
1418 gst_structure_get_name_id (gst_caps_get_structure (caps, start)))
1420 for (j = start; j >= 0; j--) {
1423 compare = gst_caps_get_structure (caps, j);
1424 if (gst_structure_get_name_id (simplify) !=
1425 gst_structure_get_name_id (compare)) {
1428 if (gst_caps_structure_simplify (&result, simplify, compare)) {
1430 gst_caps_switch_structures (caps, simplify, result, i);
1433 gst_caps_remove_structure (caps, i);
1445 /* gst_caps_do_simplify (caps); */
1449 #ifndef GST_DISABLE_LOADSAVE
1451 * gst_caps_save_thyself:
1452 * @caps: a #GstCaps structure
1453 * @parent: a XML parent node
1455 * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1457 * Returns: a XML node pointer
1460 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1462 char *s = gst_caps_to_string (caps);
1464 xmlNewChild (parent, NULL, (xmlChar *) "caps", (xmlChar *) s);
1470 * gst_caps_load_thyself:
1471 * @parent: a XML node
1473 * Creates a #GstCaps from its XML serialization.
1475 * Returns: a new #GstCaps structure
1478 gst_caps_load_thyself (xmlNodePtr parent)
1480 if (strcmp ("caps", (char *) parent->name) == 0) {
1481 return gst_caps_from_string ((gchar *) xmlNodeGetContent (parent));
1492 * @caps: a pointer to #GstCaps
1493 * @newcaps: a #GstCaps to replace *caps
1495 * Replaces *caps with @newcaps. Frees the #GstCaps in the location
1496 * pointed to by @caps, if applicable, then modifies @caps to point to
1500 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1504 #if 0 /* disable this, since too many plugins rely on undefined behavior */
1505 #ifdef USE_POISONING
1506 //if (newcaps) CAPS_POISON (newcaps);
1512 gst_caps_ref (newcaps);
1517 gst_caps_unref (oldcaps);
1521 * gst_caps_to_string:
1524 * Converts @caps to a string representation. This string representation
1525 * can be converted back to a #GstCaps by #gst_caps_from_string.
1527 * Returns: a newly allocated string representing @caps.
1530 gst_caps_to_string (const GstCaps * caps)
1535 /* NOTE: This function is potentially called by the debug system,
1536 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1537 * should be careful to avoid recursion. This includes any functions
1538 * called by gst_caps_to_string. In particular, calls should
1539 * not use the GST_PTR_FORMAT extension. */
1542 return g_strdup ("NULL");
1544 if (gst_caps_is_any (caps)) {
1545 return g_strdup ("ANY");
1547 if (gst_caps_is_empty (caps)) {
1548 return g_strdup ("EMPTY");
1551 s = g_string_new ("");
1552 for (i = 0; i < caps->structs->len; i++) {
1553 GstStructure *structure;
1557 g_string_append (s, "; ");
1559 structure = gst_caps_get_structure (caps, i);
1560 sstr = gst_structure_to_string (structure);
1561 g_string_append (s, sstr);
1565 return g_string_free (s, FALSE);
1569 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1571 GstStructure *structure;
1574 g_return_val_if_fail (string, FALSE);
1575 if (strcmp ("ANY", string) == 0) {
1576 caps->flags = GST_CAPS_FLAGS_ANY;
1579 if (strcmp ("EMPTY", string) == 0) {
1583 structure = gst_structure_from_string (string, &s);
1584 if (structure == NULL) {
1587 gst_caps_append_structure (caps, structure);
1591 while (g_ascii_isspace (*s))
1593 structure = gst_structure_from_string (s, &s);
1594 if (structure == NULL) {
1597 gst_caps_append_structure (caps, structure);
1598 while (g_ascii_isspace (*s))
1610 * gst_caps_from_string:
1611 * @string: a string to convert to #GstCaps
1613 * Converts @caps from a string representation.
1615 * Returns: a newly allocated #GstCaps
1618 gst_caps_from_string (const gchar * string)
1622 caps = gst_caps_new_empty ();
1623 if (gst_caps_from_string_inplace (caps, string)) {
1626 gst_caps_unref (caps);
1632 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1634 g_return_if_fail (G_IS_VALUE (src_value));
1635 g_return_if_fail (G_IS_VALUE (dest_value));
1636 g_return_if_fail (G_VALUE_HOLDS (src_value, GST_TYPE_CAPS));
1637 g_return_if_fail (G_VALUE_HOLDS (dest_value, G_TYPE_STRING)
1638 || G_VALUE_HOLDS (dest_value, G_TYPE_POINTER));
1640 dest_value->data[0].v_pointer =
1641 gst_caps_to_string (src_value->data[0].v_pointer);
1645 gst_caps_copy_conditional (GstCaps * src)
1648 return gst_caps_ref (src);