0afa857cea3b0a275d770a67f28ca3b77af26b4c
[platform/upstream/gstreamer.git] / gst / gstcaps.c
1 /* GStreamer
2  * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
24 #include <signal.h>
25
26 #include "gst_private.h"
27 #include <gst/gst.h>
28
29 /* #define DEBUG_REFCOUNT */
30
31 #define CAPS_POISON(caps) G_STMT_START{ \
32   if (caps) { \
33     GstCaps *_newcaps = gst_caps_copy (caps); \
34     gst_caps_unref(caps); \
35     caps = _newcaps; \
36   } \
37 } G_STMT_END
38 #define STRUCTURE_POISON(structure) G_STMT_START{ \
39   if (structure) { \
40     GstStructure *_newstruct = gst_structure_copy (structure); \
41     gst_structure_free(structure); \
42     structure = _newstruct; \
43   } \
44 } G_STMT_END
45 #define IS_WRITABLE(caps) \
46   (g_atomic_int_get (&(caps)->refcount) == 1)
47
48
49 static void gst_caps_transform_to_string (const GValue * src_value,
50     GValue * dest_value);
51 static gboolean gst_caps_from_string_inplace (GstCaps * caps,
52     const gchar * string);
53 static GstCaps *gst_caps_copy_conditional (GstCaps * src);
54
55 GType
56 gst_caps_get_type (void)
57 {
58   static GType gst_caps_type = 0;
59
60   if (!gst_caps_type) {
61     gst_caps_type = g_boxed_type_register_static ("GstCaps",
62         (GBoxedCopyFunc) gst_caps_copy_conditional,
63         (GBoxedFreeFunc) gst_caps_unref);
64
65     g_value_register_transform_func (gst_caps_type,
66         G_TYPE_STRING, gst_caps_transform_to_string);
67   }
68
69   return gst_caps_type;
70 }
71
72 /* creation/deletion */
73
74 /**
75  * gst_caps_new_empty:
76  *
77  * Creates a new #GstCaps that is empty.  That is, the returned
78  * #GstCaps contains no media formats.
79  *
80  * Returns: the new #GstCaps
81  */
82 GstCaps *
83 gst_caps_new_empty (void)
84 {
85   GstCaps *caps = g_new0 (GstCaps, 1);
86
87   g_atomic_int_inc (&caps->refcount);
88   caps->type = GST_TYPE_CAPS;
89   caps->structs = g_ptr_array_new ();
90
91 #ifdef DEBUG_REFCOUNT
92   GST_CAT_LOG (GST_CAT_CAPS, "created caps %p", caps);
93 #endif
94
95   return caps;
96 }
97
98 /**
99  * gst_caps_new_any:
100  *
101  * Creates a new #GstCaps that indicates that it is compatible with
102  * any media format.
103  *
104  * Returns: the new #GstCaps
105  */
106 GstCaps *
107 gst_caps_new_any (void)
108 {
109   GstCaps *caps = gst_caps_new_empty ();
110
111   caps->flags = GST_CAPS_FLAGS_ANY;
112
113   return caps;
114 }
115
116 /**
117  * gst_caps_new_simple:
118  * @media_type: the media type of the structure
119  * @fieldname: first field to set
120  * @...: additional arguments
121  *
122  * Creates a new #GstCaps that contains one #GstStructure.  The
123  * structure is defined by the arguments, which have the same format
124  * as @gst_structure_new().
125  *
126  * Returns: the new #GstCaps
127  */
128 GstCaps *
129 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
130 {
131   GstCaps *caps;
132   GstStructure *structure;
133   va_list var_args;
134
135   caps = gst_caps_new_empty ();
136
137   va_start (var_args, fieldname);
138   structure = gst_structure_new_valist (media_type, fieldname, var_args);
139   va_end (var_args);
140
141   gst_caps_append_structure (caps, structure);
142
143   return caps;
144 }
145
146 /**
147  * gst_caps_new_full:
148  * @struct1: the first structure to add
149  * @...: additional structures to add
150  *
151  * Creates a new #GstCaps and adds all the structures listed as
152  * arguments.  The list must be NULL-terminated.  The structures
153  * are not copied; the returned #GstCaps owns the structures.
154  *
155  * Returns: the new #GstCaps
156  */
157 GstCaps *
158 gst_caps_new_full (GstStructure * struct1, ...)
159 {
160   GstCaps *caps;
161   va_list var_args;
162
163   va_start (var_args, struct1);
164   caps = gst_caps_new_full_valist (struct1, var_args);
165   va_end (var_args);
166
167   return caps;
168 }
169
170 /**
171  * gst_caps_new_full_valist:
172  * @structure: the first structure to add
173  * @var_args: additional structures to add
174  *
175  * Creates a new #GstCaps and adds all the structures listed as
176  * arguments.  The list must be NULL-terminated.  The structures
177  * are not copied; the returned #GstCaps owns the structures.
178  *
179  * Returns: the new #GstCaps
180  */
181 GstCaps *
182 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
183 {
184   GstCaps *caps;
185
186   caps = gst_caps_new_empty ();
187
188   while (structure) {
189     gst_caps_append_structure (caps, structure);
190     structure = va_arg (var_args, GstStructure *);
191   }
192
193   return caps;
194 }
195
196 /**
197  * gst_caps_copy:
198  * @caps: the #GstCaps to copy
199  *
200  * Creates a new #GstCaps as a copy of the old @caps. The new caps will have a
201  * refcount of 1, owned by the caller. The structures are copied as well.
202  *
203  * Note that this function is the semantic equivalent of a gst_caps_ref()
204  * followed by a gst_caps_make_writable(). If you only want to hold on to a
205  * reference to the data, you should use gst_caps_ref().
206  *
207  * When you are finished with the caps, call gst_caps_unref() on it.
208  *
209  * Returns: the new #GstCaps
210  */
211 GstCaps *
212 gst_caps_copy (const GstCaps * caps)
213 {
214   GstCaps *newcaps;
215   GstStructure *structure;
216   int i;
217
218   g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
219
220   newcaps = gst_caps_new_empty ();
221   newcaps->flags = caps->flags;
222
223   for (i = 0; i < caps->structs->len; i++) {
224     structure = gst_caps_get_structure (caps, i);
225     gst_caps_append_structure (newcaps, gst_structure_copy (structure));
226   }
227
228   return newcaps;
229 }
230
231 static void
232 _gst_caps_free (GstCaps * caps)
233 {
234   GstStructure *structure;
235   int i;
236
237   /* The refcount must be 0, but since we're only called by gst_caps_unref,
238    * don't bother testing. */
239
240   for (i = 0; i < caps->structs->len; i++) {
241     structure = (GstStructure *) gst_caps_get_structure (caps, i);
242     gst_structure_set_parent_refcount (structure, NULL);
243     gst_structure_free (structure);
244   }
245   g_ptr_array_free (caps->structs, TRUE);
246 #ifdef USE_POISONING
247   memset (caps, 0xff, sizeof (GstCaps));
248 #endif
249   g_free (caps);
250 }
251
252 /**
253  * gst_caps_make_writable:
254  * @caps: the #GstCaps to make writable
255  *
256  * Returns a writable copy of @caps.
257  *
258  * If there is only one reference count on @caps, the caller must be the owner,
259  * and so this function will return the caps object unchanged. If on the other
260  * hand there is more than one reference on the object, a new caps object will
261  * be returned. The caller's reference on @caps will be removed, and instead the
262  * caller will own a reference to the returned object.
263  *
264  * In short, this function unrefs the caps in the argument and refs the caps
265  * that it returns. Don't access the argument after calling this function. See
266  * also: gst_caps_ref().
267  *
268  * Returns: the same #GstCaps object.
269  */
270 GstCaps *
271 gst_caps_make_writable (GstCaps * caps)
272 {
273   GstCaps *copy;
274
275   g_return_val_if_fail (caps != NULL, NULL);
276
277   /* we are the only instance reffing this caps */
278   if (g_atomic_int_get (&caps->refcount) == 1)
279     return caps;
280
281   /* else copy */
282   copy = gst_caps_copy (caps);
283   gst_caps_unref (caps);
284
285   return copy;
286 }
287
288 /**
289  * gst_caps_ref:
290  * @caps: the #GstCaps to reference
291  *
292  * Add a reference to a #GstCaps object.
293  *
294  * From this point on, until the caller calls gst_caps_unref() or
295  * gst_caps_make_writable(), it is guaranteed that the caps object will not
296  * change. This means its structures won't change, etc. To use a #GstCaps
297  * object, you must always have a refcount on it -- either the one made
298  * implicitly by gst_caps_new(), or via taking one explicitly with this
299  * function.
300  *
301  * Returns: the same #GstCaps object.
302  */
303 GstCaps *
304 gst_caps_ref (GstCaps * caps)
305 {
306   g_return_val_if_fail (caps != NULL, NULL);
307
308 #ifdef DEBUG_REFCOUNT
309   GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
310       GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) + 1);
311 #endif
312   g_return_val_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0, NULL);
313
314   g_atomic_int_inc (&caps->refcount);
315
316   return caps;
317 }
318
319 /**
320  * gst_caps_unref:
321  * @caps: the #GstCaps to unref
322  *
323  * Unref a #GstCaps and and free all its structures and the 
324  * structures' values when the refcount reaches 0.
325  */
326 void
327 gst_caps_unref (GstCaps * caps)
328 {
329   g_return_if_fail (caps != NULL);
330
331 #ifdef DEBUG_REFCOUNT
332   GST_CAT_LOG (GST_CAT_CAPS, "%p %d->%d", caps,
333       GST_CAPS_REFCOUNT_VALUE (caps), GST_CAPS_REFCOUNT_VALUE (caps) - 1);
334 #endif
335
336   g_return_if_fail (GST_CAPS_REFCOUNT_VALUE (caps) > 0);
337
338   /* if we ended up with the refcount at zero, free the caps */
339   if (g_atomic_int_dec_and_test (&caps->refcount)) {
340     _gst_caps_free (caps);
341   }
342 }
343
344 /**
345  * gst_static_caps_get:
346  * @static_caps: the #GstStaticCaps to convert
347  *
348  * Converts a #GstStaticCaps to a #GstCaps.
349  *
350  * Returns: A pointer to the #GstCaps. Although you do not have a reference on
351  * the caps, the core will never drop its references. (The core has two
352  * references on the caps so it will be immutable.)
353  */
354 GstCaps *
355 gst_static_caps_get (GstStaticCaps * static_caps)
356 {
357   GstCaps *caps = (GstCaps *) static_caps;
358   gboolean ret;
359
360   if (caps->type == 0) {
361     if (static_caps->string == NULL) {
362       g_warning ("static caps is NULL");
363       return NULL;
364     }
365
366     caps->type = GST_TYPE_CAPS;
367     /* initialize the caps to a refcount of 1 so the caps can be writable... */
368     gst_atomic_int_set (&caps->refcount, 1);
369     caps->structs = g_ptr_array_new ();
370     ret = gst_caps_from_string_inplace (caps, static_caps->string);
371
372     if (!ret) {
373       g_critical ("Could not convert static caps \"%s\"", static_caps->string);
374     }
375     /* and now that we return it to the user, keep a ref for ourselves. This
376      * makes the caps immutable... AND INVINCIBLE! WOULD YOU LIKE TO TRY MY
377      * IMMUTABLE CAPS STYLE? I AM A CAPS WARRIOR!!! */
378     gst_caps_ref (caps);
379   }
380
381   return caps;
382 }
383
384 /* manipulation */
385
386 static GstStructure *
387 gst_caps_remove_and_get_structure (GstCaps * caps, guint idx)
388 {
389   /* don't use index_fast, gst_caps_simplify relies on the order */
390   GstStructure *s = g_ptr_array_remove_index (caps->structs, idx);
391
392   gst_structure_set_parent_refcount (s, NULL);
393   return s;
394 }
395
396 /**
397  * gst_caps_append:
398  * @caps1: the #GstCaps that will be appended to
399  * @caps2: the #GstCaps to append
400  *
401  * Appends the structures contained in @caps2 to @caps1. The structures in
402  * @caps2 are not copied -- they are transferred to @caps1, and then @caps2 is
403  * freed. If either caps is ANY, the resulting caps will be ANY.
404  */
405 void
406 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
407 {
408   GstStructure *structure;
409   int i;
410
411   g_return_if_fail (GST_IS_CAPS (caps1));
412   g_return_if_fail (GST_IS_CAPS (caps2));
413   g_return_if_fail (IS_WRITABLE (caps1));
414   g_return_if_fail (IS_WRITABLE (caps2));
415
416 #ifdef USE_POISONING
417   CAPS_POISON (caps2);
418 #endif
419   if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2)) {
420     /* FIXME: this leaks */
421     caps1->flags |= GST_CAPS_FLAGS_ANY;
422     for (i = caps2->structs->len - 1; i >= 0; i--) {
423       structure = gst_caps_remove_and_get_structure (caps2, i);
424       gst_structure_free (structure);
425     }
426   } else {
427     int len = caps2->structs->len;
428
429     for (i = 0; i < len; i++) {
430       structure = gst_caps_remove_and_get_structure (caps2, 0);
431       gst_caps_append_structure (caps1, structure);
432     }
433   }
434   gst_caps_unref (caps2);       /* guaranteed to free it */
435 }
436
437 /**
438  * gst_caps_append_structure:
439  * @caps: the #GstCaps that will be appended to
440  * @structure: the #GstStructure to append
441  *
442  * Appends @structure to @caps.  The structure is not copied; @caps
443  * becomes the owner of @structure.
444  */
445 void
446 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
447 {
448   g_return_if_fail (GST_IS_CAPS (caps));
449   g_return_if_fail (IS_WRITABLE (caps));
450
451   if (structure) {
452     g_return_if_fail (structure->parent_refcount == NULL);
453 #if 0
454 #ifdef USE_POISONING
455     STRUCTURE_POISON (structure);
456 #endif
457 #endif
458     gst_structure_set_parent_refcount (structure, &caps->refcount);
459     g_ptr_array_add (caps->structs, structure);
460   }
461 }
462
463 /*
464  * gst_caps_remove_structure:
465  * @caps: the #GstCaps to remove from
466  * @idx: Index of the structure to remove
467  *
468  * removes the stucture with the given index from the list of structures 
469  * contained in @caps.
470  */
471 void
472 gst_caps_remove_structure (GstCaps * caps, guint idx)
473 {
474   GstStructure *structure;
475
476   g_return_if_fail (caps != NULL);
477   g_return_if_fail (idx <= gst_caps_get_size (caps));
478   g_return_if_fail (IS_WRITABLE (caps));
479
480   structure = gst_caps_remove_and_get_structure (caps, idx);
481   gst_structure_free (structure);
482 }
483
484 /**
485  * gst_caps_split_one:
486  * @caps: a #GstCaps
487  *
488  * This function is not implemented.
489  *
490  * Returns: NULL
491  */
492 GstCaps *
493 gst_caps_split_one (GstCaps * caps)
494 {
495   /* FIXME */
496   g_critical ("unimplemented");
497
498   return NULL;
499 }
500
501 /**
502  * gst_caps_get_size:
503  * @caps: a #GstCaps
504  *
505  * Gets the number of structures contained in @caps.
506  *
507  * Returns: the number of structures that @caps contains
508  */
509 int
510 gst_caps_get_size (const GstCaps * caps)
511 {
512   g_return_val_if_fail (GST_IS_CAPS (caps), 0);
513
514   return caps->structs->len;
515 }
516
517 /**
518  * gst_caps_get_structure:
519  * @caps: a #GstCaps
520  * @index: the index of the structure
521  *
522  * Finds the structure in @caps that has the index @index, and 
523  * returns it.
524  *
525  * WARNING: This function takes a const GstCaps *, but returns a
526  * non-const GstStructure *.  This is for programming convenience --
527  * the caller should be aware that structures inside a constant
528  * @GstCaps should not be modified.
529  *
530  * Returns: a pointer to the #GstStructure corresponding to @index
531  */
532 GstStructure *
533 gst_caps_get_structure (const GstCaps * caps, int index)
534 {
535   g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
536   g_return_val_if_fail (index >= 0, NULL);
537   g_return_val_if_fail (index < caps->structs->len, NULL);
538
539   return g_ptr_array_index (caps->structs, index);
540 }
541
542 /**      
543  * gst_caps_copy_nth:    
544  * @caps: the @GstCaps to copy   
545  * @nth: the nth structure to copy
546  *       
547  * Creates a new @GstCaps and appends a copy of the nth structure        
548  * contained in @caps.   
549  *       
550  * Returns: the new @GstCaps     
551  */
552 GstCaps *
553 gst_caps_copy_nth (const GstCaps * caps, gint nth)
554 {
555   GstCaps *newcaps;
556   GstStructure *structure;
557
558   g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
559
560   newcaps = gst_caps_new_empty ();
561   newcaps->flags = caps->flags;
562
563   if (caps->structs->len > nth) {
564     structure = gst_caps_get_structure (caps, nth);
565     gst_caps_append_structure (newcaps, gst_structure_copy (structure));
566   }
567
568   return newcaps;
569 }
570
571 /**
572  * gst_caps_set_simple:
573  * @caps: the @GstCaps to set
574  * @field: first field to set
575  * @...: additional parameters
576  *
577  * Sets fields in a simple #GstCaps.  A simple #GstCaps is one that
578  * only has one structure.  The arguments must be passed in the same
579  * manner as @gst_structure_set(), and be NULL-terminated.
580  */
581 void
582 gst_caps_set_simple (GstCaps * caps, char *field, ...)
583 {
584   GstStructure *structure;
585   va_list var_args;
586
587   g_return_if_fail (GST_IS_CAPS (caps));
588   g_return_if_fail (caps->structs->len == 1);
589   g_return_if_fail (IS_WRITABLE (caps));
590
591   structure = gst_caps_get_structure (caps, 0);
592
593   va_start (var_args, field);
594   gst_structure_set_valist (structure, field, var_args);
595   va_end (var_args);
596 }
597
598 /**
599  * gst_caps_set_simple_valist:
600  * @caps: the @GstCaps to copy
601  * @field: first field to set
602  * @varargs: additional parameters
603  *
604  * Sets fields in a simple #GstCaps.  A simple #GstCaps is one that
605  * only has one structure.  The arguments must be passed in the same
606  * manner as @gst_structure_set(), and be NULL-terminated.
607  */
608 void
609 gst_caps_set_simple_valist (GstCaps * caps, char *field, va_list varargs)
610 {
611   GstStructure *structure;
612
613   g_return_if_fail (GST_IS_CAPS (caps));
614   g_return_if_fail (caps->structs->len != 1);
615   g_return_if_fail (IS_WRITABLE (caps));
616
617   structure = gst_caps_get_structure (caps, 0);
618
619   gst_structure_set_valist (structure, field, varargs);
620 }
621
622 /* tests */
623
624 /**
625  * gst_caps_is_any:
626  * @caps: the @GstCaps to test
627  *
628  * Determines if @caps represents any media format.
629  *
630  * Returns: TRUE if @caps represents any format.
631  */
632 gboolean
633 gst_caps_is_any (const GstCaps * caps)
634 {
635   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
636
637   return (caps->flags & GST_CAPS_FLAGS_ANY);
638 }
639
640 /**
641  * gst_caps_is_empty:
642  * @caps: the @GstCaps to test
643  *
644  * Determines if @caps represents no media formats.
645  *
646  * Returns: TRUE if @caps represents no formats.
647  */
648 gboolean
649 gst_caps_is_empty (const GstCaps * caps)
650 {
651   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
652
653   if (caps->flags & GST_CAPS_FLAGS_ANY)
654     return FALSE;
655
656   return (caps->structs == NULL) || (caps->structs->len == 0);
657 }
658
659 static gboolean
660 gst_caps_is_fixed_foreach (GQuark field_id, const GValue * value,
661     gpointer unused)
662 {
663   return gst_value_is_fixed (value);
664 }
665
666 /**
667  * gst_caps_is_fixed:
668  * @caps: the @GstCaps to test
669  *
670  * Fixed @GstCaps describe exactly one format, that is, they have exactly
671  * one structure, and each field in the structure describes a fixed type.
672  * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
673  *
674  * Returns: TRUE if @caps is fixed
675  */
676 gboolean
677 gst_caps_is_fixed (const GstCaps * caps)
678 {
679   GstStructure *structure;
680
681   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
682
683   if (caps->structs->len != 1)
684     return FALSE;
685
686   structure = gst_caps_get_structure (caps, 0);
687
688   return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
689 }
690
691 static gboolean
692 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
693     gpointer data)
694 {
695   GstStructure *struct1 = (GstStructure *) data;
696   const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
697
698   if (val1 == NULL)
699     return FALSE;
700   if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
701     return TRUE;
702   }
703
704   return FALSE;
705 }
706
707 /**
708  * gst_caps_is_equal_fixed:
709  * @caps1: the #GstCaps to test
710  * @caps2: the #GstCaps to test
711  *
712  * Tests if two #GstCaps are equal.  This function only works on fixed
713  * #GstCaps.
714  *
715  * Returns: TRUE if the arguments represent the same format
716  */
717 gboolean
718 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
719 {
720   GstStructure *struct1, *struct2;
721
722   g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
723   g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
724
725   struct1 = gst_caps_get_structure (caps1, 0);
726   struct2 = gst_caps_get_structure (caps2, 0);
727
728   if (struct1->name != struct2->name) {
729     return FALSE;
730   }
731   if (struct1->fields->len != struct2->fields->len) {
732     return FALSE;
733   }
734
735   return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
736       struct2);
737 }
738
739 /**
740  * gst_caps_is_always_compatible:
741  * @caps1: the #GstCaps to test
742  * @caps2: the #GstCaps to test
743  *
744  * A given #GstCaps structure is always compatible with another if
745  * every media format that is in the first is also contained in the
746  * second.  That is, @caps1 is a subset of @caps2.
747  *
748  * Returns: TRUE if @caps1 is a subset of @caps2.
749  */
750 gboolean
751 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
752 {
753   g_return_val_if_fail (GST_IS_CAPS (caps1), FALSE);
754   g_return_val_if_fail (GST_IS_CAPS (caps2), FALSE);
755
756   return gst_caps_is_subset (caps1, caps2);
757 }
758
759 /**
760  * gst_caps_is_subset:
761  * @subset: a #GstCaps
762  * @superset: a potentially greater #GstCaps
763  *
764  * Checks if all caps represented by @subset are also represented by @superset
765  * <note>This function does not work reliably if optional properties for caps
766  * are included on one caps and omitted on the other.</note>
767  *
768  * Returns: TRUE if @subset is a subset of @superset
769  */
770 gboolean
771 gst_caps_is_subset (const GstCaps * subset, const GstCaps * superset)
772 {
773   GstCaps *caps;
774   gboolean ret;
775
776   g_return_val_if_fail (subset != NULL, FALSE);
777   g_return_val_if_fail (superset != NULL, FALSE);
778
779   if (gst_caps_is_empty (subset) || gst_caps_is_any (superset))
780     return TRUE;
781   if (gst_caps_is_any (subset) || gst_caps_is_empty (superset))
782     return FALSE;
783
784   caps = gst_caps_subtract (subset, superset);
785   ret = gst_caps_is_empty (caps);
786   gst_caps_unref (caps);
787   return ret;
788 }
789
790 /**
791  * gst_caps_is_equal:
792  * @caps1: a #GstCaps
793  * @caps2: another #GstCaps
794  *
795  * Checks if the given caps represent the same set of caps.
796  * <note>This function does not work reliably if optional properties for caps
797  * are included on one caps and omitted on the other.</note>
798  *
799  * Returns: TRUE if both caps are equal
800  */
801 gboolean
802 gst_caps_is_equal (const GstCaps * caps1, const GstCaps * caps2)
803 {
804   g_return_val_if_fail (caps1 != NULL, FALSE);
805   g_return_val_if_fail (caps2 != NULL, FALSE);
806
807   if (gst_caps_is_fixed (caps1) && gst_caps_is_fixed (caps2))
808     return gst_caps_is_equal_fixed (caps1, caps2);
809
810   return gst_caps_is_subset (caps1, caps2) && gst_caps_is_subset (caps2, caps1);
811 }
812
813 typedef struct
814 {
815   GstStructure *dest;
816   const GstStructure *intersect;
817   gboolean first_run;
818 }
819 IntersectData;
820
821 static gboolean
822 gst_caps_structure_intersect_field (GQuark id, const GValue * val1,
823     gpointer data)
824 {
825   IntersectData *idata = (IntersectData *) data;
826   GValue dest_value = { 0 };
827   const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
828
829   if (val2 == NULL) {
830     gst_structure_id_set_value (idata->dest, id, val1);
831   } else if (idata->first_run) {
832     if (gst_value_intersect (&dest_value, val1, val2)) {
833       gst_structure_id_set_value (idata->dest, id, &dest_value);
834       g_value_unset (&dest_value);
835     } else {
836       return FALSE;
837     }
838   }
839
840   return TRUE;
841 }
842
843 static GstStructure *
844 gst_caps_structure_intersect (const GstStructure * struct1,
845     const GstStructure * struct2)
846 {
847   IntersectData data;
848
849   g_return_val_if_fail (struct1 != NULL, NULL);
850   g_return_val_if_fail (struct2 != NULL, NULL);
851
852   if (struct1->name != struct2->name)
853     return NULL;
854
855   data.dest = gst_structure_id_empty_new (struct1->name);
856   data.intersect = struct2;
857   data.first_run = TRUE;
858   if (!gst_structure_foreach ((GstStructure *) struct1,
859           gst_caps_structure_intersect_field, &data))
860     goto error;
861
862   data.intersect = struct1;
863   data.first_run = FALSE;
864   if (!gst_structure_foreach ((GstStructure *) struct2,
865           gst_caps_structure_intersect_field, &data))
866     goto error;
867
868   return data.dest;
869
870 error:
871   gst_structure_free (data.dest);
872   return NULL;
873 }
874
875 #if 0
876 static GstStructure *
877 gst_caps_structure_union (const GstStructure * struct1,
878     const GstStructure * struct2)
879 {
880   int i;
881   GstStructure *dest;
882   const GstStructureField *field1;
883   const GstStructureField *field2;
884   int ret;
885
886   /* FIXME this doesn't actually work */
887
888   if (struct1->name != struct2->name)
889     return NULL;
890
891   dest = gst_structure_id_empty_new (struct1->name);
892
893   for (i = 0; i < struct1->fields->len; i++) {
894     GValue dest_value = { 0 };
895
896     field1 = GST_STRUCTURE_FIELD (struct1, i);
897     field2 = gst_structure_id_get_field (struct2, field1->name);
898
899     if (field2 == NULL) {
900       continue;
901     } else {
902       if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
903         gst_structure_set_value (dest, g_quark_to_string (field1->name),
904             &dest_value);
905       } else {
906         ret = gst_value_compare (&field1->value, &field2->value);
907       }
908     }
909   }
910
911   return dest;
912 }
913 #endif
914
915 /* operations */
916
917 /**
918  * gst_caps_intersect:
919  * @caps1: a #GstCaps to intersect
920  * @caps2: a #GstCaps to intersect
921  *
922  * Creates a new #GstCaps that contains all the formats that are common
923  * to both @caps1 and @caps2.
924  *
925  * Returns: the new #GstCaps
926  */
927 GstCaps *
928 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
929 {
930   int i, j, k;
931   GstStructure *struct1;
932   GstStructure *struct2;
933   GstCaps *dest;
934   GstStructure *istruct;
935
936   g_return_val_if_fail (GST_IS_CAPS (caps1), NULL);
937   g_return_val_if_fail (GST_IS_CAPS (caps2), NULL);
938
939   if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)) {
940     return gst_caps_new_empty ();
941   }
942   if (gst_caps_is_any (caps1))
943     return gst_caps_copy (caps2);
944   if (gst_caps_is_any (caps2))
945     return gst_caps_copy (caps1);
946
947   dest = gst_caps_new_empty ();
948
949   /* run zigzag on top line then right line, this preserves the caps order
950    * much better than a simple loop. 
951    * 
952    * This algorithm zigzags over the caps structures as demonstrated in
953    * the folowing matrix:
954    *
955    *          caps1
956    *       +-------------
957    *       | 1  2  4  7
958    * caps2 | 3  5  8 10
959    *       | 6  9 11 12
960    *       
961    * First we iterate over the caps1 structures (top line) intersecting
962    * the structures diagonally down, then we iterate over the caps2
963    * structures. 
964    */
965   for (i = 0; i < caps1->structs->len + caps2->structs->len - 1; i++) {
966     /* caps1 index goes from 0 to caps1->structs->len-1 */
967     j = MIN (i, caps1->structs->len - 1);
968     /* caps2 index stays 0 until i reaches caps1->structs->len, then it counts
969      * up from 1 to caps2->structs->len - 1 */
970     k = MAX (0, i - j);
971
972     /* now run the diagonal line, end condition is the left or bottom
973      * border */
974     while (k < caps2->structs->len && j >= 0) {
975       struct1 = gst_caps_get_structure (caps1, j);
976       struct2 = gst_caps_get_structure (caps2, k);
977
978       istruct = gst_caps_structure_intersect (struct1, struct2);
979
980       gst_caps_append_structure (dest, istruct);
981       /* move down left */
982       k++;
983       j--;
984     }
985   }
986   gst_caps_do_simplify (dest);
987   return dest;
988 }
989
990 typedef struct
991 {
992   const GstStructure *subtract_from;
993   GSList *put_into;
994 }
995 SubtractionEntry;
996
997
998 static gboolean
999 gst_caps_structure_subtract_field (GQuark field_id, const GValue * value,
1000     gpointer user_data)
1001 {
1002   SubtractionEntry *e = user_data;
1003   GValue subtraction = { 0, };
1004   const GValue *other;
1005   GstStructure *structure;
1006
1007   other = gst_structure_id_get_value (e->subtract_from, field_id);
1008   if (!other) {
1009     return FALSE;
1010   }
1011   if (!gst_value_subtract (&subtraction, other, value))
1012     return TRUE;
1013   if (gst_value_compare (&subtraction, other) == GST_VALUE_EQUAL) {
1014     g_value_unset (&subtraction);
1015     return FALSE;
1016   } else {
1017     structure = gst_structure_copy (e->subtract_from);
1018     gst_structure_id_set_value (structure, field_id, &subtraction);
1019     g_value_unset (&subtraction);
1020     e->put_into = g_slist_prepend (e->put_into, structure);
1021     return TRUE;
1022   }
1023 }
1024
1025 static gboolean
1026 gst_caps_structure_subtract (GSList ** into, const GstStructure * minuend,
1027     const GstStructure * subtrahend)
1028 {
1029   SubtractionEntry e;
1030   gboolean ret;
1031
1032   e.subtract_from = minuend;
1033   e.put_into = NULL;
1034
1035   ret = gst_structure_foreach ((GstStructure *) subtrahend,
1036       gst_caps_structure_subtract_field, &e);
1037   if (ret) {
1038     *into = e.put_into;
1039   } else {
1040     GSList *walk;
1041
1042     for (walk = e.put_into; walk; walk = g_slist_next (walk)) {
1043       gst_structure_free (walk->data);
1044     }
1045     g_slist_free (e.put_into);
1046   }
1047   return ret;
1048 }
1049
1050 /**
1051  * gst_caps_subtract:
1052  * @minuend: #GstCaps to substract from
1053  * @subtrahend: #GstCaps to substract
1054  *
1055  * Subtracts the @subtrahend from the @minuend.
1056  * <note>This function does not work reliably if optional properties for caps
1057  * are included on one caps and omitted on the other.</note>
1058  *
1059  * Returns: the resulting caps
1060  */
1061 GstCaps *
1062 gst_caps_subtract (const GstCaps * minuend, const GstCaps * subtrahend)
1063 {
1064   int i, j;
1065   GstStructure *min;
1066   GstStructure *sub;
1067   GstCaps *dest = NULL, *src;
1068
1069   g_return_val_if_fail (minuend != NULL, NULL);
1070   g_return_val_if_fail (subtrahend != NULL, NULL);
1071
1072   if (gst_caps_is_empty (minuend) || gst_caps_is_any (subtrahend)) {
1073     return gst_caps_new_empty ();
1074   }
1075   if (gst_caps_is_empty (subtrahend))
1076     return gst_caps_copy (minuend);
1077
1078   /* FIXME: Do we want this here or above?
1079      The reason we need this is that there is no definition about what
1080      ANY means for specific types, so it's not possible to reduce ANY partially
1081      You can only remove everything or nothing and that is done above.
1082      Note: there's a test that checks this behaviour. */
1083   g_return_val_if_fail (!gst_caps_is_any (minuend), NULL);
1084   g_assert (subtrahend->structs->len > 0);
1085
1086   src = gst_caps_copy (minuend);
1087   for (i = 0; i < subtrahend->structs->len; i++) {
1088     sub = gst_caps_get_structure (subtrahend, i);
1089     if (dest) {
1090       gst_caps_unref (src);
1091       src = dest;
1092     }
1093     dest = gst_caps_new_empty ();
1094     for (j = 0; j < src->structs->len; j++) {
1095       min = gst_caps_get_structure (src, j);
1096       if (gst_structure_get_name_id (min) == gst_structure_get_name_id (sub)) {
1097         GSList *list;
1098
1099         if (gst_caps_structure_subtract (&list, min, sub)) {
1100           GSList *walk;
1101
1102           for (walk = list; walk; walk = g_slist_next (walk)) {
1103             gst_caps_append_structure (dest, (GstStructure *) walk->data);
1104           }
1105           g_slist_free (list);
1106         } else {
1107           gst_caps_append_structure (dest, gst_structure_copy (min));
1108         }
1109       } else {
1110         gst_caps_append_structure (dest, gst_structure_copy (min));
1111       }
1112     }
1113     if (gst_caps_is_empty (dest)) {
1114       gst_caps_unref (src);
1115       return dest;
1116     }
1117   }
1118
1119   gst_caps_unref (src);
1120   gst_caps_do_simplify (dest);
1121   return dest;
1122 }
1123
1124 /**
1125  * gst_caps_union:
1126  * @caps1: a #GstCaps to union
1127  * @caps2: a #GstCaps to union
1128  *
1129  * Creates a new #GstCaps that contains all the formats that are in
1130  * either @caps1 and @caps2.
1131  *
1132  * Returns: the new #GstCaps
1133  */
1134 GstCaps *
1135 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
1136 {
1137   GstCaps *dest1;
1138   GstCaps *dest2;
1139
1140   if (gst_caps_is_any (caps1) || gst_caps_is_any (caps2))
1141     return gst_caps_new_any ();
1142
1143   dest1 = gst_caps_copy (caps1);
1144   dest2 = gst_caps_copy (caps2);
1145   gst_caps_append (dest1, dest2);
1146
1147   gst_caps_do_simplify (dest1);
1148   return dest1;
1149 }
1150
1151 typedef struct _NormalizeForeach
1152 {
1153   GstCaps *caps;
1154   GstStructure *structure;
1155 }
1156 NormalizeForeach;
1157
1158 static gboolean
1159 gst_caps_normalize_foreach (GQuark field_id, const GValue * value, gpointer ptr)
1160 {
1161   NormalizeForeach *nf = (NormalizeForeach *) ptr;
1162   GValue val = { 0 };
1163   int i;
1164
1165   if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1166     for (i = 1; i < gst_value_list_get_size (value); i++) {
1167       const GValue *v = gst_value_list_get_value (value, i);
1168       GstStructure *structure = gst_structure_copy (nf->structure);
1169
1170       gst_structure_id_set_value (structure, field_id, v);
1171       gst_caps_append_structure (nf->caps, structure);
1172     }
1173
1174     gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
1175     gst_structure_id_set_value (nf->structure, field_id, &val);
1176     g_value_unset (&val);
1177
1178     return FALSE;
1179   }
1180   return TRUE;
1181 }
1182
1183 /**
1184  * gst_caps_normalize:
1185  * @caps: a #GstCaps to normalize
1186  *
1187  * Creates a new #GstCaps that represents the same set of formats as
1188  * @caps, but contains no lists.  Each list is expanded into separate
1189  * @GstStructures.
1190  *
1191  * Returns: the new #GstCaps
1192  */
1193 GstCaps *
1194 gst_caps_normalize (const GstCaps * caps)
1195 {
1196   NormalizeForeach nf;
1197   GstCaps *newcaps;
1198   int i;
1199
1200   g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
1201
1202   newcaps = gst_caps_copy (caps);
1203   nf.caps = newcaps;
1204
1205   for (i = 0; i < newcaps->structs->len; i++) {
1206     nf.structure = gst_caps_get_structure (newcaps, i);
1207
1208     while (!gst_structure_foreach (nf.structure,
1209             gst_caps_normalize_foreach, &nf));
1210   }
1211
1212   return newcaps;
1213 }
1214
1215 static gint
1216 gst_caps_compare_structures (gconstpointer one, gconstpointer two)
1217 {
1218   gint ret;
1219   const GstStructure *struct1 = *((const GstStructure **) one);
1220   const GstStructure *struct2 = *((const GstStructure **) two);
1221
1222   /* FIXME: this orders aphabetically, but ordering the quarks might be faster 
1223      So what's the best way? */
1224   ret = strcmp (gst_structure_get_name (struct1),
1225       gst_structure_get_name (struct2));
1226   if (ret)
1227     return ret;
1228
1229   return gst_structure_n_fields (struct2) - gst_structure_n_fields (struct1);
1230 }
1231
1232 /**
1233  * gst_caps_simplify:
1234  * @caps: a #GstCaps to simplify
1235  *
1236  * Creates a new #GstCaps that represents the same set of formats as
1237  * @caps, but simpler.  Component structures that are identical are
1238  * merged.  Component structures that have ranges or lists that can
1239  * be merged are also merged.
1240  *
1241  * Returns: the new #GstCaps
1242  */
1243 GstCaps *
1244 gst_caps_simplify (const GstCaps * caps)
1245 {
1246   GstCaps *ret;
1247
1248   g_return_val_if_fail (caps != NULL, NULL);
1249
1250   ret = gst_caps_copy (caps);
1251   gst_caps_do_simplify (ret);
1252
1253   return ret;
1254 }
1255
1256 typedef struct
1257 {
1258   GQuark name;
1259   GValue value;
1260   GstStructure *compare;
1261 }
1262 UnionField;
1263
1264 static gboolean
1265 gst_caps_structure_figure_out_union (GQuark field_id, const GValue * value,
1266     gpointer user_data)
1267 {
1268   UnionField *u = user_data;
1269   const GValue *val = gst_structure_id_get_value (u->compare, field_id);
1270
1271   if (!val) {
1272     if (u->name)
1273       g_value_unset (&u->value);
1274     return FALSE;
1275   }
1276   if (gst_value_compare (val, value) == GST_VALUE_EQUAL)
1277     return TRUE;
1278   if (u->name) {
1279     g_value_unset (&u->value);
1280     return FALSE;
1281   }
1282   u->name = field_id;
1283   gst_value_union (&u->value, val, value);
1284   return TRUE;
1285 }
1286
1287 static gboolean
1288 gst_caps_structure_simplify (GstStructure ** result,
1289     const GstStructure * simplify, GstStructure * compare)
1290 {
1291   GSList *list;
1292   UnionField field = { 0, {0,}, NULL };
1293
1294   /* try to subtract to get a real subset */
1295   if (gst_caps_structure_subtract (&list, simplify, compare)) {
1296     switch (g_slist_length (list)) {
1297       case 0:
1298         *result = NULL;
1299         return TRUE;
1300       case 1:
1301         *result = list->data;
1302         g_slist_free (list);
1303         return TRUE;
1304       default:
1305       {
1306         GSList *walk;
1307
1308         for (walk = list; walk; walk = g_slist_next (walk)) {
1309           gst_structure_free (walk->data);
1310         }
1311         g_slist_free (list);
1312         break;
1313       }
1314     }
1315   }
1316
1317   /* try to union both structs */
1318   field.compare = compare;
1319   if (gst_structure_foreach ((GstStructure *) simplify,
1320           gst_caps_structure_figure_out_union, &field)) {
1321     gboolean ret = FALSE;
1322
1323     /* now we know all of simplify's fields are the same in compare
1324      * but at most one field: field.name */
1325     if (G_IS_VALUE (&field.value)) {
1326       if (gst_structure_n_fields (simplify) == gst_structure_n_fields (compare)) {
1327         gst_structure_id_set_value (compare, field.name, &field.value);
1328         *result = NULL;
1329         ret = TRUE;
1330       }
1331       g_value_unset (&field.value);
1332     } else if (gst_structure_n_fields (simplify) <=
1333         gst_structure_n_fields (compare)) {
1334       /* compare is just more specific, will be optimized away later */
1335       /* FIXME: do this here? */
1336       GST_LOG ("found a case that will be optimized later.");
1337     } else {
1338       gchar *one = gst_structure_to_string (simplify);
1339       gchar *two = gst_structure_to_string (compare);
1340
1341       GST_ERROR
1342           ("caps mismatch: structures %s and %s claim to be possible to unify, but aren't",
1343           one, two);
1344       g_free (one);
1345       g_free (two);
1346     }
1347     return ret;
1348   }
1349
1350   return FALSE;
1351 }
1352
1353 static void
1354 gst_caps_switch_structures (GstCaps * caps, GstStructure * old,
1355     GstStructure * new, gint i)
1356 {
1357   gst_structure_set_parent_refcount (old, NULL);
1358   gst_structure_free (old);
1359   gst_structure_set_parent_refcount (new, &caps->refcount);
1360   g_ptr_array_index (caps->structs, i) = new;
1361 }
1362
1363 /**
1364  * gst_caps_do_simplify:
1365  * @caps: a #GstCaps to simplify
1366  *
1367  * Modifies the given @caps inplace into a representation that represents the 
1368  * same set of formats, but in a simpler form.  Component structures that are 
1369  * identical are merged.  Component structures that have values that can be 
1370  * merged are also merged.
1371  *
1372  * Returns: TRUE, if the caps could be simplified
1373  */
1374 gboolean
1375 gst_caps_do_simplify (GstCaps * caps)
1376 {
1377   GstStructure *simplify, *compare, *result = NULL;
1378   gint i, j, start;
1379   gboolean changed = FALSE;
1380
1381   g_return_val_if_fail (caps != NULL, FALSE);
1382   g_return_val_if_fail (IS_WRITABLE (caps), FALSE);
1383
1384   if (gst_caps_get_size (caps) < 2)
1385     return FALSE;
1386
1387   g_ptr_array_sort (caps->structs, gst_caps_compare_structures);
1388
1389   start = caps->structs->len - 1;
1390   for (i = caps->structs->len - 1; i >= 0; i--) {
1391     simplify = gst_caps_get_structure (caps, i);
1392     if (gst_structure_get_name_id (simplify) !=
1393         gst_structure_get_name_id (gst_caps_get_structure (caps, start)))
1394       start = i;
1395     for (j = start; j >= 0; j--) {
1396       if (j == i)
1397         continue;
1398       compare = gst_caps_get_structure (caps, j);
1399       if (gst_structure_get_name_id (simplify) !=
1400           gst_structure_get_name_id (compare)) {
1401         break;
1402       }
1403       if (gst_caps_structure_simplify (&result, simplify, compare)) {
1404         if (result) {
1405           gst_caps_switch_structures (caps, simplify, result, i);
1406         } else {
1407           gst_caps_remove_structure (caps, i);
1408           start--;
1409           break;
1410         }
1411         changed = TRUE;
1412       }
1413     }
1414   }
1415
1416   if (!changed)
1417     return FALSE;
1418
1419   /* gst_caps_do_simplify (caps); */
1420   return TRUE;
1421 }
1422
1423 #ifndef GST_DISABLE_LOADSAVE
1424 /**
1425  * gst_caps_save_thyself:
1426  * @caps: a #GstCaps structure
1427  * @parent: a XML parent node
1428  *
1429  * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1430  *
1431  * Returns: a XML node pointer
1432  */
1433 xmlNodePtr
1434 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1435 {
1436   char *s = gst_caps_to_string (caps);
1437
1438   xmlNewChild (parent, NULL, (xmlChar *) "caps", (xmlChar *) s);
1439   g_free (s);
1440   return parent;
1441 }
1442
1443 /**
1444  * gst_caps_load_thyself:
1445  * @parent: a XML node
1446  *
1447  * Creates a #GstCaps from its XML serialization.
1448  *
1449  * Returns: a new #GstCaps structure
1450  */
1451 GstCaps *
1452 gst_caps_load_thyself (xmlNodePtr parent)
1453 {
1454   if (strcmp ("caps", (char *) parent->name) == 0) {
1455     return gst_caps_from_string ((gchar *) xmlNodeGetContent (parent));
1456   }
1457
1458   return NULL;
1459 }
1460 #endif
1461
1462 /* utility */
1463
1464 /**
1465  * gst_caps_replace:
1466  * @caps: a pointer to #GstCaps
1467  * @newcaps: a #GstCaps to replace *caps
1468  *
1469  * Replaces *caps with @newcaps.  Frees the #GstCaps in the location
1470  * pointed to by @caps, if applicable, then modifies @caps to point to
1471  * @newcaps.
1472  */
1473 void
1474 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1475 {
1476   GstCaps *oldcaps;
1477
1478 #if 0                           /* disable this, since too many plugins rely on undefined behavior */
1479 #ifdef USE_POISONING
1480   //if (newcaps) CAPS_POISON (newcaps);
1481 #endif
1482 #endif
1483   oldcaps = *caps;
1484
1485   if (newcaps)
1486     gst_caps_ref (newcaps);
1487
1488   *caps = newcaps;
1489
1490   if (oldcaps)
1491     gst_caps_unref (oldcaps);
1492 }
1493
1494 /**
1495  * gst_caps_to_string:
1496  * @caps: a #GstCaps
1497  *
1498  * Converts @caps to a string representation.  This string representation
1499  * can be converted back to a #GstCaps by #gst_caps_from_string.
1500  *
1501  * Returns: a newly allocated string representing @caps.
1502  */
1503 gchar *
1504 gst_caps_to_string (const GstCaps * caps)
1505 {
1506   int i;
1507   GstStructure *structure;
1508   GString *s;
1509   char *sstr;
1510
1511   /* NOTE:  This function is potentially called by the debug system,
1512    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1513    * should be careful to avoid recursion.  This includes any functions
1514    * called by gst_caps_to_string.  In particular, calls should
1515    * not use the GST_PTR_FORMAT extension.  */
1516
1517   /* FIXME does this leak? */
1518
1519   if (caps == NULL) {
1520     return g_strdup ("NULL");
1521   }
1522   if (gst_caps_is_any (caps)) {
1523     return g_strdup ("ANY");
1524   }
1525   if (gst_caps_is_empty (caps)) {
1526     return g_strdup ("EMPTY");
1527   }
1528   s = g_string_new ("");
1529   structure = gst_caps_get_structure (caps, 0);
1530   sstr = gst_structure_to_string (structure);
1531   g_string_append (s, sstr);
1532   g_free (sstr);
1533
1534   for (i = 1; i < caps->structs->len; i++) {
1535     structure = gst_caps_get_structure (caps, i);
1536
1537     g_string_append (s, "; ");
1538     sstr = gst_structure_to_string (structure);
1539     g_string_append (s, sstr);
1540     g_free (sstr);
1541   }
1542
1543   return g_string_free (s, FALSE);
1544 }
1545
1546 static gboolean
1547 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1548 {
1549   GstStructure *structure;
1550   gchar *s;
1551
1552   g_return_val_if_fail (string, FALSE);
1553   if (strcmp ("ANY", string) == 0) {
1554     caps->flags = GST_CAPS_FLAGS_ANY;
1555     return TRUE;
1556   }
1557   if (strcmp ("EMPTY", string) == 0) {
1558     return TRUE;
1559   }
1560
1561   structure = gst_structure_from_string (string, &s);
1562   if (structure == NULL) {
1563     return FALSE;
1564   }
1565   gst_caps_append_structure (caps, structure);
1566
1567   while (*s == ';') {
1568     s++;
1569     while (g_ascii_isspace (*s))
1570       s++;
1571     structure = gst_structure_from_string (s, &s);
1572     if (structure == NULL) {
1573       return FALSE;
1574     }
1575     gst_caps_append_structure (caps, structure);
1576     while (g_ascii_isspace (*s))
1577       s++;
1578   }
1579
1580   if (*s != 0) {
1581     return FALSE;
1582   }
1583
1584   return TRUE;
1585 }
1586
1587 /**
1588  * gst_caps_from_string:
1589  * @string: a string to convert to #GstCaps
1590  *
1591  * Converts @caps from a string representation.
1592  *
1593  * Returns: a newly allocated #GstCaps
1594  */
1595 GstCaps *
1596 gst_caps_from_string (const gchar * string)
1597 {
1598   GstCaps *caps;
1599
1600   caps = gst_caps_new_empty ();
1601   if (gst_caps_from_string_inplace (caps, string)) {
1602     return caps;
1603   } else {
1604     gst_caps_unref (caps);
1605     return NULL;
1606   }
1607 }
1608
1609 static void
1610 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1611 {
1612   g_return_if_fail (G_IS_VALUE (src_value));
1613   g_return_if_fail (G_IS_VALUE (dest_value));
1614   g_return_if_fail (G_VALUE_HOLDS (src_value, GST_TYPE_CAPS));
1615   g_return_if_fail (G_VALUE_HOLDS (dest_value, G_TYPE_STRING)
1616       || G_VALUE_HOLDS (dest_value, G_TYPE_POINTER));
1617
1618   dest_value->data[0].v_pointer =
1619       gst_caps_to_string (src_value->data[0].v_pointer);
1620 }
1621
1622 static GstCaps *
1623 gst_caps_copy_conditional (GstCaps * src)
1624 {
1625   if (src) {
1626     return gst_caps_ref (src);
1627   } else {
1628     return NULL;
1629   }
1630 }