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 * @...: additional arguments
112 * Creates a new #GstCaps that contains one #GstStructure. The
113 * structure is defined by the arguments, which have the same format
114 * as @gst_structure_new().
116 * Returns: the new #GstCaps
119 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
122 GstStructure *structure;
125 caps = g_new0 (GstCaps, 1);
126 caps->type = GST_TYPE_CAPS;
127 caps->structs = g_ptr_array_new ();
129 va_start (var_args, fieldname);
130 structure = gst_structure_new_valist (media_type, fieldname, var_args);
133 gst_caps_append_structure (caps, structure);
140 * @struct1: the first structure to add
141 * @...: additional structures to add
143 * Creates a new #GstCaps and adds all the structures listed as
144 * arguments. The list must be NULL-terminated. The structures
145 * are not copied; the returned #GstCaps owns the structures.
147 * Returns: the new #GstCaps
150 gst_caps_new_full (GstStructure * struct1, ...)
155 va_start (var_args, struct1);
156 caps = gst_caps_new_full_valist (struct1, var_args);
163 * gst_caps_new_full_valist:
164 * @struct1: the first structure to add
165 * @var_args: additional structures to add
167 * Creates a new #GstCaps and adds all the structures listed as
168 * arguments. The list must be NULL-terminated. The structures
169 * are not copied; the returned #GstCaps owns the structures.
171 * Returns: the new #GstCaps
174 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
178 caps = g_new0 (GstCaps, 1);
179 caps->type = GST_TYPE_CAPS;
180 caps->structs = g_ptr_array_new ();
183 gst_caps_append_structure (caps, structure);
184 structure = va_arg (var_args, GstStructure *);
192 * @caps: the #GstCaps to copy
194 * Deeply copies a #GstCaps, including all structures and all the
195 * structures' values.
197 * Returns: the new #GstCaps
200 gst_caps_copy (const GstCaps * caps)
203 GstStructure *structure;
206 g_return_val_if_fail (caps != NULL, NULL);
208 newcaps = g_new0 (GstCaps, 1);
209 newcaps->type = GST_TYPE_CAPS;
210 newcaps->flags = caps->flags;
211 newcaps->structs = g_ptr_array_new ();
213 for (i = 0; i < caps->structs->len; i++) {
214 structure = gst_caps_get_structure (caps, i);
215 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
223 * @caps: the #GstCaps to free
225 * Frees a #GstCaps and all its structures and the structures'
229 gst_caps_free (GstCaps * caps)
231 GstStructure *structure;
234 g_return_if_fail (caps != NULL);
236 for (i = 0; i < caps->structs->len; i++) {
237 structure = gst_caps_get_structure (caps, i);
238 gst_structure_free (structure);
240 g_ptr_array_free (caps->structs, TRUE);
242 memset (caps, 0xff, sizeof (GstCaps));
248 * gst_static_caps_get:
249 * @static_caps: the #GstStaticCaps to convert
251 * Converts a #GstStaticCaps to a #GstCaps.
253 * Returns: the new #GstCaps
256 gst_static_caps_get (GstStaticCaps * static_caps)
258 GstCaps *caps = (GstCaps *) static_caps;
261 if (caps->type == 0) {
262 caps->type = GST_TYPE_CAPS;
263 caps->structs = g_ptr_array_new ();
264 ret = gst_caps_from_string_inplace (caps, static_caps->string);
267 g_critical ("Could not convert static caps \"%s\"", static_caps->string);
278 * @caps1: the #GstCaps that will be appended to
279 * @caps2: the #GstCaps to append
281 * Appends the structures contained in @caps2 to @caps1. The structures
282 * in @caps2 are not copied -- they are transferred to @caps1, and then
286 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
288 GstStructure *structure;
291 g_return_if_fail (caps1 != NULL);
292 g_return_if_fail (caps2 != NULL);
297 for (i = 0; i < caps2->structs->len; i++) {
298 structure = gst_caps_get_structure (caps2, i);
299 gst_caps_append_structure (caps1, structure);
301 g_ptr_array_free (caps2->structs, TRUE);
303 memset (caps2, 0xff, sizeof (GstCaps));
309 * gst_caps_append_structure:
310 * @caps: the #GstCaps that will be appended to
311 * @structure: the #GstStructure to append
313 * Appends @structure to @caps1. The structure is not copied; @caps1
314 * becomes the owner of @structure.
317 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
319 g_return_if_fail (caps != NULL);
324 STRUCTURE_POISON (structure);
327 g_ptr_array_add (caps->structs, structure);
332 * gst_caps_split_one:
335 * This function is not implemented.
340 gst_caps_split_one (GstCaps * caps)
343 g_critical ("unimplemented");
352 * Gets the number of structures contained in @caps.
354 * Returns: the number of structures that @caps contains
357 gst_caps_get_size (const GstCaps * caps)
359 g_return_val_if_fail (caps != NULL, 0);
361 return caps->structs->len;
365 * gst_caps_get_structure:
367 * @index: the index of the structure
369 * Finds the structure in @caps that has the index @index, and
372 * WARNING: This function takes a const GstCaps *, but returns a
373 * non-const GstStructure *. This is for programming convenience --
374 * the caller should be aware that structures inside a constant
375 * @GstCaps should not be modified.
377 * Returns: a pointer to the #GstStructure corresponding to @index
380 gst_caps_get_structure (const GstCaps * caps, int index)
382 g_return_val_if_fail (caps != NULL, NULL);
383 g_return_val_if_fail (index >= 0, NULL);
384 g_return_val_if_fail (index < caps->structs->len, NULL);
386 return g_ptr_array_index (caps->structs, index);
391 * @caps: the @GstCaps to copy
393 * Creates a new @GstCaps and appends a copy of the first structure
394 * contained in @caps.
396 * Returns: the new @GstCaps
399 gst_caps_copy_1 (const GstCaps * caps)
402 GstStructure *structure;
404 g_return_val_if_fail (caps != NULL, NULL);
406 newcaps = g_new0 (GstCaps, 1);
407 newcaps->type = GST_TYPE_CAPS;
408 newcaps->flags = caps->flags;
409 newcaps->structs = g_ptr_array_new ();
411 if (caps->structs->len > 0) {
412 structure = gst_caps_get_structure (caps, 0);
413 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
420 * gst_caps_set_simple:
421 * @caps: the @GstCaps to set
422 * @field: first field to set
423 * @...: additional parameters
425 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
426 * only has one structure. The arguments must be passed in the same
427 * manner as @gst_structure_set(), and be NULL-terminated.
430 gst_caps_set_simple (GstCaps * caps, char *field, ...)
432 GstStructure *structure;
435 g_return_if_fail (caps != NULL);
436 g_return_if_fail (caps->structs->len == 1);
438 structure = gst_caps_get_structure (caps, 0);
440 va_start (var_args, field);
441 gst_structure_set_valist (structure, field, var_args);
446 * gst_caps_set_simple_valist:
447 * @caps: the @GstCaps to copy
448 * @field: first field to set
449 * @varargs: additional parameters
451 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
452 * only has one structure. The arguments must be passed in the same
453 * manner as @gst_structure_set(), and be NULL-terminated.
456 gst_caps_set_simple_valist (GstCaps * caps, char *field, va_list varargs)
458 GstStructure *structure;
460 g_return_if_fail (caps != NULL);
461 g_return_if_fail (caps->structs->len != 1);
463 structure = gst_caps_get_structure (caps, 0);
465 gst_structure_set_valist (structure, field, varargs);
472 * @caps: the @GstCaps to test
474 * Determines if @caps represents any media format.
476 * Returns: TRUE if @caps represents any format.
479 gst_caps_is_any (const GstCaps * caps)
481 g_return_val_if_fail (caps != NULL, FALSE);
483 return (caps->flags & GST_CAPS_FLAGS_ANY);
488 * @caps: the @GstCaps to test
490 * Determines if @caps represents no media formats.
492 * Returns: TRUE if @caps represents no formats.
495 gst_caps_is_empty (const GstCaps * caps)
497 g_return_val_if_fail (caps != NULL, FALSE);
499 if (caps->flags & GST_CAPS_FLAGS_ANY)
502 return (caps->structs == NULL) || (caps->structs->len == 0);
506 * gst_caps_is_chained:
507 * @caps: the @GstCaps to test
509 * Determines if @caps contains multiple #GstStructures.
511 * This function is deprecated, and should not be used in new code.
512 * Use #gst_caps_is_simple() instead.
514 * Returns: TRUE if @caps contains more than one structure
517 gst_caps_is_chained (const GstCaps * caps)
519 g_return_val_if_fail (caps != NULL, FALSE);
521 return (caps->structs->len > 1);
525 gst_caps_is_fixed_foreach (GQuark field_id, GValue * value, gpointer unused)
527 GType type = G_VALUE_TYPE (value);
529 if (G_TYPE_IS_FUNDAMENTAL (type))
531 if (type == GST_TYPE_FOURCC)
538 * @caps: the @GstCaps to test
540 * Fixed @GstCaps describe exactly one format, that is, they have exactly
541 * one structure, and each field in the structure describes a fixed type.
542 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
544 * Returns: TRUE if @caps is fixed
547 gst_caps_is_fixed (const GstCaps * caps)
549 GstStructure *structure;
551 g_return_val_if_fail (caps != NULL, FALSE);
553 if (caps->structs->len != 1)
556 structure = gst_caps_get_structure (caps, 0);
558 return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
562 gst_structure_is_equal_foreach (GQuark field_id, GValue * val2, gpointer data)
564 GstStructure *struct1 = (GstStructure *) data;
565 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
569 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
577 * gst_caps_is_equal_fixed:
578 * @caps1: the #GstCaps to test
579 * @caps2: the #GstCaps to test
581 * Tests if two #GstCaps are equal. This function only works on fixed
584 * Returns: TRUE if the arguments represent the same format
587 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
589 GstStructure *struct1, *struct2;
591 g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
592 g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
594 struct1 = gst_caps_get_structure (caps1, 0);
595 struct2 = gst_caps_get_structure (caps2, 0);
597 if (struct1->name != struct2->name) {
600 if (struct1->fields->len != struct2->fields->len) {
604 return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
609 gst_structure_field_has_compatible (GQuark field_id,
610 GValue * val2, gpointer data)
613 GstStructure *struct1 = (GstStructure *) data;
614 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
618 if (gst_value_intersect (&dest, val1, val2)) {
619 g_value_unset (&dest);
627 gst_cap_is_always_compatible (const GstStructure * struct1,
628 const GstStructure * struct2)
630 if (struct1->name != struct2->name) {
634 /* the reversed order is important */
635 return gst_structure_foreach ((GstStructure *) struct2,
636 gst_structure_field_has_compatible, (gpointer) struct1);
640 gst_caps_cap_is_always_compatible (const GstStructure * struct1,
641 const GstCaps * caps2)
645 for (i = 0; i < caps2->structs->len; i++) {
646 GstStructure *struct2 = gst_caps_get_structure (caps2, i);
648 if (gst_cap_is_always_compatible (struct1, struct2)) {
657 * gst_caps_is_always_compatible:
658 * @caps1: the #GstCaps to test
659 * @caps2: the #GstCaps to test
661 * A given #GstCaps structure is always compatible with another if
662 * every media format that is in the first is also contained in the
663 * second. That is, @caps1 is a subset of @caps2.
665 * Returns: TRUE if @caps1 is a subset of @caps2.
668 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
672 g_return_val_if_fail (caps1 != NULL, FALSE);
673 g_return_val_if_fail (caps2 != NULL, FALSE);
675 if (gst_caps_is_any (caps2))
677 if (gst_caps_is_any (caps1))
679 if (gst_caps_is_empty (caps1))
681 if (gst_caps_is_empty (caps2))
684 for (i = 0; i < caps1->structs->len; i++) {
685 GstStructure *struct1 = gst_caps_get_structure (caps1, i);
687 if (gst_caps_cap_is_always_compatible (struct1, caps2) == FALSE) {
699 const GstStructure *intersect;
705 gst_caps_structure_intersect_field (GQuark id, GValue * val1, gpointer data)
707 IntersectData *idata = (IntersectData *) data;
708 GValue dest_value = { 0 };
709 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
712 gst_structure_id_set_value (idata->dest, id, val1);
713 } else if (idata->first_run) {
714 if (gst_value_intersect (&dest_value, val1, val2)) {
715 gst_structure_id_set_value (idata->dest, id, &dest_value);
716 g_value_unset (&dest_value);
725 static GstStructure *
726 gst_caps_structure_intersect (const GstStructure * struct1,
727 const GstStructure * struct2)
731 g_return_val_if_fail (struct1 != NULL, NULL);
732 g_return_val_if_fail (struct2 != NULL, NULL);
734 if (struct1->name != struct2->name)
737 data.dest = gst_structure_id_empty_new (struct1->name);
738 data.intersect = struct2;
739 data.first_run = TRUE;
740 if (!gst_structure_foreach ((GstStructure *) struct1,
741 gst_caps_structure_intersect_field, &data))
744 data.intersect = struct1;
745 data.first_run = FALSE;
746 if (!gst_structure_foreach ((GstStructure *) struct2,
747 gst_caps_structure_intersect_field, &data))
753 gst_structure_free (data.dest);
758 static GstStructure *
759 gst_caps_structure_union (const GstStructure * struct1,
760 const GstStructure * struct2)
764 const GstStructureField *field1;
765 const GstStructureField *field2;
768 /* FIXME this doesn't actually work */
770 if (struct1->name != struct2->name)
773 dest = gst_structure_id_empty_new (struct1->name);
775 for (i = 0; i < struct1->fields->len; i++) {
776 GValue dest_value = { 0 };
778 field1 = GST_STRUCTURE_FIELD (struct1, i);
779 field2 = gst_structure_id_get_field (struct2, field1->name);
781 if (field2 == NULL) {
784 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
785 gst_structure_set_value (dest, g_quark_to_string (field1->name),
788 ret = gst_value_compare (&field1->value, &field2->value);
800 * gst_caps_intersect:
801 * @caps1: a #GstCaps to intersect
802 * @caps2: a #GstCaps to intersect
804 * Creates a new #GstCaps that contains all the formats that are common
805 * to both @caps1 and @caps2.
807 * Returns: the new #GstCaps
810 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
813 GstStructure *struct1;
814 GstStructure *struct2;
821 g_return_val_if_fail (caps1 != NULL, NULL);
822 g_return_val_if_fail (caps2 != NULL, NULL);
824 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)) {
825 return gst_caps_new_empty ();
827 if (gst_caps_is_any (caps1))
828 return gst_caps_copy (caps2);
829 if (gst_caps_is_any (caps2))
830 return gst_caps_copy (caps1);
832 dest = gst_caps_new_empty ();
833 for (i = 0; i < caps1->structs->len; i++) {
834 struct1 = gst_caps_get_structure (caps1, i);
835 for (j = 0; j < caps2->structs->len; j++) {
836 GstStructure *istruct;
838 struct2 = gst_caps_get_structure (caps2, j);
839 istruct = gst_caps_structure_intersect (struct1, struct2);
841 gst_caps_append_structure (dest, istruct);
846 caps = gst_caps_simplify (dest);
847 gst_caps_free (dest);
857 * @caps1: a #GstCaps to union
858 * @caps2: a #GstCaps to union
860 * Creates a new #GstCaps that contains all the formats that are in
861 * either @caps1 and @caps2.
863 * Returns: the new #GstCaps
866 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
871 dest1 = gst_caps_copy (caps1);
872 dest2 = gst_caps_copy (caps2);
873 gst_caps_append (dest1, dest2);
875 /* FIXME: need a simplify function */
880 typedef struct _NormalizeForeach
883 GstStructure *structure;
888 gst_caps_normalize_foreach (GQuark field_id, GValue * value, gpointer ptr)
890 NormalizeForeach *nf = (NormalizeForeach *) ptr;
894 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
895 for (i = 1; i < gst_value_list_get_size (value); i++) {
896 const GValue *v = gst_value_list_get_value (value, i);
897 GstStructure *structure = gst_structure_copy (nf->structure);
899 gst_structure_id_set_value (structure, field_id, v);
900 gst_caps_append_structure (nf->caps, structure);
903 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
904 gst_structure_id_set_value (nf->structure, field_id, &val);
905 g_value_unset (&val);
913 * gst_caps_normalize:
914 * @caps: a #GstCaps to normalize
916 * Creates a new #GstCaps that represents the same set of formats as
917 * @caps, but contains no lists. Each list is expanded into separate
920 * Returns: the new #GstCaps
923 gst_caps_normalize (const GstCaps * caps)
929 g_return_val_if_fail (caps != NULL, NULL);
931 newcaps = gst_caps_copy (caps);
934 for (i = 0; i < newcaps->structs->len; i++) {
935 nf.structure = gst_caps_get_structure (newcaps, i);
937 while (!gst_structure_foreach (nf.structure,
938 gst_caps_normalize_foreach, &nf));
945 simplify_foreach (GQuark field_id, GValue * value, gpointer user_data)
947 GstStructure *s2 = (GstStructure *) user_data;
950 v2 = gst_structure_id_get_value (s2, field_id);
954 if (gst_value_compare (value, v2) == GST_VALUE_EQUAL)
960 gst_caps_structure_simplify (GstStructure * struct1,
961 const GstStructure * struct2)
963 /* FIXME this is just a simple compare. Better would be to merge
964 * the two structures */
965 if (struct1->name != struct2->name)
967 if (struct1->fields->len != struct2->fields->len)
970 return gst_structure_foreach (struct1, simplify_foreach, (void *) struct2);
975 * @caps: a #GstCaps to simplify
977 * Creates a new #GstCaps that represents the same set of formats as
978 * @caps, but simpler. Component structures that are identical are
979 * merged. Component structures that have ranges or lists that can
980 * be merged are also merged.
982 * Returns: the new #GstCaps
985 gst_caps_simplify (const GstCaps * caps)
990 GstStructure *structure;
991 GstStructure *struct2;
993 if (gst_caps_get_size (caps) < 2) {
994 return gst_caps_copy (caps);
997 newcaps = gst_caps_new_empty ();
999 for (i = 0; i < gst_caps_get_size (caps); i++) {
1000 structure = gst_caps_get_structure (caps, i);
1002 for (j = 0; j < gst_caps_get_size (newcaps); j++) {
1003 struct2 = gst_caps_get_structure (caps, i);
1004 if (gst_caps_structure_simplify (struct2, structure)) {
1008 if (j == gst_caps_get_size (newcaps)) {
1009 gst_caps_append_structure (newcaps, gst_structure_copy (structure));
1016 #ifndef GST_DISABLE_LOADSAVE
1018 * gst_caps_save_thyself:
1019 * @caps: a #GstCaps structure
1020 * @parent: a XML parent node
1022 * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1024 * Returns: a XML node pointer
1027 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1034 * gst_caps_load_thyself:
1035 * @parent: a XML node
1037 * Creates a #GstCaps from its XML serialization.
1039 * Returns: a new #GstCaps structure
1042 gst_caps_load_thyself (xmlNodePtr parent)
1053 * @caps: a pointer to #GstCaps
1054 * @newcaps: a #GstCaps to replace *caps
1056 * Replaces *caps with @newcaps. Frees the #GstCaps in the location
1057 * pointed to by @caps, if applicable, then modifies @caps to point to
1061 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1063 #if 0 /* disable this, since too many plugins rely on undefined behavior */
1064 #ifdef USE_POISONING
1065 //if (newcaps) CAPS_POISON (newcaps);
1069 gst_caps_free (*caps);
1074 * gst_caps_to_string:
1077 * Converts @caps to a string representation. This string representation
1078 * can be converted back to a #GstCaps by #gst_caps_from_string().
1080 * Returns: a string representing @caps
1083 gst_caps_to_string (const GstCaps * caps)
1086 GstStructure *structure;
1090 /* NOTE: This function is potentially called by the debug system,
1091 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1092 * should be careful to avoid recursion. This includes any functions
1093 * called by gst_caps_to_string. In particular, calls should
1094 * not use the GST_PTR_FORMAT extension. */
1096 /* FIXME does this leak? */
1099 return g_strdup ("NULL");
1101 if (gst_caps_is_any (caps)) {
1102 return g_strdup ("ANY");
1104 if (gst_caps_is_empty (caps)) {
1105 return g_strdup ("EMPTY");
1107 s = g_string_new ("");
1108 structure = gst_caps_get_structure (caps, 0);
1109 sstr = gst_structure_to_string (structure);
1110 g_string_append (s, sstr);
1113 for (i = 1; i < caps->structs->len; i++) {
1114 structure = gst_caps_get_structure (caps, i);
1116 g_string_append (s, "; ");
1117 sstr = gst_structure_to_string (structure);
1118 g_string_append (s, sstr);
1122 return g_string_free (s, FALSE);
1126 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1128 GstStructure *structure;
1131 if (strcmp ("ANY", string) == 0) {
1132 caps->flags = GST_CAPS_FLAGS_ANY;
1135 if (strcmp ("NONE", string) == 0) {
1139 structure = gst_structure_from_string (string, &s);
1140 if (structure == NULL) {
1143 gst_caps_append_structure (caps, structure);
1147 while (g_ascii_isspace (*s))
1149 structure = gst_structure_from_string (s, &s);
1150 if (structure == NULL) {
1153 gst_caps_append_structure (caps, structure);
1154 while (g_ascii_isspace (*s))
1166 * gst_caps_from_string:
1167 * @caps: a string to convert to #GstCaps
1169 * Converts @caps from a string representation.
1171 * Returns: a new #GstCaps
1174 gst_caps_from_string (const gchar * string)
1178 caps = gst_caps_new_empty ();
1179 if (gst_caps_from_string_inplace (caps, string)) {
1182 gst_caps_free (caps);
1188 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1190 g_return_if_fail (src_value != NULL);
1191 g_return_if_fail (dest_value != NULL);
1193 dest_value->data[0].v_pointer =
1194 gst_caps_to_string (src_value->data[0].v_pointer);
1198 gst_caps_copy_conditional (const GstCaps * src)
1201 return gst_caps_copy (src);
1207 /* fixate utility functions */
1210 * gst_caps_structure_fixate_field_nearest_int:
1211 * @structure: a #GstStructure
1212 * @field_name: a field in @structure
1213 * @target: the target value of the fixation
1215 * Fixates a #GstStructure by changing the given field to the nearest
1216 * integer to @target that is a subset of the existing field.
1218 * Returns: TRUE if the structure could be fixated
1221 gst_caps_structure_fixate_field_nearest_int (GstStructure * structure,
1222 const char *field_name, int target)
1224 const GValue *value;
1226 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1228 value = gst_structure_get_value (structure, field_name);
1230 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1233 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1236 x = gst_value_get_int_range_min (value);
1239 x = gst_value_get_int_range_max (value);
1242 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1244 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1245 const GValue *list_value;
1248 int best_index = -1;
1250 n = gst_value_list_get_size (value);
1251 for (i = 0; i < n; i++) {
1252 list_value = gst_value_list_get_value (value, i);
1253 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1254 int x = g_value_get_int (list_value);
1256 if (best_index == -1 || (ABS (target - x) < ABS (best - x))) {
1262 if (best_index != -1) {
1263 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1273 * gst_caps_structure_fixate_field_nearest_double:
1274 * @structure: a #GstStructure
1275 * @field_name: a field in @structure
1276 * @target: the target value of the fixation
1278 * Fixates a #GstStructure by changing the given field to the nearest
1279 * double to @target that is a subset of the existing field.
1281 * Returns: TRUE if the structure could be fixated
1284 gst_caps_structure_fixate_field_nearest_double (GstStructure * structure,
1285 const char *field_name, double target)
1287 const GValue *value;
1289 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1291 value = gst_structure_get_value (structure, field_name);
1293 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1296 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1299 x = gst_value_get_double_range_min (value);
1302 x = gst_value_get_double_range_max (value);
1305 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1307 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1308 const GValue *list_value;
1311 int best_index = -1;
1313 n = gst_value_list_get_size (value);
1314 for (i = 0; i < n; i++) {
1315 list_value = gst_value_list_get_value (value, i);
1316 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1317 double x = g_value_get_double (list_value);
1319 if (best_index == -1 || (ABS (target - x) < ABS (best - x))) {
1325 if (best_index != -1) {
1326 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);