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