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