gstvalue: use g_assert() in internal function for already-checked things
[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_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_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_list (const GValue * value)
921 {
922   return gst_value_serialize_any_list (value, "{ ", " }");
923 }
924
925 static gboolean
926 gst_value_deserialize_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_array (const GValue * value)
934 {
935   return gst_value_serialize_any_list (value, "< ", " >");
936 }
937
938 static gboolean
939 gst_value_deserialize_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
2163 static gboolean
2164 gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
2165 {
2166   GstTagList *taglist;
2167
2168   if (*s != '"') {
2169     taglist = gst_tag_list_new_from_string (s);
2170   } else {
2171     gchar *str = gst_string_unwrap (s);
2172
2173     if (G_UNLIKELY (!str))
2174       return FALSE;
2175
2176     taglist = gst_tag_list_new_from_string (str);
2177     g_free (str);
2178   }
2179
2180   if (G_LIKELY (taglist != NULL)) {
2181     g_value_take_boxed (dest, taglist);
2182     return TRUE;
2183   }
2184   return FALSE;
2185 }
2186
2187 static gchar *
2188 gst_value_serialize_tag_list (const GValue * value)
2189 {
2190   GstTagList *taglist = g_value_get_boxed (value);
2191
2192   return gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
2193 }
2194
2195
2196 /*************
2197  * GstBuffer *
2198  *************/
2199
2200 static gint
2201 compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
2202 {
2203   gsize size1, size2;
2204   GstMapInfo info1, info2;
2205   gint result, mret;
2206
2207   if (buf1 == buf2)
2208     return GST_VALUE_EQUAL;
2209
2210   size1 = gst_buffer_get_size (buf1);
2211   size2 = gst_buffer_get_size (buf2);
2212
2213   if (size1 != size2)
2214     return GST_VALUE_UNORDERED;
2215
2216   if (size1 == 0)
2217     return GST_VALUE_EQUAL;
2218
2219   if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
2220     return GST_VALUE_UNORDERED;
2221
2222   if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
2223     gst_buffer_unmap (buf1, &info1);
2224     return GST_VALUE_UNORDERED;
2225   }
2226
2227   mret = memcmp (info1.data, info2.data, info1.size);
2228   if (mret == 0)
2229     result = GST_VALUE_EQUAL;
2230   else if (mret < 0)
2231     result = GST_VALUE_LESS_THAN;
2232   else
2233     result = GST_VALUE_GREATER_THAN;
2234
2235   gst_buffer_unmap (buf1, &info1);
2236   gst_buffer_unmap (buf2, &info2);
2237
2238   return result;
2239 }
2240
2241 static gint
2242 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
2243 {
2244   GstBuffer *buf1 = gst_value_get_buffer (value1);
2245   GstBuffer *buf2 = gst_value_get_buffer (value2);
2246
2247   return compare_buffer (buf1, buf2);
2248 }
2249
2250 static gchar *
2251 gst_value_serialize_buffer (const GValue * value)
2252 {
2253   GstMapInfo info;
2254   guint8 *data;
2255   gint i;
2256   gchar *string;
2257   GstBuffer *buffer;
2258
2259   buffer = gst_value_get_buffer (value);
2260   if (buffer == NULL)
2261     return NULL;
2262
2263   if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
2264     return NULL;
2265
2266   data = info.data;
2267
2268   string = g_malloc (info.size * 2 + 1);
2269   for (i = 0; i < info.size; i++) {
2270     sprintf (string + i * 2, "%02x", data[i]);
2271   }
2272   string[info.size * 2] = 0;
2273
2274   gst_buffer_unmap (buffer, &info);
2275
2276   return string;
2277 }
2278
2279 static gboolean
2280 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
2281 {
2282   GstBuffer *buffer;
2283   gint len;
2284   gchar ts[3];
2285   GstMapInfo info;
2286   guint8 *data;
2287   gint i;
2288
2289   len = strlen (s);
2290   if (len & 1)
2291     goto wrong_length;
2292
2293   buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
2294   if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
2295     goto map_failed;
2296   data = info.data;
2297
2298   for (i = 0; i < len / 2; i++) {
2299     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
2300       goto wrong_char;
2301
2302     ts[0] = s[i * 2 + 0];
2303     ts[1] = s[i * 2 + 1];
2304     ts[2] = 0;
2305
2306     data[i] = (guint8) strtoul (ts, NULL, 16);
2307   }
2308   gst_buffer_unmap (buffer, &info);
2309
2310   gst_value_take_buffer (dest, buffer);
2311
2312   return TRUE;
2313
2314   /* ERRORS */
2315 wrong_length:
2316   {
2317     return FALSE;
2318   }
2319 map_failed:
2320   {
2321     return FALSE;
2322   }
2323 wrong_char:
2324   {
2325     gst_buffer_unref (buffer);
2326     gst_buffer_unmap (buffer, &info);
2327     return FALSE;
2328   }
2329 }
2330
2331 /*************
2332  * GstSample *
2333  *************/
2334
2335 /* This function is mostly used for comparing image/buffer tags in taglists */
2336 static gint
2337 gst_value_compare_sample (const GValue * value1, const GValue * value2)
2338 {
2339   GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
2340   GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
2341
2342   /* FIXME: should we take into account anything else such as caps? */
2343   return compare_buffer (buf1, buf2);
2344 }
2345
2346 static gchar *
2347 gst_value_serialize_sample (const GValue * value)
2348 {
2349   const GstStructure *info_structure;
2350   GstSegment *segment;
2351   GstBuffer *buffer;
2352   GstCaps *caps;
2353   GstSample *sample;
2354   GValue val = { 0, };
2355   gchar *info_str, *caps_str, *tmp;
2356   gchar *buf_str, *seg_str, *s;
2357
2358   sample = g_value_get_boxed (value);
2359
2360   buffer = gst_sample_get_buffer (sample);
2361   if (buffer) {
2362     g_value_init (&val, GST_TYPE_BUFFER);
2363     g_value_set_boxed (&val, buffer);
2364     buf_str = gst_value_serialize_buffer (&val);
2365     g_value_unset (&val);
2366   } else {
2367     buf_str = g_strdup ("None");
2368   }
2369
2370   caps = gst_sample_get_caps (sample);
2371   if (caps) {
2372     tmp = gst_caps_to_string (caps);
2373     caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2374     g_strdelimit (caps_str, "=", '_');
2375     g_free (tmp);
2376   } else {
2377     caps_str = g_strdup ("None");
2378   }
2379
2380   segment = gst_sample_get_segment (sample);
2381   if (segment) {
2382     g_value_init (&val, GST_TYPE_SEGMENT);
2383     g_value_set_boxed (&val, segment);
2384     tmp = gst_value_serialize_segment_internal (&val, FALSE);
2385     seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2386     g_strdelimit (seg_str, "=", '_');
2387     g_free (tmp);
2388     g_value_unset (&val);
2389   } else {
2390     seg_str = g_strdup ("None");
2391   }
2392
2393   info_structure = gst_sample_get_info (sample);
2394   if (info_structure) {
2395     tmp = gst_structure_to_string (info_structure);
2396     info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2397     g_strdelimit (info_str, "=", '_');
2398     g_free (tmp);
2399   } else {
2400     info_str = g_strdup ("None");
2401   }
2402
2403   s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
2404   g_free (buf_str);
2405   g_free (caps_str);
2406   g_free (seg_str);
2407   g_free (info_str);
2408
2409   return s;
2410 }
2411
2412 static gboolean
2413 gst_value_deserialize_sample (GValue * dest, const gchar * s)
2414 {
2415   GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
2416   GstStructure *info;
2417   GstSample *sample;
2418   GstCaps *caps;
2419   gboolean ret = FALSE;
2420   gchar **fields;
2421   gsize outlen;
2422   gint len;
2423
2424   GST_TRACE ("deserialize '%s'", s);
2425
2426   fields = g_strsplit (s, ":", -1);
2427   len = g_strv_length (fields);
2428   if (len != 4)
2429     goto wrong_length;
2430
2431   g_value_init (&bval, GST_TYPE_BUFFER);
2432   g_value_init (&sval, GST_TYPE_SEGMENT);
2433
2434   if (!gst_value_deserialize_buffer (&bval, fields[0]))
2435     goto fail;
2436
2437   if (strcmp (fields[1], "None") != 0) {
2438     g_strdelimit (fields[1], "_", '=');
2439     g_base64_decode_inplace (fields[1], &outlen);
2440     GST_TRACE ("caps    : %s", fields[1]);
2441     caps = gst_caps_from_string (fields[1]);
2442     if (caps == NULL)
2443       goto fail;
2444   } else {
2445     caps = NULL;
2446   }
2447
2448   if (strcmp (fields[2], "None") != 0) {
2449     g_strdelimit (fields[2], "_", '=');
2450     g_base64_decode_inplace (fields[2], &outlen);
2451     GST_TRACE ("segment : %s", fields[2]);
2452     if (!gst_value_deserialize_segment (&sval, fields[2]))
2453       goto fail;
2454   }
2455
2456   if (strcmp (fields[3], "None") != 0) {
2457     g_strdelimit (fields[3], "_", '=');
2458     g_base64_decode_inplace (fields[3], &outlen);
2459     GST_TRACE ("info    : %s", fields[3]);
2460     info = gst_structure_from_string (fields[3], NULL);
2461     if (info == NULL)
2462       goto fail;
2463   } else {
2464     info = NULL;
2465   }
2466
2467   sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
2468       g_value_get_boxed (&sval), info);
2469
2470   g_value_take_boxed (dest, sample);
2471
2472   if (caps)
2473     gst_caps_unref (caps);
2474
2475   ret = TRUE;
2476
2477 fail:
2478
2479   g_value_unset (&bval);
2480   g_value_unset (&sval);
2481
2482 wrong_length:
2483
2484   g_strfreev (fields);
2485
2486   return ret;
2487 }
2488
2489 /***********
2490  * boolean *
2491  ***********/
2492
2493 static gint
2494 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
2495 {
2496   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
2497     return GST_VALUE_EQUAL;
2498   return GST_VALUE_UNORDERED;
2499 }
2500
2501 static gchar *
2502 gst_value_serialize_boolean (const GValue * value)
2503 {
2504   if (value->data[0].v_int) {
2505     return g_strdup ("true");
2506   }
2507   return g_strdup ("false");
2508 }
2509
2510 static gboolean
2511 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
2512 {
2513   gboolean ret = FALSE;
2514
2515   if (g_ascii_strcasecmp (s, "true") == 0 ||
2516       g_ascii_strcasecmp (s, "yes") == 0 ||
2517       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
2518     g_value_set_boolean (dest, TRUE);
2519     ret = TRUE;
2520   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
2521       g_ascii_strcasecmp (s, "no") == 0 ||
2522       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
2523     g_value_set_boolean (dest, FALSE);
2524     ret = TRUE;
2525   }
2526
2527   return ret;
2528 }
2529
2530 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
2531 static gint                                                             \
2532 gst_value_compare_ ## _type                                             \
2533 (const GValue * value1, const GValue * value2)                          \
2534 {                                                                       \
2535   g ## _type val1 = g_value_get_ ## _type (value1);                     \
2536   g ## _type val2 = g_value_get_ ## _type (value2);                     \
2537   if (val1 > val2)                                                      \
2538     return GST_VALUE_GREATER_THAN;                                      \
2539   if (val1 < val2)                                                      \
2540     return GST_VALUE_LESS_THAN;                                         \
2541   return GST_VALUE_EQUAL;                                               \
2542 }                                                                       \
2543                                                                         \
2544 static gchar *                                                          \
2545 gst_value_serialize_ ## _type (const GValue * value)                    \
2546 {                                                                       \
2547   GValue val = { 0, };                                                  \
2548   g_value_init (&val, G_TYPE_STRING);                                   \
2549   if (!g_value_transform (value, &val))                                 \
2550     g_assert_not_reached ();                                            \
2551   /* NO_COPY_MADNESS!!! */                                              \
2552   return (char *) g_value_get_string (&val);                            \
2553 }
2554
2555 /* deserialize the given s into to as a gint64.
2556  * check if the result is actually storeable in the given size number of
2557  * bytes.
2558  */
2559 static gboolean
2560 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
2561     gint64 min, gint64 max, gint size)
2562 {
2563   gboolean ret = FALSE;
2564   gchar *end;
2565   guint64 mask = ~0;
2566
2567   errno = 0;
2568   *to = g_ascii_strtoull (s, &end, 0);
2569   /* a range error is a definitive no-no */
2570   if (errno == ERANGE) {
2571     return FALSE;
2572   }
2573
2574   if (*end == 0) {
2575     ret = TRUE;
2576   } else {
2577     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
2578       *to = G_LITTLE_ENDIAN;
2579       ret = TRUE;
2580     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
2581       *to = G_BIG_ENDIAN;
2582       ret = TRUE;
2583     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
2584       *to = G_BYTE_ORDER;
2585       ret = TRUE;
2586     } else if (g_ascii_strcasecmp (s, "min") == 0) {
2587       *to = min;
2588       ret = TRUE;
2589     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2590       *to = max;
2591       ret = TRUE;
2592     }
2593   }
2594   if (ret) {
2595     /* by definition, a gint64 fits into a gint64; so ignore those */
2596     if (size != sizeof (mask)) {
2597       if (*to >= 0) {
2598         /* for positive numbers, we create a mask of 1's outside of the range
2599          * and 0's inside the range.  An and will thus keep only 1 bits
2600          * outside of the range */
2601         mask <<= (size * 8);
2602         if ((mask & *to) != 0) {
2603           ret = FALSE;
2604         }
2605       } else {
2606         /* for negative numbers, we do a 2's complement version */
2607         mask <<= ((size * 8) - 1);
2608         if ((mask & *to) != mask) {
2609           ret = FALSE;
2610         }
2611       }
2612     }
2613   }
2614   return ret;
2615 }
2616
2617 #define CREATE_SERIALIZATION(_type,_macro)                              \
2618 CREATE_SERIALIZATION_START(_type,_macro)                                \
2619                                                                         \
2620 static gboolean                                                         \
2621 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2622 {                                                                       \
2623   gint64 x;                                                             \
2624                                                                         \
2625   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
2626       G_MAX ## _macro, sizeof (g ## _type))) {                          \
2627     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
2628     return TRUE;                                                        \
2629   } else {                                                              \
2630     return FALSE;                                                       \
2631   }                                                                     \
2632 }
2633
2634 #define CREATE_USERIALIZATION(_type,_macro)                             \
2635 CREATE_SERIALIZATION_START(_type,_macro)                                \
2636                                                                         \
2637 static gboolean                                                         \
2638 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2639 {                                                                       \
2640   gint64 x;                                                             \
2641   gchar *end;                                                           \
2642   gboolean ret = FALSE;                                                 \
2643                                                                         \
2644   errno = 0;                                                            \
2645   x = g_ascii_strtoull (s, &end, 0);                                    \
2646   /* a range error is a definitive no-no */                             \
2647   if (errno == ERANGE) {                                                \
2648     return FALSE;                                                       \
2649   }                                                                     \
2650   /* the cast ensures the range check later on makes sense */           \
2651   x = (g ## _type) x;                                                   \
2652   if (*end == 0) {                                                      \
2653     ret = TRUE;                                                         \
2654   } else {                                                              \
2655     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
2656       x = G_LITTLE_ENDIAN;                                              \
2657       ret = TRUE;                                                       \
2658     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
2659       x = G_BIG_ENDIAN;                                                 \
2660       ret = TRUE;                                                       \
2661     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
2662       x = G_BYTE_ORDER;                                                 \
2663       ret = TRUE;                                                       \
2664     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
2665       x = 0;                                                            \
2666       ret = TRUE;                                                       \
2667     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
2668       x = G_MAX ## _macro;                                              \
2669       ret = TRUE;                                                       \
2670     }                                                                   \
2671   }                                                                     \
2672   if (ret) {                                                            \
2673     if (x > G_MAX ## _macro) {                                          \
2674       ret = FALSE;                                                      \
2675     } else {                                                            \
2676       g_value_set_ ## _type (dest, x);                                  \
2677     }                                                                   \
2678   }                                                                     \
2679   return ret;                                                           \
2680 }
2681
2682 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
2683 G_STMT_START {                                                          \
2684   static const GstValueTable gst_value = {                              \
2685     _gtype,                                                             \
2686     gst_value_compare_ ## _type,                                        \
2687     gst_value_serialize_ ## _type,                                      \
2688     gst_value_deserialize_ ## _type,                                    \
2689   };                                                                    \
2690                                                                         \
2691   gst_value_register (&gst_value);                                      \
2692 } G_STMT_END
2693
2694 CREATE_SERIALIZATION (int, INT);
2695 CREATE_SERIALIZATION (int64, INT64);
2696 CREATE_SERIALIZATION (long, LONG);
2697
2698 CREATE_USERIALIZATION (uint, UINT);
2699 CREATE_USERIALIZATION (uint64, UINT64);
2700 CREATE_USERIALIZATION (ulong, ULONG);
2701
2702 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2703 #ifndef G_MAXUCHAR
2704 #define G_MAXUCHAR 255
2705 #endif
2706 CREATE_USERIALIZATION (uchar, UCHAR);
2707
2708 /**********
2709  * double *
2710  **********/
2711 static gint
2712 gst_value_compare_double (const GValue * value1, const GValue * value2)
2713 {
2714   if (value1->data[0].v_double > value2->data[0].v_double)
2715     return GST_VALUE_GREATER_THAN;
2716   if (value1->data[0].v_double < value2->data[0].v_double)
2717     return GST_VALUE_LESS_THAN;
2718   if (value1->data[0].v_double == value2->data[0].v_double)
2719     return GST_VALUE_EQUAL;
2720   return GST_VALUE_UNORDERED;
2721 }
2722
2723 static gchar *
2724 gst_value_serialize_double (const GValue * value)
2725 {
2726   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2727
2728   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2729   return g_strdup (d);
2730 }
2731
2732 static gboolean
2733 gst_value_deserialize_double (GValue * dest, const gchar * s)
2734 {
2735   gdouble x;
2736   gboolean ret = FALSE;
2737   gchar *end;
2738
2739   x = g_ascii_strtod (s, &end);
2740   if (*end == 0) {
2741     ret = TRUE;
2742   } else {
2743     if (g_ascii_strcasecmp (s, "min") == 0) {
2744       x = -G_MAXDOUBLE;
2745       ret = TRUE;
2746     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2747       x = G_MAXDOUBLE;
2748       ret = TRUE;
2749     }
2750   }
2751   if (ret) {
2752     g_value_set_double (dest, x);
2753   }
2754   return ret;
2755 }
2756
2757 /*********
2758  * float *
2759  *********/
2760
2761 static gint
2762 gst_value_compare_float (const GValue * value1, const GValue * value2)
2763 {
2764   if (value1->data[0].v_float > value2->data[0].v_float)
2765     return GST_VALUE_GREATER_THAN;
2766   if (value1->data[0].v_float < value2->data[0].v_float)
2767     return GST_VALUE_LESS_THAN;
2768   if (value1->data[0].v_float == value2->data[0].v_float)
2769     return GST_VALUE_EQUAL;
2770   return GST_VALUE_UNORDERED;
2771 }
2772
2773 static gchar *
2774 gst_value_serialize_float (const GValue * value)
2775 {
2776   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2777
2778   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2779   return g_strdup (d);
2780 }
2781
2782 static gboolean
2783 gst_value_deserialize_float (GValue * dest, const gchar * s)
2784 {
2785   gdouble x;
2786   gboolean ret = FALSE;
2787   gchar *end;
2788
2789   x = g_ascii_strtod (s, &end);
2790   if (*end == 0) {
2791     ret = TRUE;
2792   } else {
2793     if (g_ascii_strcasecmp (s, "min") == 0) {
2794       x = -G_MAXFLOAT;
2795       ret = TRUE;
2796     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2797       x = G_MAXFLOAT;
2798       ret = TRUE;
2799     }
2800   }
2801   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2802     ret = FALSE;
2803   if (ret) {
2804     g_value_set_float (dest, (float) x);
2805   }
2806   return ret;
2807 }
2808
2809 /**********
2810  * string *
2811  **********/
2812
2813 static gint
2814 gst_value_compare_string (const GValue * value1, const GValue * value2)
2815 {
2816   if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2817     /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2818     if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2819       return GST_VALUE_UNORDERED;
2820   } else {
2821     gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2822
2823     if (x < 0)
2824       return GST_VALUE_LESS_THAN;
2825     if (x > 0)
2826       return GST_VALUE_GREATER_THAN;
2827   }
2828
2829   return GST_VALUE_EQUAL;
2830 }
2831
2832 static gint
2833 gst_string_measure_wrapping (const gchar * s)
2834 {
2835   gint len;
2836   gboolean wrap = FALSE;
2837
2838   if (G_UNLIKELY (s == NULL))
2839     return -1;
2840
2841   /* Special case: the actual string NULL needs wrapping */
2842   if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2843     return 4;
2844
2845   len = 0;
2846   while (*s) {
2847     if (GST_ASCII_IS_STRING (*s)) {
2848       len++;
2849     } else if (*s < 0x20 || *s >= 0x7f) {
2850       wrap = TRUE;
2851       len += 4;
2852     } else {
2853       wrap = TRUE;
2854       len += 2;
2855     }
2856     s++;
2857   }
2858
2859   /* Wrap the string if we found something that needs
2860    * wrapping, or the empty string (len == 0) */
2861   return (wrap || len == 0) ? len : -1;
2862 }
2863
2864 static gchar *
2865 gst_string_wrap_inner (const gchar * s, gint len)
2866 {
2867   gchar *d, *e;
2868
2869   e = d = g_malloc (len + 3);
2870
2871   *e++ = '\"';
2872   while (*s) {
2873     if (GST_ASCII_IS_STRING (*s)) {
2874       *e++ = *s++;
2875     } else if (*s < 0x20 || *s >= 0x7f) {
2876       *e++ = '\\';
2877       *e++ = '0' + ((*(guchar *) s) >> 6);
2878       *e++ = '0' + (((*s) >> 3) & 0x7);
2879       *e++ = '0' + ((*s++) & 0x7);
2880     } else {
2881       *e++ = '\\';
2882       *e++ = *s++;
2883     }
2884   }
2885   *e++ = '\"';
2886   *e = 0;
2887
2888   g_assert (e - d <= len + 3);
2889   return d;
2890 }
2891
2892 /* Do string wrapping/escaping */
2893 static gchar *
2894 gst_string_wrap (const gchar * s)
2895 {
2896   gint len = gst_string_measure_wrapping (s);
2897
2898   if (G_LIKELY (len < 0))
2899     return g_strdup (s);
2900
2901   return gst_string_wrap_inner (s, len);
2902 }
2903
2904 /* Same as above, but take ownership of the string */
2905 static gchar *
2906 gst_string_take_and_wrap (gchar * s)
2907 {
2908   gchar *out;
2909   gint len = gst_string_measure_wrapping (s);
2910
2911   if (G_LIKELY (len < 0))
2912     return s;
2913
2914   out = gst_string_wrap_inner (s, len);
2915   g_free (s);
2916
2917   return out;
2918 }
2919
2920 /*
2921  * This function takes a string delimited with double quotes (")
2922  * and unescapes any \xxx octal numbers.
2923  *
2924  * If sequences of \y are found where y is not in the range of
2925  * 0->3, y is copied unescaped.
2926  *
2927  * If \xyy is found where x is an octal number but y is not, an
2928  * error is encountered and %NULL is returned.
2929  *
2930  * the input string must be \0 terminated.
2931  */
2932 static gchar *
2933 gst_string_unwrap (const gchar * s)
2934 {
2935   gchar *ret;
2936   gchar *read, *write;
2937
2938   /* NULL string returns NULL */
2939   if (s == NULL)
2940     return NULL;
2941
2942   /* strings not starting with " are invalid */
2943   if (*s != '"')
2944     return NULL;
2945
2946   /* make copy of original string to hold the result. This
2947    * string will always be smaller than the original */
2948   ret = g_strdup (s);
2949   read = ret;
2950   write = ret;
2951
2952   /* need to move to the next position as we parsed the " */
2953   read++;
2954
2955   while (*read) {
2956     if (GST_ASCII_IS_STRING (*read)) {
2957       /* normal chars are just copied */
2958       *write++ = *read++;
2959     } else if (*read == '"') {
2960       /* quote marks end of string */
2961       break;
2962     } else if (*read == '\\') {
2963       /* got an escape char, move to next position to read a tripplet
2964        * of octal numbers */
2965       read++;
2966       /* is the next char a possible first octal number? */
2967       if (*read >= '0' && *read <= '3') {
2968         /* parse other 2 numbers, if one of them is not in the range of
2969          * an octal number, we error. We also catch the case where a zero
2970          * byte is found here. */
2971         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2972           goto beach;
2973
2974         /* now convert the octal number to a byte again. */
2975         *write++ = ((read[0] - '0') << 6) +
2976             ((read[1] - '0') << 3) + (read[2] - '0');
2977
2978         read += 3;
2979       } else {
2980         /* if we run into a \0 here, we definitely won't get a quote later */
2981         if (*read == 0)
2982           goto beach;
2983
2984         /* else copy \X sequence */
2985         *write++ = *read++;
2986       }
2987     } else {
2988       /* weird character, error */
2989       goto beach;
2990     }
2991   }
2992   /* if the string is not ending in " and zero terminated, we error */
2993   if (*read != '"' || read[1] != '\0')
2994     goto beach;
2995
2996   /* null terminate result string and return */
2997   *write = '\0';
2998   return ret;
2999
3000 beach:
3001   g_free (ret);
3002   return NULL;
3003 }
3004
3005 static gchar *
3006 gst_value_serialize_string (const GValue * value)
3007 {
3008   return gst_string_wrap (value->data[0].v_pointer);
3009 }
3010
3011 static gboolean
3012 gst_value_deserialize_string (GValue * dest, const gchar * s)
3013 {
3014   if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
3015     g_value_set_string (dest, NULL);
3016     return TRUE;
3017   } else if (G_LIKELY (*s != '"')) {
3018     if (!g_utf8_validate (s, -1, NULL))
3019       return FALSE;
3020     g_value_set_string (dest, s);
3021     return TRUE;
3022   } else {
3023     gchar *str = gst_string_unwrap (s);
3024     if (G_UNLIKELY (!str))
3025       return FALSE;
3026     g_value_take_string (dest, str);
3027   }
3028
3029   return TRUE;
3030 }
3031
3032 /********
3033  * enum *
3034  ********/
3035
3036 static gint
3037 gst_value_compare_enum (const GValue * value1, const GValue * value2)
3038 {
3039   GEnumValue *en1, *en2;
3040   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
3041   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
3042
3043   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
3044   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
3045   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
3046   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
3047   g_type_class_unref (klass1);
3048   g_type_class_unref (klass2);
3049   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
3050   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
3051   if (en1->value < en2->value)
3052     return GST_VALUE_LESS_THAN;
3053   if (en1->value > en2->value)
3054     return GST_VALUE_GREATER_THAN;
3055
3056   return GST_VALUE_EQUAL;
3057 }
3058
3059 static gchar *
3060 gst_value_serialize_enum (const GValue * value)
3061 {
3062   GEnumValue *en;
3063   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
3064
3065   g_return_val_if_fail (klass, NULL);
3066   en = g_enum_get_value (klass, g_value_get_enum (value));
3067   g_type_class_unref (klass);
3068
3069   /* might be one of the custom formats registered later */
3070   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
3071     const GstFormatDefinition *format_def;
3072
3073     format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
3074     g_return_val_if_fail (format_def != NULL, NULL);
3075     return g_strdup (format_def->description);
3076   }
3077
3078   g_return_val_if_fail (en, NULL);
3079   return g_strdup (en->value_name);
3080 }
3081
3082 static gint
3083 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
3084     const gchar * s)
3085 {
3086   const GstFormatDefinition *format_def =
3087       g_value_get_pointer (format_def_value);
3088
3089   if (g_ascii_strcasecmp (s, format_def->nick) == 0)
3090     return 0;
3091
3092   return g_ascii_strcasecmp (s, format_def->description);
3093 }
3094
3095 static gboolean
3096 gst_value_deserialize_enum (GValue * dest, const gchar * s)
3097 {
3098   GEnumValue *en;
3099   gchar *endptr = NULL;
3100   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3101
3102   g_return_val_if_fail (klass, FALSE);
3103   if (!(en = g_enum_get_value_by_name (klass, s))) {
3104     if (!(en = g_enum_get_value_by_nick (klass, s))) {
3105       gint i = strtol (s, &endptr, 0);
3106
3107       if (endptr && *endptr == '\0') {
3108         en = g_enum_get_value (klass, i);
3109       }
3110     }
3111   }
3112   g_type_class_unref (klass);
3113
3114   /* might be one of the custom formats registered later */
3115   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
3116     GValue res = { 0, };
3117     const GstFormatDefinition *format_def;
3118     GstIterator *iter;
3119     gboolean found;
3120
3121     iter = gst_format_iterate_definitions ();
3122
3123     found = gst_iterator_find_custom (iter,
3124         (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
3125
3126     if (found) {
3127       format_def = g_value_get_pointer (&res);
3128       g_return_val_if_fail (format_def != NULL, FALSE);
3129       g_value_set_enum (dest, (gint) format_def->value);
3130       g_value_unset (&res);
3131     }
3132     gst_iterator_free (iter);
3133     return found;
3134   }
3135
3136   /* enum name/nick not found */
3137   if (en == NULL)
3138     return FALSE;
3139
3140   g_value_set_enum (dest, en->value);
3141   return TRUE;
3142 }
3143
3144 /********
3145  * flags *
3146  ********/
3147
3148 /* we just compare the value here */
3149 static gint
3150 gst_value_compare_flags (const GValue * value1, const GValue * value2)
3151 {
3152   guint fl1, fl2;
3153   GFlagsClass *klass1 =
3154       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
3155   GFlagsClass *klass2 =
3156       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
3157
3158   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
3159   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
3160   fl1 = g_value_get_flags (value1);
3161   fl2 = g_value_get_flags (value2);
3162   g_type_class_unref (klass1);
3163   g_type_class_unref (klass2);
3164   if (fl1 < fl2)
3165     return GST_VALUE_LESS_THAN;
3166   if (fl1 > fl2)
3167     return GST_VALUE_GREATER_THAN;
3168
3169   return GST_VALUE_EQUAL;
3170 }
3171
3172 /* the different flags are serialized separated with a + */
3173 static gchar *
3174 gst_value_serialize_flags (const GValue * value)
3175 {
3176   guint flags;
3177   GFlagsValue *fl;
3178   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
3179   gchar *result, *tmp;
3180   gboolean first = TRUE;
3181
3182   g_return_val_if_fail (klass, NULL);
3183
3184   flags = g_value_get_flags (value);
3185
3186   /* if no flags are set, try to serialize to the _NONE string */
3187   if (!flags) {
3188     fl = g_flags_get_first_value (klass, flags);
3189     if (fl)
3190       return g_strdup (fl->value_name);
3191     else
3192       return g_strdup ("0");
3193   }
3194
3195   /* some flags are set, so serialize one by one */
3196   result = g_strdup ("");
3197   while (flags) {
3198     fl = g_flags_get_first_value (klass, flags);
3199     if (fl != NULL) {
3200       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
3201       g_free (result);
3202       result = tmp;
3203       first = FALSE;
3204
3205       /* clear flag */
3206       flags &= ~fl->value;
3207     }
3208   }
3209   g_type_class_unref (klass);
3210
3211   return result;
3212 }
3213
3214 static gboolean
3215 gst_value_deserialize_flags (GValue * dest, const gchar * s)
3216 {
3217   GFlagsValue *fl;
3218   gchar *endptr = NULL;
3219   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3220   gchar **split;
3221   guint flags;
3222   gint i;
3223
3224   g_return_val_if_fail (klass, FALSE);
3225
3226   /* split into parts delimited with + */
3227   split = g_strsplit (s, "+", 0);
3228
3229   flags = 0;
3230   i = 0;
3231   /* loop over each part */
3232   while (split[i]) {
3233     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
3234       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
3235         gint val = strtol (split[i], &endptr, 0);
3236
3237         /* just or numeric value */
3238         if (endptr && *endptr == '\0') {
3239           flags |= val;
3240         }
3241       }
3242     }
3243     if (fl) {
3244       flags |= fl->value;
3245     }
3246     i++;
3247   }
3248   g_strfreev (split);
3249   g_type_class_unref (klass);
3250   g_value_set_flags (dest, flags);
3251
3252   return TRUE;
3253 }
3254
3255 /****************
3256  * subset *
3257  ****************/
3258
3259 static gboolean
3260 gst_value_is_subset_int_range_int_range (const GValue * value1,
3261     const GValue * value2)
3262 {
3263   gint gcd;
3264
3265   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
3266   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
3267
3268   if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
3269       INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
3270     return FALSE;
3271   if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
3272       INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
3273     return FALSE;
3274
3275   if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
3276     if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
3277         INT_RANGE_STEP (value1))
3278       return FALSE;
3279     return TRUE;
3280   }
3281
3282   gcd =
3283       gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
3284       INT_RANGE_STEP (value2));
3285   if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
3286     return FALSE;
3287
3288   return TRUE;
3289 }
3290
3291 static gboolean
3292 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
3293     const GValue * value2)
3294 {
3295   gint64 gcd;
3296
3297   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
3298   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
3299
3300   if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
3301     return FALSE;
3302   if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
3303     return FALSE;
3304
3305   if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
3306     if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
3307         INT64_RANGE_STEP (value1))
3308       return FALSE;
3309     return TRUE;
3310   }
3311
3312   gcd =
3313       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
3314       INT64_RANGE_STEP (value2));
3315   if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
3316     return FALSE;
3317
3318   return TRUE;
3319 }
3320
3321 /**
3322  * gst_value_is_subset:
3323  * @value1: a #GValue
3324  * @value2: a #GValue
3325  *
3326  * Check that @value1 is a subset of @value2.
3327  *
3328  * Return: %TRUE is @value1 is a subset of @value2
3329  */
3330 gboolean
3331 gst_value_is_subset (const GValue * value1, const GValue * value2)
3332 {
3333   /* special case for int/int64 ranges, since we cannot compute
3334      the difference for those when they have different steps,
3335      and it's actually a lot simpler to compute whether a range
3336      is a subset of another. */
3337   if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
3338     return gst_value_is_subset_int_range_int_range (value1, value2);
3339   } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
3340       && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
3341     return gst_value_is_subset_int64_range_int64_range (value1, value2);
3342   }
3343
3344   /*
3345    * 1 - [1,2] = empty
3346    * -> !subset
3347    *
3348    * [1,2] - 1 = 2
3349    *  -> 1 - [1,2] = empty
3350    *  -> subset
3351    *
3352    * [1,3] - [1,2] = 3
3353    * -> [1,2] - [1,3] = empty
3354    * -> subset
3355    *
3356    * {1,2} - {1,3} = 2
3357    * -> {1,3} - {1,2} = 3
3358    * -> !subset
3359    *
3360    *  First caps subtraction needs to return a non-empty set, second
3361    *  subtractions needs to give en empty set.
3362    *  Both substractions are switched below, as it's faster that way.
3363    */
3364   if (!gst_value_subtract (NULL, value1, value2)) {
3365     if (gst_value_subtract (NULL, value2, value1)) {
3366       return TRUE;
3367     }
3368   }
3369   return FALSE;
3370 }
3371
3372 /*********
3373  * union *
3374  *********/
3375
3376 static gboolean
3377 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
3378     const GValue * src2)
3379 {
3380   gint v = src1->data[0].v_int;
3381
3382   /* check if it's already in the range */
3383   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
3384       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
3385       v % INT_RANGE_STEP (src2) == 0) {
3386     if (dest)
3387       gst_value_init_and_copy (dest, src2);
3388     return TRUE;
3389   }
3390
3391   /* check if it extends the range */
3392   if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
3393     if (dest) {
3394       guint64 new_min =
3395           (guint) ((INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2));
3396       guint64 new_max = (guint) (INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3397
3398       gst_value_init_and_copy (dest, src2);
3399       dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3400     }
3401     return TRUE;
3402   }
3403   if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
3404     if (dest) {
3405       guint64 new_min = (guint) (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3406       guint64 new_max =
3407           (guint) ((INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2));
3408
3409       gst_value_init_and_copy (dest, src2);
3410       dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3411     }
3412     return TRUE;
3413   }
3414
3415   return FALSE;
3416 }
3417
3418 static gboolean
3419 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
3420     const GValue * src2)
3421 {
3422   /* We can union in several special cases:
3423      1 - one is a subset of another
3424      2 - same step and not disjoint
3425      3 - different step, at least one with one value which matches a 'next' or 'previous'
3426      - anything else ?
3427    */
3428
3429   /* 1 - subset */
3430   if (gst_value_is_subset_int_range_int_range (src1, src2)) {
3431     if (dest)
3432       gst_value_init_and_copy (dest, src2);
3433     return TRUE;
3434   }
3435   if (gst_value_is_subset_int_range_int_range (src2, src1)) {
3436     if (dest)
3437       gst_value_init_and_copy (dest, src1);
3438     return TRUE;
3439   }
3440
3441   /* 2 - same step and not disjoint */
3442   if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
3443     if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
3444             INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
3445         (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
3446             INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
3447       if (dest) {
3448         gint step = INT_RANGE_STEP (src1);
3449         gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
3450         gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
3451         g_value_init (dest, GST_TYPE_INT_RANGE);
3452         gst_value_set_int_range_step (dest, min, max, step);
3453       }
3454       return TRUE;
3455     }
3456   }
3457
3458   /* 3 - single value matches next or previous */
3459   if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
3460     gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
3461     gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
3462     if (n1 == 1 || n2 == 1) {
3463       const GValue *range_value = NULL;
3464       gint scalar = 0;
3465       if (n1 == 1) {
3466         range_value = src2;
3467         scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
3468       } else if (n2 == 1) {
3469         range_value = src1;
3470         scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
3471       }
3472
3473       if (scalar ==
3474           (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
3475         if (dest) {
3476           guint64 new_min = (guint)
3477               ((INT_RANGE_MIN (range_value) -
3478                   1) * INT_RANGE_STEP (range_value));
3479           guint64 new_max = (guint)
3480               (INT_RANGE_MAX (range_value) * INT_RANGE_STEP (range_value));
3481
3482           gst_value_init_and_copy (dest, range_value);
3483           dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3484         }
3485         return TRUE;
3486       } else if (scalar ==
3487           (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
3488         if (dest) {
3489           guint64 new_min = (guint)
3490               (INT_RANGE_MIN (range_value) * INT_RANGE_STEP (range_value));
3491           guint64 new_max = (guint)
3492               ((INT_RANGE_MAX (range_value) +
3493                   1) * INT_RANGE_STEP (range_value));
3494           gst_value_init_and_copy (dest, range_value);
3495           dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3496         }
3497         return TRUE;
3498       }
3499     }
3500   }
3501
3502   /* If we get there, we did not find a way to make a union that can be
3503      represented with our simplistic model. */
3504   return FALSE;
3505 }
3506
3507 /****************
3508  * intersection *
3509  ****************/
3510
3511 static gboolean
3512 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
3513     const GValue * src2)
3514 {
3515   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
3516       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
3517       src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
3518     if (dest)
3519       gst_value_init_and_copy (dest, src1);
3520     return TRUE;
3521   }
3522
3523   return FALSE;
3524 }
3525
3526 static gboolean
3527 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
3528     const GValue * src2)
3529 {
3530   gint min;
3531   gint max;
3532   gint step;
3533
3534   step =
3535       INT_RANGE_STEP (src1) /
3536       gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
3537       INT_RANGE_STEP (src2));
3538   if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
3539     return FALSE;
3540   step *= INT_RANGE_STEP (src2);
3541
3542   min =
3543       MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
3544       INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3545   min = (min + step - 1) / step * step;
3546   max =
3547       MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
3548       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3549   max = max / step * step;
3550
3551   if (min < max) {
3552     if (dest) {
3553       g_value_init (dest, GST_TYPE_INT_RANGE);
3554       gst_value_set_int_range_step (dest, min, max, step);
3555     }
3556     return TRUE;
3557   }
3558   if (min == max) {
3559     if (dest) {
3560       g_value_init (dest, G_TYPE_INT);
3561       g_value_set_int (dest, min);
3562     }
3563     return TRUE;
3564   }
3565
3566   return FALSE;
3567 }
3568
3569 #define INT64_RANGE_MIN_VAL(v) (INT64_RANGE_MIN (v) * INT64_RANGE_STEP (v))
3570 #define INT64_RANGE_MAX_VAL(v) (INT64_RANGE_MAX (v) * INT64_RANGE_STEP (v))
3571
3572 static gboolean
3573 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
3574     const GValue * src2)
3575 {
3576   if (INT64_RANGE_MIN_VAL (src2) <= src1->data[0].v_int64 &&
3577       INT64_RANGE_MAX_VAL (src2) >= src1->data[0].v_int64 &&
3578       src1->data[0].v_int64 % INT64_RANGE_STEP (src2) == 0) {
3579     if (dest)
3580       gst_value_init_and_copy (dest, src1);
3581     return TRUE;
3582   }
3583
3584   return FALSE;
3585 }
3586
3587 static gboolean
3588 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
3589     const GValue * src2)
3590 {
3591   gint64 min;
3592   gint64 max;
3593   gint64 step;
3594
3595   step =
3596       INT64_RANGE_STEP (src1) /
3597       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
3598       INT64_RANGE_STEP (src2));
3599   if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
3600     return FALSE;
3601   step *= INT64_RANGE_STEP (src2);
3602
3603   min =
3604       MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
3605       INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
3606   min = (min + step - 1) / step * step;
3607   max =
3608       MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
3609       INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
3610   max = max / step * step;
3611
3612   if (min < max) {
3613     if (dest) {
3614       g_value_init (dest, GST_TYPE_INT64_RANGE);
3615       gst_value_set_int64_range_step (dest, min, max, step);
3616     }
3617     return TRUE;
3618   }
3619   if (min == max) {
3620     if (dest) {
3621       g_value_init (dest, G_TYPE_INT64);
3622       g_value_set_int64 (dest, min);
3623     }
3624     return TRUE;
3625   }
3626
3627   return FALSE;
3628 }
3629
3630 static gboolean
3631 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
3632     const GValue * src2)
3633 {
3634   if (src2->data[0].v_double <= src1->data[0].v_double &&
3635       src2->data[1].v_double >= src1->data[0].v_double) {
3636     if (dest)
3637       gst_value_init_and_copy (dest, src1);
3638     return TRUE;
3639   }
3640
3641   return FALSE;
3642 }
3643
3644 static gboolean
3645 gst_value_intersect_double_range_double_range (GValue * dest,
3646     const GValue * src1, const GValue * src2)
3647 {
3648   gdouble min;
3649   gdouble max;
3650
3651   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
3652   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
3653
3654   if (min < max) {
3655     if (dest) {
3656       g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
3657       gst_value_set_double_range (dest, min, max);
3658     }
3659     return TRUE;
3660   }
3661   if (min == max) {
3662     if (dest) {
3663       g_value_init (dest, G_TYPE_DOUBLE);
3664       g_value_set_int (dest, (int) min);
3665     }
3666     return TRUE;
3667   }
3668
3669   return FALSE;
3670 }
3671
3672 static gboolean
3673 gst_value_intersect_list (GValue * dest, const GValue * value1,
3674     const GValue * value2)
3675 {
3676   guint i, size;
3677   GValue intersection = { 0, };
3678   gboolean ret = FALSE;
3679
3680   size = VALUE_LIST_SIZE (value1);
3681   for (i = 0; i < size; i++) {
3682     const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
3683
3684     /* quicker version when we don't need the resulting set */
3685     if (!dest) {
3686       if (gst_value_intersect (NULL, cur, value2)) {
3687         ret = TRUE;
3688         break;
3689       }
3690       continue;
3691     }
3692
3693     if (gst_value_intersect (&intersection, cur, value2)) {
3694       /* append value */
3695       if (!ret) {
3696         gst_value_move (dest, &intersection);
3697         ret = TRUE;
3698       } else if (GST_VALUE_HOLDS_LIST (dest)) {
3699         _gst_value_list_append_and_take_value (dest, &intersection);
3700       } else {
3701         GValue temp;
3702
3703         gst_value_move (&temp, dest);
3704         gst_value_list_merge (dest, &temp, &intersection);
3705         g_value_unset (&temp);
3706         g_value_unset (&intersection);
3707       }
3708     }
3709   }
3710
3711   return ret;
3712 }
3713
3714 static gboolean
3715 gst_value_intersect_array (GValue * dest, const GValue * src1,
3716     const GValue * src2)
3717 {
3718   guint size;
3719   guint n;
3720   GValue val = { 0 };
3721
3722   /* only works on similar-sized arrays */
3723   size = gst_value_array_get_size (src1);
3724   if (size != gst_value_array_get_size (src2))
3725     return FALSE;
3726
3727   /* quicker value when we don't need the resulting set */
3728   if (!dest) {
3729     for (n = 0; n < size; n++) {
3730       if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
3731               gst_value_array_get_value (src2, n))) {
3732         return FALSE;
3733       }
3734     }
3735     return TRUE;
3736   }
3737
3738   g_value_init (dest, GST_TYPE_ARRAY);
3739
3740   for (n = 0; n < size; n++) {
3741     if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
3742             gst_value_array_get_value (src2, n))) {
3743       g_value_unset (dest);
3744       return FALSE;
3745     }
3746     _gst_value_array_append_and_take_value (dest, &val);
3747   }
3748
3749   return TRUE;
3750 }
3751
3752 static gboolean
3753 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
3754     const GValue * src2)
3755 {
3756   gint res1, res2;
3757   GValue *vals;
3758   GstValueCompareFunc compare;
3759
3760   vals = src2->data[0].v_pointer;
3761
3762   if (vals == NULL)
3763     return FALSE;
3764
3765   if ((compare = gst_value_get_compare_func (src1))) {
3766     res1 = gst_value_compare_with_func (&vals[0], src1, compare);
3767     res2 = gst_value_compare_with_func (&vals[1], src1, compare);
3768
3769     if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
3770         (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
3771       if (dest)
3772         gst_value_init_and_copy (dest, src1);
3773       return TRUE;
3774     }
3775   }
3776
3777   return FALSE;
3778 }
3779
3780 static gboolean
3781 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
3782     const GValue * src1, const GValue * src2)
3783 {
3784   GValue *min;
3785   GValue *max;
3786   gint res;
3787   GValue *vals1, *vals2;
3788   GstValueCompareFunc compare;
3789
3790   vals1 = src1->data[0].v_pointer;
3791   vals2 = src2->data[0].v_pointer;
3792   g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
3793
3794   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
3795     /* min = MAX (src1.start, src2.start) */
3796     res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
3797     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3798     if (res == GST_VALUE_LESS_THAN)
3799       min = &vals2[0];          /* Take the max of the 2 */
3800     else
3801       min = &vals1[0];
3802
3803     /* max = MIN (src1.end, src2.end) */
3804     res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
3805     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3806     if (res == GST_VALUE_GREATER_THAN)
3807       max = &vals2[1];          /* Take the min of the 2 */
3808     else
3809       max = &vals1[1];
3810
3811     res = gst_value_compare_with_func (min, max, compare);
3812     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3813     if (res == GST_VALUE_LESS_THAN) {
3814       if (dest) {
3815         g_value_init (dest, GST_TYPE_FRACTION_RANGE);
3816         vals1 = dest->data[0].v_pointer;
3817         g_value_copy (min, &vals1[0]);
3818         g_value_copy (max, &vals1[1]);
3819       }
3820       return TRUE;
3821     }
3822     if (res == GST_VALUE_EQUAL) {
3823       if (dest)
3824         gst_value_init_and_copy (dest, min);
3825       return TRUE;
3826     }
3827   }
3828
3829   return FALSE;
3830 }
3831
3832 /***************
3833  * subtraction *
3834  ***************/
3835
3836 static gboolean
3837 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
3838     const GValue * subtrahend)
3839 {
3840   gint min = gst_value_get_int_range_min (subtrahend);
3841   gint max = gst_value_get_int_range_max (subtrahend);
3842   gint step = gst_value_get_int_range_step (subtrahend);
3843   gint val = g_value_get_int (minuend);
3844
3845   if (step == 0)
3846     return FALSE;
3847
3848   /* subtracting a range from an int only works if the int is not in the
3849    * range */
3850   if (val < min || val > max || val % step) {
3851     /* and the result is the int */
3852     if (dest)
3853       gst_value_init_and_copy (dest, minuend);
3854     return TRUE;
3855   }
3856   return FALSE;
3857 }
3858
3859 /* creates a new int range based on input values.
3860  */
3861 static gboolean
3862 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
3863     gint max2, gint step)
3864 {
3865   GValue v1 = { 0, };
3866   GValue v2 = { 0, };
3867   GValue *pv1, *pv2;            /* yeah, hungarian! */
3868
3869   g_return_val_if_fail (step > 0, FALSE);
3870   g_return_val_if_fail (min1 % step == 0, FALSE);
3871   g_return_val_if_fail (max1 % step == 0, FALSE);
3872   g_return_val_if_fail (min2 % step == 0, FALSE);
3873   g_return_val_if_fail (max2 % step == 0, FALSE);
3874
3875   if (min1 <= max1 && min2 <= max2) {
3876     pv1 = &v1;
3877     pv2 = &v2;
3878   } else if (min1 <= max1) {
3879     pv1 = dest;
3880     pv2 = NULL;
3881   } else if (min2 <= max2) {
3882     pv1 = NULL;
3883     pv2 = dest;
3884   } else {
3885     return FALSE;
3886   }
3887
3888   if (!dest)
3889     return TRUE;
3890
3891   if (min1 < max1) {
3892     g_value_init (pv1, GST_TYPE_INT_RANGE);
3893     gst_value_set_int_range_step (pv1, min1, max1, step);
3894   } else if (min1 == max1) {
3895     g_value_init (pv1, G_TYPE_INT);
3896     g_value_set_int (pv1, min1);
3897   }
3898   if (min2 < max2) {
3899     g_value_init (pv2, GST_TYPE_INT_RANGE);
3900     gst_value_set_int_range_step (pv2, min2, max2, step);
3901   } else if (min2 == max2) {
3902     g_value_init (pv2, G_TYPE_INT);
3903     g_value_set_int (pv2, min2);
3904   }
3905
3906   if (min1 <= max1 && min2 <= max2) {
3907     gst_value_list_concat_and_take_values (dest, pv1, pv2);
3908   }
3909   return TRUE;
3910 }
3911
3912 static gboolean
3913 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
3914     const GValue * subtrahend)
3915 {
3916   gint min = gst_value_get_int_range_min (minuend);
3917   gint max = gst_value_get_int_range_max (minuend);
3918   gint step = gst_value_get_int_range_step (minuend);
3919   gint val = g_value_get_int (subtrahend);
3920
3921   g_return_val_if_fail (min < max, FALSE);
3922
3923   if (step == 0)
3924     return FALSE;
3925
3926   /* value is outside of the range, return range unchanged */
3927   if (val < min || val > max || val % step) {
3928     if (dest)
3929       gst_value_init_and_copy (dest, minuend);
3930     return TRUE;
3931   } else {
3932     /* max must be MAXINT too as val <= max */
3933     if (val >= G_MAXINT - step + 1) {
3934       max -= step;
3935       val -= step;
3936     }
3937     /* min must be MININT too as val >= max */
3938     if (val <= G_MININT + step - 1) {
3939       min += step;
3940       val += step;
3941     }
3942     if (dest)
3943       gst_value_create_new_range (dest, min, val - step, val + step, max, step);
3944   }
3945   return TRUE;
3946 }
3947
3948 static gboolean
3949 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
3950     const GValue * subtrahend)
3951 {
3952   gint min1 = gst_value_get_int_range_min (minuend);
3953   gint max1 = gst_value_get_int_range_max (minuend);
3954   gint step1 = gst_value_get_int_range_step (minuend);
3955   gint min2 = gst_value_get_int_range_min (subtrahend);
3956   gint max2 = gst_value_get_int_range_max (subtrahend);
3957   gint step2 = gst_value_get_int_range_step (subtrahend);
3958   gint step;
3959
3960   if (step1 != step2) {
3961     /* ENOIMPL */
3962     g_assert (FALSE);
3963     return FALSE;
3964   }
3965   step = step1;
3966
3967   if (step == 0)
3968     return FALSE;
3969
3970   if (max2 >= max1 && min2 <= min1) {
3971     return FALSE;
3972   } else if (max2 >= max1) {
3973     return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3974         step, 0, step);
3975   } else if (min2 <= min1) {
3976     return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
3977         step, 0, step);
3978   } else {
3979     return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3980         MAX (max2 + step, min1), max1, step);
3981   }
3982 }
3983
3984 static gboolean
3985 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
3986     const GValue * subtrahend)
3987 {
3988   gint64 min = gst_value_get_int64_range_min (subtrahend);
3989   gint64 max = gst_value_get_int64_range_max (subtrahend);
3990   gint64 step = gst_value_get_int64_range_step (subtrahend);
3991   gint64 val = g_value_get_int64 (minuend);
3992
3993   if (step == 0)
3994     return FALSE;
3995   /* subtracting a range from an int64 only works if the int64 is not in the
3996    * range */
3997   if (val < min || val > max || val % step) {
3998     /* and the result is the int64 */
3999     if (dest)
4000       gst_value_init_and_copy (dest, minuend);
4001     return TRUE;
4002   }
4003   return FALSE;
4004 }
4005
4006 /* creates a new int64 range based on input values.
4007  */
4008 static gboolean
4009 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
4010     gint64 min2, gint64 max2, gint64 step)
4011 {
4012   GValue v1 = { 0, };
4013   GValue v2 = { 0, };
4014   GValue *pv1, *pv2;            /* yeah, hungarian! */
4015
4016   g_return_val_if_fail (step > 0, FALSE);
4017   g_return_val_if_fail (min1 % step == 0, FALSE);
4018   g_return_val_if_fail (max1 % step == 0, FALSE);
4019   g_return_val_if_fail (min2 % step == 0, FALSE);
4020   g_return_val_if_fail (max2 % step == 0, FALSE);
4021
4022   if (min1 <= max1 && min2 <= max2) {
4023     pv1 = &v1;
4024     pv2 = &v2;
4025   } else if (min1 <= max1) {
4026     pv1 = dest;
4027     pv2 = NULL;
4028   } else if (min2 <= max2) {
4029     pv1 = NULL;
4030     pv2 = dest;
4031   } else {
4032     return FALSE;
4033   }
4034
4035   if (!dest)
4036     return TRUE;
4037
4038   if (min1 < max1) {
4039     g_value_init (pv1, GST_TYPE_INT64_RANGE);
4040     gst_value_set_int64_range_step (pv1, min1, max1, step);
4041   } else if (min1 == max1) {
4042     g_value_init (pv1, G_TYPE_INT64);
4043     g_value_set_int64 (pv1, min1);
4044   }
4045   if (min2 < max2) {
4046     g_value_init (pv2, GST_TYPE_INT64_RANGE);
4047     gst_value_set_int64_range_step (pv2, min2, max2, step);
4048   } else if (min2 == max2) {
4049     g_value_init (pv2, G_TYPE_INT64);
4050     g_value_set_int64 (pv2, min2);
4051   }
4052
4053   if (min1 <= max1 && min2 <= max2) {
4054     gst_value_list_concat_and_take_values (dest, pv1, pv2);
4055   }
4056   return TRUE;
4057 }
4058
4059 static gboolean
4060 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
4061     const GValue * subtrahend)
4062 {
4063   gint64 min = gst_value_get_int64_range_min (minuend);
4064   gint64 max = gst_value_get_int64_range_max (minuend);
4065   gint64 step = gst_value_get_int64_range_step (minuend);
4066   gint64 val = g_value_get_int64 (subtrahend);
4067
4068   g_return_val_if_fail (min < max, FALSE);
4069
4070   if (step == 0)
4071     return FALSE;
4072
4073   /* value is outside of the range, return range unchanged */
4074   if (val < min || val > max || val % step) {
4075     if (dest)
4076       gst_value_init_and_copy (dest, minuend);
4077     return TRUE;
4078   } else {
4079     /* max must be MAXINT64 too as val <= max */
4080     if (val >= G_MAXINT64 - step + 1) {
4081       max -= step;
4082       val -= step;
4083     }
4084     /* min must be MININT64 too as val >= max */
4085     if (val <= G_MININT64 + step - 1) {
4086       min += step;
4087       val += step;
4088     }
4089     if (dest)
4090       gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
4091           step);
4092   }
4093   return TRUE;
4094 }
4095
4096 static gboolean
4097 gst_value_subtract_int64_range_int64_range (GValue * dest,
4098     const GValue * minuend, const GValue * subtrahend)
4099 {
4100   gint64 min1 = gst_value_get_int64_range_min (minuend);
4101   gint64 max1 = gst_value_get_int64_range_max (minuend);
4102   gint64 step1 = gst_value_get_int64_range_step (minuend);
4103   gint64 min2 = gst_value_get_int64_range_min (subtrahend);
4104   gint64 max2 = gst_value_get_int64_range_max (subtrahend);
4105   gint64 step2 = gst_value_get_int64_range_step (subtrahend);
4106   gint64 step;
4107
4108   if (step1 != step2) {
4109     /* ENOIMPL */
4110     g_assert (FALSE);
4111     return FALSE;
4112   }
4113
4114   if (step1 == 0)
4115     return FALSE;
4116
4117   step = step1;
4118
4119   if (max2 >= max1 && min2 <= min1) {
4120     return FALSE;
4121   } else if (max2 >= max1) {
4122     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
4123             max1), step, 0, step);
4124   } else if (min2 <= min1) {
4125     return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
4126         max1, step, 0, step);
4127   } else {
4128     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
4129             max1), MAX (max2 + step, min1), max1, step);
4130   }
4131 }
4132
4133 static gboolean
4134 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
4135     const GValue * subtrahend)
4136 {
4137   gdouble min = gst_value_get_double_range_min (subtrahend);
4138   gdouble max = gst_value_get_double_range_max (subtrahend);
4139   gdouble val = g_value_get_double (minuend);
4140
4141   if (val < min || val > max) {
4142     if (dest)
4143       gst_value_init_and_copy (dest, minuend);
4144     return TRUE;
4145   }
4146   return FALSE;
4147 }
4148
4149 static gboolean
4150 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
4151     const GValue * subtrahend)
4152 {
4153   /* since we don't have open ranges, we cannot create a hole in
4154    * a double range. We return the original range */
4155   if (dest)
4156     gst_value_init_and_copy (dest, minuend);
4157   return TRUE;
4158 }
4159
4160 static gboolean
4161 gst_value_subtract_double_range_double_range (GValue * dest,
4162     const GValue * minuend, const GValue * subtrahend)
4163 {
4164   /* since we don't have open ranges, we have to approximate */
4165   /* done like with ints */
4166   gdouble min1 = gst_value_get_double_range_min (minuend);
4167   gdouble max2 = gst_value_get_double_range_max (minuend);
4168   gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
4169   gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
4170   GValue v1 = { 0, };
4171   GValue v2 = { 0, };
4172   GValue *pv1, *pv2;            /* yeah, hungarian! */
4173
4174   if (min1 < max1 && min2 < max2) {
4175     pv1 = &v1;
4176     pv2 = &v2;
4177   } else if (min1 < max1) {
4178     pv1 = dest;
4179     pv2 = NULL;
4180   } else if (min2 < max2) {
4181     pv1 = NULL;
4182     pv2 = dest;
4183   } else {
4184     return FALSE;
4185   }
4186
4187   if (!dest)
4188     return TRUE;
4189
4190   if (min1 < max1) {
4191     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
4192     gst_value_set_double_range (pv1, min1, max1);
4193   }
4194   if (min2 < max2) {
4195     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
4196     gst_value_set_double_range (pv2, min2, max2);
4197   }
4198
4199   if (min1 < max1 && min2 < max2) {
4200     gst_value_list_concat_and_take_values (dest, pv1, pv2);
4201   }
4202   return TRUE;
4203 }
4204
4205 static gboolean
4206 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
4207     const GValue * subtrahend)
4208 {
4209   guint i, size;
4210   GValue subtraction = { 0, };
4211   gboolean ret = FALSE;
4212   GType ltype;
4213
4214   ltype = gst_value_list_get_type ();
4215
4216   size = VALUE_LIST_SIZE (minuend);
4217   for (i = 0; i < size; i++) {
4218     const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
4219
4220     /* quicker version when we can discard the result */
4221     if (!dest) {
4222       if (gst_value_subtract (NULL, cur, subtrahend)) {
4223         ret = TRUE;
4224         break;
4225       }
4226       continue;
4227     }
4228
4229     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
4230       if (!ret) {
4231         gst_value_move (dest, &subtraction);
4232         ret = TRUE;
4233       } else if (G_VALUE_HOLDS (dest, ltype)
4234           && !G_VALUE_HOLDS (&subtraction, ltype)) {
4235         _gst_value_list_append_and_take_value (dest, &subtraction);
4236       } else {
4237         GValue temp;
4238
4239         gst_value_move (&temp, dest);
4240         gst_value_list_concat_and_take_values (dest, &temp, &subtraction);
4241       }
4242     }
4243   }
4244   return ret;
4245 }
4246
4247 static gboolean
4248 gst_value_subtract_list (GValue * dest, const GValue * minuend,
4249     const GValue * subtrahend)
4250 {
4251   guint i, size;
4252   GValue data[2] = { {0,}, {0,} };
4253   GValue *subtraction = &data[0], *result = &data[1];
4254
4255   gst_value_init_and_copy (result, minuend);
4256   size = VALUE_LIST_SIZE (subtrahend);
4257   for (i = 0; i < size; i++) {
4258     const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
4259
4260     if (gst_value_subtract (subtraction, result, cur)) {
4261       GValue *temp = result;
4262
4263       result = subtraction;
4264       subtraction = temp;
4265       g_value_unset (subtraction);
4266     } else {
4267       g_value_unset (result);
4268       return FALSE;
4269     }
4270   }
4271   if (dest) {
4272     gst_value_move (dest, result);
4273   } else {
4274     g_value_unset (result);
4275   }
4276   return TRUE;
4277 }
4278
4279 static gboolean
4280 gst_value_subtract_fraction_fraction_range (GValue * dest,
4281     const GValue * minuend, const GValue * subtrahend)
4282 {
4283   const GValue *min = gst_value_get_fraction_range_min (subtrahend);
4284   const GValue *max = gst_value_get_fraction_range_max (subtrahend);
4285   GstValueCompareFunc compare;
4286
4287   if ((compare = gst_value_get_compare_func (minuend))) {
4288     /* subtracting a range from an fraction only works if the fraction
4289      * is not in the range */
4290     if (gst_value_compare_with_func (minuend, min, compare) ==
4291         GST_VALUE_LESS_THAN ||
4292         gst_value_compare_with_func (minuend, max, compare) ==
4293         GST_VALUE_GREATER_THAN) {
4294       /* and the result is the value */
4295       if (dest)
4296         gst_value_init_and_copy (dest, minuend);
4297       return TRUE;
4298     }
4299   }
4300   return FALSE;
4301 }
4302
4303 static gboolean
4304 gst_value_subtract_fraction_range_fraction (GValue * dest,
4305     const GValue * minuend, const GValue * subtrahend)
4306 {
4307   /* since we don't have open ranges, we cannot create a hole in
4308    * a range. We return the original range */
4309   if (dest)
4310     gst_value_init_and_copy (dest, minuend);
4311   return TRUE;
4312 }
4313
4314 static gboolean
4315 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
4316     const GValue * minuend, const GValue * subtrahend)
4317 {
4318   /* since we don't have open ranges, we have to approximate */
4319   /* done like with ints and doubles. Creates a list of 2 fraction ranges */
4320   const GValue *min1 = gst_value_get_fraction_range_min (minuend);
4321   const GValue *max2 = gst_value_get_fraction_range_max (minuend);
4322   const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
4323   const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
4324   gint cmp1, cmp2;
4325   GValue v1 = { 0, };
4326   GValue v2 = { 0, };
4327   GValue *pv1, *pv2;            /* yeah, hungarian! */
4328   GstValueCompareFunc compare;
4329
4330   g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
4331   g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
4332
4333   compare = gst_value_get_compare_func (min1);
4334   g_return_val_if_fail (compare, FALSE);
4335
4336   cmp1 = gst_value_compare_with_func (max2, max1, compare);
4337   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
4338   if (cmp1 == GST_VALUE_LESS_THAN)
4339     max1 = max2;
4340   cmp1 = gst_value_compare_with_func (min1, min2, compare);
4341   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
4342   if (cmp1 == GST_VALUE_GREATER_THAN)
4343     min2 = min1;
4344
4345   cmp1 = gst_value_compare_with_func (min1, max1, compare);
4346   cmp2 = gst_value_compare_with_func (min2, max2, compare);
4347
4348   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
4349     pv1 = &v1;
4350     pv2 = &v2;
4351   } else if (cmp1 == GST_VALUE_LESS_THAN) {
4352     pv1 = dest;
4353     pv2 = NULL;
4354   } else if (cmp2 == GST_VALUE_LESS_THAN) {
4355     pv1 = NULL;
4356     pv2 = dest;
4357   } else {
4358     return FALSE;
4359   }
4360
4361   if (!dest)
4362     return TRUE;
4363
4364   if (cmp1 == GST_VALUE_LESS_THAN) {
4365     g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
4366     gst_value_set_fraction_range (pv1, min1, max1);
4367   }
4368   if (cmp2 == GST_VALUE_LESS_THAN) {
4369     g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
4370     gst_value_set_fraction_range (pv2, min2, max2);
4371   }
4372
4373   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
4374     gst_value_list_concat_and_take_values (dest, pv1, pv2);
4375   }
4376   return TRUE;
4377 }
4378
4379
4380 /**************
4381  * comparison *
4382  **************/
4383
4384 /*
4385  * gst_value_get_compare_func:
4386  * @value1: a value to get the compare function for
4387  *
4388  * Determines the compare function to be used with values of the same type as
4389  * @value1. The function can be given to gst_value_compare_with_func().
4390  *
4391  * Returns: A #GstValueCompareFunc value
4392  */
4393 static GstValueCompareFunc
4394 gst_value_get_compare_func (const GValue * value1)
4395 {
4396   GstValueTable *table, *best = NULL;
4397   guint i;
4398   GType type1;
4399
4400   type1 = G_VALUE_TYPE (value1);
4401
4402   /* this is a fast check */
4403   best = gst_value_hash_lookup_type (type1);
4404
4405   /* slower checks */
4406   if (G_UNLIKELY (!best || !best->compare)) {
4407     guint len = gst_value_table->len;
4408
4409     best = NULL;
4410     for (i = 0; i < len; i++) {
4411       table = &g_array_index (gst_value_table, GstValueTable, i);
4412       if (table->compare && g_type_is_a (type1, table->type)) {
4413         if (!best || g_type_is_a (table->type, best->type))
4414           best = table;
4415       }
4416     }
4417   }
4418   if (G_LIKELY (best))
4419     return best->compare;
4420
4421   return NULL;
4422 }
4423
4424 static inline gboolean
4425 gst_value_can_compare_unchecked (const GValue * value1, const GValue * value2)
4426 {
4427   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4428     return FALSE;
4429
4430   return gst_value_get_compare_func (value1) != NULL;
4431 }
4432
4433 /**
4434  * gst_value_can_compare:
4435  * @value1: a value to compare
4436  * @value2: another value to compare
4437  *
4438  * Determines if @value1 and @value2 can be compared.
4439  *
4440  * Returns: %TRUE if the values can be compared
4441  */
4442 gboolean
4443 gst_value_can_compare (const GValue * value1, const GValue * value2)
4444 {
4445   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4446   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4447
4448   return gst_value_can_compare_unchecked (value1, value2);
4449 }
4450
4451 static gboolean
4452 gst_value_list_equals_range (const GValue * list, const GValue * value)
4453 {
4454   const GValue *first;
4455   guint list_size, n;
4456
4457   g_assert (G_IS_VALUE (list));
4458   g_assert (G_IS_VALUE (value));
4459   g_assert (GST_VALUE_HOLDS_LIST (list));
4460
4461   /* TODO: compare against an empty list ? No type though... */
4462   list_size = VALUE_LIST_SIZE (list);
4463   if (list_size == 0)
4464     return FALSE;
4465
4466   /* compare the basic types - they have to match */
4467   first = VALUE_LIST_GET_VALUE (list, 0);
4468 #define CHECK_TYPES(type,prefix) \
4469   (prefix##_VALUE_HOLDS_##type(first) && GST_VALUE_HOLDS_##type##_RANGE (value))
4470   if (CHECK_TYPES (INT, G)) {
4471     const gint rmin = gst_value_get_int_range_min (value);
4472     const gint rmax = gst_value_get_int_range_max (value);
4473     const gint rstep = gst_value_get_int_range_step (value);
4474     if (rstep == 0)
4475       return FALSE;
4476     /* note: this will overflow for min 0 and max INT_MAX, but this
4477        would only be equal to a list of INT_MAX elements, which seems
4478        very unlikely */
4479     if (list_size != rmax / rstep - rmin / rstep + 1)
4480       return FALSE;
4481     for (n = 0; n < list_size; ++n) {
4482       gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
4483       if (v < rmin || v > rmax || v % rstep) {
4484         return FALSE;
4485       }
4486     }
4487     return TRUE;
4488   } else if (CHECK_TYPES (INT64, G)) {
4489     const gint64 rmin = gst_value_get_int64_range_min (value);
4490     const gint64 rmax = gst_value_get_int64_range_max (value);
4491     const gint64 rstep = gst_value_get_int64_range_step (value);
4492     GST_DEBUG ("List/range of int64s");
4493     if (rstep == 0)
4494       return FALSE;
4495     if (list_size != rmax / rstep - rmin / rstep + 1)
4496       return FALSE;
4497     for (n = 0; n < list_size; ++n) {
4498       gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
4499       if (v < rmin || v > rmax || v % rstep)
4500         return FALSE;
4501     }
4502     return TRUE;
4503   }
4504 #undef CHECK_TYPES
4505
4506   /* other combinations don't make sense for equality */
4507   return FALSE;
4508 }
4509
4510 /* "Pure" variant of gst_value_compare which is guaranteed to
4511  * not have list arguments and therefore does basic comparisions
4512  */
4513 static inline gint
4514 _gst_value_compare_nolist (const GValue * value1, const GValue * value2)
4515 {
4516   GstValueCompareFunc compare;
4517
4518   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4519     return GST_VALUE_UNORDERED;
4520
4521   compare = gst_value_get_compare_func (value1);
4522   if (compare) {
4523     return compare (value1, value2);
4524   }
4525
4526   g_critical ("unable to compare values of type %s\n",
4527       g_type_name (G_VALUE_TYPE (value1)));
4528   return GST_VALUE_UNORDERED;
4529 }
4530
4531 /**
4532  * gst_value_compare:
4533  * @value1: a value to compare
4534  * @value2: another value to compare
4535  *
4536  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
4537  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
4538  * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
4539  * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
4540  * If the values are equal, GST_VALUE_EQUAL is returned.
4541  *
4542  * Returns: comparison result
4543  */
4544 gint
4545 gst_value_compare (const GValue * value1, const GValue * value2)
4546 {
4547   GType ltype;
4548
4549   g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
4550   g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
4551
4552   /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
4553      as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
4554   ltype = gst_value_list_get_type ();
4555   if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)) {
4556     gint i, n, ret;
4557
4558     if (gst_value_list_equals_range (value1, value2)) {
4559       return GST_VALUE_EQUAL;
4560     }
4561
4562     n = gst_value_list_get_size (value1);
4563     if (n == 0)
4564       return GST_VALUE_UNORDERED;
4565
4566     for (i = 0; i < n; i++) {
4567       const GValue *elt;
4568
4569       elt = gst_value_list_get_value (value1, i);
4570       ret = gst_value_compare (elt, value2);
4571       if (ret != GST_VALUE_EQUAL && n == 1)
4572         return ret;
4573       else if (ret != GST_VALUE_EQUAL)
4574         return GST_VALUE_UNORDERED;
4575     }
4576
4577     return GST_VALUE_EQUAL;
4578   } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)) {
4579     gint i, n, ret;
4580
4581     if (gst_value_list_equals_range (value2, value1)) {
4582       return GST_VALUE_EQUAL;
4583     }
4584
4585     n = gst_value_list_get_size (value2);
4586     if (n == 0)
4587       return GST_VALUE_UNORDERED;
4588
4589     for (i = 0; i < n; i++) {
4590       const GValue *elt;
4591
4592       elt = gst_value_list_get_value (value2, i);
4593       ret = gst_value_compare (elt, value1);
4594       if (ret != GST_VALUE_EQUAL && n == 1)
4595         return ret;
4596       else if (ret != GST_VALUE_EQUAL)
4597         return GST_VALUE_UNORDERED;
4598     }
4599
4600     return GST_VALUE_EQUAL;
4601   }
4602
4603   /* And now handle the generic case */
4604   return _gst_value_compare_nolist (value1, value2);
4605 }
4606
4607 /*
4608  * gst_value_compare_with_func:
4609  * @value1: a value to compare
4610  * @value2: another value to compare
4611  * @compare: compare function
4612  *
4613  * Compares @value1 and @value2 using the @compare function. Works like
4614  * gst_value_compare() but allows to save time determining the compare function
4615  * a multiple times. 
4616  *
4617  * Returns: comparison result
4618  */
4619 static gint
4620 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
4621     GstValueCompareFunc compare)
4622 {
4623   g_assert (compare);
4624
4625   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4626     return GST_VALUE_UNORDERED;
4627
4628   return compare (value1, value2);
4629 }
4630
4631 /* union */
4632
4633 /**
4634  * gst_value_can_union:
4635  * @value1: a value to union
4636  * @value2: another value to union
4637  *
4638  * Determines if @value1 and @value2 can be non-trivially unioned.
4639  * Any two values can be trivially unioned by adding both of them
4640  * to a #GstValueList.  However, certain types have the possibility
4641  * to be unioned in a simpler way.  For example, an integer range
4642  * and an integer can be unioned if the integer is a subset of the
4643  * integer range.  If there is the possibility that two values can
4644  * be unioned, this function returns %TRUE.
4645  *
4646  * Returns: %TRUE if there is a function allowing the two values to
4647  * be unioned.
4648  */
4649 gboolean
4650 gst_value_can_union (const GValue * value1, const GValue * value2)
4651 {
4652   GstValueUnionInfo *union_info;
4653   guint i, len;
4654
4655   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4656   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4657
4658   len = gst_value_union_funcs->len;
4659
4660   for (i = 0; i < len; i++) {
4661     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4662     if (union_info->type1 == G_VALUE_TYPE (value1) &&
4663         union_info->type2 == G_VALUE_TYPE (value2))
4664       return TRUE;
4665     if (union_info->type1 == G_VALUE_TYPE (value2) &&
4666         union_info->type2 == G_VALUE_TYPE (value1))
4667       return TRUE;
4668   }
4669
4670   return FALSE;
4671 }
4672
4673 /**
4674  * gst_value_union:
4675  * @dest: (out caller-allocates): the destination value
4676  * @value1: a value to union
4677  * @value2: another value to union
4678  *
4679  * Creates a GValue corresponding to the union of @value1 and @value2.
4680  *
4681  * Returns: %TRUE if the union succeeded.
4682  */
4683 gboolean
4684 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
4685 {
4686   const GstValueUnionInfo *union_info;
4687   guint i, len;
4688   GType type1, type2;
4689
4690   g_return_val_if_fail (dest != NULL, FALSE);
4691   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4692   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4693   g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
4694       FALSE);
4695
4696   len = gst_value_union_funcs->len;
4697   type1 = G_VALUE_TYPE (value1);
4698   type2 = G_VALUE_TYPE (value2);
4699
4700   for (i = 0; i < len; i++) {
4701     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4702     if (union_info->type1 == type1 && union_info->type2 == type2) {
4703       return union_info->func (dest, value1, value2);
4704     }
4705     if (union_info->type1 == type2 && union_info->type2 == type1) {
4706       return union_info->func (dest, value2, value1);
4707     }
4708   }
4709
4710   gst_value_list_concat (dest, value1, value2);
4711   return TRUE;
4712 }
4713
4714 /* gst_value_register_union_func: (skip)
4715  * @type1: a type to union
4716  * @type2: another type to union
4717  * @func: a function that implements creating a union between the two types
4718  *
4719  * Registers a union function that can create a union between #GValue items
4720  * of the type @type1 and @type2.
4721  *
4722  * Union functions should be registered at startup before any pipelines are
4723  * started, as gst_value_register_union_func() is not thread-safe and cannot
4724  * be used at the same time as gst_value_union() or gst_value_can_union().
4725  */
4726 static void
4727 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
4728 {
4729   GstValueUnionInfo union_info;
4730
4731   union_info.type1 = type1;
4732   union_info.type2 = type2;
4733   union_info.func = func;
4734
4735   g_array_append_val (gst_value_union_funcs, union_info);
4736 }
4737
4738 /* intersection */
4739
4740 /**
4741  * gst_value_can_intersect:
4742  * @value1: a value to intersect
4743  * @value2: another value to intersect
4744  *
4745  * Determines if intersecting two values will produce a valid result.
4746  * Two values will produce a valid intersection if they have the same
4747  * type.
4748  *
4749  * Returns: %TRUE if the values can intersect
4750  */
4751 gboolean
4752 gst_value_can_intersect (const GValue * value1, const GValue * value2)
4753 {
4754   GstValueIntersectInfo *intersect_info;
4755   guint i, len;
4756   GType ltype, type1, type2;
4757
4758   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4759   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4760
4761   ltype = gst_value_list_get_type ();
4762
4763   /* special cases */
4764   if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
4765     return TRUE;
4766
4767   type1 = G_VALUE_TYPE (value1);
4768   type2 = G_VALUE_TYPE (value2);
4769
4770   /* practically all GstValue types have a compare function (_can_compare=TRUE)
4771    * GstStructure and GstCaps have npot, but are intersectable */
4772   if (type1 == type2)
4773     return TRUE;
4774
4775   /* check registered intersect functions */
4776   len = gst_value_intersect_funcs->len;
4777   for (i = 0; i < len; i++) {
4778     intersect_info = &g_array_index (gst_value_intersect_funcs,
4779         GstValueIntersectInfo, i);
4780     if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
4781         (intersect_info->type1 == type2 && intersect_info->type2 == type1))
4782       return TRUE;
4783   }
4784
4785   return gst_value_can_compare_unchecked (value1, value2);
4786 }
4787
4788 /**
4789  * gst_value_intersect:
4790  * @dest: (out caller-allocates) (transfer full): a uninitialized #GValue that will hold the calculated
4791  * intersection value. May be %NULL if the resulting set if not needed.
4792  * @value1: a value to intersect
4793  * @value2: another value to intersect
4794  *
4795  * Calculates the intersection of two values.  If the values have
4796  * a non-empty intersection, the value representing the intersection
4797  * is placed in @dest, unless %NULL.  If the intersection is non-empty,
4798  * @dest is not modified.
4799  *
4800  * Returns: %TRUE if the intersection is non-empty
4801  */
4802 gboolean
4803 gst_value_intersect (GValue * dest, const GValue * value1,
4804     const GValue * value2)
4805 {
4806   GstValueIntersectInfo *intersect_info;
4807   guint i, len;
4808   GType ltype, type1, type2;
4809
4810   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4811   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4812
4813   ltype = gst_value_list_get_type ();
4814
4815   /* special cases first */
4816   if (G_VALUE_HOLDS (value1, ltype))
4817     return gst_value_intersect_list (dest, value1, value2);
4818   if (G_VALUE_HOLDS (value2, ltype))
4819     return gst_value_intersect_list (dest, value2, value1);
4820
4821   if (_gst_value_compare_nolist (value1, value2) == GST_VALUE_EQUAL) {
4822     if (dest)
4823       gst_value_init_and_copy (dest, value1);
4824     return TRUE;
4825   }
4826
4827   type1 = G_VALUE_TYPE (value1);
4828   type2 = G_VALUE_TYPE (value2);
4829
4830   len = gst_value_intersect_funcs->len;
4831   for (i = 0; i < len; i++) {
4832     intersect_info = &g_array_index (gst_value_intersect_funcs,
4833         GstValueIntersectInfo, i);
4834     if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
4835       return intersect_info->func (dest, value1, value2);
4836     }
4837     if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
4838       return intersect_info->func (dest, value2, value1);
4839     }
4840   }
4841   return FALSE;
4842 }
4843
4844
4845
4846 /* gst_value_register_intersect_func: (skip)
4847  * @type1: the first type to intersect
4848  * @type2: the second type to intersect
4849  * @func: the intersection function
4850  *
4851  * Registers a function that is called to calculate the intersection
4852  * of the values having the types @type1 and @type2.
4853  *
4854  * Intersect functions should be registered at startup before any pipelines are
4855  * started, as gst_value_register_intersect_func() is not thread-safe and
4856  * cannot be used at the same time as gst_value_intersect() or
4857  * gst_value_can_intersect().
4858  */
4859 static void
4860 gst_value_register_intersect_func (GType type1, GType type2,
4861     GstValueIntersectFunc func)
4862 {
4863   GstValueIntersectInfo intersect_info;
4864
4865   intersect_info.type1 = type1;
4866   intersect_info.type2 = type2;
4867   intersect_info.func = func;
4868
4869   g_array_append_val (gst_value_intersect_funcs, intersect_info);
4870 }
4871
4872
4873 /* subtraction */
4874
4875 /**
4876  * gst_value_subtract:
4877  * @dest: (out caller-allocates): the destination value for the result if the
4878  *     subtraction is not empty. May be %NULL, in which case the resulting set
4879  *     will not be computed, which can give a fair speedup.
4880  * @minuend: the value to subtract from
4881  * @subtrahend: the value to subtract
4882  *
4883  * Subtracts @subtrahend from @minuend and stores the result in @dest.
4884  * Note that this means subtraction as in sets, not as in mathematics.
4885  *
4886  * Returns: %TRUE if the subtraction is not empty
4887  */
4888 gboolean
4889 gst_value_subtract (GValue * dest, const GValue * minuend,
4890     const GValue * subtrahend)
4891 {
4892   GstValueSubtractInfo *info;
4893   guint i, len;
4894   GType ltype, mtype, stype;
4895
4896   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4897   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4898
4899   ltype = gst_value_list_get_type ();
4900
4901   /* special cases first */
4902   if (G_VALUE_HOLDS (minuend, ltype))
4903     return gst_value_subtract_from_list (dest, minuend, subtrahend);
4904   if (G_VALUE_HOLDS (subtrahend, ltype))
4905     return gst_value_subtract_list (dest, minuend, subtrahend);
4906
4907   mtype = G_VALUE_TYPE (minuend);
4908   stype = G_VALUE_TYPE (subtrahend);
4909
4910   len = gst_value_subtract_funcs->len;
4911   for (i = 0; i < len; i++) {
4912     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4913     if (info->minuend == mtype && info->subtrahend == stype) {
4914       return info->func (dest, minuend, subtrahend);
4915     }
4916   }
4917
4918   if (_gst_value_compare_nolist (minuend, subtrahend) != GST_VALUE_EQUAL) {
4919     if (dest)
4920       gst_value_init_and_copy (dest, minuend);
4921     return TRUE;
4922   }
4923
4924   return FALSE;
4925 }
4926
4927 #if 0
4928 gboolean
4929 gst_value_subtract (GValue * dest, const GValue * minuend,
4930     const GValue * subtrahend)
4931 {
4932   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
4933
4934   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
4935       gst_value_serialize (subtrahend),
4936       ret ? gst_value_serialize (dest) : "---");
4937   return ret;
4938 }
4939 #endif
4940
4941 /**
4942  * gst_value_can_subtract:
4943  * @minuend: the value to subtract from
4944  * @subtrahend: the value to subtract
4945  *
4946  * Checks if it's possible to subtract @subtrahend from @minuend.
4947  *
4948  * Returns: %TRUE if a subtraction is possible
4949  */
4950 gboolean
4951 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
4952 {
4953   GstValueSubtractInfo *info;
4954   guint i, len;
4955   GType ltype, mtype, stype;
4956
4957   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4958   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4959
4960   ltype = gst_value_list_get_type ();
4961
4962   /* special cases */
4963   if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
4964     return TRUE;
4965
4966   mtype = G_VALUE_TYPE (minuend);
4967   stype = G_VALUE_TYPE (subtrahend);
4968
4969   len = gst_value_subtract_funcs->len;
4970   for (i = 0; i < len; i++) {
4971     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4972     if (info->minuend == mtype && info->subtrahend == stype)
4973       return TRUE;
4974   }
4975
4976   return gst_value_can_compare_unchecked (minuend, subtrahend);
4977 }
4978
4979 /* gst_value_register_subtract_func: (skip)
4980  * @minuend_type: type of the minuend
4981  * @subtrahend_type: type of the subtrahend
4982  * @func: function to use
4983  *
4984  * Registers @func as a function capable of subtracting the values of
4985  * @subtrahend_type from values of @minuend_type.
4986  *
4987  * Subtract functions should be registered at startup before any pipelines are
4988  * started, as gst_value_register_subtract_func() is not thread-safe and
4989  * cannot be used at the same time as gst_value_subtract().
4990  */
4991 static void
4992 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
4993     GstValueSubtractFunc func)
4994 {
4995   GstValueSubtractInfo info;
4996
4997   g_return_if_fail (!gst_type_is_fixed (minuend_type)
4998       || !gst_type_is_fixed (subtrahend_type));
4999
5000   info.minuend = minuend_type;
5001   info.subtrahend = subtrahend_type;
5002   info.func = func;
5003
5004   g_array_append_val (gst_value_subtract_funcs, info);
5005 }
5006
5007 /**
5008  * gst_value_register:
5009  * @table: structure containing functions to register
5010  *
5011  * Registers functions to perform calculations on #GValue items of a given
5012  * type. Each type can only be added once.
5013  */
5014 void
5015 gst_value_register (const GstValueTable * table)
5016 {
5017   GstValueTable *found;
5018
5019   g_return_if_fail (table != NULL);
5020
5021   g_array_append_val (gst_value_table, *table);
5022
5023   found = gst_value_hash_lookup_type (table->type);
5024   if (found)
5025     g_warning ("adding type %s multiple times", g_type_name (table->type));
5026
5027   /* FIXME: we're not really doing the const justice, we assume the table is
5028    * static */
5029   gst_value_hash_add_type (table->type, table);
5030 }
5031
5032 /**
5033  * gst_value_init_and_copy:
5034  * @dest: (out caller-allocates): the target value
5035  * @src: the source value
5036  *
5037  * Initialises the target value to be of the same type as source and then copies
5038  * the contents from source to target.
5039  */
5040 void
5041 gst_value_init_and_copy (GValue * dest, const GValue * src)
5042 {
5043   g_return_if_fail (G_IS_VALUE (src));
5044   g_return_if_fail (dest != NULL);
5045
5046   g_value_init (dest, G_VALUE_TYPE (src));
5047   g_value_copy (src, dest);
5048 }
5049
5050 /* move src into dest and clear src */
5051 static void
5052 gst_value_move (GValue * dest, GValue * src)
5053 {
5054   g_assert (G_IS_VALUE (src));
5055   g_assert (dest != NULL);
5056
5057   *dest = *src;
5058   memset (src, 0, sizeof (GValue));
5059 }
5060
5061 /**
5062  * gst_value_serialize:
5063  * @value: a #GValue to serialize
5064  *
5065  * tries to transform the given @value into a string representation that allows
5066  * getting back this string later on using gst_value_deserialize().
5067  *
5068  * Free-function: g_free
5069  *
5070  * Returns: (transfer full): the serialization for @value or %NULL if none exists
5071  */
5072 gchar *
5073 gst_value_serialize (const GValue * value)
5074 {
5075   guint i, len;
5076   GValue s_val = { 0 };
5077   GstValueTable *table, *best;
5078   gchar *s;
5079   GType type;
5080
5081   g_return_val_if_fail (G_IS_VALUE (value), NULL);
5082
5083   type = G_VALUE_TYPE (value);
5084
5085   best = gst_value_hash_lookup_type (type);
5086
5087   if (G_UNLIKELY (!best || !best->serialize)) {
5088     len = gst_value_table->len;
5089     best = NULL;
5090     for (i = 0; i < len; i++) {
5091       table = &g_array_index (gst_value_table, GstValueTable, i);
5092       if (table->serialize && g_type_is_a (type, table->type)) {
5093         if (!best || g_type_is_a (table->type, best->type))
5094           best = table;
5095       }
5096     }
5097   }
5098   if (G_LIKELY (best))
5099     return best->serialize (value);
5100
5101   g_value_init (&s_val, G_TYPE_STRING);
5102   if (g_value_transform (value, &s_val)) {
5103     s = gst_string_wrap (g_value_get_string (&s_val));
5104   } else {
5105     s = NULL;
5106   }
5107   g_value_unset (&s_val);
5108
5109   return s;
5110 }
5111
5112 /**
5113  * gst_value_deserialize:
5114  * @dest: (out caller-allocates): #GValue to fill with contents of
5115  *     deserialization
5116  * @src: string to deserialize
5117  *
5118  * Tries to deserialize a string into the type specified by the given GValue.
5119  * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
5120  *
5121  * Returns: %TRUE on success
5122  */
5123 gboolean
5124 gst_value_deserialize (GValue * dest, const gchar * src)
5125 {
5126   GstValueTable *table, *best;
5127   guint i, len;
5128   GType type;
5129
5130   g_return_val_if_fail (src != NULL, FALSE);
5131   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
5132
5133   type = G_VALUE_TYPE (dest);
5134
5135   best = gst_value_hash_lookup_type (type);
5136   if (G_UNLIKELY (!best || !best->deserialize)) {
5137     len = gst_value_table->len;
5138     best = NULL;
5139     for (i = 0; i < len; i++) {
5140       table = &g_array_index (gst_value_table, GstValueTable, i);
5141       if (table->deserialize && g_type_is_a (type, table->type)) {
5142         if (!best || g_type_is_a (table->type, best->type))
5143           best = table;
5144       }
5145     }
5146   }
5147   if (G_LIKELY (best))
5148     return best->deserialize (dest, src);
5149
5150   return FALSE;
5151 }
5152
5153 /**
5154  * gst_value_is_fixed:
5155  * @value: the #GValue to check
5156  *
5157  * Tests if the given GValue, if available in a GstStructure (or any other
5158  * container) contains a "fixed" (which means: one value) or an "unfixed"
5159  * (which means: multiple possible values, such as data lists or data
5160  * ranges) value.
5161  *
5162  * Returns: true if the value is "fixed".
5163  */
5164
5165 gboolean
5166 gst_value_is_fixed (const GValue * value)
5167 {
5168   GType type;
5169
5170   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
5171
5172   type = G_VALUE_TYPE (value);
5173
5174   /* the most common types are just basic plain glib types */
5175   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
5176     return TRUE;
5177   }
5178
5179   if (type == GST_TYPE_ARRAY) {
5180     gint size, n;
5181     const GValue *kid;
5182
5183     /* check recursively */
5184     size = gst_value_array_get_size (value);
5185     for (n = 0; n < size; n++) {
5186       kid = gst_value_array_get_value (value, n);
5187       if (!gst_value_is_fixed (kid))
5188         return FALSE;
5189     }
5190     return TRUE;
5191   }
5192   return gst_type_is_fixed (type);
5193 }
5194
5195 /**
5196  * gst_value_fixate:
5197  * @dest: the #GValue destination
5198  * @src: the #GValue to fixate
5199  *
5200  * Fixate @src into a new value @dest.
5201  * For ranges, the first element is taken. For lists and arrays, the
5202  * first item is fixated and returned.
5203  * If @src is already fixed, this function returns %FALSE.
5204  *
5205  * Returns: %TRUE if @dest contains a fixated version of @src.
5206  */
5207 gboolean
5208 gst_value_fixate (GValue * dest, const GValue * src)
5209 {
5210   g_return_val_if_fail (G_IS_VALUE (src), FALSE);
5211   g_return_val_if_fail (dest != NULL, FALSE);
5212
5213   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
5214     g_value_init (dest, G_TYPE_INT);
5215     g_value_set_int (dest, gst_value_get_int_range_min (src));
5216   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
5217     g_value_init (dest, G_TYPE_DOUBLE);
5218     g_value_set_double (dest, gst_value_get_double_range_min (src));
5219   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
5220     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
5221   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
5222     GValue temp = { 0 };
5223
5224     /* list could be empty */
5225     if (gst_value_list_get_size (src) <= 0)
5226       return FALSE;
5227
5228     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
5229
5230     if (!gst_value_fixate (dest, &temp)) {
5231       gst_value_move (dest, &temp);
5232     } else {
5233       g_value_unset (&temp);
5234     }
5235   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
5236     gboolean res = FALSE;
5237     guint n, len;
5238
5239     len = gst_value_array_get_size (src);
5240     g_value_init (dest, GST_TYPE_ARRAY);
5241     for (n = 0; n < len; n++) {
5242       GValue kid = { 0 };
5243       const GValue *orig_kid = gst_value_array_get_value (src, n);
5244
5245       if (!gst_value_fixate (&kid, orig_kid))
5246         gst_value_init_and_copy (&kid, orig_kid);
5247       else
5248         res = TRUE;
5249       _gst_value_array_append_and_take_value (dest, &kid);
5250     }
5251
5252     if (!res)
5253       g_value_unset (dest);
5254
5255     return res;
5256   } else {
5257     return FALSE;
5258   }
5259   return TRUE;
5260 }
5261
5262
5263 /************
5264  * fraction *
5265  ************/
5266
5267 /* helper functions */
5268 static void
5269 gst_value_init_fraction (GValue * value)
5270 {
5271   value->data[0].v_int = 0;
5272   value->data[1].v_int = 1;
5273 }
5274
5275 static void
5276 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
5277 {
5278   dest_value->data[0].v_int = src_value->data[0].v_int;
5279   dest_value->data[1].v_int = src_value->data[1].v_int;
5280 }
5281
5282 static gchar *
5283 gst_value_collect_fraction (GValue * value, guint n_collect_values,
5284     GTypeCValue * collect_values, guint collect_flags)
5285 {
5286   if (n_collect_values != 2)
5287     return g_strdup_printf ("not enough value locations for `%s' passed",
5288         G_VALUE_TYPE_NAME (value));
5289   if (collect_values[1].v_int == 0)
5290     return g_strdup_printf ("passed '0' as denominator for `%s'",
5291         G_VALUE_TYPE_NAME (value));
5292   if (collect_values[0].v_int < -G_MAXINT)
5293     return
5294         g_strdup_printf
5295         ("passed value smaller than -G_MAXINT as numerator for `%s'",
5296         G_VALUE_TYPE_NAME (value));
5297   if (collect_values[1].v_int < -G_MAXINT)
5298     return
5299         g_strdup_printf
5300         ("passed value smaller than -G_MAXINT as denominator for `%s'",
5301         G_VALUE_TYPE_NAME (value));
5302
5303   gst_value_set_fraction (value,
5304       collect_values[0].v_int, collect_values[1].v_int);
5305
5306   return NULL;
5307 }
5308
5309 static gchar *
5310 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
5311     GTypeCValue * collect_values, guint collect_flags)
5312 {
5313   gint *numerator = collect_values[0].v_pointer;
5314   gint *denominator = collect_values[1].v_pointer;
5315
5316   if (!numerator)
5317     return g_strdup_printf ("numerator for `%s' passed as NULL",
5318         G_VALUE_TYPE_NAME (value));
5319   if (!denominator)
5320     return g_strdup_printf ("denominator for `%s' passed as NULL",
5321         G_VALUE_TYPE_NAME (value));
5322
5323   *numerator = value->data[0].v_int;
5324   *denominator = value->data[1].v_int;
5325
5326   return NULL;
5327 }
5328
5329 /**
5330  * gst_value_set_fraction:
5331  * @value: a GValue initialized to #GST_TYPE_FRACTION
5332  * @numerator: the numerator of the fraction
5333  * @denominator: the denominator of the fraction
5334  *
5335  * Sets @value to the fraction specified by @numerator over @denominator.
5336  * The fraction gets reduced to the smallest numerator and denominator,
5337  * and if necessary the sign is moved to the numerator.
5338  */
5339 void
5340 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
5341 {
5342   gint gcd = 0;
5343
5344   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
5345   g_return_if_fail (denominator != 0);
5346   g_return_if_fail (denominator >= -G_MAXINT);
5347   g_return_if_fail (numerator >= -G_MAXINT);
5348
5349   /* normalize sign */
5350   if (denominator < 0) {
5351     numerator = -numerator;
5352     denominator = -denominator;
5353   }
5354
5355   /* check for reduction */
5356   gcd = gst_util_greatest_common_divisor (numerator, denominator);
5357   if (gcd) {
5358     numerator /= gcd;
5359     denominator /= gcd;
5360   }
5361
5362   g_assert (denominator > 0);
5363
5364   value->data[0].v_int = numerator;
5365   value->data[1].v_int = denominator;
5366 }
5367
5368 /**
5369  * gst_value_get_fraction_numerator:
5370  * @value: a GValue initialized to #GST_TYPE_FRACTION
5371  *
5372  * Gets the numerator of the fraction specified by @value.
5373  *
5374  * Returns: the numerator of the fraction.
5375  */
5376 gint
5377 gst_value_get_fraction_numerator (const GValue * value)
5378 {
5379   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
5380
5381   return value->data[0].v_int;
5382 }
5383
5384 /**
5385  * gst_value_get_fraction_denominator:
5386  * @value: a GValue initialized to #GST_TYPE_FRACTION
5387  *
5388  * Gets the denominator of the fraction specified by @value.
5389  *
5390  * Returns: the denominator of the fraction.
5391  */
5392 gint
5393 gst_value_get_fraction_denominator (const GValue * value)
5394 {
5395   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
5396
5397   return value->data[1].v_int;
5398 }
5399
5400 /**
5401  * gst_value_fraction_multiply:
5402  * @product: a GValue initialized to #GST_TYPE_FRACTION
5403  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
5404  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
5405  *
5406  * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
5407  * @product to the product of the two fractions.
5408  *
5409  * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
5410  */
5411 gboolean
5412 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
5413     const GValue * factor2)
5414 {
5415   gint n1, n2, d1, d2;
5416   gint res_n, res_d;
5417
5418   g_return_val_if_fail (product != NULL, FALSE);
5419   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
5420   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
5421
5422   n1 = factor1->data[0].v_int;
5423   n2 = factor2->data[0].v_int;
5424   d1 = factor1->data[1].v_int;
5425   d2 = factor2->data[1].v_int;
5426
5427   if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
5428     return FALSE;
5429
5430   gst_value_set_fraction (product, res_n, res_d);
5431
5432   return TRUE;
5433 }
5434
5435 /**
5436  * gst_value_fraction_subtract:
5437  * @dest: a GValue initialized to #GST_TYPE_FRACTION
5438  * @minuend: a GValue initialized to #GST_TYPE_FRACTION
5439  * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
5440  *
5441  * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
5442  *
5443  * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
5444  */
5445 gboolean
5446 gst_value_fraction_subtract (GValue * dest,
5447     const GValue * minuend, const GValue * subtrahend)
5448 {
5449   gint n1, n2, d1, d2;
5450   gint res_n, res_d;
5451
5452   g_return_val_if_fail (dest != NULL, FALSE);
5453   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
5454   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
5455
5456   n1 = minuend->data[0].v_int;
5457   n2 = subtrahend->data[0].v_int;
5458   d1 = minuend->data[1].v_int;
5459   d2 = subtrahend->data[1].v_int;
5460
5461   if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
5462     return FALSE;
5463   gst_value_set_fraction (dest, res_n, res_d);
5464
5465   return TRUE;
5466 }
5467
5468 static gchar *
5469 gst_value_serialize_fraction (const GValue * value)
5470 {
5471   gint32 numerator = value->data[0].v_int;
5472   gint32 denominator = value->data[1].v_int;
5473   gboolean positive = TRUE;
5474
5475   /* get the sign and make components absolute */
5476   if (numerator < 0) {
5477     numerator = -numerator;
5478     positive = !positive;
5479   }
5480   if (denominator < 0) {
5481     denominator = -denominator;
5482     positive = !positive;
5483   }
5484
5485   return g_strdup_printf ("%s%d/%d",
5486       positive ? "" : "-", numerator, denominator);
5487 }
5488
5489 static gboolean
5490 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
5491 {
5492   gint num, den;
5493   gint num_chars;
5494
5495   if (G_UNLIKELY (s == NULL))
5496     return FALSE;
5497
5498   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
5499     return FALSE;
5500
5501   if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
5502     if (s[num_chars] != 0)
5503       return FALSE;
5504     if (den == 0)
5505       return FALSE;
5506
5507     gst_value_set_fraction (dest, num, den);
5508     return TRUE;
5509   } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
5510     gst_value_set_fraction (dest, 1, G_MAXINT);
5511     return TRUE;
5512   } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
5513     if (s[num_chars] != 0)
5514       return FALSE;
5515     gst_value_set_fraction (dest, num, 1);
5516     return TRUE;
5517   } else if (g_ascii_strcasecmp (s, "min") == 0) {
5518     gst_value_set_fraction (dest, -G_MAXINT, 1);
5519     return TRUE;
5520   } else if (g_ascii_strcasecmp (s, "max") == 0) {
5521     gst_value_set_fraction (dest, G_MAXINT, 1);
5522     return TRUE;
5523   }
5524
5525   return FALSE;
5526 }
5527
5528 static void
5529 gst_value_transform_fraction_string (const GValue * src_value,
5530     GValue * dest_value)
5531 {
5532   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
5533 }
5534
5535 static void
5536 gst_value_transform_string_fraction (const GValue * src_value,
5537     GValue * dest_value)
5538 {
5539   if (!gst_value_deserialize_fraction (dest_value,
5540           src_value->data[0].v_pointer))
5541     /* If the deserialize fails, ensure we leave the fraction in a
5542      * valid, if incorrect, state */
5543     gst_value_set_fraction (dest_value, 0, 1);
5544 }
5545
5546 static void
5547 gst_value_transform_double_fraction (const GValue * src_value,
5548     GValue * dest_value)
5549 {
5550   gdouble src = g_value_get_double (src_value);
5551   gint n, d;
5552
5553   gst_util_double_to_fraction (src, &n, &d);
5554   gst_value_set_fraction (dest_value, n, d);
5555 }
5556
5557 static void
5558 gst_value_transform_float_fraction (const GValue * src_value,
5559     GValue * dest_value)
5560 {
5561   gfloat src = g_value_get_float (src_value);
5562   gint n, d;
5563
5564   gst_util_double_to_fraction (src, &n, &d);
5565   gst_value_set_fraction (dest_value, n, d);
5566 }
5567
5568 static void
5569 gst_value_transform_fraction_double (const GValue * src_value,
5570     GValue * dest_value)
5571 {
5572   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
5573       ((double) src_value->data[1].v_int);
5574 }
5575
5576 static void
5577 gst_value_transform_fraction_float (const GValue * src_value,
5578     GValue * dest_value)
5579 {
5580   dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
5581       ((float) src_value->data[1].v_int);
5582 }
5583
5584 static gint
5585 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
5586 {
5587   gint n1, n2;
5588   gint d1, d2;
5589   gint ret;
5590
5591   n1 = value1->data[0].v_int;
5592   n2 = value2->data[0].v_int;
5593   d1 = value1->data[1].v_int;
5594   d2 = value2->data[1].v_int;
5595
5596   /* fractions are reduced when set, so we can quickly see if they're equal */
5597   if (n1 == n2 && d1 == d2)
5598     return GST_VALUE_EQUAL;
5599
5600   if (d1 == 0 && d2 == 0)
5601     return GST_VALUE_UNORDERED;
5602   else if (d1 == 0)
5603     return GST_VALUE_GREATER_THAN;
5604   else if (d2 == 0)
5605     return GST_VALUE_LESS_THAN;
5606
5607   ret = gst_util_fraction_compare (n1, d1, n2, d2);
5608   if (ret == -1)
5609     return GST_VALUE_LESS_THAN;
5610   else if (ret == 1)
5611     return GST_VALUE_GREATER_THAN;
5612
5613   /* Equality can't happen here because we check for that
5614    * first already */
5615   g_return_val_if_reached (GST_VALUE_UNORDERED);
5616 }
5617
5618 /*********
5619  * GDate *
5620  *********/
5621
5622 static gint
5623 gst_value_compare_date (const GValue * value1, const GValue * value2)
5624 {
5625   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
5626   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
5627   guint32 j1, j2;
5628
5629   if (date1 == date2)
5630     return GST_VALUE_EQUAL;
5631
5632   if ((date1 == NULL || !g_date_valid (date1))
5633       && (date2 != NULL && g_date_valid (date2))) {
5634     return GST_VALUE_LESS_THAN;
5635   }
5636
5637   if ((date2 == NULL || !g_date_valid (date2))
5638       && (date1 != NULL && g_date_valid (date1))) {
5639     return GST_VALUE_GREATER_THAN;
5640   }
5641
5642   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
5643       || !g_date_valid (date2)) {
5644     return GST_VALUE_UNORDERED;
5645   }
5646
5647   j1 = g_date_get_julian (date1);
5648   j2 = g_date_get_julian (date2);
5649
5650   if (j1 == j2)
5651     return GST_VALUE_EQUAL;
5652   else if (j1 < j2)
5653     return GST_VALUE_LESS_THAN;
5654   else
5655     return GST_VALUE_GREATER_THAN;
5656 }
5657
5658 static gchar *
5659 gst_value_serialize_date (const GValue * val)
5660 {
5661   const GDate *date = (const GDate *) g_value_get_boxed (val);
5662
5663   if (date == NULL || !g_date_valid (date))
5664     return g_strdup ("9999-99-99");
5665
5666   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
5667       g_date_get_month (date), g_date_get_day (date));
5668 }
5669
5670 static gboolean
5671 gst_value_deserialize_date (GValue * dest, const gchar * s)
5672 {
5673   guint year, month, day;
5674
5675   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
5676     return FALSE;
5677
5678   if (!g_date_valid_dmy (day, month, year))
5679     return FALSE;
5680
5681   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
5682   return TRUE;
5683 }
5684
5685 /*************
5686  * GstDateTime *
5687  *************/
5688
5689 static gint
5690 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
5691 {
5692   const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
5693   const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
5694
5695   if (date1 == date2)
5696     return GST_VALUE_EQUAL;
5697
5698   if ((date1 == NULL) && (date2 != NULL)) {
5699     return GST_VALUE_LESS_THAN;
5700   }
5701   if ((date2 == NULL) && (date1 != NULL)) {
5702     return GST_VALUE_LESS_THAN;
5703   }
5704
5705   /* returns GST_VALUE_* */
5706   return __gst_date_time_compare (date1, date2);
5707 }
5708
5709 static gchar *
5710 gst_value_serialize_date_time (const GValue * val)
5711 {
5712   GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
5713
5714   if (date == NULL)
5715     return g_strdup ("null");
5716
5717   return __gst_date_time_serialize (date, TRUE);
5718 }
5719
5720 static gboolean
5721 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
5722 {
5723   GstDateTime *datetime;
5724
5725   if (!s || strcmp (s, "null") == 0) {
5726     return FALSE;
5727   }
5728
5729   datetime = gst_date_time_new_from_iso8601_string (s);
5730   if (datetime != NULL) {
5731     g_value_take_boxed (dest, datetime);
5732     return TRUE;
5733   }
5734   GST_WARNING ("Failed to deserialize date time string '%s'", s);
5735   return FALSE;
5736 }
5737
5738 static void
5739 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
5740 {
5741   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
5742 }
5743
5744 static void
5745 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
5746 {
5747   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
5748 }
5749
5750
5751 /************
5752  * bitmask *
5753  ************/
5754
5755 /* helper functions */
5756 static void
5757 gst_value_init_bitmask (GValue * value)
5758 {
5759   value->data[0].v_uint64 = 0;
5760 }
5761
5762 static void
5763 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
5764 {
5765   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5766 }
5767
5768 static gchar *
5769 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
5770     GTypeCValue * collect_values, guint collect_flags)
5771 {
5772   if (n_collect_values != 1)
5773     return g_strdup_printf ("not enough value locations for `%s' passed",
5774         G_VALUE_TYPE_NAME (value));
5775
5776   gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
5777
5778   return NULL;
5779 }
5780
5781 static gchar *
5782 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
5783     GTypeCValue * collect_values, guint collect_flags)
5784 {
5785   guint64 *bitmask = collect_values[0].v_pointer;
5786
5787   if (!bitmask)
5788     return g_strdup_printf ("value for `%s' passed as NULL",
5789         G_VALUE_TYPE_NAME (value));
5790
5791   *bitmask = value->data[0].v_uint64;
5792
5793   return NULL;
5794 }
5795
5796 /**
5797  * gst_value_set_bitmask:
5798  * @value: a GValue initialized to #GST_TYPE_BITMASK
5799  * @bitmask: the bitmask
5800  *
5801  * Sets @value to the bitmask specified by @bitmask.
5802  */
5803 void
5804 gst_value_set_bitmask (GValue * value, guint64 bitmask)
5805 {
5806   g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
5807
5808   value->data[0].v_uint64 = bitmask;
5809 }
5810
5811 /**
5812  * gst_value_get_bitmask:
5813  * @value: a GValue initialized to #GST_TYPE_BITMASK
5814  *
5815  * Gets the bitmask specified by @value.
5816  *
5817  * Returns: the bitmask.
5818  */
5819 guint64
5820 gst_value_get_bitmask (const GValue * value)
5821 {
5822   g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
5823
5824   return value->data[0].v_uint64;
5825 }
5826
5827 static gchar *
5828 gst_value_serialize_bitmask (const GValue * value)
5829 {
5830   guint64 bitmask = value->data[0].v_uint64;
5831
5832   return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
5833 }
5834
5835 static gboolean
5836 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
5837 {
5838   gchar *endptr = NULL;
5839   guint64 val;
5840
5841   if (G_UNLIKELY (s == NULL))
5842     return FALSE;
5843
5844   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
5845     return FALSE;
5846
5847   val = g_ascii_strtoull (s, &endptr, 16);
5848   if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
5849     return FALSE;
5850   if (val == 0 && endptr == s)
5851     return FALSE;
5852
5853   gst_value_set_bitmask (dest, val);
5854
5855   return TRUE;
5856 }
5857
5858 static void
5859 gst_value_transform_bitmask_string (const GValue * src_value,
5860     GValue * dest_value)
5861 {
5862   dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
5863 }
5864
5865 static void
5866 gst_value_transform_string_bitmask (const GValue * src_value,
5867     GValue * dest_value)
5868 {
5869   if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
5870     gst_value_set_bitmask (dest_value, 0);
5871 }
5872
5873 static void
5874 gst_value_transform_uint64_bitmask (const GValue * src_value,
5875     GValue * dest_value)
5876 {
5877   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5878 }
5879
5880 static void
5881 gst_value_transform_bitmask_uint64 (const GValue * src_value,
5882     GValue * dest_value)
5883 {
5884   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5885 }
5886
5887 static gint
5888 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
5889 {
5890   guint64 v1, v2;
5891
5892   v1 = value1->data[0].v_uint64;
5893   v2 = value2->data[0].v_uint64;
5894
5895   if (v1 == v2)
5896     return GST_VALUE_EQUAL;
5897
5898   return GST_VALUE_UNORDERED;
5899 }
5900
5901
5902 /***********************
5903  * GstAllocationParams *
5904  ***********************/
5905 static gint
5906 gst_value_compare_allocation_params (const GValue * value1,
5907     const GValue * value2)
5908 {
5909   GstAllocationParams *v1, *v2;
5910
5911   v1 = value1->data[0].v_pointer;
5912   v2 = value2->data[0].v_pointer;
5913
5914   if (v1 == NULL && v1 == v2)
5915     return GST_VALUE_EQUAL;
5916
5917   if (v1 == NULL || v2 == NULL)
5918     return GST_VALUE_UNORDERED;
5919
5920   if (v1->flags == v2->flags && v1->align == v2->align &&
5921       v1->prefix == v2->prefix && v1->padding == v2->padding)
5922     return GST_VALUE_EQUAL;
5923
5924   return GST_VALUE_UNORDERED;
5925 }
5926
5927
5928 /************
5929  * GObject *
5930  ************/
5931
5932 static gint
5933 gst_value_compare_object (const GValue * value1, const GValue * value2)
5934 {
5935   gpointer v1, v2;
5936
5937   v1 = value1->data[0].v_pointer;
5938   v2 = value2->data[0].v_pointer;
5939
5940   if (v1 == v2)
5941     return GST_VALUE_EQUAL;
5942
5943   return GST_VALUE_UNORDERED;
5944 }
5945
5946 static void
5947 gst_value_transform_object_string (const GValue * src_value,
5948     GValue * dest_value)
5949 {
5950   GstObject *obj;
5951   gchar *str;
5952
5953   obj = g_value_get_object (src_value);
5954   if (obj) {
5955     str =
5956         g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
5957         GST_OBJECT_NAME (obj));
5958   } else {
5959     str = g_strdup ("NULL");
5960   }
5961
5962   dest_value->data[0].v_pointer = str;
5963 }
5964
5965 static GTypeInfo _info = {
5966   0,
5967   NULL,
5968   NULL,
5969   NULL,
5970   NULL,
5971   NULL,
5972   0,
5973   0,
5974   NULL,
5975   NULL,
5976 };
5977
5978 static GTypeFundamentalInfo _finfo = {
5979   0
5980 };
5981
5982 #define FUNC_VALUE_GET_TYPE(type, name)                         \
5983 GType _gst_ ## type ## _type = 0;                               \
5984                                                                 \
5985 GType gst_ ## type ## _get_type (void)                          \
5986 {                                                               \
5987   static volatile GType gst_ ## type ## _type = 0;              \
5988                                                                 \
5989   if (g_once_init_enter (&gst_ ## type ## _type)) {             \
5990     GType _type;                                                \
5991     _info.value_table = & _gst_ ## type ## _value_table;        \
5992     _type = g_type_register_fundamental (                       \
5993         g_type_fundamental_next (),                             \
5994         name, &_info, &_finfo, 0);                              \
5995     _gst_ ## type ## _type = _type;                              \
5996     g_once_init_leave(&gst_ ## type ## _type, _type);           \
5997   }                                                             \
5998                                                                 \
5999   return gst_ ## type ## _type;                                 \
6000 }
6001
6002 static const GTypeValueTable _gst_int_range_value_table = {
6003   gst_value_init_int_range,
6004   NULL,
6005   gst_value_copy_int_range,
6006   NULL,
6007   (char *) "ii",
6008   gst_value_collect_int_range,
6009   (char *) "pp",
6010   gst_value_lcopy_int_range
6011 };
6012
6013 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
6014
6015 static const GTypeValueTable _gst_int64_range_value_table = {
6016   gst_value_init_int64_range,
6017   gst_value_free_int64_range,
6018   gst_value_copy_int64_range,
6019   NULL,
6020   (char *) "qq",
6021   gst_value_collect_int64_range,
6022   (char *) "pp",
6023   gst_value_lcopy_int64_range
6024 };
6025
6026 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
6027
6028 static const GTypeValueTable _gst_double_range_value_table = {
6029   gst_value_init_double_range,
6030   NULL,
6031   gst_value_copy_double_range,
6032   NULL,
6033   (char *) "dd",
6034   gst_value_collect_double_range,
6035   (char *) "pp",
6036   gst_value_lcopy_double_range
6037 };
6038
6039 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
6040
6041 static const GTypeValueTable _gst_fraction_range_value_table = {
6042   gst_value_init_fraction_range,
6043   gst_value_free_fraction_range,
6044   gst_value_copy_fraction_range,
6045   NULL,
6046   (char *) "iiii",
6047   gst_value_collect_fraction_range,
6048   (char *) "pppp",
6049   gst_value_lcopy_fraction_range
6050 };
6051
6052 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
6053
6054 static const GTypeValueTable _gst_value_list_value_table = {
6055   gst_value_init_list_or_array,
6056   gst_value_free_list_or_array,
6057   gst_value_copy_list_or_array,
6058   gst_value_list_or_array_peek_pointer,
6059   (char *) "p",
6060   gst_value_collect_list_or_array,
6061   (char *) "p",
6062   gst_value_lcopy_list_or_array
6063 };
6064
6065 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
6066
6067 static const GTypeValueTable _gst_value_array_value_table = {
6068   gst_value_init_list_or_array,
6069   gst_value_free_list_or_array,
6070   gst_value_copy_list_or_array,
6071   gst_value_list_or_array_peek_pointer,
6072   (char *) "p",
6073   gst_value_collect_list_or_array,
6074   (char *) "p",
6075   gst_value_lcopy_list_or_array
6076 };
6077
6078 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
6079
6080 static const GTypeValueTable _gst_fraction_value_table = {
6081   gst_value_init_fraction,
6082   NULL,
6083   gst_value_copy_fraction,
6084   NULL,
6085   (char *) "ii",
6086   gst_value_collect_fraction,
6087   (char *) "pp",
6088   gst_value_lcopy_fraction
6089 };
6090
6091 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
6092
6093 static const GTypeValueTable _gst_bitmask_value_table = {
6094   gst_value_init_bitmask,
6095   NULL,
6096   gst_value_copy_bitmask,
6097   NULL,
6098   (char *) "q",
6099   gst_value_collect_bitmask,
6100   (char *) "p",
6101   gst_value_lcopy_bitmask
6102 };
6103
6104 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
6105
6106 GType
6107 gst_g_thread_get_type (void)
6108 {
6109 #if GLIB_CHECK_VERSION(2,35,3)
6110   return G_TYPE_THREAD;
6111 #else
6112   static volatile gsize type_id = 0;
6113
6114   if (g_once_init_enter (&type_id)) {
6115     GType tmp =
6116         g_boxed_type_register_static (g_intern_static_string ("GstGThread"),
6117         (GBoxedCopyFunc) g_thread_ref,
6118         (GBoxedFreeFunc) g_thread_unref);
6119     g_once_init_leave (&type_id, tmp);
6120   }
6121
6122   return type_id;
6123 #endif
6124 }
6125
6126 void
6127 _priv_gst_value_initialize (void)
6128 {
6129   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
6130   gst_value_hash = g_hash_table_new (NULL, NULL);
6131   gst_value_union_funcs = g_array_new (FALSE, FALSE,
6132       sizeof (GstValueUnionInfo));
6133   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
6134       sizeof (GstValueIntersectInfo));
6135   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
6136       sizeof (GstValueSubtractInfo));
6137
6138   {
6139     static GstValueTable gst_value = {
6140       0,
6141       gst_value_compare_int_range,
6142       gst_value_serialize_int_range,
6143       gst_value_deserialize_int_range,
6144     };
6145
6146     gst_value.type = gst_int_range_get_type ();
6147     gst_value_register (&gst_value);
6148   }
6149
6150   {
6151     static GstValueTable gst_value = {
6152       0,
6153       gst_value_compare_int64_range,
6154       gst_value_serialize_int64_range,
6155       gst_value_deserialize_int64_range,
6156     };
6157
6158     gst_value.type = gst_int64_range_get_type ();
6159     gst_value_register (&gst_value);
6160   }
6161
6162   {
6163     static GstValueTable gst_value = {
6164       0,
6165       gst_value_compare_double_range,
6166       gst_value_serialize_double_range,
6167       gst_value_deserialize_double_range,
6168     };
6169
6170     gst_value.type = gst_double_range_get_type ();
6171     gst_value_register (&gst_value);
6172   }
6173
6174   {
6175     static GstValueTable gst_value = {
6176       0,
6177       gst_value_compare_fraction_range,
6178       gst_value_serialize_fraction_range,
6179       gst_value_deserialize_fraction_range,
6180     };
6181
6182     gst_value.type = gst_fraction_range_get_type ();
6183     gst_value_register (&gst_value);
6184   }
6185
6186   {
6187     static GstValueTable gst_value = {
6188       0,
6189       gst_value_compare_list,
6190       gst_value_serialize_list,
6191       gst_value_deserialize_list,
6192     };
6193
6194     gst_value.type = gst_value_list_get_type ();
6195     gst_value_register (&gst_value);
6196   }
6197
6198   {
6199     static GstValueTable gst_value = {
6200       0,
6201       gst_value_compare_array,
6202       gst_value_serialize_array,
6203       gst_value_deserialize_array,
6204     };
6205
6206     gst_value.type = gst_value_array_get_type ();
6207     gst_value_register (&gst_value);
6208   }
6209
6210   {
6211 #if 0
6212     static const GTypeValueTable value_table = {
6213       gst_value_init_buffer,
6214       NULL,
6215       gst_value_copy_buffer,
6216       NULL,
6217       "i",
6218       NULL,                     /*gst_value_collect_buffer, */
6219       "p",
6220       NULL                      /*gst_value_lcopy_buffer */
6221     };
6222 #endif
6223     static GstValueTable gst_value = {
6224       0,
6225       gst_value_compare_buffer,
6226       gst_value_serialize_buffer,
6227       gst_value_deserialize_buffer,
6228     };
6229
6230     gst_value.type = GST_TYPE_BUFFER;
6231     gst_value_register (&gst_value);
6232   }
6233   {
6234     static GstValueTable gst_value = {
6235       0,
6236       gst_value_compare_sample,
6237       gst_value_serialize_sample,
6238       gst_value_deserialize_sample,
6239     };
6240
6241     gst_value.type = GST_TYPE_SAMPLE;
6242     gst_value_register (&gst_value);
6243   }
6244   {
6245     static GstValueTable gst_value = {
6246       0,
6247       gst_value_compare_fraction,
6248       gst_value_serialize_fraction,
6249       gst_value_deserialize_fraction,
6250     };
6251
6252     gst_value.type = gst_fraction_get_type ();
6253     gst_value_register (&gst_value);
6254   }
6255   {
6256     static GstValueTable gst_value = {
6257       0,
6258       gst_value_compare_caps,
6259       gst_value_serialize_caps,
6260       gst_value_deserialize_caps,
6261     };
6262
6263     gst_value.type = GST_TYPE_CAPS;
6264     gst_value_register (&gst_value);
6265   }
6266   {
6267     static GstValueTable gst_value = {
6268       0,
6269       NULL,
6270       gst_value_serialize_segment,
6271       gst_value_deserialize_segment,
6272     };
6273
6274     gst_value.type = GST_TYPE_SEGMENT;
6275     gst_value_register (&gst_value);
6276   }
6277   {
6278     static GstValueTable gst_value = {
6279       0,
6280       NULL,
6281       gst_value_serialize_structure,
6282       gst_value_deserialize_structure,
6283     };
6284
6285     gst_value.type = GST_TYPE_STRUCTURE;
6286     gst_value_register (&gst_value);
6287   }
6288   {
6289     static GstValueTable gst_value = {
6290       0,
6291       NULL,
6292       gst_value_serialize_caps_features,
6293       gst_value_deserialize_caps_features,
6294     };
6295
6296     gst_value.type = GST_TYPE_CAPS_FEATURES;
6297     gst_value_register (&gst_value);
6298   }
6299   {
6300     static GstValueTable gst_value = {
6301       0,
6302       NULL,
6303       gst_value_serialize_tag_list,
6304       gst_value_deserialize_tag_list,
6305     };
6306
6307     gst_value.type = GST_TYPE_TAG_LIST;
6308     gst_value_register (&gst_value);
6309   }
6310   {
6311     static GstValueTable gst_value = {
6312       0,
6313       gst_value_compare_date,
6314       gst_value_serialize_date,
6315       gst_value_deserialize_date,
6316     };
6317
6318     gst_value.type = G_TYPE_DATE;
6319     gst_value_register (&gst_value);
6320   }
6321   {
6322     static GstValueTable gst_value = {
6323       0,
6324       gst_value_compare_date_time,
6325       gst_value_serialize_date_time,
6326       gst_value_deserialize_date_time,
6327     };
6328
6329     gst_value.type = gst_date_time_get_type ();
6330     gst_value_register (&gst_value);
6331   }
6332
6333   {
6334     static GstValueTable gst_value = {
6335       0,
6336       gst_value_compare_bitmask,
6337       gst_value_serialize_bitmask,
6338       gst_value_deserialize_bitmask,
6339     };
6340
6341     gst_value.type = gst_bitmask_get_type ();
6342     gst_value_register (&gst_value);
6343   }
6344
6345   {
6346     static GstValueTable gst_value = {
6347       0,
6348       gst_value_compare_allocation_params,
6349       NULL,
6350       NULL,
6351     };
6352
6353     gst_value.type = gst_allocation_params_get_type ();
6354     gst_value_register (&gst_value);
6355   }
6356
6357   {
6358     static GstValueTable gst_value = {
6359       0,
6360       gst_value_compare_object,
6361       NULL,
6362       NULL,
6363     };
6364
6365     gst_value.type = G_TYPE_OBJECT;
6366     gst_value_register (&gst_value);
6367   }
6368
6369   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
6370   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
6371
6372   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
6373   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
6374   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
6375
6376   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
6377
6378   REGISTER_SERIALIZATION (G_TYPE_INT, int);
6379
6380   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
6381   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
6382
6383   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
6384   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
6385   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
6386
6387   REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
6388
6389   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
6390       gst_value_transform_int_range_string);
6391   g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
6392       gst_value_transform_int64_range_string);
6393   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
6394       gst_value_transform_double_range_string);
6395   g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
6396       gst_value_transform_fraction_range_string);
6397   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
6398       gst_value_transform_list_string);
6399   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
6400       gst_value_transform_array_string);
6401   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
6402       gst_value_transform_fraction_string);
6403   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
6404       gst_value_transform_string_fraction);
6405   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
6406       gst_value_transform_fraction_double);
6407   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
6408       gst_value_transform_fraction_float);
6409   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
6410       gst_value_transform_double_fraction);
6411   g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
6412       gst_value_transform_float_fraction);
6413   g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
6414       gst_value_transform_date_string);
6415   g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
6416       gst_value_transform_string_date);
6417   g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
6418       gst_value_transform_object_string);
6419   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
6420       gst_value_transform_bitmask_uint64);
6421   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
6422       gst_value_transform_bitmask_string);
6423   g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
6424       gst_value_transform_uint64_bitmask);
6425   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
6426       gst_value_transform_string_bitmask);
6427
6428   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6429       gst_value_intersect_int_int_range);
6430   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6431       gst_value_intersect_int_range_int_range);
6432   gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6433       gst_value_intersect_int64_int64_range);
6434   gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6435       gst_value_intersect_int64_range_int64_range);
6436   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6437       gst_value_intersect_double_double_range);
6438   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
6439       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
6440   gst_value_register_intersect_func (GST_TYPE_ARRAY,
6441       GST_TYPE_ARRAY, gst_value_intersect_array);
6442   gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6443       gst_value_intersect_fraction_fraction_range);
6444   gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
6445       GST_TYPE_FRACTION_RANGE,
6446       gst_value_intersect_fraction_range_fraction_range);
6447
6448   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6449       gst_value_subtract_int_int_range);
6450   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
6451       gst_value_subtract_int_range_int);
6452   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6453       gst_value_subtract_int_range_int_range);
6454   gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6455       gst_value_subtract_int64_int64_range);
6456   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
6457       gst_value_subtract_int64_range_int64);
6458   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6459       gst_value_subtract_int64_range_int64_range);
6460   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6461       gst_value_subtract_double_double_range);
6462   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
6463       gst_value_subtract_double_range_double);
6464   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
6465       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
6466   gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6467       gst_value_subtract_fraction_fraction_range);
6468   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
6469       gst_value_subtract_fraction_range_fraction);
6470   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
6471       GST_TYPE_FRACTION_RANGE,
6472       gst_value_subtract_fraction_range_fraction_range);
6473
6474   /* see bug #317246, #64994, #65041 */
6475   {
6476     volatile GType date_type = G_TYPE_DATE;
6477
6478     g_type_name (date_type);
6479   }
6480
6481   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6482       gst_value_union_int_int_range);
6483   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6484       gst_value_union_int_range_int_range);
6485
6486 #if 0
6487   /* Implement these if needed */
6488   gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6489       gst_value_union_fraction_fraction_range);
6490   gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
6491       GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
6492 #endif
6493 }