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