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