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"
27 #include "gstatomic_impl.h"
30 //#define DEBUG_REFCOUNT
32 #define CAPS_POISON(caps) G_STMT_START{ \
34 GstCaps *_newcaps = gst_caps_copy (caps); \
35 gst_caps_unref(caps); \
39 #define STRUCTURE_POISON(structure) G_STMT_START{ \
41 GstStructure *_newstruct = gst_structure_copy (structure); \
42 gst_structure_free(structure); \
43 structure = _newstruct; \
46 #define IS_WRITABLE(caps) \
47 (gst_atomic_int_read(&(caps)->refcount) == 1)
50 static void gst_caps_transform_to_string (const GValue * src_value,
52 static gboolean gst_caps_from_string_inplace (GstCaps * caps,
53 const gchar * string);
54 static GstCaps *gst_caps_copy_conditional (GstCaps * src);
57 gst_caps_get_type (void)
59 static GType gst_caps_type = 0;
62 gst_caps_type = g_boxed_type_register_static ("GstCaps",
63 (GBoxedCopyFunc) gst_caps_copy_conditional,
64 (GBoxedFreeFunc) gst_caps_unref);
66 g_value_register_transform_func (gst_caps_type,
67 G_TYPE_STRING, gst_caps_transform_to_string);
73 /* creation/deletion */
78 * Creates a new #GstCaps that is empty. That is, the returned
79 * #GstCaps contains no media formats.
81 * Returns: the new #GstCaps
84 gst_caps_new_empty (void)
86 GstCaps *caps = g_new0 (GstCaps, 1);
88 gst_atomic_int_init (&(caps)->refcount, 1);
89 caps->type = GST_TYPE_CAPS;
90 caps->structs = g_ptr_array_new ();
93 GST_CAT_LOG (GST_CAT_CAPS, "created caps %p", caps);
102 * Creates a new #GstCaps that indicates that it is compatible with
105 * Returns: the new #GstCaps
108 gst_caps_new_any (void)
110 GstCaps *caps = gst_caps_new_empty ();
112 caps->flags = GST_CAPS_FLAGS_ANY;
118 * gst_caps_new_simple:
119 * @media_type: the media type of the structure
120 * @fieldname: first field to set
121 * @...: additional arguments
123 * Creates a new #GstCaps that contains one #GstStructure. The
124 * structure is defined by the arguments, which have the same format
125 * as @gst_structure_new().
127 * Returns: the new #GstCaps
130 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
133 GstStructure *structure;
136 caps = gst_caps_new_empty ();
138 va_start (var_args, fieldname);
139 structure = gst_structure_new_valist (media_type, fieldname, var_args);
142 gst_caps_append_structure (caps, structure);
149 * @struct1: the first structure to add
150 * @...: additional structures to add
152 * Creates a new #GstCaps and adds all the structures listed as
153 * arguments. The list must be NULL-terminated. The structures
154 * are not copied; the returned #GstCaps owns the structures.
156 * Returns: the new #GstCaps
159 gst_caps_new_full (GstStructure * struct1, ...)
164 va_start (var_args, struct1);
165 caps = gst_caps_new_full_valist (struct1, var_args);
172 * gst_caps_new_full_valist:
173 * @structure: the first structure to add
174 * @var_args: additional structures to add
176 * Creates a new #GstCaps and adds all the structures listed as
177 * arguments. The list must be NULL-terminated. The structures
178 * are not copied; the returned #GstCaps owns the structures.
180 * Returns: the new #GstCaps
183 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
187 caps = gst_caps_new_empty ();
190 gst_caps_append_structure (caps, structure);
191 structure = va_arg (var_args, GstStructure *);
199 * @caps: the #GstCaps to copy
201 * Creates a new #GstCaps as a copy of the old @caps. The new caps will have a
202 * refcount of 1, owned by the caller. The structures are copied as well.
204 * Note that this function is the semantic equivalent of a gst_caps_ref()
205 * followed by a gst_caps_make_writable(). If you only want to hold on to a
206 * reference to the data, you should use gst_caps_ref().
208 * When you are finished with the caps, call gst_caps_unref() on it.
210 * Returns: the new #GstCaps
213 gst_caps_copy (const GstCaps * caps)
216 GstStructure *structure;
219 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
221 newcaps = gst_caps_new_empty ();
222 newcaps->flags = caps->flags;
224 for (i = 0; i < caps->structs->len; i++) {
225 structure = gst_caps_get_structure (caps, i);
226 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
233 _gst_caps_free (GstCaps * caps)
235 GstStructure *structure;
238 /* The refcount must be 0, but since we're only called by gst_caps_unref,
239 * don't bother testing. */
241 for (i = 0; i < caps->structs->len; i++) {
242 structure = (GstStructure *) gst_caps_get_structure (caps, i);
243 gst_structure_set_parent_refcount (structure, NULL);
244 gst_structure_free (structure);
246 g_ptr_array_free (caps->structs, TRUE);
248 memset (caps, 0xff, sizeof (GstCaps));
250 gst_atomic_int_destroy (&(caps)->refcount);
255 * gst_caps_make_writable:
256 * @caps: the #GstCaps to make writable
258 * Returns a writable copy of @caps.
260 * If there is only one reference count on @caps, the caller must be the owner,
261 * and so this function will return the caps object unchanged. If on the other
262 * hand there is more than one reference on the object, a new caps object will
263 * be returned. The caller's reference on @caps will be removed, and instead the
264 * caller will own a reference to the returned object.
266 * In short, this function unrefs the caps in the argument and refs the caps
267 * that it returns. Don't access the argument after calling this function. See
268 * also: gst_caps_ref().
270 * Returns: the same #GstCaps object.
273 gst_caps_make_writable (GstCaps * caps)
277 g_return_val_if_fail (caps != NULL, NULL);
279 /* we are the only instance reffing this caps */
280 if (gst_atomic_int_read (&caps->refcount) == 1)
284 copy = gst_caps_copy (caps);
285 gst_caps_unref (caps);
292 * @caps: the #GstCaps to reference
294 * Add a reference to a #GstCaps object.
296 * From this point on, until the caller calls gst_caps_unref() or
297 * gst_caps_make_writable(), it is guaranteed that the caps object will not
298 * change. This means its structures won't change, etc. To use a #GstCaps
299 * object, you must always have a refcount on it -- either the one made
300 * implicitly by gst_caps_new(), or via taking one explicitly with this
303 * Returns: the same #GstCaps object.
306 gst_caps_ref (GstCaps * caps)
308 g_return_val_if_fail (caps != NULL, NULL);
310 #ifdef DEBUG_REFCOUNT
311 GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
312 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) + 1);
315 gst_atomic_int_inc (&caps->refcount);
322 * @caps: the #GstCaps to unref
324 * Unref a #GstCaps and and free all its structures and the
325 * structures' values when the refcount reaches 0.
328 gst_caps_unref (GstCaps * caps)
330 g_return_if_fail (caps != NULL);
331 g_return_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0);
333 #ifdef DEBUG_REFCOUNT
334 GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
335 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) - 1);
338 /* if we ended up with the refcount at zero, free the caps */
339 if (gst_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: the new #GstCaps
353 gst_static_caps_get (GstStaticCaps * static_caps)
355 GstCaps *caps = (GstCaps *) static_caps;
358 if (caps->type == 0) {
359 caps->type = GST_TYPE_CAPS;
360 /* initialize the caps to a refcount of 1 so the caps can be writable... */
361 gst_atomic_int_init (&(caps)->refcount, 1);
362 caps->structs = g_ptr_array_new ();
363 ret = gst_caps_from_string_inplace (caps, static_caps->string);
366 g_critical ("Could not convert static caps \"%s\"", static_caps->string);
368 /* and now that we return it to the user, keep a ref for ourselves. This
369 * makes the caps immutable... AND INVINCIBLE! WOULD YOU LIKE TO TRY MY
370 * IMMUTABLE CAPS STYLE? I AM A CAPS WARRIOR!!! */
379 static GstStructure *
380 gst_caps_remove_and_get_structure (GstCaps * caps, guint idx)
382 /* don't use index_fast, gst_caps_simplify relies on the order */
383 GstStructure *s = g_ptr_array_remove_index (caps->structs, idx);
385 gst_structure_set_parent_refcount (s, NULL);
391 * @caps1: the #GstCaps that will be appended to
392 * @caps2: the #GstCaps to append
394 * Appends the structures contained in @caps2 to @caps1. The structures in
395 * @caps2 are not copied -- they are transferred to @caps1, and then @caps2 is
396 * freed. If either caps is ANY, the resulting caps will be ANY.
399 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
401 GstStructure *structure;
404 g_return_if_fail (GST_IS_CAPS (caps1));
405 g_return_if_fail (GST_IS_CAPS (caps2));
406 g_return_if_fail (IS_WRITABLE (caps1));
407 g_return_if_fail (IS_WRITABLE (caps2));
412 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2)) {
413 /* FIXME: this leaks */
414 caps1->flags |= GST_CAPS_FLAGS_ANY;
415 for (i = caps2->structs->len - 1; i >= 0; i--) {
416 structure = gst_caps_remove_and_get_structure (caps2, i);
417 gst_structure_free (structure);
420 int len = caps2->structs->len;
422 for (i = 0; i < len; i++) {
423 structure = gst_caps_remove_and_get_structure (caps2, 0);
424 gst_caps_append_structure (caps1, structure);
427 gst_caps_unref (caps2); /* guaranteed to free it */
431 * gst_caps_append_structure:
432 * @caps: the #GstCaps that will be appended to
433 * @structure: the #GstStructure to append
435 * Appends @structure to @caps. The structure is not copied; @caps
436 * becomes the owner of @structure.
439 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
441 g_return_if_fail (GST_IS_CAPS (caps));
442 g_return_if_fail (IS_WRITABLE (caps));
445 g_return_if_fail (structure->parent_refcount == NULL);
448 STRUCTURE_POISON (structure);
451 gst_structure_set_parent_refcount (structure, &caps->refcount);
452 g_ptr_array_add (caps->structs, structure);
457 * gst_caps_remove_structure:
458 * @caps: the #GstCaps to remove from
459 * @idx: Index of the structure to remove
461 * removes the stucture with the given index from the list of structures
462 * contained in @caps.
465 gst_caps_remove_structure (GstCaps * caps, guint idx)
467 GstStructure *structure;
469 g_return_if_fail (caps != NULL);
470 g_return_if_fail (idx <= gst_caps_get_size (caps));
471 g_return_if_fail (IS_WRITABLE (caps));
473 structure = gst_caps_remove_and_get_structure (caps, idx);
474 gst_structure_free (structure);
478 * gst_caps_split_one:
481 * This function is not implemented.
486 gst_caps_split_one (GstCaps * caps)
489 g_critical ("unimplemented");
498 * Gets the number of structures contained in @caps.
500 * Returns: the number of structures that @caps contains
503 gst_caps_get_size (const GstCaps * caps)
505 g_return_val_if_fail (GST_IS_CAPS (caps), 0);
507 return caps->structs->len;
511 * gst_caps_get_structure:
513 * @index: the index of the structure
515 * Finds the structure in @caps that has the index @index, and
518 * WARNING: This function takes a const GstCaps *, but returns a
519 * non-const GstStructure *. This is for programming convenience --
520 * the caller should be aware that structures inside a constant
521 * @GstCaps should not be modified.
523 * Returns: a pointer to the #GstStructure corresponding to @index
526 gst_caps_get_structure (const GstCaps * caps, int index)
528 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
529 g_return_val_if_fail (index >= 0, NULL);
530 g_return_val_if_fail (index < caps->structs->len, NULL);
532 return g_ptr_array_index (caps->structs, index);
537 * @caps: the @GstCaps to copy
538 * @nth: the nth structure to copy
540 * Creates a new @GstCaps and appends a copy of the nth structure
541 * contained in @caps.
543 * Returns: the new @GstCaps
546 gst_caps_copy_nth (const GstCaps * caps, gint nth)
549 GstStructure *structure;
551 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
553 newcaps = gst_caps_new_empty ();
554 newcaps->flags = caps->flags;
556 if (caps->structs->len > nth) {
557 structure = gst_caps_get_structure (caps, nth);
558 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
565 * gst_caps_set_simple:
566 * @caps: the @GstCaps to set
567 * @field: first field to set
568 * @...: additional parameters
570 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
571 * only has one structure. The arguments must be passed in the same
572 * manner as @gst_structure_set(), and be NULL-terminated.
575 gst_caps_set_simple (GstCaps * caps, char *field, ...)
577 GstStructure *structure;
580 g_return_if_fail (GST_IS_CAPS (caps));
581 g_return_if_fail (caps->structs->len == 1);
582 g_return_if_fail (IS_WRITABLE (caps));
584 structure = gst_caps_get_structure (caps, 0);
586 va_start (var_args, field);
587 gst_structure_set_valist (structure, field, var_args);
592 * gst_caps_set_simple_valist:
593 * @caps: the @GstCaps to copy
594 * @field: first field to set
595 * @varargs: additional parameters
597 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
598 * only has one structure. The arguments must be passed in the same
599 * manner as @gst_structure_set(), and be NULL-terminated.
602 gst_caps_set_simple_valist (GstCaps * caps, char *field, va_list varargs)
604 GstStructure *structure;
606 g_return_if_fail (GST_IS_CAPS (caps));
607 g_return_if_fail (caps->structs->len != 1);
608 g_return_if_fail (IS_WRITABLE (caps));
610 structure = gst_caps_get_structure (caps, 0);
612 gst_structure_set_valist (structure, field, varargs);
619 * @caps: the @GstCaps to test
621 * Determines if @caps represents any media format.
623 * Returns: TRUE if @caps represents any format.
626 gst_caps_is_any (const GstCaps * caps)
628 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
630 return (caps->flags & GST_CAPS_FLAGS_ANY);
635 * @caps: the @GstCaps to test
637 * Determines if @caps represents no media formats.
639 * Returns: TRUE if @caps represents no formats.
642 gst_caps_is_empty (const GstCaps * caps)
644 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
646 if (caps->flags & GST_CAPS_FLAGS_ANY)
649 return (caps->structs == NULL) || (caps->structs->len == 0);
653 gst_caps_is_fixed_foreach (GQuark field_id, const GValue * value,
656 return gst_value_is_fixed (value);
661 * @caps: the @GstCaps to test
663 * Fixed @GstCaps describe exactly one format, that is, they have exactly
664 * one structure, and each field in the structure describes a fixed type.
665 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
667 * Returns: TRUE if @caps is fixed
670 gst_caps_is_fixed (const GstCaps * caps)
672 GstStructure *structure;
674 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
676 if (caps->structs->len != 1)
679 structure = gst_caps_get_structure (caps, 0);
681 return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
685 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
688 GstStructure *struct1 = (GstStructure *) data;
689 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
693 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
701 * gst_caps_is_equal_fixed:
702 * @caps1: the #GstCaps to test
703 * @caps2: the #GstCaps to test
705 * Tests if two #GstCaps are equal. This function only works on fixed
708 * Returns: TRUE if the arguments represent the same format
711 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
713 GstStructure *struct1, *struct2;
715 g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
716 g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
718 struct1 = gst_caps_get_structure (caps1, 0);
719 struct2 = gst_caps_get_structure (caps2, 0);
721 if (struct1->name != struct2->name) {
724 if (struct1->fields->len != struct2->fields->len) {
728 return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
733 * gst_caps_is_always_compatible:
734 * @caps1: the #GstCaps to test
735 * @caps2: the #GstCaps to test
737 * A given #GstCaps structure is always compatible with another if
738 * every media format that is in the first is also contained in the
739 * second. That is, @caps1 is a subset of @caps2.
741 * Returns: TRUE if @caps1 is a subset of @caps2.
744 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
746 g_return_val_if_fail (GST_IS_CAPS (caps1), FALSE);
747 g_return_val_if_fail (GST_IS_CAPS (caps2), FALSE);
749 return gst_caps_is_subset (caps1, caps2);
753 * gst_caps_is_subset:
754 * @subset: a #GstCaps
755 * @superset: a potentially greater #GstCaps
757 * Checks if all caps represented by @subset are also represented by @superset
758 * <note>This function does not work reliably if optional properties for caps
759 * are included on one caps and omitted on the other.</note>
761 * Returns: TRUE if @subset is a subset of @superset
764 gst_caps_is_subset (const GstCaps * subset, const GstCaps * superset)
769 g_return_val_if_fail (subset != NULL, FALSE);
770 g_return_val_if_fail (superset != NULL, FALSE);
772 if (gst_caps_is_empty (subset) || gst_caps_is_any (superset))
774 if (gst_caps_is_any (subset) || gst_caps_is_empty (superset))
777 caps = gst_caps_subtract (subset, superset);
778 ret = gst_caps_is_empty (caps);
779 gst_caps_unref (caps);
786 * @caps2: another #GstCaps
788 * Checks if the given caps represent the same set of caps.
789 * <note>This function does not work reliably if optional properties for caps
790 * are included on one caps and omitted on the other.</note>
792 * Returns: TRUE if both caps are equal
795 gst_caps_is_equal (const GstCaps * caps1, const GstCaps * caps2)
797 g_return_val_if_fail (caps1 != NULL, FALSE);
798 g_return_val_if_fail (caps2 != NULL, FALSE);
800 if (gst_caps_is_fixed (caps1) && gst_caps_is_fixed (caps2))
801 return gst_caps_is_equal_fixed (caps1, caps2);
803 return gst_caps_is_subset (caps1, caps2) && gst_caps_is_subset (caps2, caps1);
809 const GstStructure *intersect;
815 gst_caps_structure_intersect_field (GQuark id, const GValue * val1,
818 IntersectData *idata = (IntersectData *) data;
819 GValue dest_value = { 0 };
820 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
823 gst_structure_id_set_value (idata->dest, id, val1);
824 } else if (idata->first_run) {
825 if (gst_value_intersect (&dest_value, val1, val2)) {
826 gst_structure_id_set_value (idata->dest, id, &dest_value);
827 g_value_unset (&dest_value);
836 static GstStructure *
837 gst_caps_structure_intersect (const GstStructure * struct1,
838 const GstStructure * struct2)
842 g_return_val_if_fail (struct1 != NULL, NULL);
843 g_return_val_if_fail (struct2 != NULL, NULL);
845 if (struct1->name != struct2->name)
848 data.dest = gst_structure_id_empty_new (struct1->name);
849 data.intersect = struct2;
850 data.first_run = TRUE;
851 if (!gst_structure_foreach ((GstStructure *) struct1,
852 gst_caps_structure_intersect_field, &data))
855 data.intersect = struct1;
856 data.first_run = FALSE;
857 if (!gst_structure_foreach ((GstStructure *) struct2,
858 gst_caps_structure_intersect_field, &data))
864 gst_structure_free (data.dest);
869 static GstStructure *
870 gst_caps_structure_union (const GstStructure * struct1,
871 const GstStructure * struct2)
875 const GstStructureField *field1;
876 const GstStructureField *field2;
879 /* FIXME this doesn't actually work */
881 if (struct1->name != struct2->name)
884 dest = gst_structure_id_empty_new (struct1->name);
886 for (i = 0; i < struct1->fields->len; i++) {
887 GValue dest_value = { 0 };
889 field1 = GST_STRUCTURE_FIELD (struct1, i);
890 field2 = gst_structure_id_get_field (struct2, field1->name);
892 if (field2 == NULL) {
895 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
896 gst_structure_set_value (dest, g_quark_to_string (field1->name),
899 ret = gst_value_compare (&field1->value, &field2->value);
911 * gst_caps_intersect:
912 * @caps1: a #GstCaps to intersect
913 * @caps2: a #GstCaps to intersect
915 * Creates a new #GstCaps that contains all the formats that are common
916 * to both @caps1 and @caps2.
918 * Returns: the new #GstCaps
921 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
924 GstStructure *struct1;
925 GstStructure *struct2;
927 GstStructure *istruct;
929 g_return_val_if_fail (GST_IS_CAPS (caps1), NULL);
930 g_return_val_if_fail (GST_IS_CAPS (caps2), NULL);
932 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)) {
933 return gst_caps_new_empty ();
935 if (gst_caps_is_any (caps1))
936 return gst_caps_copy (caps2);
937 if (gst_caps_is_any (caps2))
938 return gst_caps_copy (caps1);
940 dest = gst_caps_new_empty ();
942 /* run zigzag on top line then right line, this preserves the caps order
943 * much better than a simple loop.
945 * This algorithm zigzags over the caps structures as demonstrated in
946 * the folowing matrix:
954 * First we iterate over the caps1 structures (top line) intersecting
955 * the structures diagonally down, then we iterate over the caps2
958 for (i = 0; i < caps1->structs->len + caps2->structs->len - 1; i++) {
959 /* caps1 index goes from 0 to caps1->structs->len-1 */
960 j = MIN (i, caps1->structs->len - 1);
961 /* caps2 index stays 0 until i reaches caps1->structs->len, then it counts
962 * up from 1 to caps2->structs->len - 1 */
965 /* now run the diagonal line, end condition is the left or bottom
967 while (k < caps2->structs->len && j >= 0) {
968 struct1 = gst_caps_get_structure (caps1, j);
969 struct2 = gst_caps_get_structure (caps2, k);
971 istruct = gst_caps_structure_intersect (struct1, struct2);
973 gst_caps_append_structure (dest, istruct);
979 gst_caps_do_simplify (dest);
985 const GstStructure *subtract_from;
992 gst_caps_structure_subtract_field (GQuark field_id, const GValue * value,
995 SubtractionEntry *e = user_data;
996 GValue subtraction = { 0, };
998 GstStructure *structure;
1000 other = gst_structure_id_get_value (e->subtract_from, field_id);
1004 if (!gst_value_subtract (&subtraction, other, value))
1006 if (gst_value_compare (&subtraction, other) == GST_VALUE_EQUAL) {
1007 g_value_unset (&subtraction);
1010 structure = gst_structure_copy (e->subtract_from);
1011 gst_structure_id_set_value (structure, field_id, &subtraction);
1012 g_value_unset (&subtraction);
1013 e->put_into = g_slist_prepend (e->put_into, structure);
1019 gst_caps_structure_subtract (GSList ** into, const GstStructure * minuend,
1020 const GstStructure * subtrahend)
1025 e.subtract_from = minuend;
1028 ret = gst_structure_foreach ((GstStructure *) subtrahend,
1029 gst_caps_structure_subtract_field, &e);
1035 for (walk = e.put_into; walk; walk = g_slist_next (walk)) {
1036 gst_structure_free (walk->data);
1038 g_slist_free (e.put_into);
1044 * gst_caps_subtract:
1045 * @minuend: #GstCaps to substract from
1046 * @subtrahend: #GstCaps to substract
1048 * Subtracts the @subtrahend from the @minuend.
1049 * <note>This function does not work reliably if optional properties for caps
1050 * are included on one caps and omitted on the other.</note>
1052 * Returns: the resulting caps
1055 gst_caps_subtract (const GstCaps * minuend, const GstCaps * subtrahend)
1060 GstCaps *dest = NULL, *src;
1062 g_return_val_if_fail (minuend != NULL, NULL);
1063 g_return_val_if_fail (subtrahend != NULL, NULL);
1065 if (gst_caps_is_empty (minuend) || gst_caps_is_any (subtrahend)) {
1066 return gst_caps_new_empty ();
1068 if (gst_caps_is_empty (subtrahend))
1069 return gst_caps_copy (minuend);
1071 /* FIXME: Do we want this here or above?
1072 The reason we need this is that there is no definition about what
1073 ANY means for specific types, so it's not possible to reduce ANY partially
1074 You can only remove everything or nothing and that is done above.
1075 Note: there's a test that checks this behaviour. */
1076 g_return_val_if_fail (!gst_caps_is_any (minuend), NULL);
1077 g_assert (subtrahend->structs->len > 0);
1079 src = gst_caps_copy (minuend);
1080 for (i = 0; i < subtrahend->structs->len; i++) {
1081 sub = gst_caps_get_structure (subtrahend, i);
1083 gst_caps_unref (src);
1086 dest = gst_caps_new_empty ();
1087 for (j = 0; j < src->structs->len; j++) {
1088 min = gst_caps_get_structure (src, j);
1089 if (gst_structure_get_name_id (min) == gst_structure_get_name_id (sub)) {
1092 if (gst_caps_structure_subtract (&list, min, sub)) {
1095 for (walk = list; walk; walk = g_slist_next (walk)) {
1096 gst_caps_append_structure (dest, (GstStructure *) walk->data);
1098 g_slist_free (list);
1100 gst_caps_append_structure (dest, gst_structure_copy (min));
1103 gst_caps_append_structure (dest, gst_structure_copy (min));
1106 if (gst_caps_is_empty (dest)) {
1107 gst_caps_unref (src);
1112 gst_caps_unref (src);
1113 gst_caps_do_simplify (dest);
1119 * @caps1: a #GstCaps to union
1120 * @caps2: a #GstCaps to union
1122 * Creates a new #GstCaps that contains all the formats that are in
1123 * either @caps1 and @caps2.
1125 * Returns: the new #GstCaps
1128 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
1133 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2))
1134 return gst_caps_new_any ();
1136 dest1 = gst_caps_copy (caps1);
1137 dest2 = gst_caps_copy (caps2);
1138 gst_caps_append (dest1, dest2);
1140 gst_caps_do_simplify (dest1);
1144 typedef struct _NormalizeForeach
1147 GstStructure *structure;
1152 gst_caps_normalize_foreach (GQuark field_id, const GValue * value, gpointer ptr)
1154 NormalizeForeach *nf = (NormalizeForeach *) ptr;
1158 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1159 for (i = 1; i < gst_value_list_get_size (value); i++) {
1160 const GValue *v = gst_value_list_get_value (value, i);
1161 GstStructure *structure = gst_structure_copy (nf->structure);
1163 gst_structure_id_set_value (structure, field_id, v);
1164 gst_caps_append_structure (nf->caps, structure);
1167 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
1168 gst_structure_id_set_value (nf->structure, field_id, &val);
1169 g_value_unset (&val);
1177 * gst_caps_normalize:
1178 * @caps: a #GstCaps to normalize
1180 * Creates a new #GstCaps that represents the same set of formats as
1181 * @caps, but contains no lists. Each list is expanded into separate
1184 * Returns: the new #GstCaps
1187 gst_caps_normalize (const GstCaps * caps)
1189 NormalizeForeach nf;
1193 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
1195 newcaps = gst_caps_copy (caps);
1198 for (i = 0; i < newcaps->structs->len; i++) {
1199 nf.structure = gst_caps_get_structure (newcaps, i);
1201 while (!gst_structure_foreach (nf.structure,
1202 gst_caps_normalize_foreach, &nf));
1209 gst_caps_compare_structures (gconstpointer one, gconstpointer two)
1212 const GstStructure *struct1 = *((const GstStructure **) one);
1213 const GstStructure *struct2 = *((const GstStructure **) two);
1215 /* FIXME: this orders aphabetically, but ordering the quarks might be faster
1216 So what's the best way? */
1217 ret = strcmp (gst_structure_get_name (struct1),
1218 gst_structure_get_name (struct2));
1222 return gst_structure_n_fields (struct2) - gst_structure_n_fields (struct1);
1226 * gst_caps_simplify:
1227 * @caps: a #GstCaps to simplify
1229 * Creates a new #GstCaps that represents the same set of formats as
1230 * @caps, but simpler. Component structures that are identical are
1231 * merged. Component structures that have ranges or lists that can
1232 * be merged are also merged.
1234 * Returns: the new #GstCaps
1237 gst_caps_simplify (const GstCaps * caps)
1241 g_return_val_if_fail (caps != NULL, NULL);
1243 ret = gst_caps_copy (caps);
1244 gst_caps_do_simplify (ret);
1253 GstStructure *compare;
1258 gst_caps_structure_figure_out_union (GQuark field_id, const GValue * value,
1261 UnionField *u = user_data;
1262 const GValue *val = gst_structure_id_get_value (u->compare, field_id);
1266 g_value_unset (&u->value);
1269 if (gst_value_compare (val, value) == GST_VALUE_EQUAL)
1272 g_value_unset (&u->value);
1276 gst_value_union (&u->value, val, value);
1281 gst_caps_structure_simplify (GstStructure ** result,
1282 const GstStructure * simplify, GstStructure * compare)
1285 UnionField field = { 0, {0,}, NULL };
1287 /* try to subtract to get a real subset */
1288 if (gst_caps_structure_subtract (&list, simplify, compare)) {
1289 switch (g_slist_length (list)) {
1294 *result = list->data;
1295 g_slist_free (list);
1301 for (walk = list; walk; walk = g_slist_next (walk)) {
1302 gst_structure_free (walk->data);
1304 g_slist_free (list);
1310 /* try to union both structs */
1311 field.compare = compare;
1312 if (gst_structure_foreach ((GstStructure *) simplify,
1313 gst_caps_structure_figure_out_union, &field)) {
1314 gboolean ret = FALSE;
1316 /* now we know all of simplify's fields are the same in compare
1317 * but at most one field: field.name */
1318 if (G_IS_VALUE (&field.value)) {
1319 if (gst_structure_n_fields (simplify) == gst_structure_n_fields (compare)) {
1320 gst_structure_id_set_value (compare, field.name, &field.value);
1324 g_value_unset (&field.value);
1325 } else if (gst_structure_n_fields (simplify) <=
1326 gst_structure_n_fields (compare)) {
1327 /* compare is just more specific, will be optimized away later */
1328 /* FIXME: do this here? */
1329 GST_LOG ("found a case that will be optimized later.");
1331 gchar *one = gst_structure_to_string (simplify);
1332 gchar *two = gst_structure_to_string (compare);
1335 ("caps mismatch: structures %s and %s claim to be possible to unify, but aren't",
1347 gst_caps_switch_structures (GstCaps * caps, GstStructure * old,
1348 GstStructure * new, gint i)
1350 gst_structure_set_parent_refcount (old, NULL);
1351 gst_structure_free (old);
1352 gst_structure_set_parent_refcount (new, &caps->refcount);
1353 g_ptr_array_index (caps->structs, i) = new;
1357 * gst_caps_do_simplify:
1358 * @caps: a #GstCaps to simplify
1360 * Modifies the given @caps inplace into a representation that represents the
1361 * same set of formats, but in a simpler form. Component structures that are
1362 * identical are merged. Component structures that have values that can be
1363 * merged are also merged.
1365 * Returns: TRUE, if the caps could be simplified
1368 gst_caps_do_simplify (GstCaps * caps)
1370 GstStructure *simplify, *compare, *result;
1372 gboolean changed = FALSE;
1374 g_return_val_if_fail (caps != NULL, FALSE);
1375 g_return_val_if_fail (IS_WRITABLE (caps), FALSE);
1377 if (gst_caps_get_size (caps) < 2)
1380 g_ptr_array_sort (caps->structs, gst_caps_compare_structures);
1382 start = caps->structs->len - 1;
1383 for (i = caps->structs->len - 1; i >= 0; i--) {
1384 simplify = gst_caps_get_structure (caps, i);
1385 if (gst_structure_get_name_id (simplify) !=
1386 gst_structure_get_name_id (gst_caps_get_structure (caps, start)))
1388 for (j = start; j >= 0; j--) {
1391 compare = gst_caps_get_structure (caps, j);
1392 if (gst_structure_get_name_id (simplify) !=
1393 gst_structure_get_name_id (compare)) {
1396 if (gst_caps_structure_simplify (&result, simplify, compare)) {
1398 g_print ("%s - %s = %s\n",
1399 gst_structure_to_string (simplify),
1400 gst_structure_to_string (compare),
1401 result ? gst_structure_to_string (result) : "---");
1404 gst_caps_switch_structures (caps, simplify, result, i);
1406 gst_caps_remove_structure (caps, i);
1418 /* gst_caps_do_simplify (caps); */
1422 #ifndef GST_DISABLE_LOADSAVE
1424 * gst_caps_save_thyself:
1425 * @caps: a #GstCaps structure
1426 * @parent: a XML parent node
1428 * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1430 * Returns: a XML node pointer
1433 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1435 char *s = gst_caps_to_string (caps);
1437 xmlNewChild (parent, NULL, "caps", s);
1443 * gst_caps_load_thyself:
1444 * @parent: a XML node
1446 * Creates a #GstCaps from its XML serialization.
1448 * Returns: a new #GstCaps structure
1451 gst_caps_load_thyself (xmlNodePtr parent)
1453 if (strcmp ("caps", parent->name) == 0) {
1454 return gst_caps_from_string (xmlNodeGetContent (parent));
1465 * @caps: a pointer to #GstCaps
1466 * @newcaps: a #GstCaps to replace *caps
1468 * Replaces *caps with @newcaps. Frees the #GstCaps in the location
1469 * pointed to by @caps, if applicable, then modifies @caps to point to
1473 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1475 #if 0 /* disable this, since too many plugins rely on undefined behavior */
1476 #ifdef USE_POISONING
1477 //if (newcaps) CAPS_POISON (newcaps);
1481 gst_caps_unref (*caps);
1486 * gst_caps_to_string:
1489 * Converts @caps to a string representation. This string representation
1490 * can be converted back to a #GstCaps by #gst_caps_from_string.
1492 * Returns: a newly allocated string representing @caps.
1495 gst_caps_to_string (const GstCaps * caps)
1498 GstStructure *structure;
1502 /* NOTE: This function is potentially called by the debug system,
1503 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1504 * should be careful to avoid recursion. This includes any functions
1505 * called by gst_caps_to_string. In particular, calls should
1506 * not use the GST_PTR_FORMAT extension. */
1508 /* FIXME does this leak? */
1511 return g_strdup ("NULL");
1513 if (gst_caps_is_any (caps)) {
1514 return g_strdup ("ANY");
1516 if (gst_caps_is_empty (caps)) {
1517 return g_strdup ("EMPTY");
1519 s = g_string_new ("");
1520 structure = gst_caps_get_structure (caps, 0);
1521 sstr = gst_structure_to_string (structure);
1522 g_string_append (s, sstr);
1525 for (i = 1; i < caps->structs->len; i++) {
1526 structure = gst_caps_get_structure (caps, i);
1528 g_string_append (s, "; ");
1529 sstr = gst_structure_to_string (structure);
1530 g_string_append (s, sstr);
1534 return g_string_free (s, FALSE);
1538 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1540 GstStructure *structure;
1543 g_return_val_if_fail (string, FALSE);
1544 if (strcmp ("ANY", string) == 0) {
1545 caps->flags = GST_CAPS_FLAGS_ANY;
1548 if (strcmp ("EMPTY", string) == 0) {
1552 structure = gst_structure_from_string (string, &s);
1553 if (structure == NULL) {
1556 gst_caps_append_structure (caps, structure);
1560 while (g_ascii_isspace (*s))
1562 structure = gst_structure_from_string (s, &s);
1563 if (structure == NULL) {
1566 gst_caps_append_structure (caps, structure);
1567 while (g_ascii_isspace (*s))
1579 * gst_caps_from_string:
1580 * @string: a string to convert to #GstCaps
1582 * Converts @caps from a string representation.
1584 * Returns: a newly allocated #GstCaps
1587 gst_caps_from_string (const gchar * string)
1591 caps = gst_caps_new_empty ();
1592 if (gst_caps_from_string_inplace (caps, string)) {
1595 gst_caps_unref (caps);
1601 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1603 g_return_if_fail (G_IS_VALUE (src_value));
1604 g_return_if_fail (G_IS_VALUE (dest_value));
1605 g_return_if_fail (G_VALUE_HOLDS (src_value, GST_TYPE_CAPS));
1606 g_return_if_fail (G_VALUE_HOLDS (dest_value, G_TYPE_STRING)
1607 || G_VALUE_HOLDS (dest_value, G_TYPE_POINTER));
1609 dest_value->data[0].v_pointer =
1610 gst_caps_to_string (src_value->data[0].v_pointer);
1614 gst_caps_copy_conditional (GstCaps * src)
1617 return gst_caps_ref (src);