e14db463b3ebf082690009c57bd1318f81609401
[platform/upstream/gstreamer.git] / gst / gstcaps.c
1 /* GStreamer
2  * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <string.h>
24
25 #include <gst/gst.h>
26
27 #define CAPS_POISON(caps) G_STMT_START{ \
28   if (caps) { \
29     GstCaps *_newcaps = gst_caps_copy (caps); \
30     gst_caps_free(caps); \
31     caps = _newcaps; \
32   } \
33 } G_STMT_END
34 #define STRUCTURE_POISON(structure) G_STMT_START{ \
35   if (structure) { \
36     GstStructure *_newstruct = gst_structure_copy (structure); \
37     gst_structure_free(structure); \
38     structure = _newstruct; \
39   } \
40 } G_STMT_END
41
42
43 static void gst_caps_transform_to_string (const GValue * src_value,
44     GValue * dest_value);
45 static gboolean gst_caps_from_string_inplace (GstCaps * caps,
46     const gchar * string);
47 static GstCaps *gst_caps_copy_conditional (const GstCaps * src);
48
49 GType
50 gst_caps_get_type (void)
51 {
52   static GType gst_caps_type = 0;
53
54   if (!gst_caps_type) {
55     gst_caps_type = g_boxed_type_register_static ("GstCaps",
56         (GBoxedCopyFunc) gst_caps_copy_conditional,
57         (GBoxedFreeFunc) gst_caps_free);
58
59     g_value_register_transform_func (gst_caps_type,
60         G_TYPE_STRING, gst_caps_transform_to_string);
61   }
62
63   return gst_caps_type;
64 }
65
66 /* creation/deletion */
67
68 /**
69  * gst_caps_new_empty:
70  *
71  * Creates a new #GstCaps that is empty.  That is, the returned
72  * #GstCaps contains no media formats.
73  *
74  * Returns: the new #GstCaps
75  */
76 GstCaps *
77 gst_caps_new_empty (void)
78 {
79   GstCaps *caps = g_new0 (GstCaps, 1);
80
81   caps->type = GST_TYPE_CAPS;
82   caps->structs = g_ptr_array_new ();
83
84   return caps;
85 }
86
87 /**
88  * gst_caps_new_empty:
89  *
90  * Creates a new #GstCaps that indicates that it is compatible with
91  * any media format.
92  *
93  * Returns: the new #GstCaps
94  */
95 GstCaps *
96 gst_caps_new_any (void)
97 {
98   GstCaps *caps = g_new0 (GstCaps, 1);
99
100   caps->type = GST_TYPE_CAPS;
101   caps->structs = g_ptr_array_new ();
102   caps->flags = GST_CAPS_FLAGS_ANY;
103
104   return caps;
105 }
106
107 /**
108  * gst_caps_new_simple:
109  * @media_type: the media type of the structure
110  * @...: additional arguments
111  *
112  * Creates a new #GstCaps that contains one #GstStructure.  The
113  * structure is defined by the arguments, which have the same format
114  * as @gst_structure_new().
115  *
116  * Returns: the new #GstCaps
117  */
118 GstCaps *
119 gst_caps_new_simple (const char *media_type, const char *fieldname, ...)
120 {
121   GstCaps *caps;
122   GstStructure *structure;
123   va_list var_args;
124
125   caps = g_new0 (GstCaps, 1);
126   caps->type = GST_TYPE_CAPS;
127   caps->structs = g_ptr_array_new ();
128
129   va_start (var_args, fieldname);
130   structure = gst_structure_new_valist (media_type, fieldname, var_args);
131   va_end (var_args);
132
133   gst_caps_append_structure (caps, structure);
134
135   return caps;
136 }
137
138 /**
139  * gst_caps_new_full:
140  * @struct1: the first structure to add
141  * @...: additional structures to add
142  *
143  * Creates a new #GstCaps and adds all the structures listed as
144  * arguments.  The list must be NULL-terminated.  The structures
145  * are not copied; the returned #GstCaps owns the structures.
146  *
147  * Returns: the new #GstCaps
148  */
149 GstCaps *
150 gst_caps_new_full (GstStructure * struct1, ...)
151 {
152   GstCaps *caps;
153   va_list var_args;
154
155   va_start (var_args, struct1);
156   caps = gst_caps_new_full_valist (struct1, var_args);
157   va_end (var_args);
158
159   return caps;
160 }
161
162 /**
163  * gst_caps_new_full_valist:
164  * @struct1: the first structure to add
165  * @var_args: additional structures to add
166  *
167  * Creates a new #GstCaps and adds all the structures listed as
168  * arguments.  The list must be NULL-terminated.  The structures
169  * are not copied; the returned #GstCaps owns the structures.
170  *
171  * Returns: the new #GstCaps
172  */
173 GstCaps *
174 gst_caps_new_full_valist (GstStructure * structure, va_list var_args)
175 {
176   GstCaps *caps;
177
178   caps = g_new0 (GstCaps, 1);
179   caps->type = GST_TYPE_CAPS;
180   caps->structs = g_ptr_array_new ();
181
182   while (structure) {
183     gst_caps_append_structure (caps, structure);
184     structure = va_arg (var_args, GstStructure *);
185   }
186
187   return caps;
188 }
189
190 /**
191  * gst_caps_copy:
192  * @caps: the #GstCaps to copy
193  *
194  * Deeply copies a #GstCaps, including all structures and all the
195  * structures' values.
196  *
197  * Returns: the new #GstCaps
198  */
199 GstCaps *
200 gst_caps_copy (const GstCaps * caps)
201 {
202   GstCaps *newcaps;
203   GstStructure *structure;
204   int i;
205
206   g_return_val_if_fail (caps != NULL, NULL);
207
208   newcaps = g_new0 (GstCaps, 1);
209   newcaps->type = GST_TYPE_CAPS;
210   newcaps->flags = caps->flags;
211   newcaps->structs = g_ptr_array_new ();
212
213   for (i = 0; i < caps->structs->len; i++) {
214     structure = gst_caps_get_structure (caps, i);
215     gst_caps_append_structure (newcaps, gst_structure_copy (structure));
216   }
217
218   return newcaps;
219 }
220
221 /**
222  * gst_caps_free:
223  * @caps: the #GstCaps to free
224  *
225  * Frees a #GstCaps and all its structures and the structures'
226  * values.
227  */
228 void
229 gst_caps_free (GstCaps * caps)
230 {
231   GstStructure *structure;
232   int i;
233
234   g_return_if_fail (caps != NULL);
235
236   for (i = 0; i < caps->structs->len; i++) {
237     structure = gst_caps_get_structure (caps, i);
238     gst_structure_free (structure);
239   }
240   g_ptr_array_free (caps->structs, TRUE);
241 #ifdef USE_POISONING
242   memset (caps, 0xff, sizeof (GstCaps));
243 #endif
244   g_free (caps);
245 }
246
247 /**
248  * gst_static_caps_get:
249  * @static_caps: the #GstStaticCaps to convert
250  *
251  * Converts a #GstStaticCaps to a #GstCaps.
252  *
253  * Returns: the new #GstCaps
254  */
255 const GstCaps *
256 gst_static_caps_get (GstStaticCaps * static_caps)
257 {
258   GstCaps *caps = (GstCaps *) static_caps;
259   gboolean ret;
260
261   if (caps->type == 0) {
262     caps->type = GST_TYPE_CAPS;
263     caps->structs = g_ptr_array_new ();
264     ret = gst_caps_from_string_inplace (caps, static_caps->string);
265
266     if (!ret) {
267       g_critical ("Could not convert static caps \"%s\"", static_caps->string);
268     }
269   }
270
271   return caps;
272 }
273
274 /* manipulation */
275
276 /**
277  * gst_caps_append:
278  * @caps1: the #GstCaps that will be appended to
279  * @caps2: the #GstCaps to append
280  *
281  * Appends the structures contained in @caps2 to @caps1.  The structures
282  * in @caps2 are not copied -- they are transferred to @caps1, and then
283  * @caps2 is freed.
284  */
285 void
286 gst_caps_append (GstCaps * caps1, GstCaps * caps2)
287 {
288   GstStructure *structure;
289   int i;
290
291   g_return_if_fail (caps1 != NULL);
292   g_return_if_fail (caps2 != NULL);
293
294 #ifdef USE_POISONING
295   CAPS_POISON (caps2);
296 #endif
297   for (i = 0; i < caps2->structs->len; i++) {
298     structure = gst_caps_get_structure (caps2, i);
299     gst_caps_append_structure (caps1, structure);
300   }
301   g_ptr_array_free (caps2->structs, TRUE);
302 #ifdef USE_POISONING
303   memset (caps2, 0xff, sizeof (GstCaps));
304 #endif
305   g_free (caps2);
306 }
307
308 /**
309  * gst_caps_append_structure:
310  * @caps: the #GstCaps that will be appended to
311  * @structure: the #GstStructure to append
312  *
313  * Appends @structure to @caps1.  The structure is not copied; @caps1
314  * becomes the owner of @structure.
315  */
316 void
317 gst_caps_append_structure (GstCaps * caps, GstStructure * structure)
318 {
319   g_return_if_fail (caps != NULL);
320
321   if (structure) {
322 #if 0
323 #ifdef USE_POISONING
324     STRUCTURE_POISON (structure);
325 #endif
326 #endif
327     g_ptr_array_add (caps->structs, structure);
328   }
329 }
330
331 /**
332  * gst_caps_split_one:
333  * @caps: 
334  *
335  * This function is not implemented.
336  *
337  * Returns:
338  */
339 GstCaps *
340 gst_caps_split_one (GstCaps * caps)
341 {
342   /* FIXME */
343   g_critical ("unimplemented");
344
345   return NULL;
346 }
347
348 /**
349  * gst_caps_get_size:
350  * @caps: a #GstCaps
351  *
352  * Gets the number of structures contained in @caps.
353  *
354  * Returns: the number of structures that @caps contains
355  */
356 int
357 gst_caps_get_size (const GstCaps * caps)
358 {
359   g_return_val_if_fail (caps != NULL, 0);
360
361   return caps->structs->len;
362 }
363
364 /**
365  * gst_caps_get_structure:
366  * @caps: a #GstCaps
367  * @index: the index of the structure
368  *
369  * Finds the structure in @caps that has the index @index, and 
370  * returns it.
371  *
372  * WARNING: This function takes a const GstCaps *, but returns a
373  * non-const GstStructure *.  This is for programming convenience --
374  * the caller should be aware that structures inside a constant
375  * @GstCaps should not be modified.
376  *
377  * Returns: a pointer to the #GstStructure corresponding to @index
378  */
379 GstStructure *
380 gst_caps_get_structure (const GstCaps * caps, int index)
381 {
382   g_return_val_if_fail (caps != NULL, NULL);
383   g_return_val_if_fail (index >= 0, NULL);
384   g_return_val_if_fail (index < caps->structs->len, NULL);
385
386   return g_ptr_array_index (caps->structs, index);
387 }
388
389 /**
390  * gst_caps_copy_1:
391  * @caps: the @GstCaps to copy
392  *
393  * Creates a new @GstCaps and appends a copy of the first structure
394  * contained in @caps.
395  *
396  * Returns: the new @GstCaps
397  */
398 GstCaps *
399 gst_caps_copy_1 (const GstCaps * caps)
400 {
401   GstCaps *newcaps;
402   GstStructure *structure;
403
404   g_return_val_if_fail (caps != NULL, NULL);
405
406   newcaps = g_new0 (GstCaps, 1);
407   newcaps->type = GST_TYPE_CAPS;
408   newcaps->flags = caps->flags;
409   newcaps->structs = g_ptr_array_new ();
410
411   if (caps->structs->len > 0) {
412     structure = gst_caps_get_structure (caps, 0);
413     gst_caps_append_structure (newcaps, gst_structure_copy (structure));
414   }
415
416   return newcaps;
417 }
418
419 /**
420  * gst_caps_set_simple:
421  * @caps: the @GstCaps to set
422  * @field: first field to set
423  * @...: additional parameters
424  *
425  * Sets fields in a simple #GstCaps.  A simple #GstCaps is one that
426  * only has one structure.  The arguments must be passed in the same
427  * manner as @gst_structure_set(), and be NULL-terminated.
428  */
429 void
430 gst_caps_set_simple (GstCaps * caps, char *field, ...)
431 {
432   GstStructure *structure;
433   va_list var_args;
434
435   g_return_if_fail (caps != NULL);
436   g_return_if_fail (caps->structs->len == 1);
437
438   structure = gst_caps_get_structure (caps, 0);
439
440   va_start (var_args, field);
441   gst_structure_set_valist (structure, field, var_args);
442   va_end (var_args);
443 }
444
445 /**
446  * gst_caps_set_simple_valist:
447  * @caps: the @GstCaps to copy
448  * @field: first field to set
449  * @varargs: additional parameters
450  *
451  * Sets fields in a simple #GstCaps.  A simple #GstCaps is one that
452  * only has one structure.  The arguments must be passed in the same
453  * manner as @gst_structure_set(), and be NULL-terminated.
454  */
455 void
456 gst_caps_set_simple_valist (GstCaps * caps, char *field, va_list varargs)
457 {
458   GstStructure *structure;
459
460   g_return_if_fail (caps != NULL);
461   g_return_if_fail (caps->structs->len != 1);
462
463   structure = gst_caps_get_structure (caps, 0);
464
465   gst_structure_set_valist (structure, field, varargs);
466 }
467
468 /* tests */
469
470 /**
471  * gst_caps_is_any:
472  * @caps: the @GstCaps to test
473  *
474  * Determines if @caps represents any media format.
475  *
476  * Returns: TRUE if @caps represents any format.
477  */
478 gboolean
479 gst_caps_is_any (const GstCaps * caps)
480 {
481   g_return_val_if_fail (caps != NULL, FALSE);
482
483   return (caps->flags & GST_CAPS_FLAGS_ANY);
484 }
485
486 /**
487  * gst_caps_is_empty:
488  * @caps: the @GstCaps to test
489  *
490  * Determines if @caps represents no media formats.
491  *
492  * Returns: TRUE if @caps represents no formats.
493  */
494 gboolean
495 gst_caps_is_empty (const GstCaps * caps)
496 {
497   g_return_val_if_fail (caps != NULL, FALSE);
498
499   if (caps->flags & GST_CAPS_FLAGS_ANY)
500     return FALSE;
501
502   return (caps->structs == NULL) || (caps->structs->len == 0);
503 }
504
505 /**
506  * gst_caps_is_chained:
507  * @caps: the @GstCaps to test
508  *
509  * Determines if @caps contains multiple #GstStructures.
510  *
511  * This function is deprecated, and should not be used in new code.
512  * Use #gst_caps_is_simple() instead.
513  *
514  * Returns: TRUE if @caps contains more than one structure
515  */
516 gboolean
517 gst_caps_is_chained (const GstCaps * caps)
518 {
519   g_return_val_if_fail (caps != NULL, FALSE);
520
521   return (caps->structs->len > 1);
522 }
523
524 static gboolean
525 gst_caps_is_fixed_foreach (GQuark field_id, GValue * value, gpointer unused)
526 {
527   GType type = G_VALUE_TYPE (value);
528
529   if (G_TYPE_IS_FUNDAMENTAL (type))
530     return TRUE;
531   if (type == GST_TYPE_FOURCC)
532     return TRUE;
533   return FALSE;
534 }
535
536 /**
537  * gst_caps_is_fixed:
538  * @caps: the @GstCaps to test
539  *
540  * Fixed @GstCaps describe exactly one format, that is, they have exactly
541  * one structure, and each field in the structure describes a fixed type.
542  * Examples of non-fixed types are GST_TYPE_INT_RANGE and GST_TYPE_LIST.
543  *
544  * Returns: TRUE if @caps is fixed
545  */
546 gboolean
547 gst_caps_is_fixed (const GstCaps * caps)
548 {
549   GstStructure *structure;
550
551   g_return_val_if_fail (caps != NULL, FALSE);
552
553   if (caps->structs->len != 1)
554     return FALSE;
555
556   structure = gst_caps_get_structure (caps, 0);
557
558   return gst_structure_foreach (structure, gst_caps_is_fixed_foreach, NULL);
559 }
560
561 static gboolean
562 gst_structure_is_equal_foreach (GQuark field_id, GValue * val2, gpointer data)
563 {
564   GstStructure *struct1 = (GstStructure *) data;
565   const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
566
567   if (val1 == NULL)
568     return FALSE;
569   if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
570     return TRUE;
571   }
572
573   return FALSE;
574 }
575
576 /**
577  * gst_caps_is_equal_fixed:
578  * @caps1: the #GstCaps to test
579  * @caps2: the #GstCaps to test
580  *
581  * Tests if two #GstCaps are equal.  This function only works on fixed
582  * #GstCaps.
583  *
584  * Returns: TRUE if the arguments represent the same format
585  */
586 gboolean
587 gst_caps_is_equal_fixed (const GstCaps * caps1, const GstCaps * caps2)
588 {
589   GstStructure *struct1, *struct2;
590
591   g_return_val_if_fail (gst_caps_is_fixed (caps1), FALSE);
592   g_return_val_if_fail (gst_caps_is_fixed (caps2), FALSE);
593
594   struct1 = gst_caps_get_structure (caps1, 0);
595   struct2 = gst_caps_get_structure (caps2, 0);
596
597   if (struct1->name != struct2->name) {
598     return FALSE;
599   }
600   if (struct1->fields->len != struct2->fields->len) {
601     return FALSE;
602   }
603
604   return gst_structure_foreach (struct1, gst_structure_is_equal_foreach,
605       struct2);
606 }
607
608 static gboolean
609 gst_structure_field_has_compatible (GQuark field_id,
610     GValue * val2, gpointer data)
611 {
612   GValue dest = { 0 };
613   GstStructure *struct1 = (GstStructure *) data;
614   const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
615
616   if (val1 == NULL)
617     return FALSE;
618   if (gst_value_intersect (&dest, val1, val2)) {
619     g_value_unset (&dest);
620     return TRUE;
621   }
622
623   return FALSE;
624 }
625
626 static gboolean
627 gst_cap_is_always_compatible (const GstStructure * struct1,
628     const GstStructure * struct2)
629 {
630   if (struct1->name != struct2->name) {
631     return FALSE;
632   }
633
634   /* the reversed order is important */
635   return gst_structure_foreach ((GstStructure *) struct2,
636       gst_structure_field_has_compatible, (gpointer) struct1);
637 }
638
639 static gboolean
640 gst_caps_cap_is_always_compatible (const GstStructure * struct1,
641     const GstCaps * caps2)
642 {
643   int i;
644
645   for (i = 0; i < caps2->structs->len; i++) {
646     GstStructure *struct2 = gst_caps_get_structure (caps2, i);
647
648     if (gst_cap_is_always_compatible (struct1, struct2)) {
649       return TRUE;
650     }
651   }
652
653   return FALSE;
654 }
655
656 /**
657  * gst_caps_is_always_compatible:
658  * @caps1: the #GstCaps to test
659  * @caps2: the #GstCaps to test
660  *
661  * A given #GstCaps structure is always compatible with another if
662  * every media format that is in the first is also contained in the
663  * second.  That is, @caps1 is a subset of @caps2.
664  *
665  * Returns: TRUE if @caps1 is a subset of @caps2.
666  */
667 gboolean
668 gst_caps_is_always_compatible (const GstCaps * caps1, const GstCaps * caps2)
669 {
670   int i;
671
672   g_return_val_if_fail (caps1 != NULL, FALSE);
673   g_return_val_if_fail (caps2 != NULL, FALSE);
674
675   if (gst_caps_is_any (caps2))
676     return TRUE;
677   if (gst_caps_is_any (caps1))
678     return FALSE;
679   if (gst_caps_is_empty (caps1))
680     return TRUE;
681   if (gst_caps_is_empty (caps2))
682     return FALSE;
683
684   for (i = 0; i < caps1->structs->len; i++) {
685     GstStructure *struct1 = gst_caps_get_structure (caps1, i);
686
687     if (gst_caps_cap_is_always_compatible (struct1, caps2) == FALSE) {
688       return FALSE;
689     }
690
691   }
692
693   return FALSE;
694 }
695
696 typedef struct
697 {
698   GstStructure *dest;
699   const GstStructure *intersect;
700   gboolean first_run;
701 }
702 IntersectData;
703
704 static gboolean
705 gst_caps_structure_intersect_field (GQuark id, GValue * val1, gpointer data)
706 {
707   IntersectData *idata = (IntersectData *) data;
708   GValue dest_value = { 0 };
709   const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
710
711   if (val2 == NULL) {
712     gst_structure_id_set_value (idata->dest, id, val1);
713   } else if (idata->first_run) {
714     if (gst_value_intersect (&dest_value, val1, val2)) {
715       gst_structure_id_set_value (idata->dest, id, &dest_value);
716       g_value_unset (&dest_value);
717     } else {
718       return FALSE;
719     }
720   }
721
722   return TRUE;
723 }
724
725 static GstStructure *
726 gst_caps_structure_intersect (const GstStructure * struct1,
727     const GstStructure * struct2)
728 {
729   IntersectData data;
730
731   g_return_val_if_fail (struct1 != NULL, NULL);
732   g_return_val_if_fail (struct2 != NULL, NULL);
733
734   if (struct1->name != struct2->name)
735     return NULL;
736
737   data.dest = gst_structure_id_empty_new (struct1->name);
738   data.intersect = struct2;
739   data.first_run = TRUE;
740   if (!gst_structure_foreach ((GstStructure *) struct1,
741           gst_caps_structure_intersect_field, &data))
742     goto error;
743
744   data.intersect = struct1;
745   data.first_run = FALSE;
746   if (!gst_structure_foreach ((GstStructure *) struct2,
747           gst_caps_structure_intersect_field, &data))
748     goto error;
749
750   return data.dest;
751
752 error:
753   gst_structure_free (data.dest);
754   return NULL;
755 }
756
757 #if 0
758 static GstStructure *
759 gst_caps_structure_union (const GstStructure * struct1,
760     const GstStructure * struct2)
761 {
762   int i;
763   GstStructure *dest;
764   const GstStructureField *field1;
765   const GstStructureField *field2;
766   int ret;
767
768   /* FIXME this doesn't actually work */
769
770   if (struct1->name != struct2->name)
771     return NULL;
772
773   dest = gst_structure_id_empty_new (struct1->name);
774
775   for (i = 0; i < struct1->fields->len; i++) {
776     GValue dest_value = { 0 };
777
778     field1 = GST_STRUCTURE_FIELD (struct1, i);
779     field2 = gst_structure_id_get_field (struct2, field1->name);
780
781     if (field2 == NULL) {
782       continue;
783     } else {
784       if (gst_value_union (&dest_value, &field1->value, &field2->value)) {
785         gst_structure_set_value (dest, g_quark_to_string (field1->name),
786             &dest_value);
787       } else {
788         ret = gst_value_compare (&field1->value, &field2->value);
789       }
790     }
791   }
792
793   return dest;
794 }
795 #endif
796
797 /* operations */
798
799 /**
800  * gst_caps_intersect:
801  * @caps1: a #GstCaps to intersect
802  * @caps2: a #GstCaps to intersect
803  *
804  * Creates a new #GstCaps that contains all the formats that are common
805  * to both @caps1 and @caps2.
806  *
807  * Returns: the new #GstCaps
808  */
809 GstCaps *
810 gst_caps_intersect (const GstCaps * caps1, const GstCaps * caps2)
811 {
812   int i, j;
813   GstStructure *struct1;
814   GstStructure *struct2;
815   GstCaps *dest;
816
817 #if 0
818   GstCaps *caps;
819 #endif
820
821   g_return_val_if_fail (caps1 != NULL, NULL);
822   g_return_val_if_fail (caps2 != NULL, NULL);
823
824   if (gst_caps_is_empty (caps1) || gst_caps_is_empty (caps2)) {
825     return gst_caps_new_empty ();
826   }
827   if (gst_caps_is_any (caps1))
828     return gst_caps_copy (caps2);
829   if (gst_caps_is_any (caps2))
830     return gst_caps_copy (caps1);
831
832   dest = gst_caps_new_empty ();
833   for (i = 0; i < caps1->structs->len; i++) {
834     struct1 = gst_caps_get_structure (caps1, i);
835     for (j = 0; j < caps2->structs->len; j++) {
836       GstStructure *istruct;
837
838       struct2 = gst_caps_get_structure (caps2, j);
839       istruct = gst_caps_structure_intersect (struct1, struct2);
840
841       gst_caps_append_structure (dest, istruct);
842     }
843   }
844
845 #if 0
846   caps = gst_caps_simplify (dest);
847   gst_caps_free (dest);
848
849   return caps;
850 #else
851   return dest;
852 #endif
853 }
854
855 /**
856  * gst_caps_union:
857  * @caps1: a #GstCaps to union
858  * @caps2: a #GstCaps to union
859  *
860  * Creates a new #GstCaps that contains all the formats that are in
861  * either @caps1 and @caps2.
862  *
863  * Returns: the new #GstCaps
864  */
865 GstCaps *
866 gst_caps_union (const GstCaps * caps1, const GstCaps * caps2)
867 {
868   GstCaps *dest1;
869   GstCaps *dest2;
870
871   dest1 = gst_caps_copy (caps1);
872   dest2 = gst_caps_copy (caps2);
873   gst_caps_append (dest1, dest2);
874
875   /* FIXME: need a simplify function */
876
877   return dest1;
878 }
879
880 typedef struct _NormalizeForeach
881 {
882   GstCaps *caps;
883   GstStructure *structure;
884 }
885 NormalizeForeach;
886
887 static gboolean
888 gst_caps_normalize_foreach (GQuark field_id, GValue * value, gpointer ptr)
889 {
890   NormalizeForeach *nf = (NormalizeForeach *) ptr;
891   GValue val = { 0 };
892   int i;
893
894   if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
895     for (i = 1; i < gst_value_list_get_size (value); i++) {
896       const GValue *v = gst_value_list_get_value (value, i);
897       GstStructure *structure = gst_structure_copy (nf->structure);
898
899       gst_structure_id_set_value (structure, field_id, v);
900       gst_caps_append_structure (nf->caps, structure);
901     }
902
903     gst_value_init_and_copy (&val, gst_value_list_get_value (value, 0));
904     gst_structure_id_set_value (nf->structure, field_id, &val);
905     g_value_unset (&val);
906
907     return FALSE;
908   }
909   return TRUE;
910 }
911
912 /**
913  * gst_caps_normalize:
914  * @caps: a #GstCaps to normalize
915  *
916  * Creates a new #GstCaps that represents the same set of formats as
917  * @caps, but contains no lists.  Each list is expanded into separate
918  * @GstStructures.
919  *
920  * Returns: the new #GstCaps
921  */
922 GstCaps *
923 gst_caps_normalize (const GstCaps * caps)
924 {
925   NormalizeForeach nf;
926   GstCaps *newcaps;
927   int i;
928
929   g_return_val_if_fail (caps != NULL, NULL);
930
931   newcaps = gst_caps_copy (caps);
932   nf.caps = newcaps;
933
934   for (i = 0; i < newcaps->structs->len; i++) {
935     nf.structure = gst_caps_get_structure (newcaps, i);
936
937     while (!gst_structure_foreach (nf.structure,
938             gst_caps_normalize_foreach, &nf));
939   }
940
941   return newcaps;
942 }
943
944 static gboolean
945 simplify_foreach (GQuark field_id, GValue * value, gpointer user_data)
946 {
947   GstStructure *s2 = (GstStructure *) user_data;
948   const GValue *v2;
949
950   v2 = gst_structure_id_get_value (s2, field_id);
951   if (v2 == NULL)
952     return FALSE;
953
954   if (gst_value_compare (value, v2) == GST_VALUE_EQUAL)
955     return TRUE;
956   return FALSE;
957 }
958
959 static gboolean
960 gst_caps_structure_simplify (GstStructure * struct1,
961     const GstStructure * struct2)
962 {
963   /* FIXME this is just a simple compare.  Better would be to merge
964    * the two structures */
965   if (struct1->name != struct2->name)
966     return FALSE;
967   if (struct1->fields->len != struct2->fields->len)
968     return FALSE;
969
970   return gst_structure_foreach (struct1, simplify_foreach, (void *) struct2);
971 }
972
973 /**
974  * gst_caps_simplify:
975  * @caps: a #GstCaps to simplify
976  *
977  * Creates a new #GstCaps that represents the same set of formats as
978  * @caps, but simpler.  Component structures that are identical are
979  * merged.  Component structures that have ranges or lists that can
980  * be merged are also merged.
981  *
982  * Returns: the new #GstCaps
983  */
984 GstCaps *
985 gst_caps_simplify (const GstCaps * caps)
986 {
987   int i;
988   int j;
989   GstCaps *newcaps;
990   GstStructure *structure;
991   GstStructure *struct2;
992
993   if (gst_caps_get_size (caps) < 2) {
994     return gst_caps_copy (caps);
995   }
996
997   newcaps = gst_caps_new_empty ();
998
999   for (i = 0; i < gst_caps_get_size (caps); i++) {
1000     structure = gst_caps_get_structure (caps, i);
1001
1002     for (j = 0; j < gst_caps_get_size (newcaps); j++) {
1003       struct2 = gst_caps_get_structure (caps, i);
1004       if (gst_caps_structure_simplify (struct2, structure)) {
1005         break;
1006       }
1007     }
1008     if (j == gst_caps_get_size (newcaps)) {
1009       gst_caps_append_structure (newcaps, gst_structure_copy (structure));
1010     }
1011   }
1012
1013   return newcaps;
1014 }
1015
1016 #ifndef GST_DISABLE_LOADSAVE
1017 /**
1018  * gst_caps_save_thyself:
1019  * @caps: a #GstCaps structure
1020  * @parent: a XML parent node
1021  *
1022  * Serializes a #GstCaps to XML and adds it as a child node of @parent.
1023  *
1024  * Returns: a XML node pointer
1025  */
1026 xmlNodePtr
1027 gst_caps_save_thyself (const GstCaps * caps, xmlNodePtr parent)
1028 {
1029
1030   return 0;
1031 }
1032
1033 /**
1034  * gst_caps_load_thyself:
1035  * @parent: a XML node
1036  *
1037  * Creates a #GstCaps from its XML serialization.
1038  *
1039  * Returns: a new #GstCaps structure
1040  */
1041 GstCaps *
1042 gst_caps_load_thyself (xmlNodePtr parent)
1043 {
1044
1045   return NULL;
1046 }
1047 #endif
1048
1049 /* utility */
1050
1051 /**
1052  * gst_caps_replace:
1053  * @caps: a pointer to #GstCaps
1054  * @newcaps: a #GstCaps to replace *caps
1055  *
1056  * Replaces *caps with @newcaps.  Frees the #GstCaps in the location
1057  * pointed to by @caps, if applicable, then modifies @caps to point to
1058  * @newcaps.
1059  */
1060 void
1061 gst_caps_replace (GstCaps ** caps, GstCaps * newcaps)
1062 {
1063 #if 0                           /* disable this, since too many plugins rely on undefined behavior */
1064 #ifdef USE_POISONING
1065   //if (newcaps) CAPS_POISON (newcaps);
1066 #endif
1067 #endif
1068   if (*caps)
1069     gst_caps_free (*caps);
1070   *caps = newcaps;
1071 }
1072
1073 /**
1074  * gst_caps_to_string:
1075  * @caps: a #GstCaps
1076  *
1077  * Converts @caps to a string representation.  This string representation
1078  * can be converted back to a #GstCaps by #gst_caps_from_string().
1079  *
1080  * Returns: a string representing @caps
1081  */
1082 gchar *
1083 gst_caps_to_string (const GstCaps * caps)
1084 {
1085   int i;
1086   GstStructure *structure;
1087   GString *s;
1088   char *sstr;
1089
1090   /* NOTE:  This function is potentially called by the debug system,
1091    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1092    * should be careful to avoid recursion.  This includes any functions
1093    * called by gst_caps_to_string.  In particular, calls should
1094    * not use the GST_PTR_FORMAT extension.  */
1095
1096   /* FIXME does this leak? */
1097
1098   if (caps == NULL) {
1099     return g_strdup ("NULL");
1100   }
1101   if (gst_caps_is_any (caps)) {
1102     return g_strdup ("ANY");
1103   }
1104   if (gst_caps_is_empty (caps)) {
1105     return g_strdup ("EMPTY");
1106   }
1107   s = g_string_new ("");
1108   structure = gst_caps_get_structure (caps, 0);
1109   sstr = gst_structure_to_string (structure);
1110   g_string_append (s, sstr);
1111   g_free (sstr);
1112
1113   for (i = 1; i < caps->structs->len; i++) {
1114     structure = gst_caps_get_structure (caps, i);
1115
1116     g_string_append (s, "; ");
1117     sstr = gst_structure_to_string (structure);
1118     g_string_append (s, sstr);
1119     g_free (sstr);
1120   }
1121
1122   return g_string_free (s, FALSE);
1123 }
1124
1125 static gboolean
1126 gst_caps_from_string_inplace (GstCaps * caps, const gchar * string)
1127 {
1128   GstStructure *structure;
1129   gchar *s;
1130
1131   if (strcmp ("ANY", string) == 0) {
1132     caps->flags = GST_CAPS_FLAGS_ANY;
1133     return TRUE;
1134   }
1135   if (strcmp ("NONE", string) == 0) {
1136     return TRUE;
1137   }
1138
1139   structure = gst_structure_from_string (string, &s);
1140   if (structure == NULL) {
1141     return FALSE;
1142   }
1143   gst_caps_append_structure (caps, structure);
1144
1145   while (*s == ';') {
1146     s++;
1147     while (g_ascii_isspace (*s))
1148       s++;
1149     structure = gst_structure_from_string (s, &s);
1150     if (structure == NULL) {
1151       return FALSE;
1152     }
1153     gst_caps_append_structure (caps, structure);
1154     while (g_ascii_isspace (*s))
1155       s++;
1156   }
1157
1158   if (*s != 0) {
1159     return FALSE;
1160   }
1161
1162   return TRUE;
1163 }
1164
1165 /**
1166  * gst_caps_from_string:
1167  * @caps: a string to convert to #GstCaps
1168  *
1169  * Converts @caps from a string representation.
1170  *
1171  * Returns: a new #GstCaps
1172  */
1173 GstCaps *
1174 gst_caps_from_string (const gchar * string)
1175 {
1176   GstCaps *caps;
1177
1178   caps = gst_caps_new_empty ();
1179   if (gst_caps_from_string_inplace (caps, string)) {
1180     return caps;
1181   } else {
1182     gst_caps_free (caps);
1183     return NULL;
1184   }
1185 }
1186
1187 static void
1188 gst_caps_transform_to_string (const GValue * src_value, GValue * dest_value)
1189 {
1190   g_return_if_fail (src_value != NULL);
1191   g_return_if_fail (dest_value != NULL);
1192
1193   dest_value->data[0].v_pointer =
1194       gst_caps_to_string (src_value->data[0].v_pointer);
1195 }
1196
1197 static GstCaps *
1198 gst_caps_copy_conditional (const GstCaps * src)
1199 {
1200   if (src) {
1201     return gst_caps_copy (src);
1202   } else {
1203     return NULL;
1204   }
1205 }
1206
1207 /* fixate utility functions */
1208
1209 /**
1210  * gst_caps_structure_fixate_field_nearest_int:
1211  * @structure: a #GstStructure
1212  * @field_name: a field in @structure
1213  * @target: the target value of the fixation
1214  *
1215  * Fixates a #GstStructure by changing the given field to the nearest
1216  * integer to @target that is a subset of the existing field.
1217  *
1218  * Returns: TRUE if the structure could be fixated
1219  */
1220 gboolean
1221 gst_caps_structure_fixate_field_nearest_int (GstStructure * structure,
1222     const char *field_name, int target)
1223 {
1224   const GValue *value;
1225
1226   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1227
1228   value = gst_structure_get_value (structure, field_name);
1229
1230   if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1231     /* already fixed */
1232     return FALSE;
1233   } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1234     int x;
1235
1236     x = gst_value_get_int_range_min (value);
1237     if (target < x)
1238       target = x;
1239     x = gst_value_get_int_range_max (value);
1240     if (target > x)
1241       target = x;
1242     gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1243     return TRUE;
1244   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1245     const GValue *list_value;
1246     int i, n;
1247     int best = 0;
1248     int best_index = -1;
1249
1250     n = gst_value_list_get_size (value);
1251     for (i = 0; i < n; i++) {
1252       list_value = gst_value_list_get_value (value, i);
1253       if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1254         int x = g_value_get_int (list_value);
1255
1256         if (best_index == -1 || (ABS (target - x) < ABS (best - x))) {
1257           best_index = i;
1258           best = x;
1259         }
1260       }
1261     }
1262     if (best_index != -1) {
1263       gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1264       return TRUE;
1265     }
1266     return FALSE;
1267   }
1268
1269   return FALSE;
1270 }
1271
1272 /**
1273  * gst_caps_structure_fixate_field_nearest_double:
1274  * @structure: a #GstStructure
1275  * @field_name: a field in @structure
1276  * @target: the target value of the fixation
1277  *
1278  * Fixates a #GstStructure by changing the given field to the nearest
1279  * double to @target that is a subset of the existing field.
1280  *
1281  * Returns: TRUE if the structure could be fixated
1282  */
1283 gboolean
1284 gst_caps_structure_fixate_field_nearest_double (GstStructure * structure,
1285     const char *field_name, double target)
1286 {
1287   const GValue *value;
1288
1289   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1290
1291   value = gst_structure_get_value (structure, field_name);
1292
1293   if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1294     /* already fixed */
1295     return FALSE;
1296   } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1297     double x;
1298
1299     x = gst_value_get_double_range_min (value);
1300     if (target < x)
1301       target = x;
1302     x = gst_value_get_double_range_max (value);
1303     if (target > x)
1304       target = x;
1305     gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1306     return TRUE;
1307   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1308     const GValue *list_value;
1309     int i, n;
1310     double best = 0;
1311     int best_index = -1;
1312
1313     n = gst_value_list_get_size (value);
1314     for (i = 0; i < n; i++) {
1315       list_value = gst_value_list_get_value (value, i);
1316       if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1317         double x = g_value_get_double (list_value);
1318
1319         if (best_index == -1 || (ABS (target - x) < ABS (best - x))) {
1320           best_index = i;
1321           best = x;
1322         }
1323       }
1324     }
1325     if (best_index != -1) {
1326       gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
1327       return TRUE;
1328     }
1329     return FALSE;
1330   }
1331
1332   return FALSE;
1333
1334 }