gstvalue: Don't initialize arrays from variables
[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 typedef struct _GstValueUnionInfo GstValueUnionInfo;
49 struct _GstValueUnionInfo
50 {
51   GType type1;
52   GType type2;
53   GstValueUnionFunc func;
54 };
55
56 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
57 struct _GstValueIntersectInfo
58 {
59   GType type1;
60   GType type2;
61   GstValueIntersectFunc func;
62 };
63
64 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
65 struct _GstValueSubtractInfo
66 {
67   GType minuend;
68   GType subtrahend;
69   GstValueSubtractFunc func;
70 };
71
72 #define FUNDAMENTAL_TYPE_ID_MAX \
73     (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
74 #define FUNDAMENTAL_TYPE_ID(type) \
75     ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
76
77 #define VALUE_LIST_SIZE(v) (((GArray *) (v)->data[0].v_pointer)->len)
78 #define VALUE_LIST_GET_VALUE(v, index) ((const GValue *) &g_array_index ((GArray *) (v)->data[0].v_pointer, GValue, (index)))
79
80 static GArray *gst_value_table;
81 static GHashTable *gst_value_hash;
82 static GstValueTable *gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID_MAX + 1];
83 static GArray *gst_value_union_funcs;
84 static GArray *gst_value_intersect_funcs;
85 static GArray *gst_value_subtract_funcs;
86
87 /* Forward declarations */
88 static gchar *gst_value_serialize_fraction (const GValue * value);
89
90 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
91 static gint gst_value_compare_with_func (const GValue * value1,
92     const GValue * value2, GstValueCompareFunc compare);
93
94 static gchar *gst_string_wrap (const gchar * s);
95 static gchar *gst_string_take_and_wrap (gchar * s);
96 static gchar *gst_string_unwrap (const gchar * s);
97
98 static inline GstValueTable *
99 gst_value_hash_lookup_type (GType type)
100 {
101   if (G_LIKELY (G_TYPE_IS_FUNDAMENTAL (type)))
102     return gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)];
103   else
104     return g_hash_table_lookup (gst_value_hash, (gpointer) type);
105 }
106
107 static void
108 gst_value_hash_add_type (GType type, const GstValueTable * table)
109 {
110   if (G_TYPE_IS_FUNDAMENTAL (type))
111     gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)] = (gpointer) table;
112
113   g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table);
114 }
115
116 /********
117  * list *
118  ********/
119
120 /* two helper functions to serialize/stringify any type of list
121  * regular lists are done with { }, arrays with < >
122  */
123 static gchar *
124 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
125     const gchar * end)
126 {
127   guint i;
128   GArray *array = value->data[0].v_pointer;
129   GString *s;
130   GValue *v;
131   gchar *s_val;
132   guint alen = array->len;
133
134   /* estimate minimum string length to minimise re-allocs in GString */
135   s = g_string_sized_new (2 + (6 * alen) + 2);
136   g_string_append (s, begin);
137   for (i = 0; i < alen; i++) {
138     v = &g_array_index (array, GValue, i);
139     s_val = gst_value_serialize (v);
140     g_string_append (s, s_val);
141     g_free (s_val);
142     if (i < alen - 1) {
143       g_string_append_len (s, ", ", 2);
144     }
145   }
146   g_string_append (s, end);
147   return g_string_free (s, FALSE);
148 }
149
150 static void
151 gst_value_transform_any_list_string (const GValue * src_value,
152     GValue * dest_value, const gchar * begin, const gchar * end)
153 {
154   GValue *list_value;
155   GArray *array;
156   GString *s;
157   guint i;
158   gchar *list_s;
159   guint alen;
160
161   array = src_value->data[0].v_pointer;
162   alen = array->len;
163
164   /* estimate minimum string length to minimise re-allocs in GString */
165   s = g_string_sized_new (2 + (10 * alen) + 2);
166   g_string_append (s, begin);
167   for (i = 0; i < alen; i++) {
168     list_value = &g_array_index (array, GValue, i);
169
170     if (i != 0) {
171       g_string_append_len (s, ", ", 2);
172     }
173     list_s = g_strdup_value_contents (list_value);
174     g_string_append (s, list_s);
175     g_free (list_s);
176   }
177   g_string_append (s, end);
178
179   dest_value->data[0].v_pointer = g_string_free (s, FALSE);
180 }
181
182 /*
183  * helper function to see if a type is fixed. Is used internally here and
184  * there. Do not export, since it doesn't work for types where the content
185  * decides the fixedness (e.g. GST_TYPE_ARRAY).
186  */
187 static gboolean
188 gst_type_is_fixed (GType type)
189 {
190   /* the basic int, string, double types */
191   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
192     return TRUE;
193   }
194   /* our fundamental types that are certainly not fixed */
195   if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
196       type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
197     return FALSE;
198   }
199   /* other (boxed) types that are fixed */
200   if (type == GST_TYPE_BUFFER) {
201     return TRUE;
202   }
203   /* heavy checks */
204   if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
205       G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
206     return TRUE;
207   }
208
209   return FALSE;
210 }
211
212 /* GValue functions usable for both regular lists and arrays */
213 static void
214 gst_value_init_list_or_array (GValue * value)
215 {
216   value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
217 }
218
219 static GArray *
220 copy_garray_of_gstvalue (const GArray * src)
221 {
222   GArray *dest;
223   guint i, len;
224
225   len = src->len;
226   dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), len);
227   g_array_set_size (dest, len);
228   for (i = 0; i < len; i++) {
229     gst_value_init_and_copy (&g_array_index (dest, GValue, i),
230         &g_array_index (src, GValue, i));
231   }
232
233   return dest;
234 }
235
236 static void
237 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
238 {
239   dest_value->data[0].v_pointer =
240       copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
241 }
242
243 static void
244 gst_value_free_list_or_array (GValue * value)
245 {
246   guint i, len;
247   GArray *src = (GArray *) value->data[0].v_pointer;
248   len = src->len;
249
250   if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
251     for (i = 0; i < len; i++) {
252       g_value_unset (&g_array_index (src, GValue, i));
253     }
254     g_array_free (src, TRUE);
255   }
256 }
257
258 static gpointer
259 gst_value_list_or_array_peek_pointer (const GValue * value)
260 {
261   return value->data[0].v_pointer;
262 }
263
264 static gchar *
265 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
266     GTypeCValue * collect_values, guint collect_flags)
267 {
268   if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
269     value->data[0].v_pointer = collect_values[0].v_pointer;
270     value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
271   } else {
272     value->data[0].v_pointer =
273         copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
274   }
275   return NULL;
276 }
277
278 static gchar *
279 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
280     GTypeCValue * collect_values, guint collect_flags)
281 {
282   GArray **dest = collect_values[0].v_pointer;
283
284   if (!dest)
285     return g_strdup_printf ("value location for `%s' passed as NULL",
286         G_VALUE_TYPE_NAME (value));
287   if (!value->data[0].v_pointer)
288     return g_strdup_printf ("invalid value given for `%s'",
289         G_VALUE_TYPE_NAME (value));
290   if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
291     *dest = (GArray *) value->data[0].v_pointer;
292   } else {
293     *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
294   }
295   return NULL;
296 }
297
298 /**
299  * gst_value_list_append_value:
300  * @value: a #GValue of type #GST_TYPE_LIST
301  * @append_value: the value to append
302  *
303  * Appends @append_value to the GstValueList in @value.
304  */
305 void
306 gst_value_list_append_value (GValue * value, const GValue * append_value)
307 {
308   GValue val = { 0, };
309
310   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
311   g_return_if_fail (G_IS_VALUE (append_value));
312
313   gst_value_init_and_copy (&val, append_value);
314   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
315 }
316
317 /**
318  * gst_value_list_prepend_value:
319  * @value: a #GValue of type #GST_TYPE_LIST
320  * @prepend_value: the value to prepend
321  *
322  * Prepends @prepend_value to the GstValueList in @value.
323  */
324 void
325 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
326 {
327   GValue val = { 0, };
328
329   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
330   g_return_if_fail (G_IS_VALUE (prepend_value));
331
332   gst_value_init_and_copy (&val, prepend_value);
333   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
334 }
335
336 /**
337  * gst_value_list_concat:
338  * @dest: an uninitialized #GValue to take the result
339  * @value1: a #GValue
340  * @value2: a #GValue
341  *
342  * Concatenates copies of @value1 and @value2 into a list.  Values that are not
343  * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
344  * @dest will be initialized to the type #GST_TYPE_LIST.
345  */
346 void
347 gst_value_list_concat (GValue * dest, const GValue * value1,
348     const GValue * value2)
349 {
350   guint i, value1_length, value2_length;
351   GArray *array;
352
353   g_return_if_fail (dest != NULL);
354   g_return_if_fail (G_VALUE_TYPE (dest) == 0);
355   g_return_if_fail (G_IS_VALUE (value1));
356   g_return_if_fail (G_IS_VALUE (value2));
357
358   value1_length =
359       (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
360   value2_length =
361       (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
362   g_value_init (dest, GST_TYPE_LIST);
363   array = (GArray *) dest->data[0].v_pointer;
364   g_array_set_size (array, value1_length + value2_length);
365
366   if (GST_VALUE_HOLDS_LIST (value1)) {
367     for (i = 0; i < value1_length; i++) {
368       gst_value_init_and_copy (&g_array_index (array, GValue, i),
369           VALUE_LIST_GET_VALUE (value1, i));
370     }
371   } else {
372     gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
373   }
374
375   if (GST_VALUE_HOLDS_LIST (value2)) {
376     for (i = 0; i < value2_length; i++) {
377       gst_value_init_and_copy (&g_array_index (array, GValue,
378               i + value1_length), VALUE_LIST_GET_VALUE (value2, i));
379     }
380   } else {
381     gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
382         value2);
383   }
384 }
385
386 /**
387  * gst_value_list_get_size:
388  * @value: a #GValue of type #GST_TYPE_LIST
389  *
390  * Gets the number of values contained in @value.
391  *
392  * Returns: the number of values
393  */
394 guint
395 gst_value_list_get_size (const GValue * value)
396 {
397   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
398
399   return ((GArray *) value->data[0].v_pointer)->len;
400 }
401
402 /**
403  * gst_value_list_get_value:
404  * @value: a #GValue of type #GST_TYPE_LIST
405  * @index: index of value to get from the list
406  *
407  * Gets the value that is a member of the list contained in @value and
408  * has the index @index.
409  *
410  * Returns: the value at the given index
411  */
412 const GValue *
413 gst_value_list_get_value (const GValue * value, guint index)
414 {
415   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
416   g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
417
418   return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
419       GValue, index);
420 }
421
422 /**
423  * gst_value_array_append_value:
424  * @value: a #GValue of type #GST_TYPE_ARRAY
425  * @append_value: the value to append
426  *
427  * Appends @append_value to the GstValueArray in @value.
428  */
429 void
430 gst_value_array_append_value (GValue * value, const GValue * append_value)
431 {
432   GValue val = { 0, };
433
434   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
435   g_return_if_fail (G_IS_VALUE (append_value));
436
437   gst_value_init_and_copy (&val, append_value);
438   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
439 }
440
441 /**
442  * gst_value_array_prepend_value:
443  * @value: a #GValue of type #GST_TYPE_ARRAY
444  * @prepend_value: the value to prepend
445  *
446  * Prepends @prepend_value to the GstValueArray in @value.
447  */
448 void
449 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
450 {
451   GValue val = { 0, };
452
453   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
454   g_return_if_fail (G_IS_VALUE (prepend_value));
455
456   gst_value_init_and_copy (&val, prepend_value);
457   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
458 }
459
460 /**
461  * gst_value_array_get_size:
462  * @value: a #GValue of type #GST_TYPE_ARRAY
463  *
464  * Gets the number of values contained in @value.
465  *
466  * Returns: the number of values
467  */
468 guint
469 gst_value_array_get_size (const GValue * value)
470 {
471   g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
472
473   return ((GArray *) value->data[0].v_pointer)->len;
474 }
475
476 /**
477  * gst_value_array_get_value:
478  * @value: a #GValue of type #GST_TYPE_ARRAY
479  * @index: index of value to get from the array
480  *
481  * Gets the value that is a member of the array contained in @value and
482  * has the index @index.
483  *
484  * Returns: the value at the given index
485  */
486 const GValue *
487 gst_value_array_get_value (const GValue * value, guint index)
488 {
489   g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
490   g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
491
492   return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
493       GValue, index);
494 }
495
496 static void
497 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
498 {
499   gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
500 }
501
502 static void
503 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
504 {
505   gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
506 }
507
508 /* Do an unordered compare of the contents of a list */
509 static gint
510 gst_value_compare_list (const GValue * value1, const GValue * value2)
511 {
512   guint i, j;
513   GArray *array1 = value1->data[0].v_pointer;
514   GArray *array2 = value2->data[0].v_pointer;
515   GValue *v1;
516   GValue *v2;
517   gint len, to_remove;
518   guint8 *removed;
519   GstValueCompareFunc compare;
520
521   /* get length and do initial length check. */
522   len = array1->len;
523   if (len != array2->len)
524     return GST_VALUE_UNORDERED;
525
526   /* place to mark removed value indices of array2 */
527   removed = g_newa (guint8, len);
528   memset (removed, 0, len);
529   to_remove = len;
530
531   /* loop over array1, all items should be in array2. When we find an
532    * item in array2, remove it from array2 by marking it as removed */
533   for (i = 0; i < len; i++) {
534     v1 = &g_array_index (array1, GValue, i);
535     if ((compare = gst_value_get_compare_func (v1))) {
536       for (j = 0; j < len; j++) {
537         /* item is removed, we can skip it */
538         if (removed[j])
539           continue;
540         v2 = &g_array_index (array2, GValue, j);
541         if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
542           /* mark item as removed now that we found it in array2 and 
543            * decrement the number of remaining items in array2. */
544           removed[j] = 1;
545           to_remove--;
546           break;
547         }
548       }
549       /* item in array1 and not in array2, UNORDERED */
550       if (j == len)
551         return GST_VALUE_UNORDERED;
552     } else
553       return GST_VALUE_UNORDERED;
554   }
555   /* if not all items were removed, array2 contained something not in array1 */
556   if (to_remove != 0)
557     return GST_VALUE_UNORDERED;
558
559   /* arrays are equal */
560   return GST_VALUE_EQUAL;
561 }
562
563 /* Perform an ordered comparison of the contents of an array */
564 static gint
565 gst_value_compare_array (const GValue * value1, const GValue * value2)
566 {
567   guint i;
568   GArray *array1 = value1->data[0].v_pointer;
569   GArray *array2 = value2->data[0].v_pointer;
570   guint len = array1->len;
571   GValue *v1;
572   GValue *v2;
573
574   if (len != array2->len)
575     return GST_VALUE_UNORDERED;
576
577   for (i = 0; i < len; i++) {
578     v1 = &g_array_index (array1, GValue, i);
579     v2 = &g_array_index (array2, GValue, i);
580     if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
581       return GST_VALUE_UNORDERED;
582   }
583
584   return GST_VALUE_EQUAL;
585 }
586
587 static gchar *
588 gst_value_serialize_list (const GValue * value)
589 {
590   return gst_value_serialize_any_list (value, "{ ", " }");
591 }
592
593 static gboolean
594 gst_value_deserialize_list (GValue * dest, const gchar * s)
595 {
596   g_warning ("gst_value_deserialize_list: unimplemented");
597   return FALSE;
598 }
599
600 static gchar *
601 gst_value_serialize_array (const GValue * value)
602 {
603   return gst_value_serialize_any_list (value, "< ", " >");
604 }
605
606 static gboolean
607 gst_value_deserialize_array (GValue * dest, const gchar * s)
608 {
609   g_warning ("gst_value_deserialize_array: unimplemented");
610   return FALSE;
611 }
612
613 /**********
614  * fourcc *
615  **********/
616
617 static void
618 gst_value_init_fourcc (GValue * value)
619 {
620   value->data[0].v_int = 0;
621 }
622
623 static void
624 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
625 {
626   dest_value->data[0].v_int = src_value->data[0].v_int;
627 }
628
629 static gchar *
630 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
631     GTypeCValue * collect_values, guint collect_flags)
632 {
633   value->data[0].v_int = collect_values[0].v_int;
634
635   return NULL;
636 }
637
638 static gchar *
639 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
640     GTypeCValue * collect_values, guint collect_flags)
641 {
642   guint32 *fourcc_p = collect_values[0].v_pointer;
643
644   if (!fourcc_p)
645     return g_strdup_printf ("value location for `%s' passed as NULL",
646         G_VALUE_TYPE_NAME (value));
647
648   *fourcc_p = value->data[0].v_int;
649
650   return NULL;
651 }
652
653 /**
654  * gst_value_set_fourcc:
655  * @value: a GValue initialized to #GST_TYPE_FOURCC
656  * @fourcc: the #guint32 fourcc to set
657  *
658  * Sets @value to @fourcc.
659  */
660 void
661 gst_value_set_fourcc (GValue * value, guint32 fourcc)
662 {
663   g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
664
665   value->data[0].v_int = fourcc;
666 }
667
668 /**
669  * gst_value_get_fourcc:
670  * @value: a GValue initialized to #GST_TYPE_FOURCC
671  *
672  * Gets the #guint32 fourcc contained in @value.
673  *
674  * Returns: the #guint32 fourcc contained in @value.
675  */
676 guint32
677 gst_value_get_fourcc (const GValue * value)
678 {
679   g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
680
681   return value->data[0].v_int;
682 }
683
684 static void
685 gst_value_transform_fourcc_string (const GValue * src_value,
686     GValue * dest_value)
687 {
688   guint32 fourcc = src_value->data[0].v_int;
689   gchar fourcc_char[4];
690
691   fourcc_char[0] = (fourcc >> 0) & 0xff;
692   fourcc_char[1] = (fourcc >> 8) & 0xff;
693   fourcc_char[2] = (fourcc >> 16) & 0xff;
694   fourcc_char[3] = (fourcc >> 24) & 0xff;
695
696   if ((g_ascii_isalnum (fourcc_char[0]) || fourcc_char[0] == ' ') &&
697       (g_ascii_isalnum (fourcc_char[1]) || fourcc_char[1] == ' ') &&
698       (g_ascii_isalnum (fourcc_char[2]) || fourcc_char[2] == ' ') &&
699       (g_ascii_isalnum (fourcc_char[3]) || fourcc_char[3] == ' ')) {
700     dest_value->data[0].v_pointer =
701         g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
702   } else {
703     dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
704   }
705 }
706
707 static gint
708 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
709 {
710   if (value2->data[0].v_int == value1->data[0].v_int)
711     return GST_VALUE_EQUAL;
712   return GST_VALUE_UNORDERED;
713 }
714
715 static gchar *
716 gst_value_serialize_fourcc (const GValue * value)
717 {
718   guint32 fourcc = value->data[0].v_int;
719   gchar fourcc_char[4];
720
721   fourcc_char[0] = (fourcc >> 0) & 0xff;
722   fourcc_char[1] = (fourcc >> 8) & 0xff;
723   fourcc_char[2] = (fourcc >> 16) & 0xff;
724   fourcc_char[3] = (fourcc >> 24) & 0xff;
725
726   if ((g_ascii_isalnum (fourcc_char[0]) || fourcc_char[0] == ' ') &&
727       (g_ascii_isalnum (fourcc_char[1]) || fourcc_char[1] == ' ') &&
728       (g_ascii_isalnum (fourcc_char[2]) || fourcc_char[2] == ' ') &&
729       (g_ascii_isalnum (fourcc_char[3]) || fourcc_char[3] == ' ')) {
730     return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
731   } else {
732     return g_strdup_printf ("0x%08x", fourcc);
733   }
734 }
735
736 static gboolean
737 gst_value_deserialize_fourcc (GValue * dest, const gchar * s)
738 {
739   gboolean ret = FALSE;
740   guint32 fourcc = 0;
741   gchar *end;
742   gint l = strlen (s);
743
744   if (l == 4) {
745     fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
746     ret = TRUE;
747   } else if (l == 3) {
748     fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], ' ');
749     ret = TRUE;
750   } else if (l == 2) {
751     fourcc = GST_MAKE_FOURCC (s[0], s[1], ' ', ' ');
752     ret = TRUE;
753   } else if (l == 1) {
754     fourcc = GST_MAKE_FOURCC (s[0], ' ', ' ', ' ');
755     ret = TRUE;
756   } else if (g_ascii_isdigit (*s)) {
757     fourcc = strtoul (s, &end, 0);
758     if (*end == 0) {
759       ret = TRUE;
760     }
761   }
762   gst_value_set_fourcc (dest, fourcc);
763
764   return ret;
765 }
766
767 /*************
768  * int range *
769  *************/
770
771 static void
772 gst_value_init_int_range (GValue * value)
773 {
774   value->data[0].v_int = 0;
775   value->data[1].v_int = 0;
776 }
777
778 static void
779 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
780 {
781   dest_value->data[0].v_int = src_value->data[0].v_int;
782   dest_value->data[1].v_int = src_value->data[1].v_int;
783 }
784
785 static gchar *
786 gst_value_collect_int_range (GValue * value, guint n_collect_values,
787     GTypeCValue * collect_values, guint collect_flags)
788 {
789   value->data[0].v_int = collect_values[0].v_int;
790   value->data[1].v_int = collect_values[1].v_int;
791
792   return NULL;
793 }
794
795 static gchar *
796 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
797     GTypeCValue * collect_values, guint collect_flags)
798 {
799   guint32 *int_range_start = collect_values[0].v_pointer;
800   guint32 *int_range_end = collect_values[1].v_pointer;
801
802   if (!int_range_start)
803     return g_strdup_printf ("start value location for `%s' passed as NULL",
804         G_VALUE_TYPE_NAME (value));
805   if (!int_range_end)
806     return g_strdup_printf ("end value location for `%s' passed as NULL",
807         G_VALUE_TYPE_NAME (value));
808
809   *int_range_start = value->data[0].v_int;
810   *int_range_end = value->data[1].v_int;
811
812   return NULL;
813 }
814
815 /**
816  * gst_value_set_int_range:
817  * @value: a GValue initialized to GST_TYPE_INT_RANGE
818  * @start: the start of the range
819  * @end: the end of the range
820  *
821  * Sets @value to the range specified by @start and @end.
822  */
823 void
824 gst_value_set_int_range (GValue * value, gint start, gint end)
825 {
826   g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
827   g_return_if_fail (start < end);
828
829   value->data[0].v_int = start;
830   value->data[1].v_int = end;
831 }
832
833 /**
834  * gst_value_get_int_range_min:
835  * @value: a GValue initialized to GST_TYPE_INT_RANGE
836  *
837  * Gets the minimum of the range specified by @value.
838  *
839  * Returns: the minimum of the range
840  */
841 gint
842 gst_value_get_int_range_min (const GValue * value)
843 {
844   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
845
846   return value->data[0].v_int;
847 }
848
849 /**
850  * gst_value_get_int_range_max:
851  * @value: a GValue initialized to GST_TYPE_INT_RANGE
852  *
853  * Gets the maximum of the range specified by @value.
854  *
855  * Returns: the maxumum of the range
856  */
857 gint
858 gst_value_get_int_range_max (const GValue * value)
859 {
860   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
861
862   return value->data[1].v_int;
863 }
864
865 static void
866 gst_value_transform_int_range_string (const GValue * src_value,
867     GValue * dest_value)
868 {
869   dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
870       (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
871 }
872
873 static gint
874 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
875 {
876   if (value2->data[0].v_int == value1->data[0].v_int &&
877       value2->data[1].v_int == value1->data[1].v_int)
878     return GST_VALUE_EQUAL;
879   return GST_VALUE_UNORDERED;
880 }
881
882 static gchar *
883 gst_value_serialize_int_range (const GValue * value)
884 {
885   return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
886       value->data[1].v_int);
887 }
888
889 static gboolean
890 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
891 {
892   g_warning ("unimplemented");
893   return FALSE;
894 }
895
896 /****************
897  * double range *
898  ****************/
899
900 static void
901 gst_value_init_double_range (GValue * value)
902 {
903   value->data[0].v_double = 0;
904   value->data[1].v_double = 0;
905 }
906
907 static void
908 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
909 {
910   dest_value->data[0].v_double = src_value->data[0].v_double;
911   dest_value->data[1].v_double = src_value->data[1].v_double;
912 }
913
914 static gchar *
915 gst_value_collect_double_range (GValue * value, guint n_collect_values,
916     GTypeCValue * collect_values, guint collect_flags)
917 {
918   value->data[0].v_double = collect_values[0].v_double;
919   value->data[1].v_double = collect_values[1].v_double;
920
921   return NULL;
922 }
923
924 static gchar *
925 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
926     GTypeCValue * collect_values, guint collect_flags)
927 {
928   gdouble *double_range_start = collect_values[0].v_pointer;
929   gdouble *double_range_end = collect_values[1].v_pointer;
930
931   if (!double_range_start)
932     return g_strdup_printf ("start value location for `%s' passed as NULL",
933         G_VALUE_TYPE_NAME (value));
934   if (!double_range_end)
935     return g_strdup_printf ("end value location for `%s' passed as NULL",
936         G_VALUE_TYPE_NAME (value));
937
938   *double_range_start = value->data[0].v_double;
939   *double_range_end = value->data[1].v_double;
940
941   return NULL;
942 }
943
944 /**
945  * gst_value_set_double_range:
946  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
947  * @start: the start of the range
948  * @end: the end of the range
949  *
950  * Sets @value to the range specified by @start and @end.
951  */
952 void
953 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
954 {
955   g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
956   g_return_if_fail (start < end);
957
958   value->data[0].v_double = start;
959   value->data[1].v_double = end;
960 }
961
962 /**
963  * gst_value_get_double_range_min:
964  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
965  *
966  * Gets the minimum of the range specified by @value.
967  *
968  * Returns: the minimum of the range
969  */
970 gdouble
971 gst_value_get_double_range_min (const GValue * value)
972 {
973   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
974
975   return value->data[0].v_double;
976 }
977
978 /**
979  * gst_value_get_double_range_max:
980  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
981  *
982  * Gets the maximum of the range specified by @value.
983  *
984  * Returns: the maxumum of the range
985  */
986 gdouble
987 gst_value_get_double_range_max (const GValue * value)
988 {
989   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
990
991   return value->data[1].v_double;
992 }
993
994 static void
995 gst_value_transform_double_range_string (const GValue * src_value,
996     GValue * dest_value)
997 {
998   gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
999
1000   dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1001       g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1002           src_value->data[0].v_double),
1003       g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1004           src_value->data[1].v_double));
1005 }
1006
1007 static gint
1008 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1009 {
1010   if (value2->data[0].v_double == value1->data[0].v_double &&
1011       value2->data[0].v_double == value1->data[0].v_double)
1012     return GST_VALUE_EQUAL;
1013   return GST_VALUE_UNORDERED;
1014 }
1015
1016 static gchar *
1017 gst_value_serialize_double_range (const GValue * value)
1018 {
1019   gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1020   gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1021
1022   g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1023   g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1024   return g_strdup_printf ("[ %s, %s ]", d1, d2);
1025 }
1026
1027 static gboolean
1028 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1029 {
1030   g_warning ("unimplemented");
1031   return FALSE;
1032 }
1033
1034 /****************
1035  * fraction range *
1036  ****************/
1037
1038 static void
1039 gst_value_init_fraction_range (GValue * value)
1040 {
1041   GValue *vals;
1042   GType ftype;
1043
1044   ftype = GST_TYPE_FRACTION;
1045
1046   value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1047   g_value_init (&vals[0], ftype);
1048   g_value_init (&vals[1], ftype);
1049 }
1050
1051 static void
1052 gst_value_free_fraction_range (GValue * value)
1053 {
1054   GValue *vals = (GValue *) value->data[0].v_pointer;
1055
1056   if (vals != NULL) {
1057     g_value_unset (&vals[0]);
1058     g_value_unset (&vals[1]);
1059     g_slice_free1 (2 * sizeof (GValue), vals);
1060     value->data[0].v_pointer = NULL;
1061   }
1062 }
1063
1064 static void
1065 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1066 {
1067   GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1068   GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1069
1070   if (vals == NULL) {
1071     gst_value_init_fraction_range (dest_value);
1072     vals = dest_value->data[0].v_pointer;
1073   }
1074   if (src_vals != NULL) {
1075     g_value_copy (&src_vals[0], &vals[0]);
1076     g_value_copy (&src_vals[1], &vals[1]);
1077   }
1078 }
1079
1080 static gchar *
1081 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1082     GTypeCValue * collect_values, guint collect_flags)
1083 {
1084   GValue *vals = (GValue *) value->data[0].v_pointer;
1085
1086   if (n_collect_values != 4)
1087     return g_strdup_printf ("not enough value locations for `%s' passed",
1088         G_VALUE_TYPE_NAME (value));
1089   if (vals == NULL) {
1090     gst_value_init_fraction_range (value);
1091     vals = value->data[0].v_pointer;
1092   }
1093
1094   gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1095       collect_values[1].v_int);
1096   gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1097       collect_values[3].v_int);
1098
1099   return NULL;
1100 }
1101
1102 static gchar *
1103 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1104     GTypeCValue * collect_values, guint collect_flags)
1105 {
1106   gint i;
1107   gint *dest_values[4];
1108   GValue *vals = (GValue *) value->data[0].v_pointer;
1109
1110   if (G_UNLIKELY (n_collect_values != 4))
1111     return g_strdup_printf ("not enough value locations for `%s' passed",
1112         G_VALUE_TYPE_NAME (value));
1113
1114   for (i = 0; i < 4; i++) {
1115     if (G_UNLIKELY (collect_values[i].v_pointer == NULL)) {
1116       return g_strdup_printf ("value location for `%s' passed as NULL",
1117           G_VALUE_TYPE_NAME (value));
1118     }
1119     dest_values[i] = collect_values[i].v_pointer;
1120   }
1121
1122   if (G_UNLIKELY (vals == NULL)) {
1123     return g_strdup_printf ("Uninitialised `%s' passed",
1124         G_VALUE_TYPE_NAME (value));
1125   }
1126
1127   dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1128   dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1129   dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1130   dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1131   return NULL;
1132 }
1133
1134 /**
1135  * gst_value_set_fraction_range:
1136  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1137  * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1138  * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1139  *
1140  * Sets @value to the range specified by @start and @end.
1141  */
1142 void
1143 gst_value_set_fraction_range (GValue * value, const GValue * start,
1144     const GValue * end)
1145 {
1146   GValue *vals;
1147
1148   g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1149
1150   vals = (GValue *) value->data[0].v_pointer;
1151   if (vals == NULL) {
1152     gst_value_init_fraction_range (value);
1153     vals = value->data[0].v_pointer;
1154   }
1155   g_value_copy (start, &vals[0]);
1156   g_value_copy (end, &vals[1]);
1157 }
1158
1159 /**
1160  * gst_value_set_fraction_range_full:
1161  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1162  * @numerator_start: the numerator start of the range
1163  * @denominator_start: the denominator start of the range
1164  * @numerator_end: the numerator end of the range
1165  * @denominator_end: the denominator end of the range
1166  *
1167  * Sets @value to the range specified by @numerator_start/@denominator_start
1168  * and @numerator_end/@denominator_end.
1169  */
1170 void
1171 gst_value_set_fraction_range_full (GValue * value,
1172     gint numerator_start, gint denominator_start,
1173     gint numerator_end, gint denominator_end)
1174 {
1175   GValue start = { 0 };
1176   GValue end = { 0 };
1177
1178   g_return_if_fail (value != NULL);
1179   g_return_if_fail (denominator_start != 0);
1180   g_return_if_fail (denominator_end != 0);
1181
1182   g_value_init (&start, GST_TYPE_FRACTION);
1183   g_value_init (&end, GST_TYPE_FRACTION);
1184
1185   gst_value_set_fraction (&start, numerator_start, denominator_start);
1186   gst_value_set_fraction (&end, numerator_end, denominator_end);
1187   gst_value_set_fraction_range (value, &start, &end);
1188
1189   g_value_unset (&start);
1190   g_value_unset (&end);
1191 }
1192
1193 /**
1194  * gst_value_get_fraction_range_min:
1195  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1196  *
1197  * Gets the minimum of the range specified by @value.
1198  *
1199  * Returns: the minimum of the range
1200  */
1201 const GValue *
1202 gst_value_get_fraction_range_min (const GValue * value)
1203 {
1204   GValue *vals;
1205
1206   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1207
1208   vals = (GValue *) value->data[0].v_pointer;
1209   if (vals != NULL) {
1210     return &vals[0];
1211   }
1212
1213   return NULL;
1214 }
1215
1216 /**
1217  * gst_value_get_fraction_range_max:
1218  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1219  *
1220  * Gets the maximum of the range specified by @value.
1221  *
1222  * Returns: the maximum of the range
1223  */
1224 const GValue *
1225 gst_value_get_fraction_range_max (const GValue * value)
1226 {
1227   GValue *vals;
1228
1229   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1230
1231   vals = (GValue *) value->data[0].v_pointer;
1232   if (vals != NULL) {
1233     return &vals[1];
1234   }
1235
1236   return NULL;
1237 }
1238
1239 static gchar *
1240 gst_value_serialize_fraction_range (const GValue * value)
1241 {
1242   GValue *vals = (GValue *) value->data[0].v_pointer;
1243   gchar *retval;
1244
1245   if (vals == NULL) {
1246     retval = g_strdup ("[ 0/1, 0/1 ]");
1247   } else {
1248     gchar *start, *end;
1249
1250     start = gst_value_serialize_fraction (&vals[0]);
1251     end = gst_value_serialize_fraction (&vals[1]);
1252
1253     retval = g_strdup_printf ("[ %s, %s ]", start, end);
1254     g_free (start);
1255     g_free (end);
1256   }
1257
1258   return retval;
1259 }
1260
1261 static void
1262 gst_value_transform_fraction_range_string (const GValue * src_value,
1263     GValue * dest_value)
1264 {
1265   dest_value->data[0].v_pointer =
1266       gst_value_serialize_fraction_range (src_value);
1267 }
1268
1269 static gint
1270 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1271 {
1272   GValue *vals1, *vals2;
1273   GstValueCompareFunc compare;
1274
1275   if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1276     return GST_VALUE_EQUAL;     /* Only possible if both are NULL */
1277
1278   if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1279     return GST_VALUE_UNORDERED;
1280
1281   vals1 = (GValue *) value1->data[0].v_pointer;
1282   vals2 = (GValue *) value2->data[0].v_pointer;
1283   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1284     if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1285         GST_VALUE_EQUAL &&
1286         gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1287         GST_VALUE_EQUAL)
1288       return GST_VALUE_EQUAL;
1289   }
1290   return GST_VALUE_UNORDERED;
1291 }
1292
1293 static gboolean
1294 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1295 {
1296   g_warning ("unimplemented");
1297   return FALSE;
1298 }
1299
1300 /***********
1301  * GstCaps *
1302  ***********/
1303
1304 /**
1305  * gst_value_set_caps:
1306  * @value: a GValue initialized to GST_TYPE_CAPS
1307  * @caps: the caps to set the value to
1308  *
1309  * Sets the contents of @value to @caps.  The actual
1310  * #GstCaps structure is copied before it is used.
1311  */
1312 void
1313 gst_value_set_caps (GValue * value, const GstCaps * caps)
1314 {
1315   g_return_if_fail (G_IS_VALUE (value));
1316   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1317   g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
1318
1319   g_value_set_boxed (value, caps);
1320 }
1321
1322 /**
1323  * gst_value_get_caps:
1324  * @value: a GValue initialized to GST_TYPE_CAPS
1325  *
1326  * Gets the contents of @value.
1327  *
1328  * Returns: the contents of @value
1329  */
1330 const GstCaps *
1331 gst_value_get_caps (const GValue * value)
1332 {
1333   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1334   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1335
1336   return (GstCaps *) g_value_get_boxed (value);
1337 }
1338
1339 static gchar *
1340 gst_value_serialize_caps (const GValue * value)
1341 {
1342   GstCaps *caps = g_value_get_boxed (value);
1343
1344   return gst_caps_to_string (caps);
1345 }
1346
1347 static gboolean
1348 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1349 {
1350   GstCaps *caps;
1351
1352   caps = gst_caps_from_string (s);
1353
1354   if (caps) {
1355     g_value_take_boxed (dest, caps);
1356     return TRUE;
1357   }
1358   return FALSE;
1359 }
1360
1361 /****************
1362  * GstStructure *
1363  ****************/
1364
1365 /**
1366  * gst_value_set_structure:
1367  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1368  * @structure: the structure to set the value to
1369  *
1370  * Sets the contents of @value to @structure.  The actual
1371  *
1372  * Since: 0.10.15
1373  */
1374 void
1375 gst_value_set_structure (GValue * value, const GstStructure * structure)
1376 {
1377   g_return_if_fail (G_IS_VALUE (value));
1378   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1379   g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
1380
1381   g_value_set_boxed (value, structure);
1382 }
1383
1384 /**
1385  * gst_value_get_structure:
1386  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1387  *
1388  * Gets the contents of @value.
1389  *
1390  * Returns: the contents of @value
1391  *
1392  * Since: 0.10.15
1393  */
1394 const GstStructure *
1395 gst_value_get_structure (const GValue * value)
1396 {
1397   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1398   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1399
1400   return (GstStructure *) g_value_get_boxed (value);
1401 }
1402
1403 static gchar *
1404 gst_value_serialize_structure (const GValue * value)
1405 {
1406   GstStructure *structure = g_value_get_boxed (value);
1407
1408   return gst_string_take_and_wrap (gst_structure_to_string (structure));
1409 }
1410
1411 static gboolean
1412 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1413 {
1414   GstStructure *structure;
1415
1416   if (*s != '"') {
1417     structure = gst_structure_from_string (s, NULL);
1418   } else {
1419     gchar *str = gst_string_unwrap (s);
1420
1421     if (G_UNLIKELY (!str))
1422       return FALSE;
1423
1424     structure = gst_structure_from_string (str, NULL);
1425     g_free (str);
1426   }
1427
1428   if (G_LIKELY (structure)) {
1429     g_value_take_boxed (dest, structure);
1430     return TRUE;
1431   }
1432   return FALSE;
1433 }
1434
1435 /*************
1436  * GstBuffer *
1437  *************/
1438
1439 static gint
1440 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1441 {
1442   GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
1443   GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
1444
1445   if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
1446     return GST_VALUE_UNORDERED;
1447   if (GST_BUFFER_SIZE (buf1) == 0)
1448     return GST_VALUE_EQUAL;
1449   g_assert (GST_BUFFER_DATA (buf1));
1450   g_assert (GST_BUFFER_DATA (buf2));
1451   if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
1452           GST_BUFFER_SIZE (buf1)) == 0)
1453     return GST_VALUE_EQUAL;
1454
1455   return GST_VALUE_UNORDERED;
1456 }
1457
1458 static gchar *
1459 gst_value_serialize_buffer (const GValue * value)
1460 {
1461   guint8 *data;
1462   gint i;
1463   gint size;
1464   gchar *string;
1465   GstBuffer *buffer;
1466
1467   buffer = gst_value_get_buffer (value);
1468   if (buffer == NULL)
1469     return NULL;
1470
1471   data = GST_BUFFER_DATA (buffer);
1472   size = GST_BUFFER_SIZE (buffer);
1473
1474   string = g_malloc (size * 2 + 1);
1475   for (i = 0; i < size; i++) {
1476     sprintf (string + i * 2, "%02x", data[i]);
1477   }
1478   string[size * 2] = 0;
1479
1480   return string;
1481 }
1482
1483 static gboolean
1484 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1485 {
1486   GstBuffer *buffer;
1487   gint len;
1488   gchar ts[3];
1489   guint8 *data;
1490   gint i;
1491
1492   len = strlen (s);
1493   if (len & 1)
1494     goto wrong_length;
1495
1496   buffer = gst_buffer_new_and_alloc (len / 2);
1497   data = GST_BUFFER_DATA (buffer);
1498   for (i = 0; i < len / 2; i++) {
1499     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1500       goto wrong_char;
1501
1502     ts[0] = s[i * 2 + 0];
1503     ts[1] = s[i * 2 + 1];
1504     ts[2] = 0;
1505
1506     data[i] = (guint8) strtoul (ts, NULL, 16);
1507   }
1508
1509   gst_value_take_buffer (dest, buffer);
1510
1511   return TRUE;
1512
1513   /* ERRORS */
1514 wrong_length:
1515   {
1516     return FALSE;
1517   }
1518 wrong_char:
1519   {
1520     gst_buffer_unref (buffer);
1521     return FALSE;
1522   }
1523 }
1524
1525
1526 /***********
1527  * boolean *
1528  ***********/
1529
1530 static gint
1531 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1532 {
1533   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1534     return GST_VALUE_EQUAL;
1535   return GST_VALUE_UNORDERED;
1536 }
1537
1538 static gchar *
1539 gst_value_serialize_boolean (const GValue * value)
1540 {
1541   if (value->data[0].v_int) {
1542     return g_strdup ("true");
1543   }
1544   return g_strdup ("false");
1545 }
1546
1547 static gboolean
1548 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1549 {
1550   gboolean ret = FALSE;
1551
1552   if (g_ascii_strcasecmp (s, "true") == 0 ||
1553       g_ascii_strcasecmp (s, "yes") == 0 ||
1554       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1555     g_value_set_boolean (dest, TRUE);
1556     ret = TRUE;
1557   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1558       g_ascii_strcasecmp (s, "no") == 0 ||
1559       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1560     g_value_set_boolean (dest, FALSE);
1561     ret = TRUE;
1562   }
1563
1564   return ret;
1565 }
1566
1567 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
1568 static gint                                                             \
1569 gst_value_compare_ ## _type                                             \
1570 (const GValue * value1, const GValue * value2)                          \
1571 {                                                                       \
1572   g ## _type val1 = g_value_get_ ## _type (value1);                     \
1573   g ## _type val2 = g_value_get_ ## _type (value2);                     \
1574   if (val1 > val2)                                                      \
1575     return GST_VALUE_GREATER_THAN;                                      \
1576   if (val1 < val2)                                                      \
1577     return GST_VALUE_LESS_THAN;                                         \
1578   return GST_VALUE_EQUAL;                                               \
1579 }                                                                       \
1580                                                                         \
1581 static gchar *                                                          \
1582 gst_value_serialize_ ## _type (const GValue * value)                    \
1583 {                                                                       \
1584   GValue val = { 0, };                                                  \
1585   g_value_init (&val, G_TYPE_STRING);                                   \
1586   if (!g_value_transform (value, &val))                                 \
1587     g_assert_not_reached ();                                            \
1588   /* NO_COPY_MADNESS!!! */                                              \
1589   return (char *) g_value_get_string (&val);                            \
1590 }
1591
1592 /* deserialize the given s into to as a gint64.
1593  * check if the result is actually storeable in the given size number of
1594  * bytes.
1595  */
1596 static gboolean
1597 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1598     gint64 min, gint64 max, gint size)
1599 {
1600   gboolean ret = FALSE;
1601   gchar *end;
1602   gint64 mask = -1;
1603
1604   errno = 0;
1605   *to = g_ascii_strtoull (s, &end, 0);
1606   /* a range error is a definitive no-no */
1607   if (errno == ERANGE) {
1608     return FALSE;
1609   }
1610
1611   if (*end == 0) {
1612     ret = TRUE;
1613   } else {
1614     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1615       *to = G_LITTLE_ENDIAN;
1616       ret = TRUE;
1617     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1618       *to = G_BIG_ENDIAN;
1619       ret = TRUE;
1620     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1621       *to = G_BYTE_ORDER;
1622       ret = TRUE;
1623     } else if (g_ascii_strcasecmp (s, "min") == 0) {
1624       *to = min;
1625       ret = TRUE;
1626     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1627       *to = max;
1628       ret = TRUE;
1629     }
1630   }
1631   if (ret) {
1632     /* by definition, a gint64 fits into a gint64; so ignore those */
1633     if (size != sizeof (mask)) {
1634       if (*to >= 0) {
1635         /* for positive numbers, we create a mask of 1's outside of the range
1636          * and 0's inside the range.  An and will thus keep only 1 bits
1637          * outside of the range */
1638         mask <<= (size * 8);
1639         if ((mask & *to) != 0) {
1640           ret = FALSE;
1641         }
1642       } else {
1643         /* for negative numbers, we do a 2's complement version */
1644         mask <<= ((size * 8) - 1);
1645         if ((mask & *to) != mask) {
1646           ret = FALSE;
1647         }
1648       }
1649     }
1650   }
1651   return ret;
1652 }
1653
1654 #define CREATE_SERIALIZATION(_type,_macro)                              \
1655 CREATE_SERIALIZATION_START(_type,_macro)                                \
1656                                                                         \
1657 static gboolean                                                         \
1658 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
1659 {                                                                       \
1660   gint64 x;                                                             \
1661                                                                         \
1662   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
1663       G_MAX ## _macro, sizeof (g ## _type))) {                          \
1664     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
1665     return TRUE;                                                        \
1666   } else {                                                              \
1667     return FALSE;                                                       \
1668   }                                                                     \
1669 }
1670
1671 #define CREATE_USERIALIZATION(_type,_macro)                             \
1672 CREATE_SERIALIZATION_START(_type,_macro)                                \
1673                                                                         \
1674 static gboolean                                                         \
1675 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
1676 {                                                                       \
1677   gint64 x;                                                             \
1678   gchar *end;                                                           \
1679   gboolean ret = FALSE;                                                 \
1680                                                                         \
1681   errno = 0;                                                            \
1682   x = g_ascii_strtoull (s, &end, 0);                                    \
1683   /* a range error is a definitive no-no */                             \
1684   if (errno == ERANGE) {                                                \
1685     return FALSE;                                                       \
1686   }                                                                     \
1687   /* the cast ensures the range check later on makes sense */           \
1688   x = (g ## _type) x;                                                   \
1689   if (*end == 0) {                                                      \
1690     ret = TRUE;                                                         \
1691   } else {                                                              \
1692     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
1693       x = G_LITTLE_ENDIAN;                                              \
1694       ret = TRUE;                                                       \
1695     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
1696       x = G_BIG_ENDIAN;                                                 \
1697       ret = TRUE;                                                       \
1698     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
1699       x = G_BYTE_ORDER;                                                 \
1700       ret = TRUE;                                                       \
1701     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
1702       x = 0;                                                            \
1703       ret = TRUE;                                                       \
1704     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
1705       x = G_MAX ## _macro;                                              \
1706       ret = TRUE;                                                       \
1707     }                                                                   \
1708   }                                                                     \
1709   if (ret) {                                                            \
1710     if (x > G_MAX ## _macro) {                                          \
1711       ret = FALSE;                                                      \
1712     } else {                                                            \
1713       g_value_set_ ## _type (dest, x);                                  \
1714     }                                                                   \
1715   }                                                                     \
1716   return ret;                                                           \
1717 }
1718
1719 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
1720 G_STMT_START {                                                          \
1721   static const GstValueTable gst_value = {                              \
1722     _gtype,                                                             \
1723     gst_value_compare_ ## _type,                                        \
1724     gst_value_serialize_ ## _type,                                      \
1725     gst_value_deserialize_ ## _type,                                    \
1726   };                                                                    \
1727                                                                         \
1728   gst_value_register (&gst_value);                                      \
1729 } G_STMT_END
1730
1731 CREATE_SERIALIZATION (int, INT);
1732 CREATE_SERIALIZATION (int64, INT64);
1733 CREATE_SERIALIZATION (long, LONG);
1734
1735 CREATE_USERIALIZATION (uint, UINT);
1736 CREATE_USERIALIZATION (uint64, UINT64);
1737 CREATE_USERIALIZATION (ulong, ULONG);
1738
1739 /**********
1740  * double *
1741  **********/
1742 static gint
1743 gst_value_compare_double (const GValue * value1, const GValue * value2)
1744 {
1745   if (value1->data[0].v_double > value2->data[0].v_double)
1746     return GST_VALUE_GREATER_THAN;
1747   if (value1->data[0].v_double < value2->data[0].v_double)
1748     return GST_VALUE_LESS_THAN;
1749   if (value1->data[0].v_double == value2->data[0].v_double)
1750     return GST_VALUE_EQUAL;
1751   return GST_VALUE_UNORDERED;
1752 }
1753
1754 static gchar *
1755 gst_value_serialize_double (const GValue * value)
1756 {
1757   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
1758
1759   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1760   return g_strdup (d);
1761 }
1762
1763 static gboolean
1764 gst_value_deserialize_double (GValue * dest, const gchar * s)
1765 {
1766   gdouble x;
1767   gboolean ret = FALSE;
1768   gchar *end;
1769
1770   x = g_ascii_strtod (s, &end);
1771   if (*end == 0) {
1772     ret = TRUE;
1773   } else {
1774     if (g_ascii_strcasecmp (s, "min") == 0) {
1775       x = -G_MAXDOUBLE;
1776       ret = TRUE;
1777     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1778       x = G_MAXDOUBLE;
1779       ret = TRUE;
1780     }
1781   }
1782   if (ret) {
1783     g_value_set_double (dest, x);
1784   }
1785   return ret;
1786 }
1787
1788 /*********
1789  * float *
1790  *********/
1791
1792 static gint
1793 gst_value_compare_float (const GValue * value1, const GValue * value2)
1794 {
1795   if (value1->data[0].v_float > value2->data[0].v_float)
1796     return GST_VALUE_GREATER_THAN;
1797   if (value1->data[0].v_float < value2->data[0].v_float)
1798     return GST_VALUE_LESS_THAN;
1799   if (value1->data[0].v_float == value2->data[0].v_float)
1800     return GST_VALUE_EQUAL;
1801   return GST_VALUE_UNORDERED;
1802 }
1803
1804 static gchar *
1805 gst_value_serialize_float (const GValue * value)
1806 {
1807   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
1808
1809   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1810   return g_strdup (d);
1811 }
1812
1813 static gboolean
1814 gst_value_deserialize_float (GValue * dest, const gchar * s)
1815 {
1816   gdouble x;
1817   gboolean ret = FALSE;
1818   gchar *end;
1819
1820   x = g_ascii_strtod (s, &end);
1821   if (*end == 0) {
1822     ret = TRUE;
1823   } else {
1824     if (g_ascii_strcasecmp (s, "min") == 0) {
1825       x = -G_MAXFLOAT;
1826       ret = TRUE;
1827     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1828       x = G_MAXFLOAT;
1829       ret = TRUE;
1830     }
1831   }
1832   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1833     ret = FALSE;
1834   if (ret) {
1835     g_value_set_float (dest, (float) x);
1836   }
1837   return ret;
1838 }
1839
1840 /**********
1841  * string *
1842  **********/
1843
1844 static gint
1845 gst_value_compare_string (const GValue * value1, const GValue * value2)
1846 {
1847   if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
1848     /* if only one is NULL, no match - otherwise both NULL == EQUAL */
1849     if (value1->data[0].v_pointer != value2->data[0].v_pointer)
1850       return GST_VALUE_UNORDERED;
1851   } else {
1852     gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1853
1854     if (x < 0)
1855       return GST_VALUE_LESS_THAN;
1856     if (x > 0)
1857       return GST_VALUE_GREATER_THAN;
1858   }
1859
1860   return GST_VALUE_EQUAL;
1861 }
1862
1863 static gint
1864 gst_string_measure_wrapping (const gchar * s)
1865 {
1866   gint len;
1867   gboolean wrap = FALSE;
1868
1869   if (G_UNLIKELY (s == NULL))
1870     return -1;
1871
1872   /* Special case: the actual string NULL needs wrapping */
1873   if (G_UNLIKELY (strcmp (s, "NULL") == 0))
1874     return 4;
1875
1876   len = 0;
1877   while (*s) {
1878     if (GST_ASCII_IS_STRING (*s)) {
1879       len++;
1880     } else if (*s < 0x20 || *s >= 0x7f) {
1881       wrap = TRUE;
1882       len += 4;
1883     } else {
1884       wrap = TRUE;
1885       len += 2;
1886     }
1887     s++;
1888   }
1889
1890   /* Wrap the string if we found something that needs
1891    * wrapping, or the empty string (len == 0) */
1892   return (wrap || len == 0) ? len : -1;
1893 }
1894
1895 static gchar *
1896 gst_string_wrap_inner (const gchar * s, gint len)
1897 {
1898   gchar *d, *e;
1899
1900   e = d = g_malloc (len + 3);
1901
1902   *e++ = '\"';
1903   while (*s) {
1904     if (GST_ASCII_IS_STRING (*s)) {
1905       *e++ = *s++;
1906     } else if (*s < 0x20 || *s >= 0x7f) {
1907       *e++ = '\\';
1908       *e++ = '0' + ((*(guchar *) s) >> 6);
1909       *e++ = '0' + (((*s) >> 3) & 0x7);
1910       *e++ = '0' + ((*s++) & 0x7);
1911     } else {
1912       *e++ = '\\';
1913       *e++ = *s++;
1914     }
1915   }
1916   *e++ = '\"';
1917   *e = 0;
1918
1919   g_assert (e - d <= len + 3);
1920   return d;
1921 }
1922
1923 /* Do string wrapping/escaping */
1924 static gchar *
1925 gst_string_wrap (const gchar * s)
1926 {
1927   gint len = gst_string_measure_wrapping (s);
1928
1929   if (G_LIKELY (len < 0))
1930     return g_strdup (s);
1931
1932   return gst_string_wrap_inner (s, len);
1933 }
1934
1935 /* Same as above, but take ownership of the string */
1936 static gchar *
1937 gst_string_take_and_wrap (gchar * s)
1938 {
1939   gchar *out;
1940   gint len = gst_string_measure_wrapping (s);
1941
1942   if (G_LIKELY (len < 0))
1943     return s;
1944
1945   out = gst_string_wrap_inner (s, len);
1946   g_free (s);
1947
1948   return out;
1949 }
1950
1951 /*
1952  * This function takes a string delimited with double quotes (")
1953  * and unescapes any \xxx octal numbers.
1954  *
1955  * If sequences of \y are found where y is not in the range of
1956  * 0->3, y is copied unescaped.
1957  *
1958  * If \xyy is found where x is an octal number but y is not, an
1959  * error is encountered and NULL is returned.
1960  *
1961  * the input string must be \0 terminated.
1962  */
1963 static gchar *
1964 gst_string_unwrap (const gchar * s)
1965 {
1966   gchar *ret;
1967   gchar *read, *write;
1968
1969   /* NULL string returns NULL */
1970   if (s == NULL)
1971     return NULL;
1972
1973   /* strings not starting with " are invalid */
1974   if (*s != '"')
1975     return NULL;
1976
1977   /* make copy of original string to hold the result. This
1978    * string will always be smaller than the original */
1979   ret = g_strdup (s);
1980   read = ret;
1981   write = ret;
1982
1983   /* need to move to the next position as we parsed the " */
1984   read++;
1985
1986   while (*read) {
1987     if (GST_ASCII_IS_STRING (*read)) {
1988       /* normal chars are just copied */
1989       *write++ = *read++;
1990     } else if (*read == '"') {
1991       /* quote marks end of string */
1992       break;
1993     } else if (*read == '\\') {
1994       /* got an escape char, move to next position to read a tripplet
1995        * of octal numbers */
1996       read++;
1997       /* is the next char a possible first octal number? */
1998       if (*read >= '0' && *read <= '3') {
1999         /* parse other 2 numbers, if one of them is not in the range of
2000          * an octal number, we error. We also catch the case where a zero
2001          * byte is found here. */
2002         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2003           goto beach;
2004
2005         /* now convert the octal number to a byte again. */
2006         *write++ = ((read[0] - '0') << 6) +
2007             ((read[1] - '0') << 3) + (read[2] - '0');
2008
2009         read += 3;
2010       } else {
2011         /* if we run into a \0 here, we definately won't get a quote later */
2012         if (*read == 0)
2013           goto beach;
2014
2015         /* else copy \X sequence */
2016         *write++ = *read++;
2017       }
2018     } else {
2019       /* weird character, error */
2020       goto beach;
2021     }
2022   }
2023   /* if the string is not ending in " and zero terminated, we error */
2024   if (*read != '"' || read[1] != '\0')
2025     goto beach;
2026
2027   /* null terminate result string and return */
2028   *write = '\0';
2029   return ret;
2030
2031 beach:
2032   g_free (ret);
2033   return NULL;
2034 }
2035
2036 static gchar *
2037 gst_value_serialize_string (const GValue * value)
2038 {
2039   return gst_string_wrap (value->data[0].v_pointer);
2040 }
2041
2042 static gboolean
2043 gst_value_deserialize_string (GValue * dest, const gchar * s)
2044 {
2045   if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2046     g_value_set_string (dest, NULL);
2047     return TRUE;
2048   } else if (G_LIKELY (*s != '"')) {
2049     if (!g_utf8_validate (s, -1, NULL))
2050       return FALSE;
2051     g_value_set_string (dest, s);
2052     return TRUE;
2053   } else {
2054     gchar *str = gst_string_unwrap (s);
2055     if (G_UNLIKELY (!str))
2056       return FALSE;
2057     g_value_take_string (dest, str);
2058   }
2059
2060   return TRUE;
2061 }
2062
2063 /********
2064  * enum *
2065  ********/
2066
2067 static gint
2068 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2069 {
2070   GEnumValue *en1, *en2;
2071   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2072   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2073
2074   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2075   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2076   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2077   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2078   g_type_class_unref (klass1);
2079   g_type_class_unref (klass2);
2080   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2081   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2082   if (en1->value < en2->value)
2083     return GST_VALUE_LESS_THAN;
2084   if (en1->value > en2->value)
2085     return GST_VALUE_GREATER_THAN;
2086
2087   return GST_VALUE_EQUAL;
2088 }
2089
2090 static gchar *
2091 gst_value_serialize_enum (const GValue * value)
2092 {
2093   GEnumValue *en;
2094   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
2095
2096   g_return_val_if_fail (klass, NULL);
2097   en = g_enum_get_value (klass, g_value_get_enum (value));
2098   g_type_class_unref (klass);
2099
2100   /* might be one of the custom formats registered later */
2101   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
2102     const GstFormatDefinition *format_def;
2103
2104     format_def = gst_format_get_details (g_value_get_enum (value));
2105     g_return_val_if_fail (format_def != NULL, NULL);
2106     return g_strdup (format_def->description);
2107   }
2108
2109   g_return_val_if_fail (en, NULL);
2110   return g_strdup (en->value_name);
2111 }
2112
2113 static gint
2114 gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
2115     const gchar * s)
2116 {
2117   if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2118     return 0;
2119
2120   return g_ascii_strcasecmp (s, format_def->description);
2121 }
2122
2123 static gboolean
2124 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2125 {
2126   GEnumValue *en;
2127   gchar *endptr = NULL;
2128   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2129
2130   g_return_val_if_fail (klass, FALSE);
2131   if (!(en = g_enum_get_value_by_name (klass, s))) {
2132     if (!(en = g_enum_get_value_by_nick (klass, s))) {
2133       gint i = strtol (s, &endptr, 0);
2134
2135       if (endptr && *endptr == '\0') {
2136         en = g_enum_get_value (klass, i);
2137       }
2138     }
2139   }
2140   g_type_class_unref (klass);
2141
2142   /* might be one of the custom formats registered later */
2143   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2144     const GstFormatDefinition *format_def;
2145     GstIterator *iter;
2146
2147     iter = gst_format_iterate_definitions ();
2148
2149     format_def = gst_iterator_find_custom (iter,
2150         (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
2151
2152     g_return_val_if_fail (format_def != NULL, FALSE);
2153     g_value_set_enum (dest, (gint) format_def->value);
2154     gst_iterator_free (iter);
2155     return TRUE;
2156   }
2157
2158   g_return_val_if_fail (en, FALSE);
2159   g_value_set_enum (dest, en->value);
2160   return TRUE;
2161 }
2162
2163 /********
2164  * flags *
2165  ********/
2166
2167 /* we just compare the value here */
2168 static gint
2169 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2170 {
2171   guint fl1, fl2;
2172   GFlagsClass *klass1 =
2173       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2174   GFlagsClass *klass2 =
2175       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2176
2177   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2178   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2179   fl1 = g_value_get_flags (value1);
2180   fl2 = g_value_get_flags (value2);
2181   g_type_class_unref (klass1);
2182   g_type_class_unref (klass2);
2183   if (fl1 < fl2)
2184     return GST_VALUE_LESS_THAN;
2185   if (fl1 > fl2)
2186     return GST_VALUE_GREATER_THAN;
2187
2188   return GST_VALUE_EQUAL;
2189 }
2190
2191 /* the different flags are serialized separated with a + */
2192 static gchar *
2193 gst_value_serialize_flags (const GValue * value)
2194 {
2195   guint flags;
2196   GFlagsValue *fl;
2197   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2198   gchar *result, *tmp;
2199   gboolean first = TRUE;
2200
2201   g_return_val_if_fail (klass, NULL);
2202
2203   flags = g_value_get_flags (value);
2204
2205   /* if no flags are set, try to serialize to the _NONE string */
2206   if (!flags) {
2207     fl = g_flags_get_first_value (klass, flags);
2208     return g_strdup (fl->value_name);
2209   }
2210
2211   /* some flags are set, so serialize one by one */
2212   result = g_strdup ("");
2213   while (flags) {
2214     fl = g_flags_get_first_value (klass, flags);
2215     if (fl != NULL) {
2216       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2217       g_free (result);
2218       result = tmp;
2219       first = FALSE;
2220
2221       /* clear flag */
2222       flags &= ~fl->value;
2223     }
2224   }
2225   g_type_class_unref (klass);
2226
2227   return result;
2228 }
2229
2230 static gboolean
2231 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2232 {
2233   GFlagsValue *fl;
2234   gchar *endptr = NULL;
2235   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2236   gchar **split;
2237   guint flags;
2238   gint i;
2239
2240   g_return_val_if_fail (klass, FALSE);
2241
2242   /* split into parts delimited with + */
2243   split = g_strsplit (s, "+", 0);
2244
2245   flags = 0;
2246   i = 0;
2247   /* loop over each part */
2248   while (split[i]) {
2249     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2250       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2251         gint val = strtol (split[i], &endptr, 0);
2252
2253         /* just or numeric value */
2254         if (endptr && *endptr == '\0') {
2255           flags |= val;
2256         }
2257       }
2258     }
2259     if (fl) {
2260       flags |= fl->value;
2261     }
2262     i++;
2263   }
2264   g_strfreev (split);
2265   g_type_class_unref (klass);
2266   g_value_set_flags (dest, flags);
2267
2268   return TRUE;
2269 }
2270
2271 /*********
2272  * union *
2273  *********/
2274
2275 static gboolean
2276 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2277     const GValue * src2)
2278 {
2279   if (src2->data[0].v_int <= src1->data[0].v_int &&
2280       src2->data[1].v_int >= src1->data[0].v_int) {
2281     gst_value_init_and_copy (dest, src2);
2282     return TRUE;
2283   }
2284   return FALSE;
2285 }
2286
2287 static gboolean
2288 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2289     const GValue * src2)
2290 {
2291   gint min;
2292   gint max;
2293
2294   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2295   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2296
2297   if (min <= max) {
2298     g_value_init (dest, GST_TYPE_INT_RANGE);
2299     gst_value_set_int_range (dest,
2300         MIN (src1->data[0].v_int, src2->data[0].v_int),
2301         MAX (src1->data[1].v_int, src2->data[1].v_int));
2302     return TRUE;
2303   }
2304
2305   return FALSE;
2306 }
2307
2308 /****************
2309  * intersection *
2310  ****************/
2311
2312 static gboolean
2313 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2314     const GValue * src2)
2315 {
2316   if (src2->data[0].v_int <= src1->data[0].v_int &&
2317       src2->data[1].v_int >= src1->data[0].v_int) {
2318     gst_value_init_and_copy (dest, src1);
2319     return TRUE;
2320   }
2321
2322   return FALSE;
2323 }
2324
2325 static gboolean
2326 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2327     const GValue * src2)
2328 {
2329   gint min;
2330   gint max;
2331
2332   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2333   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2334
2335   if (min < max) {
2336     g_value_init (dest, GST_TYPE_INT_RANGE);
2337     gst_value_set_int_range (dest, min, max);
2338     return TRUE;
2339   }
2340   if (min == max) {
2341     g_value_init (dest, G_TYPE_INT);
2342     g_value_set_int (dest, min);
2343     return TRUE;
2344   }
2345
2346   return FALSE;
2347 }
2348
2349 static gboolean
2350 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2351     const GValue * src2)
2352 {
2353   if (src2->data[0].v_double <= src1->data[0].v_double &&
2354       src2->data[1].v_double >= src1->data[0].v_double) {
2355     gst_value_init_and_copy (dest, src1);
2356     return TRUE;
2357   }
2358
2359   return FALSE;
2360 }
2361
2362 static gboolean
2363 gst_value_intersect_double_range_double_range (GValue * dest,
2364     const GValue * src1, const GValue * src2)
2365 {
2366   gdouble min;
2367   gdouble max;
2368
2369   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2370   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2371
2372   if (min < max) {
2373     g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2374     gst_value_set_double_range (dest, min, max);
2375     return TRUE;
2376   }
2377   if (min == max) {
2378     g_value_init (dest, G_TYPE_DOUBLE);
2379     g_value_set_int (dest, (int) min);
2380     return TRUE;
2381   }
2382
2383   return FALSE;
2384 }
2385
2386 static gboolean
2387 gst_value_intersect_list (GValue * dest, const GValue * value1,
2388     const GValue * value2)
2389 {
2390   guint i, size;
2391   GValue intersection = { 0, };
2392   gboolean ret = FALSE;
2393
2394   size = VALUE_LIST_SIZE (value1);
2395   for (i = 0; i < size; i++) {
2396     const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
2397
2398     if (gst_value_intersect (&intersection, cur, value2)) {
2399       /* append value */
2400       if (!ret) {
2401         gst_value_init_and_copy (dest, &intersection);
2402         ret = TRUE;
2403       } else if (GST_VALUE_HOLDS_LIST (dest)) {
2404         gst_value_list_append_value (dest, &intersection);
2405       } else {
2406         GValue temp = { 0, };
2407
2408         gst_value_init_and_copy (&temp, dest);
2409         g_value_unset (dest);
2410         gst_value_list_concat (dest, &temp, &intersection);
2411         g_value_unset (&temp);
2412       }
2413       g_value_unset (&intersection);
2414     }
2415   }
2416
2417   return ret;
2418 }
2419
2420 static gboolean
2421 gst_value_intersect_array (GValue * dest, const GValue * src1,
2422     const GValue * src2)
2423 {
2424   guint size;
2425   guint n;
2426   GValue val = { 0 };
2427
2428   /* only works on similar-sized arrays */
2429   size = gst_value_array_get_size (src1);
2430   if (size != gst_value_array_get_size (src2))
2431     return FALSE;
2432   g_value_init (dest, GST_TYPE_ARRAY);
2433
2434   for (n = 0; n < size; n++) {
2435     if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2436             gst_value_array_get_value (src2, n))) {
2437       g_value_unset (dest);
2438       return FALSE;
2439     }
2440     gst_value_array_append_value (dest, &val);
2441     g_value_unset (&val);
2442   }
2443
2444   return TRUE;
2445 }
2446
2447 static gboolean
2448 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2449     const GValue * src2)
2450 {
2451   gint res1, res2;
2452   GValue *vals;
2453   GstValueCompareFunc compare;
2454
2455   vals = src2->data[0].v_pointer;
2456
2457   if (vals == NULL)
2458     return FALSE;
2459
2460   if ((compare = gst_value_get_compare_func (src1))) {
2461     res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2462     res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2463
2464     if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2465         (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2466       gst_value_init_and_copy (dest, src1);
2467       return TRUE;
2468     }
2469   }
2470
2471   return FALSE;
2472 }
2473
2474 static gboolean
2475 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
2476     const GValue * src1, const GValue * src2)
2477 {
2478   GValue *min;
2479   GValue *max;
2480   gint res;
2481   GValue *vals1, *vals2;
2482   GstValueCompareFunc compare;
2483
2484   vals1 = src1->data[0].v_pointer;
2485   vals2 = src2->data[0].v_pointer;
2486   g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2487
2488   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2489     /* min = MAX (src1.start, src2.start) */
2490     res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2491     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2492     if (res == GST_VALUE_LESS_THAN)
2493       min = &vals2[0];          /* Take the max of the 2 */
2494     else
2495       min = &vals1[0];
2496
2497     /* max = MIN (src1.end, src2.end) */
2498     res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2499     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2500     if (res == GST_VALUE_GREATER_THAN)
2501       max = &vals2[1];          /* Take the min of the 2 */
2502     else
2503       max = &vals1[1];
2504
2505     res = gst_value_compare_with_func (min, max, compare);
2506     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2507     if (res == GST_VALUE_LESS_THAN) {
2508       g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2509       vals1 = dest->data[0].v_pointer;
2510       g_value_copy (min, &vals1[0]);
2511       g_value_copy (max, &vals1[1]);
2512       return TRUE;
2513     }
2514     if (res == GST_VALUE_EQUAL) {
2515       gst_value_init_and_copy (dest, min);
2516       return TRUE;
2517     }
2518   }
2519
2520   return FALSE;
2521 }
2522
2523 /***************
2524  * subtraction *
2525  ***************/
2526
2527 static gboolean
2528 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2529     const GValue * subtrahend)
2530 {
2531   gint min = gst_value_get_int_range_min (subtrahend);
2532   gint max = gst_value_get_int_range_max (subtrahend);
2533   gint val = g_value_get_int (minuend);
2534
2535   /* subtracting a range from an int only works if the int is not in the
2536    * range */
2537   if (val < min || val > max) {
2538     /* and the result is the int */
2539     gst_value_init_and_copy (dest, minuend);
2540     return TRUE;
2541   }
2542   return FALSE;
2543 }
2544
2545 /* creates a new int range based on input values.
2546  */
2547 static gboolean
2548 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2549     gint max2)
2550 {
2551   GValue v1 = { 0, };
2552   GValue v2 = { 0, };
2553   GValue *pv1, *pv2;            /* yeah, hungarian! */
2554
2555   if (min1 <= max1 && min2 <= max2) {
2556     pv1 = &v1;
2557     pv2 = &v2;
2558   } else if (min1 <= max1) {
2559     pv1 = dest;
2560     pv2 = NULL;
2561   } else if (min2 <= max2) {
2562     pv1 = NULL;
2563     pv2 = dest;
2564   } else {
2565     return FALSE;
2566   }
2567
2568   if (min1 < max1) {
2569     g_value_init (pv1, GST_TYPE_INT_RANGE);
2570     gst_value_set_int_range (pv1, min1, max1);
2571   } else if (min1 == max1) {
2572     g_value_init (pv1, G_TYPE_INT);
2573     g_value_set_int (pv1, min1);
2574   }
2575   if (min2 < max2) {
2576     g_value_init (pv2, GST_TYPE_INT_RANGE);
2577     gst_value_set_int_range (pv2, min2, max2);
2578   } else if (min2 == max2) {
2579     g_value_init (pv2, G_TYPE_INT);
2580     g_value_set_int (pv2, min2);
2581   }
2582
2583   if (min1 <= max1 && min2 <= max2) {
2584     gst_value_list_concat (dest, pv1, pv2);
2585     g_value_unset (pv1);
2586     g_value_unset (pv2);
2587   }
2588   return TRUE;
2589 }
2590
2591 static gboolean
2592 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2593     const GValue * subtrahend)
2594 {
2595   gint min = gst_value_get_int_range_min (minuend);
2596   gint max = gst_value_get_int_range_max (minuend);
2597   gint val = g_value_get_int (subtrahend);
2598
2599   g_return_val_if_fail (min < max, FALSE);
2600
2601   /* value is outside of the range, return range unchanged */
2602   if (val < min || val > max) {
2603     gst_value_init_and_copy (dest, minuend);
2604     return TRUE;
2605   } else {
2606     /* max must be MAXINT too as val <= max */
2607     if (val == G_MAXINT) {
2608       max--;
2609       val--;
2610     }
2611     /* min must be MININT too as val >= max */
2612     if (val == G_MININT) {
2613       min++;
2614       val++;
2615     }
2616     gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2617   }
2618   return TRUE;
2619 }
2620
2621 static gboolean
2622 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2623     const GValue * subtrahend)
2624 {
2625   gint min1 = gst_value_get_int_range_min (minuend);
2626   gint max1 = gst_value_get_int_range_max (minuend);
2627   gint min2 = gst_value_get_int_range_min (subtrahend);
2628   gint max2 = gst_value_get_int_range_max (subtrahend);
2629
2630   if (max2 == G_MAXINT && min2 == G_MININT) {
2631     return FALSE;
2632   } else if (max2 == G_MAXINT) {
2633     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
2634   } else if (min2 == G_MININT) {
2635     return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
2636   } else {
2637     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
2638         MAX (max2 + 1, min1), max1);
2639   }
2640 }
2641
2642 static gboolean
2643 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
2644     const GValue * subtrahend)
2645 {
2646   gdouble min = gst_value_get_double_range_min (subtrahend);
2647   gdouble max = gst_value_get_double_range_max (subtrahend);
2648   gdouble val = g_value_get_double (minuend);
2649
2650   if (val < min || val > max) {
2651     gst_value_init_and_copy (dest, minuend);
2652     return TRUE;
2653   }
2654   return FALSE;
2655 }
2656
2657 static gboolean
2658 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
2659     const GValue * subtrahend)
2660 {
2661   /* since we don't have open ranges, we cannot create a hole in
2662    * a double range. We return the original range */
2663   gst_value_init_and_copy (dest, minuend);
2664   return TRUE;
2665 }
2666
2667 static gboolean
2668 gst_value_subtract_double_range_double_range (GValue * dest,
2669     const GValue * minuend, const GValue * subtrahend)
2670 {
2671   /* since we don't have open ranges, we have to approximate */
2672   /* done like with ints */
2673   gdouble min1 = gst_value_get_double_range_min (minuend);
2674   gdouble max2 = gst_value_get_double_range_max (minuend);
2675   gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
2676   gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
2677   GValue v1 = { 0, };
2678   GValue v2 = { 0, };
2679   GValue *pv1, *pv2;            /* yeah, hungarian! */
2680
2681   if (min1 < max1 && min2 < max2) {
2682     pv1 = &v1;
2683     pv2 = &v2;
2684   } else if (min1 < max1) {
2685     pv1 = dest;
2686     pv2 = NULL;
2687   } else if (min2 < max2) {
2688     pv1 = NULL;
2689     pv2 = dest;
2690   } else {
2691     return FALSE;
2692   }
2693
2694   if (min1 < max1) {
2695     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
2696     gst_value_set_double_range (pv1, min1, max1);
2697   }
2698   if (min2 < max2) {
2699     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
2700     gst_value_set_double_range (pv2, min2, max2);
2701   }
2702
2703   if (min1 < max1 && min2 < max2) {
2704     gst_value_list_concat (dest, pv1, pv2);
2705     g_value_unset (pv1);
2706     g_value_unset (pv2);
2707   }
2708   return TRUE;
2709 }
2710
2711 static gboolean
2712 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
2713     const GValue * subtrahend)
2714 {
2715   guint i, size;
2716   GValue subtraction = { 0, };
2717   gboolean ret = FALSE;
2718   GType ltype;
2719
2720   ltype = gst_value_list_get_type ();
2721
2722   size = VALUE_LIST_SIZE (minuend);
2723   for (i = 0; i < size; i++) {
2724     const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
2725
2726     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2727       if (!ret) {
2728         gst_value_init_and_copy (dest, &subtraction);
2729         ret = TRUE;
2730       } else if (G_VALUE_HOLDS (dest, ltype)
2731           && !G_VALUE_HOLDS (&subtraction, ltype)) {
2732         gst_value_list_append_value (dest, &subtraction);
2733       } else {
2734         GValue temp = { 0, };
2735
2736         gst_value_init_and_copy (&temp, dest);
2737         g_value_unset (dest);
2738         gst_value_list_concat (dest, &temp, &subtraction);
2739         g_value_unset (&temp);
2740       }
2741       g_value_unset (&subtraction);
2742     }
2743   }
2744   return ret;
2745 }
2746
2747 static gboolean
2748 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2749     const GValue * subtrahend)
2750 {
2751   guint i, size;
2752   GValue data[2] = { {0,}, {0,} };
2753   GValue *subtraction = &data[0], *result = &data[1];
2754
2755   gst_value_init_and_copy (result, minuend);
2756   size = VALUE_LIST_SIZE (subtrahend);
2757   for (i = 0; i < size; i++) {
2758     const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
2759
2760     if (gst_value_subtract (subtraction, result, cur)) {
2761       GValue *temp = result;
2762
2763       result = subtraction;
2764       subtraction = temp;
2765       g_value_unset (subtraction);
2766     } else {
2767       g_value_unset (result);
2768       return FALSE;
2769     }
2770   }
2771   gst_value_init_and_copy (dest, result);
2772   g_value_unset (result);
2773   return TRUE;
2774 }
2775
2776 static gboolean
2777 gst_value_subtract_fraction_fraction_range (GValue * dest,
2778     const GValue * minuend, const GValue * subtrahend)
2779 {
2780   const GValue *min = gst_value_get_fraction_range_min (subtrahend);
2781   const GValue *max = gst_value_get_fraction_range_max (subtrahend);
2782   GstValueCompareFunc compare;
2783
2784   if ((compare = gst_value_get_compare_func (minuend))) {
2785     /* subtracting a range from an fraction only works if the fraction
2786      * is not in the range */
2787     if (gst_value_compare_with_func (minuend, min, compare) ==
2788         GST_VALUE_LESS_THAN ||
2789         gst_value_compare_with_func (minuend, max, compare) ==
2790         GST_VALUE_GREATER_THAN) {
2791       /* and the result is the value */
2792       gst_value_init_and_copy (dest, minuend);
2793       return TRUE;
2794     }
2795   }
2796   return FALSE;
2797 }
2798
2799 static gboolean
2800 gst_value_subtract_fraction_range_fraction (GValue * dest,
2801     const GValue * minuend, const GValue * subtrahend)
2802 {
2803   /* since we don't have open ranges, we cannot create a hole in
2804    * a range. We return the original range */
2805   gst_value_init_and_copy (dest, minuend);
2806   return TRUE;
2807 }
2808
2809 static gboolean
2810 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
2811     const GValue * minuend, const GValue * subtrahend)
2812 {
2813   /* since we don't have open ranges, we have to approximate */
2814   /* done like with ints and doubles. Creates a list of 2 fraction ranges */
2815   const GValue *min1 = gst_value_get_fraction_range_min (minuend);
2816   const GValue *max2 = gst_value_get_fraction_range_max (minuend);
2817   const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
2818   const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
2819   gint cmp1, cmp2;
2820   GValue v1 = { 0, };
2821   GValue v2 = { 0, };
2822   GValue *pv1, *pv2;            /* yeah, hungarian! */
2823   GstValueCompareFunc compare;
2824
2825   g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
2826   g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
2827
2828   compare = gst_value_get_compare_func (min1);
2829   g_return_val_if_fail (compare, FALSE);
2830
2831   cmp1 = gst_value_compare_with_func (max2, max1, compare);
2832   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2833   if (cmp1 == GST_VALUE_LESS_THAN)
2834     max1 = max2;
2835   cmp1 = gst_value_compare_with_func (min1, min2, compare);
2836   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2837   if (cmp1 == GST_VALUE_GREATER_THAN)
2838     min2 = min1;
2839
2840   cmp1 = gst_value_compare_with_func (min1, max1, compare);
2841   cmp2 = gst_value_compare_with_func (min2, max2, compare);
2842
2843   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2844     pv1 = &v1;
2845     pv2 = &v2;
2846   } else if (cmp1 == GST_VALUE_LESS_THAN) {
2847     pv1 = dest;
2848     pv2 = NULL;
2849   } else if (cmp2 == GST_VALUE_LESS_THAN) {
2850     pv1 = NULL;
2851     pv2 = dest;
2852   } else {
2853     return FALSE;
2854   }
2855
2856   if (cmp1 == GST_VALUE_LESS_THAN) {
2857     g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
2858     gst_value_set_fraction_range (pv1, min1, max1);
2859   }
2860   if (cmp2 == GST_VALUE_LESS_THAN) {
2861     g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
2862     gst_value_set_fraction_range (pv2, min2, max2);
2863   }
2864
2865   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2866     gst_value_list_concat (dest, pv1, pv2);
2867     g_value_unset (pv1);
2868     g_value_unset (pv2);
2869   }
2870   return TRUE;
2871 }
2872
2873
2874 /**************
2875  * comparison *
2876  **************/
2877
2878 /*
2879  * gst_value_get_compare_func:
2880  * @value1: a value to get the compare function for
2881  *
2882  * Determines the compare function to be used with values of the same type as
2883  * @value1. The function can be given to gst_value_compare_with_func().
2884  *
2885  * Returns: A #GstValueCompareFunc value
2886  */
2887 static GstValueCompareFunc
2888 gst_value_get_compare_func (const GValue * value1)
2889 {
2890   GstValueTable *table, *best = NULL;
2891   guint i;
2892   GType type1;
2893
2894   type1 = G_VALUE_TYPE (value1);
2895
2896   /* this is a fast check */
2897   best = gst_value_hash_lookup_type (type1);
2898
2899   /* slower checks */
2900   if (G_UNLIKELY (!best || !best->compare)) {
2901     guint len = gst_value_table->len;
2902
2903     best = NULL;
2904     for (i = 0; i < len; i++) {
2905       table = &g_array_index (gst_value_table, GstValueTable, i);
2906       if (table->compare && g_type_is_a (type1, table->type)) {
2907         if (!best || g_type_is_a (table->type, best->type))
2908           best = table;
2909       }
2910     }
2911   }
2912   if (G_LIKELY (best))
2913     return best->compare;
2914
2915   return NULL;
2916 }
2917
2918 /**
2919  * gst_value_can_compare:
2920  * @value1: a value to compare
2921  * @value2: another value to compare
2922  *
2923  * Determines if @value1 and @value2 can be compared.
2924  *
2925  * Returns: TRUE if the values can be compared
2926  */
2927 gboolean
2928 gst_value_can_compare (const GValue * value1, const GValue * value2)
2929 {
2930   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
2931   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
2932
2933   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2934     return FALSE;
2935
2936   return gst_value_get_compare_func (value1) != NULL;
2937 }
2938
2939 /**
2940  * gst_value_compare:
2941  * @value1: a value to compare
2942  * @value2: another value to compare
2943  *
2944  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
2945  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
2946  * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
2947  * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
2948  * If the values are equal, GST_VALUE_EQUAL is returned.
2949  *
2950  * Returns: comparison result
2951  */
2952 gint
2953 gst_value_compare (const GValue * value1, const GValue * value2)
2954 {
2955   GstValueCompareFunc compare;
2956
2957   g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
2958   g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
2959
2960   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2961     return GST_VALUE_UNORDERED;
2962
2963   compare = gst_value_get_compare_func (value1);
2964   if (compare) {
2965     return compare (value1, value2);
2966   }
2967
2968   g_critical ("unable to compare values of type %s\n",
2969       g_type_name (G_VALUE_TYPE (value1)));
2970   return GST_VALUE_UNORDERED;
2971 }
2972
2973 /*
2974  * gst_value_compare_with_func:
2975  * @value1: a value to compare
2976  * @value2: another value to compare
2977  * @compare: compare function
2978  *
2979  * Compares @value1 and @value2 using the @compare function. Works like
2980  * gst_value_compare() but allows to save time determining the compare function
2981  * a multiple times. 
2982  *
2983  * Returns: comparison result
2984  */
2985 static gint
2986 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
2987     GstValueCompareFunc compare)
2988 {
2989   g_assert (compare);
2990
2991   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2992     return GST_VALUE_UNORDERED;
2993
2994   return compare (value1, value2);
2995 }
2996
2997 /* union */
2998
2999 /**
3000  * gst_value_can_union:
3001  * @value1: a value to union
3002  * @value2: another value to union
3003  *
3004  * Determines if @value1 and @value2 can be non-trivially unioned.
3005  * Any two values can be trivially unioned by adding both of them
3006  * to a GstValueList.  However, certain types have the possibility
3007  * to be unioned in a simpler way.  For example, an integer range
3008  * and an integer can be unioned if the integer is a subset of the
3009  * integer range.  If there is the possibility that two values can
3010  * be unioned, this function returns TRUE.
3011  *
3012  * Returns: TRUE if there is a function allowing the two values to
3013  * be unioned.
3014  */
3015 gboolean
3016 gst_value_can_union (const GValue * value1, const GValue * value2)
3017 {
3018   GstValueUnionInfo *union_info;
3019   guint i, len;
3020
3021   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3022   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3023
3024   len = gst_value_union_funcs->len;
3025
3026   for (i = 0; i < len; i++) {
3027     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
3028     if (union_info->type1 == G_VALUE_TYPE (value1) &&
3029         union_info->type2 == G_VALUE_TYPE (value2))
3030       return TRUE;
3031     if (union_info->type1 == G_VALUE_TYPE (value2) &&
3032         union_info->type2 == G_VALUE_TYPE (value1))
3033       return TRUE;
3034   }
3035
3036   return FALSE;
3037 }
3038
3039 /**
3040  * gst_value_union:
3041  * @dest: the destination value
3042  * @value1: a value to union
3043  * @value2: another value to union
3044  *
3045  * Creates a GValue corresponding to the union of @value1 and @value2.
3046  *
3047  * Returns: always returns %TRUE
3048  */
3049 /* FIXME: change return type to 'void'? */
3050 gboolean
3051 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
3052 {
3053   GstValueUnionInfo *union_info;
3054   guint i, len;
3055
3056   g_return_val_if_fail (dest != NULL, FALSE);
3057   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3058   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3059
3060   len = gst_value_union_funcs->len;
3061
3062   for (i = 0; i < len; i++) {
3063     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
3064     if (union_info->type1 == G_VALUE_TYPE (value1) &&
3065         union_info->type2 == G_VALUE_TYPE (value2)) {
3066       if (union_info->func (dest, value1, value2)) {
3067         return TRUE;
3068       }
3069     }
3070     if (union_info->type1 == G_VALUE_TYPE (value2) &&
3071         union_info->type2 == G_VALUE_TYPE (value1)) {
3072       if (union_info->func (dest, value2, value1)) {
3073         return TRUE;
3074       }
3075     }
3076   }
3077
3078   gst_value_list_concat (dest, value1, value2);
3079   return TRUE;
3080 }
3081
3082 /**
3083  * gst_value_register_union_func:
3084  * @type1: a type to union
3085  * @type2: another type to union
3086  * @func: a function that implments creating a union between the two types
3087  *
3088  * Registers a union function that can create a union between #GValue items
3089  * of the type @type1 and @type2.
3090  *
3091  * Union functions should be registered at startup before any pipelines are
3092  * started, as gst_value_register_union_func() is not thread-safe and cannot
3093  * be used at the same time as gst_value_union() or gst_value_can_union().
3094  */
3095 void
3096 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
3097 {
3098   GstValueUnionInfo union_info;
3099
3100   union_info.type1 = type1;
3101   union_info.type2 = type2;
3102   union_info.func = func;
3103
3104   g_array_append_val (gst_value_union_funcs, union_info);
3105 }
3106
3107 /* intersection */
3108
3109 /**
3110  * gst_value_can_intersect:
3111  * @value1: a value to intersect
3112  * @value2: another value to intersect
3113  *
3114  * Determines if intersecting two values will produce a valid result.
3115  * Two values will produce a valid intersection if they have the same
3116  * type, or if there is a method (registered by
3117  * gst_value_register_intersect_func()) to calculate the intersection.
3118  *
3119  * Returns: TRUE if the values can intersect
3120  */
3121 gboolean
3122 gst_value_can_intersect (const GValue * value1, const GValue * value2)
3123 {
3124   GstValueIntersectInfo *intersect_info;
3125   guint i, len;
3126   GType ltype, type1, type2;
3127
3128   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3129   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3130
3131   ltype = gst_value_list_get_type ();
3132
3133   /* special cases */
3134   if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
3135     return TRUE;
3136
3137   type1 = G_VALUE_TYPE (value1);
3138   type2 = G_VALUE_TYPE (value2);
3139
3140   /* practically all GstValue types have a compare function (_can_compare=TRUE)
3141    * GstStructure and GstCaps have npot, but are intersectable */
3142   if (type1 == type2)
3143     return TRUE;
3144
3145   /* check registered intersect functions */
3146   len = gst_value_intersect_funcs->len;
3147   for (i = 0; i < len; i++) {
3148     intersect_info = &g_array_index (gst_value_intersect_funcs,
3149         GstValueIntersectInfo, i);
3150     if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
3151         (intersect_info->type1 == type2 && intersect_info->type2 == type1))
3152       return TRUE;
3153   }
3154
3155   return gst_value_can_compare (value1, value2);
3156 }
3157
3158 /**
3159  * gst_value_intersect:
3160  * @dest: a uninitialized #GValue that will hold the calculated
3161  * intersection value
3162  * @value1: a value to intersect
3163  * @value2: another value to intersect
3164  *
3165  * Calculates the intersection of two values.  If the values have
3166  * a non-empty intersection, the value representing the intersection
3167  * is placed in @dest.  If the intersection is non-empty, @dest is
3168  * not modified.
3169  *
3170  * Returns: TRUE if the intersection is non-empty
3171  */
3172 gboolean
3173 gst_value_intersect (GValue * dest, const GValue * value1,
3174     const GValue * value2)
3175 {
3176   GstValueIntersectInfo *intersect_info;
3177   guint i, len;
3178   GType ltype, type1, type2;
3179
3180   g_return_val_if_fail (dest != NULL, FALSE);
3181   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3182   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3183
3184   ltype = gst_value_list_get_type ();
3185
3186   /* special cases first */
3187   if (G_VALUE_HOLDS (value1, ltype))
3188     return gst_value_intersect_list (dest, value1, value2);
3189   if (G_VALUE_HOLDS (value2, ltype))
3190     return gst_value_intersect_list (dest, value2, value1);
3191
3192   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
3193     gst_value_init_and_copy (dest, value1);
3194     return TRUE;
3195   }
3196
3197   type1 = G_VALUE_TYPE (value1);
3198   type2 = G_VALUE_TYPE (value2);
3199
3200   len = gst_value_intersect_funcs->len;
3201   for (i = 0; i < len; i++) {
3202     intersect_info = &g_array_index (gst_value_intersect_funcs,
3203         GstValueIntersectInfo, i);
3204     if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
3205       return intersect_info->func (dest, value1, value2);
3206     }
3207     if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
3208       return intersect_info->func (dest, value2, value1);
3209     }
3210   }
3211   return FALSE;
3212 }
3213
3214 /**
3215  * gst_value_register_intersect_func:
3216  * @type1: the first type to intersect
3217  * @type2: the second type to intersect
3218  * @func: the intersection function
3219  *
3220  * Registers a function that is called to calculate the intersection
3221  * of the values having the types @type1 and @type2.
3222  *
3223  * Intersect functions should be registered at startup before any pipelines are
3224  * started, as gst_value_register_intersect_func() is not thread-safe and
3225  * cannot be used at the same time as gst_value_intersect() or
3226  * gst_value_can_intersect().
3227  */
3228 void
3229 gst_value_register_intersect_func (GType type1, GType type2,
3230     GstValueIntersectFunc func)
3231 {
3232   GstValueIntersectInfo intersect_info;
3233
3234   intersect_info.type1 = type1;
3235   intersect_info.type2 = type2;
3236   intersect_info.func = func;
3237
3238   g_array_append_val (gst_value_intersect_funcs, intersect_info);
3239 }
3240
3241
3242 /* subtraction */
3243
3244 /**
3245  * gst_value_subtract:
3246  * @dest: the destination value for the result if the subtraction is not empty
3247  * @minuend: the value to subtract from
3248  * @subtrahend: the value to subtract
3249  *
3250  * Subtracts @subtrahend from @minuend and stores the result in @dest.
3251  * Note that this means subtraction as in sets, not as in mathematics.
3252  *
3253  * Returns: %TRUE if the subtraction is not empty
3254  */
3255 gboolean
3256 gst_value_subtract (GValue * dest, const GValue * minuend,
3257     const GValue * subtrahend)
3258 {
3259   GstValueSubtractInfo *info;
3260   guint i, len;
3261   GType ltype, mtype, stype;
3262
3263   g_return_val_if_fail (dest != NULL, FALSE);
3264   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
3265   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
3266
3267   ltype = gst_value_list_get_type ();
3268
3269   /* special cases first */
3270   if (G_VALUE_HOLDS (minuend, ltype))
3271     return gst_value_subtract_from_list (dest, minuend, subtrahend);
3272   if (G_VALUE_HOLDS (subtrahend, ltype))
3273     return gst_value_subtract_list (dest, minuend, subtrahend);
3274
3275   mtype = G_VALUE_TYPE (minuend);
3276   stype = G_VALUE_TYPE (subtrahend);
3277
3278   len = gst_value_subtract_funcs->len;
3279   for (i = 0; i < len; i++) {
3280     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3281     if (info->minuend == mtype && info->subtrahend == stype) {
3282       return info->func (dest, minuend, subtrahend);
3283     }
3284   }
3285
3286   if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3287     gst_value_init_and_copy (dest, minuend);
3288     return TRUE;
3289   }
3290
3291   return FALSE;
3292 }
3293
3294 #if 0
3295 gboolean
3296 gst_value_subtract (GValue * dest, const GValue * minuend,
3297     const GValue * subtrahend)
3298 {
3299   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3300
3301   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
3302       gst_value_serialize (subtrahend),
3303       ret ? gst_value_serialize (dest) : "---");
3304   return ret;
3305 }
3306 #endif
3307
3308 /**
3309  * gst_value_can_subtract:
3310  * @minuend: the value to subtract from
3311  * @subtrahend: the value to subtract
3312  *
3313  * Checks if it's possible to subtract @subtrahend from @minuend.
3314  *
3315  * Returns: TRUE if a subtraction is possible
3316  */
3317 gboolean
3318 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3319 {
3320   GstValueSubtractInfo *info;
3321   guint i, len;
3322   GType ltype, mtype, stype;
3323
3324   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
3325   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
3326
3327   ltype = gst_value_list_get_type ();
3328
3329   /* special cases */
3330   if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
3331     return TRUE;
3332
3333   mtype = G_VALUE_TYPE (minuend);
3334   stype = G_VALUE_TYPE (subtrahend);
3335
3336   len = gst_value_subtract_funcs->len;
3337   for (i = 0; i < len; i++) {
3338     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3339     if (info->minuend == mtype && info->subtrahend == stype)
3340       return TRUE;
3341   }
3342
3343   return gst_value_can_compare (minuend, subtrahend);
3344 }
3345
3346 /**
3347  * gst_value_register_subtract_func:
3348  * @minuend_type: type of the minuend
3349  * @subtrahend_type: type of the subtrahend
3350  * @func: function to use
3351  *
3352  * Registers @func as a function capable of subtracting the values of
3353  * @subtrahend_type from values of @minuend_type.
3354  *
3355  * Subtract functions should be registered at startup before any pipelines are
3356  * started, as gst_value_register_subtract_func() is not thread-safe and
3357  * cannot be used at the same time as gst_value_subtract().
3358  */
3359 void
3360 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3361     GstValueSubtractFunc func)
3362 {
3363   GstValueSubtractInfo info;
3364
3365   /* one type must be unfixed, other subtractions can be done as comparisons */
3366   g_return_if_fail (!gst_type_is_fixed (minuend_type)
3367       || !gst_type_is_fixed (subtrahend_type));
3368
3369   info.minuend = minuend_type;
3370   info.subtrahend = subtrahend_type;
3371   info.func = func;
3372
3373   g_array_append_val (gst_value_subtract_funcs, info);
3374 }
3375
3376 /**
3377  * gst_value_register:
3378  * @table: structure containing functions to register
3379  *
3380  * Registers functions to perform calculations on #GValue items of a given
3381  * type. Each type can only be added once.
3382  */
3383 void
3384 gst_value_register (const GstValueTable * table)
3385 {
3386   GstValueTable *found;
3387
3388   g_return_if_fail (table != NULL);
3389
3390   g_array_append_val (gst_value_table, *table);
3391
3392   found = gst_value_hash_lookup_type (table->type);
3393   if (found)
3394     g_warning ("adding type %s multiple times", g_type_name (table->type));
3395
3396   /* FIXME: we're not really doing the const justice, we assume the table is
3397    * static */
3398   gst_value_hash_add_type (table->type, table);
3399 }
3400
3401 /**
3402  * gst_value_init_and_copy:
3403  * @dest: the target value
3404  * @src: the source value
3405  *
3406  * Initialises the target value to be of the same type as source and then copies
3407  * the contents from source to target.
3408  */
3409 void
3410 gst_value_init_and_copy (GValue * dest, const GValue * src)
3411 {
3412   g_return_if_fail (G_IS_VALUE (src));
3413   g_return_if_fail (dest != NULL);
3414
3415   g_value_init (dest, G_VALUE_TYPE (src));
3416   g_value_copy (src, dest);
3417 }
3418
3419 /**
3420  * gst_value_serialize:
3421  * @value: a #GValue to serialize
3422  *
3423  * tries to transform the given @value into a string representation that allows
3424  * getting back this string later on using gst_value_deserialize().
3425  *
3426  * Returns: the serialization for @value or NULL if none exists
3427  */
3428 gchar *
3429 gst_value_serialize (const GValue * value)
3430 {
3431   guint i, len;
3432   GValue s_val = { 0 };
3433   GstValueTable *table, *best;
3434   gchar *s;
3435   GType type;
3436
3437   g_return_val_if_fail (G_IS_VALUE (value), NULL);
3438
3439   type = G_VALUE_TYPE (value);
3440
3441   best = gst_value_hash_lookup_type (type);
3442
3443   if (G_UNLIKELY (!best || !best->serialize)) {
3444     len = gst_value_table->len;
3445     best = NULL;
3446     for (i = 0; i < len; i++) {
3447       table = &g_array_index (gst_value_table, GstValueTable, i);
3448       if (table->serialize && g_type_is_a (type, table->type)) {
3449         if (!best || g_type_is_a (table->type, best->type))
3450           best = table;
3451       }
3452     }
3453   }
3454   if (G_LIKELY (best))
3455     return best->serialize (value);
3456
3457   g_value_init (&s_val, G_TYPE_STRING);
3458   if (g_value_transform (value, &s_val)) {
3459     s = gst_string_wrap (g_value_get_string (&s_val));
3460   } else {
3461     s = NULL;
3462   }
3463   g_value_unset (&s_val);
3464
3465   return s;
3466 }
3467
3468 /**
3469  * gst_value_deserialize:
3470  * @dest: #GValue to fill with contents of deserialization
3471  * @src: string to deserialize
3472  *
3473  * Tries to deserialize a string into the type specified by the given GValue.
3474  * If the operation succeeds, TRUE is returned, FALSE otherwise.
3475  *
3476  * Returns: TRUE on success
3477  */
3478 gboolean
3479 gst_value_deserialize (GValue * dest, const gchar * src)
3480 {
3481   GstValueTable *table, *best;
3482   guint i, len;
3483   GType type;
3484
3485   g_return_val_if_fail (src != NULL, FALSE);
3486   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
3487
3488   type = G_VALUE_TYPE (dest);
3489
3490   best = gst_value_hash_lookup_type (type);
3491   if (G_UNLIKELY (!best || !best->deserialize)) {
3492     len = gst_value_table->len;
3493     best = NULL;
3494     for (i = 0; i < len; i++) {
3495       table = &g_array_index (gst_value_table, GstValueTable, i);
3496       if (table->deserialize && g_type_is_a (type, table->type)) {
3497         if (!best || g_type_is_a (table->type, best->type))
3498           best = table;
3499       }
3500     }
3501   }
3502   if (G_LIKELY (best))
3503     return best->deserialize (dest, src);
3504
3505   return FALSE;
3506 }
3507
3508 /**
3509  * gst_value_is_fixed:
3510  * @value: the #GValue to check
3511  *
3512  * Tests if the given GValue, if available in a GstStructure (or any other
3513  * container) contains a "fixed" (which means: one value) or an "unfixed"
3514  * (which means: multiple possible values, such as data lists or data
3515  * ranges) value.
3516  *
3517  * Returns: true if the value is "fixed".
3518  */
3519
3520 gboolean
3521 gst_value_is_fixed (const GValue * value)
3522 {
3523   GType type;
3524
3525   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
3526
3527   type = G_VALUE_TYPE (value);
3528
3529   /* the most common types are just basic plain glib types */
3530   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
3531     return TRUE;
3532   }
3533
3534   if (type == GST_TYPE_ARRAY) {
3535     gint size, n;
3536     const GValue *kid;
3537
3538     /* check recursively */
3539     size = gst_value_array_get_size (value);
3540     for (n = 0; n < size; n++) {
3541       kid = gst_value_array_get_value (value, n);
3542       if (!gst_value_is_fixed (kid))
3543         return FALSE;
3544     }
3545     return TRUE;
3546   }
3547   return gst_type_is_fixed (type);
3548 }
3549
3550 /************
3551  * fraction *
3552  ************/
3553
3554 /* helper functions */
3555 static void
3556 gst_value_init_fraction (GValue * value)
3557 {
3558   value->data[0].v_int = 0;
3559   value->data[1].v_int = 1;
3560 }
3561
3562 static void
3563 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
3564 {
3565   dest_value->data[0].v_int = src_value->data[0].v_int;
3566   dest_value->data[1].v_int = src_value->data[1].v_int;
3567 }
3568
3569 static gchar *
3570 gst_value_collect_fraction (GValue * value, guint n_collect_values,
3571     GTypeCValue * collect_values, guint collect_flags)
3572 {
3573   gst_value_set_fraction (value,
3574       collect_values[0].v_int, collect_values[1].v_int);
3575
3576   return NULL;
3577 }
3578
3579 static gchar *
3580 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
3581     GTypeCValue * collect_values, guint collect_flags)
3582 {
3583   gint *numerator = collect_values[0].v_pointer;
3584   gint *denominator = collect_values[1].v_pointer;
3585
3586   if (!numerator)
3587     return g_strdup_printf ("numerator for `%s' passed as NULL",
3588         G_VALUE_TYPE_NAME (value));
3589   if (!denominator)
3590     return g_strdup_printf ("denominator for `%s' passed as NULL",
3591         G_VALUE_TYPE_NAME (value));
3592
3593   *numerator = value->data[0].v_int;
3594   *denominator = value->data[1].v_int;
3595
3596   return NULL;
3597 }
3598
3599 /**
3600  * gst_value_set_fraction:
3601  * @value: a GValue initialized to #GST_TYPE_FRACTION
3602  * @numerator: the numerator of the fraction
3603  * @denominator: the denominator of the fraction
3604  *
3605  * Sets @value to the fraction specified by @numerator over @denominator.
3606  * The fraction gets reduced to the smallest numerator and denominator,
3607  * and if necessary the sign is moved to the numerator.
3608  */
3609 void
3610 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
3611 {
3612   gint gcd = 0;
3613
3614   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
3615   g_return_if_fail (denominator != 0);
3616   g_return_if_fail (denominator >= -G_MAXINT);
3617   g_return_if_fail (numerator >= -G_MAXINT);
3618
3619   /* normalize sign */
3620   if (denominator < 0) {
3621     numerator = -numerator;
3622     denominator = -denominator;
3623   }
3624
3625   /* check for reduction */
3626   gcd = gst_util_greatest_common_divisor (numerator, denominator);
3627   if (gcd) {
3628     numerator /= gcd;
3629     denominator /= gcd;
3630   }
3631
3632   g_assert (denominator > 0);
3633
3634   value->data[0].v_int = numerator;
3635   value->data[1].v_int = denominator;
3636 }
3637
3638 /**
3639  * gst_value_get_fraction_numerator:
3640  * @value: a GValue initialized to #GST_TYPE_FRACTION
3641  *
3642  * Gets the numerator of the fraction specified by @value.
3643  *
3644  * Returns: the numerator of the fraction.
3645  */
3646 gint
3647 gst_value_get_fraction_numerator (const GValue * value)
3648 {
3649   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
3650
3651   return value->data[0].v_int;
3652 }
3653
3654 /**
3655  * gst_value_get_fraction_denominator:
3656  * @value: a GValue initialized to #GST_TYPE_FRACTION
3657  *
3658  * Gets the denominator of the fraction specified by @value.
3659  *
3660  * Returns: the denominator of the fraction.
3661  */
3662 gint
3663 gst_value_get_fraction_denominator (const GValue * value)
3664 {
3665   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
3666
3667   return value->data[1].v_int;
3668 }
3669
3670 /**
3671  * gst_value_fraction_multiply:
3672  * @product: a GValue initialized to #GST_TYPE_FRACTION
3673  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
3674  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
3675  *
3676  * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
3677  * @product to the product of the two fractions.
3678  *
3679  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3680  */
3681 gboolean
3682 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
3683     const GValue * factor2)
3684 {
3685   gint n1, n2, d1, d2;
3686   gint res_n, res_d;
3687
3688   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
3689   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
3690
3691   n1 = factor1->data[0].v_int;
3692   n2 = factor2->data[0].v_int;
3693   d1 = factor1->data[1].v_int;
3694   d2 = factor2->data[1].v_int;
3695
3696   if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
3697     return FALSE;
3698
3699   gst_value_set_fraction (product, res_n, res_d);
3700
3701   return TRUE;
3702 }
3703
3704 /**
3705  * gst_value_fraction_subtract:
3706  * @dest: a GValue initialized to #GST_TYPE_FRACTION
3707  * @minuend: a GValue initialized to #GST_TYPE_FRACTION
3708  * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
3709  *
3710  * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
3711  *
3712  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3713  */
3714 gboolean
3715 gst_value_fraction_subtract (GValue * dest,
3716     const GValue * minuend, const GValue * subtrahend)
3717 {
3718   gint n1, n2, d1, d2;
3719   gint res_n, res_d;
3720
3721   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
3722   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
3723
3724   n1 = minuend->data[0].v_int;
3725   n2 = subtrahend->data[0].v_int;
3726   d1 = minuend->data[1].v_int;
3727   d2 = subtrahend->data[1].v_int;
3728
3729   if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
3730     return FALSE;
3731   gst_value_set_fraction (dest, res_n, res_d);
3732
3733   return TRUE;
3734 }
3735
3736 static gchar *
3737 gst_value_serialize_fraction (const GValue * value)
3738 {
3739   gint32 numerator = value->data[0].v_int;
3740   gint32 denominator = value->data[1].v_int;
3741   gboolean positive = TRUE;
3742
3743   /* get the sign and make components absolute */
3744   if (numerator < 0) {
3745     numerator = -numerator;
3746     positive = !positive;
3747   }
3748   if (denominator < 0) {
3749     denominator = -denominator;
3750     positive = !positive;
3751   }
3752
3753   return g_strdup_printf ("%s%d/%d",
3754       positive ? "" : "-", numerator, denominator);
3755 }
3756
3757 static gboolean
3758 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
3759 {
3760   gint num, den;
3761   gint num_chars;
3762
3763   if (G_UNLIKELY (s == NULL))
3764     return FALSE;
3765
3766   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
3767     return FALSE;
3768
3769   if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
3770     if (s[num_chars] != 0)
3771       return FALSE;
3772     gst_value_set_fraction (dest, num, den);
3773     return TRUE;
3774   } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
3775     gst_value_set_fraction (dest, 1, G_MAXINT);
3776     return TRUE;
3777   } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
3778     if (s[num_chars] != 0)
3779       return FALSE;
3780     gst_value_set_fraction (dest, num, 1);
3781     return TRUE;
3782   } else if (g_ascii_strcasecmp (s, "min") == 0) {
3783     gst_value_set_fraction (dest, -G_MAXINT, 1);
3784     return TRUE;
3785   } else if (g_ascii_strcasecmp (s, "max") == 0) {
3786     gst_value_set_fraction (dest, G_MAXINT, 1);
3787     return TRUE;
3788   }
3789
3790   return FALSE;
3791 }
3792
3793 static void
3794 gst_value_transform_fraction_string (const GValue * src_value,
3795     GValue * dest_value)
3796 {
3797   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
3798 }
3799
3800 static void
3801 gst_value_transform_string_fraction (const GValue * src_value,
3802     GValue * dest_value)
3803 {
3804   if (!gst_value_deserialize_fraction (dest_value,
3805           src_value->data[0].v_pointer))
3806     /* If the deserialize fails, ensure we leave the fraction in a
3807      * valid, if incorrect, state */
3808     gst_value_set_fraction (dest_value, 0, 1);
3809 }
3810
3811 static void
3812 gst_value_transform_double_fraction (const GValue * src_value,
3813     GValue * dest_value)
3814 {
3815   gdouble src = g_value_get_double (src_value);
3816   gint n, d;
3817
3818   gst_util_double_to_fraction (src, &n, &d);
3819   gst_value_set_fraction (dest_value, n, d);
3820 }
3821
3822 static void
3823 gst_value_transform_float_fraction (const GValue * src_value,
3824     GValue * dest_value)
3825 {
3826   gfloat src = g_value_get_float (src_value);
3827   gint n, d;
3828
3829   gst_util_double_to_fraction (src, &n, &d);
3830   gst_value_set_fraction (dest_value, n, d);
3831 }
3832
3833 static void
3834 gst_value_transform_fraction_double (const GValue * src_value,
3835     GValue * dest_value)
3836 {
3837   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
3838       ((double) src_value->data[1].v_int);
3839 }
3840
3841 static void
3842 gst_value_transform_fraction_float (const GValue * src_value,
3843     GValue * dest_value)
3844 {
3845   dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
3846       ((float) src_value->data[1].v_int);
3847 }
3848
3849 static gint
3850 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
3851 {
3852   gint n1, n2;
3853   gint d1, d2;
3854
3855   gint64 new_num_1;
3856   gint64 new_num_2;
3857
3858   n1 = value1->data[0].v_int;
3859   n2 = value2->data[0].v_int;
3860   d1 = value1->data[1].v_int;
3861   d2 = value2->data[1].v_int;
3862
3863   /* fractions are reduced when set, so we can quickly see if they're equal */
3864   if (n1 == n2 && d1 == d2)
3865     return GST_VALUE_EQUAL;
3866
3867   /* extend to 64 bits */
3868   new_num_1 = ((gint64) n1) * d2;
3869   new_num_2 = ((gint64) n2) * d1;
3870   if (new_num_1 < new_num_2)
3871     return GST_VALUE_LESS_THAN;
3872   if (new_num_1 > new_num_2)
3873     return GST_VALUE_GREATER_THAN;
3874
3875   /* new_num_1 == new_num_2 implies that both denominators must have 
3876    * been 0, beause otherwise simplification would have caught the
3877    * equivalence */
3878   return GST_VALUE_UNORDERED;
3879 }
3880
3881 /*********
3882  * GDate *
3883  *********/
3884
3885 /**
3886  * gst_value_set_date:
3887  * @value: a GValue initialized to GST_TYPE_DATE
3888  * @date: the date to set the value to
3889  *
3890  * Sets the contents of @value to coorespond to @date.  The actual
3891  * #GDate structure is copied before it is used.
3892  */
3893 void
3894 gst_value_set_date (GValue * value, const GDate * date)
3895 {
3896   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
3897   g_return_if_fail (g_date_valid (date));
3898
3899   g_value_set_boxed (value, date);
3900 }
3901
3902 /**
3903  * gst_value_get_date:
3904  * @value: a GValue initialized to GST_TYPE_DATE
3905  *
3906  * Gets the contents of @value.
3907  *
3908  * Returns: the contents of @value
3909  */
3910 const GDate *
3911 gst_value_get_date (const GValue * value)
3912 {
3913   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3914
3915   return (const GDate *) g_value_get_boxed (value);
3916 }
3917
3918 static gpointer
3919 gst_date_copy (gpointer boxed)
3920 {
3921   const GDate *date = (const GDate *) boxed;
3922
3923   if (!g_date_valid (date)) {
3924     GST_WARNING ("invalid GDate");
3925     return NULL;
3926   }
3927
3928   return g_date_new_julian (g_date_get_julian (date));
3929 }
3930
3931 static gint
3932 gst_value_compare_date (const GValue * value1, const GValue * value2)
3933 {
3934   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3935   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3936   guint32 j1, j2;
3937
3938   if (date1 == date2)
3939     return GST_VALUE_EQUAL;
3940
3941   if ((date1 == NULL || !g_date_valid (date1))
3942       && (date2 != NULL && g_date_valid (date2))) {
3943     return GST_VALUE_LESS_THAN;
3944   }
3945
3946   if ((date2 == NULL || !g_date_valid (date2))
3947       && (date1 != NULL && g_date_valid (date1))) {
3948     return GST_VALUE_GREATER_THAN;
3949   }
3950
3951   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3952       || !g_date_valid (date2)) {
3953     return GST_VALUE_UNORDERED;
3954   }
3955
3956   j1 = g_date_get_julian (date1);
3957   j2 = g_date_get_julian (date2);
3958
3959   if (j1 == j2)
3960     return GST_VALUE_EQUAL;
3961   else if (j1 < j2)
3962     return GST_VALUE_LESS_THAN;
3963   else
3964     return GST_VALUE_GREATER_THAN;
3965 }
3966
3967 static gchar *
3968 gst_value_serialize_date (const GValue * val)
3969 {
3970   const GDate *date = (const GDate *) g_value_get_boxed (val);
3971
3972   if (date == NULL || !g_date_valid (date))
3973     return g_strdup ("9999-99-99");
3974
3975   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3976       g_date_get_month (date), g_date_get_day (date));
3977 }
3978
3979 static gboolean
3980 gst_value_deserialize_date (GValue * dest, const gchar * s)
3981 {
3982   guint year, month, day;
3983
3984   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3985     return FALSE;
3986
3987   if (!g_date_valid_dmy (day, month, year))
3988     return FALSE;
3989
3990   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3991   return TRUE;
3992 }
3993
3994 static void
3995 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3996 {
3997   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3998 }
3999
4000 static void
4001 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
4002 {
4003   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
4004 }
4005
4006 static void
4007 gst_value_transform_object_string (const GValue * src_value,
4008     GValue * dest_value)
4009 {
4010   GstObject *obj;
4011   gchar *str;
4012
4013   obj = g_value_get_object (src_value);
4014   if (obj) {
4015     str =
4016         g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
4017         GST_OBJECT_NAME (obj));
4018   } else {
4019     str = g_strdup ("NULL");
4020   }
4021
4022   dest_value->data[0].v_pointer = str;
4023 }
4024
4025 static GTypeInfo _info = {
4026   0,
4027   NULL,
4028   NULL,
4029   NULL,
4030   NULL,
4031   NULL,
4032   0,
4033   0,
4034   NULL,
4035   NULL,
4036 };
4037
4038 static GTypeFundamentalInfo _finfo = {
4039   0
4040 };
4041
4042 #define FUNC_VALUE_GET_TYPE(type, name)                         \
4043 GType gst_ ## type ## _get_type (void)                          \
4044 {                                                               \
4045   static GType gst_ ## type ## _type = 0;                       \
4046                                                                 \
4047   if (G_UNLIKELY (gst_ ## type ## _type == 0)) {                \
4048     _info.value_table = & _gst_ ## type ## _value_table;        \
4049     gst_ ## type ## _type = g_type_register_fundamental (       \
4050         g_type_fundamental_next (),                             \
4051         name, &_info, &_finfo, 0);                              \
4052   }                                                             \
4053                                                                 \
4054   return gst_ ## type ## _type;                                 \
4055 }
4056
4057 static const GTypeValueTable _gst_fourcc_value_table = {
4058   gst_value_init_fourcc,
4059   NULL,
4060   gst_value_copy_fourcc,
4061   NULL,
4062   (char *) "i",
4063   gst_value_collect_fourcc,
4064   (char *) "p",
4065   gst_value_lcopy_fourcc
4066 };
4067
4068 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
4069
4070 static const GTypeValueTable _gst_int_range_value_table = {
4071   gst_value_init_int_range,
4072   NULL,
4073   gst_value_copy_int_range,
4074   NULL,
4075   (char *) "ii",
4076   gst_value_collect_int_range,
4077   (char *) "pp",
4078   gst_value_lcopy_int_range
4079 };
4080
4081 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
4082
4083 static const GTypeValueTable _gst_double_range_value_table = {
4084   gst_value_init_double_range,
4085   NULL,
4086   gst_value_copy_double_range,
4087   NULL,
4088   (char *) "dd",
4089   gst_value_collect_double_range,
4090   (char *) "pp",
4091   gst_value_lcopy_double_range
4092 };
4093
4094 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
4095
4096 static const GTypeValueTable _gst_fraction_range_value_table = {
4097   gst_value_init_fraction_range,
4098   gst_value_free_fraction_range,
4099   gst_value_copy_fraction_range,
4100   NULL,
4101   (char *) "iiii",
4102   gst_value_collect_fraction_range,
4103   (char *) "pppp",
4104   gst_value_lcopy_fraction_range
4105 };
4106
4107 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
4108
4109 static const GTypeValueTable _gst_value_list_value_table = {
4110   gst_value_init_list_or_array,
4111   gst_value_free_list_or_array,
4112   gst_value_copy_list_or_array,
4113   gst_value_list_or_array_peek_pointer,
4114   (char *) "p",
4115   gst_value_collect_list_or_array,
4116   (char *) "p",
4117   gst_value_lcopy_list_or_array
4118 };
4119
4120 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
4121
4122 static const GTypeValueTable _gst_value_array_value_table = {
4123   gst_value_init_list_or_array,
4124   gst_value_free_list_or_array,
4125   gst_value_copy_list_or_array,
4126   gst_value_list_or_array_peek_pointer,
4127   (char *) "p",
4128   gst_value_collect_list_or_array,
4129   (char *) "p",
4130   gst_value_lcopy_list_or_array
4131 };
4132
4133 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
4134
4135 static const GTypeValueTable _gst_fraction_value_table = {
4136   gst_value_init_fraction,
4137   NULL,
4138   gst_value_copy_fraction,
4139   NULL,
4140   (char *) "ii",
4141   gst_value_collect_fraction,
4142   (char *) "pp",
4143   gst_value_lcopy_fraction
4144 };
4145
4146 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
4147
4148
4149 GType
4150 gst_date_get_type (void)
4151 {
4152   static GType gst_date_type = 0;
4153
4154   if (G_UNLIKELY (gst_date_type == 0)) {
4155     /* FIXME 0.11: we require GLib 2.8 already
4156      * Not using G_TYPE_DATE here on purpose, even if we could
4157      * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
4158      * serialised strings to have different type strings depending
4159      * on what version is used, so FIXME when we require GLib-2.8 */
4160     gst_date_type = g_boxed_type_register_static ("GstDate",
4161         (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
4162   }
4163
4164   return gst_date_type;
4165 }
4166
4167 void
4168 _gst_value_initialize (void)
4169 {
4170   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
4171   gst_value_hash = g_hash_table_new (NULL, NULL);
4172   gst_value_union_funcs = g_array_new (FALSE, FALSE,
4173       sizeof (GstValueUnionInfo));
4174   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
4175       sizeof (GstValueIntersectInfo));
4176   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4177       sizeof (GstValueSubtractInfo));
4178
4179   {
4180     static GstValueTable gst_value = {
4181       0,
4182       gst_value_compare_fourcc,
4183       gst_value_serialize_fourcc,
4184       gst_value_deserialize_fourcc,
4185     };
4186
4187     gst_value.type = gst_fourcc_get_type ();
4188     gst_value_register (&gst_value);
4189   }
4190
4191   {
4192     static GstValueTable gst_value = {
4193       0,
4194       gst_value_compare_int_range,
4195       gst_value_serialize_int_range,
4196       gst_value_deserialize_int_range,
4197     };
4198
4199     gst_value.type = gst_int_range_get_type ();
4200     gst_value_register (&gst_value);
4201   }
4202
4203   {
4204     static GstValueTable gst_value = {
4205       0,
4206       gst_value_compare_double_range,
4207       gst_value_serialize_double_range,
4208       gst_value_deserialize_double_range,
4209     };
4210
4211     gst_value.type = gst_double_range_get_type ();
4212     gst_value_register (&gst_value);
4213   }
4214
4215   {
4216     static GstValueTable gst_value = {
4217       0,
4218       gst_value_compare_fraction_range,
4219       gst_value_serialize_fraction_range,
4220       gst_value_deserialize_fraction_range,
4221     };
4222
4223     gst_value.type = gst_fraction_range_get_type ();
4224     gst_value_register (&gst_value);
4225   }
4226
4227   {
4228     static GstValueTable gst_value = {
4229       0,
4230       gst_value_compare_list,
4231       gst_value_serialize_list,
4232       gst_value_deserialize_list,
4233     };
4234
4235     gst_value.type = gst_value_list_get_type ();
4236     gst_value_register (&gst_value);
4237   }
4238
4239   {
4240     static GstValueTable gst_value = {
4241       0,
4242       gst_value_compare_array,
4243       gst_value_serialize_array,
4244       gst_value_deserialize_array,
4245     };
4246
4247     gst_value.type = gst_value_array_get_type ();
4248     gst_value_register (&gst_value);
4249   }
4250
4251   {
4252 #if 0
4253     static const GTypeValueTable value_table = {
4254       gst_value_init_buffer,
4255       NULL,
4256       gst_value_copy_buffer,
4257       NULL,
4258       "i",
4259       NULL,                     /*gst_value_collect_buffer, */
4260       "p",
4261       NULL                      /*gst_value_lcopy_buffer */
4262     };
4263 #endif
4264     static GstValueTable gst_value = {
4265       0,
4266       gst_value_compare_buffer,
4267       gst_value_serialize_buffer,
4268       gst_value_deserialize_buffer,
4269     };
4270
4271     gst_value.type = GST_TYPE_BUFFER;
4272     gst_value_register (&gst_value);
4273   }
4274   {
4275     static GstValueTable gst_value = {
4276       0,
4277       gst_value_compare_fraction,
4278       gst_value_serialize_fraction,
4279       gst_value_deserialize_fraction,
4280     };
4281
4282     gst_value.type = gst_fraction_get_type ();
4283     gst_value_register (&gst_value);
4284   }
4285   {
4286     static GstValueTable gst_value = {
4287       0,
4288       NULL,
4289       gst_value_serialize_caps,
4290       gst_value_deserialize_caps,
4291     };
4292
4293     gst_value.type = GST_TYPE_CAPS;
4294     gst_value_register (&gst_value);
4295   }
4296   {
4297     static GstValueTable gst_value = {
4298       0,
4299       NULL,
4300       gst_value_serialize_structure,
4301       gst_value_deserialize_structure,
4302     };
4303
4304     gst_value.type = GST_TYPE_STRUCTURE;
4305     gst_value_register (&gst_value);
4306   }
4307   {
4308     static GstValueTable gst_value = {
4309       0,
4310       gst_value_compare_date,
4311       gst_value_serialize_date,
4312       gst_value_deserialize_date,
4313     };
4314
4315     gst_value.type = gst_date_get_type ();
4316     gst_value_register (&gst_value);
4317   }
4318
4319   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
4320   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
4321
4322   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
4323   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
4324   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
4325
4326   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
4327
4328   REGISTER_SERIALIZATION (G_TYPE_INT, int);
4329
4330   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
4331   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
4332
4333   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
4334   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
4335   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
4336
4337   g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
4338       gst_value_transform_fourcc_string);
4339   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
4340       gst_value_transform_int_range_string);
4341   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
4342       gst_value_transform_double_range_string);
4343   g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
4344       gst_value_transform_fraction_range_string);
4345   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
4346       gst_value_transform_list_string);
4347   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
4348       gst_value_transform_array_string);
4349   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
4350       gst_value_transform_fraction_string);
4351   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
4352       gst_value_transform_string_fraction);
4353   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
4354       gst_value_transform_fraction_double);
4355   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
4356       gst_value_transform_fraction_float);
4357   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
4358       gst_value_transform_double_fraction);
4359   g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
4360       gst_value_transform_float_fraction);
4361   g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
4362       gst_value_transform_date_string);
4363   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
4364       gst_value_transform_string_date);
4365   g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
4366       gst_value_transform_object_string);
4367
4368   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4369       gst_value_intersect_int_int_range);
4370   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4371       gst_value_intersect_int_range_int_range);
4372   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4373       gst_value_intersect_double_double_range);
4374   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
4375       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
4376   gst_value_register_intersect_func (GST_TYPE_ARRAY,
4377       GST_TYPE_ARRAY, gst_value_intersect_array);
4378   gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4379       gst_value_intersect_fraction_fraction_range);
4380   gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
4381       GST_TYPE_FRACTION_RANGE,
4382       gst_value_intersect_fraction_range_fraction_range);
4383
4384   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4385       gst_value_subtract_int_int_range);
4386   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
4387       gst_value_subtract_int_range_int);
4388   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4389       gst_value_subtract_int_range_int_range);
4390   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4391       gst_value_subtract_double_double_range);
4392   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
4393       gst_value_subtract_double_range_double);
4394   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
4395       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
4396
4397   gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4398       gst_value_subtract_fraction_fraction_range);
4399   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
4400       gst_value_subtract_fraction_range_fraction);
4401   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
4402       GST_TYPE_FRACTION_RANGE,
4403       gst_value_subtract_fraction_range_fraction_range);
4404
4405   /* see bug #317246, #64994, #65041 */
4406   {
4407     volatile GType date_type = G_TYPE_DATE;
4408
4409     g_type_name (date_type);
4410   }
4411
4412   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4413       gst_value_union_int_int_range);
4414   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4415       gst_value_union_int_range_int_range);
4416
4417 #if 0
4418   /* Implement these if needed */
4419   gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4420       gst_value_union_fraction_fraction_range);
4421   gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
4422       GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
4423 #endif
4424 }