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) do{ \
28 GstCaps *_newcaps = gst_caps_copy (caps); \
29 gst_caps_free(caps); \
32 #define STRUCTURE_POISON(structure) do{ \
33 GstStructure *_newstruct = gst_structure_copy (structure); \
34 gst_structure_free(structure); \
35 structure = _newstruct; \
39 static void _gst_caps_transform_to_string (const GValue *src_value,
41 static void _gst_caps_value_init (GValue *value);
42 static void _gst_caps_value_free (GValue *value);
43 static void _gst_caps_value_copy (const GValue *src, GValue *dest);
44 static gpointer _gst_caps_value_peek_pointer (const GValue *value);
45 static gboolean _gst_caps_from_string_inplace (GstCaps *caps,
51 void _gst_caps_initialize (void)
53 static const GTypeValueTable type_value_table = {
57 _gst_caps_value_peek_pointer,
63 static const GTypeInfo caps2_info = {
76 _gst_caps_type = g_type_register_static (G_TYPE_BOXED, "GstCaps",
79 g_value_register_transform_func (_gst_caps_type, G_TYPE_STRING,
80 _gst_caps_transform_to_string);
83 GType gst_caps_get_type (void)
85 return _gst_caps_type;
88 /* creation/deletion */
93 * Creates a new #GstCaps that is empty. That is, the returned
94 * #GstCaps contains no media formats.
96 * Returns: the new #GstCaps
98 GstCaps *gst_caps_new_empty (void)
100 GstCaps *caps = g_new0(GstCaps, 1);
102 caps->type = _gst_caps_type;
103 caps->structs = g_ptr_array_new();
109 * gst_caps_new_empty:
111 * Creates a new #GstCaps that indicates that it is compatible with
114 * Returns: the new #GstCaps
116 GstCaps *gst_caps_new_any (void)
118 GstCaps *caps = g_new0(GstCaps, 1);
120 caps->type = _gst_caps_type;
121 caps->structs = g_ptr_array_new();
122 caps->flags = GST_CAPS_FLAGS_ANY;
128 * gst_caps_new_simple:
129 * @media_type: the media type of the structure
130 * @...: additional arguments
132 * Creates a new #GstCaps that contains one #GstStructure. The
133 * structure is defined by the arguments, which have the same format
134 * as @gst_structure_new().
136 * Returns: the new #GstCaps
138 GstCaps *gst_caps_new_simple (const char *media_type, const char *fieldname,
142 GstStructure *structure;
145 caps = g_new0(GstCaps, 1);
146 caps->type = _gst_caps_type;
147 caps->structs = g_ptr_array_new();
149 va_start (var_args, fieldname);
150 structure = gst_structure_new_valist (media_type, fieldname, var_args);
153 gst_caps_append_structure (caps, structure);
160 * @struct1: the first structure to add
161 * @...: additional structures to add
163 * Creates a new #GstCaps and adds all the structures listed as
164 * arguments. The list must be NULL-terminated. The structures
165 * are not copied; the returned #GstCaps owns the structures.
167 * Returns: the new #GstCaps
169 GstCaps *gst_caps_new_full (GstStructure *struct1, ...)
174 va_start (var_args, struct1);
175 caps = gst_caps_new_full_valist (struct1, var_args);
182 * gst_caps_new_full_valist:
183 * @struct1: the first structure to add
184 * @var_args: additional structures to add
186 * Creates a new #GstCaps and adds all the structures listed as
187 * arguments. The list must be NULL-terminated. The structures
188 * are not copied; the returned #GstCaps owns the structures.
190 * Returns: the new #GstCaps
192 GstCaps *gst_caps_new_full_valist (GstStructure *structure,
197 caps = g_new0(GstCaps, 1);
198 caps->type = _gst_caps_type;
199 caps->structs = g_ptr_array_new();
202 gst_caps_append_structure (caps, structure);
203 structure = va_arg (var_args, GstStructure *);
211 * @caps: the #GstCaps to copy
213 * Deeply copies a #GstCaps, including all structures and all the
214 * structures' values.
216 * Returns: the new #GstCaps
218 GstCaps *gst_caps_copy (const GstCaps *caps)
221 GstStructure *structure;
224 g_return_val_if_fail(caps != NULL, NULL);
226 newcaps = g_new0(GstCaps, 1);
227 newcaps->type = _gst_caps_type;
228 newcaps->flags = caps->flags;
229 newcaps->structs = g_ptr_array_new();
231 for(i=0;i<caps->structs->len;i++){
232 structure = gst_caps_get_structure (caps, i);
233 gst_caps_append_structure (newcaps, gst_structure_copy(structure));
241 * @caps: the #GstCaps to free
243 * Frees a #GstCaps and all its structures and the structures'
246 void gst_caps_free (GstCaps *caps)
248 GstStructure *structure;
251 g_return_if_fail(caps != NULL);
253 for(i=0;i<caps->structs->len;i++){
254 structure = gst_caps_get_structure (caps, i);
255 gst_structure_free (structure);
257 g_ptr_array_free(caps->structs, TRUE);
259 memset (caps, 0xff, sizeof(GstCaps));
265 * gst_static_caps_get:
266 * @static_caps: the #GstStaticCaps to convert
268 * Converts a #GstStaticCaps to a #GstCaps.
270 * Returns: the new #GstCaps
272 const GstCaps *gst_static_caps_get (GstStaticCaps *static_caps)
274 GstCaps *caps = (GstCaps *)static_caps;
277 if (caps->type == 0) {
278 caps->type = _gst_caps_type;
279 caps->structs = g_ptr_array_new();
280 ret = _gst_caps_from_string_inplace (caps, static_caps->string);
283 g_critical ("Could not convert static caps \"%s\"", static_caps->string);
294 * @caps1: the #GstCaps that will be appended to
295 * @caps2: the #GstCaps to append
297 * Appends the structures contained in @caps2 to @caps1. The structures
298 * in @caps2 are not copied -- they are transferred to @caps1, and then
301 void gst_caps_append (GstCaps *caps1, GstCaps *caps2)
303 GstStructure *structure;
306 g_return_if_fail (caps1 != NULL);
307 g_return_if_fail (caps2 != NULL);
312 for(i=0;i<caps2->structs->len;i++){
313 structure = gst_caps_get_structure (caps2, i);
314 gst_caps_append_structure (caps1, structure);
316 g_ptr_array_free(caps2->structs, TRUE);
318 memset (caps2, 0xff, sizeof(GstCaps));
324 * gst_caps_append_structure:
325 * @caps: the #GstCaps that will be appended to
326 * @structure: the #GstStructure to append
328 * Appends @structure to @caps1. The structure is not copied; @caps1
329 * becomes the owner of @structure.
331 void gst_caps_append_structure (GstCaps *caps, GstStructure *structure)
333 g_return_if_fail(caps != NULL);
338 STRUCTURE_POISON (structure);
341 g_ptr_array_add (caps->structs, structure);
346 * gst_caps_split_one:
351 GstCaps *gst_caps_split_one (GstCaps *caps)
354 g_critical ("unimplemented");
360 * gst_caps_split_one:
363 * Returns: the number of structures that @caps contains
365 int gst_caps_get_size (const GstCaps *caps)
367 g_return_val_if_fail (caps != NULL, 0);
369 return caps->structs->len;
373 * gst_caps_get_structure:
375 * @index: the index of the structure
377 * Finds the structure in @caps that has the index @index, and
380 * WARNING: This function takes a const GstCaps *, but returns a
381 * non-const GstStructure *. This is for programming convenience --
382 * the caller should be aware that structures inside a constant
383 * @GstCaps should not be modified.
385 * Returns: a pointer to the #GstStructure corresponding to @index
387 GstStructure *gst_caps_get_structure (const GstCaps *caps, int index)
389 g_return_val_if_fail (caps != NULL, NULL);
390 g_return_val_if_fail (index >= 0, NULL);
391 g_return_val_if_fail (index < caps->structs->len, NULL);
393 return g_ptr_array_index(caps->structs, index);
398 * @caps: the @GstCaps to copy
400 * Creates a new @GstCaps and appends a copy of the first structure
401 * contained in @caps.
403 * Returns: the new @GstCaps
405 GstCaps *gst_caps_copy_1 (const GstCaps *caps)
408 GstStructure *structure;
410 g_return_val_if_fail(caps != NULL, NULL);
412 newcaps = g_new0(GstCaps, 1);
413 newcaps->type = _gst_caps_type;
414 newcaps->flags = caps->flags;
415 newcaps->structs = g_ptr_array_new();
417 if (caps->structs->len > 0){
418 structure = gst_caps_get_structure (caps, 0);
419 gst_caps_append_structure (newcaps, gst_structure_copy(structure));
426 * gst_caps_set_simple:
427 * @caps: the @GstCaps to set
428 * @field: first field to set
429 * @...: additional parameters
431 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
432 * only has one structure. The arguments must be passed in the same
433 * manner as @gst_structure_set(), and be NULL-terminated.
435 void gst_caps_set_simple (GstCaps *caps, char *field, ...)
437 GstStructure *structure;
440 g_return_if_fail (caps != NULL);
441 g_return_if_fail (caps->structs->len == 1);
443 structure = gst_caps_get_structure (caps, 0);
445 va_start (var_args, field);
446 gst_structure_set_valist (structure, field, var_args);
451 * gst_caps_set_simple_valist:
452 * @caps: the @GstCaps to copy
453 * @field: first field to set
454 * @varargs: additional parameters
456 * Sets fields in a simple #GstCaps. A simple #GstCaps is one that
457 * only has one structure. The arguments must be passed in the same
458 * manner as @gst_structure_set(), and be NULL-terminated.
460 void gst_caps_set_simple_valist (GstCaps *caps, char *field, va_list varargs)
462 GstStructure *structure;
464 g_return_if_fail (caps != NULL);
465 g_return_if_fail (caps->structs->len != 1);
467 structure = gst_caps_get_structure (caps, 0);
469 gst_structure_set_valist (structure, field, varargs);
476 * @caps: the @GstCaps to test
478 * Returns: TRUE if @caps represents any format.
480 gboolean 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 * Returns: TRUE if @caps represents no formats.
493 gboolean gst_caps_is_empty (const GstCaps *caps)
495 g_return_val_if_fail(caps != NULL, FALSE);
497 if (caps->flags & GST_CAPS_FLAGS_ANY) return FALSE;
499 return (caps->structs == NULL) || (caps->structs->len == 0);
503 * gst_caps_is_chained:
504 * @caps: the @GstCaps to test
506 * Returns: TRUE if @caps contains more than one structure
508 gboolean gst_caps_is_chained (const GstCaps *caps)
510 g_return_val_if_fail(caps != NULL, FALSE);
512 return (caps->structs->len > 1);
516 _gst_caps_is_fixed_foreach (GQuark field_id, GValue *value, gpointer unused)
518 GType type = G_VALUE_TYPE (value);
519 if (G_TYPE_IS_FUNDAMENTAL (type)) return TRUE;
520 if (type == GST_TYPE_FOURCC) return TRUE;
526 * @caps: the @GstCaps to test
528 * Fixed @GstCaps describe exactly one format, that is, they have exactly
529 * one structure, and each field in the structure describes a fixed type.
530 * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
532 * Returns: TRUE if @caps is fixed
534 gboolean gst_caps_is_fixed (const GstCaps *caps)
536 GstStructure *structure;
538 g_return_val_if_fail(caps != NULL, FALSE);
540 if (caps->structs->len != 1) return FALSE;
542 structure = gst_caps_get_structure (caps, 0);
544 return gst_structure_foreach (structure, _gst_caps_is_fixed_foreach, NULL);
548 _gst_structure_is_equal_foreach (GQuark field_id,
549 GValue *val2, gpointer data)
551 GstStructure *struct1 = (GstStructure *) data;
552 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
554 if (val1 == NULL) return FALSE;
555 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
563 * gst_caps_is_equal_fixed:
564 * @caps1: the #GstCaps to test
565 * @caps2: the #GstCaps to test
567 * Tests if two #GstCaps are equal. This function only works on fixed
570 * Returns: TRUE if the arguments represent the same format
572 gboolean gst_caps_is_equal_fixed (const GstCaps *caps1, const GstCaps *caps2)
574 GstStructure *struct1, *struct2;
576 g_return_val_if_fail (gst_caps_is_fixed(caps1), FALSE);
577 g_return_val_if_fail (gst_caps_is_fixed(caps2), FALSE);
579 struct1 = gst_caps_get_structure (caps1, 0);
580 struct2 = gst_caps_get_structure (caps2, 0);
582 if (struct1->name != struct2->name) {
585 if (struct1->fields->len != struct2->fields->len) {
589 return gst_structure_foreach (struct1, _gst_structure_is_equal_foreach,
594 _gst_structure_field_has_compatible (GQuark field_id,
595 GValue *val2, gpointer data)
598 GstStructure *struct1 = (GstStructure *) data;
599 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
601 if (val1 == NULL) return FALSE;
602 if (gst_value_intersect (&dest, val1, val2)) {
603 g_value_unset (&dest);
611 _gst_cap_is_always_compatible (const GstStructure *struct1,
612 const GstStructure *struct2)
614 if(struct1->name != struct2->name){
618 /* the reversed order is important */
619 return gst_structure_foreach ((GstStructure *) struct2,
620 _gst_structure_field_has_compatible, (gpointer) struct1);
624 _gst_caps_cap_is_always_compatible (const GstStructure *struct1,
625 const GstCaps *caps2)
629 for(i=0;i<caps2->structs->len;i++){
630 GstStructure *struct2 = gst_caps_get_structure (caps2, i);
632 if (_gst_cap_is_always_compatible (struct1, struct2)) {
641 * gst_caps_is_always_compatible
642 * @caps1: the #GstCaps to test
643 * @caps2: the #GstCaps to test
645 * Returns: TRUE if @caps1 is a subset of @caps2.
648 gst_caps_is_always_compatible (const GstCaps *caps1, const GstCaps *caps2)
652 g_return_val_if_fail (caps1 != NULL, FALSE);
653 g_return_val_if_fail (caps2 != NULL, FALSE);
654 /* FIXME: is this right ? */
655 g_return_val_if_fail (!gst_caps_is_empty (caps1), FALSE);
656 g_return_val_if_fail (!gst_caps_is_empty (caps2), FALSE);
658 if (gst_caps_is_any (caps2))
660 if (gst_caps_is_any (caps1))
663 for(i=0;i<caps1->structs->len;i++) {
664 GstStructure *struct1 = gst_caps_get_structure (caps1, i);
666 if (_gst_caps_cap_is_always_compatible(struct1, caps2) == FALSE){
677 const GstStructure *intersect;
682 gst_caps_structure_intersect_field (GQuark id, GValue *val1, gpointer data)
684 IntersectData *idata = (IntersectData *) data;
685 GValue dest_value = { 0 };
686 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
689 gst_structure_id_set_value (idata->dest, id, val1);
690 } else if (idata->first_run) {
691 if (gst_value_intersect (&dest_value, val1, val2)) {
692 gst_structure_id_set_value (idata->dest, id, &dest_value);
693 g_value_unset (&dest_value);
702 static GstStructure *gst_caps_structure_intersect (const GstStructure *struct1,
703 const GstStructure *struct2)
707 g_return_val_if_fail(struct1 != NULL, NULL);
708 g_return_val_if_fail(struct2 != NULL, NULL);
710 if (struct1->name != struct2->name) return NULL;
712 data.dest = gst_structure_id_empty_new (struct1->name);
713 data.intersect = struct2;
714 data.first_run = TRUE;
715 if (!gst_structure_foreach ((GstStructure *) struct1,
716 gst_caps_structure_intersect_field, &data))
719 data.intersect = struct1;
720 data.first_run = FALSE;
721 if (!gst_structure_foreach ((GstStructure *) struct2,
722 gst_caps_structure_intersect_field, &data))
728 gst_structure_free (data.dest);
733 static GstStructure *gst_caps_structure_union (const GstStructure *struct1,
734 const GstStructure *struct2)
738 const GstStructureField *field1;
739 const GstStructureField *field2;
742 /* FIXME this doesn't actually work */
744 if (struct1->name != struct2->name) return NULL;
746 dest = gst_structure_id_empty_new (struct1->name);
748 for(i=0;i<struct1->fields->len;i++){
749 GValue dest_value = { 0 };
751 field1 = GST_STRUCTURE_FIELD (struct1, i);
752 field2 = gst_structure_id_get_field (struct2, field1->name);
754 if (field2 == NULL) {
757 if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
758 gst_structure_set_value (dest, g_quark_to_string(field1->name),
761 ret = gst_value_compare (&field1->value, &field2->value);
773 * gst_caps_intersect:
774 * @caps1: a #GstCaps to intersect
775 * @caps2: a #GstCaps to intersect
777 * Creates a new #GstCaps that contains all the formats that are common
778 * to both @caps1 and @caps2.
780 * Returns: the new #GstCaps
782 GstCaps *gst_caps_intersect (const GstCaps *caps1, const GstCaps *caps2)
785 GstStructure *struct1;
786 GstStructure *struct2;
792 g_return_val_if_fail (caps1 != NULL, NULL);
793 g_return_val_if_fail (caps2 != NULL, NULL);
795 if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)){
796 return gst_caps_new_empty ();
798 if (gst_caps_is_any (caps1)) return gst_caps_copy (caps2);
799 if (gst_caps_is_any (caps2)) return gst_caps_copy (caps1);
801 dest = gst_caps_new_empty();
802 for(i=0;i<caps1->structs->len;i++){
803 struct1 = gst_caps_get_structure (caps1, i);
804 for(j=0;j<caps2->structs->len;j++){
805 GstStructure *istruct;
807 struct2 = gst_caps_get_structure (caps2, j);
808 istruct = gst_caps_structure_intersect (struct1, struct2);
810 gst_caps_append_structure(dest, istruct);
815 caps = gst_caps_simplify (dest);
816 gst_caps_free (dest);
826 * @caps1: a #GstCaps to union
827 * @caps2: a #GstCaps to union
829 * Creates a new #GstCaps that contains all the formats that are in
830 * either @caps1 and @caps2.
832 * Returns: the new #GstCaps
834 GstCaps *gst_caps_union (const GstCaps *caps1, const GstCaps *caps2)
839 dest1 = gst_caps_copy (caps1);
840 dest2 = gst_caps_copy (caps2);
841 gst_caps_append (dest1, dest2);
843 /* FIXME: need a simplify function */
848 typedef struct _NormalizeForeach {
850 GstStructure *structure;
854 _gst_caps_normalize_foreach (GQuark field_id, GValue *value, gpointer ptr)
856 NormalizeForeach *nf = (NormalizeForeach *) ptr;
860 if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
861 for (i=1; i<gst_value_list_get_size (value); i++) {
862 const GValue *v = gst_value_list_get_value (value, i);
863 GstStructure *structure = gst_structure_copy (nf->structure);
865 gst_structure_id_set_value (structure, field_id, v);
866 gst_caps_append_structure (nf->caps, structure);
869 gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
870 gst_structure_id_set_value (nf->structure, field_id, &val);
871 g_value_unset (&val);
879 * gst_caps_normalize:
880 * @caps: a #GstCaps to normalize
882 * Creates a new #GstCaps that represents the same set of formats as
883 * @caps, but contains no lists. Each list is expanded into separate
886 * Returns: the new #GstCaps
888 GstCaps *gst_caps_normalize (const GstCaps *caps)
894 g_return_val_if_fail(caps != NULL, NULL);
896 newcaps = gst_caps_copy (caps);
899 for(i=0;i<newcaps->structs->len;i++){
900 nf.structure = gst_caps_get_structure (newcaps, i);
902 while (!gst_structure_foreach (nf.structure, _gst_caps_normalize_foreach,
910 simplify_foreach (GQuark field_id, GValue *value, gpointer user_data)
912 GstStructure *s2 = (GstStructure *) user_data;
915 v2 = gst_structure_id_get_value (s2, field_id);
916 if (v2 == NULL) return FALSE;
918 if (gst_value_compare (value, v2) == GST_VALUE_EQUAL) return TRUE;
923 gst_caps_structure_simplify (GstStructure *struct1, const GstStructure *struct2)
925 /* FIXME this is just a simple compare. Better would be to merge
926 * the two structures */
927 if (struct1->name != struct2->name) return FALSE;
928 if (struct1->fields->len != struct2->fields->len) return FALSE;
930 return gst_structure_foreach (struct1, simplify_foreach, (void *)struct2);
935 * @caps: a #GstCaps to simplify
937 * Creates a new #GstCaps that represents the same set of formats as
938 * @caps, but simpler. Component structures that are identical are
939 * merged. Component structures that have ranges or lists that can
940 * be merged are also merged.
942 * Returns: the new #GstCaps
944 GstCaps *gst_caps_simplify (const GstCaps *caps)
949 GstStructure *structure;
950 GstStructure *struct2;
952 if (gst_caps_get_size (caps) < 2) {
953 return gst_caps_copy (caps);
956 newcaps = gst_caps_new_empty ();
958 for(i=0;i<gst_caps_get_size (caps);i++){
959 structure = gst_caps_get_structure (caps, i);
961 for(j=0;j<gst_caps_get_size (newcaps);j++){
962 struct2 = gst_caps_get_structure (caps, i);
963 if (gst_caps_structure_simplify (struct2, structure)) {
967 if (j==gst_caps_get_size (newcaps)) {
968 gst_caps_append_structure (newcaps, gst_structure_copy(structure));
975 #ifndef GST_DISABLE_LOADSAVE
976 xmlNodePtr gst_caps_save_thyself (const GstCaps *caps, xmlNodePtr parent)
982 GstCaps *gst_caps_load_thyself (xmlNodePtr parent)
993 * @caps: a pointer to #GstCaps
994 * @newcaps: a #GstCaps to replace *caps
996 * Replaces *caps with @newcaps. Frees the #GstCaps in the location
997 * pointed to by @caps, if applicable, then modifies @caps to point to
1000 void gst_caps_replace (GstCaps **caps, GstCaps *newcaps)
1002 #if 0 /* disable this, since too many plugins rely on undefined behavior */
1003 #ifdef USE_POISONING
1004 //if (newcaps) CAPS_POISON (newcaps);
1007 if (*caps) gst_caps_free(*caps);
1012 * gst_caps_to_string:
1015 * Converts @caps to a string representation. This string representation
1016 * can be converted back to a #GstCaps by #gst_caps_from_string().
1018 * Returns: a string representing @caps
1020 gchar *gst_caps_to_string (const GstCaps *caps)
1023 GstStructure *structure;
1026 /* FIXME does this leak? */
1029 return g_strdup("NULL");
1031 if(gst_caps_is_any(caps)){
1032 return g_strdup("ANY");
1034 if(gst_caps_is_empty(caps)){
1035 return g_strdup("EMPTY");
1037 s = g_string_new("");
1038 structure = gst_caps_get_structure (caps, 0);
1039 g_string_append(s, gst_structure_to_string(structure));
1041 for(i=1;i<caps->structs->len;i++){
1042 structure = gst_caps_get_structure (caps, i);
1044 g_string_append(s, "; ");
1045 g_string_append(s, gst_structure_to_string(structure));
1048 return g_string_free(s, FALSE);
1051 static gboolean _gst_caps_from_string_inplace (GstCaps *caps,
1052 const gchar *string)
1054 GstStructure *structure;
1057 if (strcmp("ANY", string)==0) {
1058 caps->flags = GST_CAPS_FLAGS_ANY;
1061 if (strcmp("NONE", string)==0) {
1065 structure = gst_structure_from_string(string, &s);
1066 if (structure == NULL) {
1069 gst_caps_append_structure (caps, structure);
1073 while (g_ascii_isspace(*s))s++;
1074 structure = gst_structure_from_string(s, &s);
1075 if (structure == NULL) {
1078 gst_caps_append_structure (caps, structure);
1079 while (g_ascii_isspace(*s))s++;
1090 * gst_caps_from_string:
1091 * @caps: a string to convert to #GstCaps
1093 * Converts @caps from a string representation.
1095 * Returns: a new #GstCaps
1097 GstCaps *gst_caps_from_string (const gchar *string)
1101 caps = gst_caps_new_empty();
1102 if (_gst_caps_from_string_inplace (caps, string)) {
1105 gst_caps_free (caps);
1110 static void _gst_caps_transform_to_string (const GValue *src_value,
1113 g_return_if_fail (src_value != NULL);
1114 g_return_if_fail (dest_value != NULL);
1116 dest_value->data[0].v_pointer =
1117 gst_caps_to_string (src_value->data[0].v_pointer);
1120 static void _gst_caps_value_init (GValue *value)
1122 value->data[0].v_pointer = gst_caps_new_empty();
1125 static void _gst_caps_value_free (GValue *value)
1127 if (value->data[0].v_pointer) gst_caps_free (value->data[0].v_pointer);
1130 static void _gst_caps_value_copy (const GValue *src, GValue *dest)
1132 if (dest->data[0].v_pointer) {
1133 gst_caps_free (dest->data[0].v_pointer);
1135 if (src->data[0].v_pointer) {
1136 dest->data[0].v_pointer = gst_caps_copy (src->data[0].v_pointer);
1138 dest->data[0].v_pointer = NULL;
1142 static gpointer _gst_caps_value_peek_pointer (const GValue *value)
1144 return value->data[0].v_pointer;
1147 /* fixate utility functions */
1149 gboolean gst_caps_structure_fixate_field_nearest_int (GstStructure *structure,
1150 const char *field_name, int target)
1152 const GValue *value;
1154 g_return_val_if_fail(gst_structure_has_field (structure, field_name), FALSE);
1156 value = gst_structure_get_value (structure, field_name);
1158 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1161 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1163 x = gst_value_get_int_range_min (value);
1164 if (target < x) target = x;
1165 x = gst_value_get_int_range_max (value);
1166 if (target > x) target = x;
1167 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1169 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1170 const GValue *list_value;
1173 int best_index = -1;
1175 n = gst_value_list_get_size (value);
1177 list_value = gst_value_list_get_value (value, i);
1178 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1179 int x = g_value_get_int (list_value);
1180 if (best_index == -1 || (ABS(target-x) < ABS(best-x))) {
1186 if(best_index != -1) {
1187 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1196 gboolean gst_caps_structure_fixate_field_nearest_double (GstStructure
1197 *structure, const char *field_name, double target)
1199 const GValue *value;
1201 g_return_val_if_fail(gst_structure_has_field (structure, field_name), FALSE);
1203 value = gst_structure_get_value (structure, field_name);
1205 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1208 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1210 x = gst_value_get_double_range_min (value);
1211 if (target < x) target = x;
1212 x = gst_value_get_double_range_max (value);
1213 if (target > x) target = x;
1214 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1216 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1217 const GValue *list_value;
1220 int best_index = -1;
1222 n = gst_value_list_get_size (value);
1224 list_value = gst_value_list_get_value (value, i);
1225 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1226 double x = g_value_get_double (list_value);
1227 if (best_index == -1 || (ABS(target-x) < ABS(best-x))) {
1233 if(best_index != -1) {
1234 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);