gstvalue: consider lists and ranges equal if they hold the same set
[platform/upstream/gstreamer.git] / gst / gstvalue.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:gstvalue
22  * @short_description: GValue implementations specific
23  * to GStreamer
24  *
25  * GValue implementations specific to GStreamer.
26  *
27  * Note that operations on the same #GValue from multiple threads may lead to
28  * undefined behaviour.
29  *
30  * Last reviewed on 2008-03-11 (0.10.18)
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #include <math.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <ctype.h>
41
42 #include "gst_private.h"
43 #include "glib-compat-private.h"
44 #include <gst/gst.h>
45 #include <gobject/gvaluecollector.h>
46 #include "gstutils.h"
47
48 typedef struct _GstValueUnionInfo GstValueUnionInfo;
49 struct _GstValueUnionInfo
50 {
51   GType type1;
52   GType type2;
53   GstValueUnionFunc func;
54 };
55
56 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
57 struct _GstValueIntersectInfo
58 {
59   GType type1;
60   GType type2;
61   GstValueIntersectFunc func;
62 };
63
64 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
65 struct _GstValueSubtractInfo
66 {
67   GType minuend;
68   GType subtrahend;
69   GstValueSubtractFunc func;
70 };
71
72 #define FUNDAMENTAL_TYPE_ID_MAX \
73     (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
74 #define FUNDAMENTAL_TYPE_ID(type) \
75     ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
76
77 #define VALUE_LIST_SIZE(v) (((GArray *) (v)->data[0].v_pointer)->len)
78 #define VALUE_LIST_GET_VALUE(v, index) ((const GValue *) &g_array_index ((GArray *) (v)->data[0].v_pointer, GValue, (index)))
79
80 static GArray *gst_value_table;
81 static GHashTable *gst_value_hash;
82 static GstValueTable *gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID_MAX + 1];
83 static GArray *gst_value_union_funcs;
84 static GArray *gst_value_intersect_funcs;
85 static GArray *gst_value_subtract_funcs;
86
87 /* Forward declarations */
88 static gchar *gst_value_serialize_fraction (const GValue * value);
89
90 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
91 static gint gst_value_compare_with_func (const GValue * value1,
92     const GValue * value2, GstValueCompareFunc compare);
93
94 static gchar *gst_string_wrap (const gchar * s);
95 static gchar *gst_string_take_and_wrap (gchar * s);
96 static gchar *gst_string_unwrap (const gchar * s);
97
98 static inline GstValueTable *
99 gst_value_hash_lookup_type (GType type)
100 {
101   if (G_LIKELY (G_TYPE_IS_FUNDAMENTAL (type)))
102     return gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)];
103   else
104     return g_hash_table_lookup (gst_value_hash, (gpointer) type);
105 }
106
107 static void
108 gst_value_hash_add_type (GType type, const GstValueTable * table)
109 {
110   if (G_TYPE_IS_FUNDAMENTAL (type))
111     gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)] = (gpointer) table;
112
113   g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table);
114 }
115
116 /********
117  * list *
118  ********/
119
120 /* two helper functions to serialize/stringify any type of list
121  * regular lists are done with { }, arrays with < >
122  */
123 static gchar *
124 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
125     const gchar * end)
126 {
127   guint i;
128   GArray *array = value->data[0].v_pointer;
129   GString *s;
130   GValue *v;
131   gchar *s_val;
132   guint alen = array->len;
133
134   /* estimate minimum string length to minimise re-allocs in GString */
135   s = g_string_sized_new (2 + (6 * alen) + 2);
136   g_string_append (s, begin);
137   for (i = 0; i < alen; i++) {
138     v = &g_array_index (array, GValue, i);
139     s_val = gst_value_serialize (v);
140     g_string_append (s, s_val);
141     g_free (s_val);
142     if (i < alen - 1) {
143       g_string_append_len (s, ", ", 2);
144     }
145   }
146   g_string_append (s, end);
147   return g_string_free (s, FALSE);
148 }
149
150 static void
151 gst_value_transform_any_list_string (const GValue * src_value,
152     GValue * dest_value, const gchar * begin, const gchar * end)
153 {
154   GValue *list_value;
155   GArray *array;
156   GString *s;
157   guint i;
158   gchar *list_s;
159   guint alen;
160
161   array = src_value->data[0].v_pointer;
162   alen = array->len;
163
164   /* estimate minimum string length to minimise re-allocs in GString */
165   s = g_string_sized_new (2 + (10 * alen) + 2);
166   g_string_append (s, begin);
167   for (i = 0; i < alen; i++) {
168     list_value = &g_array_index (array, GValue, i);
169
170     if (i != 0) {
171       g_string_append_len (s, ", ", 2);
172     }
173     list_s = g_strdup_value_contents (list_value);
174     g_string_append (s, list_s);
175     g_free (list_s);
176   }
177   g_string_append (s, end);
178
179   dest_value->data[0].v_pointer = g_string_free (s, FALSE);
180 }
181
182 /*
183  * helper function to see if a type is fixed. Is used internally here and
184  * there. Do not export, since it doesn't work for types where the content
185  * decides the fixedness (e.g. GST_TYPE_ARRAY).
186  */
187 static gboolean
188 gst_type_is_fixed (GType type)
189 {
190   /* the basic int, string, double types */
191   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
192     return TRUE;
193   }
194   /* our fundamental types that are certainly not fixed */
195   if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
196       type == GST_TYPE_INT64_RANGE ||
197       type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
198     return FALSE;
199   }
200   /* other (boxed) types that are fixed */
201   if (type == GST_TYPE_BUFFER) {
202     return TRUE;
203   }
204   /* heavy checks */
205   if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
206       G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
207     return TRUE;
208   }
209
210   return FALSE;
211 }
212
213 /* GValue functions usable for both regular lists and arrays */
214 static void
215 gst_value_init_list_or_array (GValue * value)
216 {
217   value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
218 }
219
220 static GArray *
221 copy_garray_of_gstvalue (const GArray * src)
222 {
223   GArray *dest;
224   guint i, len;
225
226   len = src->len;
227   dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), len);
228   g_array_set_size (dest, len);
229   for (i = 0; i < len; i++) {
230     gst_value_init_and_copy (&g_array_index (dest, GValue, i),
231         &g_array_index (src, GValue, i));
232   }
233
234   return dest;
235 }
236
237 static void
238 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
239 {
240   dest_value->data[0].v_pointer =
241       copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
242 }
243
244 static void
245 gst_value_free_list_or_array (GValue * value)
246 {
247   guint i, len;
248   GArray *src = (GArray *) value->data[0].v_pointer;
249   len = src->len;
250
251   if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
252     for (i = 0; i < len; i++) {
253       g_value_unset (&g_array_index (src, GValue, i));
254     }
255     g_array_free (src, TRUE);
256   }
257 }
258
259 static gpointer
260 gst_value_list_or_array_peek_pointer (const GValue * value)
261 {
262   return value->data[0].v_pointer;
263 }
264
265 static gchar *
266 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
267     GTypeCValue * collect_values, guint collect_flags)
268 {
269   if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
270     value->data[0].v_pointer = collect_values[0].v_pointer;
271     value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
272   } else {
273     value->data[0].v_pointer =
274         copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
275   }
276   return NULL;
277 }
278
279 static gchar *
280 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
281     GTypeCValue * collect_values, guint collect_flags)
282 {
283   GArray **dest = collect_values[0].v_pointer;
284
285   if (!dest)
286     return g_strdup_printf ("value location for `%s' passed as NULL",
287         G_VALUE_TYPE_NAME (value));
288   if (!value->data[0].v_pointer)
289     return g_strdup_printf ("invalid value given for `%s'",
290         G_VALUE_TYPE_NAME (value));
291   if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
292     *dest = (GArray *) value->data[0].v_pointer;
293   } else {
294     *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
295   }
296   return NULL;
297 }
298
299 /**
300  * gst_value_list_append_value:
301  * @value: a #GValue of type #GST_TYPE_LIST
302  * @append_value: the value to append
303  *
304  * Appends @append_value to the GstValueList in @value.
305  */
306 void
307 gst_value_list_append_value (GValue * value, const GValue * append_value)
308 {
309   GValue val = { 0, };
310
311   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
312   g_return_if_fail (G_IS_VALUE (append_value));
313
314   gst_value_init_and_copy (&val, append_value);
315   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
316 }
317
318 /**
319  * gst_value_list_prepend_value:
320  * @value: a #GValue of type #GST_TYPE_LIST
321  * @prepend_value: the value to prepend
322  *
323  * Prepends @prepend_value to the GstValueList in @value.
324  */
325 void
326 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
327 {
328   GValue val = { 0, };
329
330   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
331   g_return_if_fail (G_IS_VALUE (prepend_value));
332
333   gst_value_init_and_copy (&val, prepend_value);
334   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
335 }
336
337 /**
338  * gst_value_list_concat:
339  * @dest: (out caller-allocates): an uninitialized #GValue to take the result
340  * @value1: a #GValue
341  * @value2: a #GValue
342  *
343  * Concatenates copies of @value1 and @value2 into a list.  Values that are not
344  * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
345  * @dest will be initialized to the type #GST_TYPE_LIST.
346  */
347 void
348 gst_value_list_concat (GValue * dest, const GValue * value1,
349     const GValue * value2)
350 {
351   guint i, value1_length, value2_length;
352   GArray *array;
353
354   g_return_if_fail (dest != NULL);
355   g_return_if_fail (G_VALUE_TYPE (dest) == 0);
356   g_return_if_fail (G_IS_VALUE (value1));
357   g_return_if_fail (G_IS_VALUE (value2));
358
359   value1_length =
360       (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
361   value2_length =
362       (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
363   g_value_init (dest, GST_TYPE_LIST);
364   array = (GArray *) dest->data[0].v_pointer;
365   g_array_set_size (array, value1_length + value2_length);
366
367   if (GST_VALUE_HOLDS_LIST (value1)) {
368     for (i = 0; i < value1_length; i++) {
369       gst_value_init_and_copy (&g_array_index (array, GValue, i),
370           VALUE_LIST_GET_VALUE (value1, i));
371     }
372   } else {
373     gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
374   }
375
376   if (GST_VALUE_HOLDS_LIST (value2)) {
377     for (i = 0; i < value2_length; i++) {
378       gst_value_init_and_copy (&g_array_index (array, GValue,
379               i + value1_length), VALUE_LIST_GET_VALUE (value2, i));
380     }
381   } else {
382     gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
383         value2);
384   }
385 }
386
387 /**
388  * gst_value_list_merge:
389  * @dest: (out caller-allocates): an uninitialized #GValue to take the result
390  * @value1: a #GValue
391  * @value2: a #GValue
392  *
393  * Merges copies of @value1 and @value2.  Values that are not
394  * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
395  *
396  * The result will be put into @dest and will either be a list that will not
397  * contain any duplicates, or a non-list type (if @value1 and @value2
398  * were equal).
399  *
400  * Since: 0.10.32
401  */
402 void
403 gst_value_list_merge (GValue * dest, const GValue * value1,
404     const GValue * value2)
405 {
406   guint i, j, k, value1_length, value2_length, skipped;
407   const GValue *src;
408   gboolean skip;
409   GArray *array;
410
411   g_return_if_fail (dest != NULL);
412   g_return_if_fail (G_VALUE_TYPE (dest) == 0);
413   g_return_if_fail (G_IS_VALUE (value1));
414   g_return_if_fail (G_IS_VALUE (value2));
415
416   value1_length =
417       (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
418   value2_length =
419       (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
420   g_value_init (dest, GST_TYPE_LIST);
421   array = (GArray *) dest->data[0].v_pointer;
422   g_array_set_size (array, value1_length + value2_length);
423
424   if (GST_VALUE_HOLDS_LIST (value1)) {
425     for (i = 0; i < value1_length; i++) {
426       gst_value_init_and_copy (&g_array_index (array, GValue, i),
427           VALUE_LIST_GET_VALUE (value1, i));
428     }
429   } else {
430     gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
431   }
432
433   j = value1_length;
434   skipped = 0;
435   if (GST_VALUE_HOLDS_LIST (value2)) {
436     for (i = 0; i < value2_length; i++) {
437       skip = FALSE;
438       src = VALUE_LIST_GET_VALUE (value2, i);
439       for (k = 0; k < value1_length; k++) {
440         if (gst_value_compare (&g_array_index (array, GValue, k),
441                 src) == GST_VALUE_EQUAL) {
442           skip = TRUE;
443           skipped++;
444           break;
445         }
446       }
447       if (!skip) {
448         gst_value_init_and_copy (&g_array_index (array, GValue, j), src);
449         j++;
450       }
451     }
452   } else {
453     skip = FALSE;
454     for (k = 0; k < value1_length; k++) {
455       if (gst_value_compare (&g_array_index (array, GValue, k),
456               value2) == GST_VALUE_EQUAL) {
457         skip = TRUE;
458         skipped++;
459         break;
460       }
461     }
462     if (!skip) {
463       gst_value_init_and_copy (&g_array_index (array, GValue, j), value2);
464     }
465   }
466   if (skipped) {
467     guint new_size = value1_length + (value2_length - skipped);
468
469     if (new_size > 1) {
470       /* shrink list */
471       g_array_set_size (array, new_size);
472     } else {
473       GValue single_dest;
474
475       /* size is 1, take single value in list and make it new dest */
476       single_dest = g_array_index (array, GValue, 0);
477
478       /* clean up old value allocations: must set array size to 0, because
479        * allocated values are not inited meaning g_value_unset() will not
480        * work on them */
481       g_array_set_size (array, 0);
482       g_value_unset (dest);
483
484       /* the single value is our new result */
485       *dest = single_dest;
486     }
487   }
488 }
489
490 /**
491  * gst_value_list_get_size:
492  * @value: a #GValue of type #GST_TYPE_LIST
493  *
494  * Gets the number of values contained in @value.
495  *
496  * Returns: the number of values
497  */
498 guint
499 gst_value_list_get_size (const GValue * value)
500 {
501   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
502
503   return ((GArray *) value->data[0].v_pointer)->len;
504 }
505
506 /**
507  * gst_value_list_get_value:
508  * @value: a #GValue of type #GST_TYPE_LIST
509  * @index: index of value to get from the list
510  *
511  * Gets the value that is a member of the list contained in @value and
512  * has the index @index.
513  *
514  * Returns: (transfer none): the value at the given index
515  */
516 const GValue *
517 gst_value_list_get_value (const GValue * value, guint index)
518 {
519   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
520   g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
521
522   return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
523       GValue, index);
524 }
525
526 /**
527  * gst_value_array_append_value:
528  * @value: a #GValue of type #GST_TYPE_ARRAY
529  * @append_value: the value to append
530  *
531  * Appends @append_value to the GstValueArray in @value.
532  */
533 void
534 gst_value_array_append_value (GValue * value, const GValue * append_value)
535 {
536   GValue val = { 0, };
537
538   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
539   g_return_if_fail (G_IS_VALUE (append_value));
540
541   gst_value_init_and_copy (&val, append_value);
542   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
543 }
544
545 /**
546  * gst_value_array_prepend_value:
547  * @value: a #GValue of type #GST_TYPE_ARRAY
548  * @prepend_value: the value to prepend
549  *
550  * Prepends @prepend_value to the GstValueArray in @value.
551  */
552 void
553 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
554 {
555   GValue val = { 0, };
556
557   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
558   g_return_if_fail (G_IS_VALUE (prepend_value));
559
560   gst_value_init_and_copy (&val, prepend_value);
561   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
562 }
563
564 /**
565  * gst_value_array_get_size:
566  * @value: a #GValue of type #GST_TYPE_ARRAY
567  *
568  * Gets the number of values contained in @value.
569  *
570  * Returns: the number of values
571  */
572 guint
573 gst_value_array_get_size (const GValue * value)
574 {
575   g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
576
577   return ((GArray *) value->data[0].v_pointer)->len;
578 }
579
580 /**
581  * gst_value_array_get_value:
582  * @value: a #GValue of type #GST_TYPE_ARRAY
583  * @index: index of value to get from the array
584  *
585  * Gets the value that is a member of the array contained in @value and
586  * has the index @index.
587  *
588  * Returns: (transfer none): the value at the given index
589  */
590 const GValue *
591 gst_value_array_get_value (const GValue * value, guint index)
592 {
593   g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
594   g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
595
596   return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
597       GValue, index);
598 }
599
600 static void
601 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
602 {
603   gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
604 }
605
606 static void
607 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
608 {
609   gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
610 }
611
612 /* Do an unordered compare of the contents of a list */
613 static gint
614 gst_value_compare_list (const GValue * value1, const GValue * value2)
615 {
616   guint i, j;
617   GArray *array1 = value1->data[0].v_pointer;
618   GArray *array2 = value2->data[0].v_pointer;
619   GValue *v1;
620   GValue *v2;
621   gint len, to_remove;
622   guint8 *removed;
623   GstValueCompareFunc compare;
624
625   /* get length and do initial length check. */
626   len = array1->len;
627   if (len != array2->len)
628     return GST_VALUE_UNORDERED;
629
630   /* place to mark removed value indices of array2 */
631   removed = g_newa (guint8, len);
632   memset (removed, 0, len);
633   to_remove = len;
634
635   /* loop over array1, all items should be in array2. When we find an
636    * item in array2, remove it from array2 by marking it as removed */
637   for (i = 0; i < len; i++) {
638     v1 = &g_array_index (array1, GValue, i);
639     if ((compare = gst_value_get_compare_func (v1))) {
640       for (j = 0; j < len; j++) {
641         /* item is removed, we can skip it */
642         if (removed[j])
643           continue;
644         v2 = &g_array_index (array2, GValue, j);
645         if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
646           /* mark item as removed now that we found it in array2 and 
647            * decrement the number of remaining items in array2. */
648           removed[j] = 1;
649           to_remove--;
650           break;
651         }
652       }
653       /* item in array1 and not in array2, UNORDERED */
654       if (j == len)
655         return GST_VALUE_UNORDERED;
656     } else
657       return GST_VALUE_UNORDERED;
658   }
659   /* if not all items were removed, array2 contained something not in array1 */
660   if (to_remove != 0)
661     return GST_VALUE_UNORDERED;
662
663   /* arrays are equal */
664   return GST_VALUE_EQUAL;
665 }
666
667 /* Perform an ordered comparison of the contents of an array */
668 static gint
669 gst_value_compare_array (const GValue * value1, const GValue * value2)
670 {
671   guint i;
672   GArray *array1 = value1->data[0].v_pointer;
673   GArray *array2 = value2->data[0].v_pointer;
674   guint len = array1->len;
675   GValue *v1;
676   GValue *v2;
677
678   if (len != array2->len)
679     return GST_VALUE_UNORDERED;
680
681   for (i = 0; i < len; i++) {
682     v1 = &g_array_index (array1, GValue, i);
683     v2 = &g_array_index (array2, GValue, i);
684     if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
685       return GST_VALUE_UNORDERED;
686   }
687
688   return GST_VALUE_EQUAL;
689 }
690
691 static gchar *
692 gst_value_serialize_list (const GValue * value)
693 {
694   return gst_value_serialize_any_list (value, "{ ", " }");
695 }
696
697 static gboolean
698 gst_value_deserialize_list (GValue * dest, const gchar * s)
699 {
700   g_warning ("gst_value_deserialize_list: unimplemented");
701   return FALSE;
702 }
703
704 static gchar *
705 gst_value_serialize_array (const GValue * value)
706 {
707   return gst_value_serialize_any_list (value, "< ", " >");
708 }
709
710 static gboolean
711 gst_value_deserialize_array (GValue * dest, const gchar * s)
712 {
713   g_warning ("gst_value_deserialize_array: unimplemented");
714   return FALSE;
715 }
716
717 /**********
718  * fourcc *
719  **********/
720
721 static void
722 gst_value_init_fourcc (GValue * value)
723 {
724   value->data[0].v_int = 0;
725 }
726
727 static void
728 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
729 {
730   dest_value->data[0].v_int = src_value->data[0].v_int;
731 }
732
733 static gchar *
734 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
735     GTypeCValue * collect_values, guint collect_flags)
736 {
737   value->data[0].v_int = collect_values[0].v_int;
738
739   return NULL;
740 }
741
742 static gchar *
743 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
744     GTypeCValue * collect_values, guint collect_flags)
745 {
746   guint32 *fourcc_p = collect_values[0].v_pointer;
747
748   if (!fourcc_p)
749     return g_strdup_printf ("value location for `%s' passed as NULL",
750         G_VALUE_TYPE_NAME (value));
751
752   *fourcc_p = value->data[0].v_int;
753
754   return NULL;
755 }
756
757 /**
758  * gst_value_set_fourcc:
759  * @value: a GValue initialized to #GST_TYPE_FOURCC
760  * @fourcc: the #guint32 fourcc to set
761  *
762  * Sets @value to @fourcc.
763  */
764 void
765 gst_value_set_fourcc (GValue * value, guint32 fourcc)
766 {
767   g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
768
769   value->data[0].v_int = fourcc;
770 }
771
772 /**
773  * gst_value_get_fourcc:
774  * @value: a GValue initialized to #GST_TYPE_FOURCC
775  *
776  * Gets the #guint32 fourcc contained in @value.
777  *
778  * Returns: the #guint32 fourcc contained in @value.
779  */
780 guint32
781 gst_value_get_fourcc (const GValue * value)
782 {
783   g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
784
785   return value->data[0].v_int;
786 }
787
788 static void
789 gst_value_transform_fourcc_string (const GValue * src_value,
790     GValue * dest_value)
791 {
792   guint32 fourcc = src_value->data[0].v_int;
793   gchar fourcc_char[4];
794
795   fourcc_char[0] = (fourcc >> 0) & 0xff;
796   fourcc_char[1] = (fourcc >> 8) & 0xff;
797   fourcc_char[2] = (fourcc >> 16) & 0xff;
798   fourcc_char[3] = (fourcc >> 24) & 0xff;
799
800   if ((g_ascii_isalnum (fourcc_char[0]) || fourcc_char[0] == ' ') &&
801       (g_ascii_isalnum (fourcc_char[1]) || fourcc_char[1] == ' ') &&
802       (g_ascii_isalnum (fourcc_char[2]) || fourcc_char[2] == ' ') &&
803       (g_ascii_isalnum (fourcc_char[3]) || fourcc_char[3] == ' ')) {
804     dest_value->data[0].v_pointer =
805         g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
806   } else {
807     dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
808   }
809 }
810
811 static gint
812 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
813 {
814   if (value2->data[0].v_int == value1->data[0].v_int)
815     return GST_VALUE_EQUAL;
816   return GST_VALUE_UNORDERED;
817 }
818
819 static gchar *
820 gst_value_serialize_fourcc (const GValue * value)
821 {
822   guint32 fourcc = value->data[0].v_int;
823   gchar fourcc_char[4];
824
825   fourcc_char[0] = (fourcc >> 0) & 0xff;
826   fourcc_char[1] = (fourcc >> 8) & 0xff;
827   fourcc_char[2] = (fourcc >> 16) & 0xff;
828   fourcc_char[3] = (fourcc >> 24) & 0xff;
829
830   if ((g_ascii_isalnum (fourcc_char[0]) || fourcc_char[0] == ' ') &&
831       (g_ascii_isalnum (fourcc_char[1]) || fourcc_char[1] == ' ') &&
832       (g_ascii_isalnum (fourcc_char[2]) || fourcc_char[2] == ' ') &&
833       (g_ascii_isalnum (fourcc_char[3]) || fourcc_char[3] == ' ')) {
834     return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
835   } else {
836     return g_strdup_printf ("0x%08x", fourcc);
837   }
838 }
839
840 static gboolean
841 gst_value_deserialize_fourcc (GValue * dest, const gchar * s)
842 {
843   gboolean ret = FALSE;
844   guint32 fourcc = 0;
845   gchar *end;
846   gint l = strlen (s);
847
848   if (l == 4) {
849     fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
850     ret = TRUE;
851   } else if (l == 3) {
852     fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], ' ');
853     ret = TRUE;
854   } else if (l == 2) {
855     fourcc = GST_MAKE_FOURCC (s[0], s[1], ' ', ' ');
856     ret = TRUE;
857   } else if (l == 1) {
858     fourcc = GST_MAKE_FOURCC (s[0], ' ', ' ', ' ');
859     ret = TRUE;
860   } else if (g_ascii_isdigit (*s)) {
861     fourcc = strtoul (s, &end, 0);
862     if (*end == 0) {
863       ret = TRUE;
864     }
865   }
866   gst_value_set_fourcc (dest, fourcc);
867
868   return ret;
869 }
870
871 /*************
872  * int range *
873  *************/
874
875 static void
876 gst_value_init_int_range (GValue * value)
877 {
878   value->data[0].v_int = 0;
879   value->data[1].v_int = 0;
880 }
881
882 static void
883 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
884 {
885   dest_value->data[0].v_int = src_value->data[0].v_int;
886   dest_value->data[1].v_int = src_value->data[1].v_int;
887 }
888
889 static gchar *
890 gst_value_collect_int_range (GValue * value, guint n_collect_values,
891     GTypeCValue * collect_values, guint collect_flags)
892 {
893   if (n_collect_values != 2)
894     return g_strdup_printf ("not enough value locations for `%s' passed",
895         G_VALUE_TYPE_NAME (value));
896   if (collect_values[0].v_int >= collect_values[1].v_int)
897     return g_strdup_printf ("range start is not smaller than end for `%s'",
898         G_VALUE_TYPE_NAME (value));
899
900   value->data[0].v_int = collect_values[0].v_int;
901   value->data[1].v_int = collect_values[1].v_int;
902
903   return NULL;
904 }
905
906 static gchar *
907 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
908     GTypeCValue * collect_values, guint collect_flags)
909 {
910   guint32 *int_range_start = collect_values[0].v_pointer;
911   guint32 *int_range_end = collect_values[1].v_pointer;
912
913   if (!int_range_start)
914     return g_strdup_printf ("start value location for `%s' passed as NULL",
915         G_VALUE_TYPE_NAME (value));
916   if (!int_range_end)
917     return g_strdup_printf ("end value location for `%s' passed as NULL",
918         G_VALUE_TYPE_NAME (value));
919
920   *int_range_start = value->data[0].v_int;
921   *int_range_end = value->data[1].v_int;
922
923   return NULL;
924 }
925
926 /**
927  * gst_value_set_int_range:
928  * @value: a GValue initialized to GST_TYPE_INT_RANGE
929  * @start: the start of the range
930  * @end: the end of the range
931  *
932  * Sets @value to the range specified by @start and @end.
933  */
934 void
935 gst_value_set_int_range (GValue * value, gint start, gint end)
936 {
937   g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
938   g_return_if_fail (start < end);
939
940   value->data[0].v_int = start;
941   value->data[1].v_int = end;
942 }
943
944 /**
945  * gst_value_get_int_range_min:
946  * @value: a GValue initialized to GST_TYPE_INT_RANGE
947  *
948  * Gets the minimum of the range specified by @value.
949  *
950  * Returns: the minimum of the range
951  */
952 gint
953 gst_value_get_int_range_min (const GValue * value)
954 {
955   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
956
957   return value->data[0].v_int;
958 }
959
960 /**
961  * gst_value_get_int_range_max:
962  * @value: a GValue initialized to GST_TYPE_INT_RANGE
963  *
964  * Gets the maximum of the range specified by @value.
965  *
966  * Returns: the maxumum of the range
967  */
968 gint
969 gst_value_get_int_range_max (const GValue * value)
970 {
971   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
972
973   return value->data[1].v_int;
974 }
975
976 static void
977 gst_value_transform_int_range_string (const GValue * src_value,
978     GValue * dest_value)
979 {
980   dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
981       (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
982 }
983
984 static gint
985 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
986 {
987   if (value2->data[0].v_int == value1->data[0].v_int &&
988       value2->data[1].v_int == value1->data[1].v_int)
989     return GST_VALUE_EQUAL;
990   return GST_VALUE_UNORDERED;
991 }
992
993 static gchar *
994 gst_value_serialize_int_range (const GValue * value)
995 {
996   return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
997       value->data[1].v_int);
998 }
999
1000 static gboolean
1001 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1002 {
1003   g_warning ("unimplemented");
1004   return FALSE;
1005 }
1006
1007 /***************
1008  * int64 range *
1009  ***************/
1010
1011 static void
1012 gst_value_init_int64_range (GValue * value)
1013 {
1014   value->data[0].v_int64 = 0;
1015   value->data[1].v_int64 = 0;
1016 }
1017
1018 static void
1019 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1020 {
1021   dest_value->data[0].v_int64 = src_value->data[0].v_int64;
1022   dest_value->data[1].v_int64 = src_value->data[1].v_int64;
1023 }
1024
1025 static gchar *
1026 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1027     GTypeCValue * collect_values, guint collect_flags)
1028 {
1029   if (n_collect_values != 2)
1030     return g_strdup_printf ("not enough value locations for `%s' passed",
1031         G_VALUE_TYPE_NAME (value));
1032   if (collect_values[0].v_int64 >= collect_values[1].v_int64)
1033     return g_strdup_printf ("range start is not smaller than end for `%s'",
1034         G_VALUE_TYPE_NAME (value));
1035
1036   value->data[0].v_int64 = collect_values[0].v_int64;
1037   value->data[1].v_int64 = collect_values[1].v_int64;
1038
1039   return NULL;
1040 }
1041
1042 static gchar *
1043 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1044     GTypeCValue * collect_values, guint collect_flags)
1045 {
1046   guint64 *int_range_start = collect_values[0].v_pointer;
1047   guint64 *int_range_end = collect_values[1].v_pointer;
1048
1049   if (!int_range_start)
1050     return g_strdup_printf ("start value location for `%s' passed as NULL",
1051         G_VALUE_TYPE_NAME (value));
1052   if (!int_range_end)
1053     return g_strdup_printf ("end value location for `%s' passed as NULL",
1054         G_VALUE_TYPE_NAME (value));
1055
1056   *int_range_start = value->data[0].v_int64;
1057   *int_range_end = value->data[1].v_int64;
1058
1059   return NULL;
1060 }
1061
1062 /**
1063  * gst_value_set_int64_range:
1064  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1065  * @start: the start of the range
1066  * @end: the end of the range
1067  *
1068  * Sets @value to the range specified by @start and @end.
1069  *
1070  * Since: 0.10.31
1071  */
1072 void
1073 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1074 {
1075   g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1076   g_return_if_fail (start < end);
1077
1078   value->data[0].v_int64 = start;
1079   value->data[1].v_int64 = end;
1080 }
1081
1082 /**
1083  * gst_value_get_int64_range_min:
1084  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1085  *
1086  * Gets the minimum of the range specified by @value.
1087  *
1088  * Returns: the minimum of the range
1089  *
1090  * Since: 0.10.31
1091  */
1092 gint64
1093 gst_value_get_int64_range_min (const GValue * value)
1094 {
1095   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1096
1097   return value->data[0].v_int64;
1098 }
1099
1100 /**
1101  * gst_value_get_int64_range_max:
1102  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1103  *
1104  * Gets the maximum of the range specified by @value.
1105  *
1106  * Returns: the maxumum of the range
1107  *
1108  * Since: 0.10.31
1109  */
1110 gint64
1111 gst_value_get_int64_range_max (const GValue * value)
1112 {
1113   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1114
1115   return value->data[1].v_int64;
1116 }
1117
1118 static void
1119 gst_value_transform_int64_range_string (const GValue * src_value,
1120     GValue * dest_value)
1121 {
1122   dest_value->data[0].v_pointer =
1123       g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1124       src_value->data[0].v_int64, src_value->data[1].v_int64);
1125 }
1126
1127 static gint
1128 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1129 {
1130   if (value2->data[0].v_int64 == value1->data[0].v_int64 &&
1131       value2->data[1].v_int64 == value1->data[1].v_int64)
1132     return GST_VALUE_EQUAL;
1133   return GST_VALUE_UNORDERED;
1134 }
1135
1136 static gchar *
1137 gst_value_serialize_int64_range (const GValue * value)
1138 {
1139   return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1140       value->data[0].v_int64, value->data[1].v_int64);
1141 }
1142
1143 static gboolean
1144 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1145 {
1146   g_warning ("unimplemented");
1147   return FALSE;
1148 }
1149
1150 /****************
1151  * double range *
1152  ****************/
1153
1154 static void
1155 gst_value_init_double_range (GValue * value)
1156 {
1157   value->data[0].v_double = 0;
1158   value->data[1].v_double = 0;
1159 }
1160
1161 static void
1162 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1163 {
1164   dest_value->data[0].v_double = src_value->data[0].v_double;
1165   dest_value->data[1].v_double = src_value->data[1].v_double;
1166 }
1167
1168 static gchar *
1169 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1170     GTypeCValue * collect_values, guint collect_flags)
1171 {
1172   if (n_collect_values != 2)
1173     return g_strdup_printf ("not enough value locations for `%s' passed",
1174         G_VALUE_TYPE_NAME (value));
1175   if (collect_values[0].v_double >= collect_values[1].v_double)
1176     return g_strdup_printf ("range start is not smaller than end for `%s'",
1177         G_VALUE_TYPE_NAME (value));
1178
1179   value->data[0].v_double = collect_values[0].v_double;
1180   value->data[1].v_double = collect_values[1].v_double;
1181
1182   return NULL;
1183 }
1184
1185 static gchar *
1186 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1187     GTypeCValue * collect_values, guint collect_flags)
1188 {
1189   gdouble *double_range_start = collect_values[0].v_pointer;
1190   gdouble *double_range_end = collect_values[1].v_pointer;
1191
1192   if (!double_range_start)
1193     return g_strdup_printf ("start value location for `%s' passed as NULL",
1194         G_VALUE_TYPE_NAME (value));
1195   if (!double_range_end)
1196     return g_strdup_printf ("end value location for `%s' passed as NULL",
1197         G_VALUE_TYPE_NAME (value));
1198
1199   *double_range_start = value->data[0].v_double;
1200   *double_range_end = value->data[1].v_double;
1201
1202   return NULL;
1203 }
1204
1205 /**
1206  * gst_value_set_double_range:
1207  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1208  * @start: the start of the range
1209  * @end: the end of the range
1210  *
1211  * Sets @value to the range specified by @start and @end.
1212  */
1213 void
1214 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1215 {
1216   g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1217   g_return_if_fail (start < end);
1218
1219   value->data[0].v_double = start;
1220   value->data[1].v_double = end;
1221 }
1222
1223 /**
1224  * gst_value_get_double_range_min:
1225  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1226  *
1227  * Gets the minimum of the range specified by @value.
1228  *
1229  * Returns: the minimum of the range
1230  */
1231 gdouble
1232 gst_value_get_double_range_min (const GValue * value)
1233 {
1234   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1235
1236   return value->data[0].v_double;
1237 }
1238
1239 /**
1240  * gst_value_get_double_range_max:
1241  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1242  *
1243  * Gets the maximum of the range specified by @value.
1244  *
1245  * Returns: the maxumum of the range
1246  */
1247 gdouble
1248 gst_value_get_double_range_max (const GValue * value)
1249 {
1250   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1251
1252   return value->data[1].v_double;
1253 }
1254
1255 static void
1256 gst_value_transform_double_range_string (const GValue * src_value,
1257     GValue * dest_value)
1258 {
1259   gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1260
1261   dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1262       g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1263           src_value->data[0].v_double),
1264       g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1265           src_value->data[1].v_double));
1266 }
1267
1268 static gint
1269 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1270 {
1271   if (value2->data[0].v_double == value1->data[0].v_double &&
1272       value2->data[0].v_double == value1->data[0].v_double)
1273     return GST_VALUE_EQUAL;
1274   return GST_VALUE_UNORDERED;
1275 }
1276
1277 static gchar *
1278 gst_value_serialize_double_range (const GValue * value)
1279 {
1280   gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1281   gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1282
1283   g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1284   g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1285   return g_strdup_printf ("[ %s, %s ]", d1, d2);
1286 }
1287
1288 static gboolean
1289 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1290 {
1291   g_warning ("unimplemented");
1292   return FALSE;
1293 }
1294
1295 /****************
1296  * fraction range *
1297  ****************/
1298
1299 static void
1300 gst_value_init_fraction_range (GValue * value)
1301 {
1302   GValue *vals;
1303   GType ftype;
1304
1305   ftype = GST_TYPE_FRACTION;
1306
1307   value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1308   g_value_init (&vals[0], ftype);
1309   g_value_init (&vals[1], ftype);
1310 }
1311
1312 static void
1313 gst_value_free_fraction_range (GValue * value)
1314 {
1315   GValue *vals = (GValue *) value->data[0].v_pointer;
1316
1317   if (vals != NULL) {
1318     g_value_unset (&vals[0]);
1319     g_value_unset (&vals[1]);
1320     g_slice_free1 (2 * sizeof (GValue), vals);
1321     value->data[0].v_pointer = NULL;
1322   }
1323 }
1324
1325 static void
1326 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1327 {
1328   GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1329   GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1330
1331   if (vals == NULL) {
1332     gst_value_init_fraction_range (dest_value);
1333     vals = dest_value->data[0].v_pointer;
1334   }
1335   if (src_vals != NULL) {
1336     g_value_copy (&src_vals[0], &vals[0]);
1337     g_value_copy (&src_vals[1], &vals[1]);
1338   }
1339 }
1340
1341 static gchar *
1342 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1343     GTypeCValue * collect_values, guint collect_flags)
1344 {
1345   GValue *vals = (GValue *) value->data[0].v_pointer;
1346
1347   if (n_collect_values != 4)
1348     return g_strdup_printf ("not enough value locations for `%s' passed",
1349         G_VALUE_TYPE_NAME (value));
1350   if (collect_values[1].v_int == 0)
1351     return g_strdup_printf ("passed '0' as first denominator for `%s'",
1352         G_VALUE_TYPE_NAME (value));
1353   if (collect_values[3].v_int == 0)
1354     return g_strdup_printf ("passed '0' as second denominator for `%s'",
1355         G_VALUE_TYPE_NAME (value));
1356   if (gst_util_fraction_compare (collect_values[0].v_int,
1357           collect_values[1].v_int, collect_values[2].v_int,
1358           collect_values[3].v_int) >= 0)
1359     return g_strdup_printf ("range start is not smaller than end for `%s'",
1360         G_VALUE_TYPE_NAME (value));
1361
1362   if (vals == NULL) {
1363     gst_value_init_fraction_range (value);
1364     vals = value->data[0].v_pointer;
1365   }
1366
1367   gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1368       collect_values[1].v_int);
1369   gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1370       collect_values[3].v_int);
1371
1372   return NULL;
1373 }
1374
1375 static gchar *
1376 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1377     GTypeCValue * collect_values, guint collect_flags)
1378 {
1379   gint i;
1380   gint *dest_values[4];
1381   GValue *vals = (GValue *) value->data[0].v_pointer;
1382
1383   if (G_UNLIKELY (n_collect_values != 4))
1384     return g_strdup_printf ("not enough value locations for `%s' passed",
1385         G_VALUE_TYPE_NAME (value));
1386
1387   for (i = 0; i < 4; i++) {
1388     if (G_UNLIKELY (collect_values[i].v_pointer == NULL)) {
1389       return g_strdup_printf ("value location for `%s' passed as NULL",
1390           G_VALUE_TYPE_NAME (value));
1391     }
1392     dest_values[i] = collect_values[i].v_pointer;
1393   }
1394
1395   if (G_UNLIKELY (vals == NULL)) {
1396     return g_strdup_printf ("Uninitialised `%s' passed",
1397         G_VALUE_TYPE_NAME (value));
1398   }
1399
1400   dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1401   dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1402   dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1403   dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1404   return NULL;
1405 }
1406
1407 /**
1408  * gst_value_set_fraction_range:
1409  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1410  * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1411  * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1412  *
1413  * Sets @value to the range specified by @start and @end.
1414  */
1415 void
1416 gst_value_set_fraction_range (GValue * value, const GValue * start,
1417     const GValue * end)
1418 {
1419   GValue *vals;
1420
1421   g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1422   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
1423   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
1424   g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
1425           start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
1426
1427   vals = (GValue *) value->data[0].v_pointer;
1428   if (vals == NULL) {
1429     gst_value_init_fraction_range (value);
1430     vals = value->data[0].v_pointer;
1431   }
1432   g_value_copy (start, &vals[0]);
1433   g_value_copy (end, &vals[1]);
1434 }
1435
1436 /**
1437  * gst_value_set_fraction_range_full:
1438  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1439  * @numerator_start: the numerator start of the range
1440  * @denominator_start: the denominator start of the range
1441  * @numerator_end: the numerator end of the range
1442  * @denominator_end: the denominator end of the range
1443  *
1444  * Sets @value to the range specified by @numerator_start/@denominator_start
1445  * and @numerator_end/@denominator_end.
1446  */
1447 void
1448 gst_value_set_fraction_range_full (GValue * value,
1449     gint numerator_start, gint denominator_start,
1450     gint numerator_end, gint denominator_end)
1451 {
1452   GValue start = { 0 };
1453   GValue end = { 0 };
1454
1455   g_return_if_fail (value != NULL);
1456   g_return_if_fail (denominator_start != 0);
1457   g_return_if_fail (denominator_end != 0);
1458   g_return_if_fail (gst_util_fraction_compare (numerator_start,
1459           denominator_start, numerator_end, denominator_end) < 0);
1460
1461   g_value_init (&start, GST_TYPE_FRACTION);
1462   g_value_init (&end, GST_TYPE_FRACTION);
1463
1464   gst_value_set_fraction (&start, numerator_start, denominator_start);
1465   gst_value_set_fraction (&end, numerator_end, denominator_end);
1466   gst_value_set_fraction_range (value, &start, &end);
1467
1468   g_value_unset (&start);
1469   g_value_unset (&end);
1470 }
1471
1472 /**
1473  * gst_value_get_fraction_range_min:
1474  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1475  *
1476  * Gets the minimum of the range specified by @value.
1477  *
1478  * Returns: the minimum of the range
1479  */
1480 const GValue *
1481 gst_value_get_fraction_range_min (const GValue * value)
1482 {
1483   GValue *vals;
1484
1485   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1486
1487   vals = (GValue *) value->data[0].v_pointer;
1488   if (vals != NULL) {
1489     return &vals[0];
1490   }
1491
1492   return NULL;
1493 }
1494
1495 /**
1496  * gst_value_get_fraction_range_max:
1497  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1498  *
1499  * Gets the maximum of the range specified by @value.
1500  *
1501  * Returns: the maximum of the range
1502  */
1503 const GValue *
1504 gst_value_get_fraction_range_max (const GValue * value)
1505 {
1506   GValue *vals;
1507
1508   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1509
1510   vals = (GValue *) value->data[0].v_pointer;
1511   if (vals != NULL) {
1512     return &vals[1];
1513   }
1514
1515   return NULL;
1516 }
1517
1518 static gchar *
1519 gst_value_serialize_fraction_range (const GValue * value)
1520 {
1521   GValue *vals = (GValue *) value->data[0].v_pointer;
1522   gchar *retval;
1523
1524   if (vals == NULL) {
1525     retval = g_strdup ("[ 0/1, 0/1 ]");
1526   } else {
1527     gchar *start, *end;
1528
1529     start = gst_value_serialize_fraction (&vals[0]);
1530     end = gst_value_serialize_fraction (&vals[1]);
1531
1532     retval = g_strdup_printf ("[ %s, %s ]", start, end);
1533     g_free (start);
1534     g_free (end);
1535   }
1536
1537   return retval;
1538 }
1539
1540 static void
1541 gst_value_transform_fraction_range_string (const GValue * src_value,
1542     GValue * dest_value)
1543 {
1544   dest_value->data[0].v_pointer =
1545       gst_value_serialize_fraction_range (src_value);
1546 }
1547
1548 static gint
1549 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1550 {
1551   GValue *vals1, *vals2;
1552   GstValueCompareFunc compare;
1553
1554   if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1555     return GST_VALUE_EQUAL;     /* Only possible if both are NULL */
1556
1557   if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1558     return GST_VALUE_UNORDERED;
1559
1560   vals1 = (GValue *) value1->data[0].v_pointer;
1561   vals2 = (GValue *) value2->data[0].v_pointer;
1562   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1563     if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1564         GST_VALUE_EQUAL &&
1565         gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1566         GST_VALUE_EQUAL)
1567       return GST_VALUE_EQUAL;
1568   }
1569   return GST_VALUE_UNORDERED;
1570 }
1571
1572 static gboolean
1573 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1574 {
1575   g_warning ("unimplemented");
1576   return FALSE;
1577 }
1578
1579 /***********
1580  * GstCaps *
1581  ***********/
1582
1583 /**
1584  * gst_value_set_caps:
1585  * @value: a GValue initialized to GST_TYPE_CAPS
1586  * @caps: (transfer none): the caps to set the value to
1587  *
1588  * Sets the contents of @value to @caps. A reference to the
1589  * provided @caps will be taken by the @value.
1590  */
1591 void
1592 gst_value_set_caps (GValue * value, const GstCaps * caps)
1593 {
1594   g_return_if_fail (G_IS_VALUE (value));
1595   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1596   g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
1597
1598   g_value_set_boxed (value, caps);
1599 }
1600
1601 /**
1602  * gst_value_get_caps:
1603  * @value: a GValue initialized to GST_TYPE_CAPS
1604  *
1605  * Gets the contents of @value. The reference count of the returned
1606  * #GstCaps will not be modified, therefore the caller must take one
1607  * before getting rid of the @value.
1608  *
1609  * Returns: (transfer none): the contents of @value
1610  */
1611 const GstCaps *
1612 gst_value_get_caps (const GValue * value)
1613 {
1614   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1615   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1616
1617   return (GstCaps *) g_value_get_boxed (value);
1618 }
1619
1620 static gchar *
1621 gst_value_serialize_caps (const GValue * value)
1622 {
1623   GstCaps *caps = g_value_get_boxed (value);
1624
1625   return gst_caps_to_string (caps);
1626 }
1627
1628 static gboolean
1629 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1630 {
1631   GstCaps *caps;
1632
1633   caps = gst_caps_from_string (s);
1634
1635   if (caps) {
1636     g_value_take_boxed (dest, caps);
1637     return TRUE;
1638   }
1639   return FALSE;
1640 }
1641
1642 /****************
1643  * GstStructure *
1644  ****************/
1645
1646 /**
1647  * gst_value_set_structure:
1648  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1649  * @structure: the structure to set the value to
1650  *
1651  * Sets the contents of @value to @structure.  The actual
1652  *
1653  * Since: 0.10.15
1654  */
1655 void
1656 gst_value_set_structure (GValue * value, const GstStructure * structure)
1657 {
1658   g_return_if_fail (G_IS_VALUE (value));
1659   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1660   g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
1661
1662   g_value_set_boxed (value, structure);
1663 }
1664
1665 /**
1666  * gst_value_get_structure:
1667  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1668  *
1669  * Gets the contents of @value.
1670  *
1671  * Returns: (transfer none): the contents of @value
1672  *
1673  * Since: 0.10.15
1674  */
1675 const GstStructure *
1676 gst_value_get_structure (const GValue * value)
1677 {
1678   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1679   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1680
1681   return (GstStructure *) g_value_get_boxed (value);
1682 }
1683
1684 static gchar *
1685 gst_value_serialize_structure (const GValue * value)
1686 {
1687   GstStructure *structure = g_value_get_boxed (value);
1688
1689   return gst_string_take_and_wrap (gst_structure_to_string (structure));
1690 }
1691
1692 static gboolean
1693 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1694 {
1695   GstStructure *structure;
1696
1697   if (*s != '"') {
1698     structure = gst_structure_from_string (s, NULL);
1699   } else {
1700     gchar *str = gst_string_unwrap (s);
1701
1702     if (G_UNLIKELY (!str))
1703       return FALSE;
1704
1705     structure = gst_structure_from_string (str, NULL);
1706     g_free (str);
1707   }
1708
1709   if (G_LIKELY (structure)) {
1710     g_value_take_boxed (dest, structure);
1711     return TRUE;
1712   }
1713   return FALSE;
1714 }
1715
1716 /*************
1717  * GstBuffer *
1718  *************/
1719
1720 static gint
1721 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1722 {
1723   GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
1724   GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
1725
1726   if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
1727     return GST_VALUE_UNORDERED;
1728   if (GST_BUFFER_SIZE (buf1) == 0)
1729     return GST_VALUE_EQUAL;
1730   g_assert (GST_BUFFER_DATA (buf1));
1731   g_assert (GST_BUFFER_DATA (buf2));
1732   if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
1733           GST_BUFFER_SIZE (buf1)) == 0)
1734     return GST_VALUE_EQUAL;
1735
1736   return GST_VALUE_UNORDERED;
1737 }
1738
1739 static gchar *
1740 gst_value_serialize_buffer (const GValue * value)
1741 {
1742   guint8 *data;
1743   gint i;
1744   gint size;
1745   gchar *string;
1746   GstBuffer *buffer;
1747
1748   buffer = gst_value_get_buffer (value);
1749   if (buffer == NULL)
1750     return NULL;
1751
1752   data = GST_BUFFER_DATA (buffer);
1753   size = GST_BUFFER_SIZE (buffer);
1754
1755   string = g_malloc (size * 2 + 1);
1756   for (i = 0; i < size; i++) {
1757     sprintf (string + i * 2, "%02x", data[i]);
1758   }
1759   string[size * 2] = 0;
1760
1761   return string;
1762 }
1763
1764 static gboolean
1765 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1766 {
1767   GstBuffer *buffer;
1768   gint len;
1769   gchar ts[3];
1770   guint8 *data;
1771   gint i;
1772
1773   len = strlen (s);
1774   if (len & 1)
1775     goto wrong_length;
1776
1777   buffer = gst_buffer_new_and_alloc (len / 2);
1778   data = GST_BUFFER_DATA (buffer);
1779   for (i = 0; i < len / 2; i++) {
1780     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1781       goto wrong_char;
1782
1783     ts[0] = s[i * 2 + 0];
1784     ts[1] = s[i * 2 + 1];
1785     ts[2] = 0;
1786
1787     data[i] = (guint8) strtoul (ts, NULL, 16);
1788   }
1789
1790   gst_value_take_buffer (dest, buffer);
1791
1792   return TRUE;
1793
1794   /* ERRORS */
1795 wrong_length:
1796   {
1797     return FALSE;
1798   }
1799 wrong_char:
1800   {
1801     gst_buffer_unref (buffer);
1802     return FALSE;
1803   }
1804 }
1805
1806
1807 /***********
1808  * boolean *
1809  ***********/
1810
1811 static gint
1812 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1813 {
1814   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1815     return GST_VALUE_EQUAL;
1816   return GST_VALUE_UNORDERED;
1817 }
1818
1819 static gchar *
1820 gst_value_serialize_boolean (const GValue * value)
1821 {
1822   if (value->data[0].v_int) {
1823     return g_strdup ("true");
1824   }
1825   return g_strdup ("false");
1826 }
1827
1828 static gboolean
1829 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1830 {
1831   gboolean ret = FALSE;
1832
1833   if (g_ascii_strcasecmp (s, "true") == 0 ||
1834       g_ascii_strcasecmp (s, "yes") == 0 ||
1835       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1836     g_value_set_boolean (dest, TRUE);
1837     ret = TRUE;
1838   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1839       g_ascii_strcasecmp (s, "no") == 0 ||
1840       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1841     g_value_set_boolean (dest, FALSE);
1842     ret = TRUE;
1843   }
1844
1845   return ret;
1846 }
1847
1848 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
1849 static gint                                                             \
1850 gst_value_compare_ ## _type                                             \
1851 (const GValue * value1, const GValue * value2)                          \
1852 {                                                                       \
1853   g ## _type val1 = g_value_get_ ## _type (value1);                     \
1854   g ## _type val2 = g_value_get_ ## _type (value2);                     \
1855   if (val1 > val2)                                                      \
1856     return GST_VALUE_GREATER_THAN;                                      \
1857   if (val1 < val2)                                                      \
1858     return GST_VALUE_LESS_THAN;                                         \
1859   return GST_VALUE_EQUAL;                                               \
1860 }                                                                       \
1861                                                                         \
1862 static gchar *                                                          \
1863 gst_value_serialize_ ## _type (const GValue * value)                    \
1864 {                                                                       \
1865   GValue val = { 0, };                                                  \
1866   g_value_init (&val, G_TYPE_STRING);                                   \
1867   if (!g_value_transform (value, &val))                                 \
1868     g_assert_not_reached ();                                            \
1869   /* NO_COPY_MADNESS!!! */                                              \
1870   return (char *) g_value_get_string (&val);                            \
1871 }
1872
1873 /* deserialize the given s into to as a gint64.
1874  * check if the result is actually storeable in the given size number of
1875  * bytes.
1876  */
1877 static gboolean
1878 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1879     gint64 min, gint64 max, gint size)
1880 {
1881   gboolean ret = FALSE;
1882   gchar *end;
1883   gint64 mask = -1;
1884
1885   errno = 0;
1886   *to = g_ascii_strtoull (s, &end, 0);
1887   /* a range error is a definitive no-no */
1888   if (errno == ERANGE) {
1889     return FALSE;
1890   }
1891
1892   if (*end == 0) {
1893     ret = TRUE;
1894   } else {
1895     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1896       *to = G_LITTLE_ENDIAN;
1897       ret = TRUE;
1898     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1899       *to = G_BIG_ENDIAN;
1900       ret = TRUE;
1901     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1902       *to = G_BYTE_ORDER;
1903       ret = TRUE;
1904     } else if (g_ascii_strcasecmp (s, "min") == 0) {
1905       *to = min;
1906       ret = TRUE;
1907     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1908       *to = max;
1909       ret = TRUE;
1910     }
1911   }
1912   if (ret) {
1913     /* by definition, a gint64 fits into a gint64; so ignore those */
1914     if (size != sizeof (mask)) {
1915       if (*to >= 0) {
1916         /* for positive numbers, we create a mask of 1's outside of the range
1917          * and 0's inside the range.  An and will thus keep only 1 bits
1918          * outside of the range */
1919         mask <<= (size * 8);
1920         if ((mask & *to) != 0) {
1921           ret = FALSE;
1922         }
1923       } else {
1924         /* for negative numbers, we do a 2's complement version */
1925         mask <<= ((size * 8) - 1);
1926         if ((mask & *to) != mask) {
1927           ret = FALSE;
1928         }
1929       }
1930     }
1931   }
1932   return ret;
1933 }
1934
1935 #define CREATE_SERIALIZATION(_type,_macro)                              \
1936 CREATE_SERIALIZATION_START(_type,_macro)                                \
1937                                                                         \
1938 static gboolean                                                         \
1939 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
1940 {                                                                       \
1941   gint64 x;                                                             \
1942                                                                         \
1943   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
1944       G_MAX ## _macro, sizeof (g ## _type))) {                          \
1945     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
1946     return TRUE;                                                        \
1947   } else {                                                              \
1948     return FALSE;                                                       \
1949   }                                                                     \
1950 }
1951
1952 #define CREATE_USERIALIZATION(_type,_macro)                             \
1953 CREATE_SERIALIZATION_START(_type,_macro)                                \
1954                                                                         \
1955 static gboolean                                                         \
1956 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
1957 {                                                                       \
1958   gint64 x;                                                             \
1959   gchar *end;                                                           \
1960   gboolean ret = FALSE;                                                 \
1961                                                                         \
1962   errno = 0;                                                            \
1963   x = g_ascii_strtoull (s, &end, 0);                                    \
1964   /* a range error is a definitive no-no */                             \
1965   if (errno == ERANGE) {                                                \
1966     return FALSE;                                                       \
1967   }                                                                     \
1968   /* the cast ensures the range check later on makes sense */           \
1969   x = (g ## _type) x;                                                   \
1970   if (*end == 0) {                                                      \
1971     ret = TRUE;                                                         \
1972   } else {                                                              \
1973     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
1974       x = G_LITTLE_ENDIAN;                                              \
1975       ret = TRUE;                                                       \
1976     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
1977       x = G_BIG_ENDIAN;                                                 \
1978       ret = TRUE;                                                       \
1979     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
1980       x = G_BYTE_ORDER;                                                 \
1981       ret = TRUE;                                                       \
1982     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
1983       x = 0;                                                            \
1984       ret = TRUE;                                                       \
1985     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
1986       x = G_MAX ## _macro;                                              \
1987       ret = TRUE;                                                       \
1988     }                                                                   \
1989   }                                                                     \
1990   if (ret) {                                                            \
1991     if (x > G_MAX ## _macro) {                                          \
1992       ret = FALSE;                                                      \
1993     } else {                                                            \
1994       g_value_set_ ## _type (dest, x);                                  \
1995     }                                                                   \
1996   }                                                                     \
1997   return ret;                                                           \
1998 }
1999
2000 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
2001 G_STMT_START {                                                          \
2002   static const GstValueTable gst_value = {                              \
2003     _gtype,                                                             \
2004     gst_value_compare_ ## _type,                                        \
2005     gst_value_serialize_ ## _type,                                      \
2006     gst_value_deserialize_ ## _type,                                    \
2007   };                                                                    \
2008                                                                         \
2009   gst_value_register (&gst_value);                                      \
2010 } G_STMT_END
2011
2012 CREATE_SERIALIZATION (int, INT);
2013 CREATE_SERIALIZATION (int64, INT64);
2014 CREATE_SERIALIZATION (long, LONG);
2015
2016 CREATE_USERIALIZATION (uint, UINT);
2017 CREATE_USERIALIZATION (uint64, UINT64);
2018 CREATE_USERIALIZATION (ulong, ULONG);
2019
2020 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2021 #ifndef G_MAXUCHAR
2022 #define G_MAXUCHAR 255
2023 #endif
2024 CREATE_USERIALIZATION (uchar, UCHAR);
2025
2026 /**********
2027  * double *
2028  **********/
2029 static gint
2030 gst_value_compare_double (const GValue * value1, const GValue * value2)
2031 {
2032   if (value1->data[0].v_double > value2->data[0].v_double)
2033     return GST_VALUE_GREATER_THAN;
2034   if (value1->data[0].v_double < value2->data[0].v_double)
2035     return GST_VALUE_LESS_THAN;
2036   if (value1->data[0].v_double == value2->data[0].v_double)
2037     return GST_VALUE_EQUAL;
2038   return GST_VALUE_UNORDERED;
2039 }
2040
2041 static gchar *
2042 gst_value_serialize_double (const GValue * value)
2043 {
2044   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2045
2046   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2047   return g_strdup (d);
2048 }
2049
2050 static gboolean
2051 gst_value_deserialize_double (GValue * dest, const gchar * s)
2052 {
2053   gdouble x;
2054   gboolean ret = FALSE;
2055   gchar *end;
2056
2057   x = g_ascii_strtod (s, &end);
2058   if (*end == 0) {
2059     ret = TRUE;
2060   } else {
2061     if (g_ascii_strcasecmp (s, "min") == 0) {
2062       x = -G_MAXDOUBLE;
2063       ret = TRUE;
2064     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2065       x = G_MAXDOUBLE;
2066       ret = TRUE;
2067     }
2068   }
2069   if (ret) {
2070     g_value_set_double (dest, x);
2071   }
2072   return ret;
2073 }
2074
2075 /*********
2076  * float *
2077  *********/
2078
2079 static gint
2080 gst_value_compare_float (const GValue * value1, const GValue * value2)
2081 {
2082   if (value1->data[0].v_float > value2->data[0].v_float)
2083     return GST_VALUE_GREATER_THAN;
2084   if (value1->data[0].v_float < value2->data[0].v_float)
2085     return GST_VALUE_LESS_THAN;
2086   if (value1->data[0].v_float == value2->data[0].v_float)
2087     return GST_VALUE_EQUAL;
2088   return GST_VALUE_UNORDERED;
2089 }
2090
2091 static gchar *
2092 gst_value_serialize_float (const GValue * value)
2093 {
2094   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2095
2096   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2097   return g_strdup (d);
2098 }
2099
2100 static gboolean
2101 gst_value_deserialize_float (GValue * dest, const gchar * s)
2102 {
2103   gdouble x;
2104   gboolean ret = FALSE;
2105   gchar *end;
2106
2107   x = g_ascii_strtod (s, &end);
2108   if (*end == 0) {
2109     ret = TRUE;
2110   } else {
2111     if (g_ascii_strcasecmp (s, "min") == 0) {
2112       x = -G_MAXFLOAT;
2113       ret = TRUE;
2114     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2115       x = G_MAXFLOAT;
2116       ret = TRUE;
2117     }
2118   }
2119   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2120     ret = FALSE;
2121   if (ret) {
2122     g_value_set_float (dest, (float) x);
2123   }
2124   return ret;
2125 }
2126
2127 /**********
2128  * string *
2129  **********/
2130
2131 static gint
2132 gst_value_compare_string (const GValue * value1, const GValue * value2)
2133 {
2134   if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2135     /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2136     if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2137       return GST_VALUE_UNORDERED;
2138   } else {
2139     gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2140
2141     if (x < 0)
2142       return GST_VALUE_LESS_THAN;
2143     if (x > 0)
2144       return GST_VALUE_GREATER_THAN;
2145   }
2146
2147   return GST_VALUE_EQUAL;
2148 }
2149
2150 static gint
2151 gst_string_measure_wrapping (const gchar * s)
2152 {
2153   gint len;
2154   gboolean wrap = FALSE;
2155
2156   if (G_UNLIKELY (s == NULL))
2157     return -1;
2158
2159   /* Special case: the actual string NULL needs wrapping */
2160   if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2161     return 4;
2162
2163   len = 0;
2164   while (*s) {
2165     if (GST_ASCII_IS_STRING (*s)) {
2166       len++;
2167     } else if (*s < 0x20 || *s >= 0x7f) {
2168       wrap = TRUE;
2169       len += 4;
2170     } else {
2171       wrap = TRUE;
2172       len += 2;
2173     }
2174     s++;
2175   }
2176
2177   /* Wrap the string if we found something that needs
2178    * wrapping, or the empty string (len == 0) */
2179   return (wrap || len == 0) ? len : -1;
2180 }
2181
2182 static gchar *
2183 gst_string_wrap_inner (const gchar * s, gint len)
2184 {
2185   gchar *d, *e;
2186
2187   e = d = g_malloc (len + 3);
2188
2189   *e++ = '\"';
2190   while (*s) {
2191     if (GST_ASCII_IS_STRING (*s)) {
2192       *e++ = *s++;
2193     } else if (*s < 0x20 || *s >= 0x7f) {
2194       *e++ = '\\';
2195       *e++ = '0' + ((*(guchar *) s) >> 6);
2196       *e++ = '0' + (((*s) >> 3) & 0x7);
2197       *e++ = '0' + ((*s++) & 0x7);
2198     } else {
2199       *e++ = '\\';
2200       *e++ = *s++;
2201     }
2202   }
2203   *e++ = '\"';
2204   *e = 0;
2205
2206   g_assert (e - d <= len + 3);
2207   return d;
2208 }
2209
2210 /* Do string wrapping/escaping */
2211 static gchar *
2212 gst_string_wrap (const gchar * s)
2213 {
2214   gint len = gst_string_measure_wrapping (s);
2215
2216   if (G_LIKELY (len < 0))
2217     return g_strdup (s);
2218
2219   return gst_string_wrap_inner (s, len);
2220 }
2221
2222 /* Same as above, but take ownership of the string */
2223 static gchar *
2224 gst_string_take_and_wrap (gchar * s)
2225 {
2226   gchar *out;
2227   gint len = gst_string_measure_wrapping (s);
2228
2229   if (G_LIKELY (len < 0))
2230     return s;
2231
2232   out = gst_string_wrap_inner (s, len);
2233   g_free (s);
2234
2235   return out;
2236 }
2237
2238 /*
2239  * This function takes a string delimited with double quotes (")
2240  * and unescapes any \xxx octal numbers.
2241  *
2242  * If sequences of \y are found where y is not in the range of
2243  * 0->3, y is copied unescaped.
2244  *
2245  * If \xyy is found where x is an octal number but y is not, an
2246  * error is encountered and NULL is returned.
2247  *
2248  * the input string must be \0 terminated.
2249  */
2250 static gchar *
2251 gst_string_unwrap (const gchar * s)
2252 {
2253   gchar *ret;
2254   gchar *read, *write;
2255
2256   /* NULL string returns NULL */
2257   if (s == NULL)
2258     return NULL;
2259
2260   /* strings not starting with " are invalid */
2261   if (*s != '"')
2262     return NULL;
2263
2264   /* make copy of original string to hold the result. This
2265    * string will always be smaller than the original */
2266   ret = g_strdup (s);
2267   read = ret;
2268   write = ret;
2269
2270   /* need to move to the next position as we parsed the " */
2271   read++;
2272
2273   while (*read) {
2274     if (GST_ASCII_IS_STRING (*read)) {
2275       /* normal chars are just copied */
2276       *write++ = *read++;
2277     } else if (*read == '"') {
2278       /* quote marks end of string */
2279       break;
2280     } else if (*read == '\\') {
2281       /* got an escape char, move to next position to read a tripplet
2282        * of octal numbers */
2283       read++;
2284       /* is the next char a possible first octal number? */
2285       if (*read >= '0' && *read <= '3') {
2286         /* parse other 2 numbers, if one of them is not in the range of
2287          * an octal number, we error. We also catch the case where a zero
2288          * byte is found here. */
2289         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2290           goto beach;
2291
2292         /* now convert the octal number to a byte again. */
2293         *write++ = ((read[0] - '0') << 6) +
2294             ((read[1] - '0') << 3) + (read[2] - '0');
2295
2296         read += 3;
2297       } else {
2298         /* if we run into a \0 here, we definitely won't get a quote later */
2299         if (*read == 0)
2300           goto beach;
2301
2302         /* else copy \X sequence */
2303         *write++ = *read++;
2304       }
2305     } else {
2306       /* weird character, error */
2307       goto beach;
2308     }
2309   }
2310   /* if the string is not ending in " and zero terminated, we error */
2311   if (*read != '"' || read[1] != '\0')
2312     goto beach;
2313
2314   /* null terminate result string and return */
2315   *write = '\0';
2316   return ret;
2317
2318 beach:
2319   g_free (ret);
2320   return NULL;
2321 }
2322
2323 static gchar *
2324 gst_value_serialize_string (const GValue * value)
2325 {
2326   return gst_string_wrap (value->data[0].v_pointer);
2327 }
2328
2329 static gboolean
2330 gst_value_deserialize_string (GValue * dest, const gchar * s)
2331 {
2332   if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2333     g_value_set_string (dest, NULL);
2334     return TRUE;
2335   } else if (G_LIKELY (*s != '"')) {
2336     if (!g_utf8_validate (s, -1, NULL))
2337       return FALSE;
2338     g_value_set_string (dest, s);
2339     return TRUE;
2340   } else {
2341     gchar *str = gst_string_unwrap (s);
2342     if (G_UNLIKELY (!str))
2343       return FALSE;
2344     g_value_take_string (dest, str);
2345   }
2346
2347   return TRUE;
2348 }
2349
2350 /********
2351  * enum *
2352  ********/
2353
2354 static gint
2355 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2356 {
2357   GEnumValue *en1, *en2;
2358   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2359   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2360
2361   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2362   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2363   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2364   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2365   g_type_class_unref (klass1);
2366   g_type_class_unref (klass2);
2367   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2368   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2369   if (en1->value < en2->value)
2370     return GST_VALUE_LESS_THAN;
2371   if (en1->value > en2->value)
2372     return GST_VALUE_GREATER_THAN;
2373
2374   return GST_VALUE_EQUAL;
2375 }
2376
2377 static gchar *
2378 gst_value_serialize_enum (const GValue * value)
2379 {
2380   GEnumValue *en;
2381   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
2382
2383   g_return_val_if_fail (klass, NULL);
2384   en = g_enum_get_value (klass, g_value_get_enum (value));
2385   g_type_class_unref (klass);
2386
2387   /* might be one of the custom formats registered later */
2388   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
2389     const GstFormatDefinition *format_def;
2390
2391     format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
2392     g_return_val_if_fail (format_def != NULL, NULL);
2393     return g_strdup (format_def->description);
2394   }
2395
2396   g_return_val_if_fail (en, NULL);
2397   return g_strdup (en->value_name);
2398 }
2399
2400 static gint
2401 gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
2402     const gchar * s)
2403 {
2404   if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2405     return 0;
2406
2407   return g_ascii_strcasecmp (s, format_def->description);
2408 }
2409
2410 static gboolean
2411 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2412 {
2413   GEnumValue *en;
2414   gchar *endptr = NULL;
2415   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2416
2417   g_return_val_if_fail (klass, FALSE);
2418   if (!(en = g_enum_get_value_by_name (klass, s))) {
2419     if (!(en = g_enum_get_value_by_nick (klass, s))) {
2420       gint i = strtol (s, &endptr, 0);
2421
2422       if (endptr && *endptr == '\0') {
2423         en = g_enum_get_value (klass, i);
2424       }
2425     }
2426   }
2427   g_type_class_unref (klass);
2428
2429   /* might be one of the custom formats registered later */
2430   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2431     const GstFormatDefinition *format_def;
2432     GstIterator *iter;
2433
2434     iter = gst_format_iterate_definitions ();
2435
2436     format_def = gst_iterator_find_custom (iter,
2437         (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
2438
2439     g_return_val_if_fail (format_def != NULL, FALSE);
2440     g_value_set_enum (dest, (gint) format_def->value);
2441     gst_iterator_free (iter);
2442     return TRUE;
2443   }
2444
2445   g_return_val_if_fail (en, FALSE);
2446   g_value_set_enum (dest, en->value);
2447   return TRUE;
2448 }
2449
2450 /********
2451  * flags *
2452  ********/
2453
2454 /* we just compare the value here */
2455 static gint
2456 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2457 {
2458   guint fl1, fl2;
2459   GFlagsClass *klass1 =
2460       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2461   GFlagsClass *klass2 =
2462       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2463
2464   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2465   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2466   fl1 = g_value_get_flags (value1);
2467   fl2 = g_value_get_flags (value2);
2468   g_type_class_unref (klass1);
2469   g_type_class_unref (klass2);
2470   if (fl1 < fl2)
2471     return GST_VALUE_LESS_THAN;
2472   if (fl1 > fl2)
2473     return GST_VALUE_GREATER_THAN;
2474
2475   return GST_VALUE_EQUAL;
2476 }
2477
2478 /* the different flags are serialized separated with a + */
2479 static gchar *
2480 gst_value_serialize_flags (const GValue * value)
2481 {
2482   guint flags;
2483   GFlagsValue *fl;
2484   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2485   gchar *result, *tmp;
2486   gboolean first = TRUE;
2487
2488   g_return_val_if_fail (klass, NULL);
2489
2490   flags = g_value_get_flags (value);
2491
2492   /* if no flags are set, try to serialize to the _NONE string */
2493   if (!flags) {
2494     fl = g_flags_get_first_value (klass, flags);
2495     return g_strdup (fl->value_name);
2496   }
2497
2498   /* some flags are set, so serialize one by one */
2499   result = g_strdup ("");
2500   while (flags) {
2501     fl = g_flags_get_first_value (klass, flags);
2502     if (fl != NULL) {
2503       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2504       g_free (result);
2505       result = tmp;
2506       first = FALSE;
2507
2508       /* clear flag */
2509       flags &= ~fl->value;
2510     }
2511   }
2512   g_type_class_unref (klass);
2513
2514   return result;
2515 }
2516
2517 static gboolean
2518 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2519 {
2520   GFlagsValue *fl;
2521   gchar *endptr = NULL;
2522   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2523   gchar **split;
2524   guint flags;
2525   gint i;
2526
2527   g_return_val_if_fail (klass, FALSE);
2528
2529   /* split into parts delimited with + */
2530   split = g_strsplit (s, "+", 0);
2531
2532   flags = 0;
2533   i = 0;
2534   /* loop over each part */
2535   while (split[i]) {
2536     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2537       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2538         gint val = strtol (split[i], &endptr, 0);
2539
2540         /* just or numeric value */
2541         if (endptr && *endptr == '\0') {
2542           flags |= val;
2543         }
2544       }
2545     }
2546     if (fl) {
2547       flags |= fl->value;
2548     }
2549     i++;
2550   }
2551   g_strfreev (split);
2552   g_type_class_unref (klass);
2553   g_value_set_flags (dest, flags);
2554
2555   return TRUE;
2556 }
2557
2558 /*********
2559  * union *
2560  *********/
2561
2562 static gboolean
2563 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2564     const GValue * src2)
2565 {
2566   if (src2->data[0].v_int <= src1->data[0].v_int &&
2567       src2->data[1].v_int >= src1->data[0].v_int) {
2568     gst_value_init_and_copy (dest, src2);
2569     return TRUE;
2570   }
2571   return FALSE;
2572 }
2573
2574 static gboolean
2575 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2576     const GValue * src2)
2577 {
2578   gint min;
2579   gint max;
2580
2581   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2582   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2583
2584   if (min <= max) {
2585     g_value_init (dest, GST_TYPE_INT_RANGE);
2586     gst_value_set_int_range (dest,
2587         MIN (src1->data[0].v_int, src2->data[0].v_int),
2588         MAX (src1->data[1].v_int, src2->data[1].v_int));
2589     return TRUE;
2590   }
2591
2592   return FALSE;
2593 }
2594
2595 /****************
2596  * intersection *
2597  ****************/
2598
2599 static gboolean
2600 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2601     const GValue * src2)
2602 {
2603   if (src2->data[0].v_int <= src1->data[0].v_int &&
2604       src2->data[1].v_int >= src1->data[0].v_int) {
2605     if (dest)
2606       gst_value_init_and_copy (dest, src1);
2607     return TRUE;
2608   }
2609
2610   return FALSE;
2611 }
2612
2613 static gboolean
2614 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2615     const GValue * src2)
2616 {
2617   gint min;
2618   gint max;
2619
2620   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2621   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2622
2623   if (min < max) {
2624     if (dest) {
2625       g_value_init (dest, GST_TYPE_INT_RANGE);
2626       gst_value_set_int_range (dest, min, max);
2627     }
2628     return TRUE;
2629   }
2630   if (min == max) {
2631     if (dest) {
2632       g_value_init (dest, G_TYPE_INT);
2633       g_value_set_int (dest, min);
2634     }
2635     return TRUE;
2636   }
2637
2638   return FALSE;
2639 }
2640
2641 static gboolean
2642 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
2643     const GValue * src2)
2644 {
2645   if (src2->data[0].v_int64 <= src1->data[0].v_int64 &&
2646       src2->data[1].v_int64 >= src1->data[0].v_int64) {
2647     if (dest)
2648       gst_value_init_and_copy (dest, src1);
2649     return TRUE;
2650   }
2651
2652   return FALSE;
2653 }
2654
2655 static gboolean
2656 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
2657     const GValue * src2)
2658 {
2659   gint64 min;
2660   gint64 max;
2661
2662   min = MAX (src1->data[0].v_int64, src2->data[0].v_int64);
2663   max = MIN (src1->data[1].v_int64, src2->data[1].v_int64);
2664
2665   if (min < max) {
2666     if (dest) {
2667       g_value_init (dest, GST_TYPE_INT64_RANGE);
2668       gst_value_set_int64_range (dest, min, max);
2669     }
2670     return TRUE;
2671   }
2672   if (min == max) {
2673     if (dest) {
2674       g_value_init (dest, G_TYPE_INT64);
2675       g_value_set_int64 (dest, min);
2676     }
2677     return TRUE;
2678   }
2679
2680   return FALSE;
2681 }
2682
2683 static gboolean
2684 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2685     const GValue * src2)
2686 {
2687   if (src2->data[0].v_double <= src1->data[0].v_double &&
2688       src2->data[1].v_double >= src1->data[0].v_double) {
2689     if (dest)
2690       gst_value_init_and_copy (dest, src1);
2691     return TRUE;
2692   }
2693
2694   return FALSE;
2695 }
2696
2697 static gboolean
2698 gst_value_intersect_double_range_double_range (GValue * dest,
2699     const GValue * src1, const GValue * src2)
2700 {
2701   gdouble min;
2702   gdouble max;
2703
2704   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2705   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2706
2707   if (min < max) {
2708     if (dest) {
2709       g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2710       gst_value_set_double_range (dest, min, max);
2711     }
2712     return TRUE;
2713   }
2714   if (min == max) {
2715     if (dest) {
2716       g_value_init (dest, G_TYPE_DOUBLE);
2717       g_value_set_int (dest, (int) min);
2718     }
2719     return TRUE;
2720   }
2721
2722   return FALSE;
2723 }
2724
2725 static gboolean
2726 gst_value_intersect_list (GValue * dest, const GValue * value1,
2727     const GValue * value2)
2728 {
2729   guint i, size;
2730   GValue intersection = { 0, };
2731   gboolean ret = FALSE;
2732
2733   size = VALUE_LIST_SIZE (value1);
2734   for (i = 0; i < size; i++) {
2735     const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
2736
2737     /* quicker version when we don't need the resulting set */
2738     if (!dest) {
2739       if (gst_value_intersect (NULL, cur, value2)) {
2740         ret = TRUE;
2741         break;
2742       }
2743       continue;
2744     }
2745
2746     if (gst_value_intersect (&intersection, cur, value2)) {
2747       /* append value */
2748       if (!ret) {
2749         gst_value_init_and_copy (dest, &intersection);
2750         ret = TRUE;
2751       } else if (GST_VALUE_HOLDS_LIST (dest)) {
2752         gst_value_list_append_value (dest, &intersection);
2753       } else {
2754         GValue temp = { 0, };
2755
2756         gst_value_init_and_copy (&temp, dest);
2757         g_value_unset (dest);
2758         gst_value_list_concat (dest, &temp, &intersection);
2759         g_value_unset (&temp);
2760       }
2761       g_value_unset (&intersection);
2762     }
2763   }
2764
2765   return ret;
2766 }
2767
2768 static gboolean
2769 gst_value_intersect_array (GValue * dest, const GValue * src1,
2770     const GValue * src2)
2771 {
2772   guint size;
2773   guint n;
2774   GValue val = { 0 };
2775
2776   /* only works on similar-sized arrays */
2777   size = gst_value_array_get_size (src1);
2778   if (size != gst_value_array_get_size (src2))
2779     return FALSE;
2780
2781   /* quicker value when we don't need the resulting set */
2782   if (!dest) {
2783     for (n = 0; n < size; n++) {
2784       if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
2785               gst_value_array_get_value (src2, n))) {
2786         return FALSE;
2787       }
2788     }
2789     return TRUE;
2790   }
2791
2792   g_value_init (dest, GST_TYPE_ARRAY);
2793
2794   for (n = 0; n < size; n++) {
2795     if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2796             gst_value_array_get_value (src2, n))) {
2797       g_value_unset (dest);
2798       return FALSE;
2799     }
2800     gst_value_array_append_value (dest, &val);
2801     g_value_unset (&val);
2802   }
2803
2804   return TRUE;
2805 }
2806
2807 static gboolean
2808 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2809     const GValue * src2)
2810 {
2811   gint res1, res2;
2812   GValue *vals;
2813   GstValueCompareFunc compare;
2814
2815   vals = src2->data[0].v_pointer;
2816
2817   if (vals == NULL)
2818     return FALSE;
2819
2820   if ((compare = gst_value_get_compare_func (src1))) {
2821     res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2822     res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2823
2824     if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2825         (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2826       if (dest)
2827         gst_value_init_and_copy (dest, src1);
2828       return TRUE;
2829     }
2830   }
2831
2832   return FALSE;
2833 }
2834
2835 static gboolean
2836 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
2837     const GValue * src1, const GValue * src2)
2838 {
2839   GValue *min;
2840   GValue *max;
2841   gint res;
2842   GValue *vals1, *vals2;
2843   GstValueCompareFunc compare;
2844
2845   vals1 = src1->data[0].v_pointer;
2846   vals2 = src2->data[0].v_pointer;
2847   g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2848
2849   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2850     /* min = MAX (src1.start, src2.start) */
2851     res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2852     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2853     if (res == GST_VALUE_LESS_THAN)
2854       min = &vals2[0];          /* Take the max of the 2 */
2855     else
2856       min = &vals1[0];
2857
2858     /* max = MIN (src1.end, src2.end) */
2859     res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2860     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2861     if (res == GST_VALUE_GREATER_THAN)
2862       max = &vals2[1];          /* Take the min of the 2 */
2863     else
2864       max = &vals1[1];
2865
2866     res = gst_value_compare_with_func (min, max, compare);
2867     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2868     if (res == GST_VALUE_LESS_THAN) {
2869       if (dest) {
2870         g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2871         vals1 = dest->data[0].v_pointer;
2872         g_value_copy (min, &vals1[0]);
2873         g_value_copy (max, &vals1[1]);
2874       }
2875       return TRUE;
2876     }
2877     if (res == GST_VALUE_EQUAL) {
2878       if (dest)
2879         gst_value_init_and_copy (dest, min);
2880       return TRUE;
2881     }
2882   }
2883
2884   return FALSE;
2885 }
2886
2887 /***************
2888  * subtraction *
2889  ***************/
2890
2891 static gboolean
2892 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2893     const GValue * subtrahend)
2894 {
2895   gint min = gst_value_get_int_range_min (subtrahend);
2896   gint max = gst_value_get_int_range_max (subtrahend);
2897   gint val = g_value_get_int (minuend);
2898
2899   /* subtracting a range from an int only works if the int is not in the
2900    * range */
2901   if (val < min || val > max) {
2902     /* and the result is the int */
2903     if (dest)
2904       gst_value_init_and_copy (dest, minuend);
2905     return TRUE;
2906   }
2907   return FALSE;
2908 }
2909
2910 /* creates a new int range based on input values.
2911  */
2912 static gboolean
2913 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2914     gint max2)
2915 {
2916   GValue v1 = { 0, };
2917   GValue v2 = { 0, };
2918   GValue *pv1, *pv2;            /* yeah, hungarian! */
2919
2920   if (min1 <= max1 && min2 <= max2) {
2921     pv1 = &v1;
2922     pv2 = &v2;
2923   } else if (min1 <= max1) {
2924     pv1 = dest;
2925     pv2 = NULL;
2926   } else if (min2 <= max2) {
2927     pv1 = NULL;
2928     pv2 = dest;
2929   } else {
2930     return FALSE;
2931   }
2932
2933   if (!dest)
2934     return TRUE;
2935
2936   if (min1 < max1) {
2937     g_value_init (pv1, GST_TYPE_INT_RANGE);
2938     gst_value_set_int_range (pv1, min1, max1);
2939   } else if (min1 == max1) {
2940     g_value_init (pv1, G_TYPE_INT);
2941     g_value_set_int (pv1, min1);
2942   }
2943   if (min2 < max2) {
2944     g_value_init (pv2, GST_TYPE_INT_RANGE);
2945     gst_value_set_int_range (pv2, min2, max2);
2946   } else if (min2 == max2) {
2947     g_value_init (pv2, G_TYPE_INT);
2948     g_value_set_int (pv2, min2);
2949   }
2950
2951   if (min1 <= max1 && min2 <= max2) {
2952     gst_value_list_concat (dest, pv1, pv2);
2953     g_value_unset (pv1);
2954     g_value_unset (pv2);
2955   }
2956   return TRUE;
2957 }
2958
2959 static gboolean
2960 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2961     const GValue * subtrahend)
2962 {
2963   gint min = gst_value_get_int_range_min (minuend);
2964   gint max = gst_value_get_int_range_max (minuend);
2965   gint val = g_value_get_int (subtrahend);
2966
2967   g_return_val_if_fail (min < max, FALSE);
2968
2969   /* value is outside of the range, return range unchanged */
2970   if (val < min || val > max) {
2971     if (dest)
2972       gst_value_init_and_copy (dest, minuend);
2973     return TRUE;
2974   } else {
2975     /* max must be MAXINT too as val <= max */
2976     if (val == G_MAXINT) {
2977       max--;
2978       val--;
2979     }
2980     /* min must be MININT too as val >= max */
2981     if (val == G_MININT) {
2982       min++;
2983       val++;
2984     }
2985     if (dest)
2986       gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2987   }
2988   return TRUE;
2989 }
2990
2991 static gboolean
2992 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2993     const GValue * subtrahend)
2994 {
2995   gint min1 = gst_value_get_int_range_min (minuend);
2996   gint max1 = gst_value_get_int_range_max (minuend);
2997   gint min2 = gst_value_get_int_range_min (subtrahend);
2998   gint max2 = gst_value_get_int_range_max (subtrahend);
2999
3000   if (max2 == G_MAXINT && min2 == G_MININT) {
3001     return FALSE;
3002   } else if (max2 == G_MAXINT) {
3003     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
3004   } else if (min2 == G_MININT) {
3005     return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
3006   } else {
3007     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
3008         MAX (max2 + 1, min1), max1);
3009   }
3010 }
3011
3012 static gboolean
3013 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
3014     const GValue * subtrahend)
3015 {
3016   gint64 min = gst_value_get_int64_range_min (subtrahend);
3017   gint64 max = gst_value_get_int64_range_max (subtrahend);
3018   gint64 val = g_value_get_int64 (minuend);
3019
3020   /* subtracting a range from an int64 only works if the int64 is not in the
3021    * range */
3022   if (val < min || val > max) {
3023     /* and the result is the int64 */
3024     if (dest)
3025       gst_value_init_and_copy (dest, minuend);
3026     return TRUE;
3027   }
3028   return FALSE;
3029 }
3030
3031 /* creates a new int64 range based on input values.
3032  */
3033 static gboolean
3034 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
3035     gint64 min2, gint64 max2)
3036 {
3037   GValue v1 = { 0, };
3038   GValue v2 = { 0, };
3039   GValue *pv1, *pv2;            /* yeah, hungarian! */
3040
3041   if (min1 <= max1 && min2 <= max2) {
3042     pv1 = &v1;
3043     pv2 = &v2;
3044   } else if (min1 <= max1) {
3045     pv1 = dest;
3046     pv2 = NULL;
3047   } else if (min2 <= max2) {
3048     pv1 = NULL;
3049     pv2 = dest;
3050   } else {
3051     return FALSE;
3052   }
3053
3054   if (!dest)
3055     return TRUE;
3056
3057   if (min1 < max1) {
3058     g_value_init (pv1, GST_TYPE_INT64_RANGE);
3059     gst_value_set_int64_range (pv1, min1, max1);
3060   } else if (min1 == max1) {
3061     g_value_init (pv1, G_TYPE_INT64);
3062     g_value_set_int64 (pv1, min1);
3063   }
3064   if (min2 < max2) {
3065     g_value_init (pv2, GST_TYPE_INT64_RANGE);
3066     gst_value_set_int64_range (pv2, min2, max2);
3067   } else if (min2 == max2) {
3068     g_value_init (pv2, G_TYPE_INT64);
3069     g_value_set_int64 (pv2, min2);
3070   }
3071
3072   if (min1 <= max1 && min2 <= max2) {
3073     gst_value_list_concat (dest, pv1, pv2);
3074     g_value_unset (pv1);
3075     g_value_unset (pv2);
3076   }
3077   return TRUE;
3078 }
3079
3080 static gboolean
3081 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
3082     const GValue * subtrahend)
3083 {
3084   gint64 min = gst_value_get_int64_range_min (minuend);
3085   gint64 max = gst_value_get_int64_range_max (minuend);
3086   gint64 val = g_value_get_int64 (subtrahend);
3087
3088   g_return_val_if_fail (min < max, FALSE);
3089
3090   /* value is outside of the range, return range unchanged */
3091   if (val < min || val > max) {
3092     if (dest)
3093       gst_value_init_and_copy (dest, minuend);
3094     return TRUE;
3095   } else {
3096     /* max must be MAXINT64 too as val <= max */
3097     if (val == G_MAXINT64) {
3098       max--;
3099       val--;
3100     }
3101     /* min must be MININT64 too as val >= max */
3102     if (val == G_MININT64) {
3103       min++;
3104       val++;
3105     }
3106     if (dest)
3107       gst_value_create_new_int64_range (dest, min, val - 1, val + 1, max);
3108   }
3109   return TRUE;
3110 }
3111
3112 static gboolean
3113 gst_value_subtract_int64_range_int64_range (GValue * dest,
3114     const GValue * minuend, const GValue * subtrahend)
3115 {
3116   gint64 min1 = gst_value_get_int64_range_min (minuend);
3117   gint64 max1 = gst_value_get_int64_range_max (minuend);
3118   gint64 min2 = gst_value_get_int64_range_min (subtrahend);
3119   gint64 max2 = gst_value_get_int64_range_max (subtrahend);
3120
3121   if (max2 == G_MAXINT64 && min2 == G_MININT64) {
3122     return FALSE;
3123   } else if (max2 == G_MAXINT64) {
3124     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
3125         1, 0);
3126   } else if (min2 == G_MININT64) {
3127     return gst_value_create_new_int64_range (dest, MAX (max2 + 1, min1), max1,
3128         1, 0);
3129   } else {
3130     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
3131         MAX (max2 + 1, min1), max1);
3132   }
3133 }
3134
3135 static gboolean
3136 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
3137     const GValue * subtrahend)
3138 {
3139   gdouble min = gst_value_get_double_range_min (subtrahend);
3140   gdouble max = gst_value_get_double_range_max (subtrahend);
3141   gdouble val = g_value_get_double (minuend);
3142
3143   if (val < min || val > max) {
3144     if (dest)
3145       gst_value_init_and_copy (dest, minuend);
3146     return TRUE;
3147   }
3148   return FALSE;
3149 }
3150
3151 static gboolean
3152 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
3153     const GValue * subtrahend)
3154 {
3155   /* since we don't have open ranges, we cannot create a hole in
3156    * a double range. We return the original range */
3157   if (dest)
3158     gst_value_init_and_copy (dest, minuend);
3159   return TRUE;
3160 }
3161
3162 static gboolean
3163 gst_value_subtract_double_range_double_range (GValue * dest,
3164     const GValue * minuend, const GValue * subtrahend)
3165 {
3166   /* since we don't have open ranges, we have to approximate */
3167   /* done like with ints */
3168   gdouble min1 = gst_value_get_double_range_min (minuend);
3169   gdouble max2 = gst_value_get_double_range_max (minuend);
3170   gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
3171   gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
3172   GValue v1 = { 0, };
3173   GValue v2 = { 0, };
3174   GValue *pv1, *pv2;            /* yeah, hungarian! */
3175
3176   if (min1 < max1 && min2 < max2) {
3177     pv1 = &v1;
3178     pv2 = &v2;
3179   } else if (min1 < max1) {
3180     pv1 = dest;
3181     pv2 = NULL;
3182   } else if (min2 < max2) {
3183     pv1 = NULL;
3184     pv2 = dest;
3185   } else {
3186     return FALSE;
3187   }
3188
3189   if (!dest)
3190     return TRUE;
3191
3192   if (min1 < max1) {
3193     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
3194     gst_value_set_double_range (pv1, min1, max1);
3195   }
3196   if (min2 < max2) {
3197     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
3198     gst_value_set_double_range (pv2, min2, max2);
3199   }
3200
3201   if (min1 < max1 && min2 < max2) {
3202     gst_value_list_concat (dest, pv1, pv2);
3203     g_value_unset (pv1);
3204     g_value_unset (pv2);
3205   }
3206   return TRUE;
3207 }
3208
3209 static gboolean
3210 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
3211     const GValue * subtrahend)
3212 {
3213   guint i, size;
3214   GValue subtraction = { 0, };
3215   gboolean ret = FALSE;
3216   GType ltype;
3217
3218   ltype = gst_value_list_get_type ();
3219
3220   size = VALUE_LIST_SIZE (minuend);
3221   for (i = 0; i < size; i++) {
3222     const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
3223
3224     /* quicker version when we can discard the result */
3225     if (!dest) {
3226       if (gst_value_subtract (NULL, cur, subtrahend)) {
3227         ret = TRUE;
3228         break;
3229       }
3230       continue;
3231     }
3232
3233     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
3234       if (!ret) {
3235         gst_value_init_and_copy (dest, &subtraction);
3236         ret = TRUE;
3237       } else if (G_VALUE_HOLDS (dest, ltype)
3238           && !G_VALUE_HOLDS (&subtraction, ltype)) {
3239         gst_value_list_append_value (dest, &subtraction);
3240       } else {
3241         GValue temp = { 0, };
3242
3243         gst_value_init_and_copy (&temp, dest);
3244         g_value_unset (dest);
3245         gst_value_list_concat (dest, &temp, &subtraction);
3246         g_value_unset (&temp);
3247       }
3248       g_value_unset (&subtraction);
3249     }
3250   }
3251   return ret;
3252 }
3253
3254 static gboolean
3255 gst_value_subtract_list (GValue * dest, const GValue * minuend,
3256     const GValue * subtrahend)
3257 {
3258   guint i, size;
3259   GValue data[2] = { {0,}, {0,} };
3260   GValue *subtraction = &data[0], *result = &data[1];
3261
3262   gst_value_init_and_copy (result, minuend);
3263   size = VALUE_LIST_SIZE (subtrahend);
3264   for (i = 0; i < size; i++) {
3265     const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
3266
3267     if (gst_value_subtract (subtraction, result, cur)) {
3268       GValue *temp = result;
3269
3270       result = subtraction;
3271       subtraction = temp;
3272       g_value_unset (subtraction);
3273     } else {
3274       g_value_unset (result);
3275       return FALSE;
3276     }
3277   }
3278   if (dest)
3279     gst_value_init_and_copy (dest, result);
3280   g_value_unset (result);
3281   return TRUE;
3282 }
3283
3284 static gboolean
3285 gst_value_subtract_fraction_fraction_range (GValue * dest,
3286     const GValue * minuend, const GValue * subtrahend)
3287 {
3288   const GValue *min = gst_value_get_fraction_range_min (subtrahend);
3289   const GValue *max = gst_value_get_fraction_range_max (subtrahend);
3290   GstValueCompareFunc compare;
3291
3292   if ((compare = gst_value_get_compare_func (minuend))) {
3293     /* subtracting a range from an fraction only works if the fraction
3294      * is not in the range */
3295     if (gst_value_compare_with_func (minuend, min, compare) ==
3296         GST_VALUE_LESS_THAN ||
3297         gst_value_compare_with_func (minuend, max, compare) ==
3298         GST_VALUE_GREATER_THAN) {
3299       /* and the result is the value */
3300       if (dest)
3301         gst_value_init_and_copy (dest, minuend);
3302       return TRUE;
3303     }
3304   }
3305   return FALSE;
3306 }
3307
3308 static gboolean
3309 gst_value_subtract_fraction_range_fraction (GValue * dest,
3310     const GValue * minuend, const GValue * subtrahend)
3311 {
3312   /* since we don't have open ranges, we cannot create a hole in
3313    * a range. We return the original range */
3314   if (dest)
3315     gst_value_init_and_copy (dest, minuend);
3316   return TRUE;
3317 }
3318
3319 static gboolean
3320 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
3321     const GValue * minuend, const GValue * subtrahend)
3322 {
3323   /* since we don't have open ranges, we have to approximate */
3324   /* done like with ints and doubles. Creates a list of 2 fraction ranges */
3325   const GValue *min1 = gst_value_get_fraction_range_min (minuend);
3326   const GValue *max2 = gst_value_get_fraction_range_max (minuend);
3327   const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
3328   const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
3329   gint cmp1, cmp2;
3330   GValue v1 = { 0, };
3331   GValue v2 = { 0, };
3332   GValue *pv1, *pv2;            /* yeah, hungarian! */
3333   GstValueCompareFunc compare;
3334
3335   g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
3336   g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
3337
3338   compare = gst_value_get_compare_func (min1);
3339   g_return_val_if_fail (compare, FALSE);
3340
3341   cmp1 = gst_value_compare_with_func (max2, max1, compare);
3342   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3343   if (cmp1 == GST_VALUE_LESS_THAN)
3344     max1 = max2;
3345   cmp1 = gst_value_compare_with_func (min1, min2, compare);
3346   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3347   if (cmp1 == GST_VALUE_GREATER_THAN)
3348     min2 = min1;
3349
3350   cmp1 = gst_value_compare_with_func (min1, max1, compare);
3351   cmp2 = gst_value_compare_with_func (min2, max2, compare);
3352
3353   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3354     pv1 = &v1;
3355     pv2 = &v2;
3356   } else if (cmp1 == GST_VALUE_LESS_THAN) {
3357     pv1 = dest;
3358     pv2 = NULL;
3359   } else if (cmp2 == GST_VALUE_LESS_THAN) {
3360     pv1 = NULL;
3361     pv2 = dest;
3362   } else {
3363     return FALSE;
3364   }
3365
3366   if (!dest)
3367     return TRUE;
3368
3369   if (cmp1 == GST_VALUE_LESS_THAN) {
3370     g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
3371     gst_value_set_fraction_range (pv1, min1, max1);
3372   }
3373   if (cmp2 == GST_VALUE_LESS_THAN) {
3374     g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
3375     gst_value_set_fraction_range (pv2, min2, max2);
3376   }
3377
3378   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3379     gst_value_list_concat (dest, pv1, pv2);
3380     g_value_unset (pv1);
3381     g_value_unset (pv2);
3382   }
3383   return TRUE;
3384 }
3385
3386
3387 /**************
3388  * comparison *
3389  **************/
3390
3391 /*
3392  * gst_value_get_compare_func:
3393  * @value1: a value to get the compare function for
3394  *
3395  * Determines the compare function to be used with values of the same type as
3396  * @value1. The function can be given to gst_value_compare_with_func().
3397  *
3398  * Returns: A #GstValueCompareFunc value
3399  */
3400 static GstValueCompareFunc
3401 gst_value_get_compare_func (const GValue * value1)
3402 {
3403   GstValueTable *table, *best = NULL;
3404   guint i;
3405   GType type1;
3406
3407   type1 = G_VALUE_TYPE (value1);
3408
3409   /* this is a fast check */
3410   best = gst_value_hash_lookup_type (type1);
3411
3412   /* slower checks */
3413   if (G_UNLIKELY (!best || !best->compare)) {
3414     guint len = gst_value_table->len;
3415
3416     best = NULL;
3417     for (i = 0; i < len; i++) {
3418       table = &g_array_index (gst_value_table, GstValueTable, i);
3419       if (table->compare && g_type_is_a (type1, table->type)) {
3420         if (!best || g_type_is_a (table->type, best->type))
3421           best = table;
3422       }
3423     }
3424   }
3425   if (G_LIKELY (best))
3426     return best->compare;
3427
3428   return NULL;
3429 }
3430
3431 /**
3432  * gst_value_can_compare:
3433  * @value1: a value to compare
3434  * @value2: another value to compare
3435  *
3436  * Determines if @value1 and @value2 can be compared.
3437  *
3438  * Returns: TRUE if the values can be compared
3439  */
3440 gboolean
3441 gst_value_can_compare (const GValue * value1, const GValue * value2)
3442 {
3443   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3444   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3445
3446   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3447     return FALSE;
3448
3449   return gst_value_get_compare_func (value1) != NULL;
3450 }
3451
3452 static gboolean
3453 gst_value_list_equals_range (const GValue * list, const GValue * value)
3454 {
3455   const GValue *first;
3456   guint list_size, n;
3457
3458   g_return_val_if_fail (G_IS_VALUE (list), FALSE);
3459   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
3460   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (list), FALSE);
3461
3462   /* TODO: compare against an empty list ? No type though... */
3463   list_size = VALUE_LIST_SIZE (list);
3464   if (list_size == 0)
3465     return FALSE;
3466
3467   /* compare the basic types - they have to match */
3468   first = VALUE_LIST_GET_VALUE (list, 0);
3469 #define CHECK_TYPES(type,prefix) \
3470   (prefix##_VALUE_HOLDS_##type(first) && GST_VALUE_HOLDS_##type##_RANGE (value))
3471   if (CHECK_TYPES (INT, G)) {
3472     const gint rmin = gst_value_get_int_range_min (value);
3473     const gint rmax = gst_value_get_int_range_max (value);
3474     /* note: this will overflow for min 0 and max INT_MAX, but this
3475        would only be equal to a list of INT_MAX elements, which seems
3476        very unlikely */
3477     if (list_size != rmax - rmin + 1)
3478       return FALSE;
3479     for (n = 0; n < list_size; ++n) {
3480       gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
3481       if (v < rmin || v > rmax) {
3482         return FALSE;
3483       }
3484     }
3485     return TRUE;
3486   } else if (CHECK_TYPES (INT64, G)) {
3487     const gint64 rmin = gst_value_get_int64_range_min (value);
3488     const gint64 rmax = gst_value_get_int64_range_max (value);
3489     GST_DEBUG ("List/range of int64s");
3490     if (list_size != rmax - rmin + 1)
3491       return FALSE;
3492     for (n = 0; n < list_size; ++n) {
3493       gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
3494       if (v < rmin || v > rmax)
3495         return FALSE;
3496     }
3497     return TRUE;
3498   }
3499 #undef CHECK_TYPES
3500
3501   /* other combinations don't make sense for equality */
3502   return FALSE;
3503 }
3504
3505 /**
3506  * gst_value_compare:
3507  * @value1: a value to compare
3508  * @value2: another value to compare
3509  *
3510  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
3511  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
3512  * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
3513  * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
3514  * If the values are equal, GST_VALUE_EQUAL is returned.
3515  *
3516  * Returns: comparison result
3517  */
3518 gint
3519 gst_value_compare (const GValue * value1, const GValue * value2)
3520 {
3521   GstValueCompareFunc compare;
3522   GType ltype;
3523
3524   g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
3525   g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
3526
3527   /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
3528      as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
3529   ltype = gst_value_list_get_type ();
3530   if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)) {
3531
3532     if (gst_value_list_equals_range (value1, value2)) {
3533       return GST_VALUE_EQUAL;
3534     } else if (gst_value_list_get_size (value1) == 1) {
3535       const GValue *elt;
3536
3537       elt = gst_value_list_get_value (value1, 0);
3538       return gst_value_compare (elt, value2);
3539     }
3540   } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)) {
3541     if (gst_value_list_equals_range (value2, value1)) {
3542       return GST_VALUE_EQUAL;
3543     } else if (gst_value_list_get_size (value2) == 1) {
3544       const GValue *elt;
3545
3546       elt = gst_value_list_get_value (value2, 0);
3547       return gst_value_compare (elt, value1);
3548     }
3549   }
3550
3551   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3552     return GST_VALUE_UNORDERED;
3553
3554   compare = gst_value_get_compare_func (value1);
3555   if (compare) {
3556     return compare (value1, value2);
3557   }
3558
3559   g_critical ("unable to compare values of type %s\n",
3560       g_type_name (G_VALUE_TYPE (value1)));
3561   return GST_VALUE_UNORDERED;
3562 }
3563
3564 /*
3565  * gst_value_compare_with_func:
3566  * @value1: a value to compare
3567  * @value2: another value to compare
3568  * @compare: compare function
3569  *
3570  * Compares @value1 and @value2 using the @compare function. Works like
3571  * gst_value_compare() but allows to save time determining the compare function
3572  * a multiple times. 
3573  *
3574  * Returns: comparison result
3575  */
3576 static gint
3577 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
3578     GstValueCompareFunc compare)
3579 {
3580   g_assert (compare);
3581
3582   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3583     return GST_VALUE_UNORDERED;
3584
3585   return compare (value1, value2);
3586 }
3587
3588 /* union */
3589
3590 /**
3591  * gst_value_can_union:
3592  * @value1: a value to union
3593  * @value2: another value to union
3594  *
3595  * Determines if @value1 and @value2 can be non-trivially unioned.
3596  * Any two values can be trivially unioned by adding both of them
3597  * to a GstValueList.  However, certain types have the possibility
3598  * to be unioned in a simpler way.  For example, an integer range
3599  * and an integer can be unioned if the integer is a subset of the
3600  * integer range.  If there is the possibility that two values can
3601  * be unioned, this function returns TRUE.
3602  *
3603  * Returns: TRUE if there is a function allowing the two values to
3604  * be unioned.
3605  */
3606 gboolean
3607 gst_value_can_union (const GValue * value1, const GValue * value2)
3608 {
3609   GstValueUnionInfo *union_info;
3610   guint i, len;
3611
3612   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3613   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3614
3615   len = gst_value_union_funcs->len;
3616
3617   for (i = 0; i < len; i++) {
3618     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
3619     if (union_info->type1 == G_VALUE_TYPE (value1) &&
3620         union_info->type2 == G_VALUE_TYPE (value2))
3621       return TRUE;
3622     if (union_info->type1 == G_VALUE_TYPE (value2) &&
3623         union_info->type2 == G_VALUE_TYPE (value1))
3624       return TRUE;
3625   }
3626
3627   return FALSE;
3628 }
3629
3630 /**
3631  * gst_value_union:
3632  * @dest: (out caller-allocates): the destination value
3633  * @value1: a value to union
3634  * @value2: another value to union
3635  *
3636  * Creates a GValue corresponding to the union of @value1 and @value2.
3637  *
3638  * Returns: always returns %TRUE
3639  */
3640 /* FIXME: change return type to 'void'? */
3641 gboolean
3642 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
3643 {
3644   GstValueUnionInfo *union_info;
3645   guint i, len;
3646
3647   g_return_val_if_fail (dest != NULL, FALSE);
3648   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3649   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3650
3651   len = gst_value_union_funcs->len;
3652
3653   for (i = 0; i < len; i++) {
3654     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
3655     if (union_info->type1 == G_VALUE_TYPE (value1) &&
3656         union_info->type2 == G_VALUE_TYPE (value2)) {
3657       if (union_info->func (dest, value1, value2)) {
3658         return TRUE;
3659       }
3660     }
3661     if (union_info->type1 == G_VALUE_TYPE (value2) &&
3662         union_info->type2 == G_VALUE_TYPE (value1)) {
3663       if (union_info->func (dest, value2, value1)) {
3664         return TRUE;
3665       }
3666     }
3667   }
3668
3669   gst_value_list_concat (dest, value1, value2);
3670   return TRUE;
3671 }
3672
3673 /**
3674  * gst_value_register_union_func:
3675  * @type1: a type to union
3676  * @type2: another type to union
3677  * @func: a function that implements creating a union between the two types
3678  *
3679  * Registers a union function that can create a union between #GValue items
3680  * of the type @type1 and @type2.
3681  *
3682  * Union functions should be registered at startup before any pipelines are
3683  * started, as gst_value_register_union_func() is not thread-safe and cannot
3684  * be used at the same time as gst_value_union() or gst_value_can_union().
3685  */
3686 void
3687 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
3688 {
3689   GstValueUnionInfo union_info;
3690
3691   union_info.type1 = type1;
3692   union_info.type2 = type2;
3693   union_info.func = func;
3694
3695   g_array_append_val (gst_value_union_funcs, union_info);
3696 }
3697
3698 /* intersection */
3699
3700 /**
3701  * gst_value_can_intersect:
3702  * @value1: a value to intersect
3703  * @value2: another value to intersect
3704  *
3705  * Determines if intersecting two values will produce a valid result.
3706  * Two values will produce a valid intersection if they have the same
3707  * type, or if there is a method (registered by
3708  * gst_value_register_intersect_func()) to calculate the intersection.
3709  *
3710  * Returns: TRUE if the values can intersect
3711  */
3712 gboolean
3713 gst_value_can_intersect (const GValue * value1, const GValue * value2)
3714 {
3715   GstValueIntersectInfo *intersect_info;
3716   guint i, len;
3717   GType ltype, type1, type2;
3718
3719   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3720   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3721
3722   ltype = gst_value_list_get_type ();
3723
3724   /* special cases */
3725   if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
3726     return TRUE;
3727
3728   type1 = G_VALUE_TYPE (value1);
3729   type2 = G_VALUE_TYPE (value2);
3730
3731   /* practically all GstValue types have a compare function (_can_compare=TRUE)
3732    * GstStructure and GstCaps have npot, but are intersectable */
3733   if (type1 == type2)
3734     return TRUE;
3735
3736   /* check registered intersect functions */
3737   len = gst_value_intersect_funcs->len;
3738   for (i = 0; i < len; i++) {
3739     intersect_info = &g_array_index (gst_value_intersect_funcs,
3740         GstValueIntersectInfo, i);
3741     if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
3742         (intersect_info->type1 == type2 && intersect_info->type2 == type1))
3743       return TRUE;
3744   }
3745
3746   return gst_value_can_compare (value1, value2);
3747 }
3748
3749 /**
3750  * gst_value_intersect:
3751  * @dest: (out caller-allocates): a uninitialized #GValue that will hold the calculated
3752  * intersection value. May be NULL if the resulting set if not needed.
3753  * @value1: a value to intersect
3754  * @value2: another value to intersect
3755  *
3756  * Calculates the intersection of two values.  If the values have
3757  * a non-empty intersection, the value representing the intersection
3758  * is placed in @dest, unless NULL.  If the intersection is non-empty,
3759  * @dest is not modified.
3760  *
3761  * Returns: TRUE if the intersection is non-empty
3762  */
3763 gboolean
3764 gst_value_intersect (GValue * dest, const GValue * value1,
3765     const GValue * value2)
3766 {
3767   GstValueIntersectInfo *intersect_info;
3768   guint i, len;
3769   GType ltype, type1, type2;
3770
3771   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3772   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3773
3774   ltype = gst_value_list_get_type ();
3775
3776   /* special cases first */
3777   if (G_VALUE_HOLDS (value1, ltype))
3778     return gst_value_intersect_list (dest, value1, value2);
3779   if (G_VALUE_HOLDS (value2, ltype))
3780     return gst_value_intersect_list (dest, value2, value1);
3781
3782   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
3783     if (dest)
3784       gst_value_init_and_copy (dest, value1);
3785     return TRUE;
3786   }
3787
3788   type1 = G_VALUE_TYPE (value1);
3789   type2 = G_VALUE_TYPE (value2);
3790
3791   len = gst_value_intersect_funcs->len;
3792   for (i = 0; i < len; i++) {
3793     intersect_info = &g_array_index (gst_value_intersect_funcs,
3794         GstValueIntersectInfo, i);
3795     if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
3796       return intersect_info->func (dest, value1, value2);
3797     }
3798     if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
3799       return intersect_info->func (dest, value2, value1);
3800     }
3801   }
3802   return FALSE;
3803 }
3804
3805
3806
3807 /**
3808  * gst_value_register_intersect_func:
3809  * @type1: the first type to intersect
3810  * @type2: the second type to intersect
3811  * @func: the intersection function
3812  *
3813  * Registers a function that is called to calculate the intersection
3814  * of the values having the types @type1 and @type2.
3815  *
3816  * Intersect functions should be registered at startup before any pipelines are
3817  * started, as gst_value_register_intersect_func() is not thread-safe and
3818  * cannot be used at the same time as gst_value_intersect() or
3819  * gst_value_can_intersect().
3820  */
3821 void
3822 gst_value_register_intersect_func (GType type1, GType type2,
3823     GstValueIntersectFunc func)
3824 {
3825   GstValueIntersectInfo intersect_info;
3826
3827   intersect_info.type1 = type1;
3828   intersect_info.type2 = type2;
3829   intersect_info.func = func;
3830
3831   g_array_append_val (gst_value_intersect_funcs, intersect_info);
3832 }
3833
3834
3835 /* subtraction */
3836
3837 /**
3838  * gst_value_subtract:
3839  * @dest: (out caller-allocates): the destination value for the result if the
3840  *     subtraction is not empty. May be NULL, in which case the resulting set
3841  *     will not be computed, which can give a fair speedup.
3842  * @minuend: the value to subtract from
3843  * @subtrahend: the value to subtract
3844  *
3845  * Subtracts @subtrahend from @minuend and stores the result in @dest.
3846  * Note that this means subtraction as in sets, not as in mathematics.
3847  *
3848  * Returns: %TRUE if the subtraction is not empty
3849  */
3850 gboolean
3851 gst_value_subtract (GValue * dest, const GValue * minuend,
3852     const GValue * subtrahend)
3853 {
3854   GstValueSubtractInfo *info;
3855   guint i, len;
3856   GType ltype, mtype, stype;
3857
3858   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
3859   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
3860
3861   ltype = gst_value_list_get_type ();
3862
3863   /* special cases first */
3864   if (G_VALUE_HOLDS (minuend, ltype))
3865     return gst_value_subtract_from_list (dest, minuend, subtrahend);
3866   if (G_VALUE_HOLDS (subtrahend, ltype))
3867     return gst_value_subtract_list (dest, minuend, subtrahend);
3868
3869   mtype = G_VALUE_TYPE (minuend);
3870   stype = G_VALUE_TYPE (subtrahend);
3871
3872   len = gst_value_subtract_funcs->len;
3873   for (i = 0; i < len; i++) {
3874     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3875     if (info->minuend == mtype && info->subtrahend == stype) {
3876       return info->func (dest, minuend, subtrahend);
3877     }
3878   }
3879
3880   if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3881     if (dest)
3882       gst_value_init_and_copy (dest, minuend);
3883     return TRUE;
3884   }
3885
3886   return FALSE;
3887 }
3888
3889 #if 0
3890 gboolean
3891 gst_value_subtract (GValue * dest, const GValue * minuend,
3892     const GValue * subtrahend)
3893 {
3894   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3895
3896   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
3897       gst_value_serialize (subtrahend),
3898       ret ? gst_value_serialize (dest) : "---");
3899   return ret;
3900 }
3901 #endif
3902
3903 /**
3904  * gst_value_can_subtract:
3905  * @minuend: the value to subtract from
3906  * @subtrahend: the value to subtract
3907  *
3908  * Checks if it's possible to subtract @subtrahend from @minuend.
3909  *
3910  * Returns: TRUE if a subtraction is possible
3911  */
3912 gboolean
3913 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3914 {
3915   GstValueSubtractInfo *info;
3916   guint i, len;
3917   GType ltype, mtype, stype;
3918
3919   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
3920   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
3921
3922   ltype = gst_value_list_get_type ();
3923
3924   /* special cases */
3925   if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
3926     return TRUE;
3927
3928   mtype = G_VALUE_TYPE (minuend);
3929   stype = G_VALUE_TYPE (subtrahend);
3930
3931   len = gst_value_subtract_funcs->len;
3932   for (i = 0; i < len; i++) {
3933     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3934     if (info->minuend == mtype && info->subtrahend == stype)
3935       return TRUE;
3936   }
3937
3938   return gst_value_can_compare (minuend, subtrahend);
3939 }
3940
3941 /**
3942  * gst_value_register_subtract_func:
3943  * @minuend_type: type of the minuend
3944  * @subtrahend_type: type of the subtrahend
3945  * @func: function to use
3946  *
3947  * Registers @func as a function capable of subtracting the values of
3948  * @subtrahend_type from values of @minuend_type.
3949  *
3950  * Subtract functions should be registered at startup before any pipelines are
3951  * started, as gst_value_register_subtract_func() is not thread-safe and
3952  * cannot be used at the same time as gst_value_subtract().
3953  */
3954 void
3955 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3956     GstValueSubtractFunc func)
3957 {
3958   GstValueSubtractInfo info;
3959
3960   /* one type must be unfixed, other subtractions can be done as comparisons */
3961   g_return_if_fail (!gst_type_is_fixed (minuend_type)
3962       || !gst_type_is_fixed (subtrahend_type));
3963
3964   info.minuend = minuend_type;
3965   info.subtrahend = subtrahend_type;
3966   info.func = func;
3967
3968   g_array_append_val (gst_value_subtract_funcs, info);
3969 }
3970
3971 /**
3972  * gst_value_register:
3973  * @table: structure containing functions to register
3974  *
3975  * Registers functions to perform calculations on #GValue items of a given
3976  * type. Each type can only be added once.
3977  */
3978 void
3979 gst_value_register (const GstValueTable * table)
3980 {
3981   GstValueTable *found;
3982
3983   g_return_if_fail (table != NULL);
3984
3985   g_array_append_val (gst_value_table, *table);
3986
3987   found = gst_value_hash_lookup_type (table->type);
3988   if (found)
3989     g_warning ("adding type %s multiple times", g_type_name (table->type));
3990
3991   /* FIXME: we're not really doing the const justice, we assume the table is
3992    * static */
3993   gst_value_hash_add_type (table->type, table);
3994 }
3995
3996 /**
3997  * gst_value_init_and_copy:
3998  * @dest: (out caller-allocates): the target value
3999  * @src: the source value
4000  *
4001  * Initialises the target value to be of the same type as source and then copies
4002  * the contents from source to target.
4003  */
4004 void
4005 gst_value_init_and_copy (GValue * dest, const GValue * src)
4006 {
4007   g_return_if_fail (G_IS_VALUE (src));
4008   g_return_if_fail (dest != NULL);
4009
4010   g_value_init (dest, G_VALUE_TYPE (src));
4011   g_value_copy (src, dest);
4012 }
4013
4014 /**
4015  * gst_value_serialize:
4016  * @value: a #GValue to serialize
4017  *
4018  * tries to transform the given @value into a string representation that allows
4019  * getting back this string later on using gst_value_deserialize().
4020  *
4021  * Free-function: g_free
4022  *
4023  * Returns: (transfer full): the serialization for @value or NULL if none exists
4024  */
4025 gchar *
4026 gst_value_serialize (const GValue * value)
4027 {
4028   guint i, len;
4029   GValue s_val = { 0 };
4030   GstValueTable *table, *best;
4031   gchar *s;
4032   GType type;
4033
4034   g_return_val_if_fail (G_IS_VALUE (value), NULL);
4035
4036   type = G_VALUE_TYPE (value);
4037
4038   best = gst_value_hash_lookup_type (type);
4039
4040   if (G_UNLIKELY (!best || !best->serialize)) {
4041     len = gst_value_table->len;
4042     best = NULL;
4043     for (i = 0; i < len; i++) {
4044       table = &g_array_index (gst_value_table, GstValueTable, i);
4045       if (table->serialize && g_type_is_a (type, table->type)) {
4046         if (!best || g_type_is_a (table->type, best->type))
4047           best = table;
4048       }
4049     }
4050   }
4051   if (G_LIKELY (best))
4052     return best->serialize (value);
4053
4054   g_value_init (&s_val, G_TYPE_STRING);
4055   if (g_value_transform (value, &s_val)) {
4056     s = gst_string_wrap (g_value_get_string (&s_val));
4057   } else {
4058     s = NULL;
4059   }
4060   g_value_unset (&s_val);
4061
4062   return s;
4063 }
4064
4065 /**
4066  * gst_value_deserialize:
4067  * @dest: (out caller-allocates): #GValue to fill with contents of
4068  *     deserialization
4069  * @src: string to deserialize
4070  *
4071  * Tries to deserialize a string into the type specified by the given GValue.
4072  * If the operation succeeds, TRUE is returned, FALSE otherwise.
4073  *
4074  * Returns: TRUE on success
4075  */
4076 gboolean
4077 gst_value_deserialize (GValue * dest, const gchar * src)
4078 {
4079   GstValueTable *table, *best;
4080   guint i, len;
4081   GType type;
4082
4083   g_return_val_if_fail (src != NULL, FALSE);
4084   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
4085
4086   type = G_VALUE_TYPE (dest);
4087
4088   best = gst_value_hash_lookup_type (type);
4089   if (G_UNLIKELY (!best || !best->deserialize)) {
4090     len = gst_value_table->len;
4091     best = NULL;
4092     for (i = 0; i < len; i++) {
4093       table = &g_array_index (gst_value_table, GstValueTable, i);
4094       if (table->deserialize && g_type_is_a (type, table->type)) {
4095         if (!best || g_type_is_a (table->type, best->type))
4096           best = table;
4097       }
4098     }
4099   }
4100   if (G_LIKELY (best))
4101     return best->deserialize (dest, src);
4102
4103   return FALSE;
4104 }
4105
4106 /**
4107  * gst_value_is_fixed:
4108  * @value: the #GValue to check
4109  *
4110  * Tests if the given GValue, if available in a GstStructure (or any other
4111  * container) contains a "fixed" (which means: one value) or an "unfixed"
4112  * (which means: multiple possible values, such as data lists or data
4113  * ranges) value.
4114  *
4115  * Returns: true if the value is "fixed".
4116  */
4117
4118 gboolean
4119 gst_value_is_fixed (const GValue * value)
4120 {
4121   GType type;
4122
4123   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
4124
4125   type = G_VALUE_TYPE (value);
4126
4127   /* the most common types are just basic plain glib types */
4128   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
4129     return TRUE;
4130   }
4131
4132   if (type == GST_TYPE_ARRAY) {
4133     gint size, n;
4134     const GValue *kid;
4135
4136     /* check recursively */
4137     size = gst_value_array_get_size (value);
4138     for (n = 0; n < size; n++) {
4139       kid = gst_value_array_get_value (value, n);
4140       if (!gst_value_is_fixed (kid))
4141         return FALSE;
4142     }
4143     return TRUE;
4144   }
4145   return gst_type_is_fixed (type);
4146 }
4147
4148 /************
4149  * fraction *
4150  ************/
4151
4152 /* helper functions */
4153 static void
4154 gst_value_init_fraction (GValue * value)
4155 {
4156   value->data[0].v_int = 0;
4157   value->data[1].v_int = 1;
4158 }
4159
4160 static void
4161 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
4162 {
4163   dest_value->data[0].v_int = src_value->data[0].v_int;
4164   dest_value->data[1].v_int = src_value->data[1].v_int;
4165 }
4166
4167 static gchar *
4168 gst_value_collect_fraction (GValue * value, guint n_collect_values,
4169     GTypeCValue * collect_values, guint collect_flags)
4170 {
4171   if (n_collect_values != 2)
4172     return g_strdup_printf ("not enough value locations for `%s' passed",
4173         G_VALUE_TYPE_NAME (value));
4174   if (collect_values[1].v_int == 0)
4175     return g_strdup_printf ("passed '0' as denominator for `%s'",
4176         G_VALUE_TYPE_NAME (value));
4177   if (collect_values[0].v_int < -G_MAXINT)
4178     return
4179         g_strdup_printf
4180         ("passed value smaller than -G_MAXINT as numerator for `%s'",
4181         G_VALUE_TYPE_NAME (value));
4182   if (collect_values[1].v_int < -G_MAXINT)
4183     return
4184         g_strdup_printf
4185         ("passed value smaller than -G_MAXINT as denominator for `%s'",
4186         G_VALUE_TYPE_NAME (value));
4187
4188   gst_value_set_fraction (value,
4189       collect_values[0].v_int, collect_values[1].v_int);
4190
4191   return NULL;
4192 }
4193
4194 static gchar *
4195 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
4196     GTypeCValue * collect_values, guint collect_flags)
4197 {
4198   gint *numerator = collect_values[0].v_pointer;
4199   gint *denominator = collect_values[1].v_pointer;
4200
4201   if (!numerator)
4202     return g_strdup_printf ("numerator for `%s' passed as NULL",
4203         G_VALUE_TYPE_NAME (value));
4204   if (!denominator)
4205     return g_strdup_printf ("denominator for `%s' passed as NULL",
4206         G_VALUE_TYPE_NAME (value));
4207
4208   *numerator = value->data[0].v_int;
4209   *denominator = value->data[1].v_int;
4210
4211   return NULL;
4212 }
4213
4214 /**
4215  * gst_value_set_fraction:
4216  * @value: a GValue initialized to #GST_TYPE_FRACTION
4217  * @numerator: the numerator of the fraction
4218  * @denominator: the denominator of the fraction
4219  *
4220  * Sets @value to the fraction specified by @numerator over @denominator.
4221  * The fraction gets reduced to the smallest numerator and denominator,
4222  * and if necessary the sign is moved to the numerator.
4223  */
4224 void
4225 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
4226 {
4227   gint gcd = 0;
4228
4229   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
4230   g_return_if_fail (denominator != 0);
4231   g_return_if_fail (denominator >= -G_MAXINT);
4232   g_return_if_fail (numerator >= -G_MAXINT);
4233
4234   /* normalize sign */
4235   if (denominator < 0) {
4236     numerator = -numerator;
4237     denominator = -denominator;
4238   }
4239
4240   /* check for reduction */
4241   gcd = gst_util_greatest_common_divisor (numerator, denominator);
4242   if (gcd) {
4243     numerator /= gcd;
4244     denominator /= gcd;
4245   }
4246
4247   g_assert (denominator > 0);
4248
4249   value->data[0].v_int = numerator;
4250   value->data[1].v_int = denominator;
4251 }
4252
4253 /**
4254  * gst_value_get_fraction_numerator:
4255  * @value: a GValue initialized to #GST_TYPE_FRACTION
4256  *
4257  * Gets the numerator of the fraction specified by @value.
4258  *
4259  * Returns: the numerator of the fraction.
4260  */
4261 gint
4262 gst_value_get_fraction_numerator (const GValue * value)
4263 {
4264   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
4265
4266   return value->data[0].v_int;
4267 }
4268
4269 /**
4270  * gst_value_get_fraction_denominator:
4271  * @value: a GValue initialized to #GST_TYPE_FRACTION
4272  *
4273  * Gets the denominator of the fraction specified by @value.
4274  *
4275  * Returns: the denominator of the fraction.
4276  */
4277 gint
4278 gst_value_get_fraction_denominator (const GValue * value)
4279 {
4280   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
4281
4282   return value->data[1].v_int;
4283 }
4284
4285 /**
4286  * gst_value_fraction_multiply:
4287  * @product: a GValue initialized to #GST_TYPE_FRACTION
4288  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
4289  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
4290  *
4291  * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
4292  * @product to the product of the two fractions.
4293  *
4294  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4295  */
4296 gboolean
4297 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
4298     const GValue * factor2)
4299 {
4300   gint n1, n2, d1, d2;
4301   gint res_n, res_d;
4302
4303   g_return_val_if_fail (product != NULL, FALSE);
4304   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
4305   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
4306
4307   n1 = factor1->data[0].v_int;
4308   n2 = factor2->data[0].v_int;
4309   d1 = factor1->data[1].v_int;
4310   d2 = factor2->data[1].v_int;
4311
4312   if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
4313     return FALSE;
4314
4315   gst_value_set_fraction (product, res_n, res_d);
4316
4317   return TRUE;
4318 }
4319
4320 /**
4321  * gst_value_fraction_subtract:
4322  * @dest: a GValue initialized to #GST_TYPE_FRACTION
4323  * @minuend: a GValue initialized to #GST_TYPE_FRACTION
4324  * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
4325  *
4326  * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
4327  *
4328  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4329  */
4330 gboolean
4331 gst_value_fraction_subtract (GValue * dest,
4332     const GValue * minuend, const GValue * subtrahend)
4333 {
4334   gint n1, n2, d1, d2;
4335   gint res_n, res_d;
4336
4337   g_return_val_if_fail (dest != NULL, FALSE);
4338   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
4339   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
4340
4341   n1 = minuend->data[0].v_int;
4342   n2 = subtrahend->data[0].v_int;
4343   d1 = minuend->data[1].v_int;
4344   d2 = subtrahend->data[1].v_int;
4345
4346   if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
4347     return FALSE;
4348   gst_value_set_fraction (dest, res_n, res_d);
4349
4350   return TRUE;
4351 }
4352
4353 static gchar *
4354 gst_value_serialize_fraction (const GValue * value)
4355 {
4356   gint32 numerator = value->data[0].v_int;
4357   gint32 denominator = value->data[1].v_int;
4358   gboolean positive = TRUE;
4359
4360   /* get the sign and make components absolute */
4361   if (numerator < 0) {
4362     numerator = -numerator;
4363     positive = !positive;
4364   }
4365   if (denominator < 0) {
4366     denominator = -denominator;
4367     positive = !positive;
4368   }
4369
4370   return g_strdup_printf ("%s%d/%d",
4371       positive ? "" : "-", numerator, denominator);
4372 }
4373
4374 static gboolean
4375 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
4376 {
4377   gint num, den;
4378   gint num_chars;
4379
4380   if (G_UNLIKELY (s == NULL))
4381     return FALSE;
4382
4383   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
4384     return FALSE;
4385
4386   if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
4387     if (s[num_chars] != 0)
4388       return FALSE;
4389     if (den == 0)
4390       return FALSE;
4391
4392     gst_value_set_fraction (dest, num, den);
4393     return TRUE;
4394   } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
4395     gst_value_set_fraction (dest, 1, G_MAXINT);
4396     return TRUE;
4397   } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
4398     if (s[num_chars] != 0)
4399       return FALSE;
4400     gst_value_set_fraction (dest, num, 1);
4401     return TRUE;
4402   } else if (g_ascii_strcasecmp (s, "min") == 0) {
4403     gst_value_set_fraction (dest, -G_MAXINT, 1);
4404     return TRUE;
4405   } else if (g_ascii_strcasecmp (s, "max") == 0) {
4406     gst_value_set_fraction (dest, G_MAXINT, 1);
4407     return TRUE;
4408   }
4409
4410   return FALSE;
4411 }
4412
4413 static void
4414 gst_value_transform_fraction_string (const GValue * src_value,
4415     GValue * dest_value)
4416 {
4417   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
4418 }
4419
4420 static void
4421 gst_value_transform_string_fraction (const GValue * src_value,
4422     GValue * dest_value)
4423 {
4424   if (!gst_value_deserialize_fraction (dest_value,
4425           src_value->data[0].v_pointer))
4426     /* If the deserialize fails, ensure we leave the fraction in a
4427      * valid, if incorrect, state */
4428     gst_value_set_fraction (dest_value, 0, 1);
4429 }
4430
4431 static void
4432 gst_value_transform_double_fraction (const GValue * src_value,
4433     GValue * dest_value)
4434 {
4435   gdouble src = g_value_get_double (src_value);
4436   gint n, d;
4437
4438   gst_util_double_to_fraction (src, &n, &d);
4439   gst_value_set_fraction (dest_value, n, d);
4440 }
4441
4442 static void
4443 gst_value_transform_float_fraction (const GValue * src_value,
4444     GValue * dest_value)
4445 {
4446   gfloat src = g_value_get_float (src_value);
4447   gint n, d;
4448
4449   gst_util_double_to_fraction (src, &n, &d);
4450   gst_value_set_fraction (dest_value, n, d);
4451 }
4452
4453 static void
4454 gst_value_transform_fraction_double (const GValue * src_value,
4455     GValue * dest_value)
4456 {
4457   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
4458       ((double) src_value->data[1].v_int);
4459 }
4460
4461 static void
4462 gst_value_transform_fraction_float (const GValue * src_value,
4463     GValue * dest_value)
4464 {
4465   dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
4466       ((float) src_value->data[1].v_int);
4467 }
4468
4469 static gint
4470 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
4471 {
4472   gint n1, n2;
4473   gint d1, d2;
4474   gint ret;
4475
4476   n1 = value1->data[0].v_int;
4477   n2 = value2->data[0].v_int;
4478   d1 = value1->data[1].v_int;
4479   d2 = value2->data[1].v_int;
4480
4481   /* fractions are reduced when set, so we can quickly see if they're equal */
4482   if (n1 == n2 && d1 == d2)
4483     return GST_VALUE_EQUAL;
4484
4485   if (d1 == 0 && d2 == 0)
4486     return GST_VALUE_UNORDERED;
4487   else if (d1 == 0)
4488     return GST_VALUE_GREATER_THAN;
4489   else if (d2 == 0)
4490     return GST_VALUE_LESS_THAN;
4491
4492   ret = gst_util_fraction_compare (n1, d1, n2, d2);
4493   if (ret == -1)
4494     return GST_VALUE_LESS_THAN;
4495   else if (ret == 1)
4496     return GST_VALUE_GREATER_THAN;
4497
4498   /* Equality can't happen here because we check for that
4499    * first already */
4500   g_return_val_if_reached (GST_VALUE_UNORDERED);
4501 }
4502
4503 /*********
4504  * GDate *
4505  *********/
4506
4507 /**
4508  * gst_value_set_date:
4509  * @value: a GValue initialized to GST_TYPE_DATE
4510  * @date: the date to set the value to
4511  *
4512  * Sets the contents of @value to correspond to @date.  The actual
4513  * #GDate structure is copied before it is used.
4514  */
4515 void
4516 gst_value_set_date (GValue * value, const GDate * date)
4517 {
4518   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
4519   g_return_if_fail (g_date_valid (date));
4520
4521   g_value_set_boxed (value, date);
4522 }
4523
4524 /**
4525  * gst_value_get_date:
4526  * @value: a GValue initialized to GST_TYPE_DATE
4527  *
4528  * Gets the contents of @value.
4529  *
4530  * Returns: (transfer none): the contents of @value
4531  */
4532 const GDate *
4533 gst_value_get_date (const GValue * value)
4534 {
4535   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
4536
4537   return (const GDate *) g_value_get_boxed (value);
4538 }
4539
4540 static gpointer
4541 gst_date_copy (gpointer boxed)
4542 {
4543   const GDate *date = (const GDate *) boxed;
4544
4545   if (!g_date_valid (date)) {
4546     GST_WARNING ("invalid GDate");
4547     return NULL;
4548   }
4549
4550   return g_date_new_julian (g_date_get_julian (date));
4551 }
4552
4553 static gint
4554 gst_value_compare_date (const GValue * value1, const GValue * value2)
4555 {
4556   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
4557   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
4558   guint32 j1, j2;
4559
4560   if (date1 == date2)
4561     return GST_VALUE_EQUAL;
4562
4563   if ((date1 == NULL || !g_date_valid (date1))
4564       && (date2 != NULL && g_date_valid (date2))) {
4565     return GST_VALUE_LESS_THAN;
4566   }
4567
4568   if ((date2 == NULL || !g_date_valid (date2))
4569       && (date1 != NULL && g_date_valid (date1))) {
4570     return GST_VALUE_GREATER_THAN;
4571   }
4572
4573   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
4574       || !g_date_valid (date2)) {
4575     return GST_VALUE_UNORDERED;
4576   }
4577
4578   j1 = g_date_get_julian (date1);
4579   j2 = g_date_get_julian (date2);
4580
4581   if (j1 == j2)
4582     return GST_VALUE_EQUAL;
4583   else if (j1 < j2)
4584     return GST_VALUE_LESS_THAN;
4585   else
4586     return GST_VALUE_GREATER_THAN;
4587 }
4588
4589 static gchar *
4590 gst_value_serialize_date (const GValue * val)
4591 {
4592   const GDate *date = (const GDate *) g_value_get_boxed (val);
4593
4594   if (date == NULL || !g_date_valid (date))
4595     return g_strdup ("9999-99-99");
4596
4597   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
4598       g_date_get_month (date), g_date_get_day (date));
4599 }
4600
4601 static gboolean
4602 gst_value_deserialize_date (GValue * dest, const gchar * s)
4603 {
4604   guint year, month, day;
4605
4606   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
4607     return FALSE;
4608
4609   if (!g_date_valid_dmy (day, month, year))
4610     return FALSE;
4611
4612   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
4613   return TRUE;
4614 }
4615
4616 /*************
4617  * GstDateTime *
4618  *************/
4619
4620 static gint
4621 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
4622 {
4623   const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
4624   const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
4625   gint ret;
4626
4627   if (date1 == date2)
4628     return GST_VALUE_EQUAL;
4629
4630   if ((date1 == NULL) && (date2 != NULL)) {
4631     return GST_VALUE_LESS_THAN;
4632   }
4633   if ((date2 == NULL) && (date1 != NULL)) {
4634     return GST_VALUE_LESS_THAN;
4635   }
4636
4637   ret = priv_gst_date_time_compare (date1, date2);
4638
4639   if (ret == 0)
4640     return GST_VALUE_EQUAL;
4641   else if (ret < 0)
4642     return GST_VALUE_LESS_THAN;
4643   else
4644     return GST_VALUE_GREATER_THAN;
4645 }
4646
4647 static gchar *
4648 gst_value_serialize_date_time (const GValue * val)
4649 {
4650   GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
4651   gfloat offset;
4652   gint tzhour, tzminute;
4653
4654   if (date == NULL)
4655     return g_strdup ("null");
4656
4657   offset = gst_date_time_get_time_zone_offset (date);
4658
4659   tzhour = (gint) ABS (offset);
4660   tzminute = (gint) ((ABS (offset) - tzhour) * 60);
4661
4662   return g_strdup_printf ("\"%04d-%02d-%02dT%02d:%02d:%02d.%06d"
4663       "%c%02d%02d\"", gst_date_time_get_year (date),
4664       gst_date_time_get_month (date), gst_date_time_get_day (date),
4665       gst_date_time_get_hour (date), gst_date_time_get_minute (date),
4666       gst_date_time_get_second (date), gst_date_time_get_microsecond (date),
4667       offset >= 0 ? '+' : '-', tzhour, tzminute);
4668 }
4669
4670 static gboolean
4671 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
4672 {
4673   gint year, month, day, hour, minute, second, usecond;
4674   gchar signal;
4675   gint offset = 0;
4676   gfloat tzoffset = 0;
4677   gint ret;
4678
4679   if (!s || strcmp (s, "null") == 0) {
4680     return FALSE;
4681   }
4682
4683   ret = sscanf (s, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%04d",
4684       &year, &month, &day, &hour, &minute, &second, &usecond, &signal, &offset);
4685   if (ret >= 9) {
4686     tzoffset = (offset / 100) + ((offset % 100) / 60.0);
4687     if (signal == '-')
4688       tzoffset = -tzoffset;
4689   } else
4690     return FALSE;
4691
4692   g_value_take_boxed (dest, gst_date_time_new (tzoffset, year, month, day, hour,
4693           minute, second + (usecond / 1000000.0)));
4694   return TRUE;
4695 }
4696
4697 static void
4698 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
4699 {
4700   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
4701 }
4702
4703 static void
4704 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
4705 {
4706   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
4707 }
4708
4709 static void
4710 gst_value_transform_object_string (const GValue * src_value,
4711     GValue * dest_value)
4712 {
4713   GstObject *obj;
4714   gchar *str;
4715
4716   obj = g_value_get_object (src_value);
4717   if (obj) {
4718     str =
4719         g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
4720         GST_OBJECT_NAME (obj));
4721   } else {
4722     str = g_strdup ("NULL");
4723   }
4724
4725   dest_value->data[0].v_pointer = str;
4726 }
4727
4728 static GTypeInfo _info = {
4729   0,
4730   NULL,
4731   NULL,
4732   NULL,
4733   NULL,
4734   NULL,
4735   0,
4736   0,
4737   NULL,
4738   NULL,
4739 };
4740
4741 static GTypeFundamentalInfo _finfo = {
4742   0
4743 };
4744
4745 #define FUNC_VALUE_GET_TYPE(type, name)                         \
4746 GType gst_ ## type ## _get_type (void)                          \
4747 {                                                               \
4748   static volatile GType gst_ ## type ## _type = 0;                       \
4749                                                                 \
4750   if (g_once_init_enter (&gst_ ## type ## _type)) {             \
4751     GType _type;                                        \
4752     _info.value_table = & _gst_ ## type ## _value_table;        \
4753     _type = g_type_register_fundamental (       \
4754         g_type_fundamental_next (),                             \
4755         name, &_info, &_finfo, 0);                              \
4756     g_once_init_leave(&gst_ ## type ## _type, _type);   \
4757   }                                                             \
4758                                                                 \
4759   return gst_ ## type ## _type;                                 \
4760 }
4761
4762 static const GTypeValueTable _gst_fourcc_value_table = {
4763   gst_value_init_fourcc,
4764   NULL,
4765   gst_value_copy_fourcc,
4766   NULL,
4767   (char *) "i",
4768   gst_value_collect_fourcc,
4769   (char *) "p",
4770   gst_value_lcopy_fourcc
4771 };
4772
4773 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
4774
4775 static const GTypeValueTable _gst_int_range_value_table = {
4776   gst_value_init_int_range,
4777   NULL,
4778   gst_value_copy_int_range,
4779   NULL,
4780   (char *) "ii",
4781   gst_value_collect_int_range,
4782   (char *) "pp",
4783   gst_value_lcopy_int_range
4784 };
4785
4786 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
4787
4788 static const GTypeValueTable _gst_int64_range_value_table = {
4789   gst_value_init_int64_range,
4790   NULL,
4791   gst_value_copy_int64_range,
4792   NULL,
4793   (char *) "qq",
4794   gst_value_collect_int64_range,
4795   (char *) "pp",
4796   gst_value_lcopy_int64_range
4797 };
4798
4799 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
4800
4801 static const GTypeValueTable _gst_double_range_value_table = {
4802   gst_value_init_double_range,
4803   NULL,
4804   gst_value_copy_double_range,
4805   NULL,
4806   (char *) "dd",
4807   gst_value_collect_double_range,
4808   (char *) "pp",
4809   gst_value_lcopy_double_range
4810 };
4811
4812 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
4813
4814 static const GTypeValueTable _gst_fraction_range_value_table = {
4815   gst_value_init_fraction_range,
4816   gst_value_free_fraction_range,
4817   gst_value_copy_fraction_range,
4818   NULL,
4819   (char *) "iiii",
4820   gst_value_collect_fraction_range,
4821   (char *) "pppp",
4822   gst_value_lcopy_fraction_range
4823 };
4824
4825 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
4826
4827 static const GTypeValueTable _gst_value_list_value_table = {
4828   gst_value_init_list_or_array,
4829   gst_value_free_list_or_array,
4830   gst_value_copy_list_or_array,
4831   gst_value_list_or_array_peek_pointer,
4832   (char *) "p",
4833   gst_value_collect_list_or_array,
4834   (char *) "p",
4835   gst_value_lcopy_list_or_array
4836 };
4837
4838 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
4839
4840 static const GTypeValueTable _gst_value_array_value_table = {
4841   gst_value_init_list_or_array,
4842   gst_value_free_list_or_array,
4843   gst_value_copy_list_or_array,
4844   gst_value_list_or_array_peek_pointer,
4845   (char *) "p",
4846   gst_value_collect_list_or_array,
4847   (char *) "p",
4848   gst_value_lcopy_list_or_array
4849 };
4850
4851 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
4852
4853 static const GTypeValueTable _gst_fraction_value_table = {
4854   gst_value_init_fraction,
4855   NULL,
4856   gst_value_copy_fraction,
4857   NULL,
4858   (char *) "ii",
4859   gst_value_collect_fraction,
4860   (char *) "pp",
4861   gst_value_lcopy_fraction
4862 };
4863
4864 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
4865
4866
4867 GType
4868 gst_date_get_type (void)
4869 {
4870   static GType gst_date_type = 0;
4871
4872   if (G_UNLIKELY (gst_date_type == 0)) {
4873     /* FIXME 0.11: we require GLib 2.8 already
4874      * Not using G_TYPE_DATE here on purpose, even if we could
4875      * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
4876      * serialised strings to have different type strings depending
4877      * on what version is used, so FIXME when we require GLib-2.8 */
4878     gst_date_type = g_boxed_type_register_static ("GstDate",
4879         (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
4880   }
4881
4882   return gst_date_type;
4883 }
4884
4885 GType
4886 gst_date_time_get_type (void)
4887 {
4888   static GType gst_date_time_type = 0;
4889
4890   if (G_UNLIKELY (gst_date_time_type == 0)) {
4891     gst_date_time_type = g_boxed_type_register_static ("GstDateTime",
4892         (GBoxedCopyFunc) gst_date_time_ref,
4893         (GBoxedFreeFunc) gst_date_time_unref);
4894   }
4895
4896   return gst_date_time_type;
4897 }
4898
4899
4900 void
4901 _gst_value_initialize (void)
4902 {
4903   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
4904   gst_value_hash = g_hash_table_new (NULL, NULL);
4905   gst_value_union_funcs = g_array_new (FALSE, FALSE,
4906       sizeof (GstValueUnionInfo));
4907   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
4908       sizeof (GstValueIntersectInfo));
4909   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4910       sizeof (GstValueSubtractInfo));
4911
4912   {
4913     static GstValueTable gst_value = {
4914       0,
4915       gst_value_compare_fourcc,
4916       gst_value_serialize_fourcc,
4917       gst_value_deserialize_fourcc,
4918     };
4919
4920     gst_value.type = gst_fourcc_get_type ();
4921     gst_value_register (&gst_value);
4922   }
4923
4924   {
4925     static GstValueTable gst_value = {
4926       0,
4927       gst_value_compare_int_range,
4928       gst_value_serialize_int_range,
4929       gst_value_deserialize_int_range,
4930     };
4931
4932     gst_value.type = gst_int_range_get_type ();
4933     gst_value_register (&gst_value);
4934   }
4935
4936   {
4937     static GstValueTable gst_value = {
4938       0,
4939       gst_value_compare_int64_range,
4940       gst_value_serialize_int64_range,
4941       gst_value_deserialize_int64_range,
4942     };
4943
4944     gst_value.type = gst_int64_range_get_type ();
4945     gst_value_register (&gst_value);
4946   }
4947
4948   {
4949     static GstValueTable gst_value = {
4950       0,
4951       gst_value_compare_double_range,
4952       gst_value_serialize_double_range,
4953       gst_value_deserialize_double_range,
4954     };
4955
4956     gst_value.type = gst_double_range_get_type ();
4957     gst_value_register (&gst_value);
4958   }
4959
4960   {
4961     static GstValueTable gst_value = {
4962       0,
4963       gst_value_compare_fraction_range,
4964       gst_value_serialize_fraction_range,
4965       gst_value_deserialize_fraction_range,
4966     };
4967
4968     gst_value.type = gst_fraction_range_get_type ();
4969     gst_value_register (&gst_value);
4970   }
4971
4972   {
4973     static GstValueTable gst_value = {
4974       0,
4975       gst_value_compare_list,
4976       gst_value_serialize_list,
4977       gst_value_deserialize_list,
4978     };
4979
4980     gst_value.type = gst_value_list_get_type ();
4981     gst_value_register (&gst_value);
4982   }
4983
4984   {
4985     static GstValueTable gst_value = {
4986       0,
4987       gst_value_compare_array,
4988       gst_value_serialize_array,
4989       gst_value_deserialize_array,
4990     };
4991
4992     gst_value.type = gst_value_array_get_type ();
4993     gst_value_register (&gst_value);
4994   }
4995
4996   {
4997 #if 0
4998     static const GTypeValueTable value_table = {
4999       gst_value_init_buffer,
5000       NULL,
5001       gst_value_copy_buffer,
5002       NULL,
5003       "i",
5004       NULL,                     /*gst_value_collect_buffer, */
5005       "p",
5006       NULL                      /*gst_value_lcopy_buffer */
5007     };
5008 #endif
5009     static GstValueTable gst_value = {
5010       0,
5011       gst_value_compare_buffer,
5012       gst_value_serialize_buffer,
5013       gst_value_deserialize_buffer,
5014     };
5015
5016     gst_value.type = GST_TYPE_BUFFER;
5017     gst_value_register (&gst_value);
5018   }
5019   {
5020     static GstValueTable gst_value = {
5021       0,
5022       gst_value_compare_fraction,
5023       gst_value_serialize_fraction,
5024       gst_value_deserialize_fraction,
5025     };
5026
5027     gst_value.type = gst_fraction_get_type ();
5028     gst_value_register (&gst_value);
5029   }
5030   {
5031     static GstValueTable gst_value = {
5032       0,
5033       NULL,
5034       gst_value_serialize_caps,
5035       gst_value_deserialize_caps,
5036     };
5037
5038     gst_value.type = GST_TYPE_CAPS;
5039     gst_value_register (&gst_value);
5040   }
5041   {
5042     static GstValueTable gst_value = {
5043       0,
5044       NULL,
5045       gst_value_serialize_structure,
5046       gst_value_deserialize_structure,
5047     };
5048
5049     gst_value.type = GST_TYPE_STRUCTURE;
5050     gst_value_register (&gst_value);
5051   }
5052   {
5053     static GstValueTable gst_value = {
5054       0,
5055       gst_value_compare_date,
5056       gst_value_serialize_date,
5057       gst_value_deserialize_date,
5058     };
5059
5060     gst_value.type = gst_date_get_type ();
5061     gst_value_register (&gst_value);
5062   }
5063   {
5064     static GstValueTable gst_value = {
5065       0,
5066       gst_value_compare_date_time,
5067       gst_value_serialize_date_time,
5068       gst_value_deserialize_date_time,
5069     };
5070
5071     gst_value.type = gst_date_time_get_type ();
5072     gst_value_register (&gst_value);
5073   }
5074
5075   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
5076   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
5077
5078   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
5079   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
5080   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
5081
5082   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
5083
5084   REGISTER_SERIALIZATION (G_TYPE_INT, int);
5085
5086   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
5087   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
5088
5089   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
5090   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
5091   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
5092
5093   REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
5094
5095   g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
5096       gst_value_transform_fourcc_string);
5097   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
5098       gst_value_transform_int_range_string);
5099   g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
5100       gst_value_transform_int64_range_string);
5101   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
5102       gst_value_transform_double_range_string);
5103   g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
5104       gst_value_transform_fraction_range_string);
5105   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
5106       gst_value_transform_list_string);
5107   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
5108       gst_value_transform_array_string);
5109   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
5110       gst_value_transform_fraction_string);
5111   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
5112       gst_value_transform_string_fraction);
5113   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
5114       gst_value_transform_fraction_double);
5115   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
5116       gst_value_transform_fraction_float);
5117   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
5118       gst_value_transform_double_fraction);
5119   g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
5120       gst_value_transform_float_fraction);
5121   g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
5122       gst_value_transform_date_string);
5123   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
5124       gst_value_transform_string_date);
5125   g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
5126       gst_value_transform_object_string);
5127
5128   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5129       gst_value_intersect_int_int_range);
5130   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5131       gst_value_intersect_int_range_int_range);
5132   gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5133       gst_value_intersect_int64_int64_range);
5134   gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5135       gst_value_intersect_int64_range_int64_range);
5136   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5137       gst_value_intersect_double_double_range);
5138   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
5139       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
5140   gst_value_register_intersect_func (GST_TYPE_ARRAY,
5141       GST_TYPE_ARRAY, gst_value_intersect_array);
5142   gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5143       gst_value_intersect_fraction_fraction_range);
5144   gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
5145       GST_TYPE_FRACTION_RANGE,
5146       gst_value_intersect_fraction_range_fraction_range);
5147
5148   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5149       gst_value_subtract_int_int_range);
5150   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
5151       gst_value_subtract_int_range_int);
5152   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5153       gst_value_subtract_int_range_int_range);
5154   gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5155       gst_value_subtract_int64_int64_range);
5156   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
5157       gst_value_subtract_int64_range_int64);
5158   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5159       gst_value_subtract_int64_range_int64_range);
5160   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5161       gst_value_subtract_double_double_range);
5162   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
5163       gst_value_subtract_double_range_double);
5164   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
5165       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
5166
5167   gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5168       gst_value_subtract_fraction_fraction_range);
5169   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
5170       gst_value_subtract_fraction_range_fraction);
5171   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
5172       GST_TYPE_FRACTION_RANGE,
5173       gst_value_subtract_fraction_range_fraction_range);
5174
5175   /* see bug #317246, #64994, #65041 */
5176   {
5177     volatile GType date_type = G_TYPE_DATE;
5178
5179     g_type_name (date_type);
5180   }
5181
5182   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5183       gst_value_union_int_int_range);
5184   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5185       gst_value_union_int_range_int_range);
5186
5187 #if 0
5188   /* Implement these if needed */
5189   gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5190       gst_value_union_fraction_fraction_range);
5191   gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
5192       GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
5193 #endif
5194 }