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