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