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