gstvalue: Add compare function for caps
[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 gint
1836 gst_value_compare_caps (const GValue * value1, const GValue * value2)
1837 {
1838   GstCaps *caps1 = GST_CAPS (gst_value_get_caps (value1));
1839   GstCaps *caps2 = GST_CAPS (gst_value_get_caps (value2));
1840
1841   if (gst_caps_is_equal (caps1, caps2))
1842     return GST_VALUE_EQUAL;
1843   return GST_VALUE_UNORDERED;
1844 }
1845
1846 static gchar *
1847 gst_value_serialize_caps (const GValue * value)
1848 {
1849   GstCaps *caps = g_value_get_boxed (value);
1850
1851   return gst_caps_to_string (caps);
1852 }
1853
1854 static gboolean
1855 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1856 {
1857   GstCaps *caps;
1858
1859   caps = gst_caps_from_string (s);
1860
1861   if (caps) {
1862     g_value_take_boxed (dest, caps);
1863     return TRUE;
1864   }
1865   return FALSE;
1866 }
1867
1868 /**************
1869  * GstSegment *
1870  **************/
1871
1872 static gchar *
1873 gst_value_serialize_segment_internal (const GValue * value, gboolean escape)
1874 {
1875   GstSegment *seg = g_value_get_boxed (value);
1876   gchar *t, *res;
1877   GstStructure *s;
1878
1879   s = gst_structure_new ("GstSegment",
1880       "flags", GST_TYPE_SEGMENT_FLAGS, seg->flags,
1881       "rate", G_TYPE_DOUBLE, seg->rate,
1882       "applied-rate", G_TYPE_DOUBLE, seg->applied_rate,
1883       "format", GST_TYPE_FORMAT, seg->format,
1884       "base", G_TYPE_UINT64, seg->base,
1885       "offset", G_TYPE_UINT64, seg->offset,
1886       "start", G_TYPE_UINT64, seg->start,
1887       "stop", G_TYPE_UINT64, seg->stop,
1888       "time", G_TYPE_UINT64, seg->time,
1889       "position", G_TYPE_UINT64, seg->position,
1890       "duration", G_TYPE_UINT64, seg->duration, NULL);
1891   t = gst_structure_to_string (s);
1892   if (escape) {
1893     res = g_strdup_printf ("\"%s\"", t);
1894     g_free (t);
1895   } else {
1896     res = t;
1897   }
1898   gst_structure_free (s);
1899
1900   return res;
1901 }
1902
1903 static gchar *
1904 gst_value_serialize_segment (const GValue * value)
1905 {
1906   return gst_value_serialize_segment_internal (value, TRUE);
1907 }
1908
1909 static gboolean
1910 gst_value_deserialize_segment (GValue * dest, const gchar * s)
1911 {
1912   GstStructure *str;
1913   GstSegment seg;
1914   gboolean res;
1915
1916   str = gst_structure_from_string (s, NULL);
1917   if (str == NULL)
1918     return FALSE;
1919
1920   res = gst_structure_get (str,
1921       "flags", GST_TYPE_SEGMENT_FLAGS, &seg.flags,
1922       "rate", G_TYPE_DOUBLE, &seg.rate,
1923       "applied-rate", G_TYPE_DOUBLE, &seg.applied_rate,
1924       "format", GST_TYPE_FORMAT, &seg.format,
1925       "base", G_TYPE_UINT64, &seg.base,
1926       "offset", G_TYPE_UINT64, &seg.offset,
1927       "start", G_TYPE_UINT64, &seg.start,
1928       "stop", G_TYPE_UINT64, &seg.stop,
1929       "time", G_TYPE_UINT64, &seg.time,
1930       "position", G_TYPE_UINT64, &seg.position,
1931       "duration", G_TYPE_UINT64, &seg.duration, NULL);
1932   gst_structure_free (str);
1933
1934   if (res)
1935     g_value_set_boxed (dest, &seg);
1936
1937   return res;
1938 }
1939
1940 /****************
1941  * GstStructure *
1942  ****************/
1943
1944 /**
1945  * gst_value_set_structure:
1946  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1947  * @structure: the structure to set the value to
1948  *
1949  * Sets the contents of @value to @structure.  The actual
1950  */
1951 void
1952 gst_value_set_structure (GValue * value, const GstStructure * structure)
1953 {
1954   g_return_if_fail (G_IS_VALUE (value));
1955   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1956   g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
1957
1958   g_value_set_boxed (value, structure);
1959 }
1960
1961 /**
1962  * gst_value_get_structure:
1963  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1964  *
1965  * Gets the contents of @value.
1966  *
1967  * Returns: (transfer none): the contents of @value
1968  */
1969 const GstStructure *
1970 gst_value_get_structure (const GValue * value)
1971 {
1972   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1973   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1974
1975   return (GstStructure *) g_value_get_boxed (value);
1976 }
1977
1978 static gchar *
1979 gst_value_serialize_structure (const GValue * value)
1980 {
1981   GstStructure *structure = g_value_get_boxed (value);
1982
1983   return gst_string_take_and_wrap (gst_structure_to_string (structure));
1984 }
1985
1986 static gboolean
1987 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1988 {
1989   GstStructure *structure;
1990
1991   if (*s != '"') {
1992     structure = gst_structure_from_string (s, NULL);
1993   } else {
1994     gchar *str = gst_string_unwrap (s);
1995
1996     if (G_UNLIKELY (!str))
1997       return FALSE;
1998
1999     structure = gst_structure_from_string (str, NULL);
2000     g_free (str);
2001   }
2002
2003   if (G_LIKELY (structure)) {
2004     g_value_take_boxed (dest, structure);
2005     return TRUE;
2006   }
2007   return FALSE;
2008 }
2009
2010 /*******************
2011  * GstCapsFeatures *
2012  *******************/
2013
2014 /**
2015  * gst_value_set_caps_features:
2016  * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
2017  * @features: the features to set the value to
2018  *
2019  * Sets the contents of @value to @features.
2020  */
2021 void
2022 gst_value_set_caps_features (GValue * value, const GstCapsFeatures * features)
2023 {
2024   g_return_if_fail (G_IS_VALUE (value));
2025   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES);
2026   g_return_if_fail (features == NULL || GST_IS_CAPS_FEATURES (features));
2027
2028   g_value_set_boxed (value, features);
2029 }
2030
2031 /**
2032  * gst_value_get_caps_features:
2033  * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
2034  *
2035  * Gets the contents of @value.
2036  *
2037  * Returns: (transfer none): the contents of @value
2038  */
2039 const GstCapsFeatures *
2040 gst_value_get_caps_features (const GValue * value)
2041 {
2042   g_return_val_if_fail (G_IS_VALUE (value), NULL);
2043   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES, NULL);
2044
2045   return (GstCapsFeatures *) g_value_get_boxed (value);
2046 }
2047
2048 static gchar *
2049 gst_value_serialize_caps_features (const GValue * value)
2050 {
2051   GstCapsFeatures *features = g_value_get_boxed (value);
2052
2053   return gst_string_take_and_wrap (gst_caps_features_to_string (features));
2054 }
2055
2056 static gboolean
2057 gst_value_deserialize_caps_features (GValue * dest, const gchar * s)
2058 {
2059   GstCapsFeatures *features;
2060
2061   if (*s != '"') {
2062     features = gst_caps_features_from_string (s);
2063   } else {
2064     gchar *str = gst_string_unwrap (s);
2065
2066     if (G_UNLIKELY (!str))
2067       return FALSE;
2068
2069     features = gst_caps_features_from_string (str);
2070     g_free (str);
2071   }
2072
2073   if (G_LIKELY (features)) {
2074     g_value_take_boxed (dest, features);
2075     return TRUE;
2076   }
2077   return FALSE;
2078 }
2079
2080 /**************
2081  * GstTagList *
2082  **************/
2083
2084 static gboolean
2085 gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
2086 {
2087   GstTagList *taglist;
2088
2089   if (*s != '"') {
2090     taglist = gst_tag_list_new_from_string (s);
2091   } else {
2092     gchar *str = gst_string_unwrap (s);
2093
2094     if (G_UNLIKELY (!str))
2095       return FALSE;
2096
2097     taglist = gst_tag_list_new_from_string (str);
2098     g_free (str);
2099   }
2100
2101   if (G_LIKELY (taglist != NULL)) {
2102     g_value_take_boxed (dest, taglist);
2103     return TRUE;
2104   }
2105   return FALSE;
2106 }
2107
2108 static gchar *
2109 gst_value_serialize_tag_list (const GValue * value)
2110 {
2111   GstTagList *taglist = g_value_get_boxed (value);
2112
2113   return gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
2114 }
2115
2116
2117 /*************
2118  * GstBuffer *
2119  *************/
2120
2121 static gint
2122 compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
2123 {
2124   gsize size1, size2;
2125   GstMapInfo info1, info2;
2126   gint result, mret;
2127
2128   if (buf1 == buf2)
2129     return GST_VALUE_EQUAL;
2130
2131   size1 = gst_buffer_get_size (buf1);
2132   size2 = gst_buffer_get_size (buf2);
2133
2134   if (size1 != size2)
2135     return GST_VALUE_UNORDERED;
2136
2137   if (size1 == 0)
2138     return GST_VALUE_EQUAL;
2139
2140   if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
2141     return GST_VALUE_UNORDERED;
2142
2143   if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
2144     gst_buffer_unmap (buf1, &info1);
2145     return GST_VALUE_UNORDERED;
2146   }
2147
2148   mret = memcmp (info1.data, info2.data, info1.size);
2149   if (mret == 0)
2150     result = GST_VALUE_EQUAL;
2151   else if (mret < 0)
2152     result = GST_VALUE_LESS_THAN;
2153   else
2154     result = GST_VALUE_GREATER_THAN;
2155
2156   gst_buffer_unmap (buf1, &info1);
2157   gst_buffer_unmap (buf2, &info2);
2158
2159   return result;
2160 }
2161
2162 static gint
2163 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
2164 {
2165   GstBuffer *buf1 = gst_value_get_buffer (value1);
2166   GstBuffer *buf2 = gst_value_get_buffer (value2);
2167
2168   return compare_buffer (buf1, buf2);
2169 }
2170
2171 static gchar *
2172 gst_value_serialize_buffer (const GValue * value)
2173 {
2174   GstMapInfo info;
2175   guint8 *data;
2176   gint i;
2177   gchar *string;
2178   GstBuffer *buffer;
2179
2180   buffer = gst_value_get_buffer (value);
2181   if (buffer == NULL)
2182     return NULL;
2183
2184   if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
2185     return NULL;
2186
2187   data = info.data;
2188
2189   string = g_malloc (info.size * 2 + 1);
2190   for (i = 0; i < info.size; i++) {
2191     sprintf (string + i * 2, "%02x", data[i]);
2192   }
2193   string[info.size * 2] = 0;
2194
2195   gst_buffer_unmap (buffer, &info);
2196
2197   return string;
2198 }
2199
2200 static gboolean
2201 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
2202 {
2203   GstBuffer *buffer;
2204   gint len;
2205   gchar ts[3];
2206   GstMapInfo info;
2207   guint8 *data;
2208   gint i;
2209
2210   len = strlen (s);
2211   if (len & 1)
2212     goto wrong_length;
2213
2214   buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
2215   if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
2216     goto map_failed;
2217   data = info.data;
2218
2219   for (i = 0; i < len / 2; i++) {
2220     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
2221       goto wrong_char;
2222
2223     ts[0] = s[i * 2 + 0];
2224     ts[1] = s[i * 2 + 1];
2225     ts[2] = 0;
2226
2227     data[i] = (guint8) strtoul (ts, NULL, 16);
2228   }
2229   gst_buffer_unmap (buffer, &info);
2230
2231   gst_value_take_buffer (dest, buffer);
2232
2233   return TRUE;
2234
2235   /* ERRORS */
2236 wrong_length:
2237   {
2238     return FALSE;
2239   }
2240 map_failed:
2241   {
2242     return FALSE;
2243   }
2244 wrong_char:
2245   {
2246     gst_buffer_unref (buffer);
2247     gst_buffer_unmap (buffer, &info);
2248     return FALSE;
2249   }
2250 }
2251
2252 /*************
2253  * GstSample *
2254  *************/
2255
2256 /* This function is mostly used for comparing image/buffer tags in taglists */
2257 static gint
2258 gst_value_compare_sample (const GValue * value1, const GValue * value2)
2259 {
2260   GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
2261   GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
2262
2263   /* FIXME: should we take into account anything else such as caps? */
2264   return compare_buffer (buf1, buf2);
2265 }
2266
2267 static gchar *
2268 gst_value_serialize_sample (const GValue * value)
2269 {
2270   const GstStructure *info_structure;
2271   GstSegment *segment;
2272   GstBuffer *buffer;
2273   GstCaps *caps;
2274   GstSample *sample;
2275   GValue val = { 0, };
2276   gchar *info_str, *caps_str, *tmp;
2277   gchar *buf_str, *seg_str, *s;
2278
2279   sample = g_value_get_boxed (value);
2280
2281   buffer = gst_sample_get_buffer (sample);
2282   if (buffer) {
2283     g_value_init (&val, GST_TYPE_BUFFER);
2284     g_value_set_boxed (&val, buffer);
2285     buf_str = gst_value_serialize_buffer (&val);
2286     g_value_unset (&val);
2287   } else {
2288     buf_str = g_strdup ("None");
2289   }
2290
2291   caps = gst_sample_get_caps (sample);
2292   if (caps) {
2293     tmp = gst_caps_to_string (caps);
2294     caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2295     g_strdelimit (caps_str, "=", '_');
2296     g_free (tmp);
2297   } else {
2298     caps_str = g_strdup ("None");
2299   }
2300
2301   segment = gst_sample_get_segment (sample);
2302   if (segment) {
2303     g_value_init (&val, GST_TYPE_SEGMENT);
2304     g_value_set_boxed (&val, segment);
2305     tmp = gst_value_serialize_segment_internal (&val, FALSE);
2306     seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2307     g_strdelimit (seg_str, "=", '_');
2308     g_free (tmp);
2309     g_value_unset (&val);
2310   } else {
2311     seg_str = g_strdup ("None");
2312   }
2313
2314   info_structure = gst_sample_get_info (sample);
2315   if (info_structure) {
2316     tmp = gst_structure_to_string (info_structure);
2317     info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2318     g_strdelimit (info_str, "=", '_');
2319     g_free (tmp);
2320   } else {
2321     info_str = g_strdup ("None");
2322   }
2323
2324   s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
2325   g_free (buf_str);
2326   g_free (caps_str);
2327   g_free (seg_str);
2328   g_free (info_str);
2329
2330   return s;
2331 }
2332
2333 static gboolean
2334 gst_value_deserialize_sample (GValue * dest, const gchar * s)
2335 {
2336   GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
2337   GstStructure *info;
2338   GstSample *sample;
2339   GstCaps *caps;
2340   gboolean ret = FALSE;
2341   gchar **fields;
2342   gsize outlen;
2343   gint len;
2344
2345   GST_TRACE ("deserialize '%s'", s);
2346
2347   fields = g_strsplit (s, ":", -1);
2348   len = g_strv_length (fields);
2349   if (len != 4)
2350     goto wrong_length;
2351
2352   g_value_init (&bval, GST_TYPE_BUFFER);
2353   g_value_init (&sval, GST_TYPE_SEGMENT);
2354
2355   if (!gst_value_deserialize_buffer (&bval, fields[0]))
2356     goto fail;
2357
2358   if (strcmp (fields[1], "None") != 0) {
2359     g_strdelimit (fields[1], "_", '=');
2360     g_base64_decode_inplace (fields[1], &outlen);
2361     GST_TRACE ("caps    : %s", fields[1]);
2362     caps = gst_caps_from_string (fields[1]);
2363     if (caps == NULL)
2364       goto fail;
2365   } else {
2366     caps = NULL;
2367   }
2368
2369   if (strcmp (fields[2], "None") != 0) {
2370     g_strdelimit (fields[2], "_", '=');
2371     g_base64_decode_inplace (fields[2], &outlen);
2372     GST_TRACE ("segment : %s", fields[2]);
2373     if (!gst_value_deserialize_segment (&sval, fields[2]))
2374       goto fail;
2375   }
2376
2377   if (strcmp (fields[3], "None") != 0) {
2378     g_strdelimit (fields[3], "_", '=');
2379     g_base64_decode_inplace (fields[3], &outlen);
2380     GST_TRACE ("info    : %s", fields[3]);
2381     info = gst_structure_from_string (fields[3], NULL);
2382     if (info == NULL)
2383       goto fail;
2384   } else {
2385     info = NULL;
2386   }
2387
2388   sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
2389       g_value_get_boxed (&sval), info);
2390
2391   g_value_take_boxed (dest, sample);
2392
2393   if (caps)
2394     gst_caps_unref (caps);
2395
2396   ret = TRUE;
2397
2398 fail:
2399
2400   g_value_unset (&bval);
2401   g_value_unset (&sval);
2402
2403 wrong_length:
2404
2405   g_strfreev (fields);
2406
2407   return ret;
2408 }
2409
2410 /***********
2411  * boolean *
2412  ***********/
2413
2414 static gint
2415 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
2416 {
2417   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
2418     return GST_VALUE_EQUAL;
2419   return GST_VALUE_UNORDERED;
2420 }
2421
2422 static gchar *
2423 gst_value_serialize_boolean (const GValue * value)
2424 {
2425   if (value->data[0].v_int) {
2426     return g_strdup ("true");
2427   }
2428   return g_strdup ("false");
2429 }
2430
2431 static gboolean
2432 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
2433 {
2434   gboolean ret = FALSE;
2435
2436   if (g_ascii_strcasecmp (s, "true") == 0 ||
2437       g_ascii_strcasecmp (s, "yes") == 0 ||
2438       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
2439     g_value_set_boolean (dest, TRUE);
2440     ret = TRUE;
2441   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
2442       g_ascii_strcasecmp (s, "no") == 0 ||
2443       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
2444     g_value_set_boolean (dest, FALSE);
2445     ret = TRUE;
2446   }
2447
2448   return ret;
2449 }
2450
2451 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
2452 static gint                                                             \
2453 gst_value_compare_ ## _type                                             \
2454 (const GValue * value1, const GValue * value2)                          \
2455 {                                                                       \
2456   g ## _type val1 = g_value_get_ ## _type (value1);                     \
2457   g ## _type val2 = g_value_get_ ## _type (value2);                     \
2458   if (val1 > val2)                                                      \
2459     return GST_VALUE_GREATER_THAN;                                      \
2460   if (val1 < val2)                                                      \
2461     return GST_VALUE_LESS_THAN;                                         \
2462   return GST_VALUE_EQUAL;                                               \
2463 }                                                                       \
2464                                                                         \
2465 static gchar *                                                          \
2466 gst_value_serialize_ ## _type (const GValue * value)                    \
2467 {                                                                       \
2468   GValue val = { 0, };                                                  \
2469   g_value_init (&val, G_TYPE_STRING);                                   \
2470   if (!g_value_transform (value, &val))                                 \
2471     g_assert_not_reached ();                                            \
2472   /* NO_COPY_MADNESS!!! */                                              \
2473   return (char *) g_value_get_string (&val);                            \
2474 }
2475
2476 /* deserialize the given s into to as a gint64.
2477  * check if the result is actually storeable in the given size number of
2478  * bytes.
2479  */
2480 static gboolean
2481 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
2482     gint64 min, gint64 max, gint size)
2483 {
2484   gboolean ret = FALSE;
2485   gchar *end;
2486   gint64 mask = -1;
2487
2488   errno = 0;
2489   *to = g_ascii_strtoull (s, &end, 0);
2490   /* a range error is a definitive no-no */
2491   if (errno == ERANGE) {
2492     return FALSE;
2493   }
2494
2495   if (*end == 0) {
2496     ret = TRUE;
2497   } else {
2498     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
2499       *to = G_LITTLE_ENDIAN;
2500       ret = TRUE;
2501     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
2502       *to = G_BIG_ENDIAN;
2503       ret = TRUE;
2504     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
2505       *to = G_BYTE_ORDER;
2506       ret = TRUE;
2507     } else if (g_ascii_strcasecmp (s, "min") == 0) {
2508       *to = min;
2509       ret = TRUE;
2510     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2511       *to = max;
2512       ret = TRUE;
2513     }
2514   }
2515   if (ret) {
2516     /* by definition, a gint64 fits into a gint64; so ignore those */
2517     if (size != sizeof (mask)) {
2518       if (*to >= 0) {
2519         /* for positive numbers, we create a mask of 1's outside of the range
2520          * and 0's inside the range.  An and will thus keep only 1 bits
2521          * outside of the range */
2522         mask <<= (size * 8);
2523         if ((mask & *to) != 0) {
2524           ret = FALSE;
2525         }
2526       } else {
2527         /* for negative numbers, we do a 2's complement version */
2528         mask <<= ((size * 8) - 1);
2529         if ((mask & *to) != mask) {
2530           ret = FALSE;
2531         }
2532       }
2533     }
2534   }
2535   return ret;
2536 }
2537
2538 #define CREATE_SERIALIZATION(_type,_macro)                              \
2539 CREATE_SERIALIZATION_START(_type,_macro)                                \
2540                                                                         \
2541 static gboolean                                                         \
2542 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2543 {                                                                       \
2544   gint64 x;                                                             \
2545                                                                         \
2546   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
2547       G_MAX ## _macro, sizeof (g ## _type))) {                          \
2548     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
2549     return TRUE;                                                        \
2550   } else {                                                              \
2551     return FALSE;                                                       \
2552   }                                                                     \
2553 }
2554
2555 #define CREATE_USERIALIZATION(_type,_macro)                             \
2556 CREATE_SERIALIZATION_START(_type,_macro)                                \
2557                                                                         \
2558 static gboolean                                                         \
2559 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2560 {                                                                       \
2561   gint64 x;                                                             \
2562   gchar *end;                                                           \
2563   gboolean ret = FALSE;                                                 \
2564                                                                         \
2565   errno = 0;                                                            \
2566   x = g_ascii_strtoull (s, &end, 0);                                    \
2567   /* a range error is a definitive no-no */                             \
2568   if (errno == ERANGE) {                                                \
2569     return FALSE;                                                       \
2570   }                                                                     \
2571   /* the cast ensures the range check later on makes sense */           \
2572   x = (g ## _type) x;                                                   \
2573   if (*end == 0) {                                                      \
2574     ret = TRUE;                                                         \
2575   } else {                                                              \
2576     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
2577       x = G_LITTLE_ENDIAN;                                              \
2578       ret = TRUE;                                                       \
2579     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
2580       x = G_BIG_ENDIAN;                                                 \
2581       ret = TRUE;                                                       \
2582     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
2583       x = G_BYTE_ORDER;                                                 \
2584       ret = TRUE;                                                       \
2585     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
2586       x = 0;                                                            \
2587       ret = TRUE;                                                       \
2588     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
2589       x = G_MAX ## _macro;                                              \
2590       ret = TRUE;                                                       \
2591     }                                                                   \
2592   }                                                                     \
2593   if (ret) {                                                            \
2594     if (x > G_MAX ## _macro) {                                          \
2595       ret = FALSE;                                                      \
2596     } else {                                                            \
2597       g_value_set_ ## _type (dest, x);                                  \
2598     }                                                                   \
2599   }                                                                     \
2600   return ret;                                                           \
2601 }
2602
2603 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
2604 G_STMT_START {                                                          \
2605   static const GstValueTable gst_value = {                              \
2606     _gtype,                                                             \
2607     gst_value_compare_ ## _type,                                        \
2608     gst_value_serialize_ ## _type,                                      \
2609     gst_value_deserialize_ ## _type,                                    \
2610   };                                                                    \
2611                                                                         \
2612   gst_value_register (&gst_value);                                      \
2613 } G_STMT_END
2614
2615 CREATE_SERIALIZATION (int, INT);
2616 CREATE_SERIALIZATION (int64, INT64);
2617 CREATE_SERIALIZATION (long, LONG);
2618
2619 CREATE_USERIALIZATION (uint, UINT);
2620 CREATE_USERIALIZATION (uint64, UINT64);
2621 CREATE_USERIALIZATION (ulong, ULONG);
2622
2623 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2624 #ifndef G_MAXUCHAR
2625 #define G_MAXUCHAR 255
2626 #endif
2627 CREATE_USERIALIZATION (uchar, UCHAR);
2628
2629 /**********
2630  * double *
2631  **********/
2632 static gint
2633 gst_value_compare_double (const GValue * value1, const GValue * value2)
2634 {
2635   if (value1->data[0].v_double > value2->data[0].v_double)
2636     return GST_VALUE_GREATER_THAN;
2637   if (value1->data[0].v_double < value2->data[0].v_double)
2638     return GST_VALUE_LESS_THAN;
2639   if (value1->data[0].v_double == value2->data[0].v_double)
2640     return GST_VALUE_EQUAL;
2641   return GST_VALUE_UNORDERED;
2642 }
2643
2644 static gchar *
2645 gst_value_serialize_double (const GValue * value)
2646 {
2647   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2648
2649   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2650   return g_strdup (d);
2651 }
2652
2653 static gboolean
2654 gst_value_deserialize_double (GValue * dest, const gchar * s)
2655 {
2656   gdouble x;
2657   gboolean ret = FALSE;
2658   gchar *end;
2659
2660   x = g_ascii_strtod (s, &end);
2661   if (*end == 0) {
2662     ret = TRUE;
2663   } else {
2664     if (g_ascii_strcasecmp (s, "min") == 0) {
2665       x = -G_MAXDOUBLE;
2666       ret = TRUE;
2667     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2668       x = G_MAXDOUBLE;
2669       ret = TRUE;
2670     }
2671   }
2672   if (ret) {
2673     g_value_set_double (dest, x);
2674   }
2675   return ret;
2676 }
2677
2678 /*********
2679  * float *
2680  *********/
2681
2682 static gint
2683 gst_value_compare_float (const GValue * value1, const GValue * value2)
2684 {
2685   if (value1->data[0].v_float > value2->data[0].v_float)
2686     return GST_VALUE_GREATER_THAN;
2687   if (value1->data[0].v_float < value2->data[0].v_float)
2688     return GST_VALUE_LESS_THAN;
2689   if (value1->data[0].v_float == value2->data[0].v_float)
2690     return GST_VALUE_EQUAL;
2691   return GST_VALUE_UNORDERED;
2692 }
2693
2694 static gchar *
2695 gst_value_serialize_float (const GValue * value)
2696 {
2697   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2698
2699   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2700   return g_strdup (d);
2701 }
2702
2703 static gboolean
2704 gst_value_deserialize_float (GValue * dest, const gchar * s)
2705 {
2706   gdouble x;
2707   gboolean ret = FALSE;
2708   gchar *end;
2709
2710   x = g_ascii_strtod (s, &end);
2711   if (*end == 0) {
2712     ret = TRUE;
2713   } else {
2714     if (g_ascii_strcasecmp (s, "min") == 0) {
2715       x = -G_MAXFLOAT;
2716       ret = TRUE;
2717     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2718       x = G_MAXFLOAT;
2719       ret = TRUE;
2720     }
2721   }
2722   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2723     ret = FALSE;
2724   if (ret) {
2725     g_value_set_float (dest, (float) x);
2726   }
2727   return ret;
2728 }
2729
2730 /**********
2731  * string *
2732  **********/
2733
2734 static gint
2735 gst_value_compare_string (const GValue * value1, const GValue * value2)
2736 {
2737   if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2738     /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2739     if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2740       return GST_VALUE_UNORDERED;
2741   } else {
2742     gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2743
2744     if (x < 0)
2745       return GST_VALUE_LESS_THAN;
2746     if (x > 0)
2747       return GST_VALUE_GREATER_THAN;
2748   }
2749
2750   return GST_VALUE_EQUAL;
2751 }
2752
2753 static gint
2754 gst_string_measure_wrapping (const gchar * s)
2755 {
2756   gint len;
2757   gboolean wrap = FALSE;
2758
2759   if (G_UNLIKELY (s == NULL))
2760     return -1;
2761
2762   /* Special case: the actual string NULL needs wrapping */
2763   if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2764     return 4;
2765
2766   len = 0;
2767   while (*s) {
2768     if (GST_ASCII_IS_STRING (*s)) {
2769       len++;
2770     } else if (*s < 0x20 || *s >= 0x7f) {
2771       wrap = TRUE;
2772       len += 4;
2773     } else {
2774       wrap = TRUE;
2775       len += 2;
2776     }
2777     s++;
2778   }
2779
2780   /* Wrap the string if we found something that needs
2781    * wrapping, or the empty string (len == 0) */
2782   return (wrap || len == 0) ? len : -1;
2783 }
2784
2785 static gchar *
2786 gst_string_wrap_inner (const gchar * s, gint len)
2787 {
2788   gchar *d, *e;
2789
2790   e = d = g_malloc (len + 3);
2791
2792   *e++ = '\"';
2793   while (*s) {
2794     if (GST_ASCII_IS_STRING (*s)) {
2795       *e++ = *s++;
2796     } else if (*s < 0x20 || *s >= 0x7f) {
2797       *e++ = '\\';
2798       *e++ = '0' + ((*(guchar *) s) >> 6);
2799       *e++ = '0' + (((*s) >> 3) & 0x7);
2800       *e++ = '0' + ((*s++) & 0x7);
2801     } else {
2802       *e++ = '\\';
2803       *e++ = *s++;
2804     }
2805   }
2806   *e++ = '\"';
2807   *e = 0;
2808
2809   g_assert (e - d <= len + 3);
2810   return d;
2811 }
2812
2813 /* Do string wrapping/escaping */
2814 static gchar *
2815 gst_string_wrap (const gchar * s)
2816 {
2817   gint len = gst_string_measure_wrapping (s);
2818
2819   if (G_LIKELY (len < 0))
2820     return g_strdup (s);
2821
2822   return gst_string_wrap_inner (s, len);
2823 }
2824
2825 /* Same as above, but take ownership of the string */
2826 static gchar *
2827 gst_string_take_and_wrap (gchar * s)
2828 {
2829   gchar *out;
2830   gint len = gst_string_measure_wrapping (s);
2831
2832   if (G_LIKELY (len < 0))
2833     return s;
2834
2835   out = gst_string_wrap_inner (s, len);
2836   g_free (s);
2837
2838   return out;
2839 }
2840
2841 /*
2842  * This function takes a string delimited with double quotes (")
2843  * and unescapes any \xxx octal numbers.
2844  *
2845  * If sequences of \y are found where y is not in the range of
2846  * 0->3, y is copied unescaped.
2847  *
2848  * If \xyy is found where x is an octal number but y is not, an
2849  * error is encountered and NULL is returned.
2850  *
2851  * the input string must be \0 terminated.
2852  */
2853 static gchar *
2854 gst_string_unwrap (const gchar * s)
2855 {
2856   gchar *ret;
2857   gchar *read, *write;
2858
2859   /* NULL string returns NULL */
2860   if (s == NULL)
2861     return NULL;
2862
2863   /* strings not starting with " are invalid */
2864   if (*s != '"')
2865     return NULL;
2866
2867   /* make copy of original string to hold the result. This
2868    * string will always be smaller than the original */
2869   ret = g_strdup (s);
2870   read = ret;
2871   write = ret;
2872
2873   /* need to move to the next position as we parsed the " */
2874   read++;
2875
2876   while (*read) {
2877     if (GST_ASCII_IS_STRING (*read)) {
2878       /* normal chars are just copied */
2879       *write++ = *read++;
2880     } else if (*read == '"') {
2881       /* quote marks end of string */
2882       break;
2883     } else if (*read == '\\') {
2884       /* got an escape char, move to next position to read a tripplet
2885        * of octal numbers */
2886       read++;
2887       /* is the next char a possible first octal number? */
2888       if (*read >= '0' && *read <= '3') {
2889         /* parse other 2 numbers, if one of them is not in the range of
2890          * an octal number, we error. We also catch the case where a zero
2891          * byte is found here. */
2892         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2893           goto beach;
2894
2895         /* now convert the octal number to a byte again. */
2896         *write++ = ((read[0] - '0') << 6) +
2897             ((read[1] - '0') << 3) + (read[2] - '0');
2898
2899         read += 3;
2900       } else {
2901         /* if we run into a \0 here, we definitely won't get a quote later */
2902         if (*read == 0)
2903           goto beach;
2904
2905         /* else copy \X sequence */
2906         *write++ = *read++;
2907       }
2908     } else {
2909       /* weird character, error */
2910       goto beach;
2911     }
2912   }
2913   /* if the string is not ending in " and zero terminated, we error */
2914   if (*read != '"' || read[1] != '\0')
2915     goto beach;
2916
2917   /* null terminate result string and return */
2918   *write = '\0';
2919   return ret;
2920
2921 beach:
2922   g_free (ret);
2923   return NULL;
2924 }
2925
2926 static gchar *
2927 gst_value_serialize_string (const GValue * value)
2928 {
2929   return gst_string_wrap (value->data[0].v_pointer);
2930 }
2931
2932 static gboolean
2933 gst_value_deserialize_string (GValue * dest, const gchar * s)
2934 {
2935   if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2936     g_value_set_string (dest, NULL);
2937     return TRUE;
2938   } else if (G_LIKELY (*s != '"')) {
2939     if (!g_utf8_validate (s, -1, NULL))
2940       return FALSE;
2941     g_value_set_string (dest, s);
2942     return TRUE;
2943   } else {
2944     gchar *str = gst_string_unwrap (s);
2945     if (G_UNLIKELY (!str))
2946       return FALSE;
2947     g_value_take_string (dest, str);
2948   }
2949
2950   return TRUE;
2951 }
2952
2953 /********
2954  * enum *
2955  ********/
2956
2957 static gint
2958 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2959 {
2960   GEnumValue *en1, *en2;
2961   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2962   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2963
2964   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2965   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2966   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2967   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2968   g_type_class_unref (klass1);
2969   g_type_class_unref (klass2);
2970   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2971   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2972   if (en1->value < en2->value)
2973     return GST_VALUE_LESS_THAN;
2974   if (en1->value > en2->value)
2975     return GST_VALUE_GREATER_THAN;
2976
2977   return GST_VALUE_EQUAL;
2978 }
2979
2980 static gchar *
2981 gst_value_serialize_enum (const GValue * value)
2982 {
2983   GEnumValue *en;
2984   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
2985
2986   g_return_val_if_fail (klass, NULL);
2987   en = g_enum_get_value (klass, g_value_get_enum (value));
2988   g_type_class_unref (klass);
2989
2990   /* might be one of the custom formats registered later */
2991   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
2992     const GstFormatDefinition *format_def;
2993
2994     format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
2995     g_return_val_if_fail (format_def != NULL, NULL);
2996     return g_strdup (format_def->description);
2997   }
2998
2999   g_return_val_if_fail (en, NULL);
3000   return g_strdup (en->value_name);
3001 }
3002
3003 static gint
3004 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
3005     const gchar * s)
3006 {
3007   const GstFormatDefinition *format_def =
3008       g_value_get_pointer (format_def_value);
3009
3010   if (g_ascii_strcasecmp (s, format_def->nick) == 0)
3011     return 0;
3012
3013   return g_ascii_strcasecmp (s, format_def->description);
3014 }
3015
3016 static gboolean
3017 gst_value_deserialize_enum (GValue * dest, const gchar * s)
3018 {
3019   GEnumValue *en;
3020   gchar *endptr = NULL;
3021   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3022
3023   g_return_val_if_fail (klass, FALSE);
3024   if (!(en = g_enum_get_value_by_name (klass, s))) {
3025     if (!(en = g_enum_get_value_by_nick (klass, s))) {
3026       gint i = strtol (s, &endptr, 0);
3027
3028       if (endptr && *endptr == '\0') {
3029         en = g_enum_get_value (klass, i);
3030       }
3031     }
3032   }
3033   g_type_class_unref (klass);
3034
3035   /* might be one of the custom formats registered later */
3036   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
3037     GValue res = { 0, };
3038     const GstFormatDefinition *format_def;
3039     GstIterator *iter;
3040     gboolean found;
3041
3042     iter = gst_format_iterate_definitions ();
3043
3044     found = gst_iterator_find_custom (iter,
3045         (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
3046
3047     g_return_val_if_fail (found, FALSE);
3048     format_def = g_value_get_pointer (&res);
3049     g_return_val_if_fail (format_def != NULL, FALSE);
3050     g_value_set_enum (dest, (gint) format_def->value);
3051     g_value_unset (&res);
3052     gst_iterator_free (iter);
3053     return TRUE;
3054   }
3055
3056   g_return_val_if_fail (en, FALSE);
3057   g_value_set_enum (dest, en->value);
3058   return TRUE;
3059 }
3060
3061 /********
3062  * flags *
3063  ********/
3064
3065 /* we just compare the value here */
3066 static gint
3067 gst_value_compare_flags (const GValue * value1, const GValue * value2)
3068 {
3069   guint fl1, fl2;
3070   GFlagsClass *klass1 =
3071       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
3072   GFlagsClass *klass2 =
3073       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
3074
3075   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
3076   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
3077   fl1 = g_value_get_flags (value1);
3078   fl2 = g_value_get_flags (value2);
3079   g_type_class_unref (klass1);
3080   g_type_class_unref (klass2);
3081   if (fl1 < fl2)
3082     return GST_VALUE_LESS_THAN;
3083   if (fl1 > fl2)
3084     return GST_VALUE_GREATER_THAN;
3085
3086   return GST_VALUE_EQUAL;
3087 }
3088
3089 /* the different flags are serialized separated with a + */
3090 static gchar *
3091 gst_value_serialize_flags (const GValue * value)
3092 {
3093   guint flags;
3094   GFlagsValue *fl;
3095   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
3096   gchar *result, *tmp;
3097   gboolean first = TRUE;
3098
3099   g_return_val_if_fail (klass, NULL);
3100
3101   flags = g_value_get_flags (value);
3102
3103   /* if no flags are set, try to serialize to the _NONE string */
3104   if (!flags) {
3105     fl = g_flags_get_first_value (klass, flags);
3106     if (fl)
3107       return g_strdup (fl->value_name);
3108     else
3109       return g_strdup ("0");
3110   }
3111
3112   /* some flags are set, so serialize one by one */
3113   result = g_strdup ("");
3114   while (flags) {
3115     fl = g_flags_get_first_value (klass, flags);
3116     if (fl != NULL) {
3117       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
3118       g_free (result);
3119       result = tmp;
3120       first = FALSE;
3121
3122       /* clear flag */
3123       flags &= ~fl->value;
3124     }
3125   }
3126   g_type_class_unref (klass);
3127
3128   return result;
3129 }
3130
3131 static gboolean
3132 gst_value_deserialize_flags (GValue * dest, const gchar * s)
3133 {
3134   GFlagsValue *fl;
3135   gchar *endptr = NULL;
3136   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3137   gchar **split;
3138   guint flags;
3139   gint i;
3140
3141   g_return_val_if_fail (klass, FALSE);
3142
3143   /* split into parts delimited with + */
3144   split = g_strsplit (s, "+", 0);
3145
3146   flags = 0;
3147   i = 0;
3148   /* loop over each part */
3149   while (split[i]) {
3150     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
3151       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
3152         gint val = strtol (split[i], &endptr, 0);
3153
3154         /* just or numeric value */
3155         if (endptr && *endptr == '\0') {
3156           flags |= val;
3157         }
3158       }
3159     }
3160     if (fl) {
3161       flags |= fl->value;
3162     }
3163     i++;
3164   }
3165   g_strfreev (split);
3166   g_type_class_unref (klass);
3167   g_value_set_flags (dest, flags);
3168
3169   return TRUE;
3170 }
3171
3172 /****************
3173  * subset *
3174  ****************/
3175
3176 static gboolean
3177 gst_value_is_subset_int_range_int_range (const GValue * value1,
3178     const GValue * value2)
3179 {
3180   gint gcd;
3181
3182   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
3183   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
3184
3185   if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
3186       INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
3187     return FALSE;
3188   if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
3189       INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
3190     return FALSE;
3191
3192   if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
3193     if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
3194         INT_RANGE_STEP (value1))
3195       return FALSE;
3196     return TRUE;
3197   }
3198
3199   gcd =
3200       gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
3201       INT_RANGE_STEP (value2));
3202   if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
3203     return FALSE;
3204
3205   return TRUE;
3206 }
3207
3208 static gboolean
3209 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
3210     const GValue * value2)
3211 {
3212   gint64 gcd;
3213
3214   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
3215   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
3216
3217   if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
3218     return FALSE;
3219   if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
3220     return FALSE;
3221
3222   if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
3223     if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
3224         INT64_RANGE_STEP (value1))
3225       return FALSE;
3226     return TRUE;
3227   }
3228
3229   gcd =
3230       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
3231       INT64_RANGE_STEP (value2));
3232   if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
3233     return FALSE;
3234
3235   return TRUE;
3236 }
3237
3238 /**
3239  * gst_value_is_subset:
3240  * @value1: a #GValue
3241  * @value2: a #GValue
3242  *
3243  * Check that @value1 is a subset of @value2.
3244  *
3245  * Return: %TRUE is @value1 is a subset of @value2
3246  */
3247 gboolean
3248 gst_value_is_subset (const GValue * value1, const GValue * value2)
3249 {
3250   /* special case for int/int64 ranges, since we cannot compute
3251      the difference for those when they have different steps,
3252      and it's actually a lot simpler to compute whether a range
3253      is a subset of another. */
3254   if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
3255     return gst_value_is_subset_int_range_int_range (value1, value2);
3256   } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
3257       && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
3258     return gst_value_is_subset_int64_range_int64_range (value1, value2);
3259   }
3260
3261   /*
3262    * 1 - [1,2] = empty
3263    * -> !subset
3264    *
3265    * [1,2] - 1 = 2
3266    *  -> 1 - [1,2] = empty
3267    *  -> subset
3268    *
3269    * [1,3] - [1,2] = 3
3270    * -> [1,2] - [1,3] = empty
3271    * -> subset
3272    *
3273    * {1,2} - {1,3} = 2
3274    * -> {1,3} - {1,2} = 3
3275    * -> !subset
3276    *
3277    *  First caps subtraction needs to return a non-empty set, second
3278    *  subtractions needs to give en empty set.
3279    *  Both substractions are switched below, as it's faster that way.
3280    */
3281   if (!gst_value_subtract (NULL, value1, value2)) {
3282     if (gst_value_subtract (NULL, value2, value1)) {
3283       return TRUE;
3284     }
3285   }
3286   return FALSE;
3287 }
3288
3289 /*********
3290  * union *
3291  *********/
3292
3293 static gboolean
3294 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
3295     const GValue * src2)
3296 {
3297   gint v = src1->data[0].v_int;
3298
3299   /* check if it's already in the range */
3300   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
3301       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
3302       v % INT_RANGE_STEP (src2) == 0) {
3303     if (dest)
3304       gst_value_init_and_copy (dest, src2);
3305     return TRUE;
3306   }
3307
3308   /* check if it extends the range */
3309   if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
3310     if (dest) {
3311       gst_value_init_and_copy (dest, src2);
3312       --INT_RANGE_MIN (src2);
3313     }
3314     return TRUE;
3315   }
3316   if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
3317     if (dest) {
3318       gst_value_init_and_copy (dest, src2);
3319       ++INT_RANGE_MAX (src2);
3320     }
3321     return TRUE;
3322   }
3323
3324   return FALSE;
3325 }
3326
3327 static gboolean
3328 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
3329     const GValue * src2)
3330 {
3331   /* We can union in several special cases:
3332      1 - one is a subset of another
3333      2 - same step and not disjoint
3334      3 - different step, at least one with one value which matches a 'next' or 'previous'
3335      - anything else ?
3336    */
3337
3338   /* 1 - subset */
3339   if (gst_value_is_subset_int_range_int_range (src1, src2)) {
3340     if (dest)
3341       gst_value_init_and_copy (dest, src2);
3342     return TRUE;
3343   }
3344   if (gst_value_is_subset_int_range_int_range (src2, src1)) {
3345     if (dest)
3346       gst_value_init_and_copy (dest, src1);
3347     return TRUE;
3348   }
3349
3350   /* 2 - same step and not disjoint */
3351   if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
3352     if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
3353             INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
3354         (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
3355             INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
3356       if (dest) {
3357         gint step = INT_RANGE_STEP (src1);
3358         gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
3359         gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
3360         g_value_init (dest, GST_TYPE_INT_RANGE);
3361         gst_value_set_int_range_step (dest, min, max, step);
3362       }
3363       return TRUE;
3364     }
3365   }
3366
3367   /* 3 - single value matches next or previous */
3368   if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
3369     gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
3370     gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
3371     if (n1 == 1 || n2 == 1) {
3372       const GValue *range_value = NULL;
3373       gint scalar = 0;
3374       if (n1 == 1) {
3375         range_value = src2;
3376         scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
3377       } else if (n2 == 1) {
3378         range_value = src1;
3379         scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
3380       }
3381
3382       if (scalar ==
3383           (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
3384         if (dest) {
3385           gst_value_init_and_copy (dest, range_value);
3386           --INT_RANGE_MIN (range_value);
3387         }
3388         return TRUE;
3389       } else if (scalar ==
3390           (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
3391         if (dest) {
3392           gst_value_init_and_copy (dest, range_value);
3393           ++INT_RANGE_MIN (range_value);
3394         }
3395         return TRUE;
3396       }
3397     }
3398   }
3399
3400   /* If we get there, we did not find a way to make a union that can be
3401      represented with our simplistic model. */
3402   return FALSE;
3403 }
3404
3405 /****************
3406  * intersection *
3407  ****************/
3408
3409 static gboolean
3410 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
3411     const GValue * src2)
3412 {
3413   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
3414       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
3415       src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
3416     if (dest)
3417       gst_value_init_and_copy (dest, src1);
3418     return TRUE;
3419   }
3420
3421   return FALSE;
3422 }
3423
3424 static gboolean
3425 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
3426     const GValue * src2)
3427 {
3428   gint min;
3429   gint max;
3430   gint step;
3431
3432   step =
3433       INT_RANGE_STEP (src1) /
3434       gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
3435       INT_RANGE_STEP (src2));
3436   if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
3437     return FALSE;
3438   step *= INT_RANGE_STEP (src2);
3439
3440   min =
3441       MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
3442       INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3443   min = (min + step - 1) / step * step;
3444   max =
3445       MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
3446       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3447   max = max / step * step;
3448
3449   if (min < max) {
3450     if (dest) {
3451       g_value_init (dest, GST_TYPE_INT_RANGE);
3452       gst_value_set_int_range_step (dest, min, max, step);
3453     }
3454     return TRUE;
3455   }
3456   if (min == max) {
3457     if (dest) {
3458       g_value_init (dest, G_TYPE_INT);
3459       g_value_set_int (dest, min);
3460     }
3461     return TRUE;
3462   }
3463
3464   return FALSE;
3465 }
3466
3467 #define INT64_RANGE_MIN_VAL(v) (INT64_RANGE_MIN (v) * INT64_RANGE_STEP (v))
3468 #define INT64_RANGE_MAX_VAL(v) (INT64_RANGE_MAX (v) * INT64_RANGE_STEP (v))
3469
3470 static gboolean
3471 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
3472     const GValue * src2)
3473 {
3474   if (INT64_RANGE_MIN_VAL (src2) <= src1->data[0].v_int64 &&
3475       INT64_RANGE_MAX_VAL (src2) >= src1->data[0].v_int64 &&
3476       src1->data[0].v_int64 % INT64_RANGE_STEP (src2) == 0) {
3477     if (dest)
3478       gst_value_init_and_copy (dest, src1);
3479     return TRUE;
3480   }
3481
3482   return FALSE;
3483 }
3484
3485 static gboolean
3486 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
3487     const GValue * src2)
3488 {
3489   gint64 min;
3490   gint64 max;
3491   gint64 step;
3492
3493   step =
3494       INT64_RANGE_STEP (src1) /
3495       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
3496       INT64_RANGE_STEP (src2));
3497   if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
3498     return FALSE;
3499   step *= INT64_RANGE_STEP (src2);
3500
3501   min =
3502       MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
3503       INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
3504   min = (min + step - 1) / step * step;
3505   max =
3506       MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
3507       INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
3508   max = max / step * step;
3509
3510   if (min < max) {
3511     if (dest) {
3512       g_value_init (dest, GST_TYPE_INT64_RANGE);
3513       gst_value_set_int64_range_step (dest, min, max, step);
3514     }
3515     return TRUE;
3516   }
3517   if (min == max) {
3518     if (dest) {
3519       g_value_init (dest, G_TYPE_INT64);
3520       g_value_set_int64 (dest, min);
3521     }
3522     return TRUE;
3523   }
3524
3525   return FALSE;
3526 }
3527
3528 static gboolean
3529 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
3530     const GValue * src2)
3531 {
3532   if (src2->data[0].v_double <= src1->data[0].v_double &&
3533       src2->data[1].v_double >= src1->data[0].v_double) {
3534     if (dest)
3535       gst_value_init_and_copy (dest, src1);
3536     return TRUE;
3537   }
3538
3539   return FALSE;
3540 }
3541
3542 static gboolean
3543 gst_value_intersect_double_range_double_range (GValue * dest,
3544     const GValue * src1, const GValue * src2)
3545 {
3546   gdouble min;
3547   gdouble max;
3548
3549   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
3550   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
3551
3552   if (min < max) {
3553     if (dest) {
3554       g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
3555       gst_value_set_double_range (dest, min, max);
3556     }
3557     return TRUE;
3558   }
3559   if (min == max) {
3560     if (dest) {
3561       g_value_init (dest, G_TYPE_DOUBLE);
3562       g_value_set_int (dest, (int) min);
3563     }
3564     return TRUE;
3565   }
3566
3567   return FALSE;
3568 }
3569
3570 static gboolean
3571 gst_value_intersect_list (GValue * dest, const GValue * value1,
3572     const GValue * value2)
3573 {
3574   guint i, size;
3575   GValue intersection = { 0, };
3576   gboolean ret = FALSE;
3577
3578   size = VALUE_LIST_SIZE (value1);
3579   for (i = 0; i < size; i++) {
3580     const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
3581
3582     /* quicker version when we don't need the resulting set */
3583     if (!dest) {
3584       if (gst_value_intersect (NULL, cur, value2)) {
3585         ret = TRUE;
3586         break;
3587       }
3588       continue;
3589     }
3590
3591     if (gst_value_intersect (&intersection, cur, value2)) {
3592       /* append value */
3593       if (!ret) {
3594         gst_value_move (dest, &intersection);
3595         ret = TRUE;
3596       } else if (GST_VALUE_HOLDS_LIST (dest)) {
3597         gst_value_list_append_and_take_value (dest, &intersection);
3598       } else {
3599         GValue temp;
3600
3601         gst_value_move (&temp, dest);
3602         gst_value_list_merge (dest, &temp, &intersection);
3603         g_value_unset (&temp);
3604         g_value_unset (&intersection);
3605       }
3606     }
3607   }
3608
3609   return ret;
3610 }
3611
3612 static gboolean
3613 gst_value_intersect_array (GValue * dest, const GValue * src1,
3614     const GValue * src2)
3615 {
3616   guint size;
3617   guint n;
3618   GValue val = { 0 };
3619
3620   /* only works on similar-sized arrays */
3621   size = gst_value_array_get_size (src1);
3622   if (size != gst_value_array_get_size (src2))
3623     return FALSE;
3624
3625   /* quicker value when we don't need the resulting set */
3626   if (!dest) {
3627     for (n = 0; n < size; n++) {
3628       if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
3629               gst_value_array_get_value (src2, n))) {
3630         return FALSE;
3631       }
3632     }
3633     return TRUE;
3634   }
3635
3636   g_value_init (dest, GST_TYPE_ARRAY);
3637
3638   for (n = 0; n < size; n++) {
3639     if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
3640             gst_value_array_get_value (src2, n))) {
3641       g_value_unset (dest);
3642       return FALSE;
3643     }
3644     gst_value_array_append_and_take_value (dest, &val);
3645   }
3646
3647   return TRUE;
3648 }
3649
3650 static gboolean
3651 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
3652     const GValue * src2)
3653 {
3654   gint res1, res2;
3655   GValue *vals;
3656   GstValueCompareFunc compare;
3657
3658   vals = src2->data[0].v_pointer;
3659
3660   if (vals == NULL)
3661     return FALSE;
3662
3663   if ((compare = gst_value_get_compare_func (src1))) {
3664     res1 = gst_value_compare_with_func (&vals[0], src1, compare);
3665     res2 = gst_value_compare_with_func (&vals[1], src1, compare);
3666
3667     if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
3668         (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
3669       if (dest)
3670         gst_value_init_and_copy (dest, src1);
3671       return TRUE;
3672     }
3673   }
3674
3675   return FALSE;
3676 }
3677
3678 static gboolean
3679 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
3680     const GValue * src1, const GValue * src2)
3681 {
3682   GValue *min;
3683   GValue *max;
3684   gint res;
3685   GValue *vals1, *vals2;
3686   GstValueCompareFunc compare;
3687
3688   vals1 = src1->data[0].v_pointer;
3689   vals2 = src2->data[0].v_pointer;
3690   g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
3691
3692   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
3693     /* min = MAX (src1.start, src2.start) */
3694     res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
3695     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3696     if (res == GST_VALUE_LESS_THAN)
3697       min = &vals2[0];          /* Take the max of the 2 */
3698     else
3699       min = &vals1[0];
3700
3701     /* max = MIN (src1.end, src2.end) */
3702     res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
3703     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3704     if (res == GST_VALUE_GREATER_THAN)
3705       max = &vals2[1];          /* Take the min of the 2 */
3706     else
3707       max = &vals1[1];
3708
3709     res = gst_value_compare_with_func (min, max, compare);
3710     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3711     if (res == GST_VALUE_LESS_THAN) {
3712       if (dest) {
3713         g_value_init (dest, GST_TYPE_FRACTION_RANGE);
3714         vals1 = dest->data[0].v_pointer;
3715         g_value_copy (min, &vals1[0]);
3716         g_value_copy (max, &vals1[1]);
3717       }
3718       return TRUE;
3719     }
3720     if (res == GST_VALUE_EQUAL) {
3721       if (dest)
3722         gst_value_init_and_copy (dest, min);
3723       return TRUE;
3724     }
3725   }
3726
3727   return FALSE;
3728 }
3729
3730 /***************
3731  * subtraction *
3732  ***************/
3733
3734 static gboolean
3735 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
3736     const GValue * subtrahend)
3737 {
3738   gint min = gst_value_get_int_range_min (subtrahend);
3739   gint max = gst_value_get_int_range_max (subtrahend);
3740   gint step = gst_value_get_int_range_step (subtrahend);
3741   gint val = g_value_get_int (minuend);
3742
3743   /* subtracting a range from an int only works if the int is not in the
3744    * range */
3745   if (val < min || val > max || val % step) {
3746     /* and the result is the int */
3747     if (dest)
3748       gst_value_init_and_copy (dest, minuend);
3749     return TRUE;
3750   }
3751   return FALSE;
3752 }
3753
3754 /* creates a new int range based on input values.
3755  */
3756 static gboolean
3757 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
3758     gint max2, gint step)
3759 {
3760   GValue v1 = { 0, };
3761   GValue v2 = { 0, };
3762   GValue *pv1, *pv2;            /* yeah, hungarian! */
3763
3764   g_return_val_if_fail (step > 0, FALSE);
3765   g_return_val_if_fail (min1 % step == 0, FALSE);
3766   g_return_val_if_fail (max1 % step == 0, FALSE);
3767   g_return_val_if_fail (min2 % step == 0, FALSE);
3768   g_return_val_if_fail (max2 % step == 0, FALSE);
3769
3770   if (min1 <= max1 && min2 <= max2) {
3771     pv1 = &v1;
3772     pv2 = &v2;
3773   } else if (min1 <= max1) {
3774     pv1 = dest;
3775     pv2 = NULL;
3776   } else if (min2 <= max2) {
3777     pv1 = NULL;
3778     pv2 = dest;
3779   } else {
3780     return FALSE;
3781   }
3782
3783   if (!dest)
3784     return TRUE;
3785
3786   if (min1 < max1) {
3787     g_value_init (pv1, GST_TYPE_INT_RANGE);
3788     gst_value_set_int_range_step (pv1, min1, max1, step);
3789   } else if (min1 == max1) {
3790     g_value_init (pv1, G_TYPE_INT);
3791     g_value_set_int (pv1, min1);
3792   }
3793   if (min2 < max2) {
3794     g_value_init (pv2, GST_TYPE_INT_RANGE);
3795     gst_value_set_int_range_step (pv2, min2, max2, step);
3796   } else if (min2 == max2) {
3797     g_value_init (pv2, G_TYPE_INT);
3798     g_value_set_int (pv2, min2);
3799   }
3800
3801   if (min1 <= max1 && min2 <= max2) {
3802     gst_value_list_concat (dest, pv1, pv2);
3803     g_value_unset (pv1);
3804     g_value_unset (pv2);
3805   }
3806   return TRUE;
3807 }
3808
3809 static gboolean
3810 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
3811     const GValue * subtrahend)
3812 {
3813   gint min = gst_value_get_int_range_min (minuend);
3814   gint max = gst_value_get_int_range_max (minuend);
3815   gint step = gst_value_get_int_range_step (minuend);
3816   gint val = g_value_get_int (subtrahend);
3817
3818   g_return_val_if_fail (min < max, FALSE);
3819
3820   /* value is outside of the range, return range unchanged */
3821   if (val < min || val > max || val % step) {
3822     if (dest)
3823       gst_value_init_and_copy (dest, minuend);
3824     return TRUE;
3825   } else {
3826     /* max must be MAXINT too as val <= max */
3827     if (val >= G_MAXINT - step + 1) {
3828       max -= step;
3829       val -= step;
3830     }
3831     /* min must be MININT too as val >= max */
3832     if (val <= G_MININT + step - 1) {
3833       min += step;
3834       val += step;
3835     }
3836     if (dest)
3837       gst_value_create_new_range (dest, min, val - step, val + step, max, step);
3838   }
3839   return TRUE;
3840 }
3841
3842 static gboolean
3843 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
3844     const GValue * subtrahend)
3845 {
3846   gint min1 = gst_value_get_int_range_min (minuend);
3847   gint max1 = gst_value_get_int_range_max (minuend);
3848   gint step1 = gst_value_get_int_range_step (minuend);
3849   gint min2 = gst_value_get_int_range_min (subtrahend);
3850   gint max2 = gst_value_get_int_range_max (subtrahend);
3851   gint step2 = gst_value_get_int_range_step (subtrahend);
3852   gint step;
3853
3854   if (step1 != step2) {
3855     /* ENOIMPL */
3856     g_assert (FALSE);
3857     return FALSE;
3858   }
3859   step = step1;
3860
3861   if (max2 >= max1 && min2 <= min1) {
3862     return FALSE;
3863   } else if (max2 >= max1) {
3864     return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3865         step, 0, step);
3866   } else if (min2 <= min1) {
3867     return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
3868         step, 0, step);
3869   } else {
3870     return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3871         MAX (max2 + step, min1), max1, step);
3872   }
3873 }
3874
3875 static gboolean
3876 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
3877     const GValue * subtrahend)
3878 {
3879   gint64 min = gst_value_get_int64_range_min (subtrahend);
3880   gint64 max = gst_value_get_int64_range_max (subtrahend);
3881   gint64 step = gst_value_get_int64_range_step (subtrahend);
3882   gint64 val = g_value_get_int64 (minuend);
3883
3884   /* subtracting a range from an int64 only works if the int64 is not in the
3885    * range */
3886   if (val < min || val > max || val % step) {
3887     /* and the result is the int64 */
3888     if (dest)
3889       gst_value_init_and_copy (dest, minuend);
3890     return TRUE;
3891   }
3892   return FALSE;
3893 }
3894
3895 /* creates a new int64 range based on input values.
3896  */
3897 static gboolean
3898 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
3899     gint64 min2, gint64 max2, gint64 step)
3900 {
3901   GValue v1 = { 0, };
3902   GValue v2 = { 0, };
3903   GValue *pv1, *pv2;            /* yeah, hungarian! */
3904
3905   g_return_val_if_fail (step > 0, FALSE);
3906   g_return_val_if_fail (min1 % step == 0, FALSE);
3907   g_return_val_if_fail (max1 % step == 0, FALSE);
3908   g_return_val_if_fail (min2 % step == 0, FALSE);
3909   g_return_val_if_fail (max2 % step == 0, FALSE);
3910
3911   if (min1 <= max1 && min2 <= max2) {
3912     pv1 = &v1;
3913     pv2 = &v2;
3914   } else if (min1 <= max1) {
3915     pv1 = dest;
3916     pv2 = NULL;
3917   } else if (min2 <= max2) {
3918     pv1 = NULL;
3919     pv2 = dest;
3920   } else {
3921     return FALSE;
3922   }
3923
3924   if (!dest)
3925     return TRUE;
3926
3927   if (min1 < max1) {
3928     g_value_init (pv1, GST_TYPE_INT64_RANGE);
3929     gst_value_set_int64_range_step (pv1, min1, max1, step);
3930   } else if (min1 == max1) {
3931     g_value_init (pv1, G_TYPE_INT64);
3932     g_value_set_int64 (pv1, min1);
3933   }
3934   if (min2 < max2) {
3935     g_value_init (pv2, GST_TYPE_INT64_RANGE);
3936     gst_value_set_int64_range_step (pv2, min2, max2, step);
3937   } else if (min2 == max2) {
3938     g_value_init (pv2, G_TYPE_INT64);
3939     g_value_set_int64 (pv2, min2);
3940   }
3941
3942   if (min1 <= max1 && min2 <= max2) {
3943     gst_value_list_concat (dest, pv1, pv2);
3944     g_value_unset (pv1);
3945     g_value_unset (pv2);
3946   }
3947   return TRUE;
3948 }
3949
3950 static gboolean
3951 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
3952     const GValue * subtrahend)
3953 {
3954   gint64 min = gst_value_get_int64_range_min (minuend);
3955   gint64 max = gst_value_get_int64_range_max (minuend);
3956   gint64 step = gst_value_get_int64_range_step (minuend);
3957   gint64 val = g_value_get_int64 (subtrahend);
3958
3959   g_return_val_if_fail (min < max, FALSE);
3960
3961   /* value is outside of the range, return range unchanged */
3962   if (val < min || val > max || val % step) {
3963     if (dest)
3964       gst_value_init_and_copy (dest, minuend);
3965     return TRUE;
3966   } else {
3967     /* max must be MAXINT64 too as val <= max */
3968     if (val >= G_MAXINT64 - step + 1) {
3969       max -= step;
3970       val -= step;
3971     }
3972     /* min must be MININT64 too as val >= max */
3973     if (val <= G_MININT64 + step - 1) {
3974       min += step;
3975       val += step;
3976     }
3977     if (dest)
3978       gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
3979           step);
3980   }
3981   return TRUE;
3982 }
3983
3984 static gboolean
3985 gst_value_subtract_int64_range_int64_range (GValue * dest,
3986     const GValue * minuend, const GValue * subtrahend)
3987 {
3988   gint64 min1 = gst_value_get_int64_range_min (minuend);
3989   gint64 max1 = gst_value_get_int64_range_max (minuend);
3990   gint64 step1 = gst_value_get_int64_range_step (minuend);
3991   gint64 min2 = gst_value_get_int64_range_min (subtrahend);
3992   gint64 max2 = gst_value_get_int64_range_max (subtrahend);
3993   gint64 step2 = gst_value_get_int64_range_step (subtrahend);
3994   gint64 step;
3995
3996   if (step1 != step2) {
3997     /* ENOIMPL */
3998     g_assert (FALSE);
3999     return FALSE;
4000   }
4001   step = step1;
4002
4003   if (max2 >= max1 && min2 <= min1) {
4004     return FALSE;
4005   } else if (max2 >= max1) {
4006     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
4007             max1), step, 0, step);
4008   } else if (min2 <= min1) {
4009     return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
4010         max1, step, 0, step);
4011   } else {
4012     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
4013             max1), MAX (max2 + step, min1), max1, step);
4014   }
4015 }
4016
4017 static gboolean
4018 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
4019     const GValue * subtrahend)
4020 {
4021   gdouble min = gst_value_get_double_range_min (subtrahend);
4022   gdouble max = gst_value_get_double_range_max (subtrahend);
4023   gdouble val = g_value_get_double (minuend);
4024
4025   if (val < min || val > max) {
4026     if (dest)
4027       gst_value_init_and_copy (dest, minuend);
4028     return TRUE;
4029   }
4030   return FALSE;
4031 }
4032
4033 static gboolean
4034 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
4035     const GValue * subtrahend)
4036 {
4037   /* since we don't have open ranges, we cannot create a hole in
4038    * a double range. We return the original range */
4039   if (dest)
4040     gst_value_init_and_copy (dest, minuend);
4041   return TRUE;
4042 }
4043
4044 static gboolean
4045 gst_value_subtract_double_range_double_range (GValue * dest,
4046     const GValue * minuend, const GValue * subtrahend)
4047 {
4048   /* since we don't have open ranges, we have to approximate */
4049   /* done like with ints */
4050   gdouble min1 = gst_value_get_double_range_min (minuend);
4051   gdouble max2 = gst_value_get_double_range_max (minuend);
4052   gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
4053   gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
4054   GValue v1 = { 0, };
4055   GValue v2 = { 0, };
4056   GValue *pv1, *pv2;            /* yeah, hungarian! */
4057
4058   if (min1 < max1 && min2 < max2) {
4059     pv1 = &v1;
4060     pv2 = &v2;
4061   } else if (min1 < max1) {
4062     pv1 = dest;
4063     pv2 = NULL;
4064   } else if (min2 < max2) {
4065     pv1 = NULL;
4066     pv2 = dest;
4067   } else {
4068     return FALSE;
4069   }
4070
4071   if (!dest)
4072     return TRUE;
4073
4074   if (min1 < max1) {
4075     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
4076     gst_value_set_double_range (pv1, min1, max1);
4077   }
4078   if (min2 < max2) {
4079     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
4080     gst_value_set_double_range (pv2, min2, max2);
4081   }
4082
4083   if (min1 < max1 && min2 < max2) {
4084     gst_value_list_concat (dest, pv1, pv2);
4085     g_value_unset (pv1);
4086     g_value_unset (pv2);
4087   }
4088   return TRUE;
4089 }
4090
4091 static gboolean
4092 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
4093     const GValue * subtrahend)
4094 {
4095   guint i, size;
4096   GValue subtraction = { 0, };
4097   gboolean ret = FALSE;
4098   GType ltype;
4099
4100   ltype = gst_value_list_get_type ();
4101
4102   size = VALUE_LIST_SIZE (minuend);
4103   for (i = 0; i < size; i++) {
4104     const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
4105
4106     /* quicker version when we can discard the result */
4107     if (!dest) {
4108       if (gst_value_subtract (NULL, cur, subtrahend)) {
4109         ret = TRUE;
4110         break;
4111       }
4112       continue;
4113     }
4114
4115     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
4116       if (!ret) {
4117         gst_value_move (dest, &subtraction);
4118         ret = TRUE;
4119       } else if (G_VALUE_HOLDS (dest, ltype)
4120           && !G_VALUE_HOLDS (&subtraction, ltype)) {
4121         gst_value_list_append_and_take_value (dest, &subtraction);
4122       } else {
4123         GValue temp;
4124
4125         gst_value_move (&temp, dest);
4126         gst_value_list_concat (dest, &temp, &subtraction);
4127         g_value_unset (&temp);
4128         g_value_unset (&subtraction);
4129       }
4130     }
4131   }
4132   return ret;
4133 }
4134
4135 static gboolean
4136 gst_value_subtract_list (GValue * dest, const GValue * minuend,
4137     const GValue * subtrahend)
4138 {
4139   guint i, size;
4140   GValue data[2] = { {0,}, {0,} };
4141   GValue *subtraction = &data[0], *result = &data[1];
4142
4143   gst_value_init_and_copy (result, minuend);
4144   size = VALUE_LIST_SIZE (subtrahend);
4145   for (i = 0; i < size; i++) {
4146     const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
4147
4148     if (gst_value_subtract (subtraction, result, cur)) {
4149       GValue *temp = result;
4150
4151       result = subtraction;
4152       subtraction = temp;
4153       g_value_unset (subtraction);
4154     } else {
4155       g_value_unset (result);
4156       return FALSE;
4157     }
4158   }
4159   if (dest) {
4160     gst_value_move (dest, result);
4161   } else {
4162     g_value_unset (result);
4163   }
4164   return TRUE;
4165 }
4166
4167 static gboolean
4168 gst_value_subtract_fraction_fraction_range (GValue * dest,
4169     const GValue * minuend, const GValue * subtrahend)
4170 {
4171   const GValue *min = gst_value_get_fraction_range_min (subtrahend);
4172   const GValue *max = gst_value_get_fraction_range_max (subtrahend);
4173   GstValueCompareFunc compare;
4174
4175   if ((compare = gst_value_get_compare_func (minuend))) {
4176     /* subtracting a range from an fraction only works if the fraction
4177      * is not in the range */
4178     if (gst_value_compare_with_func (minuend, min, compare) ==
4179         GST_VALUE_LESS_THAN ||
4180         gst_value_compare_with_func (minuend, max, compare) ==
4181         GST_VALUE_GREATER_THAN) {
4182       /* and the result is the value */
4183       if (dest)
4184         gst_value_init_and_copy (dest, minuend);
4185       return TRUE;
4186     }
4187   }
4188   return FALSE;
4189 }
4190
4191 static gboolean
4192 gst_value_subtract_fraction_range_fraction (GValue * dest,
4193     const GValue * minuend, const GValue * subtrahend)
4194 {
4195   /* since we don't have open ranges, we cannot create a hole in
4196    * a range. We return the original range */
4197   if (dest)
4198     gst_value_init_and_copy (dest, minuend);
4199   return TRUE;
4200 }
4201
4202 static gboolean
4203 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
4204     const GValue * minuend, const GValue * subtrahend)
4205 {
4206   /* since we don't have open ranges, we have to approximate */
4207   /* done like with ints and doubles. Creates a list of 2 fraction ranges */
4208   const GValue *min1 = gst_value_get_fraction_range_min (minuend);
4209   const GValue *max2 = gst_value_get_fraction_range_max (minuend);
4210   const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
4211   const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
4212   gint cmp1, cmp2;
4213   GValue v1 = { 0, };
4214   GValue v2 = { 0, };
4215   GValue *pv1, *pv2;            /* yeah, hungarian! */
4216   GstValueCompareFunc compare;
4217
4218   g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
4219   g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
4220
4221   compare = gst_value_get_compare_func (min1);
4222   g_return_val_if_fail (compare, FALSE);
4223
4224   cmp1 = gst_value_compare_with_func (max2, max1, compare);
4225   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
4226   if (cmp1 == GST_VALUE_LESS_THAN)
4227     max1 = max2;
4228   cmp1 = gst_value_compare_with_func (min1, min2, compare);
4229   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
4230   if (cmp1 == GST_VALUE_GREATER_THAN)
4231     min2 = min1;
4232
4233   cmp1 = gst_value_compare_with_func (min1, max1, compare);
4234   cmp2 = gst_value_compare_with_func (min2, max2, compare);
4235
4236   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
4237     pv1 = &v1;
4238     pv2 = &v2;
4239   } else if (cmp1 == GST_VALUE_LESS_THAN) {
4240     pv1 = dest;
4241     pv2 = NULL;
4242   } else if (cmp2 == GST_VALUE_LESS_THAN) {
4243     pv1 = NULL;
4244     pv2 = dest;
4245   } else {
4246     return FALSE;
4247   }
4248
4249   if (!dest)
4250     return TRUE;
4251
4252   if (cmp1 == GST_VALUE_LESS_THAN) {
4253     g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
4254     gst_value_set_fraction_range (pv1, min1, max1);
4255   }
4256   if (cmp2 == GST_VALUE_LESS_THAN) {
4257     g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
4258     gst_value_set_fraction_range (pv2, min2, max2);
4259   }
4260
4261   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
4262     gst_value_list_concat (dest, pv1, pv2);
4263     g_value_unset (pv1);
4264     g_value_unset (pv2);
4265   }
4266   return TRUE;
4267 }
4268
4269
4270 /**************
4271  * comparison *
4272  **************/
4273
4274 /*
4275  * gst_value_get_compare_func:
4276  * @value1: a value to get the compare function for
4277  *
4278  * Determines the compare function to be used with values of the same type as
4279  * @value1. The function can be given to gst_value_compare_with_func().
4280  *
4281  * Returns: A #GstValueCompareFunc value
4282  */
4283 static GstValueCompareFunc
4284 gst_value_get_compare_func (const GValue * value1)
4285 {
4286   GstValueTable *table, *best = NULL;
4287   guint i;
4288   GType type1;
4289
4290   type1 = G_VALUE_TYPE (value1);
4291
4292   /* this is a fast check */
4293   best = gst_value_hash_lookup_type (type1);
4294
4295   /* slower checks */
4296   if (G_UNLIKELY (!best || !best->compare)) {
4297     guint len = gst_value_table->len;
4298
4299     best = NULL;
4300     for (i = 0; i < len; i++) {
4301       table = &g_array_index (gst_value_table, GstValueTable, i);
4302       if (table->compare && g_type_is_a (type1, table->type)) {
4303         if (!best || g_type_is_a (table->type, best->type))
4304           best = table;
4305       }
4306     }
4307   }
4308   if (G_LIKELY (best))
4309     return best->compare;
4310
4311   return NULL;
4312 }
4313
4314 /**
4315  * gst_value_can_compare:
4316  * @value1: a value to compare
4317  * @value2: another value to compare
4318  *
4319  * Determines if @value1 and @value2 can be compared.
4320  *
4321  * Returns: TRUE if the values can be compared
4322  */
4323 gboolean
4324 gst_value_can_compare (const GValue * value1, const GValue * value2)
4325 {
4326   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4327   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4328
4329   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4330     return FALSE;
4331
4332   return gst_value_get_compare_func (value1) != NULL;
4333 }
4334
4335 static gboolean
4336 gst_value_list_equals_range (const GValue * list, const GValue * value)
4337 {
4338   const GValue *first;
4339   guint list_size, n;
4340
4341   g_return_val_if_fail (G_IS_VALUE (list), FALSE);
4342   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
4343   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (list), FALSE);
4344
4345   /* TODO: compare against an empty list ? No type though... */
4346   list_size = VALUE_LIST_SIZE (list);
4347   if (list_size == 0)
4348     return FALSE;
4349
4350   /* compare the basic types - they have to match */
4351   first = VALUE_LIST_GET_VALUE (list, 0);
4352 #define CHECK_TYPES(type,prefix) \
4353   (prefix##_VALUE_HOLDS_##type(first) && GST_VALUE_HOLDS_##type##_RANGE (value))
4354   if (CHECK_TYPES (INT, G)) {
4355     const gint rmin = gst_value_get_int_range_min (value);
4356     const gint rmax = gst_value_get_int_range_max (value);
4357     const gint rstep = gst_value_get_int_range_step (value);
4358     /* note: this will overflow for min 0 and max INT_MAX, but this
4359        would only be equal to a list of INT_MAX elements, which seems
4360        very unlikely */
4361     if (list_size != rmax / rstep - rmin / rstep + 1)
4362       return FALSE;
4363     for (n = 0; n < list_size; ++n) {
4364       gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
4365       if (v < rmin || v > rmax || v % rstep) {
4366         return FALSE;
4367       }
4368     }
4369     return TRUE;
4370   } else if (CHECK_TYPES (INT64, G)) {
4371     const gint64 rmin = gst_value_get_int64_range_min (value);
4372     const gint64 rmax = gst_value_get_int64_range_max (value);
4373     const gint64 rstep = gst_value_get_int64_range_step (value);
4374     GST_DEBUG ("List/range of int64s");
4375     if (list_size != rmax / rstep - rmin / rstep + 1)
4376       return FALSE;
4377     for (n = 0; n < list_size; ++n) {
4378       gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
4379       if (v < rmin || v > rmax || v % rstep)
4380         return FALSE;
4381     }
4382     return TRUE;
4383   }
4384 #undef CHECK_TYPES
4385
4386   /* other combinations don't make sense for equality */
4387   return FALSE;
4388 }
4389
4390 /**
4391  * gst_value_compare:
4392  * @value1: a value to compare
4393  * @value2: another value to compare
4394  *
4395  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
4396  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
4397  * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
4398  * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
4399  * If the values are equal, GST_VALUE_EQUAL is returned.
4400  *
4401  * Returns: comparison result
4402  */
4403 gint
4404 gst_value_compare (const GValue * value1, const GValue * value2)
4405 {
4406   GstValueCompareFunc compare;
4407   GType ltype;
4408
4409   g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
4410   g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
4411
4412   /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
4413      as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
4414   ltype = gst_value_list_get_type ();
4415   if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)) {
4416
4417     if (gst_value_list_equals_range (value1, value2)) {
4418       return GST_VALUE_EQUAL;
4419     } else if (gst_value_list_get_size (value1) == 1) {
4420       const GValue *elt;
4421
4422       elt = gst_value_list_get_value (value1, 0);
4423       return gst_value_compare (elt, value2);
4424     }
4425   } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)) {
4426     if (gst_value_list_equals_range (value2, value1)) {
4427       return GST_VALUE_EQUAL;
4428     } else if (gst_value_list_get_size (value2) == 1) {
4429       const GValue *elt;
4430
4431       elt = gst_value_list_get_value (value2, 0);
4432       return gst_value_compare (elt, value1);
4433     }
4434   }
4435
4436   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4437     return GST_VALUE_UNORDERED;
4438
4439   compare = gst_value_get_compare_func (value1);
4440   if (compare) {
4441     return compare (value1, value2);
4442   }
4443
4444   g_critical ("unable to compare values of type %s\n",
4445       g_type_name (G_VALUE_TYPE (value1)));
4446   return GST_VALUE_UNORDERED;
4447 }
4448
4449 /*
4450  * gst_value_compare_with_func:
4451  * @value1: a value to compare
4452  * @value2: another value to compare
4453  * @compare: compare function
4454  *
4455  * Compares @value1 and @value2 using the @compare function. Works like
4456  * gst_value_compare() but allows to save time determining the compare function
4457  * a multiple times. 
4458  *
4459  * Returns: comparison result
4460  */
4461 static gint
4462 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
4463     GstValueCompareFunc compare)
4464 {
4465   g_assert (compare);
4466
4467   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4468     return GST_VALUE_UNORDERED;
4469
4470   return compare (value1, value2);
4471 }
4472
4473 /* union */
4474
4475 /**
4476  * gst_value_can_union:
4477  * @value1: a value to union
4478  * @value2: another value to union
4479  *
4480  * Determines if @value1 and @value2 can be non-trivially unioned.
4481  * Any two values can be trivially unioned by adding both of them
4482  * to a GstValueList.  However, certain types have the possibility
4483  * to be unioned in a simpler way.  For example, an integer range
4484  * and an integer can be unioned if the integer is a subset of the
4485  * integer range.  If there is the possibility that two values can
4486  * be unioned, this function returns TRUE.
4487  *
4488  * Returns: TRUE if there is a function allowing the two values to
4489  * be unioned.
4490  */
4491 gboolean
4492 gst_value_can_union (const GValue * value1, const GValue * value2)
4493 {
4494   GstValueUnionInfo *union_info;
4495   guint i, len;
4496
4497   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4498   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4499
4500   len = gst_value_union_funcs->len;
4501
4502   for (i = 0; i < len; i++) {
4503     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4504     if (union_info->type1 == G_VALUE_TYPE (value1) &&
4505         union_info->type2 == G_VALUE_TYPE (value2))
4506       return TRUE;
4507     if (union_info->type1 == G_VALUE_TYPE (value2) &&
4508         union_info->type2 == G_VALUE_TYPE (value1))
4509       return TRUE;
4510   }
4511
4512   return FALSE;
4513 }
4514
4515 /**
4516  * gst_value_union:
4517  * @dest: (out caller-allocates): the destination value
4518  * @value1: a value to union
4519  * @value2: another value to union
4520  *
4521  * Creates a GValue corresponding to the union of @value1 and @value2.
4522  *
4523  * Returns: TRUE if the union suceeded.
4524  */
4525 gboolean
4526 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
4527 {
4528   const GstValueUnionInfo *union_info;
4529   guint i, len;
4530   GType type1, type2;
4531
4532   g_return_val_if_fail (dest != NULL, FALSE);
4533   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4534   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4535   g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
4536       FALSE);
4537
4538   len = gst_value_union_funcs->len;
4539   type1 = G_VALUE_TYPE (value1);
4540   type2 = G_VALUE_TYPE (value2);
4541
4542   for (i = 0; i < len; i++) {
4543     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4544     if (union_info->type1 == type1 && union_info->type2 == type2) {
4545       return union_info->func (dest, value1, value2);
4546     }
4547     if (union_info->type1 == type2 && union_info->type2 == type1) {
4548       return union_info->func (dest, value2, value1);
4549     }
4550   }
4551
4552   gst_value_list_concat (dest, value1, value2);
4553   return TRUE;
4554 }
4555
4556 /* gst_value_register_union_func: (skip)
4557  * @type1: a type to union
4558  * @type2: another type to union
4559  * @func: a function that implements creating a union between the two types
4560  *
4561  * Registers a union function that can create a union between #GValue items
4562  * of the type @type1 and @type2.
4563  *
4564  * Union functions should be registered at startup before any pipelines are
4565  * started, as gst_value_register_union_func() is not thread-safe and cannot
4566  * be used at the same time as gst_value_union() or gst_value_can_union().
4567  */
4568 static void
4569 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
4570 {
4571   GstValueUnionInfo union_info;
4572
4573   union_info.type1 = type1;
4574   union_info.type2 = type2;
4575   union_info.func = func;
4576
4577   g_array_append_val (gst_value_union_funcs, union_info);
4578 }
4579
4580 /* intersection */
4581
4582 /**
4583  * gst_value_can_intersect:
4584  * @value1: a value to intersect
4585  * @value2: another value to intersect
4586  *
4587  * Determines if intersecting two values will produce a valid result.
4588  * Two values will produce a valid intersection if they have the same
4589  * type, or if there is a method (registered by
4590  * gst_value_register_intersect_func()) to calculate the intersection.
4591  *
4592  * Returns: TRUE if the values can intersect
4593  */
4594 gboolean
4595 gst_value_can_intersect (const GValue * value1, const GValue * value2)
4596 {
4597   GstValueIntersectInfo *intersect_info;
4598   guint i, len;
4599   GType ltype, type1, type2;
4600
4601   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4602   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4603
4604   ltype = gst_value_list_get_type ();
4605
4606   /* special cases */
4607   if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
4608     return TRUE;
4609
4610   type1 = G_VALUE_TYPE (value1);
4611   type2 = G_VALUE_TYPE (value2);
4612
4613   /* practically all GstValue types have a compare function (_can_compare=TRUE)
4614    * GstStructure and GstCaps have npot, but are intersectable */
4615   if (type1 == type2)
4616     return TRUE;
4617
4618   /* check registered intersect functions */
4619   len = gst_value_intersect_funcs->len;
4620   for (i = 0; i < len; i++) {
4621     intersect_info = &g_array_index (gst_value_intersect_funcs,
4622         GstValueIntersectInfo, i);
4623     if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
4624         (intersect_info->type1 == type2 && intersect_info->type2 == type1))
4625       return TRUE;
4626   }
4627
4628   return gst_value_can_compare (value1, value2);
4629 }
4630
4631 /**
4632  * gst_value_intersect:
4633  * @dest: (out caller-allocates) (transfer full): a uninitialized #GValue that will hold the calculated
4634  * intersection value. May be NULL if the resulting set if not needed.
4635  * @value1: a value to intersect
4636  * @value2: another value to intersect
4637  *
4638  * Calculates the intersection of two values.  If the values have
4639  * a non-empty intersection, the value representing the intersection
4640  * is placed in @dest, unless NULL.  If the intersection is non-empty,
4641  * @dest is not modified.
4642  *
4643  * Returns: TRUE if the intersection is non-empty
4644  */
4645 gboolean
4646 gst_value_intersect (GValue * dest, const GValue * value1,
4647     const GValue * value2)
4648 {
4649   GstValueIntersectInfo *intersect_info;
4650   guint i, len;
4651   GType ltype, type1, type2;
4652
4653   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4654   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4655
4656   ltype = gst_value_list_get_type ();
4657
4658   /* special cases first */
4659   if (G_VALUE_HOLDS (value1, ltype))
4660     return gst_value_intersect_list (dest, value1, value2);
4661   if (G_VALUE_HOLDS (value2, ltype))
4662     return gst_value_intersect_list (dest, value2, value1);
4663
4664   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
4665     if (dest)
4666       gst_value_init_and_copy (dest, value1);
4667     return TRUE;
4668   }
4669
4670   type1 = G_VALUE_TYPE (value1);
4671   type2 = G_VALUE_TYPE (value2);
4672
4673   len = gst_value_intersect_funcs->len;
4674   for (i = 0; i < len; i++) {
4675     intersect_info = &g_array_index (gst_value_intersect_funcs,
4676         GstValueIntersectInfo, i);
4677     if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
4678       return intersect_info->func (dest, value1, value2);
4679     }
4680     if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
4681       return intersect_info->func (dest, value2, value1);
4682     }
4683   }
4684   return FALSE;
4685 }
4686
4687
4688
4689 /* gst_value_register_intersect_func: (skip)
4690  * @type1: the first type to intersect
4691  * @type2: the second type to intersect
4692  * @func: the intersection function
4693  *
4694  * Registers a function that is called to calculate the intersection
4695  * of the values having the types @type1 and @type2.
4696  *
4697  * Intersect functions should be registered at startup before any pipelines are
4698  * started, as gst_value_register_intersect_func() is not thread-safe and
4699  * cannot be used at the same time as gst_value_intersect() or
4700  * gst_value_can_intersect().
4701  */
4702 static void
4703 gst_value_register_intersect_func (GType type1, GType type2,
4704     GstValueIntersectFunc func)
4705 {
4706   GstValueIntersectInfo intersect_info;
4707
4708   intersect_info.type1 = type1;
4709   intersect_info.type2 = type2;
4710   intersect_info.func = func;
4711
4712   g_array_append_val (gst_value_intersect_funcs, intersect_info);
4713 }
4714
4715
4716 /* subtraction */
4717
4718 /**
4719  * gst_value_subtract:
4720  * @dest: (out caller-allocates): the destination value for the result if the
4721  *     subtraction is not empty. May be NULL, in which case the resulting set
4722  *     will not be computed, which can give a fair speedup.
4723  * @minuend: the value to subtract from
4724  * @subtrahend: the value to subtract
4725  *
4726  * Subtracts @subtrahend from @minuend and stores the result in @dest.
4727  * Note that this means subtraction as in sets, not as in mathematics.
4728  *
4729  * Returns: %TRUE if the subtraction is not empty
4730  */
4731 gboolean
4732 gst_value_subtract (GValue * dest, const GValue * minuend,
4733     const GValue * subtrahend)
4734 {
4735   GstValueSubtractInfo *info;
4736   guint i, len;
4737   GType ltype, mtype, stype;
4738
4739   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4740   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4741
4742   ltype = gst_value_list_get_type ();
4743
4744   /* special cases first */
4745   if (G_VALUE_HOLDS (minuend, ltype))
4746     return gst_value_subtract_from_list (dest, minuend, subtrahend);
4747   if (G_VALUE_HOLDS (subtrahend, ltype))
4748     return gst_value_subtract_list (dest, minuend, subtrahend);
4749
4750   mtype = G_VALUE_TYPE (minuend);
4751   stype = G_VALUE_TYPE (subtrahend);
4752
4753   len = gst_value_subtract_funcs->len;
4754   for (i = 0; i < len; i++) {
4755     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4756     if (info->minuend == mtype && info->subtrahend == stype) {
4757       return info->func (dest, minuend, subtrahend);
4758     }
4759   }
4760
4761   if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
4762     if (dest)
4763       gst_value_init_and_copy (dest, minuend);
4764     return TRUE;
4765   }
4766
4767   return FALSE;
4768 }
4769
4770 #if 0
4771 gboolean
4772 gst_value_subtract (GValue * dest, const GValue * minuend,
4773     const GValue * subtrahend)
4774 {
4775   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
4776
4777   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
4778       gst_value_serialize (subtrahend),
4779       ret ? gst_value_serialize (dest) : "---");
4780   return ret;
4781 }
4782 #endif
4783
4784 /**
4785  * gst_value_can_subtract:
4786  * @minuend: the value to subtract from
4787  * @subtrahend: the value to subtract
4788  *
4789  * Checks if it's possible to subtract @subtrahend from @minuend.
4790  *
4791  * Returns: TRUE if a subtraction is possible
4792  */
4793 gboolean
4794 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
4795 {
4796   GstValueSubtractInfo *info;
4797   guint i, len;
4798   GType ltype, mtype, stype;
4799
4800   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4801   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4802
4803   ltype = gst_value_list_get_type ();
4804
4805   /* special cases */
4806   if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
4807     return TRUE;
4808
4809   mtype = G_VALUE_TYPE (minuend);
4810   stype = G_VALUE_TYPE (subtrahend);
4811
4812   len = gst_value_subtract_funcs->len;
4813   for (i = 0; i < len; i++) {
4814     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4815     if (info->minuend == mtype && info->subtrahend == stype)
4816       return TRUE;
4817   }
4818
4819   return gst_value_can_compare (minuend, subtrahend);
4820 }
4821
4822 /* gst_value_register_subtract_func: (skip)
4823  * @minuend_type: type of the minuend
4824  * @subtrahend_type: type of the subtrahend
4825  * @func: function to use
4826  *
4827  * Registers @func as a function capable of subtracting the values of
4828  * @subtrahend_type from values of @minuend_type.
4829  *
4830  * Subtract functions should be registered at startup before any pipelines are
4831  * started, as gst_value_register_subtract_func() is not thread-safe and
4832  * cannot be used at the same time as gst_value_subtract().
4833  */
4834 static void
4835 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
4836     GstValueSubtractFunc func)
4837 {
4838   GstValueSubtractInfo info;
4839
4840   g_return_if_fail (!gst_type_is_fixed (minuend_type)
4841       || !gst_type_is_fixed (subtrahend_type));
4842
4843   info.minuend = minuend_type;
4844   info.subtrahend = subtrahend_type;
4845   info.func = func;
4846
4847   g_array_append_val (gst_value_subtract_funcs, info);
4848 }
4849
4850 /**
4851  * gst_value_register:
4852  * @table: structure containing functions to register
4853  *
4854  * Registers functions to perform calculations on #GValue items of a given
4855  * type. Each type can only be added once.
4856  */
4857 void
4858 gst_value_register (const GstValueTable * table)
4859 {
4860   GstValueTable *found;
4861
4862   g_return_if_fail (table != NULL);
4863
4864   g_array_append_val (gst_value_table, *table);
4865
4866   found = gst_value_hash_lookup_type (table->type);
4867   if (found)
4868     g_warning ("adding type %s multiple times", g_type_name (table->type));
4869
4870   /* FIXME: we're not really doing the const justice, we assume the table is
4871    * static */
4872   gst_value_hash_add_type (table->type, table);
4873 }
4874
4875 /**
4876  * gst_value_init_and_copy:
4877  * @dest: (out caller-allocates): the target value
4878  * @src: the source value
4879  *
4880  * Initialises the target value to be of the same type as source and then copies
4881  * the contents from source to target.
4882  */
4883 void
4884 gst_value_init_and_copy (GValue * dest, const GValue * src)
4885 {
4886   g_return_if_fail (G_IS_VALUE (src));
4887   g_return_if_fail (dest != NULL);
4888
4889   g_value_init (dest, G_VALUE_TYPE (src));
4890   g_value_copy (src, dest);
4891 }
4892
4893 /* move src into dest and clear src */
4894 static void
4895 gst_value_move (GValue * dest, GValue * src)
4896 {
4897   g_assert (G_IS_VALUE (src));
4898   g_assert (dest != NULL);
4899
4900   *dest = *src;
4901   memset (src, 0, sizeof (GValue));
4902 }
4903
4904 /**
4905  * gst_value_serialize:
4906  * @value: a #GValue to serialize
4907  *
4908  * tries to transform the given @value into a string representation that allows
4909  * getting back this string later on using gst_value_deserialize().
4910  *
4911  * Free-function: g_free
4912  *
4913  * Returns: (transfer full): the serialization for @value or NULL if none exists
4914  */
4915 gchar *
4916 gst_value_serialize (const GValue * value)
4917 {
4918   guint i, len;
4919   GValue s_val = { 0 };
4920   GstValueTable *table, *best;
4921   gchar *s;
4922   GType type;
4923
4924   g_return_val_if_fail (G_IS_VALUE (value), NULL);
4925
4926   type = G_VALUE_TYPE (value);
4927
4928   best = gst_value_hash_lookup_type (type);
4929
4930   if (G_UNLIKELY (!best || !best->serialize)) {
4931     len = gst_value_table->len;
4932     best = NULL;
4933     for (i = 0; i < len; i++) {
4934       table = &g_array_index (gst_value_table, GstValueTable, i);
4935       if (table->serialize && g_type_is_a (type, table->type)) {
4936         if (!best || g_type_is_a (table->type, best->type))
4937           best = table;
4938       }
4939     }
4940   }
4941   if (G_LIKELY (best))
4942     return best->serialize (value);
4943
4944   g_value_init (&s_val, G_TYPE_STRING);
4945   if (g_value_transform (value, &s_val)) {
4946     s = gst_string_wrap (g_value_get_string (&s_val));
4947   } else {
4948     s = NULL;
4949   }
4950   g_value_unset (&s_val);
4951
4952   return s;
4953 }
4954
4955 /**
4956  * gst_value_deserialize:
4957  * @dest: (out caller-allocates): #GValue to fill with contents of
4958  *     deserialization
4959  * @src: string to deserialize
4960  *
4961  * Tries to deserialize a string into the type specified by the given GValue.
4962  * If the operation succeeds, TRUE is returned, FALSE otherwise.
4963  *
4964  * Returns: TRUE on success
4965  */
4966 gboolean
4967 gst_value_deserialize (GValue * dest, const gchar * src)
4968 {
4969   GstValueTable *table, *best;
4970   guint i, len;
4971   GType type;
4972
4973   g_return_val_if_fail (src != NULL, FALSE);
4974   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
4975
4976   type = G_VALUE_TYPE (dest);
4977
4978   best = gst_value_hash_lookup_type (type);
4979   if (G_UNLIKELY (!best || !best->deserialize)) {
4980     len = gst_value_table->len;
4981     best = NULL;
4982     for (i = 0; i < len; i++) {
4983       table = &g_array_index (gst_value_table, GstValueTable, i);
4984       if (table->deserialize && g_type_is_a (type, table->type)) {
4985         if (!best || g_type_is_a (table->type, best->type))
4986           best = table;
4987       }
4988     }
4989   }
4990   if (G_LIKELY (best))
4991     return best->deserialize (dest, src);
4992
4993   return FALSE;
4994 }
4995
4996 /**
4997  * gst_value_is_fixed:
4998  * @value: the #GValue to check
4999  *
5000  * Tests if the given GValue, if available in a GstStructure (or any other
5001  * container) contains a "fixed" (which means: one value) or an "unfixed"
5002  * (which means: multiple possible values, such as data lists or data
5003  * ranges) value.
5004  *
5005  * Returns: true if the value is "fixed".
5006  */
5007
5008 gboolean
5009 gst_value_is_fixed (const GValue * value)
5010 {
5011   GType type;
5012
5013   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
5014
5015   type = G_VALUE_TYPE (value);
5016
5017   /* the most common types are just basic plain glib types */
5018   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
5019     return TRUE;
5020   }
5021
5022   if (type == GST_TYPE_ARRAY) {
5023     gint size, n;
5024     const GValue *kid;
5025
5026     /* check recursively */
5027     size = gst_value_array_get_size (value);
5028     for (n = 0; n < size; n++) {
5029       kid = gst_value_array_get_value (value, n);
5030       if (!gst_value_is_fixed (kid))
5031         return FALSE;
5032     }
5033     return TRUE;
5034   }
5035   return gst_type_is_fixed (type);
5036 }
5037
5038 /**
5039  * gst_value_fixate:
5040  * @dest: the #GValue destination
5041  * @src: the #GValue to fixate
5042  *
5043  * Fixate @src into a new value @dest.
5044  * For ranges, the first element is taken. For lists and arrays, the
5045  * first item is fixated and returned.
5046  * If @src is already fixed, this function returns FALSE.
5047  *
5048  * Returns: true if @dest contains a fixated version of @src.
5049  */
5050 gboolean
5051 gst_value_fixate (GValue * dest, const GValue * src)
5052 {
5053   g_return_val_if_fail (G_IS_VALUE (src), FALSE);
5054   g_return_val_if_fail (dest != NULL, FALSE);
5055
5056   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
5057     g_value_init (dest, G_TYPE_INT);
5058     g_value_set_int (dest, gst_value_get_int_range_min (src));
5059   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
5060     g_value_init (dest, G_TYPE_DOUBLE);
5061     g_value_set_double (dest, gst_value_get_double_range_min (src));
5062   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
5063     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
5064   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
5065     GValue temp = { 0 };
5066
5067     /* list could be empty */
5068     if (gst_value_list_get_size (src) <= 0)
5069       return FALSE;
5070
5071     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
5072
5073     if (!gst_value_fixate (dest, &temp)) {
5074       gst_value_move (dest, &temp);
5075     } else {
5076       g_value_unset (&temp);
5077     }
5078   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
5079     gboolean res = FALSE;
5080     guint n, len;
5081
5082     len = gst_value_array_get_size (src);
5083     g_value_init (dest, GST_TYPE_ARRAY);
5084     for (n = 0; n < len; n++) {
5085       GValue kid = { 0 };
5086       const GValue *orig_kid = gst_value_array_get_value (src, n);
5087
5088       if (!gst_value_fixate (&kid, orig_kid))
5089         gst_value_init_and_copy (&kid, orig_kid);
5090       else
5091         res = TRUE;
5092       gst_value_array_append_and_take_value (dest, &kid);
5093     }
5094
5095     if (!res)
5096       g_value_unset (dest);
5097
5098     return res;
5099   } else {
5100     return FALSE;
5101   }
5102   return TRUE;
5103 }
5104
5105
5106 /************
5107  * fraction *
5108  ************/
5109
5110 /* helper functions */
5111 static void
5112 gst_value_init_fraction (GValue * value)
5113 {
5114   value->data[0].v_int = 0;
5115   value->data[1].v_int = 1;
5116 }
5117
5118 static void
5119 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
5120 {
5121   dest_value->data[0].v_int = src_value->data[0].v_int;
5122   dest_value->data[1].v_int = src_value->data[1].v_int;
5123 }
5124
5125 static gchar *
5126 gst_value_collect_fraction (GValue * value, guint n_collect_values,
5127     GTypeCValue * collect_values, guint collect_flags)
5128 {
5129   if (n_collect_values != 2)
5130     return g_strdup_printf ("not enough value locations for `%s' passed",
5131         G_VALUE_TYPE_NAME (value));
5132   if (collect_values[1].v_int == 0)
5133     return g_strdup_printf ("passed '0' as denominator for `%s'",
5134         G_VALUE_TYPE_NAME (value));
5135   if (collect_values[0].v_int < -G_MAXINT)
5136     return
5137         g_strdup_printf
5138         ("passed value smaller than -G_MAXINT as numerator for `%s'",
5139         G_VALUE_TYPE_NAME (value));
5140   if (collect_values[1].v_int < -G_MAXINT)
5141     return
5142         g_strdup_printf
5143         ("passed value smaller than -G_MAXINT as denominator for `%s'",
5144         G_VALUE_TYPE_NAME (value));
5145
5146   gst_value_set_fraction (value,
5147       collect_values[0].v_int, collect_values[1].v_int);
5148
5149   return NULL;
5150 }
5151
5152 static gchar *
5153 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
5154     GTypeCValue * collect_values, guint collect_flags)
5155 {
5156   gint *numerator = collect_values[0].v_pointer;
5157   gint *denominator = collect_values[1].v_pointer;
5158
5159   if (!numerator)
5160     return g_strdup_printf ("numerator for `%s' passed as NULL",
5161         G_VALUE_TYPE_NAME (value));
5162   if (!denominator)
5163     return g_strdup_printf ("denominator for `%s' passed as NULL",
5164         G_VALUE_TYPE_NAME (value));
5165
5166   *numerator = value->data[0].v_int;
5167   *denominator = value->data[1].v_int;
5168
5169   return NULL;
5170 }
5171
5172 /**
5173  * gst_value_set_fraction:
5174  * @value: a GValue initialized to #GST_TYPE_FRACTION
5175  * @numerator: the numerator of the fraction
5176  * @denominator: the denominator of the fraction
5177  *
5178  * Sets @value to the fraction specified by @numerator over @denominator.
5179  * The fraction gets reduced to the smallest numerator and denominator,
5180  * and if necessary the sign is moved to the numerator.
5181  */
5182 void
5183 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
5184 {
5185   gint gcd = 0;
5186
5187   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
5188   g_return_if_fail (denominator != 0);
5189   g_return_if_fail (denominator >= -G_MAXINT);
5190   g_return_if_fail (numerator >= -G_MAXINT);
5191
5192   /* normalize sign */
5193   if (denominator < 0) {
5194     numerator = -numerator;
5195     denominator = -denominator;
5196   }
5197
5198   /* check for reduction */
5199   gcd = gst_util_greatest_common_divisor (numerator, denominator);
5200   if (gcd) {
5201     numerator /= gcd;
5202     denominator /= gcd;
5203   }
5204
5205   g_assert (denominator > 0);
5206
5207   value->data[0].v_int = numerator;
5208   value->data[1].v_int = denominator;
5209 }
5210
5211 /**
5212  * gst_value_get_fraction_numerator:
5213  * @value: a GValue initialized to #GST_TYPE_FRACTION
5214  *
5215  * Gets the numerator of the fraction specified by @value.
5216  *
5217  * Returns: the numerator of the fraction.
5218  */
5219 gint
5220 gst_value_get_fraction_numerator (const GValue * value)
5221 {
5222   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
5223
5224   return value->data[0].v_int;
5225 }
5226
5227 /**
5228  * gst_value_get_fraction_denominator:
5229  * @value: a GValue initialized to #GST_TYPE_FRACTION
5230  *
5231  * Gets the denominator of the fraction specified by @value.
5232  *
5233  * Returns: the denominator of the fraction.
5234  */
5235 gint
5236 gst_value_get_fraction_denominator (const GValue * value)
5237 {
5238   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
5239
5240   return value->data[1].v_int;
5241 }
5242
5243 /**
5244  * gst_value_fraction_multiply:
5245  * @product: a GValue initialized to #GST_TYPE_FRACTION
5246  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
5247  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
5248  *
5249  * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
5250  * @product to the product of the two fractions.
5251  *
5252  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
5253  */
5254 gboolean
5255 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
5256     const GValue * factor2)
5257 {
5258   gint n1, n2, d1, d2;
5259   gint res_n, res_d;
5260
5261   g_return_val_if_fail (product != NULL, FALSE);
5262   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
5263   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
5264
5265   n1 = factor1->data[0].v_int;
5266   n2 = factor2->data[0].v_int;
5267   d1 = factor1->data[1].v_int;
5268   d2 = factor2->data[1].v_int;
5269
5270   if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
5271     return FALSE;
5272
5273   gst_value_set_fraction (product, res_n, res_d);
5274
5275   return TRUE;
5276 }
5277
5278 /**
5279  * gst_value_fraction_subtract:
5280  * @dest: a GValue initialized to #GST_TYPE_FRACTION
5281  * @minuend: a GValue initialized to #GST_TYPE_FRACTION
5282  * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
5283  *
5284  * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
5285  *
5286  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
5287  */
5288 gboolean
5289 gst_value_fraction_subtract (GValue * dest,
5290     const GValue * minuend, const GValue * subtrahend)
5291 {
5292   gint n1, n2, d1, d2;
5293   gint res_n, res_d;
5294
5295   g_return_val_if_fail (dest != NULL, FALSE);
5296   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
5297   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
5298
5299   n1 = minuend->data[0].v_int;
5300   n2 = subtrahend->data[0].v_int;
5301   d1 = minuend->data[1].v_int;
5302   d2 = subtrahend->data[1].v_int;
5303
5304   if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
5305     return FALSE;
5306   gst_value_set_fraction (dest, res_n, res_d);
5307
5308   return TRUE;
5309 }
5310
5311 static gchar *
5312 gst_value_serialize_fraction (const GValue * value)
5313 {
5314   gint32 numerator = value->data[0].v_int;
5315   gint32 denominator = value->data[1].v_int;
5316   gboolean positive = TRUE;
5317
5318   /* get the sign and make components absolute */
5319   if (numerator < 0) {
5320     numerator = -numerator;
5321     positive = !positive;
5322   }
5323   if (denominator < 0) {
5324     denominator = -denominator;
5325     positive = !positive;
5326   }
5327
5328   return g_strdup_printf ("%s%d/%d",
5329       positive ? "" : "-", numerator, denominator);
5330 }
5331
5332 static gboolean
5333 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
5334 {
5335   gint num, den;
5336   gint num_chars;
5337
5338   if (G_UNLIKELY (s == NULL))
5339     return FALSE;
5340
5341   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
5342     return FALSE;
5343
5344   if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
5345     if (s[num_chars] != 0)
5346       return FALSE;
5347     if (den == 0)
5348       return FALSE;
5349
5350     gst_value_set_fraction (dest, num, den);
5351     return TRUE;
5352   } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
5353     gst_value_set_fraction (dest, 1, G_MAXINT);
5354     return TRUE;
5355   } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
5356     if (s[num_chars] != 0)
5357       return FALSE;
5358     gst_value_set_fraction (dest, num, 1);
5359     return TRUE;
5360   } else if (g_ascii_strcasecmp (s, "min") == 0) {
5361     gst_value_set_fraction (dest, -G_MAXINT, 1);
5362     return TRUE;
5363   } else if (g_ascii_strcasecmp (s, "max") == 0) {
5364     gst_value_set_fraction (dest, G_MAXINT, 1);
5365     return TRUE;
5366   }
5367
5368   return FALSE;
5369 }
5370
5371 static void
5372 gst_value_transform_fraction_string (const GValue * src_value,
5373     GValue * dest_value)
5374 {
5375   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
5376 }
5377
5378 static void
5379 gst_value_transform_string_fraction (const GValue * src_value,
5380     GValue * dest_value)
5381 {
5382   if (!gst_value_deserialize_fraction (dest_value,
5383           src_value->data[0].v_pointer))
5384     /* If the deserialize fails, ensure we leave the fraction in a
5385      * valid, if incorrect, state */
5386     gst_value_set_fraction (dest_value, 0, 1);
5387 }
5388
5389 static void
5390 gst_value_transform_double_fraction (const GValue * src_value,
5391     GValue * dest_value)
5392 {
5393   gdouble src = g_value_get_double (src_value);
5394   gint n, d;
5395
5396   gst_util_double_to_fraction (src, &n, &d);
5397   gst_value_set_fraction (dest_value, n, d);
5398 }
5399
5400 static void
5401 gst_value_transform_float_fraction (const GValue * src_value,
5402     GValue * dest_value)
5403 {
5404   gfloat src = g_value_get_float (src_value);
5405   gint n, d;
5406
5407   gst_util_double_to_fraction (src, &n, &d);
5408   gst_value_set_fraction (dest_value, n, d);
5409 }
5410
5411 static void
5412 gst_value_transform_fraction_double (const GValue * src_value,
5413     GValue * dest_value)
5414 {
5415   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
5416       ((double) src_value->data[1].v_int);
5417 }
5418
5419 static void
5420 gst_value_transform_fraction_float (const GValue * src_value,
5421     GValue * dest_value)
5422 {
5423   dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
5424       ((float) src_value->data[1].v_int);
5425 }
5426
5427 static gint
5428 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
5429 {
5430   gint n1, n2;
5431   gint d1, d2;
5432   gint ret;
5433
5434   n1 = value1->data[0].v_int;
5435   n2 = value2->data[0].v_int;
5436   d1 = value1->data[1].v_int;
5437   d2 = value2->data[1].v_int;
5438
5439   /* fractions are reduced when set, so we can quickly see if they're equal */
5440   if (n1 == n2 && d1 == d2)
5441     return GST_VALUE_EQUAL;
5442
5443   if (d1 == 0 && d2 == 0)
5444     return GST_VALUE_UNORDERED;
5445   else if (d1 == 0)
5446     return GST_VALUE_GREATER_THAN;
5447   else if (d2 == 0)
5448     return GST_VALUE_LESS_THAN;
5449
5450   ret = gst_util_fraction_compare (n1, d1, n2, d2);
5451   if (ret == -1)
5452     return GST_VALUE_LESS_THAN;
5453   else if (ret == 1)
5454     return GST_VALUE_GREATER_THAN;
5455
5456   /* Equality can't happen here because we check for that
5457    * first already */
5458   g_return_val_if_reached (GST_VALUE_UNORDERED);
5459 }
5460
5461 /*********
5462  * GDate *
5463  *********/
5464
5465 static gint
5466 gst_value_compare_date (const GValue * value1, const GValue * value2)
5467 {
5468   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
5469   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
5470   guint32 j1, j2;
5471
5472   if (date1 == date2)
5473     return GST_VALUE_EQUAL;
5474
5475   if ((date1 == NULL || !g_date_valid (date1))
5476       && (date2 != NULL && g_date_valid (date2))) {
5477     return GST_VALUE_LESS_THAN;
5478   }
5479
5480   if ((date2 == NULL || !g_date_valid (date2))
5481       && (date1 != NULL && g_date_valid (date1))) {
5482     return GST_VALUE_GREATER_THAN;
5483   }
5484
5485   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
5486       || !g_date_valid (date2)) {
5487     return GST_VALUE_UNORDERED;
5488   }
5489
5490   j1 = g_date_get_julian (date1);
5491   j2 = g_date_get_julian (date2);
5492
5493   if (j1 == j2)
5494     return GST_VALUE_EQUAL;
5495   else if (j1 < j2)
5496     return GST_VALUE_LESS_THAN;
5497   else
5498     return GST_VALUE_GREATER_THAN;
5499 }
5500
5501 static gchar *
5502 gst_value_serialize_date (const GValue * val)
5503 {
5504   const GDate *date = (const GDate *) g_value_get_boxed (val);
5505
5506   if (date == NULL || !g_date_valid (date))
5507     return g_strdup ("9999-99-99");
5508
5509   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
5510       g_date_get_month (date), g_date_get_day (date));
5511 }
5512
5513 static gboolean
5514 gst_value_deserialize_date (GValue * dest, const gchar * s)
5515 {
5516   guint year, month, day;
5517
5518   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
5519     return FALSE;
5520
5521   if (!g_date_valid_dmy (day, month, year))
5522     return FALSE;
5523
5524   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
5525   return TRUE;
5526 }
5527
5528 /*************
5529  * GstDateTime *
5530  *************/
5531
5532 static gint
5533 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
5534 {
5535   const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
5536   const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
5537
5538   if (date1 == date2)
5539     return GST_VALUE_EQUAL;
5540
5541   if ((date1 == NULL) && (date2 != NULL)) {
5542     return GST_VALUE_LESS_THAN;
5543   }
5544   if ((date2 == NULL) && (date1 != NULL)) {
5545     return GST_VALUE_LESS_THAN;
5546   }
5547
5548   /* returns GST_VALUE_* */
5549   return __gst_date_time_compare (date1, date2);
5550 }
5551
5552 static gchar *
5553 gst_value_serialize_date_time (const GValue * val)
5554 {
5555   GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
5556
5557   if (date == NULL)
5558     return g_strdup ("null");
5559
5560   return __gst_date_time_serialize (date, TRUE);
5561 }
5562
5563 static gboolean
5564 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
5565 {
5566   GstDateTime *datetime;
5567
5568   if (!s || strcmp (s, "null") == 0) {
5569     return FALSE;
5570   }
5571
5572   datetime = gst_date_time_new_from_iso8601_string (s);
5573   if (datetime != NULL) {
5574     g_value_take_boxed (dest, datetime);
5575     return TRUE;
5576   }
5577   GST_WARNING ("Failed to deserialize date time string '%s'", s);
5578   return FALSE;
5579 }
5580
5581 static void
5582 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
5583 {
5584   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
5585 }
5586
5587 static void
5588 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
5589 {
5590   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
5591 }
5592
5593
5594 /************
5595  * bitmask *
5596  ************/
5597
5598 /* helper functions */
5599 static void
5600 gst_value_init_bitmask (GValue * value)
5601 {
5602   value->data[0].v_uint64 = 0;
5603 }
5604
5605 static void
5606 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
5607 {
5608   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5609 }
5610
5611 static gchar *
5612 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
5613     GTypeCValue * collect_values, guint collect_flags)
5614 {
5615   if (n_collect_values != 1)
5616     return g_strdup_printf ("not enough value locations for `%s' passed",
5617         G_VALUE_TYPE_NAME (value));
5618
5619   gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
5620
5621   return NULL;
5622 }
5623
5624 static gchar *
5625 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
5626     GTypeCValue * collect_values, guint collect_flags)
5627 {
5628   guint64 *bitmask = collect_values[0].v_pointer;
5629
5630   if (!bitmask)
5631     return g_strdup_printf ("value for `%s' passed as NULL",
5632         G_VALUE_TYPE_NAME (value));
5633
5634   *bitmask = value->data[0].v_uint64;
5635
5636   return NULL;
5637 }
5638
5639 /**
5640  * gst_value_set_bitmask:
5641  * @value: a GValue initialized to #GST_TYPE_FRACTION
5642  * @bitmask: the bitmask
5643  *
5644  * Sets @value to the bitmask specified by @bitmask.
5645  */
5646 void
5647 gst_value_set_bitmask (GValue * value, guint64 bitmask)
5648 {
5649   g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
5650
5651   value->data[0].v_uint64 = bitmask;
5652 }
5653
5654 /**
5655  * gst_value_get_bitmask:
5656  * @value: a GValue initialized to #GST_TYPE_FRACTION
5657  *
5658  * Gets the bitmask specified by @value.
5659  *
5660  * Returns: the bitmask.
5661  */
5662 guint64
5663 gst_value_get_bitmask (const GValue * value)
5664 {
5665   g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
5666
5667   return value->data[0].v_uint64;
5668 }
5669
5670 static gchar *
5671 gst_value_serialize_bitmask (const GValue * value)
5672 {
5673   guint64 bitmask = value->data[0].v_uint64;
5674
5675   return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
5676 }
5677
5678 static gboolean
5679 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
5680 {
5681   gchar *endptr = NULL;
5682   guint64 val;
5683
5684   if (G_UNLIKELY (s == NULL))
5685     return FALSE;
5686
5687   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
5688     return FALSE;
5689
5690   val = g_ascii_strtoull (s, &endptr, 16);
5691   if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
5692     return FALSE;
5693   if (val == 0 && endptr == s)
5694     return FALSE;
5695
5696   gst_value_set_bitmask (dest, val);
5697
5698   return TRUE;
5699 }
5700
5701 static void
5702 gst_value_transform_bitmask_string (const GValue * src_value,
5703     GValue * dest_value)
5704 {
5705   dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
5706 }
5707
5708 static void
5709 gst_value_transform_string_bitmask (const GValue * src_value,
5710     GValue * dest_value)
5711 {
5712   if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
5713     gst_value_set_bitmask (dest_value, 0);
5714 }
5715
5716 static void
5717 gst_value_transform_uint64_bitmask (const GValue * src_value,
5718     GValue * dest_value)
5719 {
5720   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5721 }
5722
5723 static void
5724 gst_value_transform_bitmask_uint64 (const GValue * src_value,
5725     GValue * dest_value)
5726 {
5727   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5728 }
5729
5730 static gint
5731 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
5732 {
5733   guint64 v1, v2;
5734
5735   v1 = value1->data[0].v_uint64;
5736   v2 = value2->data[0].v_uint64;
5737
5738   if (v1 == v2)
5739     return GST_VALUE_EQUAL;
5740
5741   return GST_VALUE_UNORDERED;
5742 }
5743
5744 static void
5745 gst_value_transform_object_string (const GValue * src_value,
5746     GValue * dest_value)
5747 {
5748   GstObject *obj;
5749   gchar *str;
5750
5751   obj = g_value_get_object (src_value);
5752   if (obj) {
5753     str =
5754         g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
5755         GST_OBJECT_NAME (obj));
5756   } else {
5757     str = g_strdup ("NULL");
5758   }
5759
5760   dest_value->data[0].v_pointer = str;
5761 }
5762
5763 static GTypeInfo _info = {
5764   0,
5765   NULL,
5766   NULL,
5767   NULL,
5768   NULL,
5769   NULL,
5770   0,
5771   0,
5772   NULL,
5773   NULL,
5774 };
5775
5776 static GTypeFundamentalInfo _finfo = {
5777   0
5778 };
5779
5780 #define FUNC_VALUE_GET_TYPE(type, name)                         \
5781 GType gst_ ## type ## _get_type (void)                          \
5782 {                                                               \
5783   static volatile GType gst_ ## type ## _type = 0;                       \
5784                                                                 \
5785   if (g_once_init_enter (&gst_ ## type ## _type)) {             \
5786     GType _type;                                        \
5787     _info.value_table = & _gst_ ## type ## _value_table;        \
5788     _type = g_type_register_fundamental (       \
5789         g_type_fundamental_next (),                             \
5790         name, &_info, &_finfo, 0);                              \
5791     g_once_init_leave(&gst_ ## type ## _type, _type);   \
5792   }                                                             \
5793                                                                 \
5794   return gst_ ## type ## _type;                                 \
5795 }
5796
5797 static const GTypeValueTable _gst_int_range_value_table = {
5798   gst_value_init_int_range,
5799   gst_value_free_int_range,
5800   gst_value_copy_int_range,
5801   NULL,
5802   (char *) "ii",
5803   gst_value_collect_int_range,
5804   (char *) "pp",
5805   gst_value_lcopy_int_range
5806 };
5807
5808 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
5809
5810 static const GTypeValueTable _gst_int64_range_value_table = {
5811   gst_value_init_int64_range,
5812   gst_value_free_int64_range,
5813   gst_value_copy_int64_range,
5814   NULL,
5815   (char *) "qq",
5816   gst_value_collect_int64_range,
5817   (char *) "pp",
5818   gst_value_lcopy_int64_range
5819 };
5820
5821 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
5822
5823 static const GTypeValueTable _gst_double_range_value_table = {
5824   gst_value_init_double_range,
5825   NULL,
5826   gst_value_copy_double_range,
5827   NULL,
5828   (char *) "dd",
5829   gst_value_collect_double_range,
5830   (char *) "pp",
5831   gst_value_lcopy_double_range
5832 };
5833
5834 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
5835
5836 static const GTypeValueTable _gst_fraction_range_value_table = {
5837   gst_value_init_fraction_range,
5838   gst_value_free_fraction_range,
5839   gst_value_copy_fraction_range,
5840   NULL,
5841   (char *) "iiii",
5842   gst_value_collect_fraction_range,
5843   (char *) "pppp",
5844   gst_value_lcopy_fraction_range
5845 };
5846
5847 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
5848
5849 static const GTypeValueTable _gst_value_list_value_table = {
5850   gst_value_init_list_or_array,
5851   gst_value_free_list_or_array,
5852   gst_value_copy_list_or_array,
5853   gst_value_list_or_array_peek_pointer,
5854   (char *) "p",
5855   gst_value_collect_list_or_array,
5856   (char *) "p",
5857   gst_value_lcopy_list_or_array
5858 };
5859
5860 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
5861
5862 static const GTypeValueTable _gst_value_array_value_table = {
5863   gst_value_init_list_or_array,
5864   gst_value_free_list_or_array,
5865   gst_value_copy_list_or_array,
5866   gst_value_list_or_array_peek_pointer,
5867   (char *) "p",
5868   gst_value_collect_list_or_array,
5869   (char *) "p",
5870   gst_value_lcopy_list_or_array
5871 };
5872
5873 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
5874
5875 static const GTypeValueTable _gst_fraction_value_table = {
5876   gst_value_init_fraction,
5877   NULL,
5878   gst_value_copy_fraction,
5879   NULL,
5880   (char *) "ii",
5881   gst_value_collect_fraction,
5882   (char *) "pp",
5883   gst_value_lcopy_fraction
5884 };
5885
5886 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
5887
5888 G_DEFINE_BOXED_TYPE (GstDateTime, gst_date_time,
5889     (GBoxedCopyFunc) gst_date_time_ref, (GBoxedFreeFunc) gst_date_time_unref);
5890
5891 static const GTypeValueTable _gst_bitmask_value_table = {
5892   gst_value_init_bitmask,
5893   NULL,
5894   gst_value_copy_bitmask,
5895   NULL,
5896   (char *) "q",
5897   gst_value_collect_bitmask,
5898   (char *) "p",
5899   gst_value_lcopy_bitmask
5900 };
5901
5902 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
5903
5904 GType
5905 gst_g_thread_get_type (void)
5906 {
5907 #if GLIB_CHECK_VERSION(2,35,3)
5908   return G_TYPE_THREAD;
5909 #else
5910   static volatile gsize type_id = 0;
5911
5912   if (g_once_init_enter (&type_id)) {
5913     GType tmp =
5914         g_boxed_type_register_static (g_intern_static_string ("GstGThread"),
5915         (GBoxedCopyFunc) g_thread_ref,
5916         (GBoxedFreeFunc) g_thread_unref);
5917     g_once_init_leave (&type_id, tmp);
5918   }
5919
5920   return type_id;
5921 #endif
5922 }
5923
5924 void
5925 _priv_gst_value_initialize (void)
5926 {
5927   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
5928   gst_value_hash = g_hash_table_new (NULL, NULL);
5929   gst_value_union_funcs = g_array_new (FALSE, FALSE,
5930       sizeof (GstValueUnionInfo));
5931   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
5932       sizeof (GstValueIntersectInfo));
5933   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
5934       sizeof (GstValueSubtractInfo));
5935
5936   {
5937     static GstValueTable gst_value = {
5938       0,
5939       gst_value_compare_int_range,
5940       gst_value_serialize_int_range,
5941       gst_value_deserialize_int_range,
5942     };
5943
5944     gst_value.type = gst_int_range_get_type ();
5945     gst_value_register (&gst_value);
5946   }
5947
5948   {
5949     static GstValueTable gst_value = {
5950       0,
5951       gst_value_compare_int64_range,
5952       gst_value_serialize_int64_range,
5953       gst_value_deserialize_int64_range,
5954     };
5955
5956     gst_value.type = gst_int64_range_get_type ();
5957     gst_value_register (&gst_value);
5958   }
5959
5960   {
5961     static GstValueTable gst_value = {
5962       0,
5963       gst_value_compare_double_range,
5964       gst_value_serialize_double_range,
5965       gst_value_deserialize_double_range,
5966     };
5967
5968     gst_value.type = gst_double_range_get_type ();
5969     gst_value_register (&gst_value);
5970   }
5971
5972   {
5973     static GstValueTable gst_value = {
5974       0,
5975       gst_value_compare_fraction_range,
5976       gst_value_serialize_fraction_range,
5977       gst_value_deserialize_fraction_range,
5978     };
5979
5980     gst_value.type = gst_fraction_range_get_type ();
5981     gst_value_register (&gst_value);
5982   }
5983
5984   {
5985     static GstValueTable gst_value = {
5986       0,
5987       gst_value_compare_list,
5988       gst_value_serialize_list,
5989       gst_value_deserialize_list,
5990     };
5991
5992     gst_value.type = gst_value_list_get_type ();
5993     gst_value_register (&gst_value);
5994   }
5995
5996   {
5997     static GstValueTable gst_value = {
5998       0,
5999       gst_value_compare_array,
6000       gst_value_serialize_array,
6001       gst_value_deserialize_array,
6002     };
6003
6004     gst_value.type = gst_value_array_get_type ();
6005     gst_value_register (&gst_value);
6006   }
6007
6008   {
6009 #if 0
6010     static const GTypeValueTable value_table = {
6011       gst_value_init_buffer,
6012       NULL,
6013       gst_value_copy_buffer,
6014       NULL,
6015       "i",
6016       NULL,                     /*gst_value_collect_buffer, */
6017       "p",
6018       NULL                      /*gst_value_lcopy_buffer */
6019     };
6020 #endif
6021     static GstValueTable gst_value = {
6022       0,
6023       gst_value_compare_buffer,
6024       gst_value_serialize_buffer,
6025       gst_value_deserialize_buffer,
6026     };
6027
6028     gst_value.type = GST_TYPE_BUFFER;
6029     gst_value_register (&gst_value);
6030   }
6031   {
6032     static GstValueTable gst_value = {
6033       0,
6034       gst_value_compare_sample,
6035       gst_value_serialize_sample,
6036       gst_value_deserialize_sample,
6037     };
6038
6039     gst_value.type = GST_TYPE_SAMPLE;
6040     gst_value_register (&gst_value);
6041   }
6042   {
6043     static GstValueTable gst_value = {
6044       0,
6045       gst_value_compare_fraction,
6046       gst_value_serialize_fraction,
6047       gst_value_deserialize_fraction,
6048     };
6049
6050     gst_value.type = gst_fraction_get_type ();
6051     gst_value_register (&gst_value);
6052   }
6053   {
6054     static GstValueTable gst_value = {
6055       0,
6056       gst_value_compare_caps,
6057       gst_value_serialize_caps,
6058       gst_value_deserialize_caps,
6059     };
6060
6061     gst_value.type = GST_TYPE_CAPS;
6062     gst_value_register (&gst_value);
6063   }
6064   {
6065     static GstValueTable gst_value = {
6066       0,
6067       NULL,
6068       gst_value_serialize_segment,
6069       gst_value_deserialize_segment,
6070     };
6071
6072     gst_value.type = GST_TYPE_SEGMENT;
6073     gst_value_register (&gst_value);
6074   }
6075   {
6076     static GstValueTable gst_value = {
6077       0,
6078       NULL,
6079       gst_value_serialize_structure,
6080       gst_value_deserialize_structure,
6081     };
6082
6083     gst_value.type = GST_TYPE_STRUCTURE;
6084     gst_value_register (&gst_value);
6085   }
6086   {
6087     static GstValueTable gst_value = {
6088       0,
6089       NULL,
6090       gst_value_serialize_caps_features,
6091       gst_value_deserialize_caps_features,
6092     };
6093
6094     gst_value.type = GST_TYPE_CAPS_FEATURES;
6095     gst_value_register (&gst_value);
6096   }
6097   {
6098     static GstValueTable gst_value = {
6099       0,
6100       NULL,
6101       gst_value_serialize_tag_list,
6102       gst_value_deserialize_tag_list,
6103     };
6104
6105     gst_value.type = GST_TYPE_TAG_LIST;
6106     gst_value_register (&gst_value);
6107   }
6108   {
6109     static GstValueTable gst_value = {
6110       0,
6111       gst_value_compare_date,
6112       gst_value_serialize_date,
6113       gst_value_deserialize_date,
6114     };
6115
6116     gst_value.type = G_TYPE_DATE;
6117     gst_value_register (&gst_value);
6118   }
6119   {
6120     static GstValueTable gst_value = {
6121       0,
6122       gst_value_compare_date_time,
6123       gst_value_serialize_date_time,
6124       gst_value_deserialize_date_time,
6125     };
6126
6127     gst_value.type = gst_date_time_get_type ();
6128     gst_value_register (&gst_value);
6129   }
6130
6131   {
6132     static GstValueTable gst_value = {
6133       0,
6134       gst_value_compare_bitmask,
6135       gst_value_serialize_bitmask,
6136       gst_value_deserialize_bitmask,
6137     };
6138
6139     gst_value.type = gst_bitmask_get_type ();
6140     gst_value_register (&gst_value);
6141   }
6142
6143   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
6144   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
6145
6146   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
6147   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
6148   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
6149
6150   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
6151
6152   REGISTER_SERIALIZATION (G_TYPE_INT, int);
6153
6154   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
6155   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
6156
6157   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
6158   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
6159   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
6160
6161   REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
6162
6163   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
6164       gst_value_transform_int_range_string);
6165   g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
6166       gst_value_transform_int64_range_string);
6167   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
6168       gst_value_transform_double_range_string);
6169   g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
6170       gst_value_transform_fraction_range_string);
6171   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
6172       gst_value_transform_list_string);
6173   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
6174       gst_value_transform_array_string);
6175   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
6176       gst_value_transform_fraction_string);
6177   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
6178       gst_value_transform_string_fraction);
6179   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
6180       gst_value_transform_fraction_double);
6181   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
6182       gst_value_transform_fraction_float);
6183   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
6184       gst_value_transform_double_fraction);
6185   g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
6186       gst_value_transform_float_fraction);
6187   g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
6188       gst_value_transform_date_string);
6189   g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
6190       gst_value_transform_string_date);
6191   g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
6192       gst_value_transform_object_string);
6193   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
6194       gst_value_transform_bitmask_uint64);
6195   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
6196       gst_value_transform_bitmask_string);
6197   g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
6198       gst_value_transform_uint64_bitmask);
6199   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
6200       gst_value_transform_string_bitmask);
6201
6202   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6203       gst_value_intersect_int_int_range);
6204   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6205       gst_value_intersect_int_range_int_range);
6206   gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6207       gst_value_intersect_int64_int64_range);
6208   gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6209       gst_value_intersect_int64_range_int64_range);
6210   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6211       gst_value_intersect_double_double_range);
6212   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
6213       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
6214   gst_value_register_intersect_func (GST_TYPE_ARRAY,
6215       GST_TYPE_ARRAY, gst_value_intersect_array);
6216   gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6217       gst_value_intersect_fraction_fraction_range);
6218   gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
6219       GST_TYPE_FRACTION_RANGE,
6220       gst_value_intersect_fraction_range_fraction_range);
6221
6222   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6223       gst_value_subtract_int_int_range);
6224   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
6225       gst_value_subtract_int_range_int);
6226   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6227       gst_value_subtract_int_range_int_range);
6228   gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6229       gst_value_subtract_int64_int64_range);
6230   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
6231       gst_value_subtract_int64_range_int64);
6232   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6233       gst_value_subtract_int64_range_int64_range);
6234   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6235       gst_value_subtract_double_double_range);
6236   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
6237       gst_value_subtract_double_range_double);
6238   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
6239       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
6240   gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6241       gst_value_subtract_fraction_fraction_range);
6242   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
6243       gst_value_subtract_fraction_range_fraction);
6244   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
6245       GST_TYPE_FRACTION_RANGE,
6246       gst_value_subtract_fraction_range_fraction_range);
6247
6248   /* see bug #317246, #64994, #65041 */
6249   {
6250     volatile GType date_type = G_TYPE_DATE;
6251
6252     g_type_name (date_type);
6253   }
6254
6255   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6256       gst_value_union_int_int_range);
6257   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6258       gst_value_union_int_range_int_range);
6259
6260 #if 0
6261   /* Implement these if needed */
6262   gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6263       gst_value_union_fraction_fraction_range);
6264   gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
6265       GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
6266 #endif
6267 }