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