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