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