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