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