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