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.
22 * @short_description: Structure describing sets of media formats
23 * @see_also: #GstStructure
25 * Caps (capabilities) are lighweight refcounted objects describing media types.
26 * They are composed of an array of #GstStructure.
28 * Caps are exposed on #GstPadTemplate to describe all possible types a
29 * given pad can handle. They are also stored in the #GstRegistry along with
30 * a description of the #GstElement.
32 * Caps are exposed on the element pads using the gst_pad_get_caps() pad
33 * function. This function describes the possible types that the pad can
34 * handle or produce at runtime.
36 * Caps are also attached to buffers to describe to content of the data
37 * pointed to by the buffer with gst_buffer_set_caps(). Caps attached to
38 * a #GstBuffer allow for format negotiation upstream and downstream.
40 * A #GstCaps can be constructed with the following code fragment:
43 * <title>Creating caps</title>
46 * caps = gst_caps_new_simple ("video/x-raw-yuv",
47 * "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I', '4', '2', '0'),
48 * "framerate", GST_TYPE_FRACTION, 25, 1,
49 * "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
50 * "width", G_TYPE_INT, 320,
51 * "height", G_TYPE_INT, 240,
56 * A #GstCaps is fixed when it has no properties with ranges or lists. Use
57 * gst_caps_is_fixed() to test for fixed caps. Only fixed caps can be
58 * set on a #GstPad or #GstBuffer.
60 * Various methods exist to work with the media types such as subtracting
63 * Last reviewed on 2007-02-13 (0.10.10)
72 #include "gst_private.h"
75 #define DEBUG_REFCOUNT
77 #define CAPS_POISON(caps) G_STMT_START{ \
79 GstCaps *_newcaps = gst_caps_copy (caps); \
80 gst_caps_unref(caps); \
84 #define STRUCTURE_POISON(structure) G_STMT_START{ \
86 GstStructure *_newstruct = gst_structure_copy (structure); \
87 gst_structure_free(structure); \
88 structure = _newstruct; \
91 #define IS_WRITABLE(caps) \
92 (g_atomic_int_get (&(caps)->refcount) == 1)
94 /* lock to protect multiple invocations of static caps to caps conversion */
95 G_LOCK_DEFINE_STATIC (static_caps_lock);
97 static void gst_caps_transform_to_string (const GValue * src_value,
99 static gboolean gst_caps_from_string_inplace (GstCaps * caps,
100 const gchar * string);
101 static GstCaps *gst_caps_copy_conditional (GstCaps * src);
104 gst_caps_get_type (void)
106 static GType gst_caps_type = 0;
108 if (G_UNLIKELY (gst_caps_type == 0)) {
109 gst_caps_type = g_boxed_type_register_static ("GstCaps",
110 (GBoxedCopyFunc) gst_caps_copy_conditional,
111 (GBoxedFreeFunc) gst_caps_unref);
113 g_value_register_transform_func (gst_caps_type,
114 G_TYPE_STRING, gst_caps_transform_to_string);
117 return gst_caps_type;
120 /* creation/deletion */
123 * gst_caps_new_empty:
125 * Creates a new #GstCaps that is empty. That is, the returned
126 * #GstCaps contains no media formats.
127 * Caller is responsible for unreffing the returned caps.
129 * Returns: the new #GstCaps
132 gst_caps_new_empty (void)
134 GstCaps *caps = g_slice_new (GstCaps);
136 caps->type = GST_TYPE_CAPS;
139 caps->structs = g_ptr_array_new ();
141 #ifdef DEBUG_REFCOUNT
142 GST_CAT_LOG (GST_CAT_CAPS, "created caps %p", caps);
151 * Creates a new #GstCaps that indicates that it is compatible with
154 * Returns: the new #GstCaps
157 gst_caps_new_any (void)
159 GstCaps *caps = gst_caps_new_empty ();
161 caps->flags = GST_CAPS_FLAGS_ANY;
167 * gst_caps_new_simple:
168 * @media_type: the media type of the structure
169 * @fieldname: first field to set
170 * @...: additional arguments
172 * Creates a new #GstCaps that contains one #GstStructure. The
173 * structure is defined by the arguments, which have the same format
174 * as gst_structure_new().
175 * Caller is responsible for unreffing the returned caps.
177 * Returns: the new #GstCaps
180 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
183 GstStructure *structure;
186 caps = gst_caps_new_empty ();
188 va_start (var_args, fieldname);
189 structure = gst_structure_new_valist (media_type, fieldname, var_args);
192 gst_caps_append_structure (caps, structure);
199 * @struct1: the first structure to add
200 * @...: additional structures to add
202 * Creates a new #GstCaps and adds all the structures listed as
203 * arguments. The list must be NULL-terminated. The structures
204 * are not copied; the returned #GstCaps owns the structures.
206 * Returns: the new #GstCaps
209 gst_caps_new_full (GstStructure * struct1, ...)
214 va_start (var_args, struct1);
215 caps = gst_caps_new_full_valist (struct1, var_args);
222 * gst_caps_new_full_valist:
223 * @structure: the first structure to add
224 * @var_args: additional structures to add
226 * Creates a new #GstCaps and adds all the structures listed as
227 * arguments. The list must be NULL-terminated. The structures
228 * are not copied; the returned #GstCaps owns the structures.
230 * Returns: the new #GstCaps
233 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
237 caps = gst_caps_new_empty ();
240 gst_caps_append_structure (caps, structure);
241 structure = va_arg (var_args, GstStructure *);
249 * @caps: the #GstCaps to copy
251 * Creates a new #GstCaps as a copy of the old @caps. The new caps will have a
252 * refcount of 1, owned by the caller. The structures are copied as well.
254 * Note that this function is the semantic equivalent of a gst_caps_ref()
255 * followed by a gst_caps_make_writable(). If you only want to hold on to a
256 * reference to the data, you should use gst_caps_ref().
258 * When you are finished with the caps, call gst_caps_unref() on it.
260 * Returns: the new #GstCaps
263 gst_caps_copy (const GstCaps * caps)
266 GstStructure *structure;
269 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
271 newcaps = gst_caps_new_empty ();
272 newcaps->flags = caps->flags;
274 for (i = 0; i < caps->structs->len; i++) {
275 structure = gst_caps_get_structure (caps, i);
276 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
283 _gst_caps_free (GstCaps * caps)
285 GstStructure *structure;
288 /* The refcount must be 0, but since we're only called by gst_caps_unref,
289 * don't bother testing. */
291 for (i = 0; i < caps->structs->len; i++) {
292 structure = (GstStructure *) gst_caps_get_structure (caps, i);
293 gst_structure_set_parent_refcount (structure, NULL);
294 gst_structure_free (structure);
296 g_ptr_array_free (caps->structs, TRUE);
298 memset (caps, 0xff, sizeof (GstCaps));
301 #ifdef DEBUG_REFCOUNT
302 GST_CAT_LOG (GST_CAT_CAPS, "freeing caps %p", caps);
304 g_slice_free (GstCaps, caps);
308 * gst_caps_make_writable:
309 * @caps: the #GstCaps to make writable
311 * Returns a writable copy of @caps.
313 * If there is only one reference count on @caps, the caller must be the owner,
314 * and so this function will return the caps object unchanged. If on the other
315 * hand there is more than one reference on the object, a new caps object will
316 * be returned. The caller's reference on @caps will be removed, and instead the
317 * caller will own a reference to the returned object.
319 * In short, this function unrefs the caps in the argument and refs the caps
320 * that it returns. Don't access the argument after calling this function. See
321 * also: gst_caps_ref().
323 * Returns: the same #GstCaps object.
326 gst_caps_make_writable (GstCaps * caps)
330 g_return_val_if_fail (caps != NULL, NULL);
332 /* we are the only instance reffing this caps */
333 if (g_atomic_int_get (&caps->refcount) == 1)
337 copy = gst_caps_copy (caps);
338 gst_caps_unref (caps);
345 * @caps: the #GstCaps to reference
347 * Add a reference to a #GstCaps object.
349 * From this point on, until the caller calls gst_caps_unref() or
350 * gst_caps_make_writable(), it is guaranteed that the caps object will not
351 * change. This means its structures won't change, etc. To use a #GstCaps
352 * object, you must always have a refcount on it -- either the one made
353 * implicitly by gst_caps_new(), or via taking one explicitly with this
356 * Returns: the same #GstCaps object.
359 gst_caps_ref (GstCaps * caps)
361 g_return_val_if_fail (caps != NULL, NULL);
363 #ifdef DEBUG_REFCOUNT
364 GST_CAT_LOG (GST_CAT_REFCOUNTING, "%p %d->%d", caps,
365 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) + 1);
367 g_return_val_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0, NULL);
369 g_atomic_int_inc (&caps->refcount);
376 * @caps: the #GstCaps to unref
378 * Unref a #GstCaps and and free all its structures and the
379 * structures' values when the refcount reaches 0.
382 gst_caps_unref (GstCaps * caps)
384 g_return_if_fail (caps != NULL);
386 #ifdef DEBUG_REFCOUNT
387 GST_CAT_LOG (GST_CAT_REFCOUNTING, "%p %d->%d", caps,
388 GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) - 1);
391 g_return_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0);
393 /* if we ended up with the refcount at zero, free the caps */
394 if (G_UNLIKELY (g_atomic_int_dec_and_test (&caps->refcount)))
395 _gst_caps_free (caps);
399 gst_static_caps_get_type (void)
401 static GType staticcaps_type = 0;
403 if (G_UNLIKELY (staticcaps_type == 0)) {
404 staticcaps_type = g_pointer_type_register_static ("GstStaticCaps");
406 return staticcaps_type;
411 * gst_static_caps_get:
412 * @static_caps: the #GstStaticCaps to convert
414 * Converts a #GstStaticCaps to a #GstCaps.
416 * Returns: A pointer to the #GstCaps. Unref after usage. Since the
417 * core holds an additional ref to the returned caps,
418 * use gst_caps_make_writable() on the returned caps to modify it.
421 gst_static_caps_get (GstStaticCaps * static_caps)
425 g_return_val_if_fail (static_caps != NULL, NULL);
427 caps = (GstCaps *) static_caps;
429 /* refcount is 0 when we need to convert */
430 if (G_UNLIKELY (g_atomic_int_get (&caps->refcount) == 0)) {
434 G_LOCK (static_caps_lock);
435 /* check if other thread already updated */
436 if (G_UNLIKELY (g_atomic_int_get (&caps->refcount) > 0))
439 string = static_caps->string;
441 if (G_UNLIKELY (string == NULL))
444 GST_CAT_LOG (GST_CAT_CAPS, "creating %p", static_caps);
446 /* we construct the caps on the stack, then copy over the struct into our
447 * real caps, refcount last. We do this because we must leave the refcount
448 * of the result caps to 0 so that other threads don't run away with the
449 * caps while we are constructing it. */
450 temp.type = GST_TYPE_CAPS;
452 temp.structs = g_ptr_array_new ();
454 /* initialize the caps to a refcount of 1 so the caps can be writable for
455 * the next statement */
458 /* convert to string */
459 if (G_UNLIKELY (!gst_caps_from_string_inplace (&temp, string)))
460 g_critical ("Could not convert static caps \"%s\"", string);
462 /* now copy stuff over to the real caps. */
463 caps->type = temp.type;
464 caps->flags = temp.flags;
465 caps->structs = temp.structs;
466 /* and bump the refcount so other threads can now read */
467 g_atomic_int_set (&caps->refcount, 1);
469 GST_CAT_LOG (GST_CAT_CAPS, "created %p", static_caps);
471 G_UNLOCK (static_caps_lock);
473 /* ref the caps, makes it not writable */
481 G_UNLOCK (static_caps_lock);
482 g_warning ("static caps %p string is NULL", static_caps);
488 static GstStructure *
489 gst_caps_remove_and_get_structure (GstCaps * caps, guint idx)
491 /* don't use index_fast, gst_caps_do_simplify relies on the order */
492 GstStructure *s = g_ptr_array_remove_index (caps->structs, idx);
494 gst_structure_set_parent_refcount (s, NULL);
499 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
502 GstStructure *struct1 = (GstStructure *) data;
503 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
507 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
515 gst_caps_structure_is_subset_field (GQuark field_id, const GValue * value,
518 GstStructure *subtract_from = user_data;
519 GValue subtraction = { 0, };
523 other = gst_structure_id_get_value (subtract_from, field_id);
525 /* field is missing in one set */
532 if (!gst_value_subtract (&subtraction, other, value)) {
533 /* empty result -> values are the same, or first was a value and
535 * verify that result is empty by swapping args */
536 if (!gst_value_subtract (&subtraction, value, other)) {
539 g_value_unset (&subtraction);
543 res = gst_value_compare (&subtraction, other);
544 g_value_unset (&subtraction);
546 if (res == GST_VALUE_EQUAL) {
547 /* value was empty ? */
555 gst_caps_structure_is_subset (const GstStructure * minuend,
556 const GstStructure * subtrahend)
558 if ((minuend->name != subtrahend->name) ||
559 (gst_structure_n_fields (minuend) !=
560 gst_structure_n_fields (subtrahend))) {
564 return gst_structure_foreach ((GstStructure *) subtrahend,
565 gst_caps_structure_is_subset_field, (gpointer) minuend);
570 * @caps1: the #GstCaps that will be appended to
571 * @caps2: the #GstCaps to append
573 * Appends the structures contained in @caps2 to @caps1. The structures in
574 * @caps2 are not copied -- they are transferred to @caps1, and then @caps2 is
575 * freed. If either caps is ANY, the resulting caps will be ANY.
578 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
580 GstStructure *structure;
583 g_return_if_fail (GST_IS_CAPS (caps1));
584 g_return_if_fail (GST_IS_CAPS (caps2));
585 g_return_if_fail (IS_WRITABLE (caps1));
586 g_return_if_fail (IS_WRITABLE (caps2));
591 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2)) {
592 /* FIXME: this leaks */
593 caps1->flags |= GST_CAPS_FLAGS_ANY;
594 for (i = caps2->structs->len - 1; i >= 0; i--) {
595 structure = gst_caps_remove_and_get_structure (caps2, i);
596 gst_structure_free (structure);
599 int len = caps2->structs->len;
601 for (i = 0; i < len; i++) {
602 structure = gst_caps_remove_and_get_structure (caps2, 0);
603 gst_caps_append_structure (caps1, structure);
606 gst_caps_unref (caps2); /* guaranteed to free it */
611 * @caps1: the #GstCaps that will take the new entries
612 * @caps2: the #GstCaps to merge in
614 * Appends the structures contained in @caps2 to @caps1 if they are not yet
615 * expressed by @caps1. The structures in @caps2 are not copied -- they are
616 * transferred to @caps1, and then @caps2 is freed.
617 * If either caps is ANY, the resulting caps will be ANY.
622 gst_caps_merge (GstCaps * caps1, GstCaps * caps2)
624 GstStructure *structure;
627 g_return_if_fail (GST_IS_CAPS (caps1));
628 g_return_if_fail (GST_IS_CAPS (caps2));
629 g_return_if_fail (IS_WRITABLE (caps1));
630 g_return_if_fail (IS_WRITABLE (caps2));
635 if (gst_caps_is_any (caps1)) {
636 for (i = caps2->structs->len - 1; i >= 0; i--) {
637 structure = gst_caps_remove_and_get_structure (caps2, i);
638 gst_structure_free (structure);
640 } else if (gst_caps_is_any (caps2)) {
641 caps1->flags |= GST_CAPS_FLAGS_ANY;
642 for (i = caps1->structs->len - 1; i >= 0; i--) {
643 structure = gst_caps_remove_and_get_structure (caps1, i);
644 gst_structure_free (structure);
647 int len = caps2->structs->len;
649 for (i = 0; i < len; i++) {
650 structure = gst_caps_remove_and_get_structure (caps2, 0);
651 gst_caps_merge_structure (caps1, structure);
654 GstCaps *com = gst_caps_intersect (caps1, caps2);
655 GstCaps *add = gst_caps_subtract (caps2, com);
657 GST_DEBUG ("common : %d", gst_caps_get_size (com));
658 GST_DEBUG ("adding : %d", gst_caps_get_size (add));
659 gst_caps_append (caps1, add);
660 gst_caps_unref (com);
663 gst_caps_unref (caps2); /* guaranteed to free it */
667 * gst_caps_append_structure:
668 * @caps: the #GstCaps that will be appended to
669 * @structure: the #GstStructure to append
671 * Appends @structure to @caps. The structure is not copied; @caps
672 * becomes the owner of @structure.
675 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
677 g_return_if_fail (GST_IS_CAPS (caps));
678 g_return_if_fail (IS_WRITABLE (caps));
680 if (G_LIKELY (structure)) {
681 g_return_if_fail (structure->parent_refcount == NULL);
684 STRUCTURE_POISON (structure);
687 gst_structure_set_parent_refcount (structure, &caps->refcount);
688 g_ptr_array_add (caps->structs, structure);
693 * gst_caps_remove_structure:
694 * @caps: the #GstCaps to remove from
695 * @idx: Index of the structure to remove
697 * removes the stucture with the given index from the list of structures
698 * contained in @caps.
701 gst_caps_remove_structure (GstCaps * caps, guint idx)
703 GstStructure *structure;
705 g_return_if_fail (caps != NULL);
706 g_return_if_fail (idx <= gst_caps_get_size (caps));
707 g_return_if_fail (IS_WRITABLE (caps));
709 structure = gst_caps_remove_and_get_structure (caps, idx);
710 gst_structure_free (structure);
714 * gst_caps_merge_structure:
715 * @caps: the #GstCaps that will the the new structure
716 * @structure: the #GstStructure to merge
718 * Appends @structure to @caps if its not already expressed by @caps. The
719 * structure is not copied; @caps becomes the owner of @structure.
722 gst_caps_merge_structure (GstCaps * caps, GstStructure * structure)
724 g_return_if_fail (GST_IS_CAPS (caps));
725 g_return_if_fail (IS_WRITABLE (caps));
727 if (G_LIKELY (structure)) {
728 GstStructure *structure1;
730 gboolean unique = TRUE;
732 g_return_if_fail (structure->parent_refcount == NULL);
735 STRUCTURE_POISON (structure);
738 /* check each structure */
739 for (i = caps->structs->len - 1; i >= 0; i--) {
740 structure1 = gst_caps_get_structure (caps, i);
741 /* if structure is a subset of structure1, then skip it */
742 if (gst_caps_structure_is_subset (structure1, structure)) {
748 gst_structure_set_parent_refcount (structure, &caps->refcount);
749 g_ptr_array_add (caps->structs, structure);
751 gst_structure_free (structure);
761 * Gets the number of structures contained in @caps.
763 * Returns: the number of structures that @caps contains
766 gst_caps_get_size (const GstCaps * caps)
768 g_return_val_if_fail (GST_IS_CAPS (caps), 0);
770 return caps->structs->len;
774 * gst_caps_get_structure:
776 * @index: the index of the structure
778 * Finds the structure in @caps that has the index @index, and
781 * WARNING: This function takes a const GstCaps *, but returns a
782 * non-const GstStructure *. This is for programming convenience --
783 * the caller should be aware that structures inside a constant
784 * #GstCaps should not be modified.
786 * Returns: a pointer to the #GstStructure corresponding to @index
789 gst_caps_get_structure (const GstCaps * caps, guint index)
791 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
792 g_return_val_if_fail (index < caps->structs->len, NULL);
794 return g_ptr_array_index (caps->structs, index);
799 * @caps: the #GstCaps to copy
800 * @nth: the nth structure to copy
802 * Creates a new #GstCaps and appends a copy of the nth structure
803 * contained in @caps.
805 * Returns: the new #GstCaps
808 gst_caps_copy_nth (const GstCaps * caps, guint nth)
811 GstStructure *structure;
813 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
815 newcaps = gst_caps_new_empty ();
816 newcaps->flags = caps->flags;
818 if (caps->structs->len > nth) {
819 structure = gst_caps_get_structure (caps, nth);
820 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
828 * @caps: the #GstCaps to truncate
830 * Destructively discard all but the first structure from @caps. Useful when
831 * fixating. @caps must be writable.
834 gst_caps_truncate (GstCaps * caps)
838 g_return_if_fail (GST_IS_CAPS (caps));
839 g_return_if_fail (IS_WRITABLE (caps));
841 i = caps->structs->len - 1;
844 gst_caps_remove_structure (caps, i--);
848 * gst_caps_set_simple:
849 * @caps: the #GstCaps to set
850 * @field: first field to set
851 * @...: additional parameters
853 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
854 * only has one structure. The arguments must be passed in the same
855 * manner as gst_structure_set(), and be NULL-terminated.
858 gst_caps_set_simple (GstCaps * caps, const char *field, ...)
860 GstStructure *structure;
863 g_return_if_fail (GST_IS_CAPS (caps));
864 g_return_if_fail (caps->structs->len == 1);
865 g_return_if_fail (IS_WRITABLE (caps));
867 structure = gst_caps_get_structure (caps, 0);
869 va_start (var_args, field);
870 gst_structure_set_valist (structure, field, var_args);
875 * gst_caps_set_simple_valist:
876 * @caps: the #GstCaps to copy
877 * @field: first field to set
878 * @varargs: additional parameters
880 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
881 * only has one structure. The arguments must be passed in the same
882 * manner as gst_structure_set(), and be NULL-terminated.
885 gst_caps_set_simple_valist (GstCaps * caps, const char *field, va_list varargs)
887 GstStructure *structure;
889 g_return_if_fail (GST_IS_CAPS (caps));
890 g_return_if_fail (caps->structs->len == 1);
891 g_return_if_fail (IS_WRITABLE (caps));
893 structure = gst_caps_get_structure (caps, 0);
895 gst_structure_set_valist (structure, field, varargs);
902 * @caps: the #GstCaps to test
904 * Determines if @caps represents any media format.
906 * Returns: TRUE if @caps represents any format.
909 gst_caps_is_any (const GstCaps * caps)
911 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
913 return (caps->flags & GST_CAPS_FLAGS_ANY);
918 * @caps: the #GstCaps to test
920 * Determines if @caps represents no media formats.
922 * Returns: TRUE if @caps represents no formats.
925 gst_caps_is_empty (const GstCaps * caps)
927 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
929 if (caps->flags & GST_CAPS_FLAGS_ANY)
932 return (caps->structs == NULL) || (caps->structs->len == 0);
936 gst_caps_is_fixed_foreach (GQuark field_id, const GValue * value,
939 return gst_value_is_fixed (value);
944 * @caps: the #GstCaps to test
946 * Fixed #GstCaps describe exactly one format, that is, they have exactly
947 * one structure, and each field in the structure describes a fixed type.
948 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
950 * Returns: TRUE if @caps is fixed
953 gst_caps_is_fixed (const GstCaps * caps)
955 GstStructure *structure;
957 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
959 if (caps->structs->len != 1)
962 structure = gst_caps_get_structure (caps, 0);
964 return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
968 * gst_caps_is_equal_fixed:
969 * @caps1: the #GstCaps to test
970 * @caps2: the #GstCaps to test
972 * Tests if two #GstCaps are equal. This function only works on fixed
975 * Returns: TRUE if the arguments represent the same format
978 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
980 GstStructure *struct1, *struct2;
982 g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
983 g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
985 struct1 = gst_caps_get_structure (caps1, 0);
986 struct2 = gst_caps_get_structure (caps2, 0);
988 if (struct1->name != struct2->name) {
991 if (struct1->fields->len != struct2->fields->len) {
995 return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
1000 * gst_caps_is_always_compatible:
1001 * @caps1: the #GstCaps to test
1002 * @caps2: the #GstCaps to test
1004 * A given #GstCaps structure is always compatible with another if
1005 * every media format that is in the first is also contained in the
1006 * second. That is, @caps1 is a subset of @caps2.
1008 * Returns: TRUE if @caps1 is a subset of @caps2.
1011 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
1013 g_return_val_if_fail (GST_IS_CAPS (caps1), FALSE);
1014 g_return_val_if_fail (GST_IS_CAPS (caps2), FALSE);
1016 return gst_caps_is_subset (caps1, caps2);
1020 * gst_caps_is_subset:
1021 * @subset: a #GstCaps
1022 * @superset: a potentially greater #GstCaps
1024 * Checks if all caps represented by @subset are also represented by @superset.
1025 * <note>This function does not work reliably if optional properties for caps
1026 * are included on one caps and omitted on the other.</note>
1028 * Returns: %TRUE if @subset is a subset of @superset
1031 gst_caps_is_subset (const GstCaps * subset, const GstCaps * superset)
1036 g_return_val_if_fail (subset != NULL, FALSE);
1037 g_return_val_if_fail (superset != NULL, FALSE);
1039 if (gst_caps_is_empty (subset) || gst_caps_is_any (superset))
1041 if (gst_caps_is_any (subset) || gst_caps_is_empty (superset))
1044 caps = gst_caps_subtract (subset, superset);
1045 ret = gst_caps_is_empty (caps);
1046 gst_caps_unref (caps);
1051 * gst_caps_is_equal:
1052 * @caps1: a #GstCaps
1053 * @caps2: another #GstCaps
1055 * Checks if the given caps represent the same set of caps.
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 * This function deals correctly with passing NULL for any of the caps.
1061 * Returns: TRUE if both caps are equal.
1064 gst_caps_is_equal (const GstCaps * caps1, const GstCaps * caps2)
1066 /* FIXME 0.11: NULL pointers are no valid Caps but indicate an error
1067 * So there should be an assertion that caps1 and caps2 != NULL */
1069 /* NULL <-> NULL is allowed here */
1073 /* one of them NULL => they are different (can't be both NULL because
1074 * we checked that above) */
1075 if (caps1 == NULL || caps2 == NULL)
1078 if (gst_caps_is_fixed (caps1) && gst_caps_is_fixed (caps2))
1079 return gst_caps_is_equal_fixed (caps1, caps2);
1081 return gst_caps_is_subset (caps1, caps2) && gst_caps_is_subset (caps2, caps1);
1087 const GstStructure *intersect;
1093 gst_caps_structure_intersect_field (GQuark id, const GValue * val1,
1096 IntersectData *idata = (IntersectData *) data;
1097 GValue dest_value = { 0 };
1098 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
1101 gst_structure_id_set_value (idata->dest, id, val1);
1102 } else if (idata->first_run) {
1103 if (gst_value_intersect (&dest_value, val1, val2)) {
1104 gst_structure_id_set_value (idata->dest, id, &dest_value);
1105 g_value_unset (&dest_value);
1114 static GstStructure *
1115 gst_caps_structure_intersect (const GstStructure * struct1,
1116 const GstStructure * struct2)
1120 g_return_val_if_fail (struct1 != NULL, NULL);
1121 g_return_val_if_fail (struct2 != NULL, NULL);
1123 if (struct1->name != struct2->name)
1126 data.dest = gst_structure_id_empty_new (struct1->name);
1127 data.intersect = struct2;
1128 data.first_run = TRUE;
1129 if (!gst_structure_foreach ((GstStructure *) struct1,
1130 gst_caps_structure_intersect_field, &data))
1133 data.intersect = struct1;
1134 data.first_run = FALSE;
1135 if (!gst_structure_foreach ((GstStructure *) struct2,
1136 gst_caps_structure_intersect_field, &data))
1142 gst_structure_free (data.dest);
1147 static GstStructure *
1148 gst_caps_structure_union (const GstStructure * struct1,
1149 const GstStructure * struct2)
1153 const GstStructureField *field1;
1154 const GstStructureField *field2;
1157 /* FIXME this doesn't actually work */
1159 if (struct1->name != struct2->name)
1162 dest = gst_structure_id_empty_new (struct1->name);
1164 for (i = 0; i < struct1->fields->len; i++) {
1165 GValue dest_value = { 0 };
1167 field1 = GST_STRUCTURE_FIELD (struct1, i);
1168 field2 = gst_structure_id_get_field (struct2, field1->name);
1170 if (field2 == NULL) {
1173 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
1174 gst_structure_set_value (dest, g_quark_to_string (field1->name),
1177 ret = gst_value_compare (&field1->value, &field2->value);
1189 * gst_caps_intersect:
1190 * @caps1: a #GstCaps to intersect
1191 * @caps2: a #GstCaps to intersect
1193 * Creates a new #GstCaps that contains all the formats that are common
1194 * to both @caps1 and @caps2.
1196 * Returns: the new #GstCaps
1199 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
1201 guint64 i; /* index can be up to 2 * G_MAX_UINT */
1204 GstStructure *struct1;
1205 GstStructure *struct2;
1207 GstStructure *istruct;
1209 g_return_val_if_fail (GST_IS_CAPS (caps1), NULL);
1210 g_return_val_if_fail (GST_IS_CAPS (caps2), NULL);
1212 /* caps are exactly the same pointers, just copy one caps */
1214 return gst_caps_copy (caps1);
1216 /* empty caps on either side, return empty */
1217 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2))
1218 return gst_caps_new_empty ();
1220 /* one of the caps is any, just copy the other caps */
1221 if (gst_caps_is_any (caps1))
1222 return gst_caps_copy (caps2);
1223 if (gst_caps_is_any (caps2))
1224 return gst_caps_copy (caps1);
1226 dest = gst_caps_new_empty ();
1228 /* run zigzag on top line then right line, this preserves the caps order
1229 * much better than a simple loop.
1231 * This algorithm zigzags over the caps structures as demonstrated in
1232 * the folowing matrix:
1240 * First we iterate over the caps1 structures (top line) intersecting
1241 * the structures diagonally down, then we iterate over the caps2
1244 for (i = 0; i < caps1->structs->len + caps2->structs->len - 1; i++) {
1245 /* caps1 index goes from 0 to caps1->structs->len-1 */
1246 j = MIN (i, caps1->structs->len - 1);
1247 /* caps2 index stays 0 until i reaches caps1->structs->len, then it counts
1248 * up from 1 to caps2->structs->len - 1 */
1251 /* now run the diagonal line, end condition is the left or bottom
1253 while (k < caps2->structs->len) {
1254 struct1 = gst_caps_get_structure (caps1, j);
1255 struct2 = gst_caps_get_structure (caps2, k);
1257 istruct = gst_caps_structure_intersect (struct1, struct2);
1259 gst_caps_append_structure (dest, istruct);
1260 /* move down left */
1263 break; /* so we don't roll back to G_MAXUINT */
1272 const GstStructure *subtract_from;
1279 gst_caps_structure_subtract_field (GQuark field_id, const GValue * value,
1282 SubtractionEntry *e = user_data;
1283 GValue subtraction = { 0, };
1284 const GValue *other;
1285 GstStructure *structure;
1287 other = gst_structure_id_get_value (e->subtract_from, field_id);
1291 if (!gst_value_subtract (&subtraction, other, value))
1293 if (gst_value_compare (&subtraction, other) == GST_VALUE_EQUAL) {
1294 g_value_unset (&subtraction);
1297 structure = gst_structure_copy (e->subtract_from);
1298 gst_structure_id_set_value (structure, field_id, &subtraction);
1299 g_value_unset (&subtraction);
1300 e->put_into = g_slist_prepend (e->put_into, structure);
1306 gst_caps_structure_subtract (GSList ** into, const GstStructure * minuend,
1307 const GstStructure * subtrahend)
1312 e.subtract_from = minuend;
1315 ret = gst_structure_foreach ((GstStructure *) subtrahend,
1316 gst_caps_structure_subtract_field, &e);
1322 for (walk = e.put_into; walk; walk = g_slist_next (walk)) {
1323 gst_structure_free (walk->data);
1325 g_slist_free (e.put_into);
1331 * gst_caps_subtract:
1332 * @minuend: #GstCaps to substract from
1333 * @subtrahend: #GstCaps to substract
1335 * Subtracts the @subtrahend from the @minuend.
1336 * <note>This function does not work reliably if optional properties for caps
1337 * are included on one caps and omitted on the other.</note>
1339 * Returns: the resulting caps
1342 gst_caps_subtract (const GstCaps * minuend, const GstCaps * subtrahend)
1347 GstCaps *dest = NULL, *src;
1349 g_return_val_if_fail (minuend != NULL, NULL);
1350 g_return_val_if_fail (subtrahend != NULL, NULL);
1352 if (gst_caps_is_empty (minuend) || gst_caps_is_any (subtrahend)) {
1353 return gst_caps_new_empty ();
1355 if (gst_caps_is_empty (subtrahend))
1356 return gst_caps_copy (minuend);
1358 /* FIXME: Do we want this here or above?
1359 The reason we need this is that there is no definition about what
1360 ANY means for specific types, so it's not possible to reduce ANY partially
1361 You can only remove everything or nothing and that is done above.
1362 Note: there's a test that checks this behaviour. */
1363 g_return_val_if_fail (!gst_caps_is_any (minuend), NULL);
1364 g_assert (subtrahend->structs->len > 0);
1366 src = gst_caps_copy (minuend);
1367 for (i = 0; i < subtrahend->structs->len; i++) {
1368 sub = gst_caps_get_structure (subtrahend, i);
1370 gst_caps_unref (src);
1373 dest = gst_caps_new_empty ();
1374 for (j = 0; j < src->structs->len; j++) {
1375 min = gst_caps_get_structure (src, j);
1376 if (gst_structure_get_name_id (min) == gst_structure_get_name_id (sub)) {
1379 if (gst_caps_structure_subtract (&list, min, sub)) {
1382 for (walk = list; walk; walk = g_slist_next (walk)) {
1383 gst_caps_append_structure (dest, (GstStructure *) walk->data);
1385 g_slist_free (list);
1387 gst_caps_append_structure (dest, gst_structure_copy (min));
1390 gst_caps_append_structure (dest, gst_structure_copy (min));
1393 if (gst_caps_is_empty (dest)) {
1394 gst_caps_unref (src);
1399 gst_caps_unref (src);
1400 gst_caps_do_simplify (dest);
1406 * @caps1: a #GstCaps to union
1407 * @caps2: a #GstCaps to union
1409 * Creates a new #GstCaps that contains all the formats that are in
1410 * either @caps1 and @caps2.
1412 * Returns: the new #GstCaps
1415 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
1420 /* NULL pointers are no correct GstCaps */
1421 g_return_val_if_fail (caps1 != NULL, NULL);
1422 g_return_val_if_fail (caps2 != NULL, NULL);
1424 if (gst_caps_is_empty (caps1))
1425 return gst_caps_copy (caps2);
1427 if (gst_caps_is_empty (caps2))
1428 return gst_caps_copy (caps1);
1430 if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2))
1431 return gst_caps_new_any ();
1433 dest1 = gst_caps_copy (caps1);
1434 dest2 = gst_caps_copy (caps2);
1435 gst_caps_append (dest1, dest2);
1437 gst_caps_do_simplify (dest1);
1441 typedef struct _NormalizeForeach
1444 GstStructure *structure;
1449 gst_caps_normalize_foreach (GQuark field_id, const GValue * value, gpointer ptr)
1451 NormalizeForeach *nf = (NormalizeForeach *) ptr;
1455 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1456 for (i = 1; i < gst_value_list_get_size (value); i++) {
1457 const GValue *v = gst_value_list_get_value (value, i);
1458 GstStructure *structure = gst_structure_copy (nf->structure);
1460 gst_structure_id_set_value (structure, field_id, v);
1461 gst_caps_append_structure (nf->caps, structure);
1464 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
1465 gst_structure_id_set_value (nf->structure, field_id, &val);
1466 g_value_unset (&val);
1474 * gst_caps_normalize:
1475 * @caps: a #GstCaps to normalize
1477 * Creates a new #GstCaps that represents the same set of formats as
1478 * @caps, but contains no lists. Each list is expanded into separate
1481 * Returns: the new #GstCaps
1484 gst_caps_normalize (const GstCaps * caps)
1486 NormalizeForeach nf;
1490 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
1492 newcaps = gst_caps_copy (caps);
1495 for (i = 0; i < newcaps->structs->len; i++) {
1496 nf.structure = gst_caps_get_structure (newcaps, i);
1498 while (!gst_structure_foreach (nf.structure,
1499 gst_caps_normalize_foreach, &nf));
1506 gst_caps_compare_structures (gconstpointer one, gconstpointer two)
1509 const GstStructure *struct1 = *((const GstStructure **) one);
1510 const GstStructure *struct2 = *((const GstStructure **) two);
1512 /* FIXME: this orders alphabetically, but ordering the quarks might be faster
1513 So what's the best way? */
1514 ret = strcmp (gst_structure_get_name (struct1),
1515 gst_structure_get_name (struct2));
1519 return gst_structure_n_fields (struct2) - gst_structure_n_fields (struct1);
1526 GstStructure *compare;
1531 gst_caps_structure_figure_out_union (GQuark field_id, const GValue * value,
1534 UnionField *u = user_data;
1535 const GValue *val = gst_structure_id_get_value (u->compare, field_id);
1539 g_value_unset (&u->value);
1542 if (gst_value_compare (val, value) == GST_VALUE_EQUAL)
1545 g_value_unset (&u->value);
1549 gst_value_union (&u->value, val, value);
1554 gst_caps_structure_simplify (GstStructure ** result,
1555 const GstStructure * simplify, GstStructure * compare)
1558 UnionField field = { 0, {0,}, NULL };
1560 /* try to subtract to get a real subset */
1561 if (gst_caps_structure_subtract (&list, simplify, compare)) {
1562 switch (g_slist_length (list)) {
1567 *result = list->data;
1568 g_slist_free (list);
1574 for (walk = list; walk; walk = g_slist_next (walk)) {
1575 gst_structure_free (walk->data);
1577 g_slist_free (list);
1583 /* try to union both structs */
1584 field.compare = compare;
1585 if (gst_structure_foreach ((GstStructure *) simplify,
1586 gst_caps_structure_figure_out_union, &field)) {
1587 gboolean ret = FALSE;
1589 /* now we know all of simplify's fields are the same in compare
1590 * but at most one field: field.name */
1591 if (G_IS_VALUE (&field.value)) {
1592 if (gst_structure_n_fields (simplify) == gst_structure_n_fields (compare)) {
1593 gst_structure_id_set_value (compare, field.name, &field.value);
1597 g_value_unset (&field.value);
1598 } else if (gst_structure_n_fields (simplify) <=
1599 gst_structure_n_fields (compare)) {
1600 /* compare is just more specific, will be optimized away later */
1601 /* FIXME: do this here? */
1602 GST_LOG ("found a case that will be optimized later.");
1604 gchar *one = gst_structure_to_string (simplify);
1605 gchar *two = gst_structure_to_string (compare);
1608 ("caps mismatch: structures %s and %s claim to be possible to unify, but aren't",
1620 gst_caps_switch_structures (GstCaps * caps, GstStructure * old,
1621 GstStructure * new, gint i)
1623 gst_structure_set_parent_refcount (old, NULL);
1624 gst_structure_free (old);
1625 gst_structure_set_parent_refcount (new, &caps->refcount);
1626 g_ptr_array_index (caps->structs, i) = new;
1630 * gst_caps_do_simplify:
1631 * @caps: a #GstCaps to simplify
1633 * Modifies the given @caps inplace into a representation that represents the
1634 * same set of formats, but in a simpler form. Component structures that are
1635 * identical are merged. Component structures that have values that can be
1636 * merged are also merged.
1638 * Returns: TRUE, if the caps could be simplified
1641 gst_caps_do_simplify (GstCaps * caps)
1643 GstStructure *simplify, *compare, *result = NULL;
1645 gboolean changed = FALSE;
1647 g_return_val_if_fail (caps != NULL, FALSE);
1648 g_return_val_if_fail (IS_WRITABLE (caps), FALSE);
1650 if (gst_caps_get_size (caps) < 2)
1653 g_ptr_array_sort (caps->structs, gst_caps_compare_structures);
1655 start = caps->structs->len - 1;
1656 for (i = caps->structs->len - 1; i >= 0; i--) {
1657 simplify = gst_caps_get_structure (caps, i);
1658 if (gst_structure_get_name_id (simplify) !=
1659 gst_structure_get_name_id (gst_caps_get_structure (caps, start)))
1661 for (j = start; j >= 0; j--) {
1664 compare = gst_caps_get_structure (caps, j);
1665 if (gst_structure_get_name_id (simplify) !=
1666 gst_structure_get_name_id (compare)) {
1669 if (gst_caps_structure_simplify (&result, simplify, compare)) {
1671 gst_caps_switch_structures (caps, simplify, result, i);
1674 gst_caps_remove_structure (caps, i);
1686 /* gst_caps_do_simplify (caps); */
1690 #ifndef GST_DISABLE_LOADSAVE
1692 * gst_caps_save_thyself:
1693 * @caps: a #GstCaps structure
1694 * @parent: a XML parent node
1696 * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1698 * Returns: a XML node pointer
1701 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1703 char *s = gst_caps_to_string (caps);
1705 xmlNewChild (parent, NULL, (xmlChar *) "caps", (xmlChar *) s);
1711 * gst_caps_load_thyself:
1712 * @parent: a XML node
1714 * Creates a #GstCaps from its XML serialization.
1716 * Returns: a new #GstCaps structure
1719 gst_caps_load_thyself (xmlNodePtr parent)
1721 if (strcmp ("caps", (char *) parent->name) == 0) {
1722 return gst_caps_from_string ((gchar *) xmlNodeGetContent (parent));
1733 * @caps: a pointer to #GstCaps
1734 * @newcaps: a #GstCaps to replace *caps
1736 * Replaces *caps with @newcaps. Unrefs the #GstCaps in the location
1737 * pointed to by @caps, if applicable, then modifies @caps to point to
1738 * @newcaps. An additional ref on @newcaps is taken.
1740 * This function does not take any locks so you might want to lock
1741 * the object owning @caps pointer.
1744 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1748 g_return_if_fail (caps != NULL);
1752 if (newcaps != oldcaps) {
1754 gst_caps_ref (newcaps);
1759 gst_caps_unref (oldcaps);
1764 * gst_caps_to_string:
1767 * Converts @caps to a string representation. This string representation
1768 * can be converted back to a #GstCaps by gst_caps_from_string().
1770 * For debugging purposes its easier to do something like this:
1772 * GST_LOG ("caps are %" GST_PTR_FORMAT, caps);
1774 * This prints the caps in human readble form.
1776 * Returns: a newly allocated string representing @caps.
1779 gst_caps_to_string (const GstCaps * caps)
1784 /* NOTE: This function is potentially called by the debug system,
1785 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1786 * should be careful to avoid recursion. This includes any functions
1787 * called by gst_caps_to_string. In particular, calls should
1788 * not use the GST_PTR_FORMAT extension. */
1791 return g_strdup ("NULL");
1793 if (gst_caps_is_any (caps)) {
1794 return g_strdup ("ANY");
1796 if (gst_caps_is_empty (caps)) {
1797 return g_strdup ("EMPTY");
1800 /* estimate a rough string length to avoid unnecessary reallocs in GString */
1802 for (i = 0; i < caps->structs->len; i++) {
1803 slen += STRUCTURE_ESTIMATED_STRING_LEN (gst_caps_get_structure (caps, i));
1806 s = g_string_sized_new (slen);
1807 for (i = 0; i < caps->structs->len; i++) {
1808 GstStructure *structure;
1811 /* ';' is now added by gst_structure_to_string */
1812 g_string_append_c (s, ' ');
1815 structure = gst_caps_get_structure (caps, i);
1816 priv_gst_structure_append_to_gstring (structure, s);
1818 if (s->len && s->str[s->len - 1] == ';') {
1819 /* remove latest ';' */
1820 s->str[--s->len] = '\0';
1822 return g_string_free (s, FALSE);
1826 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1828 GstStructure *structure;
1831 g_return_val_if_fail (string, FALSE);
1832 if (strcmp ("ANY", string) == 0) {
1833 caps->flags = GST_CAPS_FLAGS_ANY;
1836 if (strcmp ("EMPTY", string) == 0) {
1840 structure = gst_structure_from_string (string, &s);
1841 if (structure == NULL) {
1844 gst_caps_append_structure (caps, structure);
1848 while (g_ascii_isspace (*s))
1853 structure = gst_structure_from_string (s, &s);
1854 if (structure == NULL) {
1857 gst_caps_append_structure (caps, structure);
1865 * gst_caps_from_string:
1866 * @string: a string to convert to #GstCaps
1868 * Converts @caps from a string representation.
1870 * Returns: a newly allocated #GstCaps
1873 gst_caps_from_string (const gchar * string)
1877 caps = gst_caps_new_empty ();
1878 if (gst_caps_from_string_inplace (caps, string)) {
1881 gst_caps_unref (caps);
1887 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1889 g_return_if_fail (G_IS_VALUE (src_value));
1890 g_return_if_fail (G_IS_VALUE (dest_value));
1891 g_return_if_fail (G_VALUE_HOLDS (src_value, GST_TYPE_CAPS));
1892 g_return_if_fail (G_VALUE_HOLDS (dest_value, G_TYPE_STRING)
1893 || G_VALUE_HOLDS (dest_value, G_TYPE_POINTER));
1895 dest_value->data[0].v_pointer =
1896 gst_caps_to_string (src_value->data[0].v_pointer);
1900 gst_caps_copy_conditional (GstCaps * src)
1903 return gst_caps_ref (src);