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