Merge branch 'master' into 0.11
[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_value_get_buffer (value1);
1724   GstBuffer *buf2 = gst_value_get_buffer (value2);
1725   gsize size1, size2;
1726   gpointer data1, data2;
1727   gint result = GST_VALUE_UNORDERED;
1728
1729   size1 = gst_buffer_get_size (buf1);
1730   size2 = gst_buffer_get_size (buf2);
1731
1732   if (size1 != size2)
1733     return GST_VALUE_UNORDERED;
1734
1735   if (size1 == 0)
1736     return GST_VALUE_EQUAL;
1737
1738   data1 = gst_buffer_map (buf1, &size1, NULL, GST_MAP_READ);
1739   data2 = gst_buffer_map (buf2, &size2, NULL, GST_MAP_READ);
1740   g_assert (data1);
1741   g_assert (data2);
1742
1743   if (memcmp (data1, data2, size1) == 0)
1744     result = GST_VALUE_EQUAL;
1745
1746   gst_buffer_unmap (buf2, data2, size2);
1747   gst_buffer_unmap (buf1, data1, size1);
1748
1749   return result;
1750 }
1751
1752 static gchar *
1753 gst_value_serialize_buffer (const GValue * value)
1754 {
1755   guint8 *data;
1756   gint i;
1757   gsize size;
1758   gchar *string;
1759   GstBuffer *buffer;
1760
1761   buffer = gst_value_get_buffer (value);
1762   if (buffer == NULL)
1763     return NULL;
1764
1765   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1766
1767   string = g_malloc (size * 2 + 1);
1768   for (i = 0; i < size; i++) {
1769     sprintf (string + i * 2, "%02x", data[i]);
1770   }
1771   string[size * 2] = 0;
1772
1773   gst_buffer_unmap (buffer, data, size);
1774
1775   return string;
1776 }
1777
1778 static gboolean
1779 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1780 {
1781   GstBuffer *buffer;
1782   gint len;
1783   gchar ts[3];
1784   guint8 *data;
1785   gint i;
1786   gsize size;
1787
1788   len = strlen (s);
1789   if (len & 1)
1790     goto wrong_length;
1791
1792   buffer = gst_buffer_new_and_alloc (len / 2);
1793   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_WRITE);
1794
1795   for (i = 0; i < len / 2; i++) {
1796     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1797       goto wrong_char;
1798
1799     ts[0] = s[i * 2 + 0];
1800     ts[1] = s[i * 2 + 1];
1801     ts[2] = 0;
1802
1803     data[i] = (guint8) strtoul (ts, NULL, 16);
1804   }
1805   gst_buffer_unmap (buffer, data, size);
1806
1807   gst_value_take_buffer (dest, buffer);
1808
1809   return TRUE;
1810
1811   /* ERRORS */
1812 wrong_length:
1813   {
1814     return FALSE;
1815   }
1816 wrong_char:
1817   {
1818     gst_buffer_unref (buffer);
1819     gst_buffer_unmap (buffer, data, size);
1820     return FALSE;
1821   }
1822 }
1823
1824
1825 /***********
1826  * boolean *
1827  ***********/
1828
1829 static gint
1830 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1831 {
1832   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1833     return GST_VALUE_EQUAL;
1834   return GST_VALUE_UNORDERED;
1835 }
1836
1837 static gchar *
1838 gst_value_serialize_boolean (const GValue * value)
1839 {
1840   if (value->data[0].v_int) {
1841     return g_strdup ("true");
1842   }
1843   return g_strdup ("false");
1844 }
1845
1846 static gboolean
1847 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1848 {
1849   gboolean ret = FALSE;
1850
1851   if (g_ascii_strcasecmp (s, "true") == 0 ||
1852       g_ascii_strcasecmp (s, "yes") == 0 ||
1853       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1854     g_value_set_boolean (dest, TRUE);
1855     ret = TRUE;
1856   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1857       g_ascii_strcasecmp (s, "no") == 0 ||
1858       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1859     g_value_set_boolean (dest, FALSE);
1860     ret = TRUE;
1861   }
1862
1863   return ret;
1864 }
1865
1866 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
1867 static gint                                                             \
1868 gst_value_compare_ ## _type                                             \
1869 (const GValue * value1, const GValue * value2)                          \
1870 {                                                                       \
1871   g ## _type val1 = g_value_get_ ## _type (value1);                     \
1872   g ## _type val2 = g_value_get_ ## _type (value2);                     \
1873   if (val1 > val2)                                                      \
1874     return GST_VALUE_GREATER_THAN;                                      \
1875   if (val1 < val2)                                                      \
1876     return GST_VALUE_LESS_THAN;                                         \
1877   return GST_VALUE_EQUAL;                                               \
1878 }                                                                       \
1879                                                                         \
1880 static gchar *                                                          \
1881 gst_value_serialize_ ## _type (const GValue * value)                    \
1882 {                                                                       \
1883   GValue val = { 0, };                                                  \
1884   g_value_init (&val, G_TYPE_STRING);                                   \
1885   if (!g_value_transform (value, &val))                                 \
1886     g_assert_not_reached ();                                            \
1887   /* NO_COPY_MADNESS!!! */                                              \
1888   return (char *) g_value_get_string (&val);                            \
1889 }
1890
1891 /* deserialize the given s into to as a gint64.
1892  * check if the result is actually storeable in the given size number of
1893  * bytes.
1894  */
1895 static gboolean
1896 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1897     gint64 min, gint64 max, gint size)
1898 {
1899   gboolean ret = FALSE;
1900   gchar *end;
1901   gint64 mask = -1;
1902
1903   errno = 0;
1904   *to = g_ascii_strtoull (s, &end, 0);
1905   /* a range error is a definitive no-no */
1906   if (errno == ERANGE) {
1907     return FALSE;
1908   }
1909
1910   if (*end == 0) {
1911     ret = TRUE;
1912   } else {
1913     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1914       *to = G_LITTLE_ENDIAN;
1915       ret = TRUE;
1916     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1917       *to = G_BIG_ENDIAN;
1918       ret = TRUE;
1919     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1920       *to = G_BYTE_ORDER;
1921       ret = TRUE;
1922     } else if (g_ascii_strcasecmp (s, "min") == 0) {
1923       *to = min;
1924       ret = TRUE;
1925     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1926       *to = max;
1927       ret = TRUE;
1928     }
1929   }
1930   if (ret) {
1931     /* by definition, a gint64 fits into a gint64; so ignore those */
1932     if (size != sizeof (mask)) {
1933       if (*to >= 0) {
1934         /* for positive numbers, we create a mask of 1's outside of the range
1935          * and 0's inside the range.  An and will thus keep only 1 bits
1936          * outside of the range */
1937         mask <<= (size * 8);
1938         if ((mask & *to) != 0) {
1939           ret = FALSE;
1940         }
1941       } else {
1942         /* for negative numbers, we do a 2's complement version */
1943         mask <<= ((size * 8) - 1);
1944         if ((mask & *to) != mask) {
1945           ret = FALSE;
1946         }
1947       }
1948     }
1949   }
1950   return ret;
1951 }
1952
1953 #define CREATE_SERIALIZATION(_type,_macro)                              \
1954 CREATE_SERIALIZATION_START(_type,_macro)                                \
1955                                                                         \
1956 static gboolean                                                         \
1957 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
1958 {                                                                       \
1959   gint64 x;                                                             \
1960                                                                         \
1961   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
1962       G_MAX ## _macro, sizeof (g ## _type))) {                          \
1963     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
1964     return TRUE;                                                        \
1965   } else {                                                              \
1966     return FALSE;                                                       \
1967   }                                                                     \
1968 }
1969
1970 #define CREATE_USERIALIZATION(_type,_macro)                             \
1971 CREATE_SERIALIZATION_START(_type,_macro)                                \
1972                                                                         \
1973 static gboolean                                                         \
1974 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
1975 {                                                                       \
1976   gint64 x;                                                             \
1977   gchar *end;                                                           \
1978   gboolean ret = FALSE;                                                 \
1979                                                                         \
1980   errno = 0;                                                            \
1981   x = g_ascii_strtoull (s, &end, 0);                                    \
1982   /* a range error is a definitive no-no */                             \
1983   if (errno == ERANGE) {                                                \
1984     return FALSE;                                                       \
1985   }                                                                     \
1986   /* the cast ensures the range check later on makes sense */           \
1987   x = (g ## _type) x;                                                   \
1988   if (*end == 0) {                                                      \
1989     ret = TRUE;                                                         \
1990   } else {                                                              \
1991     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
1992       x = G_LITTLE_ENDIAN;                                              \
1993       ret = TRUE;                                                       \
1994     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
1995       x = G_BIG_ENDIAN;                                                 \
1996       ret = TRUE;                                                       \
1997     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
1998       x = G_BYTE_ORDER;                                                 \
1999       ret = TRUE;                                                       \
2000     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
2001       x = 0;                                                            \
2002       ret = TRUE;                                                       \
2003     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
2004       x = G_MAX ## _macro;                                              \
2005       ret = TRUE;                                                       \
2006     }                                                                   \
2007   }                                                                     \
2008   if (ret) {                                                            \
2009     if (x > G_MAX ## _macro) {                                          \
2010       ret = FALSE;                                                      \
2011     } else {                                                            \
2012       g_value_set_ ## _type (dest, x);                                  \
2013     }                                                                   \
2014   }                                                                     \
2015   return ret;                                                           \
2016 }
2017
2018 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
2019 G_STMT_START {                                                          \
2020   static const GstValueTable gst_value = {                              \
2021     _gtype,                                                             \
2022     gst_value_compare_ ## _type,                                        \
2023     gst_value_serialize_ ## _type,                                      \
2024     gst_value_deserialize_ ## _type,                                    \
2025   };                                                                    \
2026                                                                         \
2027   gst_value_register (&gst_value);                                      \
2028 } G_STMT_END
2029
2030 CREATE_SERIALIZATION (int, INT);
2031 CREATE_SERIALIZATION (int64, INT64);
2032 CREATE_SERIALIZATION (long, LONG);
2033
2034 CREATE_USERIALIZATION (uint, UINT);
2035 CREATE_USERIALIZATION (uint64, UINT64);
2036 CREATE_USERIALIZATION (ulong, ULONG);
2037
2038 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2039 #ifndef G_MAXUCHAR
2040 #define G_MAXUCHAR 255
2041 #endif
2042 CREATE_USERIALIZATION (uchar, UCHAR);
2043
2044 /**********
2045  * double *
2046  **********/
2047 static gint
2048 gst_value_compare_double (const GValue * value1, const GValue * value2)
2049 {
2050   if (value1->data[0].v_double > value2->data[0].v_double)
2051     return GST_VALUE_GREATER_THAN;
2052   if (value1->data[0].v_double < value2->data[0].v_double)
2053     return GST_VALUE_LESS_THAN;
2054   if (value1->data[0].v_double == value2->data[0].v_double)
2055     return GST_VALUE_EQUAL;
2056   return GST_VALUE_UNORDERED;
2057 }
2058
2059 static gchar *
2060 gst_value_serialize_double (const GValue * value)
2061 {
2062   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2063
2064   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2065   return g_strdup (d);
2066 }
2067
2068 static gboolean
2069 gst_value_deserialize_double (GValue * dest, const gchar * s)
2070 {
2071   gdouble x;
2072   gboolean ret = FALSE;
2073   gchar *end;
2074
2075   x = g_ascii_strtod (s, &end);
2076   if (*end == 0) {
2077     ret = TRUE;
2078   } else {
2079     if (g_ascii_strcasecmp (s, "min") == 0) {
2080       x = -G_MAXDOUBLE;
2081       ret = TRUE;
2082     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2083       x = G_MAXDOUBLE;
2084       ret = TRUE;
2085     }
2086   }
2087   if (ret) {
2088     g_value_set_double (dest, x);
2089   }
2090   return ret;
2091 }
2092
2093 /*********
2094  * float *
2095  *********/
2096
2097 static gint
2098 gst_value_compare_float (const GValue * value1, const GValue * value2)
2099 {
2100   if (value1->data[0].v_float > value2->data[0].v_float)
2101     return GST_VALUE_GREATER_THAN;
2102   if (value1->data[0].v_float < value2->data[0].v_float)
2103     return GST_VALUE_LESS_THAN;
2104   if (value1->data[0].v_float == value2->data[0].v_float)
2105     return GST_VALUE_EQUAL;
2106   return GST_VALUE_UNORDERED;
2107 }
2108
2109 static gchar *
2110 gst_value_serialize_float (const GValue * value)
2111 {
2112   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2113
2114   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2115   return g_strdup (d);
2116 }
2117
2118 static gboolean
2119 gst_value_deserialize_float (GValue * dest, const gchar * s)
2120 {
2121   gdouble x;
2122   gboolean ret = FALSE;
2123   gchar *end;
2124
2125   x = g_ascii_strtod (s, &end);
2126   if (*end == 0) {
2127     ret = TRUE;
2128   } else {
2129     if (g_ascii_strcasecmp (s, "min") == 0) {
2130       x = -G_MAXFLOAT;
2131       ret = TRUE;
2132     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2133       x = G_MAXFLOAT;
2134       ret = TRUE;
2135     }
2136   }
2137   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2138     ret = FALSE;
2139   if (ret) {
2140     g_value_set_float (dest, (float) x);
2141   }
2142   return ret;
2143 }
2144
2145 /**********
2146  * string *
2147  **********/
2148
2149 static gint
2150 gst_value_compare_string (const GValue * value1, const GValue * value2)
2151 {
2152   if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2153     /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2154     if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2155       return GST_VALUE_UNORDERED;
2156   } else {
2157     gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2158
2159     if (x < 0)
2160       return GST_VALUE_LESS_THAN;
2161     if (x > 0)
2162       return GST_VALUE_GREATER_THAN;
2163   }
2164
2165   return GST_VALUE_EQUAL;
2166 }
2167
2168 static gint
2169 gst_string_measure_wrapping (const gchar * s)
2170 {
2171   gint len;
2172   gboolean wrap = FALSE;
2173
2174   if (G_UNLIKELY (s == NULL))
2175     return -1;
2176
2177   /* Special case: the actual string NULL needs wrapping */
2178   if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2179     return 4;
2180
2181   len = 0;
2182   while (*s) {
2183     if (GST_ASCII_IS_STRING (*s)) {
2184       len++;
2185     } else if (*s < 0x20 || *s >= 0x7f) {
2186       wrap = TRUE;
2187       len += 4;
2188     } else {
2189       wrap = TRUE;
2190       len += 2;
2191     }
2192     s++;
2193   }
2194
2195   /* Wrap the string if we found something that needs
2196    * wrapping, or the empty string (len == 0) */
2197   return (wrap || len == 0) ? len : -1;
2198 }
2199
2200 static gchar *
2201 gst_string_wrap_inner (const gchar * s, gint len)
2202 {
2203   gchar *d, *e;
2204
2205   e = d = g_malloc (len + 3);
2206
2207   *e++ = '\"';
2208   while (*s) {
2209     if (GST_ASCII_IS_STRING (*s)) {
2210       *e++ = *s++;
2211     } else if (*s < 0x20 || *s >= 0x7f) {
2212       *e++ = '\\';
2213       *e++ = '0' + ((*(guchar *) s) >> 6);
2214       *e++ = '0' + (((*s) >> 3) & 0x7);
2215       *e++ = '0' + ((*s++) & 0x7);
2216     } else {
2217       *e++ = '\\';
2218       *e++ = *s++;
2219     }
2220   }
2221   *e++ = '\"';
2222   *e = 0;
2223
2224   g_assert (e - d <= len + 3);
2225   return d;
2226 }
2227
2228 /* Do string wrapping/escaping */
2229 static gchar *
2230 gst_string_wrap (const gchar * s)
2231 {
2232   gint len = gst_string_measure_wrapping (s);
2233
2234   if (G_LIKELY (len < 0))
2235     return g_strdup (s);
2236
2237   return gst_string_wrap_inner (s, len);
2238 }
2239
2240 /* Same as above, but take ownership of the string */
2241 static gchar *
2242 gst_string_take_and_wrap (gchar * s)
2243 {
2244   gchar *out;
2245   gint len = gst_string_measure_wrapping (s);
2246
2247   if (G_LIKELY (len < 0))
2248     return s;
2249
2250   out = gst_string_wrap_inner (s, len);
2251   g_free (s);
2252
2253   return out;
2254 }
2255
2256 /*
2257  * This function takes a string delimited with double quotes (")
2258  * and unescapes any \xxx octal numbers.
2259  *
2260  * If sequences of \y are found where y is not in the range of
2261  * 0->3, y is copied unescaped.
2262  *
2263  * If \xyy is found where x is an octal number but y is not, an
2264  * error is encountered and NULL is returned.
2265  *
2266  * the input string must be \0 terminated.
2267  */
2268 static gchar *
2269 gst_string_unwrap (const gchar * s)
2270 {
2271   gchar *ret;
2272   gchar *read, *write;
2273
2274   /* NULL string returns NULL */
2275   if (s == NULL)
2276     return NULL;
2277
2278   /* strings not starting with " are invalid */
2279   if (*s != '"')
2280     return NULL;
2281
2282   /* make copy of original string to hold the result. This
2283    * string will always be smaller than the original */
2284   ret = g_strdup (s);
2285   read = ret;
2286   write = ret;
2287
2288   /* need to move to the next position as we parsed the " */
2289   read++;
2290
2291   while (*read) {
2292     if (GST_ASCII_IS_STRING (*read)) {
2293       /* normal chars are just copied */
2294       *write++ = *read++;
2295     } else if (*read == '"') {
2296       /* quote marks end of string */
2297       break;
2298     } else if (*read == '\\') {
2299       /* got an escape char, move to next position to read a tripplet
2300        * of octal numbers */
2301       read++;
2302       /* is the next char a possible first octal number? */
2303       if (*read >= '0' && *read <= '3') {
2304         /* parse other 2 numbers, if one of them is not in the range of
2305          * an octal number, we error. We also catch the case where a zero
2306          * byte is found here. */
2307         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2308           goto beach;
2309
2310         /* now convert the octal number to a byte again. */
2311         *write++ = ((read[0] - '0') << 6) +
2312             ((read[1] - '0') << 3) + (read[2] - '0');
2313
2314         read += 3;
2315       } else {
2316         /* if we run into a \0 here, we definately won't get a quote later */
2317         if (*read == 0)
2318           goto beach;
2319
2320         /* else copy \X sequence */
2321         *write++ = *read++;
2322       }
2323     } else {
2324       /* weird character, error */
2325       goto beach;
2326     }
2327   }
2328   /* if the string is not ending in " and zero terminated, we error */
2329   if (*read != '"' || read[1] != '\0')
2330     goto beach;
2331
2332   /* null terminate result string and return */
2333   *write = '\0';
2334   return ret;
2335
2336 beach:
2337   g_free (ret);
2338   return NULL;
2339 }
2340
2341 static gchar *
2342 gst_value_serialize_string (const GValue * value)
2343 {
2344   return gst_string_wrap (value->data[0].v_pointer);
2345 }
2346
2347 static gboolean
2348 gst_value_deserialize_string (GValue * dest, const gchar * s)
2349 {
2350   if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2351     g_value_set_string (dest, NULL);
2352     return TRUE;
2353   } else if (G_LIKELY (*s != '"')) {
2354     if (!g_utf8_validate (s, -1, NULL))
2355       return FALSE;
2356     g_value_set_string (dest, s);
2357     return TRUE;
2358   } else {
2359     gchar *str = gst_string_unwrap (s);
2360     if (G_UNLIKELY (!str))
2361       return FALSE;
2362     g_value_take_string (dest, str);
2363   }
2364
2365   return TRUE;
2366 }
2367
2368 /********
2369  * enum *
2370  ********/
2371
2372 static gint
2373 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2374 {
2375   GEnumValue *en1, *en2;
2376   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2377   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2378
2379   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2380   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2381   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2382   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2383   g_type_class_unref (klass1);
2384   g_type_class_unref (klass2);
2385   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2386   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2387   if (en1->value < en2->value)
2388     return GST_VALUE_LESS_THAN;
2389   if (en1->value > en2->value)
2390     return GST_VALUE_GREATER_THAN;
2391
2392   return GST_VALUE_EQUAL;
2393 }
2394
2395 static gchar *
2396 gst_value_serialize_enum (const GValue * value)
2397 {
2398   GEnumValue *en;
2399   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
2400
2401   g_return_val_if_fail (klass, NULL);
2402   en = g_enum_get_value (klass, g_value_get_enum (value));
2403   g_type_class_unref (klass);
2404
2405   /* might be one of the custom formats registered later */
2406   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
2407     const GstFormatDefinition *format_def;
2408
2409     format_def = gst_format_get_details (g_value_get_enum (value));
2410     g_return_val_if_fail (format_def != NULL, NULL);
2411     return g_strdup (format_def->description);
2412   }
2413
2414   g_return_val_if_fail (en, NULL);
2415   return g_strdup (en->value_name);
2416 }
2417
2418 static gint
2419 gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
2420     const gchar * s)
2421 {
2422   if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2423     return 0;
2424
2425   return g_ascii_strcasecmp (s, format_def->description);
2426 }
2427
2428 static gboolean
2429 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2430 {
2431   GEnumValue *en;
2432   gchar *endptr = NULL;
2433   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2434
2435   g_return_val_if_fail (klass, FALSE);
2436   if (!(en = g_enum_get_value_by_name (klass, s))) {
2437     if (!(en = g_enum_get_value_by_nick (klass, s))) {
2438       gint i = strtol (s, &endptr, 0);
2439
2440       if (endptr && *endptr == '\0') {
2441         en = g_enum_get_value (klass, i);
2442       }
2443     }
2444   }
2445   g_type_class_unref (klass);
2446
2447   /* might be one of the custom formats registered later */
2448   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2449     const GstFormatDefinition *format_def;
2450     GstIterator *iter;
2451
2452     iter = gst_format_iterate_definitions ();
2453
2454     format_def = gst_iterator_find_custom (iter,
2455         (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
2456
2457     g_return_val_if_fail (format_def != NULL, FALSE);
2458     g_value_set_enum (dest, (gint) format_def->value);
2459     gst_iterator_free (iter);
2460     return TRUE;
2461   }
2462
2463   g_return_val_if_fail (en, FALSE);
2464   g_value_set_enum (dest, en->value);
2465   return TRUE;
2466 }
2467
2468 /********
2469  * flags *
2470  ********/
2471
2472 /* we just compare the value here */
2473 static gint
2474 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2475 {
2476   guint fl1, fl2;
2477   GFlagsClass *klass1 =
2478       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2479   GFlagsClass *klass2 =
2480       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2481
2482   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2483   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2484   fl1 = g_value_get_flags (value1);
2485   fl2 = g_value_get_flags (value2);
2486   g_type_class_unref (klass1);
2487   g_type_class_unref (klass2);
2488   if (fl1 < fl2)
2489     return GST_VALUE_LESS_THAN;
2490   if (fl1 > fl2)
2491     return GST_VALUE_GREATER_THAN;
2492
2493   return GST_VALUE_EQUAL;
2494 }
2495
2496 /* the different flags are serialized separated with a + */
2497 static gchar *
2498 gst_value_serialize_flags (const GValue * value)
2499 {
2500   guint flags;
2501   GFlagsValue *fl;
2502   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2503   gchar *result, *tmp;
2504   gboolean first = TRUE;
2505
2506   g_return_val_if_fail (klass, NULL);
2507
2508   flags = g_value_get_flags (value);
2509
2510   /* if no flags are set, try to serialize to the _NONE string */
2511   if (!flags) {
2512     fl = g_flags_get_first_value (klass, flags);
2513     return g_strdup (fl->value_name);
2514   }
2515
2516   /* some flags are set, so serialize one by one */
2517   result = g_strdup ("");
2518   while (flags) {
2519     fl = g_flags_get_first_value (klass, flags);
2520     if (fl != NULL) {
2521       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2522       g_free (result);
2523       result = tmp;
2524       first = FALSE;
2525
2526       /* clear flag */
2527       flags &= ~fl->value;
2528     }
2529   }
2530   g_type_class_unref (klass);
2531
2532   return result;
2533 }
2534
2535 static gboolean
2536 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2537 {
2538   GFlagsValue *fl;
2539   gchar *endptr = NULL;
2540   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2541   gchar **split;
2542   guint flags;
2543   gint i;
2544
2545   g_return_val_if_fail (klass, FALSE);
2546
2547   /* split into parts delimited with + */
2548   split = g_strsplit (s, "+", 0);
2549
2550   flags = 0;
2551   i = 0;
2552   /* loop over each part */
2553   while (split[i]) {
2554     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2555       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2556         gint val = strtol (split[i], &endptr, 0);
2557
2558         /* just or numeric value */
2559         if (endptr && *endptr == '\0') {
2560           flags |= val;
2561         }
2562       }
2563     }
2564     if (fl) {
2565       flags |= fl->value;
2566     }
2567     i++;
2568   }
2569   g_strfreev (split);
2570   g_type_class_unref (klass);
2571   g_value_set_flags (dest, flags);
2572
2573   return TRUE;
2574 }
2575
2576 /*********
2577  * union *
2578  *********/
2579
2580 static gboolean
2581 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2582     const GValue * src2)
2583 {
2584   if (src2->data[0].v_int <= src1->data[0].v_int &&
2585       src2->data[1].v_int >= src1->data[0].v_int) {
2586     gst_value_init_and_copy (dest, src2);
2587     return TRUE;
2588   }
2589   return FALSE;
2590 }
2591
2592 static gboolean
2593 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2594     const GValue * src2)
2595 {
2596   gint min;
2597   gint max;
2598
2599   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2600   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2601
2602   if (min <= max) {
2603     g_value_init (dest, GST_TYPE_INT_RANGE);
2604     gst_value_set_int_range (dest,
2605         MIN (src1->data[0].v_int, src2->data[0].v_int),
2606         MAX (src1->data[1].v_int, src2->data[1].v_int));
2607     return TRUE;
2608   }
2609
2610   return FALSE;
2611 }
2612
2613 /****************
2614  * intersection *
2615  ****************/
2616
2617 static gboolean
2618 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2619     const GValue * src2)
2620 {
2621   if (src2->data[0].v_int <= src1->data[0].v_int &&
2622       src2->data[1].v_int >= src1->data[0].v_int) {
2623     gst_value_init_and_copy (dest, src1);
2624     return TRUE;
2625   }
2626
2627   return FALSE;
2628 }
2629
2630 static gboolean
2631 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2632     const GValue * src2)
2633 {
2634   gint min;
2635   gint max;
2636
2637   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2638   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2639
2640   if (min < max) {
2641     g_value_init (dest, GST_TYPE_INT_RANGE);
2642     gst_value_set_int_range (dest, min, max);
2643     return TRUE;
2644   }
2645   if (min == max) {
2646     g_value_init (dest, G_TYPE_INT);
2647     g_value_set_int (dest, min);
2648     return TRUE;
2649   }
2650
2651   return FALSE;
2652 }
2653
2654 static gboolean
2655 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
2656     const GValue * src2)
2657 {
2658   if (src2->data[0].v_int64 <= src1->data[0].v_int64 &&
2659       src2->data[1].v_int64 >= src1->data[0].v_int64) {
2660     gst_value_init_and_copy (dest, src1);
2661     return TRUE;
2662   }
2663
2664   return FALSE;
2665 }
2666
2667 static gboolean
2668 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
2669     const GValue * src2)
2670 {
2671   gint64 min;
2672   gint64 max;
2673
2674   min = MAX (src1->data[0].v_int64, src2->data[0].v_int64);
2675   max = MIN (src1->data[1].v_int64, src2->data[1].v_int64);
2676
2677   if (min < max) {
2678     g_value_init (dest, GST_TYPE_INT64_RANGE);
2679     gst_value_set_int64_range (dest, min, max);
2680     return TRUE;
2681   }
2682   if (min == max) {
2683     g_value_init (dest, G_TYPE_INT64);
2684     g_value_set_int64 (dest, min);
2685     return TRUE;
2686   }
2687
2688   return FALSE;
2689 }
2690
2691 static gboolean
2692 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2693     const GValue * src2)
2694 {
2695   if (src2->data[0].v_double <= src1->data[0].v_double &&
2696       src2->data[1].v_double >= src1->data[0].v_double) {
2697     gst_value_init_and_copy (dest, src1);
2698     return TRUE;
2699   }
2700
2701   return FALSE;
2702 }
2703
2704 static gboolean
2705 gst_value_intersect_double_range_double_range (GValue * dest,
2706     const GValue * src1, const GValue * src2)
2707 {
2708   gdouble min;
2709   gdouble max;
2710
2711   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2712   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2713
2714   if (min < max) {
2715     g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2716     gst_value_set_double_range (dest, min, max);
2717     return TRUE;
2718   }
2719   if (min == max) {
2720     g_value_init (dest, G_TYPE_DOUBLE);
2721     g_value_set_int (dest, (int) min);
2722     return TRUE;
2723   }
2724
2725   return FALSE;
2726 }
2727
2728 static gboolean
2729 gst_value_intersect_list (GValue * dest, const GValue * value1,
2730     const GValue * value2)
2731 {
2732   guint i, size;
2733   GValue intersection = { 0, };
2734   gboolean ret = FALSE;
2735
2736   size = VALUE_LIST_SIZE (value1);
2737   for (i = 0; i < size; i++) {
2738     const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
2739
2740     if (gst_value_intersect (&intersection, cur, value2)) {
2741       /* append value */
2742       if (!ret) {
2743         gst_value_init_and_copy (dest, &intersection);
2744         ret = TRUE;
2745       } else if (GST_VALUE_HOLDS_LIST (dest)) {
2746         gst_value_list_append_value (dest, &intersection);
2747       } else {
2748         GValue temp = { 0, };
2749
2750         gst_value_init_and_copy (&temp, dest);
2751         g_value_unset (dest);
2752         gst_value_list_concat (dest, &temp, &intersection);
2753         g_value_unset (&temp);
2754       }
2755       g_value_unset (&intersection);
2756     }
2757   }
2758
2759   return ret;
2760 }
2761
2762 static gboolean
2763 gst_value_intersect_array (GValue * dest, const GValue * src1,
2764     const GValue * src2)
2765 {
2766   guint size;
2767   guint n;
2768   GValue val = { 0 };
2769
2770   /* only works on similar-sized arrays */
2771   size = gst_value_array_get_size (src1);
2772   if (size != gst_value_array_get_size (src2))
2773     return FALSE;
2774   g_value_init (dest, GST_TYPE_ARRAY);
2775
2776   for (n = 0; n < size; n++) {
2777     if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2778             gst_value_array_get_value (src2, n))) {
2779       g_value_unset (dest);
2780       return FALSE;
2781     }
2782     gst_value_array_append_value (dest, &val);
2783     g_value_unset (&val);
2784   }
2785
2786   return TRUE;
2787 }
2788
2789 static gboolean
2790 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2791     const GValue * src2)
2792 {
2793   gint res1, res2;
2794   GValue *vals;
2795   GstValueCompareFunc compare;
2796
2797   vals = src2->data[0].v_pointer;
2798
2799   if (vals == NULL)
2800     return FALSE;
2801
2802   if ((compare = gst_value_get_compare_func (src1))) {
2803     res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2804     res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2805
2806     if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2807         (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2808       gst_value_init_and_copy (dest, src1);
2809       return TRUE;
2810     }
2811   }
2812
2813   return FALSE;
2814 }
2815
2816 static gboolean
2817 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
2818     const GValue * src1, const GValue * src2)
2819 {
2820   GValue *min;
2821   GValue *max;
2822   gint res;
2823   GValue *vals1, *vals2;
2824   GstValueCompareFunc compare;
2825
2826   vals1 = src1->data[0].v_pointer;
2827   vals2 = src2->data[0].v_pointer;
2828   g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2829
2830   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2831     /* min = MAX (src1.start, src2.start) */
2832     res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2833     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2834     if (res == GST_VALUE_LESS_THAN)
2835       min = &vals2[0];          /* Take the max of the 2 */
2836     else
2837       min = &vals1[0];
2838
2839     /* max = MIN (src1.end, src2.end) */
2840     res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2841     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2842     if (res == GST_VALUE_GREATER_THAN)
2843       max = &vals2[1];          /* Take the min of the 2 */
2844     else
2845       max = &vals1[1];
2846
2847     res = gst_value_compare_with_func (min, max, compare);
2848     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2849     if (res == GST_VALUE_LESS_THAN) {
2850       g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2851       vals1 = dest->data[0].v_pointer;
2852       g_value_copy (min, &vals1[0]);
2853       g_value_copy (max, &vals1[1]);
2854       return TRUE;
2855     }
2856     if (res == GST_VALUE_EQUAL) {
2857       gst_value_init_and_copy (dest, min);
2858       return TRUE;
2859     }
2860   }
2861
2862   return FALSE;
2863 }
2864
2865 /***************
2866  * subtraction *
2867  ***************/
2868
2869 static gboolean
2870 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2871     const GValue * subtrahend)
2872 {
2873   gint min = gst_value_get_int_range_min (subtrahend);
2874   gint max = gst_value_get_int_range_max (subtrahend);
2875   gint val = g_value_get_int (minuend);
2876
2877   /* subtracting a range from an int only works if the int is not in the
2878    * range */
2879   if (val < min || val > max) {
2880     /* and the result is the int */
2881     gst_value_init_and_copy (dest, minuend);
2882     return TRUE;
2883   }
2884   return FALSE;
2885 }
2886
2887 /* creates a new int range based on input values.
2888  */
2889 static gboolean
2890 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2891     gint max2)
2892 {
2893   GValue v1 = { 0, };
2894   GValue v2 = { 0, };
2895   GValue *pv1, *pv2;            /* yeah, hungarian! */
2896
2897   if (min1 <= max1 && min2 <= max2) {
2898     pv1 = &v1;
2899     pv2 = &v2;
2900   } else if (min1 <= max1) {
2901     pv1 = dest;
2902     pv2 = NULL;
2903   } else if (min2 <= max2) {
2904     pv1 = NULL;
2905     pv2 = dest;
2906   } else {
2907     return FALSE;
2908   }
2909
2910   if (min1 < max1) {
2911     g_value_init (pv1, GST_TYPE_INT_RANGE);
2912     gst_value_set_int_range (pv1, min1, max1);
2913   } else if (min1 == max1) {
2914     g_value_init (pv1, G_TYPE_INT);
2915     g_value_set_int (pv1, min1);
2916   }
2917   if (min2 < max2) {
2918     g_value_init (pv2, GST_TYPE_INT_RANGE);
2919     gst_value_set_int_range (pv2, min2, max2);
2920   } else if (min2 == max2) {
2921     g_value_init (pv2, G_TYPE_INT);
2922     g_value_set_int (pv2, min2);
2923   }
2924
2925   if (min1 <= max1 && min2 <= max2) {
2926     gst_value_list_concat (dest, pv1, pv2);
2927     g_value_unset (pv1);
2928     g_value_unset (pv2);
2929   }
2930   return TRUE;
2931 }
2932
2933 static gboolean
2934 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2935     const GValue * subtrahend)
2936 {
2937   gint min = gst_value_get_int_range_min (minuend);
2938   gint max = gst_value_get_int_range_max (minuend);
2939   gint val = g_value_get_int (subtrahend);
2940
2941   g_return_val_if_fail (min < max, FALSE);
2942
2943   /* value is outside of the range, return range unchanged */
2944   if (val < min || val > max) {
2945     gst_value_init_and_copy (dest, minuend);
2946     return TRUE;
2947   } else {
2948     /* max must be MAXINT too as val <= max */
2949     if (val == G_MAXINT) {
2950       max--;
2951       val--;
2952     }
2953     /* min must be MININT too as val >= max */
2954     if (val == G_MININT) {
2955       min++;
2956       val++;
2957     }
2958     gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2959   }
2960   return TRUE;
2961 }
2962
2963 static gboolean
2964 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2965     const GValue * subtrahend)
2966 {
2967   gint min1 = gst_value_get_int_range_min (minuend);
2968   gint max1 = gst_value_get_int_range_max (minuend);
2969   gint min2 = gst_value_get_int_range_min (subtrahend);
2970   gint max2 = gst_value_get_int_range_max (subtrahend);
2971
2972   if (max2 == G_MAXINT && min2 == G_MININT) {
2973     return FALSE;
2974   } else if (max2 == G_MAXINT) {
2975     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
2976   } else if (min2 == G_MININT) {
2977     return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
2978   } else {
2979     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
2980         MAX (max2 + 1, min1), max1);
2981   }
2982 }
2983
2984 static gboolean
2985 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
2986     const GValue * subtrahend)
2987 {
2988   gint64 min = gst_value_get_int64_range_min (subtrahend);
2989   gint64 max = gst_value_get_int64_range_max (subtrahend);
2990   gint64 val = g_value_get_int64 (minuend);
2991
2992   /* subtracting a range from an int64 only works if the int64 is not in the
2993    * range */
2994   if (val < min || val > max) {
2995     /* and the result is the int64 */
2996     gst_value_init_and_copy (dest, minuend);
2997     return TRUE;
2998   }
2999   return FALSE;
3000 }
3001
3002 /* creates a new int64 range based on input values.
3003  */
3004 static gboolean
3005 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
3006     gint64 min2, gint64 max2)
3007 {
3008   GValue v1 = { 0, };
3009   GValue v2 = { 0, };
3010   GValue *pv1, *pv2;            /* yeah, hungarian! */
3011
3012   if (min1 <= max1 && min2 <= max2) {
3013     pv1 = &v1;
3014     pv2 = &v2;
3015   } else if (min1 <= max1) {
3016     pv1 = dest;
3017     pv2 = NULL;
3018   } else if (min2 <= max2) {
3019     pv1 = NULL;
3020     pv2 = dest;
3021   } else {
3022     return FALSE;
3023   }
3024
3025   if (min1 < max1) {
3026     g_value_init (pv1, GST_TYPE_INT64_RANGE);
3027     gst_value_set_int64_range (pv1, min1, max1);
3028   } else if (min1 == max1) {
3029     g_value_init (pv1, G_TYPE_INT64);
3030     g_value_set_int64 (pv1, min1);
3031   }
3032   if (min2 < max2) {
3033     g_value_init (pv2, GST_TYPE_INT64_RANGE);
3034     gst_value_set_int64_range (pv2, min2, max2);
3035   } else if (min2 == max2) {
3036     g_value_init (pv2, G_TYPE_INT64);
3037     g_value_set_int64 (pv2, min2);
3038   }
3039
3040   if (min1 <= max1 && min2 <= max2) {
3041     gst_value_list_concat (dest, pv1, pv2);
3042     g_value_unset (pv1);
3043     g_value_unset (pv2);
3044   }
3045   return TRUE;
3046 }
3047
3048 static gboolean
3049 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
3050     const GValue * subtrahend)
3051 {
3052   gint64 min = gst_value_get_int64_range_min (minuend);
3053   gint64 max = gst_value_get_int64_range_max (minuend);
3054   gint64 val = g_value_get_int64 (subtrahend);
3055
3056   g_return_val_if_fail (min < max, FALSE);
3057
3058   /* value is outside of the range, return range unchanged */
3059   if (val < min || val > max) {
3060     gst_value_init_and_copy (dest, minuend);
3061     return TRUE;
3062   } else {
3063     /* max must be MAXINT64 too as val <= max */
3064     if (val == G_MAXINT64) {
3065       max--;
3066       val--;
3067     }
3068     /* min must be MININT64 too as val >= max */
3069     if (val == G_MININT64) {
3070       min++;
3071       val++;
3072     }
3073     gst_value_create_new_int64_range (dest, min, val - 1, val + 1, max);
3074   }
3075   return TRUE;
3076 }
3077
3078 static gboolean
3079 gst_value_subtract_int64_range_int64_range (GValue * dest,
3080     const GValue * minuend, const GValue * subtrahend)
3081 {
3082   gint64 min1 = gst_value_get_int64_range_min (minuend);
3083   gint64 max1 = gst_value_get_int64_range_max (minuend);
3084   gint64 min2 = gst_value_get_int64_range_min (subtrahend);
3085   gint64 max2 = gst_value_get_int64_range_max (subtrahend);
3086
3087   if (max2 == G_MAXINT64 && min2 == G_MININT64) {
3088     return FALSE;
3089   } else if (max2 == G_MAXINT64) {
3090     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
3091         1, 0);
3092   } else if (min2 == G_MININT64) {
3093     return gst_value_create_new_int64_range (dest, MAX (max2 + 1, min1), max1,
3094         1, 0);
3095   } else {
3096     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
3097         MAX (max2 + 1, min1), max1);
3098   }
3099 }
3100
3101 static gboolean
3102 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
3103     const GValue * subtrahend)
3104 {
3105   gdouble min = gst_value_get_double_range_min (subtrahend);
3106   gdouble max = gst_value_get_double_range_max (subtrahend);
3107   gdouble val = g_value_get_double (minuend);
3108
3109   if (val < min || val > max) {
3110     gst_value_init_and_copy (dest, minuend);
3111     return TRUE;
3112   }
3113   return FALSE;
3114 }
3115
3116 static gboolean
3117 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
3118     const GValue * subtrahend)
3119 {
3120   /* since we don't have open ranges, we cannot create a hole in
3121    * a double range. We return the original range */
3122   gst_value_init_and_copy (dest, minuend);
3123   return TRUE;
3124 }
3125
3126 static gboolean
3127 gst_value_subtract_double_range_double_range (GValue * dest,
3128     const GValue * minuend, const GValue * subtrahend)
3129 {
3130   /* since we don't have open ranges, we have to approximate */
3131   /* done like with ints */
3132   gdouble min1 = gst_value_get_double_range_min (minuend);
3133   gdouble max2 = gst_value_get_double_range_max (minuend);
3134   gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
3135   gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
3136   GValue v1 = { 0, };
3137   GValue v2 = { 0, };
3138   GValue *pv1, *pv2;            /* yeah, hungarian! */
3139
3140   if (min1 < max1 && min2 < max2) {
3141     pv1 = &v1;
3142     pv2 = &v2;
3143   } else if (min1 < max1) {
3144     pv1 = dest;
3145     pv2 = NULL;
3146   } else if (min2 < max2) {
3147     pv1 = NULL;
3148     pv2 = dest;
3149   } else {
3150     return FALSE;
3151   }
3152
3153   if (min1 < max1) {
3154     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
3155     gst_value_set_double_range (pv1, min1, max1);
3156   }
3157   if (min2 < max2) {
3158     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
3159     gst_value_set_double_range (pv2, min2, max2);
3160   }
3161
3162   if (min1 < max1 && min2 < max2) {
3163     gst_value_list_concat (dest, pv1, pv2);
3164     g_value_unset (pv1);
3165     g_value_unset (pv2);
3166   }
3167   return TRUE;
3168 }
3169
3170 static gboolean
3171 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
3172     const GValue * subtrahend)
3173 {
3174   guint i, size;
3175   GValue subtraction = { 0, };
3176   gboolean ret = FALSE;
3177   GType ltype;
3178
3179   ltype = gst_value_list_get_type ();
3180
3181   size = VALUE_LIST_SIZE (minuend);
3182   for (i = 0; i < size; i++) {
3183     const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
3184
3185     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
3186       if (!ret) {
3187         gst_value_init_and_copy (dest, &subtraction);
3188         ret = TRUE;
3189       } else if (G_VALUE_HOLDS (dest, ltype)
3190           && !G_VALUE_HOLDS (&subtraction, ltype)) {
3191         gst_value_list_append_value (dest, &subtraction);
3192       } else {
3193         GValue temp = { 0, };
3194
3195         gst_value_init_and_copy (&temp, dest);
3196         g_value_unset (dest);
3197         gst_value_list_concat (dest, &temp, &subtraction);
3198         g_value_unset (&temp);
3199       }
3200       g_value_unset (&subtraction);
3201     }
3202   }
3203   return ret;
3204 }
3205
3206 static gboolean
3207 gst_value_subtract_list (GValue * dest, const GValue * minuend,
3208     const GValue * subtrahend)
3209 {
3210   guint i, size;
3211   GValue data[2] = { {0,}, {0,} };
3212   GValue *subtraction = &data[0], *result = &data[1];
3213
3214   gst_value_init_and_copy (result, minuend);
3215   size = VALUE_LIST_SIZE (subtrahend);
3216   for (i = 0; i < size; i++) {
3217     const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
3218
3219     if (gst_value_subtract (subtraction, result, cur)) {
3220       GValue *temp = result;
3221
3222       result = subtraction;
3223       subtraction = temp;
3224       g_value_unset (subtraction);
3225     } else {
3226       g_value_unset (result);
3227       return FALSE;
3228     }
3229   }
3230   gst_value_init_and_copy (dest, result);
3231   g_value_unset (result);
3232   return TRUE;
3233 }
3234
3235 static gboolean
3236 gst_value_subtract_fraction_fraction_range (GValue * dest,
3237     const GValue * minuend, const GValue * subtrahend)
3238 {
3239   const GValue *min = gst_value_get_fraction_range_min (subtrahend);
3240   const GValue *max = gst_value_get_fraction_range_max (subtrahend);
3241   GstValueCompareFunc compare;
3242
3243   if ((compare = gst_value_get_compare_func (minuend))) {
3244     /* subtracting a range from an fraction only works if the fraction
3245      * is not in the range */
3246     if (gst_value_compare_with_func (minuend, min, compare) ==
3247         GST_VALUE_LESS_THAN ||
3248         gst_value_compare_with_func (minuend, max, compare) ==
3249         GST_VALUE_GREATER_THAN) {
3250       /* and the result is the value */
3251       gst_value_init_and_copy (dest, minuend);
3252       return TRUE;
3253     }
3254   }
3255   return FALSE;
3256 }
3257
3258 static gboolean
3259 gst_value_subtract_fraction_range_fraction (GValue * dest,
3260     const GValue * minuend, const GValue * subtrahend)
3261 {
3262   /* since we don't have open ranges, we cannot create a hole in
3263    * a range. We return the original range */
3264   gst_value_init_and_copy (dest, minuend);
3265   return TRUE;
3266 }
3267
3268 static gboolean
3269 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
3270     const GValue * minuend, const GValue * subtrahend)
3271 {
3272   /* since we don't have open ranges, we have to approximate */
3273   /* done like with ints and doubles. Creates a list of 2 fraction ranges */
3274   const GValue *min1 = gst_value_get_fraction_range_min (minuend);
3275   const GValue *max2 = gst_value_get_fraction_range_max (minuend);
3276   const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
3277   const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
3278   gint cmp1, cmp2;
3279   GValue v1 = { 0, };
3280   GValue v2 = { 0, };
3281   GValue *pv1, *pv2;            /* yeah, hungarian! */
3282   GstValueCompareFunc compare;
3283
3284   g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
3285   g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
3286
3287   compare = gst_value_get_compare_func (min1);
3288   g_return_val_if_fail (compare, FALSE);
3289
3290   cmp1 = gst_value_compare_with_func (max2, max1, compare);
3291   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3292   if (cmp1 == GST_VALUE_LESS_THAN)
3293     max1 = max2;
3294   cmp1 = gst_value_compare_with_func (min1, min2, compare);
3295   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3296   if (cmp1 == GST_VALUE_GREATER_THAN)
3297     min2 = min1;
3298
3299   cmp1 = gst_value_compare_with_func (min1, max1, compare);
3300   cmp2 = gst_value_compare_with_func (min2, max2, compare);
3301
3302   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3303     pv1 = &v1;
3304     pv2 = &v2;
3305   } else if (cmp1 == GST_VALUE_LESS_THAN) {
3306     pv1 = dest;
3307     pv2 = NULL;
3308   } else if (cmp2 == GST_VALUE_LESS_THAN) {
3309     pv1 = NULL;
3310     pv2 = dest;
3311   } else {
3312     return FALSE;
3313   }
3314
3315   if (cmp1 == GST_VALUE_LESS_THAN) {
3316     g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
3317     gst_value_set_fraction_range (pv1, min1, max1);
3318   }
3319   if (cmp2 == GST_VALUE_LESS_THAN) {
3320     g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
3321     gst_value_set_fraction_range (pv2, min2, max2);
3322   }
3323
3324   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3325     gst_value_list_concat (dest, pv1, pv2);
3326     g_value_unset (pv1);
3327     g_value_unset (pv2);
3328   }
3329   return TRUE;
3330 }
3331
3332
3333 /**************
3334  * comparison *
3335  **************/
3336
3337 /*
3338  * gst_value_get_compare_func:
3339  * @value1: a value to get the compare function for
3340  *
3341  * Determines the compare function to be used with values of the same type as
3342  * @value1. The function can be given to gst_value_compare_with_func().
3343  *
3344  * Returns: A #GstValueCompareFunc value
3345  */
3346 static GstValueCompareFunc
3347 gst_value_get_compare_func (const GValue * value1)
3348 {
3349   GstValueTable *table, *best = NULL;
3350   guint i;
3351   GType type1;
3352
3353   type1 = G_VALUE_TYPE (value1);
3354
3355   /* this is a fast check */
3356   best = gst_value_hash_lookup_type (type1);
3357
3358   /* slower checks */
3359   if (G_UNLIKELY (!best || !best->compare)) {
3360     guint len = gst_value_table->len;
3361
3362     best = NULL;
3363     for (i = 0; i < len; i++) {
3364       table = &g_array_index (gst_value_table, GstValueTable, i);
3365       if (table->compare && g_type_is_a (type1, table->type)) {
3366         if (!best || g_type_is_a (table->type, best->type))
3367           best = table;
3368       }
3369     }
3370   }
3371   if (G_LIKELY (best))
3372     return best->compare;
3373
3374   return NULL;
3375 }
3376
3377 /**
3378  * gst_value_can_compare:
3379  * @value1: a value to compare
3380  * @value2: another value to compare
3381  *
3382  * Determines if @value1 and @value2 can be compared.
3383  *
3384  * Returns: TRUE if the values can be compared
3385  */
3386 gboolean
3387 gst_value_can_compare (const GValue * value1, const GValue * value2)
3388 {
3389   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3390   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3391
3392   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3393     return FALSE;
3394
3395   return gst_value_get_compare_func (value1) != NULL;
3396 }
3397
3398 /**
3399  * gst_value_compare:
3400  * @value1: a value to compare
3401  * @value2: another value to compare
3402  *
3403  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
3404  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
3405  * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
3406  * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
3407  * If the values are equal, GST_VALUE_EQUAL is returned.
3408  *
3409  * Returns: comparison result
3410  */
3411 gint
3412 gst_value_compare (const GValue * value1, const GValue * value2)
3413 {
3414   GstValueCompareFunc compare;
3415
3416   g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
3417   g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
3418
3419   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3420     return GST_VALUE_UNORDERED;
3421
3422   compare = gst_value_get_compare_func (value1);
3423   if (compare) {
3424     return compare (value1, value2);
3425   }
3426
3427   g_critical ("unable to compare values of type %s\n",
3428       g_type_name (G_VALUE_TYPE (value1)));
3429   return GST_VALUE_UNORDERED;
3430 }
3431
3432 /*
3433  * gst_value_compare_with_func:
3434  * @value1: a value to compare
3435  * @value2: another value to compare
3436  * @compare: compare function
3437  *
3438  * Compares @value1 and @value2 using the @compare function. Works like
3439  * gst_value_compare() but allows to save time determining the compare function
3440  * a multiple times. 
3441  *
3442  * Returns: comparison result
3443  */
3444 static gint
3445 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
3446     GstValueCompareFunc compare)
3447 {
3448   g_assert (compare);
3449
3450   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3451     return GST_VALUE_UNORDERED;
3452
3453   return compare (value1, value2);
3454 }
3455
3456 /* union */
3457
3458 /**
3459  * gst_value_can_union:
3460  * @value1: a value to union
3461  * @value2: another value to union
3462  *
3463  * Determines if @value1 and @value2 can be non-trivially unioned.
3464  * Any two values can be trivially unioned by adding both of them
3465  * to a GstValueList.  However, certain types have the possibility
3466  * to be unioned in a simpler way.  For example, an integer range
3467  * and an integer can be unioned if the integer is a subset of the
3468  * integer range.  If there is the possibility that two values can
3469  * be unioned, this function returns TRUE.
3470  *
3471  * Returns: TRUE if there is a function allowing the two values to
3472  * be unioned.
3473  */
3474 gboolean
3475 gst_value_can_union (const GValue * value1, const GValue * value2)
3476 {
3477   GstValueUnionInfo *union_info;
3478   guint i, len;
3479
3480   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3481   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3482
3483   len = gst_value_union_funcs->len;
3484
3485   for (i = 0; i < len; i++) {
3486     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
3487     if (union_info->type1 == G_VALUE_TYPE (value1) &&
3488         union_info->type2 == G_VALUE_TYPE (value2))
3489       return TRUE;
3490     if (union_info->type1 == G_VALUE_TYPE (value2) &&
3491         union_info->type2 == G_VALUE_TYPE (value1))
3492       return TRUE;
3493   }
3494
3495   return FALSE;
3496 }
3497
3498 /**
3499  * gst_value_union:
3500  * @dest: (out caller-allocates): the destination value
3501  * @value1: a value to union
3502  * @value2: another value to union
3503  *
3504  * Creates a GValue corresponding to the union of @value1 and @value2.
3505  *
3506  * Returns: always returns %TRUE
3507  */
3508 /* FIXME: change return type to 'void'? */
3509 gboolean
3510 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
3511 {
3512   GstValueUnionInfo *union_info;
3513   guint i, len;
3514
3515   g_return_val_if_fail (dest != NULL, FALSE);
3516   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3517   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3518
3519   len = gst_value_union_funcs->len;
3520
3521   for (i = 0; i < len; i++) {
3522     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
3523     if (union_info->type1 == G_VALUE_TYPE (value1) &&
3524         union_info->type2 == G_VALUE_TYPE (value2)) {
3525       if (union_info->func (dest, value1, value2)) {
3526         return TRUE;
3527       }
3528     }
3529     if (union_info->type1 == G_VALUE_TYPE (value2) &&
3530         union_info->type2 == G_VALUE_TYPE (value1)) {
3531       if (union_info->func (dest, value2, value1)) {
3532         return TRUE;
3533       }
3534     }
3535   }
3536
3537   gst_value_list_concat (dest, value1, value2);
3538   return TRUE;
3539 }
3540
3541 /**
3542  * gst_value_register_union_func:
3543  * @type1: a type to union
3544  * @type2: another type to union
3545  * @func: a function that implments creating a union between the two types
3546  *
3547  * Registers a union function that can create a union between #GValue items
3548  * of the type @type1 and @type2.
3549  *
3550  * Union functions should be registered at startup before any pipelines are
3551  * started, as gst_value_register_union_func() is not thread-safe and cannot
3552  * be used at the same time as gst_value_union() or gst_value_can_union().
3553  */
3554 void
3555 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
3556 {
3557   GstValueUnionInfo union_info;
3558
3559   union_info.type1 = type1;
3560   union_info.type2 = type2;
3561   union_info.func = func;
3562
3563   g_array_append_val (gst_value_union_funcs, union_info);
3564 }
3565
3566 /* intersection */
3567
3568 /**
3569  * gst_value_can_intersect:
3570  * @value1: a value to intersect
3571  * @value2: another value to intersect
3572  *
3573  * Determines if intersecting two values will produce a valid result.
3574  * Two values will produce a valid intersection if they have the same
3575  * type, or if there is a method (registered by
3576  * gst_value_register_intersect_func()) to calculate the intersection.
3577  *
3578  * Returns: TRUE if the values can intersect
3579  */
3580 gboolean
3581 gst_value_can_intersect (const GValue * value1, const GValue * value2)
3582 {
3583   GstValueIntersectInfo *intersect_info;
3584   guint i, len;
3585   GType ltype, type1, type2;
3586
3587   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3588   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3589
3590   ltype = gst_value_list_get_type ();
3591
3592   /* special cases */
3593   if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
3594     return TRUE;
3595
3596   type1 = G_VALUE_TYPE (value1);
3597   type2 = G_VALUE_TYPE (value2);
3598
3599   /* practically all GstValue types have a compare function (_can_compare=TRUE)
3600    * GstStructure and GstCaps have npot, but are intersectable */
3601   if (type1 == type2)
3602     return TRUE;
3603
3604   /* check registered intersect functions */
3605   len = gst_value_intersect_funcs->len;
3606   for (i = 0; i < len; i++) {
3607     intersect_info = &g_array_index (gst_value_intersect_funcs,
3608         GstValueIntersectInfo, i);
3609     if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
3610         (intersect_info->type1 == type2 && intersect_info->type2 == type1))
3611       return TRUE;
3612   }
3613
3614   return gst_value_can_compare (value1, value2);
3615 }
3616
3617 /**
3618  * gst_value_intersect:
3619  * @dest: (out caller-allocates): a uninitialized #GValue that will hold the calculated
3620  * intersection value
3621  * @value1: a value to intersect
3622  * @value2: another value to intersect
3623  *
3624  * Calculates the intersection of two values.  If the values have
3625  * a non-empty intersection, the value representing the intersection
3626  * is placed in @dest.  If the intersection is non-empty, @dest is
3627  * not modified.
3628  *
3629  * Returns: TRUE if the intersection is non-empty
3630  */
3631 gboolean
3632 gst_value_intersect (GValue * dest, const GValue * value1,
3633     const GValue * value2)
3634 {
3635   GstValueIntersectInfo *intersect_info;
3636   guint i, len;
3637   GType ltype, type1, type2;
3638
3639   g_return_val_if_fail (dest != NULL, FALSE);
3640   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3641   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3642
3643   ltype = gst_value_list_get_type ();
3644
3645   /* special cases first */
3646   if (G_VALUE_HOLDS (value1, ltype))
3647     return gst_value_intersect_list (dest, value1, value2);
3648   if (G_VALUE_HOLDS (value2, ltype))
3649     return gst_value_intersect_list (dest, value2, value1);
3650
3651   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
3652     gst_value_init_and_copy (dest, value1);
3653     return TRUE;
3654   }
3655
3656   type1 = G_VALUE_TYPE (value1);
3657   type2 = G_VALUE_TYPE (value2);
3658
3659   len = gst_value_intersect_funcs->len;
3660   for (i = 0; i < len; i++) {
3661     intersect_info = &g_array_index (gst_value_intersect_funcs,
3662         GstValueIntersectInfo, i);
3663     if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
3664       return intersect_info->func (dest, value1, value2);
3665     }
3666     if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
3667       return intersect_info->func (dest, value2, value1);
3668     }
3669   }
3670   return FALSE;
3671 }
3672
3673 /**
3674  * gst_value_register_intersect_func:
3675  * @type1: the first type to intersect
3676  * @type2: the second type to intersect
3677  * @func: the intersection function
3678  *
3679  * Registers a function that is called to calculate the intersection
3680  * of the values having the types @type1 and @type2.
3681  *
3682  * Intersect functions should be registered at startup before any pipelines are
3683  * started, as gst_value_register_intersect_func() is not thread-safe and
3684  * cannot be used at the same time as gst_value_intersect() or
3685  * gst_value_can_intersect().
3686  */
3687 void
3688 gst_value_register_intersect_func (GType type1, GType type2,
3689     GstValueIntersectFunc func)
3690 {
3691   GstValueIntersectInfo intersect_info;
3692
3693   intersect_info.type1 = type1;
3694   intersect_info.type2 = type2;
3695   intersect_info.func = func;
3696
3697   g_array_append_val (gst_value_intersect_funcs, intersect_info);
3698 }
3699
3700
3701 /* subtraction */
3702
3703 /**
3704  * gst_value_subtract:
3705  * @dest: (out caller-allocates): the destination value for the result if the
3706  *     subtraction is not empty
3707  * @minuend: the value to subtract from
3708  * @subtrahend: the value to subtract
3709  *
3710  * Subtracts @subtrahend from @minuend and stores the result in @dest.
3711  * Note that this means subtraction as in sets, not as in mathematics.
3712  *
3713  * Returns: %TRUE if the subtraction is not empty
3714  */
3715 gboolean
3716 gst_value_subtract (GValue * dest, const GValue * minuend,
3717     const GValue * subtrahend)
3718 {
3719   GstValueSubtractInfo *info;
3720   guint i, len;
3721   GType ltype, mtype, stype;
3722
3723   g_return_val_if_fail (dest != NULL, FALSE);
3724   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
3725   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
3726
3727   ltype = gst_value_list_get_type ();
3728
3729   /* special cases first */
3730   if (G_VALUE_HOLDS (minuend, ltype))
3731     return gst_value_subtract_from_list (dest, minuend, subtrahend);
3732   if (G_VALUE_HOLDS (subtrahend, ltype))
3733     return gst_value_subtract_list (dest, minuend, subtrahend);
3734
3735   mtype = G_VALUE_TYPE (minuend);
3736   stype = G_VALUE_TYPE (subtrahend);
3737
3738   len = gst_value_subtract_funcs->len;
3739   for (i = 0; i < len; i++) {
3740     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3741     if (info->minuend == mtype && info->subtrahend == stype) {
3742       return info->func (dest, minuend, subtrahend);
3743     }
3744   }
3745
3746   if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3747     gst_value_init_and_copy (dest, minuend);
3748     return TRUE;
3749   }
3750
3751   return FALSE;
3752 }
3753
3754 #if 0
3755 gboolean
3756 gst_value_subtract (GValue * dest, const GValue * minuend,
3757     const GValue * subtrahend)
3758 {
3759   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3760
3761   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
3762       gst_value_serialize (subtrahend),
3763       ret ? gst_value_serialize (dest) : "---");
3764   return ret;
3765 }
3766 #endif
3767
3768 /**
3769  * gst_value_can_subtract:
3770  * @minuend: the value to subtract from
3771  * @subtrahend: the value to subtract
3772  *
3773  * Checks if it's possible to subtract @subtrahend from @minuend.
3774  *
3775  * Returns: TRUE if a subtraction is possible
3776  */
3777 gboolean
3778 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3779 {
3780   GstValueSubtractInfo *info;
3781   guint i, len;
3782   GType ltype, mtype, stype;
3783
3784   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
3785   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
3786
3787   ltype = gst_value_list_get_type ();
3788
3789   /* special cases */
3790   if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
3791     return TRUE;
3792
3793   mtype = G_VALUE_TYPE (minuend);
3794   stype = G_VALUE_TYPE (subtrahend);
3795
3796   len = gst_value_subtract_funcs->len;
3797   for (i = 0; i < len; i++) {
3798     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3799     if (info->minuend == mtype && info->subtrahend == stype)
3800       return TRUE;
3801   }
3802
3803   return gst_value_can_compare (minuend, subtrahend);
3804 }
3805
3806 /**
3807  * gst_value_register_subtract_func:
3808  * @minuend_type: type of the minuend
3809  * @subtrahend_type: type of the subtrahend
3810  * @func: function to use
3811  *
3812  * Registers @func as a function capable of subtracting the values of
3813  * @subtrahend_type from values of @minuend_type.
3814  *
3815  * Subtract functions should be registered at startup before any pipelines are
3816  * started, as gst_value_register_subtract_func() is not thread-safe and
3817  * cannot be used at the same time as gst_value_subtract().
3818  */
3819 void
3820 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3821     GstValueSubtractFunc func)
3822 {
3823   GstValueSubtractInfo info;
3824
3825   /* one type must be unfixed, other subtractions can be done as comparisons */
3826   g_return_if_fail (!gst_type_is_fixed (minuend_type)
3827       || !gst_type_is_fixed (subtrahend_type));
3828
3829   info.minuend = minuend_type;
3830   info.subtrahend = subtrahend_type;
3831   info.func = func;
3832
3833   g_array_append_val (gst_value_subtract_funcs, info);
3834 }
3835
3836 /**
3837  * gst_value_register:
3838  * @table: structure containing functions to register
3839  *
3840  * Registers functions to perform calculations on #GValue items of a given
3841  * type. Each type can only be added once.
3842  */
3843 void
3844 gst_value_register (const GstValueTable * table)
3845 {
3846   GstValueTable *found;
3847
3848   g_return_if_fail (table != NULL);
3849
3850   g_array_append_val (gst_value_table, *table);
3851
3852   found = gst_value_hash_lookup_type (table->type);
3853   if (found)
3854     g_warning ("adding type %s multiple times", g_type_name (table->type));
3855
3856   /* FIXME: we're not really doing the const justice, we assume the table is
3857    * static */
3858   gst_value_hash_add_type (table->type, table);
3859 }
3860
3861 /**
3862  * gst_value_init_and_copy:
3863  * @dest: (out caller-allocates): the target value
3864  * @src: the source value
3865  *
3866  * Initialises the target value to be of the same type as source and then copies
3867  * the contents from source to target.
3868  */
3869 void
3870 gst_value_init_and_copy (GValue * dest, const GValue * src)
3871 {
3872   g_return_if_fail (G_IS_VALUE (src));
3873   g_return_if_fail (dest != NULL);
3874
3875   g_value_init (dest, G_VALUE_TYPE (src));
3876   g_value_copy (src, dest);
3877 }
3878
3879 /**
3880  * gst_value_serialize:
3881  * @value: a #GValue to serialize
3882  *
3883  * tries to transform the given @value into a string representation that allows
3884  * getting back this string later on using gst_value_deserialize().
3885  *
3886  * Free-function: g_free
3887  *
3888  * Returns: (transfer full): the serialization for @value or NULL if none exists
3889  */
3890 gchar *
3891 gst_value_serialize (const GValue * value)
3892 {
3893   guint i, len;
3894   GValue s_val = { 0 };
3895   GstValueTable *table, *best;
3896   gchar *s;
3897   GType type;
3898
3899   g_return_val_if_fail (G_IS_VALUE (value), NULL);
3900
3901   type = G_VALUE_TYPE (value);
3902
3903   best = gst_value_hash_lookup_type (type);
3904
3905   if (G_UNLIKELY (!best || !best->serialize)) {
3906     len = gst_value_table->len;
3907     best = NULL;
3908     for (i = 0; i < len; i++) {
3909       table = &g_array_index (gst_value_table, GstValueTable, i);
3910       if (table->serialize && g_type_is_a (type, table->type)) {
3911         if (!best || g_type_is_a (table->type, best->type))
3912           best = table;
3913       }
3914     }
3915   }
3916   if (G_LIKELY (best))
3917     return best->serialize (value);
3918
3919   g_value_init (&s_val, G_TYPE_STRING);
3920   if (g_value_transform (value, &s_val)) {
3921     s = gst_string_wrap (g_value_get_string (&s_val));
3922   } else {
3923     s = NULL;
3924   }
3925   g_value_unset (&s_val);
3926
3927   return s;
3928 }
3929
3930 /**
3931  * gst_value_deserialize:
3932  * @dest: (out caller-allocates): #GValue to fill with contents of
3933  *     deserialization
3934  * @src: string to deserialize
3935  *
3936  * Tries to deserialize a string into the type specified by the given GValue.
3937  * If the operation succeeds, TRUE is returned, FALSE otherwise.
3938  *
3939  * Returns: TRUE on success
3940  */
3941 gboolean
3942 gst_value_deserialize (GValue * dest, const gchar * src)
3943 {
3944   GstValueTable *table, *best;
3945   guint i, len;
3946   GType type;
3947
3948   g_return_val_if_fail (src != NULL, FALSE);
3949   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
3950
3951   type = G_VALUE_TYPE (dest);
3952
3953   best = gst_value_hash_lookup_type (type);
3954   if (G_UNLIKELY (!best || !best->deserialize)) {
3955     len = gst_value_table->len;
3956     best = NULL;
3957     for (i = 0; i < len; i++) {
3958       table = &g_array_index (gst_value_table, GstValueTable, i);
3959       if (table->deserialize && g_type_is_a (type, table->type)) {
3960         if (!best || g_type_is_a (table->type, best->type))
3961           best = table;
3962       }
3963     }
3964   }
3965   if (G_LIKELY (best))
3966     return best->deserialize (dest, src);
3967
3968   return FALSE;
3969 }
3970
3971 /**
3972  * gst_value_is_fixed:
3973  * @value: the #GValue to check
3974  *
3975  * Tests if the given GValue, if available in a GstStructure (or any other
3976  * container) contains a "fixed" (which means: one value) or an "unfixed"
3977  * (which means: multiple possible values, such as data lists or data
3978  * ranges) value.
3979  *
3980  * Returns: true if the value is "fixed".
3981  */
3982
3983 gboolean
3984 gst_value_is_fixed (const GValue * value)
3985 {
3986   GType type;
3987
3988   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
3989
3990   type = G_VALUE_TYPE (value);
3991
3992   /* the most common types are just basic plain glib types */
3993   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
3994     return TRUE;
3995   }
3996
3997   if (type == GST_TYPE_ARRAY) {
3998     gint size, n;
3999     const GValue *kid;
4000
4001     /* check recursively */
4002     size = gst_value_array_get_size (value);
4003     for (n = 0; n < size; n++) {
4004       kid = gst_value_array_get_value (value, n);
4005       if (!gst_value_is_fixed (kid))
4006         return FALSE;
4007     }
4008     return TRUE;
4009   }
4010   return gst_type_is_fixed (type);
4011 }
4012
4013 /************
4014  * fraction *
4015  ************/
4016
4017 /* helper functions */
4018 static void
4019 gst_value_init_fraction (GValue * value)
4020 {
4021   value->data[0].v_int = 0;
4022   value->data[1].v_int = 1;
4023 }
4024
4025 static void
4026 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
4027 {
4028   dest_value->data[0].v_int = src_value->data[0].v_int;
4029   dest_value->data[1].v_int = src_value->data[1].v_int;
4030 }
4031
4032 static gchar *
4033 gst_value_collect_fraction (GValue * value, guint n_collect_values,
4034     GTypeCValue * collect_values, guint collect_flags)
4035 {
4036   if (n_collect_values != 2)
4037     return g_strdup_printf ("not enough value locations for `%s' passed",
4038         G_VALUE_TYPE_NAME (value));
4039   if (collect_values[1].v_int == 0)
4040     return g_strdup_printf ("passed '0' as denominator for `%s'",
4041         G_VALUE_TYPE_NAME (value));
4042   if (collect_values[0].v_int < -G_MAXINT)
4043     return
4044         g_strdup_printf
4045         ("passed value smaller than -G_MAXINT as numerator for `%s'",
4046         G_VALUE_TYPE_NAME (value));
4047   if (collect_values[1].v_int < -G_MAXINT)
4048     return
4049         g_strdup_printf
4050         ("passed value smaller than -G_MAXINT as denominator for `%s'",
4051         G_VALUE_TYPE_NAME (value));
4052
4053   gst_value_set_fraction (value,
4054       collect_values[0].v_int, collect_values[1].v_int);
4055
4056   return NULL;
4057 }
4058
4059 static gchar *
4060 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
4061     GTypeCValue * collect_values, guint collect_flags)
4062 {
4063   gint *numerator = collect_values[0].v_pointer;
4064   gint *denominator = collect_values[1].v_pointer;
4065
4066   if (!numerator)
4067     return g_strdup_printf ("numerator for `%s' passed as NULL",
4068         G_VALUE_TYPE_NAME (value));
4069   if (!denominator)
4070     return g_strdup_printf ("denominator for `%s' passed as NULL",
4071         G_VALUE_TYPE_NAME (value));
4072
4073   *numerator = value->data[0].v_int;
4074   *denominator = value->data[1].v_int;
4075
4076   return NULL;
4077 }
4078
4079 /**
4080  * gst_value_set_fraction:
4081  * @value: a GValue initialized to #GST_TYPE_FRACTION
4082  * @numerator: the numerator of the fraction
4083  * @denominator: the denominator of the fraction
4084  *
4085  * Sets @value to the fraction specified by @numerator over @denominator.
4086  * The fraction gets reduced to the smallest numerator and denominator,
4087  * and if necessary the sign is moved to the numerator.
4088  */
4089 void
4090 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
4091 {
4092   gint gcd = 0;
4093
4094   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
4095   g_return_if_fail (denominator != 0);
4096   g_return_if_fail (denominator >= -G_MAXINT);
4097   g_return_if_fail (numerator >= -G_MAXINT);
4098
4099   /* normalize sign */
4100   if (denominator < 0) {
4101     numerator = -numerator;
4102     denominator = -denominator;
4103   }
4104
4105   /* check for reduction */
4106   gcd = gst_util_greatest_common_divisor (numerator, denominator);
4107   if (gcd) {
4108     numerator /= gcd;
4109     denominator /= gcd;
4110   }
4111
4112   g_assert (denominator > 0);
4113
4114   value->data[0].v_int = numerator;
4115   value->data[1].v_int = denominator;
4116 }
4117
4118 /**
4119  * gst_value_get_fraction_numerator:
4120  * @value: a GValue initialized to #GST_TYPE_FRACTION
4121  *
4122  * Gets the numerator of the fraction specified by @value.
4123  *
4124  * Returns: the numerator of the fraction.
4125  */
4126 gint
4127 gst_value_get_fraction_numerator (const GValue * value)
4128 {
4129   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
4130
4131   return value->data[0].v_int;
4132 }
4133
4134 /**
4135  * gst_value_get_fraction_denominator:
4136  * @value: a GValue initialized to #GST_TYPE_FRACTION
4137  *
4138  * Gets the denominator of the fraction specified by @value.
4139  *
4140  * Returns: the denominator of the fraction.
4141  */
4142 gint
4143 gst_value_get_fraction_denominator (const GValue * value)
4144 {
4145   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
4146
4147   return value->data[1].v_int;
4148 }
4149
4150 /**
4151  * gst_value_fraction_multiply:
4152  * @product: a GValue initialized to #GST_TYPE_FRACTION
4153  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
4154  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
4155  *
4156  * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
4157  * @product to the product of the two fractions.
4158  *
4159  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4160  */
4161 gboolean
4162 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
4163     const GValue * factor2)
4164 {
4165   gint n1, n2, d1, d2;
4166   gint res_n, res_d;
4167
4168   g_return_val_if_fail (product != NULL, FALSE);
4169   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
4170   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
4171
4172   n1 = factor1->data[0].v_int;
4173   n2 = factor2->data[0].v_int;
4174   d1 = factor1->data[1].v_int;
4175   d2 = factor2->data[1].v_int;
4176
4177   if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
4178     return FALSE;
4179
4180   gst_value_set_fraction (product, res_n, res_d);
4181
4182   return TRUE;
4183 }
4184
4185 /**
4186  * gst_value_fraction_subtract:
4187  * @dest: a GValue initialized to #GST_TYPE_FRACTION
4188  * @minuend: a GValue initialized to #GST_TYPE_FRACTION
4189  * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
4190  *
4191  * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
4192  *
4193  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4194  */
4195 gboolean
4196 gst_value_fraction_subtract (GValue * dest,
4197     const GValue * minuend, const GValue * subtrahend)
4198 {
4199   gint n1, n2, d1, d2;
4200   gint res_n, res_d;
4201
4202   g_return_val_if_fail (dest != NULL, FALSE);
4203   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
4204   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
4205
4206   n1 = minuend->data[0].v_int;
4207   n2 = subtrahend->data[0].v_int;
4208   d1 = minuend->data[1].v_int;
4209   d2 = subtrahend->data[1].v_int;
4210
4211   if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
4212     return FALSE;
4213   gst_value_set_fraction (dest, res_n, res_d);
4214
4215   return TRUE;
4216 }
4217
4218 static gchar *
4219 gst_value_serialize_fraction (const GValue * value)
4220 {
4221   gint32 numerator = value->data[0].v_int;
4222   gint32 denominator = value->data[1].v_int;
4223   gboolean positive = TRUE;
4224
4225   /* get the sign and make components absolute */
4226   if (numerator < 0) {
4227     numerator = -numerator;
4228     positive = !positive;
4229   }
4230   if (denominator < 0) {
4231     denominator = -denominator;
4232     positive = !positive;
4233   }
4234
4235   return g_strdup_printf ("%s%d/%d",
4236       positive ? "" : "-", numerator, denominator);
4237 }
4238
4239 static gboolean
4240 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
4241 {
4242   gint num, den;
4243   gint num_chars;
4244
4245   if (G_UNLIKELY (s == NULL))
4246     return FALSE;
4247
4248   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
4249     return FALSE;
4250
4251   if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
4252     if (s[num_chars] != 0)
4253       return FALSE;
4254     if (den == 0)
4255       return FALSE;
4256
4257     gst_value_set_fraction (dest, num, den);
4258     return TRUE;
4259   } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
4260     gst_value_set_fraction (dest, 1, G_MAXINT);
4261     return TRUE;
4262   } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
4263     if (s[num_chars] != 0)
4264       return FALSE;
4265     gst_value_set_fraction (dest, num, 1);
4266     return TRUE;
4267   } else if (g_ascii_strcasecmp (s, "min") == 0) {
4268     gst_value_set_fraction (dest, -G_MAXINT, 1);
4269     return TRUE;
4270   } else if (g_ascii_strcasecmp (s, "max") == 0) {
4271     gst_value_set_fraction (dest, G_MAXINT, 1);
4272     return TRUE;
4273   }
4274
4275   return FALSE;
4276 }
4277
4278 static void
4279 gst_value_transform_fraction_string (const GValue * src_value,
4280     GValue * dest_value)
4281 {
4282   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
4283 }
4284
4285 static void
4286 gst_value_transform_string_fraction (const GValue * src_value,
4287     GValue * dest_value)
4288 {
4289   if (!gst_value_deserialize_fraction (dest_value,
4290           src_value->data[0].v_pointer))
4291     /* If the deserialize fails, ensure we leave the fraction in a
4292      * valid, if incorrect, state */
4293     gst_value_set_fraction (dest_value, 0, 1);
4294 }
4295
4296 static void
4297 gst_value_transform_double_fraction (const GValue * src_value,
4298     GValue * dest_value)
4299 {
4300   gdouble src = g_value_get_double (src_value);
4301   gint n, d;
4302
4303   gst_util_double_to_fraction (src, &n, &d);
4304   gst_value_set_fraction (dest_value, n, d);
4305 }
4306
4307 static void
4308 gst_value_transform_float_fraction (const GValue * src_value,
4309     GValue * dest_value)
4310 {
4311   gfloat src = g_value_get_float (src_value);
4312   gint n, d;
4313
4314   gst_util_double_to_fraction (src, &n, &d);
4315   gst_value_set_fraction (dest_value, n, d);
4316 }
4317
4318 static void
4319 gst_value_transform_fraction_double (const GValue * src_value,
4320     GValue * dest_value)
4321 {
4322   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
4323       ((double) src_value->data[1].v_int);
4324 }
4325
4326 static void
4327 gst_value_transform_fraction_float (const GValue * src_value,
4328     GValue * dest_value)
4329 {
4330   dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
4331       ((float) src_value->data[1].v_int);
4332 }
4333
4334 static gint
4335 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
4336 {
4337   gint n1, n2;
4338   gint d1, d2;
4339   gint ret;
4340
4341   n1 = value1->data[0].v_int;
4342   n2 = value2->data[0].v_int;
4343   d1 = value1->data[1].v_int;
4344   d2 = value2->data[1].v_int;
4345
4346   /* fractions are reduced when set, so we can quickly see if they're equal */
4347   if (n1 == n2 && d1 == d2)
4348     return GST_VALUE_EQUAL;
4349
4350   if (d1 == 0 && d2 == 0)
4351     return GST_VALUE_UNORDERED;
4352   else if (d1 == 0)
4353     return GST_VALUE_GREATER_THAN;
4354   else if (d2 == 0)
4355     return GST_VALUE_LESS_THAN;
4356
4357   ret = gst_util_fraction_compare (n1, d1, n2, d2);
4358   if (ret == -1)
4359     return GST_VALUE_LESS_THAN;
4360   else if (ret == 1)
4361     return GST_VALUE_GREATER_THAN;
4362
4363   /* Equality can't happen here because we check for that
4364    * first already */
4365   g_return_val_if_reached (GST_VALUE_UNORDERED);
4366 }
4367
4368 /*********
4369  * GDate *
4370  *********/
4371
4372 /**
4373  * gst_value_set_date:
4374  * @value: a GValue initialized to GST_TYPE_DATE
4375  * @date: the date to set the value to
4376  *
4377  * Sets the contents of @value to coorespond to @date.  The actual
4378  * #GDate structure is copied before it is used.
4379  */
4380 void
4381 gst_value_set_date (GValue * value, const GDate * date)
4382 {
4383   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
4384   g_return_if_fail (g_date_valid (date));
4385
4386   g_value_set_boxed (value, date);
4387 }
4388
4389 /**
4390  * gst_value_get_date:
4391  * @value: a GValue initialized to GST_TYPE_DATE
4392  *
4393  * Gets the contents of @value.
4394  *
4395  * Returns: (transfer none): the contents of @value
4396  */
4397 const GDate *
4398 gst_value_get_date (const GValue * value)
4399 {
4400   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
4401
4402   return (const GDate *) g_value_get_boxed (value);
4403 }
4404
4405 static gpointer
4406 gst_date_copy (gpointer boxed)
4407 {
4408   const GDate *date = (const GDate *) boxed;
4409
4410   if (!g_date_valid (date)) {
4411     GST_WARNING ("invalid GDate");
4412     return NULL;
4413   }
4414
4415   return g_date_new_julian (g_date_get_julian (date));
4416 }
4417
4418 static gint
4419 gst_value_compare_date (const GValue * value1, const GValue * value2)
4420 {
4421   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
4422   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
4423   guint32 j1, j2;
4424
4425   if (date1 == date2)
4426     return GST_VALUE_EQUAL;
4427
4428   if ((date1 == NULL || !g_date_valid (date1))
4429       && (date2 != NULL && g_date_valid (date2))) {
4430     return GST_VALUE_LESS_THAN;
4431   }
4432
4433   if ((date2 == NULL || !g_date_valid (date2))
4434       && (date1 != NULL && g_date_valid (date1))) {
4435     return GST_VALUE_GREATER_THAN;
4436   }
4437
4438   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
4439       || !g_date_valid (date2)) {
4440     return GST_VALUE_UNORDERED;
4441   }
4442
4443   j1 = g_date_get_julian (date1);
4444   j2 = g_date_get_julian (date2);
4445
4446   if (j1 == j2)
4447     return GST_VALUE_EQUAL;
4448   else if (j1 < j2)
4449     return GST_VALUE_LESS_THAN;
4450   else
4451     return GST_VALUE_GREATER_THAN;
4452 }
4453
4454 static gchar *
4455 gst_value_serialize_date (const GValue * val)
4456 {
4457   const GDate *date = (const GDate *) g_value_get_boxed (val);
4458
4459   if (date == NULL || !g_date_valid (date))
4460     return g_strdup ("9999-99-99");
4461
4462   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
4463       g_date_get_month (date), g_date_get_day (date));
4464 }
4465
4466 static gboolean
4467 gst_value_deserialize_date (GValue * dest, const gchar * s)
4468 {
4469   guint year, month, day;
4470
4471   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
4472     return FALSE;
4473
4474   if (!g_date_valid_dmy (day, month, year))
4475     return FALSE;
4476
4477   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
4478   return TRUE;
4479 }
4480
4481 /*************
4482  * GstDateTime *
4483  *************/
4484
4485 static gint
4486 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
4487 {
4488   const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
4489   const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
4490   gint ret;
4491
4492   if (date1 == date2)
4493     return GST_VALUE_EQUAL;
4494
4495   if ((date1 == NULL) && (date2 != NULL)) {
4496     return GST_VALUE_LESS_THAN;
4497   }
4498   if ((date2 == NULL) && (date1 != NULL)) {
4499     return GST_VALUE_LESS_THAN;
4500   }
4501
4502   ret = priv_gst_date_time_compare (date1, date2);
4503
4504   if (ret == 0)
4505     return GST_VALUE_EQUAL;
4506   else if (ret < 0)
4507     return GST_VALUE_LESS_THAN;
4508   else
4509     return GST_VALUE_GREATER_THAN;
4510 }
4511
4512 static gchar *
4513 gst_value_serialize_date_time (const GValue * val)
4514 {
4515   GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
4516   gfloat offset;
4517   gint tzhour, tzminute;
4518
4519   if (date == NULL)
4520     return g_strdup ("null");
4521
4522   offset = gst_date_time_get_time_zone_offset (date);
4523
4524   tzhour = (gint) ABS (offset);
4525   tzminute = (gint) ((ABS (offset) - tzhour) * 60);
4526
4527   return g_strdup_printf ("\"%04d-%02d-%02dT%02d:%02d:%02d.%06d"
4528       "%c%02d%02d\"", gst_date_time_get_year (date),
4529       gst_date_time_get_month (date), gst_date_time_get_day (date),
4530       gst_date_time_get_hour (date), gst_date_time_get_minute (date),
4531       gst_date_time_get_second (date), gst_date_time_get_microsecond (date),
4532       offset >= 0 ? '+' : '-', tzhour, tzminute);
4533 }
4534
4535 static gboolean
4536 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
4537 {
4538   gint year, month, day, hour, minute, second, usecond;
4539   gchar signal;
4540   gint offset = 0;
4541   gfloat tzoffset = 0;
4542   gint ret;
4543
4544   if (!s || strcmp (s, "null") == 0) {
4545     return FALSE;
4546   }
4547
4548   ret = sscanf (s, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%04d",
4549       &year, &month, &day, &hour, &minute, &second, &usecond, &signal, &offset);
4550   if (ret >= 9) {
4551     tzoffset = (offset / 100) + ((offset % 100) / 60.0);
4552     if (signal == '-')
4553       tzoffset = -tzoffset;
4554   } else
4555     return FALSE;
4556
4557   g_value_take_boxed (dest, gst_date_time_new (tzoffset, year, month, day, hour,
4558           minute, second + (usecond / 1000000.0)));
4559   return TRUE;
4560 }
4561
4562 static void
4563 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
4564 {
4565   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
4566 }
4567
4568 static void
4569 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
4570 {
4571   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
4572 }
4573
4574 static void
4575 gst_value_transform_object_string (const GValue * src_value,
4576     GValue * dest_value)
4577 {
4578   GstObject *obj;
4579   gchar *str;
4580
4581   obj = g_value_get_object (src_value);
4582   if (obj) {
4583     str =
4584         g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
4585         GST_OBJECT_NAME (obj));
4586   } else {
4587     str = g_strdup ("NULL");
4588   }
4589
4590   dest_value->data[0].v_pointer = str;
4591 }
4592
4593 static GTypeInfo _info = {
4594   0,
4595   NULL,
4596   NULL,
4597   NULL,
4598   NULL,
4599   NULL,
4600   0,
4601   0,
4602   NULL,
4603   NULL,
4604 };
4605
4606 static GTypeFundamentalInfo _finfo = {
4607   0
4608 };
4609
4610 #define FUNC_VALUE_GET_TYPE(type, name)                         \
4611 GType gst_ ## type ## _get_type (void)                          \
4612 {                                                               \
4613   static volatile GType gst_ ## type ## _type = 0;                       \
4614                                                                 \
4615   if (g_once_init_enter (&gst_ ## type ## _type)) {             \
4616     GType _type;                                        \
4617     _info.value_table = & _gst_ ## type ## _value_table;        \
4618     _type = g_type_register_fundamental (       \
4619         g_type_fundamental_next (),                             \
4620         name, &_info, &_finfo, 0);                              \
4621     g_once_init_leave(&gst_ ## type ## _type, _type);   \
4622   }                                                             \
4623                                                                 \
4624   return gst_ ## type ## _type;                                 \
4625 }
4626
4627 static const GTypeValueTable _gst_fourcc_value_table = {
4628   gst_value_init_fourcc,
4629   NULL,
4630   gst_value_copy_fourcc,
4631   NULL,
4632   (char *) "i",
4633   gst_value_collect_fourcc,
4634   (char *) "p",
4635   gst_value_lcopy_fourcc
4636 };
4637
4638 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
4639
4640 static const GTypeValueTable _gst_int_range_value_table = {
4641   gst_value_init_int_range,
4642   NULL,
4643   gst_value_copy_int_range,
4644   NULL,
4645   (char *) "ii",
4646   gst_value_collect_int_range,
4647   (char *) "pp",
4648   gst_value_lcopy_int_range
4649 };
4650
4651 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
4652
4653 static const GTypeValueTable _gst_int64_range_value_table = {
4654   gst_value_init_int64_range,
4655   NULL,
4656   gst_value_copy_int64_range,
4657   NULL,
4658   (char *) "qq",
4659   gst_value_collect_int64_range,
4660   (char *) "pp",
4661   gst_value_lcopy_int64_range
4662 };
4663
4664 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
4665
4666 static const GTypeValueTable _gst_double_range_value_table = {
4667   gst_value_init_double_range,
4668   NULL,
4669   gst_value_copy_double_range,
4670   NULL,
4671   (char *) "dd",
4672   gst_value_collect_double_range,
4673   (char *) "pp",
4674   gst_value_lcopy_double_range
4675 };
4676
4677 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
4678
4679 static const GTypeValueTable _gst_fraction_range_value_table = {
4680   gst_value_init_fraction_range,
4681   gst_value_free_fraction_range,
4682   gst_value_copy_fraction_range,
4683   NULL,
4684   (char *) "iiii",
4685   gst_value_collect_fraction_range,
4686   (char *) "pppp",
4687   gst_value_lcopy_fraction_range
4688 };
4689
4690 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
4691
4692 static const GTypeValueTable _gst_value_list_value_table = {
4693   gst_value_init_list_or_array,
4694   gst_value_free_list_or_array,
4695   gst_value_copy_list_or_array,
4696   gst_value_list_or_array_peek_pointer,
4697   (char *) "p",
4698   gst_value_collect_list_or_array,
4699   (char *) "p",
4700   gst_value_lcopy_list_or_array
4701 };
4702
4703 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
4704
4705 static const GTypeValueTable _gst_value_array_value_table = {
4706   gst_value_init_list_or_array,
4707   gst_value_free_list_or_array,
4708   gst_value_copy_list_or_array,
4709   gst_value_list_or_array_peek_pointer,
4710   (char *) "p",
4711   gst_value_collect_list_or_array,
4712   (char *) "p",
4713   gst_value_lcopy_list_or_array
4714 };
4715
4716 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
4717
4718 static const GTypeValueTable _gst_fraction_value_table = {
4719   gst_value_init_fraction,
4720   NULL,
4721   gst_value_copy_fraction,
4722   NULL,
4723   (char *) "ii",
4724   gst_value_collect_fraction,
4725   (char *) "pp",
4726   gst_value_lcopy_fraction
4727 };
4728
4729 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
4730
4731
4732 GType
4733 gst_date_get_type (void)
4734 {
4735   static GType gst_date_type = 0;
4736
4737   if (G_UNLIKELY (gst_date_type == 0)) {
4738     /* FIXME 0.11: we require GLib 2.8 already
4739      * Not using G_TYPE_DATE here on purpose, even if we could
4740      * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
4741      * serialised strings to have different type strings depending
4742      * on what version is used, so FIXME when we require GLib-2.8 */
4743     gst_date_type = g_boxed_type_register_static ("GstDate",
4744         (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
4745   }
4746
4747   return gst_date_type;
4748 }
4749
4750 GType
4751 gst_date_time_get_type (void)
4752 {
4753   static GType gst_date_time_type = 0;
4754
4755   if (G_UNLIKELY (gst_date_time_type == 0)) {
4756     gst_date_time_type = g_boxed_type_register_static ("GstDateTime",
4757         (GBoxedCopyFunc) gst_date_time_ref,
4758         (GBoxedFreeFunc) gst_date_time_unref);
4759   }
4760
4761   return gst_date_time_type;
4762 }
4763
4764
4765 void
4766 _gst_value_initialize (void)
4767 {
4768   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
4769   gst_value_hash = g_hash_table_new (NULL, NULL);
4770   gst_value_union_funcs = g_array_new (FALSE, FALSE,
4771       sizeof (GstValueUnionInfo));
4772   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
4773       sizeof (GstValueIntersectInfo));
4774   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4775       sizeof (GstValueSubtractInfo));
4776
4777   {
4778     static GstValueTable gst_value = {
4779       0,
4780       gst_value_compare_fourcc,
4781       gst_value_serialize_fourcc,
4782       gst_value_deserialize_fourcc,
4783     };
4784
4785     gst_value.type = gst_fourcc_get_type ();
4786     gst_value_register (&gst_value);
4787   }
4788
4789   {
4790     static GstValueTable gst_value = {
4791       0,
4792       gst_value_compare_int_range,
4793       gst_value_serialize_int_range,
4794       gst_value_deserialize_int_range,
4795     };
4796
4797     gst_value.type = gst_int_range_get_type ();
4798     gst_value_register (&gst_value);
4799   }
4800
4801   {
4802     static GstValueTable gst_value = {
4803       0,
4804       gst_value_compare_int64_range,
4805       gst_value_serialize_int64_range,
4806       gst_value_deserialize_int64_range,
4807     };
4808
4809     gst_value.type = gst_int64_range_get_type ();
4810     gst_value_register (&gst_value);
4811   }
4812
4813   {
4814     static GstValueTable gst_value = {
4815       0,
4816       gst_value_compare_double_range,
4817       gst_value_serialize_double_range,
4818       gst_value_deserialize_double_range,
4819     };
4820
4821     gst_value.type = gst_double_range_get_type ();
4822     gst_value_register (&gst_value);
4823   }
4824
4825   {
4826     static GstValueTable gst_value = {
4827       0,
4828       gst_value_compare_fraction_range,
4829       gst_value_serialize_fraction_range,
4830       gst_value_deserialize_fraction_range,
4831     };
4832
4833     gst_value.type = gst_fraction_range_get_type ();
4834     gst_value_register (&gst_value);
4835   }
4836
4837   {
4838     static GstValueTable gst_value = {
4839       0,
4840       gst_value_compare_list,
4841       gst_value_serialize_list,
4842       gst_value_deserialize_list,
4843     };
4844
4845     gst_value.type = gst_value_list_get_type ();
4846     gst_value_register (&gst_value);
4847   }
4848
4849   {
4850     static GstValueTable gst_value = {
4851       0,
4852       gst_value_compare_array,
4853       gst_value_serialize_array,
4854       gst_value_deserialize_array,
4855     };
4856
4857     gst_value.type = gst_value_array_get_type ();
4858     gst_value_register (&gst_value);
4859   }
4860
4861   {
4862 #if 0
4863     static const GTypeValueTable value_table = {
4864       gst_value_init_buffer,
4865       NULL,
4866       gst_value_copy_buffer,
4867       NULL,
4868       "i",
4869       NULL,                     /*gst_value_collect_buffer, */
4870       "p",
4871       NULL                      /*gst_value_lcopy_buffer */
4872     };
4873 #endif
4874     static GstValueTable gst_value = {
4875       0,
4876       gst_value_compare_buffer,
4877       gst_value_serialize_buffer,
4878       gst_value_deserialize_buffer,
4879     };
4880
4881     gst_value.type = GST_TYPE_BUFFER;
4882     gst_value_register (&gst_value);
4883   }
4884   {
4885     static GstValueTable gst_value = {
4886       0,
4887       gst_value_compare_fraction,
4888       gst_value_serialize_fraction,
4889       gst_value_deserialize_fraction,
4890     };
4891
4892     gst_value.type = gst_fraction_get_type ();
4893     gst_value_register (&gst_value);
4894   }
4895   {
4896     static GstValueTable gst_value = {
4897       0,
4898       NULL,
4899       gst_value_serialize_caps,
4900       gst_value_deserialize_caps,
4901     };
4902
4903     gst_value.type = GST_TYPE_CAPS;
4904     gst_value_register (&gst_value);
4905   }
4906   {
4907     static GstValueTable gst_value = {
4908       0,
4909       NULL,
4910       gst_value_serialize_structure,
4911       gst_value_deserialize_structure,
4912     };
4913
4914     gst_value.type = GST_TYPE_STRUCTURE;
4915     gst_value_register (&gst_value);
4916   }
4917   {
4918     static GstValueTable gst_value = {
4919       0,
4920       gst_value_compare_date,
4921       gst_value_serialize_date,
4922       gst_value_deserialize_date,
4923     };
4924
4925     gst_value.type = gst_date_get_type ();
4926     gst_value_register (&gst_value);
4927   }
4928   {
4929     static GstValueTable gst_value = {
4930       0,
4931       gst_value_compare_date_time,
4932       gst_value_serialize_date_time,
4933       gst_value_deserialize_date_time,
4934     };
4935
4936     gst_value.type = gst_date_time_get_type ();
4937     gst_value_register (&gst_value);
4938   }
4939
4940   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
4941   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
4942
4943   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
4944   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
4945   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
4946
4947   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
4948
4949   REGISTER_SERIALIZATION (G_TYPE_INT, int);
4950
4951   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
4952   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
4953
4954   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
4955   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
4956   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
4957
4958   REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
4959
4960   g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
4961       gst_value_transform_fourcc_string);
4962   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
4963       gst_value_transform_int_range_string);
4964   g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
4965       gst_value_transform_int64_range_string);
4966   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
4967       gst_value_transform_double_range_string);
4968   g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
4969       gst_value_transform_fraction_range_string);
4970   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
4971       gst_value_transform_list_string);
4972   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
4973       gst_value_transform_array_string);
4974   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
4975       gst_value_transform_fraction_string);
4976   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
4977       gst_value_transform_string_fraction);
4978   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
4979       gst_value_transform_fraction_double);
4980   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
4981       gst_value_transform_fraction_float);
4982   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
4983       gst_value_transform_double_fraction);
4984   g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
4985       gst_value_transform_float_fraction);
4986   g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
4987       gst_value_transform_date_string);
4988   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
4989       gst_value_transform_string_date);
4990   g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
4991       gst_value_transform_object_string);
4992
4993   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4994       gst_value_intersect_int_int_range);
4995   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4996       gst_value_intersect_int_range_int_range);
4997   gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
4998       gst_value_intersect_int64_int64_range);
4999   gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5000       gst_value_intersect_int64_range_int64_range);
5001   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5002       gst_value_intersect_double_double_range);
5003   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
5004       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
5005   gst_value_register_intersect_func (GST_TYPE_ARRAY,
5006       GST_TYPE_ARRAY, gst_value_intersect_array);
5007   gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5008       gst_value_intersect_fraction_fraction_range);
5009   gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
5010       GST_TYPE_FRACTION_RANGE,
5011       gst_value_intersect_fraction_range_fraction_range);
5012
5013   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5014       gst_value_subtract_int_int_range);
5015   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
5016       gst_value_subtract_int_range_int);
5017   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5018       gst_value_subtract_int_range_int_range);
5019   gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5020       gst_value_subtract_int64_int64_range);
5021   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
5022       gst_value_subtract_int64_range_int64);
5023   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5024       gst_value_subtract_int64_range_int64_range);
5025   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5026       gst_value_subtract_double_double_range);
5027   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
5028       gst_value_subtract_double_range_double);
5029   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
5030       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
5031
5032   gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5033       gst_value_subtract_fraction_fraction_range);
5034   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
5035       gst_value_subtract_fraction_range_fraction);
5036   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
5037       GST_TYPE_FRACTION_RANGE,
5038       gst_value_subtract_fraction_range_fraction_range);
5039
5040   /* see bug #317246, #64994, #65041 */
5041   {
5042     volatile GType date_type = G_TYPE_DATE;
5043
5044     g_type_name (date_type);
5045   }
5046
5047   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5048       gst_value_union_int_int_range);
5049   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5050       gst_value_union_int_range_int_range);
5051
5052 #if 0
5053   /* Implement these if needed */
5054   gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5055       gst_value_union_fraction_fraction_range);
5056   gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
5057       GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
5058 #endif
5059 }