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.
26 #include "gst_private.h"
29 /* #define DEBUG_REFCOUNT */
31 #define CAPS_POISON(caps) G_STMT_START{ \
33 GstCaps *_newcaps = gst_caps_copy (caps); \
34 gst_caps_unref(caps); \
38 #define STRUCTURE_POISON(structure) G_STMT_START{ \
40 GstStructure *_newstruct = gst_structure_copy (structure); \
41 gst_structure_free(structure); \
42 structure = _newstruct; \
45 #define IS_WRITABLE(caps) \
46 (g_atomic_int_get (&(caps)->refcount) == 1)
49 static void gst_caps_transform_to_string (const GValue * src_value,
51 static gboolean gst_caps_from_string_inplace (GstCaps * caps,
52 const gchar * string);
53 static GstCaps *gst_caps_copy_conditional (GstCaps * src);
56 gst_caps_get_type (void)
58 static GType gst_caps_type = 0;
61 gst_caps_type = g_boxed_type_register_static ("GstCaps",
62 (GBoxedCopyFunc) gst_caps_copy_conditional,
63 (GBoxedFreeFunc) gst_caps_unref);
65 g_value_register_transform_func (gst_caps_type,
66 G_TYPE_STRING, gst_caps_transform_to_string);
72 /* creation/deletion */
77 * Creates a new #GstCaps that is empty. That is, the returned
78 * #GstCaps contains no media formats.
80 * Returns: the new #GstCaps
83 gst_caps_new_empty (void)
85 GstCaps *caps = g_new0 (GstCaps, 1);
87 g_atomic_int_inc (&caps->refcount);
88 caps->type = GST_TYPE_CAPS;
89 caps->structs = g_ptr_array_new ();
92 GST_CAT_LOG (GST_CAT_CAPS, "created caps %p", caps);
101 * Creates a new #GstCaps that indicates that it is compatible with
104 * Returns: the new #GstCaps
107 gst_caps_new_any (void)
109 GstCaps *caps = gst_caps_new_empty ();
111 caps->flags = GST_CAPS_FLAGS_ANY;
117 * gst_caps_new_simple:
118 * @media_type: the media type of the structure
119 * @fieldname: first field to set
120 * @...: additional arguments
122 * Creates a new #GstCaps that contains one #GstStructure. The
123 * structure is defined by the arguments, which have the same format
124 * as @gst_structure_new().
126 * Returns: the new #GstCaps
129 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
132 GstStructure *structure;
135 caps = gst_caps_new_empty ();
137 va_start (var_args, fieldname);
138 structure = gst_structure_new_valist (media_type, fieldname, var_args);
141 gst_caps_append_structure (caps, structure);
148 * @struct1: the first structure to add
149 * @...: additional structures to add
151 * Creates a new #GstCaps and adds all the structures listed as
152 * arguments. The list must be NULL-terminated. The structures
153 * are not copied; the returned #GstCaps owns the structures.
155 * Returns: the new #GstCaps
158 gst_caps_new_full (GstStructure * struct1, ...)
163 va_start (var_args, struct1);
164 caps = gst_caps_new_full_valist (struct1, var_args);
171 * gst_caps_new_full_valist:
172 * @structure: the first structure to add
173 * @var_args: additional structures to add
175 * Creates a new #GstCaps and adds all the structures listed as
176 * arguments. The list must be NULL-terminated. The structures
177 * are not copied; the returned #GstCaps owns the structures.
179 * Returns: the new #GstCaps
182 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
186 caps = gst_caps_new_empty ();
189 gst_caps_append_structure (caps, structure);
190 structure = va_arg (var_args, GstStructure *);
198 * @caps: the #GstCaps to copy
200 * Creates a new #GstCaps as a copy of the old @caps. The new caps will have a
201 * refcount of 1, owned by the caller. The structures are copied as well.
203 * Note that this function is the semantic equivalent of a gst_caps_ref()
204 * followed by a gst_caps_make_writable(). If you only want to hold on to a
205 * reference to the data, you should use gst_caps_ref().
207 * When you are finished with the caps, call gst_caps_unref() on it.
209 * Returns: the new #GstCaps
212 gst_caps_copy (const GstCaps * caps)
215 GstStructure *structure;
218 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
220 newcaps = gst_caps_new_empty ();
221 newcaps->flags = caps->flags;
223 for (i = 0; i < caps->structs->len; i++) {
224 structure = gst_caps_get_structure (caps, i);
225 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
232 _gst_caps_free (GstCaps * caps)
234 GstStructure *structure;
237 /* The refcount must be 0, but since we're only called by gst_caps_unref,
238 * don't bother testing. */
240 for (i = 0; i < caps->structs->len; i++) {
241 structure = (GstStructure *) gst_caps_get_structure (caps, i);
242 gst_structure_set_parent_refcount (structure, NULL);
243 gst_structure_free (structure);
245 g_ptr_array_free (caps->structs, TRUE);
247 memset (caps, 0xff, sizeof (GstCaps));
253 * gst_caps_make_writable:
254 * @caps: the #GstCaps to make writable
256 * Returns a writable copy of @caps.
258 * If there is only one reference count on @caps, the caller must be the owner,
259 * and so this function will return the caps object unchanged. If on the other
260 * hand there is more than one reference on the object, a new caps object will
261 * be returned. The caller's reference on @caps will be removed, and instead the
262 * caller will own a reference to the returned object.
264 * In short, this function unrefs the caps in the argument and refs the caps
265 * that it returns. Don't access the argument after calling this function. See
266 * also: gst_caps_ref().
268 * Returns: the same #GstCaps object.
271 gst_caps_make_writable (GstCaps * caps)
275 g_return_val_if_fail (caps != NULL, NULL);
277 /* we are the only instance reffing this caps */
278 if (g_atomic_int_get (&caps->refcount) == 1)
282 copy = gst_caps_copy (caps);
283 gst_caps_unref (caps);
290 * @caps: the #GstCaps to reference
292 * Add a reference to a #GstCaps object.
294 * From this point on, until the caller calls gst_caps_unref() or
295 * gst_caps_make_writable(), it is guaranteed that the caps object will not
296 * change. This means its structures won't change, etc. To use a #GstCaps
297 * object, you must always have a refcount on it -- either the one made
298 * implicitly by gst_caps_new(), or via taking one explicitly with this
301 * Returns: the same #GstCaps object.
304 gst_caps_ref (GstCaps * caps)
306 g_return_val_if_fail (caps != NULL, NULL);
308 #ifdef DEBUG_REFCOUNT
309 GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
310 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) + 1);
312 g_return_val_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0, NULL);
314 g_atomic_int_inc (&caps->refcount);
321 * @caps: the #GstCaps to unref
323 * Unref a #GstCaps and and free all its structures and the
324 * structures' values when the refcount reaches 0.
327 gst_caps_unref (GstCaps * caps)
329 g_return_if_fail (caps != NULL);
331 #ifdef DEBUG_REFCOUNT
332 GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
333 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) - 1);
336 g_return_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0);
338 /* if we ended up with the refcount at zero, free the caps */
339 if (g_atomic_int_dec_and_test (&caps->refcount)) {
340 _gst_caps_free (caps);
345 * gst_static_caps_get:
346 * @static_caps: the #GstStaticCaps to convert
348 * Converts a #GstStaticCaps to a #GstCaps.
350 * Returns: A pointer to the #GstCaps. Although you do not have a reference on
351 * the caps, the core will never drop its references. (The core has two
352 * references on the caps so it will be immutable.)
355 gst_static_caps_get (GstStaticCaps * static_caps)
357 GstCaps *caps = (GstCaps *) static_caps;
360 if (caps->type == 0) {
361 if (static_caps->string == NULL) {
362 g_warning ("static caps is NULL");
366 caps->type = GST_TYPE_CAPS;
367 /* initialize the caps to a refcount of 1 so the caps can be writable... */
368 gst_atomic_int_set (&caps->refcount, 1);
369 caps->structs = g_ptr_array_new ();
370 ret = gst_caps_from_string_inplace (caps, static_caps->string);
373 g_critical ("Could not convert static caps \"%s\"", static_caps->string);
375 /* and now that we return it to the user, keep a ref for ourselves. This
376 * makes the caps immutable... AND INVINCIBLE! WOULD YOU LIKE TO TRY MY
377 * IMMUTABLE CAPS STYLE? I AM A CAPS WARRIOR!!! */
386 static GstStructure *
387 gst_caps_remove_and_get_structure (GstCaps * caps, guint idx)
389 /* don't use index_fast, gst_caps_simplify relies on the order */
390 GstStructure *s = g_ptr_array_remove_index (caps->structs, idx);
392 gst_structure_set_parent_refcount (s, NULL);
398 * @caps1: the #GstCaps that will be appended to
399 * @caps2: the #GstCaps to append
401 * Appends the structures contained in @caps2 to @caps1. The structures in
402 * @caps2 are not copied -- they are transferred to @caps1, and then @caps2 is
403 * freed. If either caps is ANY, the resulting caps will be ANY.
406 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
408 GstStructure *structure;
411 g_return_if_fail (GST_IS_CAPS (caps1));
412 g_return_if_fail (GST_IS_CAPS (caps2));
413 g_return_if_fail (IS_WRITABLE (caps1));
414 g_return_if_fail (IS_WRITABLE (caps2));
419 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2)) {
420 /* FIXME: this leaks */
421 caps1->flags |= GST_CAPS_FLAGS_ANY;
422 for (i = caps2->structs->len - 1; i >= 0; i--) {
423 structure = gst_caps_remove_and_get_structure (caps2, i);
424 gst_structure_free (structure);
427 int len = caps2->structs->len;
429 for (i = 0; i < len; i++) {
430 structure = gst_caps_remove_and_get_structure (caps2, 0);
431 gst_caps_append_structure (caps1, structure);
434 gst_caps_unref (caps2); /* guaranteed to free it */
438 * gst_caps_append_structure:
439 * @caps: the #GstCaps that will be appended to
440 * @structure: the #GstStructure to append
442 * Appends @structure to @caps. The structure is not copied; @caps
443 * becomes the owner of @structure.
446 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
448 g_return_if_fail (GST_IS_CAPS (caps));
449 g_return_if_fail (IS_WRITABLE (caps));
452 g_return_if_fail (structure->parent_refcount == NULL);
455 STRUCTURE_POISON (structure);
458 gst_structure_set_parent_refcount (structure, &caps->refcount);
459 g_ptr_array_add (caps->structs, structure);
464 * gst_caps_remove_structure:
465 * @caps: the #GstCaps to remove from
466 * @idx: Index of the structure to remove
468 * removes the stucture with the given index from the list of structures
469 * contained in @caps.
472 gst_caps_remove_structure (GstCaps * caps, guint idx)
474 GstStructure *structure;
476 g_return_if_fail (caps != NULL);
477 g_return_if_fail (idx <= gst_caps_get_size (caps));
478 g_return_if_fail (IS_WRITABLE (caps));
480 structure = gst_caps_remove_and_get_structure (caps, idx);
481 gst_structure_free (structure);
485 * gst_caps_split_one:
488 * This function is not implemented.
493 gst_caps_split_one (GstCaps * caps)
496 g_critical ("unimplemented");
505 * Gets the number of structures contained in @caps.
507 * Returns: the number of structures that @caps contains
510 gst_caps_get_size (const GstCaps * caps)
512 g_return_val_if_fail (GST_IS_CAPS (caps), 0);
514 return caps->structs->len;
518 * gst_caps_get_structure:
520 * @index: the index of the structure
522 * Finds the structure in @caps that has the index @index, and
525 * WARNING: This function takes a const GstCaps *, but returns a
526 * non-const GstStructure *. This is for programming convenience --
527 * the caller should be aware that structures inside a constant
528 * @GstCaps should not be modified.
530 * Returns: a pointer to the #GstStructure corresponding to @index
533 gst_caps_get_structure (const GstCaps * caps, int index)
535 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
536 g_return_val_if_fail (index >= 0, NULL);
537 g_return_val_if_fail (index < caps->structs->len, NULL);
539 return g_ptr_array_index (caps->structs, index);
544 * @caps: the @GstCaps to copy
545 * @nth: the nth structure to copy
547 * Creates a new @GstCaps and appends a copy of the nth structure
548 * contained in @caps.
550 * Returns: the new @GstCaps
553 gst_caps_copy_nth (const GstCaps * caps, gint nth)
556 GstStructure *structure;
558 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
560 newcaps = gst_caps_new_empty ();
561 newcaps->flags = caps->flags;
563 if (caps->structs->len > nth) {
564 structure = gst_caps_get_structure (caps, nth);
565 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
572 * gst_caps_set_simple:
573 * @caps: the @GstCaps to set
574 * @field: first field to set
575 * @...: additional parameters
577 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
578 * only has one structure. The arguments must be passed in the same
579 * manner as @gst_structure_set(), and be NULL-terminated.
582 gst_caps_set_simple (GstCaps * caps, char *field, ...)
584 GstStructure *structure;
587 g_return_if_fail (GST_IS_CAPS (caps));
588 g_return_if_fail (caps->structs->len == 1);
589 g_return_if_fail (IS_WRITABLE (caps));
591 structure = gst_caps_get_structure (caps, 0);
593 va_start (var_args, field);
594 gst_structure_set_valist (structure, field, var_args);
599 * gst_caps_set_simple_valist:
600 * @caps: the @GstCaps to copy
601 * @field: first field to set
602 * @varargs: 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_valist (GstCaps * caps, char *field, va_list varargs)
611 GstStructure *structure;
613 g_return_if_fail (GST_IS_CAPS (caps));
614 g_return_if_fail (caps->structs->len != 1);
615 g_return_if_fail (IS_WRITABLE (caps));
617 structure = gst_caps_get_structure (caps, 0);
619 gst_structure_set_valist (structure, field, varargs);
626 * @caps: the @GstCaps to test
628 * Determines if @caps represents any media format.
630 * Returns: TRUE if @caps represents any format.
633 gst_caps_is_any (const GstCaps * caps)
635 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
637 return (caps->flags & GST_CAPS_FLAGS_ANY);
642 * @caps: the @GstCaps to test
644 * Determines if @caps represents no media formats.
646 * Returns: TRUE if @caps represents no formats.
649 gst_caps_is_empty (const GstCaps * caps)
651 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
653 if (caps->flags & GST_CAPS_FLAGS_ANY)
656 return (caps->structs == NULL) || (caps->structs->len == 0);
660 gst_caps_is_fixed_foreach (GQuark field_id, const GValue * value,
663 return gst_value_is_fixed (value);
668 * @caps: the @GstCaps to test
670 * Fixed @GstCaps describe exactly one format, that is, they have exactly
671 * one structure, and each field in the structure describes a fixed type.
672 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
674 * Returns: TRUE if @caps is fixed
677 gst_caps_is_fixed (const GstCaps * caps)
679 GstStructure *structure;
681 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
683 if (caps->structs->len != 1)
686 structure = gst_caps_get_structure (caps, 0);
688 return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
692 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
695 GstStructure *struct1 = (GstStructure *) data;
696 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
700 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
708 * gst_caps_is_equal_fixed:
709 * @caps1: the #GstCaps to test
710 * @caps2: the #GstCaps to test
712 * Tests if two #GstCaps are equal. This function only works on fixed
715 * Returns: TRUE if the arguments represent the same format
718 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
720 GstStructure *struct1, *struct2;
722 g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
723 g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
725 struct1 = gst_caps_get_structure (caps1, 0);
726 struct2 = gst_caps_get_structure (caps2, 0);
728 if (struct1->name != struct2->name) {
731 if (struct1->fields->len != struct2->fields->len) {
735 return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
740 * gst_caps_is_always_compatible:
741 * @caps1: the #GstCaps to test
742 * @caps2: the #GstCaps to test
744 * A given #GstCaps structure is always compatible with another if
745 * every media format that is in the first is also contained in the
746 * second. That is, @caps1 is a subset of @caps2.
748 * Returns: TRUE if @caps1 is a subset of @caps2.
751 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
753 g_return_val_if_fail (GST_IS_CAPS (caps1), FALSE);
754 g_return_val_if_fail (GST_IS_CAPS (caps2), FALSE);
756 return gst_caps_is_subset (caps1, caps2);
760 * gst_caps_is_subset:
761 * @subset: a #GstCaps
762 * @superset: a potentially greater #GstCaps
764 * Checks if all caps represented by @subset are also represented by @superset
765 * <note>This function does not work reliably if optional properties for caps
766 * are included on one caps and omitted on the other.</note>
768 * Returns: TRUE if @subset is a subset of @superset
771 gst_caps_is_subset (const GstCaps * subset, const GstCaps * superset)
776 g_return_val_if_fail (subset != NULL, FALSE);
777 g_return_val_if_fail (superset != NULL, FALSE);
779 if (gst_caps_is_empty (subset) || gst_caps_is_any (superset))
781 if (gst_caps_is_any (subset) || gst_caps_is_empty (superset))
784 caps = gst_caps_subtract (subset, superset);
785 ret = gst_caps_is_empty (caps);
786 gst_caps_unref (caps);
793 * @caps2: another #GstCaps
795 * Checks if the given caps represent the same set of caps.
796 * <note>This function does not work reliably if optional properties for caps
797 * are included on one caps and omitted on the other.</note>
799 * Returns: TRUE if both caps are equal
802 gst_caps_is_equal (const GstCaps * caps1, const GstCaps * caps2)
804 g_return_val_if_fail (caps1 != NULL, FALSE);
805 g_return_val_if_fail (caps2 != NULL, FALSE);
807 if (gst_caps_is_fixed (caps1) && gst_caps_is_fixed (caps2))
808 return gst_caps_is_equal_fixed (caps1, caps2);
810 return gst_caps_is_subset (caps1, caps2) && gst_caps_is_subset (caps2, caps1);
816 const GstStructure *intersect;
822 gst_caps_structure_intersect_field (GQuark id, const GValue * val1,
825 IntersectData *idata = (IntersectData *) data;
826 GValue dest_value = { 0 };
827 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
830 gst_structure_id_set_value (idata->dest, id, val1);
831 } else if (idata->first_run) {
832 if (gst_value_intersect (&dest_value, val1, val2)) {
833 gst_structure_id_set_value (idata->dest, id, &dest_value);
834 g_value_unset (&dest_value);
843 static GstStructure *
844 gst_caps_structure_intersect (const GstStructure * struct1,
845 const GstStructure * struct2)
849 g_return_val_if_fail (struct1 != NULL, NULL);
850 g_return_val_if_fail (struct2 != NULL, NULL);
852 if (struct1->name != struct2->name)
855 data.dest = gst_structure_id_empty_new (struct1->name);
856 data.intersect = struct2;
857 data.first_run = TRUE;
858 if (!gst_structure_foreach ((GstStructure *) struct1,
859 gst_caps_structure_intersect_field, &data))
862 data.intersect = struct1;
863 data.first_run = FALSE;
864 if (!gst_structure_foreach ((GstStructure *) struct2,
865 gst_caps_structure_intersect_field, &data))
871 gst_structure_free (data.dest);
876 static GstStructure *
877 gst_caps_structure_union (const GstStructure * struct1,
878 const GstStructure * struct2)
882 const GstStructureField *field1;
883 const GstStructureField *field2;
886 /* FIXME this doesn't actually work */
888 if (struct1->name != struct2->name)
891 dest = gst_structure_id_empty_new (struct1->name);
893 for (i = 0; i < struct1->fields->len; i++) {
894 GValue dest_value = { 0 };
896 field1 = GST_STRUCTURE_FIELD (struct1, i);
897 field2 = gst_structure_id_get_field (struct2, field1->name);
899 if (field2 == NULL) {
902 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
903 gst_structure_set_value (dest, g_quark_to_string (field1->name),
906 ret = gst_value_compare (&field1->value, &field2->value);
918 * gst_caps_intersect:
919 * @caps1: a #GstCaps to intersect
920 * @caps2: a #GstCaps to intersect
922 * Creates a new #GstCaps that contains all the formats that are common
923 * to both @caps1 and @caps2.
925 * Returns: the new #GstCaps
928 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
931 GstStructure *struct1;
932 GstStructure *struct2;
934 GstStructure *istruct;
936 g_return_val_if_fail (GST_IS_CAPS (caps1), NULL);
937 g_return_val_if_fail (GST_IS_CAPS (caps2), NULL);
939 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)) {
940 return gst_caps_new_empty ();
942 if (gst_caps_is_any (caps1))
943 return gst_caps_copy (caps2);
944 if (gst_caps_is_any (caps2))
945 return gst_caps_copy (caps1);
947 dest = gst_caps_new_empty ();
949 /* run zigzag on top line then right line, this preserves the caps order
950 * much better than a simple loop.
952 * This algorithm zigzags over the caps structures as demonstrated in
953 * the folowing matrix:
961 * First we iterate over the caps1 structures (top line) intersecting
962 * the structures diagonally down, then we iterate over the caps2
965 for (i = 0; i < caps1->structs->len + caps2->structs->len - 1; i++) {
966 /* caps1 index goes from 0 to caps1->structs->len-1 */
967 j = MIN (i, caps1->structs->len - 1);
968 /* caps2 index stays 0 until i reaches caps1->structs->len, then it counts
969 * up from 1 to caps2->structs->len - 1 */
972 /* now run the diagonal line, end condition is the left or bottom
974 while (k < caps2->structs->len && j >= 0) {
975 struct1 = gst_caps_get_structure (caps1, j);
976 struct2 = gst_caps_get_structure (caps2, k);
978 istruct = gst_caps_structure_intersect (struct1, struct2);
980 gst_caps_append_structure (dest, istruct);
986 gst_caps_do_simplify (dest);
992 const GstStructure *subtract_from;
999 gst_caps_structure_subtract_field (GQuark field_id, const GValue * value,
1002 SubtractionEntry *e = user_data;
1003 GValue subtraction = { 0, };
1004 const GValue *other;
1005 GstStructure *structure;
1007 other = gst_structure_id_get_value (e->subtract_from, field_id);
1011 if (!gst_value_subtract (&subtraction, other, value))
1013 if (gst_value_compare (&subtraction, other) == GST_VALUE_EQUAL) {
1014 g_value_unset (&subtraction);
1017 structure = gst_structure_copy (e->subtract_from);
1018 gst_structure_id_set_value (structure, field_id, &subtraction);
1019 g_value_unset (&subtraction);
1020 e->put_into = g_slist_prepend (e->put_into, structure);
1026 gst_caps_structure_subtract (GSList ** into, const GstStructure * minuend,
1027 const GstStructure * subtrahend)
1032 e.subtract_from = minuend;
1035 ret = gst_structure_foreach ((GstStructure *) subtrahend,
1036 gst_caps_structure_subtract_field, &e);
1042 for (walk = e.put_into; walk; walk = g_slist_next (walk)) {
1043 gst_structure_free (walk->data);
1045 g_slist_free (e.put_into);
1051 * gst_caps_subtract:
1052 * @minuend: #GstCaps to substract from
1053 * @subtrahend: #GstCaps to substract
1055 * Subtracts the @subtrahend from the @minuend.
1056 * <note>This function does not work reliably if optional properties for caps
1057 * are included on one caps and omitted on the other.</note>
1059 * Returns: the resulting caps
1062 gst_caps_subtract (const GstCaps * minuend, const GstCaps * subtrahend)
1067 GstCaps *dest = NULL, *src;
1069 g_return_val_if_fail (minuend != NULL, NULL);
1070 g_return_val_if_fail (subtrahend != NULL, NULL);
1072 if (gst_caps_is_empty (minuend) || gst_caps_is_any (subtrahend)) {
1073 return gst_caps_new_empty ();
1075 if (gst_caps_is_empty (subtrahend))
1076 return gst_caps_copy (minuend);
1078 /* FIXME: Do we want this here or above?
1079 The reason we need this is that there is no definition about what
1080 ANY means for specific types, so it's not possible to reduce ANY partially
1081 You can only remove everything or nothing and that is done above.
1082 Note: there's a test that checks this behaviour. */
1083 g_return_val_if_fail (!gst_caps_is_any (minuend), NULL);
1084 g_assert (subtrahend->structs->len > 0);
1086 src = gst_caps_copy (minuend);
1087 for (i = 0; i < subtrahend->structs->len; i++) {
1088 sub = gst_caps_get_structure (subtrahend, i);
1090 gst_caps_unref (src);
1093 dest = gst_caps_new_empty ();
1094 for (j = 0; j < src->structs->len; j++) {
1095 min = gst_caps_get_structure (src, j);
1096 if (gst_structure_get_name_id (min) == gst_structure_get_name_id (sub)) {
1099 if (gst_caps_structure_subtract (&list, min, sub)) {
1102 for (walk = list; walk; walk = g_slist_next (walk)) {
1103 gst_caps_append_structure (dest, (GstStructure *) walk->data);
1105 g_slist_free (list);
1107 gst_caps_append_structure (dest, gst_structure_copy (min));
1110 gst_caps_append_structure (dest, gst_structure_copy (min));
1113 if (gst_caps_is_empty (dest)) {
1114 gst_caps_unref (src);
1119 gst_caps_unref (src);
1120 gst_caps_do_simplify (dest);
1126 * @caps1: a #GstCaps to union
1127 * @caps2: a #GstCaps to union
1129 * Creates a new #GstCaps that contains all the formats that are in
1130 * either @caps1 and @caps2.
1132 * Returns: the new #GstCaps
1135 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
1140 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2))
1141 return gst_caps_new_any ();
1143 dest1 = gst_caps_copy (caps1);
1144 dest2 = gst_caps_copy (caps2);
1145 gst_caps_append (dest1, dest2);
1147 gst_caps_do_simplify (dest1);
1151 typedef struct _NormalizeForeach
1154 GstStructure *structure;
1159 gst_caps_normalize_foreach (GQuark field_id, const GValue * value, gpointer ptr)
1161 NormalizeForeach *nf = (NormalizeForeach *) ptr;
1165 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1166 for (i = 1; i < gst_value_list_get_size (value); i++) {
1167 const GValue *v = gst_value_list_get_value (value, i);
1168 GstStructure *structure = gst_structure_copy (nf->structure);
1170 gst_structure_id_set_value (structure, field_id, v);
1171 gst_caps_append_structure (nf->caps, structure);
1174 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
1175 gst_structure_id_set_value (nf->structure, field_id, &val);
1176 g_value_unset (&val);
1184 * gst_caps_normalize:
1185 * @caps: a #GstCaps to normalize
1187 * Creates a new #GstCaps that represents the same set of formats as
1188 * @caps, but contains no lists. Each list is expanded into separate
1191 * Returns: the new #GstCaps
1194 gst_caps_normalize (const GstCaps * caps)
1196 NormalizeForeach nf;
1200 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
1202 newcaps = gst_caps_copy (caps);
1205 for (i = 0; i < newcaps->structs->len; i++) {
1206 nf.structure = gst_caps_get_structure (newcaps, i);
1208 while (!gst_structure_foreach (nf.structure,
1209 gst_caps_normalize_foreach, &nf));
1216 gst_caps_compare_structures (gconstpointer one, gconstpointer two)
1219 const GstStructure *struct1 = *((const GstStructure **) one);
1220 const GstStructure *struct2 = *((const GstStructure **) two);
1222 /* FIXME: this orders aphabetically, but ordering the quarks might be faster
1223 So what's the best way? */
1224 ret = strcmp (gst_structure_get_name (struct1),
1225 gst_structure_get_name (struct2));
1229 return gst_structure_n_fields (struct2) - gst_structure_n_fields (struct1);
1233 * gst_caps_simplify:
1234 * @caps: a #GstCaps to simplify
1236 * Creates a new #GstCaps that represents the same set of formats as
1237 * @caps, but simpler. Component structures that are identical are
1238 * merged. Component structures that have ranges or lists that can
1239 * be merged are also merged.
1241 * Returns: the new #GstCaps
1244 gst_caps_simplify (const GstCaps * caps)
1248 g_return_val_if_fail (caps != NULL, NULL);
1250 ret = gst_caps_copy (caps);
1251 gst_caps_do_simplify (ret);
1260 GstStructure *compare;
1265 gst_caps_structure_figure_out_union (GQuark field_id, const GValue * value,
1268 UnionField *u = user_data;
1269 const GValue *val = gst_structure_id_get_value (u->compare, field_id);
1273 g_value_unset (&u->value);
1276 if (gst_value_compare (val, value) == GST_VALUE_EQUAL)
1279 g_value_unset (&u->value);
1283 gst_value_union (&u->value, val, value);
1288 gst_caps_structure_simplify (GstStructure ** result,
1289 const GstStructure * simplify, GstStructure * compare)
1292 UnionField field = { 0, {0,}, NULL };
1294 /* try to subtract to get a real subset */
1295 if (gst_caps_structure_subtract (&list, simplify, compare)) {
1296 switch (g_slist_length (list)) {
1301 *result = list->data;
1302 g_slist_free (list);
1308 for (walk = list; walk; walk = g_slist_next (walk)) {
1309 gst_structure_free (walk->data);
1311 g_slist_free (list);
1317 /* try to union both structs */
1318 field.compare = compare;
1319 if (gst_structure_foreach ((GstStructure *) simplify,
1320 gst_caps_structure_figure_out_union, &field)) {
1321 gboolean ret = FALSE;
1323 /* now we know all of simplify's fields are the same in compare
1324 * but at most one field: field.name */
1325 if (G_IS_VALUE (&field.value)) {
1326 if (gst_structure_n_fields (simplify) == gst_structure_n_fields (compare)) {
1327 gst_structure_id_set_value (compare, field.name, &field.value);
1331 g_value_unset (&field.value);
1332 } else if (gst_structure_n_fields (simplify) <=
1333 gst_structure_n_fields (compare)) {
1334 /* compare is just more specific, will be optimized away later */
1335 /* FIXME: do this here? */
1336 GST_LOG ("found a case that will be optimized later.");
1338 gchar *one = gst_structure_to_string (simplify);
1339 gchar *two = gst_structure_to_string (compare);
1342 ("caps mismatch: structures %s and %s claim to be possible to unify, but aren't",
1354 gst_caps_switch_structures (GstCaps * caps, GstStructure * old,
1355 GstStructure * new, gint i)
1357 gst_structure_set_parent_refcount (old, NULL);
1358 gst_structure_free (old);
1359 gst_structure_set_parent_refcount (new, &caps->refcount);
1360 g_ptr_array_index (caps->structs, i) = new;
1364 * gst_caps_do_simplify:
1365 * @caps: a #GstCaps to simplify
1367 * Modifies the given @caps inplace into a representation that represents the
1368 * same set of formats, but in a simpler form. Component structures that are
1369 * identical are merged. Component structures that have values that can be
1370 * merged are also merged.
1372 * Returns: TRUE, if the caps could be simplified
1375 gst_caps_do_simplify (GstCaps * caps)
1377 GstStructure *simplify, *compare, *result = NULL;
1379 gboolean changed = FALSE;
1381 g_return_val_if_fail (caps != NULL, FALSE);
1382 g_return_val_if_fail (IS_WRITABLE (caps), FALSE);
1384 if (gst_caps_get_size (caps) < 2)
1387 g_ptr_array_sort (caps->structs, gst_caps_compare_structures);
1389 start = caps->structs->len - 1;
1390 for (i = caps->structs->len - 1; i >= 0; i--) {
1391 simplify = gst_caps_get_structure (caps, i);
1392 if (gst_structure_get_name_id (simplify) !=
1393 gst_structure_get_name_id (gst_caps_get_structure (caps, start)))
1395 for (j = start; j >= 0; j--) {
1398 compare = gst_caps_get_structure (caps, j);
1399 if (gst_structure_get_name_id (simplify) !=
1400 gst_structure_get_name_id (compare)) {
1403 if (gst_caps_structure_simplify (&result, simplify, compare)) {
1405 gst_caps_switch_structures (caps, simplify, result, i);
1407 gst_caps_remove_structure (caps, i);
1419 /* gst_caps_do_simplify (caps); */
1423 #ifndef GST_DISABLE_LOADSAVE
1425 * gst_caps_save_thyself:
1426 * @caps: a #GstCaps structure
1427 * @parent: a XML parent node
1429 * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1431 * Returns: a XML node pointer
1434 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1436 char *s = gst_caps_to_string (caps);
1438 xmlNewChild (parent, NULL, (xmlChar *) "caps", (xmlChar *) s);
1444 * gst_caps_load_thyself:
1445 * @parent: a XML node
1447 * Creates a #GstCaps from its XML serialization.
1449 * Returns: a new #GstCaps structure
1452 gst_caps_load_thyself (xmlNodePtr parent)
1454 if (strcmp ("caps", (char *) parent->name) == 0) {
1455 return gst_caps_from_string ((gchar *) xmlNodeGetContent (parent));
1466 * @caps: a pointer to #GstCaps
1467 * @newcaps: a #GstCaps to replace *caps
1469 * Replaces *caps with @newcaps. Frees the #GstCaps in the location
1470 * pointed to by @caps, if applicable, then modifies @caps to point to
1474 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1478 #if 0 /* disable this, since too many plugins rely on undefined behavior */
1479 #ifdef USE_POISONING
1480 //if (newcaps) CAPS_POISON (newcaps);
1486 gst_caps_ref (newcaps);
1491 gst_caps_unref (oldcaps);
1495 * gst_caps_to_string:
1498 * Converts @caps to a string representation. This string representation
1499 * can be converted back to a #GstCaps by #gst_caps_from_string.
1501 * Returns: a newly allocated string representing @caps.
1504 gst_caps_to_string (const GstCaps * caps)
1507 GstStructure *structure;
1511 /* NOTE: This function is potentially called by the debug system,
1512 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1513 * should be careful to avoid recursion. This includes any functions
1514 * called by gst_caps_to_string. In particular, calls should
1515 * not use the GST_PTR_FORMAT extension. */
1517 /* FIXME does this leak? */
1520 return g_strdup ("NULL");
1522 if (gst_caps_is_any (caps)) {
1523 return g_strdup ("ANY");
1525 if (gst_caps_is_empty (caps)) {
1526 return g_strdup ("EMPTY");
1528 s = g_string_new ("");
1529 structure = gst_caps_get_structure (caps, 0);
1530 sstr = gst_structure_to_string (structure);
1531 g_string_append (s, sstr);
1534 for (i = 1; i < caps->structs->len; i++) {
1535 structure = gst_caps_get_structure (caps, i);
1537 g_string_append (s, "; ");
1538 sstr = gst_structure_to_string (structure);
1539 g_string_append (s, sstr);
1543 return g_string_free (s, FALSE);
1547 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1549 GstStructure *structure;
1552 g_return_val_if_fail (string, FALSE);
1553 if (strcmp ("ANY", string) == 0) {
1554 caps->flags = GST_CAPS_FLAGS_ANY;
1557 if (strcmp ("EMPTY", string) == 0) {
1561 structure = gst_structure_from_string (string, &s);
1562 if (structure == NULL) {
1565 gst_caps_append_structure (caps, structure);
1569 while (g_ascii_isspace (*s))
1571 structure = gst_structure_from_string (s, &s);
1572 if (structure == NULL) {
1575 gst_caps_append_structure (caps, structure);
1576 while (g_ascii_isspace (*s))
1588 * gst_caps_from_string:
1589 * @string: a string to convert to #GstCaps
1591 * Converts @caps from a string representation.
1593 * Returns: a newly allocated #GstCaps
1596 gst_caps_from_string (const gchar * string)
1600 caps = gst_caps_new_empty ();
1601 if (gst_caps_from_string_inplace (caps, string)) {
1604 gst_caps_unref (caps);
1610 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1612 g_return_if_fail (G_IS_VALUE (src_value));
1613 g_return_if_fail (G_IS_VALUE (dest_value));
1614 g_return_if_fail (G_VALUE_HOLDS (src_value, GST_TYPE_CAPS));
1615 g_return_if_fail (G_VALUE_HOLDS (dest_value, G_TYPE_STRING)
1616 || G_VALUE_HOLDS (dest_value, G_TYPE_POINTER));
1618 dest_value->data[0].v_pointer =
1619 gst_caps_to_string (src_value->data[0].v_pointer);
1623 gst_caps_copy_conditional (GstCaps * src)
1626 return gst_caps_ref (src);