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 void _gst_caps_value_init (GValue *value);
46 static void _gst_caps_value_free (GValue *value);
47 static void _gst_caps_value_copy (const GValue *src, GValue *dest);
48 static gpointer _gst_caps_value_peek_pointer (const GValue *value);
49 static gboolean _gst_caps_from_string_inplace (GstCaps *caps,
55 void _gst_caps_initialize (void)
57 static const GTypeValueTable type_value_table = {
61 _gst_caps_value_peek_pointer,
67 static const GTypeInfo caps2_info = {
80 _gst_caps_type = g_type_register_static (G_TYPE_BOXED, "GstCaps",
83 g_value_register_transform_func (_gst_caps_type, G_TYPE_STRING,
84 _gst_caps_transform_to_string);
87 GType gst_caps_get_type (void)
89 return _gst_caps_type;
92 /* creation/deletion */
97 * Creates a new #GstCaps that is empty. That is, the returned
98 * #GstCaps contains no media formats.
100 * Returns: the new #GstCaps
102 GstCaps *gst_caps_new_empty (void)
104 GstCaps *caps = g_new0(GstCaps, 1);
106 caps->type = _gst_caps_type;
107 caps->structs = g_ptr_array_new();
113 * gst_caps_new_empty:
115 * Creates a new #GstCaps that indicates that it is compatible with
118 * Returns: the new #GstCaps
120 GstCaps *gst_caps_new_any (void)
122 GstCaps *caps = g_new0(GstCaps, 1);
124 caps->type = _gst_caps_type;
125 caps->structs = g_ptr_array_new();
126 caps->flags = GST_CAPS_FLAGS_ANY;
132 * gst_caps_new_simple:
133 * @media_type: the media type of the structure
134 * @...: additional arguments
136 * Creates a new #GstCaps that contains one #GstStructure. The
137 * structure is defined by the arguments, which have the same format
138 * as @gst_structure_new().
140 * Returns: the new #GstCaps
142 GstCaps *gst_caps_new_simple (const char *media_type, const char *fieldname,
146 GstStructure *structure;
149 caps = g_new0(GstCaps, 1);
150 caps->type = _gst_caps_type;
151 caps->structs = g_ptr_array_new();
153 va_start (var_args, fieldname);
154 structure = gst_structure_new_valist (media_type, fieldname, var_args);
157 gst_caps_append_structure (caps, structure);
164 * @struct1: the first structure to add
165 * @...: 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
173 GstCaps *gst_caps_new_full (GstStructure *struct1, ...)
178 va_start (var_args, struct1);
179 caps = gst_caps_new_full_valist (struct1, var_args);
186 * gst_caps_new_full_valist:
187 * @struct1: the first structure to add
188 * @var_args: additional structures to add
190 * Creates a new #GstCaps and adds all the structures listed as
191 * arguments. The list must be NULL-terminated. The structures
192 * are not copied; the returned #GstCaps owns the structures.
194 * Returns: the new #GstCaps
196 GstCaps *gst_caps_new_full_valist (GstStructure *structure,
201 caps = g_new0(GstCaps, 1);
202 caps->type = _gst_caps_type;
203 caps->structs = g_ptr_array_new();
206 gst_caps_append_structure (caps, structure);
207 structure = va_arg (var_args, GstStructure *);
215 * @caps: the #GstCaps to copy
217 * Deeply copies a #GstCaps, including all structures and all the
218 * structures' values.
220 * Returns: the new #GstCaps
222 GstCaps *gst_caps_copy (const GstCaps *caps)
225 GstStructure *structure;
228 g_return_val_if_fail(caps != NULL, NULL);
230 newcaps = g_new0(GstCaps, 1);
231 newcaps->type = _gst_caps_type;
232 newcaps->flags = caps->flags;
233 newcaps->structs = g_ptr_array_new();
235 for(i=0;i<caps->structs->len;i++){
236 structure = gst_caps_get_structure (caps, i);
237 gst_caps_append_structure (newcaps, gst_structure_copy(structure));
245 * @caps: the #GstCaps to free
247 * Frees a #GstCaps and all its structures and the structures'
250 void gst_caps_free (GstCaps *caps)
252 GstStructure *structure;
255 g_return_if_fail(caps != NULL);
257 for(i=0;i<caps->structs->len;i++){
258 structure = gst_caps_get_structure (caps, i);
259 gst_structure_free (structure);
261 g_ptr_array_free(caps->structs, TRUE);
263 memset (caps, 0xff, sizeof(GstCaps));
269 * gst_static_caps_get:
270 * @static_caps: the #GstStaticCaps to convert
272 * Converts a #GstStaticCaps to a #GstCaps.
274 * Returns: the new #GstCaps
276 const GstCaps *gst_static_caps_get (GstStaticCaps *static_caps)
278 GstCaps *caps = (GstCaps *)static_caps;
281 if (caps->type == 0) {
282 caps->type = _gst_caps_type;
283 caps->structs = g_ptr_array_new();
284 ret = _gst_caps_from_string_inplace (caps, static_caps->string);
287 g_critical ("Could not convert static caps \"%s\"", static_caps->string);
298 * @caps1: the #GstCaps that will be appended to
299 * @caps2: the #GstCaps to append
301 * Appends the structures contained in @caps2 to @caps1. The structures
302 * in @caps2 are not copied -- they are transferred to @caps1, and then
305 void gst_caps_append (GstCaps *caps1, GstCaps *caps2)
307 GstStructure *structure;
310 g_return_if_fail (caps1 != NULL);
311 g_return_if_fail (caps2 != NULL);
316 for(i=0;i<caps2->structs->len;i++){
317 structure = gst_caps_get_structure (caps2, i);
318 gst_caps_append_structure (caps1, structure);
320 g_ptr_array_free(caps2->structs, TRUE);
322 memset (caps2, 0xff, sizeof(GstCaps));
328 * gst_caps_append_structure:
329 * @caps: the #GstCaps that will be appended to
330 * @structure: the #GstStructure to append
332 * Appends @structure to @caps1. The structure is not copied; @caps1
333 * becomes the owner of @structure.
335 void gst_caps_append_structure (GstCaps *caps, GstStructure *structure)
337 g_return_if_fail(caps != NULL);
342 STRUCTURE_POISON (structure);
345 g_ptr_array_add (caps->structs, structure);
350 * gst_caps_split_one:
355 GstCaps *gst_caps_split_one (GstCaps *caps)
358 g_critical ("unimplemented");
364 * gst_caps_split_one:
367 * Returns: the number of structures that @caps contains
369 int gst_caps_get_size (const GstCaps *caps)
371 g_return_val_if_fail (caps != NULL, 0);
373 return caps->structs->len;
377 * gst_caps_get_structure:
379 * @index: the index of the structure
381 * Finds the structure in @caps that has the index @index, and
384 * WARNING: This function takes a const GstCaps *, but returns a
385 * non-const GstStructure *. This is for programming convenience --
386 * the caller should be aware that structures inside a constant
387 * @GstCaps should not be modified.
389 * Returns: a pointer to the #GstStructure corresponding to @index
391 GstStructure *gst_caps_get_structure (const GstCaps *caps, int index)
393 g_return_val_if_fail (caps != NULL, NULL);
394 g_return_val_if_fail (index >= 0, NULL);
395 g_return_val_if_fail (index < caps->structs->len, NULL);
397 return g_ptr_array_index(caps->structs, index);
402 * @caps: the @GstCaps to copy
404 * Creates a new @GstCaps and appends a copy of the first structure
405 * contained in @caps.
407 * Returns: the new @GstCaps
409 GstCaps *gst_caps_copy_1 (const GstCaps *caps)
412 GstStructure *structure;
414 g_return_val_if_fail(caps != NULL, NULL);
416 newcaps = g_new0(GstCaps, 1);
417 newcaps->type = _gst_caps_type;
418 newcaps->flags = caps->flags;
419 newcaps->structs = g_ptr_array_new();
421 if (caps->structs->len > 0){
422 structure = gst_caps_get_structure (caps, 0);
423 gst_caps_append_structure (newcaps, gst_structure_copy(structure));
430 * gst_caps_set_simple:
431 * @caps: the @GstCaps to set
432 * @field: first field to set
433 * @...: additional parameters
435 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
436 * only has one structure. The arguments must be passed in the same
437 * manner as @gst_structure_set(), and be NULL-terminated.
439 void gst_caps_set_simple (GstCaps *caps, char *field, ...)
441 GstStructure *structure;
444 g_return_if_fail (caps != NULL);
445 g_return_if_fail (caps->structs->len == 1);
447 structure = gst_caps_get_structure (caps, 0);
449 va_start (var_args, field);
450 gst_structure_set_valist (structure, field, var_args);
455 * gst_caps_set_simple_valist:
456 * @caps: the @GstCaps to copy
457 * @field: first field to set
458 * @varargs: additional parameters
460 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
461 * only has one structure. The arguments must be passed in the same
462 * manner as @gst_structure_set(), and be NULL-terminated.
464 void gst_caps_set_simple_valist (GstCaps *caps, char *field, va_list varargs)
466 GstStructure *structure;
468 g_return_if_fail (caps != NULL);
469 g_return_if_fail (caps->structs->len != 1);
471 structure = gst_caps_get_structure (caps, 0);
473 gst_structure_set_valist (structure, field, varargs);
480 * @caps: the @GstCaps to test
482 * Returns: TRUE if @caps represents any format.
484 gboolean gst_caps_is_any (const GstCaps *caps)
486 g_return_val_if_fail(caps != NULL, FALSE);
488 return (caps->flags & GST_CAPS_FLAGS_ANY);
493 * @caps: the @GstCaps to test
495 * Returns: TRUE if @caps represents no formats.
497 gboolean gst_caps_is_empty (const GstCaps *caps)
499 g_return_val_if_fail(caps != NULL, FALSE);
501 if (caps->flags & GST_CAPS_FLAGS_ANY) return FALSE;
503 return (caps->structs == NULL) || (caps->structs->len == 0);
507 * gst_caps_is_chained:
508 * @caps: the @GstCaps to test
510 * Returns: TRUE if @caps contains more than one structure
512 gboolean gst_caps_is_chained (const GstCaps *caps)
514 g_return_val_if_fail(caps != NULL, FALSE);
516 return (caps->structs->len > 1);
520 _gst_caps_is_fixed_foreach (GQuark field_id, GValue *value, gpointer unused)
522 GType type = G_VALUE_TYPE (value);
523 if (G_TYPE_IS_FUNDAMENTAL (type)) return TRUE;
524 if (type == GST_TYPE_FOURCC) return TRUE;
530 * @caps: the @GstCaps to test
532 * Fixed @GstCaps describe exactly one format, that is, they have exactly
533 * one structure, and each field in the structure describes a fixed type.
534 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
536 * Returns: TRUE if @caps is fixed
538 gboolean gst_caps_is_fixed (const GstCaps *caps)
540 GstStructure *structure;
542 g_return_val_if_fail(caps != NULL, FALSE);
544 if (caps->structs->len != 1) return FALSE;
546 structure = gst_caps_get_structure (caps, 0);
548 return gst_structure_foreach (structure, _gst_caps_is_fixed_foreach, NULL);
552 _gst_structure_is_equal_foreach (GQuark field_id,
553 GValue *val2, gpointer data)
555 GstStructure *struct1 = (GstStructure *) data;
556 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
558 if (val1 == NULL) return FALSE;
559 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
567 * gst_caps_is_equal_fixed:
568 * @caps1: the #GstCaps to test
569 * @caps2: the #GstCaps to test
571 * Tests if two #GstCaps are equal. This function only works on fixed
574 * Returns: TRUE if the arguments represent the same format
576 gboolean gst_caps_is_equal_fixed (const GstCaps *caps1, const GstCaps *caps2)
578 GstStructure *struct1, *struct2;
580 g_return_val_if_fail (gst_caps_is_fixed(caps1), FALSE);
581 g_return_val_if_fail (gst_caps_is_fixed(caps2), FALSE);
583 struct1 = gst_caps_get_structure (caps1, 0);
584 struct2 = gst_caps_get_structure (caps2, 0);
586 if (struct1->name != struct2->name) {
589 if (struct1->fields->len != struct2->fields->len) {
593 return gst_structure_foreach (struct1, _gst_structure_is_equal_foreach,
598 _gst_structure_field_has_compatible (GQuark field_id,
599 GValue *val2, gpointer data)
602 GstStructure *struct1 = (GstStructure *) data;
603 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
605 if (val1 == NULL) return FALSE;
606 if (gst_value_intersect (&dest, val1, val2)) {
607 g_value_unset (&dest);
615 _gst_cap_is_always_compatible (const GstStructure *struct1,
616 const GstStructure *struct2)
618 if(struct1->name != struct2->name){
622 /* the reversed order is important */
623 return gst_structure_foreach ((GstStructure *) struct2,
624 _gst_structure_field_has_compatible, (gpointer) struct1);
628 _gst_caps_cap_is_always_compatible (const GstStructure *struct1,
629 const GstCaps *caps2)
633 for(i=0;i<caps2->structs->len;i++){
634 GstStructure *struct2 = gst_caps_get_structure (caps2, i);
636 if (_gst_cap_is_always_compatible (struct1, struct2)) {
645 * gst_caps_is_always_compatible
646 * @caps1: the #GstCaps to test
647 * @caps2: the #GstCaps to test
649 * Returns: TRUE if @caps1 is a subset of @caps2.
652 gst_caps_is_always_compatible (const GstCaps *caps1, const GstCaps *caps2)
656 g_return_val_if_fail (caps1 != NULL, FALSE);
657 g_return_val_if_fail (caps2 != NULL, FALSE);
658 /* FIXME: is this right ? */
659 g_return_val_if_fail (!gst_caps_is_empty (caps1), FALSE);
660 g_return_val_if_fail (!gst_caps_is_empty (caps2), FALSE);
662 if (gst_caps_is_any (caps2))
664 if (gst_caps_is_any (caps1))
667 for(i=0;i<caps1->structs->len;i++) {
668 GstStructure *struct1 = gst_caps_get_structure (caps1, i);
670 if (_gst_caps_cap_is_always_compatible(struct1, caps2) == FALSE){
681 const GstStructure *intersect;
686 gst_caps_structure_intersect_field (GQuark id, GValue *val1, gpointer data)
688 IntersectData *idata = (IntersectData *) data;
689 GValue dest_value = { 0 };
690 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
693 gst_structure_id_set_value (idata->dest, id, val1);
694 } else if (idata->first_run) {
695 if (gst_value_intersect (&dest_value, val1, val2)) {
696 gst_structure_id_set_value (idata->dest, id, &dest_value);
697 g_value_unset (&dest_value);
706 static GstStructure *gst_caps_structure_intersect (const GstStructure *struct1,
707 const GstStructure *struct2)
711 g_return_val_if_fail(struct1 != NULL, NULL);
712 g_return_val_if_fail(struct2 != NULL, NULL);
714 if (struct1->name != struct2->name) return NULL;
716 data.dest = gst_structure_id_empty_new (struct1->name);
717 data.intersect = struct2;
718 data.first_run = TRUE;
719 if (!gst_structure_foreach ((GstStructure *) struct1,
720 gst_caps_structure_intersect_field, &data))
723 data.intersect = struct1;
724 data.first_run = FALSE;
725 if (!gst_structure_foreach ((GstStructure *) struct2,
726 gst_caps_structure_intersect_field, &data))
732 gst_structure_free (data.dest);
737 static GstStructure *gst_caps_structure_union (const GstStructure *struct1,
738 const GstStructure *struct2)
742 const GstStructureField *field1;
743 const GstStructureField *field2;
746 /* FIXME this doesn't actually work */
748 if (struct1->name != struct2->name) return NULL;
750 dest = gst_structure_id_empty_new (struct1->name);
752 for(i=0;i<struct1->fields->len;i++){
753 GValue dest_value = { 0 };
755 field1 = GST_STRUCTURE_FIELD (struct1, i);
756 field2 = gst_structure_id_get_field (struct2, field1->name);
758 if (field2 == NULL) {
761 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
762 gst_structure_set_value (dest, g_quark_to_string(field1->name),
765 ret = gst_value_compare (&field1->value, &field2->value);
777 * gst_caps_intersect:
778 * @caps1: a #GstCaps to intersect
779 * @caps2: a #GstCaps to intersect
781 * Creates a new #GstCaps that contains all the formats that are common
782 * to both @caps1 and @caps2.
784 * Returns: the new #GstCaps
786 GstCaps *gst_caps_intersect (const GstCaps *caps1, const GstCaps *caps2)
789 GstStructure *struct1;
790 GstStructure *struct2;
796 g_return_val_if_fail (caps1 != NULL, NULL);
797 g_return_val_if_fail (caps2 != NULL, NULL);
799 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)){
800 return gst_caps_new_empty ();
802 if (gst_caps_is_any (caps1)) return gst_caps_copy (caps2);
803 if (gst_caps_is_any (caps2)) return gst_caps_copy (caps1);
805 dest = gst_caps_new_empty();
806 for(i=0;i<caps1->structs->len;i++){
807 struct1 = gst_caps_get_structure (caps1, i);
808 for(j=0;j<caps2->structs->len;j++){
809 GstStructure *istruct;
811 struct2 = gst_caps_get_structure (caps2, j);
812 istruct = gst_caps_structure_intersect (struct1, struct2);
814 gst_caps_append_structure(dest, istruct);
819 caps = gst_caps_simplify (dest);
820 gst_caps_free (dest);
830 * @caps1: a #GstCaps to union
831 * @caps2: a #GstCaps to union
833 * Creates a new #GstCaps that contains all the formats that are in
834 * either @caps1 and @caps2.
836 * Returns: the new #GstCaps
838 GstCaps *gst_caps_union (const GstCaps *caps1, const GstCaps *caps2)
843 dest1 = gst_caps_copy (caps1);
844 dest2 = gst_caps_copy (caps2);
845 gst_caps_append (dest1, dest2);
847 /* FIXME: need a simplify function */
852 typedef struct _NormalizeForeach {
854 GstStructure *structure;
858 _gst_caps_normalize_foreach (GQuark field_id, GValue *value, gpointer ptr)
860 NormalizeForeach *nf = (NormalizeForeach *) ptr;
864 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
865 for (i=1; i<gst_value_list_get_size (value); i++) {
866 const GValue *v = gst_value_list_get_value (value, i);
867 GstStructure *structure = gst_structure_copy (nf->structure);
869 gst_structure_id_set_value (structure, field_id, v);
870 gst_caps_append_structure (nf->caps, structure);
873 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
874 gst_structure_id_set_value (nf->structure, field_id, &val);
875 g_value_unset (&val);
883 * gst_caps_normalize:
884 * @caps: a #GstCaps to normalize
886 * Creates a new #GstCaps that represents the same set of formats as
887 * @caps, but contains no lists. Each list is expanded into separate
890 * Returns: the new #GstCaps
892 GstCaps *gst_caps_normalize (const GstCaps *caps)
898 g_return_val_if_fail(caps != NULL, NULL);
900 newcaps = gst_caps_copy (caps);
903 for(i=0;i<newcaps->structs->len;i++){
904 nf.structure = gst_caps_get_structure (newcaps, i);
906 while (!gst_structure_foreach (nf.structure, _gst_caps_normalize_foreach,
914 simplify_foreach (GQuark field_id, GValue *value, gpointer user_data)
916 GstStructure *s2 = (GstStructure *) user_data;
919 v2 = gst_structure_id_get_value (s2, field_id);
920 if (v2 == NULL) return FALSE;
922 if (gst_value_compare (value, v2) == GST_VALUE_EQUAL) return TRUE;
927 gst_caps_structure_simplify (GstStructure *struct1, const GstStructure *struct2)
929 /* FIXME this is just a simple compare. Better would be to merge
930 * the two structures */
931 if (struct1->name != struct2->name) return FALSE;
932 if (struct1->fields->len != struct2->fields->len) return FALSE;
934 return gst_structure_foreach (struct1, simplify_foreach, (void *)struct2);
939 * @caps: a #GstCaps to simplify
941 * Creates a new #GstCaps that represents the same set of formats as
942 * @caps, but simpler. Component structures that are identical are
943 * merged. Component structures that have ranges or lists that can
944 * be merged are also merged.
946 * Returns: the new #GstCaps
948 GstCaps *gst_caps_simplify (const GstCaps *caps)
953 GstStructure *structure;
954 GstStructure *struct2;
956 if (gst_caps_get_size (caps) < 2) {
957 return gst_caps_copy (caps);
960 newcaps = gst_caps_new_empty ();
962 for(i=0;i<gst_caps_get_size (caps);i++){
963 structure = gst_caps_get_structure (caps, i);
965 for(j=0;j<gst_caps_get_size (newcaps);j++){
966 struct2 = gst_caps_get_structure (caps, i);
967 if (gst_caps_structure_simplify (struct2, structure)) {
971 if (j==gst_caps_get_size (newcaps)) {
972 gst_caps_append_structure (newcaps, gst_structure_copy(structure));
979 #ifndef GST_DISABLE_LOADSAVE
980 xmlNodePtr gst_caps_save_thyself (const GstCaps *caps, xmlNodePtr parent)
986 GstCaps *gst_caps_load_thyself (xmlNodePtr parent)
997 * @caps: a pointer to #GstCaps
998 * @newcaps: a #GstCaps to replace *caps
1000 * Replaces *caps with @newcaps. Frees the #GstCaps in the location
1001 * pointed to by @caps, if applicable, then modifies @caps to point to
1004 void gst_caps_replace (GstCaps **caps, GstCaps *newcaps)
1006 #if 0 /* disable this, since too many plugins rely on undefined behavior */
1007 #ifdef USE_POISONING
1008 //if (newcaps) CAPS_POISON (newcaps);
1011 if (*caps) gst_caps_free(*caps);
1016 * gst_caps_to_string:
1019 * Converts @caps to a string representation. This string representation
1020 * can be converted back to a #GstCaps by #gst_caps_from_string().
1022 * Returns: a string representing @caps
1024 gchar *gst_caps_to_string (const GstCaps *caps)
1027 GstStructure *structure;
1030 /* NOTE: This function is potentially called by the debug system,
1031 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1032 * should be careful to avoid recursion. This includes any functions
1033 * called by gst_caps_to_string. In particular, calls should
1034 * not use the GST_PTR_FORMAT extension. */
1036 /* FIXME does this leak? */
1039 return g_strdup("NULL");
1041 if(gst_caps_is_any(caps)){
1042 return g_strdup("ANY");
1044 if(gst_caps_is_empty(caps)){
1045 return g_strdup("EMPTY");
1047 s = g_string_new("");
1048 structure = gst_caps_get_structure (caps, 0);
1049 g_string_append(s, gst_structure_to_string(structure));
1051 for(i=1;i<caps->structs->len;i++){
1052 structure = gst_caps_get_structure (caps, i);
1054 g_string_append(s, "; ");
1055 g_string_append(s, gst_structure_to_string(structure));
1058 return g_string_free(s, FALSE);
1061 static gboolean _gst_caps_from_string_inplace (GstCaps *caps,
1062 const gchar *string)
1064 GstStructure *structure;
1067 if (strcmp("ANY", string)==0) {
1068 caps->flags = GST_CAPS_FLAGS_ANY;
1071 if (strcmp("NONE", string)==0) {
1075 structure = gst_structure_from_string(string, &s);
1076 if (structure == NULL) {
1079 gst_caps_append_structure (caps, structure);
1083 while (g_ascii_isspace(*s))s++;
1084 structure = gst_structure_from_string(s, &s);
1085 if (structure == NULL) {
1088 gst_caps_append_structure (caps, structure);
1089 while (g_ascii_isspace(*s))s++;
1100 * gst_caps_from_string:
1101 * @caps: a string to convert to #GstCaps
1103 * Converts @caps from a string representation.
1105 * Returns: a new #GstCaps
1107 GstCaps *gst_caps_from_string (const gchar *string)
1111 caps = gst_caps_new_empty();
1112 if (_gst_caps_from_string_inplace (caps, string)) {
1115 gst_caps_free (caps);
1120 static void _gst_caps_transform_to_string (const GValue *src_value,
1123 g_return_if_fail (src_value != NULL);
1124 g_return_if_fail (dest_value != NULL);
1126 dest_value->data[0].v_pointer =
1127 gst_caps_to_string (src_value->data[0].v_pointer);
1130 static void _gst_caps_value_init (GValue *value)
1132 value->data[0].v_pointer = gst_caps_new_empty();
1135 static void _gst_caps_value_free (GValue *value)
1137 if (value->data[0].v_pointer) gst_caps_free (value->data[0].v_pointer);
1140 static void _gst_caps_value_copy (const GValue *src, GValue *dest)
1142 if (dest->data[0].v_pointer) {
1143 gst_caps_free (dest->data[0].v_pointer);
1145 if (src->data[0].v_pointer) {
1146 dest->data[0].v_pointer = gst_caps_copy (src->data[0].v_pointer);
1148 dest->data[0].v_pointer = NULL;
1152 static gpointer _gst_caps_value_peek_pointer (const GValue *value)
1154 return value->data[0].v_pointer;
1157 /* fixate utility functions */
1159 gboolean gst_caps_structure_fixate_field_nearest_int (GstStructure *structure,
1160 const char *field_name, int target)
1162 const GValue *value;
1164 g_return_val_if_fail(gst_structure_has_field (structure, field_name), FALSE);
1166 value = gst_structure_get_value (structure, field_name);
1168 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1171 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1173 x = gst_value_get_int_range_min (value);
1174 if (target < x) target = x;
1175 x = gst_value_get_int_range_max (value);
1176 if (target > x) target = x;
1177 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1179 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1180 const GValue *list_value;
1183 int best_index = -1;
1185 n = gst_value_list_get_size (value);
1187 list_value = gst_value_list_get_value (value, i);
1188 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1189 int x = g_value_get_int (list_value);
1190 if (best_index == -1 || (ABS(target-x) < ABS(best-x))) {
1196 if(best_index != -1) {
1197 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1206 gboolean gst_caps_structure_fixate_field_nearest_double (GstStructure
1207 *structure, const char *field_name, double target)
1209 const GValue *value;
1211 g_return_val_if_fail(gst_structure_has_field (structure, field_name), FALSE);
1213 value = gst_structure_get_value (structure, field_name);
1215 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1218 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1220 x = gst_value_get_double_range_min (value);
1221 if (target < x) target = x;
1222 x = gst_value_get_double_range_max (value);
1223 if (target > x) target = x;
1224 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1226 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1227 const GValue *list_value;
1230 int best_index = -1;
1232 n = gst_value_list_get_size (value);
1234 list_value = gst_value_list_get_value (value, i);
1235 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1236 double x = g_value_get_double (list_value);
1237 if (best_index == -1 || (ABS(target-x) < ABS(best-x))) {
1243 if(best_index != -1) {
1244 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);