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