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