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