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