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