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