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