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.
27 #define CAPS_POISON(caps) G_STMT_START{ \
29 GstCaps *_newcaps = gst_caps_copy (caps); \
30 gst_caps_free(caps); \
34 #define STRUCTURE_POISON(structure) G_STMT_START{ \
36 GstStructure *_newstruct = gst_structure_copy (structure); \
37 gst_structure_free(structure); \
38 structure = _newstruct; \
43 static void gst_caps_transform_to_string (const GValue * src_value,
45 static gboolean gst_caps_from_string_inplace (GstCaps * caps,
46 const gchar * string);
47 static GstCaps *gst_caps_copy_conditional (const GstCaps * src);
50 gst_caps_get_type (void)
52 static GType gst_caps_type = 0;
55 gst_caps_type = g_boxed_type_register_static ("GstCaps",
56 (GBoxedCopyFunc) gst_caps_copy_conditional,
57 (GBoxedFreeFunc) gst_caps_free);
59 g_value_register_transform_func (gst_caps_type,
60 G_TYPE_STRING, gst_caps_transform_to_string);
66 /* creation/deletion */
71 * Creates a new #GstCaps that is empty. That is, the returned
72 * #GstCaps contains no media formats.
74 * Returns: the new #GstCaps
77 gst_caps_new_empty (void)
79 GstCaps *caps = g_new0 (GstCaps, 1);
81 caps->type = GST_TYPE_CAPS;
82 caps->structs = g_ptr_array_new ();
90 * Creates a new #GstCaps that indicates that it is compatible with
93 * Returns: the new #GstCaps
96 gst_caps_new_any (void)
98 GstCaps *caps = g_new0 (GstCaps, 1);
100 caps->type = GST_TYPE_CAPS;
101 caps->structs = g_ptr_array_new ();
102 caps->flags = GST_CAPS_FLAGS_ANY;
108 * gst_caps_new_simple:
109 * @media_type: the media type of the structure
110 * @fieldname: first field to set
111 * @...: additional arguments
113 * Creates a new #GstCaps that contains one #GstStructure. The
114 * structure is defined by the arguments, which have the same format
115 * as @gst_structure_new().
117 * Returns: the new #GstCaps
120 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
123 GstStructure *structure;
126 caps = g_new0 (GstCaps, 1);
127 caps->type = GST_TYPE_CAPS;
128 caps->structs = g_ptr_array_new ();
130 va_start (var_args, fieldname);
131 structure = gst_structure_new_valist (media_type, fieldname, var_args);
134 gst_caps_append_structure (caps, structure);
141 * @struct1: the first structure to add
142 * @...: additional structures to add
144 * Creates a new #GstCaps and adds all the structures listed as
145 * arguments. The list must be NULL-terminated. The structures
146 * are not copied; the returned #GstCaps owns the structures.
148 * Returns: the new #GstCaps
151 gst_caps_new_full (GstStructure * struct1, ...)
156 va_start (var_args, struct1);
157 caps = gst_caps_new_full_valist (struct1, var_args);
164 * gst_caps_new_full_valist:
165 * @structure: the first structure to add
166 * @var_args: additional structures to add
168 * Creates a new #GstCaps and adds all the structures listed as
169 * arguments. The list must be NULL-terminated. The structures
170 * are not copied; the returned #GstCaps owns the structures.
172 * Returns: the new #GstCaps
175 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
179 caps = g_new0 (GstCaps, 1);
180 caps->type = GST_TYPE_CAPS;
181 caps->structs = g_ptr_array_new ();
184 gst_caps_append_structure (caps, structure);
185 structure = va_arg (var_args, GstStructure *);
193 * @caps: the #GstCaps to copy
195 * Deeply copies a #GstCaps, including all structures and all the
196 * structures' values.
198 * Returns: the new #GstCaps
201 gst_caps_copy (const GstCaps * caps)
204 GstStructure *structure;
207 g_return_val_if_fail (caps != NULL, NULL);
209 newcaps = g_new0 (GstCaps, 1);
210 newcaps->type = GST_TYPE_CAPS;
211 newcaps->flags = caps->flags;
212 newcaps->structs = g_ptr_array_new ();
214 for (i = 0; i < caps->structs->len; i++) {
215 structure = gst_caps_get_structure (caps, i);
216 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
224 * @caps: the #GstCaps to free
226 * Frees a #GstCaps and all its structures and the structures'
230 gst_caps_free (GstCaps * caps)
232 GstStructure *structure;
235 g_return_if_fail (caps != NULL);
237 for (i = 0; i < caps->structs->len; i++) {
238 structure = gst_caps_get_structure (caps, i);
239 gst_structure_free (structure);
241 g_ptr_array_free (caps->structs, TRUE);
243 memset (caps, 0xff, sizeof (GstCaps));
249 * gst_static_caps_get:
250 * @static_caps: the #GstStaticCaps to convert
252 * Converts a #GstStaticCaps to a #GstCaps.
254 * Returns: the new #GstCaps
257 gst_static_caps_get (GstStaticCaps * static_caps)
259 GstCaps *caps = (GstCaps *) static_caps;
262 if (caps->type == 0) {
263 caps->type = GST_TYPE_CAPS;
264 caps->structs = g_ptr_array_new ();
265 ret = gst_caps_from_string_inplace (caps, static_caps->string);
268 g_critical ("Could not convert static caps \"%s\"", static_caps->string);
279 * @caps1: the #GstCaps that will be appended to
280 * @caps2: the #GstCaps to append
282 * Appends the structures contained in @caps2 to @caps1. The structures
283 * in @caps2 are not copied -- they are transferred to @caps1, and then
287 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
289 GstStructure *structure;
292 g_return_if_fail (caps1 != NULL);
293 g_return_if_fail (caps2 != NULL);
298 for (i = 0; i < caps2->structs->len; i++) {
299 structure = gst_caps_get_structure (caps2, i);
300 gst_caps_append_structure (caps1, structure);
302 g_ptr_array_free (caps2->structs, TRUE);
304 memset (caps2, 0xff, sizeof (GstCaps));
310 * gst_caps_append_structure:
311 * @caps: the #GstCaps that will be appended to
312 * @structure: the #GstStructure to append
314 * Appends @structure to @caps. The structure is not copied; @caps
315 * becomes the owner of @structure.
318 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
320 g_return_if_fail (caps != NULL);
325 STRUCTURE_POISON (structure);
328 g_ptr_array_add (caps->structs, structure);
333 * gst_caps_split_one:
336 * This function is not implemented.
341 gst_caps_split_one (GstCaps * caps)
344 g_critical ("unimplemented");
353 * Gets the number of structures contained in @caps.
355 * Returns: the number of structures that @caps contains
358 gst_caps_get_size (const GstCaps * caps)
360 g_return_val_if_fail (caps != NULL, 0);
362 return caps->structs->len;
366 * gst_caps_get_structure:
368 * @index: the index of the structure
370 * Finds the structure in @caps that has the index @index, and
373 * WARNING: This function takes a const GstCaps *, but returns a
374 * non-const GstStructure *. This is for programming convenience --
375 * the caller should be aware that structures inside a constant
376 * @GstCaps should not be modified.
378 * Returns: a pointer to the #GstStructure corresponding to @index
381 gst_caps_get_structure (const GstCaps * caps, int index)
383 g_return_val_if_fail (caps != NULL, NULL);
384 g_return_val_if_fail (index >= 0, NULL);
385 g_return_val_if_fail (index < caps->structs->len, NULL);
387 return g_ptr_array_index (caps->structs, index);
392 * @caps: the @GstCaps to copy
394 * Creates a new @GstCaps and appends a copy of the first structure
395 * contained in @caps.
397 * Returns: the new @GstCaps
400 gst_caps_copy_1 (const GstCaps * caps)
403 GstStructure *structure;
405 g_return_val_if_fail (caps != NULL, NULL);
407 newcaps = g_new0 (GstCaps, 1);
408 newcaps->type = GST_TYPE_CAPS;
409 newcaps->flags = caps->flags;
410 newcaps->structs = g_ptr_array_new ();
412 if (caps->structs->len > 0) {
413 structure = gst_caps_get_structure (caps, 0);
414 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
421 * gst_caps_set_simple:
422 * @caps: the @GstCaps to set
423 * @field: first field to set
424 * @...: additional parameters
426 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
427 * only has one structure. The arguments must be passed in the same
428 * manner as @gst_structure_set(), and be NULL-terminated.
431 gst_caps_set_simple (GstCaps * caps, char *field, ...)
433 GstStructure *structure;
436 g_return_if_fail (caps != NULL);
437 g_return_if_fail (caps->structs->len == 1);
439 structure = gst_caps_get_structure (caps, 0);
441 va_start (var_args, field);
442 gst_structure_set_valist (structure, field, var_args);
447 * gst_caps_set_simple_valist:
448 * @caps: the @GstCaps to copy
449 * @field: first field to set
450 * @varargs: additional parameters
452 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
453 * only has one structure. The arguments must be passed in the same
454 * manner as @gst_structure_set(), and be NULL-terminated.
457 gst_caps_set_simple_valist (GstCaps * caps, char *field, va_list varargs)
459 GstStructure *structure;
461 g_return_if_fail (caps != NULL);
462 g_return_if_fail (caps->structs->len != 1);
464 structure = gst_caps_get_structure (caps, 0);
466 gst_structure_set_valist (structure, field, varargs);
473 * @caps: the @GstCaps to test
475 * Determines if @caps represents any media format.
477 * Returns: TRUE if @caps represents any format.
480 gst_caps_is_any (const GstCaps * caps)
482 g_return_val_if_fail (caps != NULL, FALSE);
484 return (caps->flags & GST_CAPS_FLAGS_ANY);
489 * @caps: the @GstCaps to test
491 * Determines if @caps represents no media formats.
493 * Returns: TRUE if @caps represents no formats.
496 gst_caps_is_empty (const GstCaps * caps)
498 g_return_val_if_fail (caps != NULL, FALSE);
500 if (caps->flags & GST_CAPS_FLAGS_ANY)
503 return (caps->structs == NULL) || (caps->structs->len == 0);
507 * gst_caps_is_chained:
508 * @caps: the @GstCaps to test
510 * Determines if @caps contains multiple #GstStructures.
512 * This function is deprecated, and should not be used in new code.
513 * Use #gst_caps_is_simple() instead.
515 * Returns: TRUE if @caps contains more than one structure
518 gst_caps_is_chained (const GstCaps * caps)
520 g_return_val_if_fail (caps != NULL, FALSE);
522 return (caps->structs->len > 1);
526 gst_caps_is_fixed_foreach (GQuark field_id, GValue * value, gpointer unused)
528 GType type = G_VALUE_TYPE (value);
530 if (G_TYPE_IS_FUNDAMENTAL (type))
532 if (type == GST_TYPE_FOURCC)
539 * @caps: the @GstCaps to test
541 * Fixed @GstCaps describe exactly one format, that is, they have exactly
542 * one structure, and each field in the structure describes a fixed type.
543 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
545 * Returns: TRUE if @caps is fixed
548 gst_caps_is_fixed (const GstCaps * caps)
550 GstStructure *structure;
552 g_return_val_if_fail (caps != NULL, FALSE);
554 if (caps->structs->len != 1)
557 structure = gst_caps_get_structure (caps, 0);
559 return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
563 gst_structure_is_equal_foreach (GQuark field_id, GValue * val2, gpointer data)
565 GstStructure *struct1 = (GstStructure *) data;
566 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
570 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
578 * gst_caps_is_equal_fixed:
579 * @caps1: the #GstCaps to test
580 * @caps2: the #GstCaps to test
582 * Tests if two #GstCaps are equal. This function only works on fixed
585 * Returns: TRUE if the arguments represent the same format
588 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
590 GstStructure *struct1, *struct2;
592 g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
593 g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
595 struct1 = gst_caps_get_structure (caps1, 0);
596 struct2 = gst_caps_get_structure (caps2, 0);
598 if (struct1->name != struct2->name) {
601 if (struct1->fields->len != struct2->fields->len) {
605 return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
610 gst_structure_field_has_compatible (GQuark field_id,
611 GValue * val2, gpointer data)
614 GstStructure *struct1 = (GstStructure *) data;
615 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
619 if (gst_value_intersect (&dest, val1, val2)) {
620 g_value_unset (&dest);
628 gst_cap_is_always_compatible (const GstStructure * struct1,
629 const GstStructure * struct2)
631 if (struct1->name != struct2->name) {
635 /* the reversed order is important */
636 return gst_structure_foreach ((GstStructure *) struct2,
637 gst_structure_field_has_compatible, (gpointer) struct1);
641 gst_caps_cap_is_always_compatible (const GstStructure * struct1,
642 const GstCaps * caps2)
646 for (i = 0; i < caps2->structs->len; i++) {
647 GstStructure *struct2 = gst_caps_get_structure (caps2, i);
649 if (gst_cap_is_always_compatible (struct1, struct2)) {
658 * gst_caps_is_always_compatible:
659 * @caps1: the #GstCaps to test
660 * @caps2: the #GstCaps to test
662 * A given #GstCaps structure is always compatible with another if
663 * every media format that is in the first is also contained in the
664 * second. That is, @caps1 is a subset of @caps2.
666 * Returns: TRUE if @caps1 is a subset of @caps2.
669 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
673 g_return_val_if_fail (caps1 != NULL, FALSE);
674 g_return_val_if_fail (caps2 != NULL, FALSE);
676 if (gst_caps_is_any (caps2))
678 if (gst_caps_is_any (caps1))
680 if (gst_caps_is_empty (caps1))
682 if (gst_caps_is_empty (caps2))
685 for (i = 0; i < caps1->structs->len; i++) {
686 GstStructure *struct1 = gst_caps_get_structure (caps1, i);
688 if (gst_caps_cap_is_always_compatible (struct1, caps2) == FALSE) {
700 const GstStructure *intersect;
706 gst_caps_structure_intersect_field (GQuark id, GValue * val1, gpointer data)
708 IntersectData *idata = (IntersectData *) data;
709 GValue dest_value = { 0 };
710 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
713 gst_structure_id_set_value (idata->dest, id, val1);
714 } else if (idata->first_run) {
715 if (gst_value_intersect (&dest_value, val1, val2)) {
716 gst_structure_id_set_value (idata->dest, id, &dest_value);
717 g_value_unset (&dest_value);
726 static GstStructure *
727 gst_caps_structure_intersect (const GstStructure * struct1,
728 const GstStructure * struct2)
732 g_return_val_if_fail (struct1 != NULL, NULL);
733 g_return_val_if_fail (struct2 != NULL, NULL);
735 if (struct1->name != struct2->name)
738 data.dest = gst_structure_id_empty_new (struct1->name);
739 data.intersect = struct2;
740 data.first_run = TRUE;
741 if (!gst_structure_foreach ((GstStructure *) struct1,
742 gst_caps_structure_intersect_field, &data))
745 data.intersect = struct1;
746 data.first_run = FALSE;
747 if (!gst_structure_foreach ((GstStructure *) struct2,
748 gst_caps_structure_intersect_field, &data))
754 gst_structure_free (data.dest);
759 static GstStructure *
760 gst_caps_structure_union (const GstStructure * struct1,
761 const GstStructure * struct2)
765 const GstStructureField *field1;
766 const GstStructureField *field2;
769 /* FIXME this doesn't actually work */
771 if (struct1->name != struct2->name)
774 dest = gst_structure_id_empty_new (struct1->name);
776 for (i = 0; i < struct1->fields->len; i++) {
777 GValue dest_value = { 0 };
779 field1 = GST_STRUCTURE_FIELD (struct1, i);
780 field2 = gst_structure_id_get_field (struct2, field1->name);
782 if (field2 == NULL) {
785 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
786 gst_structure_set_value (dest, g_quark_to_string (field1->name),
789 ret = gst_value_compare (&field1->value, &field2->value);
801 * gst_caps_intersect:
802 * @caps1: a #GstCaps to intersect
803 * @caps2: a #GstCaps to intersect
805 * Creates a new #GstCaps that contains all the formats that are common
806 * to both @caps1 and @caps2.
808 * Returns: the new #GstCaps
811 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
814 GstStructure *struct1;
815 GstStructure *struct2;
822 g_return_val_if_fail (caps1 != NULL, NULL);
823 g_return_val_if_fail (caps2 != NULL, NULL);
825 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)) {
826 return gst_caps_new_empty ();
828 if (gst_caps_is_any (caps1))
829 return gst_caps_copy (caps2);
830 if (gst_caps_is_any (caps2))
831 return gst_caps_copy (caps1);
833 dest = gst_caps_new_empty ();
834 for (i = 0; i < caps1->structs->len; i++) {
835 struct1 = gst_caps_get_structure (caps1, i);
836 for (j = 0; j < caps2->structs->len; j++) {
837 GstStructure *istruct;
839 struct2 = gst_caps_get_structure (caps2, j);
840 istruct = gst_caps_structure_intersect (struct1, struct2);
842 gst_caps_append_structure (dest, istruct);
847 caps = gst_caps_simplify (dest);
848 gst_caps_free (dest);
858 * @caps1: a #GstCaps to union
859 * @caps2: a #GstCaps to union
861 * Creates a new #GstCaps that contains all the formats that are in
862 * either @caps1 and @caps2.
864 * Returns: the new #GstCaps
867 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
872 dest1 = gst_caps_copy (caps1);
873 dest2 = gst_caps_copy (caps2);
874 gst_caps_append (dest1, dest2);
876 /* FIXME: need a simplify function */
881 typedef struct _NormalizeForeach
884 GstStructure *structure;
889 gst_caps_normalize_foreach (GQuark field_id, GValue * value, gpointer ptr)
891 NormalizeForeach *nf = (NormalizeForeach *) ptr;
895 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
896 for (i = 1; i < gst_value_list_get_size (value); i++) {
897 const GValue *v = gst_value_list_get_value (value, i);
898 GstStructure *structure = gst_structure_copy (nf->structure);
900 gst_structure_id_set_value (structure, field_id, v);
901 gst_caps_append_structure (nf->caps, structure);
904 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
905 gst_structure_id_set_value (nf->structure, field_id, &val);
906 g_value_unset (&val);
914 * gst_caps_normalize:
915 * @caps: a #GstCaps to normalize
917 * Creates a new #GstCaps that represents the same set of formats as
918 * @caps, but contains no lists. Each list is expanded into separate
921 * Returns: the new #GstCaps
924 gst_caps_normalize (const GstCaps * caps)
930 g_return_val_if_fail (caps != NULL, NULL);
932 newcaps = gst_caps_copy (caps);
935 for (i = 0; i < newcaps->structs->len; i++) {
936 nf.structure = gst_caps_get_structure (newcaps, i);
938 while (!gst_structure_foreach (nf.structure,
939 gst_caps_normalize_foreach, &nf));
946 simplify_foreach (GQuark field_id, GValue * value, gpointer user_data)
948 GstStructure *s2 = (GstStructure *) user_data;
951 v2 = gst_structure_id_get_value (s2, field_id);
955 if (gst_value_compare (value, v2) == GST_VALUE_EQUAL)
961 gst_caps_structure_simplify (GstStructure * struct1,
962 const GstStructure * struct2)
964 /* FIXME this is just a simple compare. Better would be to merge
965 * the two structures */
966 if (struct1->name != struct2->name)
968 if (struct1->fields->len != struct2->fields->len)
971 return gst_structure_foreach (struct1, simplify_foreach, (void *) struct2);
976 * @caps: a #GstCaps to simplify
978 * Creates a new #GstCaps that represents the same set of formats as
979 * @caps, but simpler. Component structures that are identical are
980 * merged. Component structures that have ranges or lists that can
981 * be merged are also merged.
983 * Returns: the new #GstCaps
986 gst_caps_simplify (const GstCaps * caps)
991 GstStructure *structure;
992 GstStructure *struct2;
994 if (gst_caps_get_size (caps) < 2) {
995 return gst_caps_copy (caps);
998 newcaps = gst_caps_new_empty ();
1000 for (i = 0; i < gst_caps_get_size (caps); i++) {
1001 structure = gst_caps_get_structure (caps, i);
1003 for (j = 0; j < gst_caps_get_size (newcaps); j++) {
1004 struct2 = gst_caps_get_structure (caps, i);
1005 if (gst_caps_structure_simplify (struct2, structure)) {
1009 if (j == gst_caps_get_size (newcaps)) {
1010 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
1017 #ifndef GST_DISABLE_LOADSAVE
1019 * gst_caps_save_thyself:
1020 * @caps: a #GstCaps structure
1021 * @parent: a XML parent node
1023 * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1025 * Returns: a XML node pointer
1028 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1035 * gst_caps_load_thyself:
1036 * @parent: a XML node
1038 * Creates a #GstCaps from its XML serialization.
1040 * Returns: a new #GstCaps structure
1043 gst_caps_load_thyself (xmlNodePtr parent)
1054 * @caps: a pointer to #GstCaps
1055 * @newcaps: a #GstCaps to replace *caps
1057 * Replaces *caps with @newcaps. Frees the #GstCaps in the location
1058 * pointed to by @caps, if applicable, then modifies @caps to point to
1062 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1064 #if 0 /* disable this, since too many plugins rely on undefined behavior */
1065 #ifdef USE_POISONING
1066 //if (newcaps) CAPS_POISON (newcaps);
1070 gst_caps_free (*caps);
1075 * gst_caps_to_string:
1078 * Converts @caps to a string representation. This string representation
1079 * can be converted back to a #GstCaps by #gst_caps_from_string().
1081 * Returns: a string representing @caps
1084 gst_caps_to_string (const GstCaps * caps)
1087 GstStructure *structure;
1091 /* NOTE: This function is potentially called by the debug system,
1092 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1093 * should be careful to avoid recursion. This includes any functions
1094 * called by gst_caps_to_string. In particular, calls should
1095 * not use the GST_PTR_FORMAT extension. */
1097 /* FIXME does this leak? */
1100 return g_strdup ("NULL");
1102 if (gst_caps_is_any (caps)) {
1103 return g_strdup ("ANY");
1105 if (gst_caps_is_empty (caps)) {
1106 return g_strdup ("EMPTY");
1108 s = g_string_new ("");
1109 structure = gst_caps_get_structure (caps, 0);
1110 sstr = gst_structure_to_string (structure);
1111 g_string_append (s, sstr);
1114 for (i = 1; i < caps->structs->len; i++) {
1115 structure = gst_caps_get_structure (caps, i);
1117 g_string_append (s, "; ");
1118 sstr = gst_structure_to_string (structure);
1119 g_string_append (s, sstr);
1123 return g_string_free (s, FALSE);
1127 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1129 GstStructure *structure;
1132 if (strcmp ("ANY", string) == 0) {
1133 caps->flags = GST_CAPS_FLAGS_ANY;
1136 if (strcmp ("NONE", string) == 0) {
1140 structure = gst_structure_from_string (string, &s);
1141 if (structure == NULL) {
1144 gst_caps_append_structure (caps, structure);
1148 while (g_ascii_isspace (*s))
1150 structure = gst_structure_from_string (s, &s);
1151 if (structure == NULL) {
1154 gst_caps_append_structure (caps, structure);
1155 while (g_ascii_isspace (*s))
1167 * gst_caps_from_string:
1168 * @string: a string to convert to #GstCaps
1170 * Converts @caps from a string representation.
1172 * Returns: a new #GstCaps
1175 gst_caps_from_string (const gchar * string)
1179 caps = gst_caps_new_empty ();
1180 if (gst_caps_from_string_inplace (caps, string)) {
1183 gst_caps_free (caps);
1189 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1191 g_return_if_fail (src_value != NULL);
1192 g_return_if_fail (dest_value != NULL);
1194 dest_value->data[0].v_pointer =
1195 gst_caps_to_string (src_value->data[0].v_pointer);
1199 gst_caps_copy_conditional (const GstCaps * src)
1202 return gst_caps_copy (src);
1208 /* fixate utility functions */
1211 * gst_caps_structure_fixate_field_nearest_int:
1212 * @structure: a #GstStructure
1213 * @field_name: a field in @structure
1214 * @target: the target value of the fixation
1216 * Fixates a #GstStructure by changing the given field to the nearest
1217 * integer to @target that is a subset of the existing field.
1219 * Returns: TRUE if the structure could be fixated
1222 gst_caps_structure_fixate_field_nearest_int (GstStructure * structure,
1223 const char *field_name, int target)
1225 const GValue *value;
1227 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1229 value = gst_structure_get_value (structure, field_name);
1231 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1234 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1237 x = gst_value_get_int_range_min (value);
1240 x = gst_value_get_int_range_max (value);
1243 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1245 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1246 const GValue *list_value;
1249 int best_index = -1;
1251 n = gst_value_list_get_size (value);
1252 for (i = 0; i < n; i++) {
1253 list_value = gst_value_list_get_value (value, i);
1254 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1255 int x = g_value_get_int (list_value);
1257 if (best_index == -1 || (ABS (target - x) < ABS (best - x))) {
1263 if (best_index != -1) {
1264 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1274 * gst_caps_structure_fixate_field_nearest_double:
1275 * @structure: a #GstStructure
1276 * @field_name: a field in @structure
1277 * @target: the target value of the fixation
1279 * Fixates a #GstStructure by changing the given field to the nearest
1280 * double to @target that is a subset of the existing field.
1282 * Returns: TRUE if the structure could be fixated
1285 gst_caps_structure_fixate_field_nearest_double (GstStructure * structure,
1286 const char *field_name, double target)
1288 const GValue *value;
1290 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1292 value = gst_structure_get_value (structure, field_name);
1294 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1297 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1300 x = gst_value_get_double_range_min (value);
1303 x = gst_value_get_double_range_max (value);
1306 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1308 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1309 const GValue *list_value;
1312 int best_index = -1;
1314 n = gst_value_list_get_size (value);
1315 for (i = 0; i < n; i++) {
1316 list_value = gst_value_list_get_value (value, i);
1317 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1318 double x = g_value_get_double (list_value);
1320 if (best_index == -1 || (ABS (target - x) < ABS (best - x))) {
1326 if (best_index != -1) {
1327 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);