Use GstClockTime in _get_state() instead of GTimeVal.
[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 to GStreamer
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #include "gst_private.h"
35 #include <gst/gst.h>
36 #include <gobject/gvaluecollector.h>
37
38 typedef struct _GstValueUnionInfo GstValueUnionInfo;
39 struct _GstValueUnionInfo
40 {
41   GType type1;
42   GType type2;
43   GstValueUnionFunc func;
44 };
45
46 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
47 struct _GstValueIntersectInfo
48 {
49   GType type1;
50   GType type2;
51   GstValueIntersectFunc func;
52 };
53
54 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
55 struct _GstValueSubtractInfo
56 {
57   GType minuend;
58   GType subtrahend;
59   GstValueSubtractFunc func;
60 };
61
62 GType gst_type_fourcc;
63 GType gst_type_int_range;
64 GType gst_type_double_range;
65 GType gst_type_list;
66 GType gst_type_array;
67 GType gst_type_fraction;
68 GType gst_type_date;
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 /********
76  * list *
77  ********/
78
79 /* two helper functions to serialize/stringify any type of list
80  * regular lists are done with { }, arrays with < >
81  */
82 static char *
83 gst_value_serialize_any_list (const GValue * value, const char *begin,
84     const char *end)
85 {
86   int i;
87   GArray *array = value->data[0].v_pointer;
88   GString *s;
89   GValue *v;
90   gchar *s_val;
91
92   s = g_string_new (begin);
93   for (i = 0; i < array->len; i++) {
94     v = &g_array_index (array, GValue, i);
95     s_val = gst_value_serialize (v);
96     g_string_append (s, s_val);
97     g_free (s_val);
98     if (i < array->len - 1) {
99       g_string_append (s, ", ");
100     }
101   }
102   g_string_append (s, end);
103   return g_string_free (s, FALSE);
104 }
105
106 static void
107 gst_value_transform_any_list_string (const GValue * src_value,
108     GValue * dest_value, const char *begin, const char *end)
109 {
110   GValue *list_value;
111   GArray *array;
112   GString *s;
113   int i;
114   char *list_s;
115
116   array = src_value->data[0].v_pointer;
117
118   s = g_string_new (begin);
119   for (i = 0; i < array->len; i++) {
120     list_value = &g_array_index (array, GValue, i);
121
122     if (i != 0) {
123       g_string_append (s, ", ");
124     }
125     list_s = g_strdup_value_contents (list_value);
126     g_string_append (s, list_s);
127     g_free (list_s);
128   }
129   g_string_append (s, end);
130
131   dest_value->data[0].v_pointer = g_string_free (s, FALSE);
132 }
133
134 /*
135  * helper function to see if a type is fixed. Is used internally here and
136  * there. Do not export, since it doesn't work for types where the content
137  * decides the fixedness (e.g. GST_TYPE_ARRAY).
138  */
139
140 static gboolean
141 gst_type_is_fixed (GType type)
142 {
143   if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
144       type == GST_TYPE_LIST) {
145     return FALSE;
146   }
147   if (G_TYPE_FUNDAMENTAL (type) <=
148       G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
149     return TRUE;
150   }
151   if (type == GST_TYPE_BUFFER || type == GST_TYPE_FOURCC
152       || type == GST_TYPE_ARRAY || type == GST_TYPE_FRACTION) {
153     return TRUE;
154   }
155
156   return FALSE;
157 }
158
159 /* GValue functions usable for both regular lists and arrays */
160 static void
161 gst_value_init_list (GValue * value)
162 {
163   value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
164 }
165
166 static GArray *
167 gst_value_list_array_copy (const GArray * src)
168 {
169   GArray *dest;
170   gint i;
171
172   dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
173   g_array_set_size (dest, src->len);
174   for (i = 0; i < src->len; i++) {
175     gst_value_init_and_copy (&g_array_index (dest, GValue, i),
176         &g_array_index (src, GValue, i));
177   }
178
179   return dest;
180 }
181
182 static void
183 gst_value_copy_list (const GValue * src_value, GValue * dest_value)
184 {
185   dest_value->data[0].v_pointer =
186       gst_value_list_array_copy ((GArray *) src_value->data[0].v_pointer);
187 }
188
189 static void
190 gst_value_free_list (GValue * value)
191 {
192   gint i;
193   GArray *src = (GArray *) value->data[0].v_pointer;
194
195   if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
196     for (i = 0; i < src->len; i++) {
197       g_value_unset (&g_array_index (src, GValue, i));
198     }
199     g_array_free (src, TRUE);
200   }
201 }
202
203 static gpointer
204 gst_value_list_peek_pointer (const GValue * value)
205 {
206   return value->data[0].v_pointer;
207 }
208
209 static gchar *
210 gst_value_collect_list (GValue * value, guint n_collect_values,
211     GTypeCValue * collect_values, guint collect_flags)
212 {
213   if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
214     value->data[0].v_pointer = collect_values[0].v_pointer;
215     value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
216   } else {
217     value->data[0].v_pointer =
218         gst_value_list_array_copy ((GArray *) collect_values[0].v_pointer);
219   }
220   return NULL;
221 }
222
223 static gchar *
224 gst_value_lcopy_list (const GValue * value, guint n_collect_values,
225     GTypeCValue * collect_values, guint collect_flags)
226 {
227   GArray **dest = collect_values[0].v_pointer;
228
229   if (!dest)
230     return g_strdup_printf ("value location for `%s' passed as NULL",
231         G_VALUE_TYPE_NAME (value));
232   if (!value->data[0].v_pointer)
233     return g_strdup_printf ("invalid value given for `%s'",
234         G_VALUE_TYPE_NAME (value));
235   if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
236     *dest = (GArray *) value->data[0].v_pointer;
237   } else {
238     *dest = gst_value_list_array_copy ((GArray *) value->data[0].v_pointer);
239   }
240   return NULL;
241 }
242
243 /**
244  * gst_value_list_prepend_value:
245  * @value: a GstValueList to prepend a value to
246  * @prepend_value: the value to prepend
247  *
248  * Prepends @prepend_value to the GstValueList in @value.
249  *
250  */
251 void
252 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
253 {
254   GValue val = { 0, };
255
256   g_return_if_fail (GST_VALUE_HOLDS_LIST (value)
257       || GST_VALUE_HOLDS_ARRAY (value));
258
259   gst_value_init_and_copy (&val, prepend_value);
260   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
261 }
262
263 /**
264  * gst_value_list_append_value:
265  * @value: a GstValueList to append a value to
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       || GST_VALUE_HOLDS_ARRAY (value));
277
278   gst_value_init_and_copy (&val, append_value);
279   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
280 }
281
282 /**
283  * gst_value_list_get_size:
284  * @value: a #GValue of type #GST_LIST_TYPE or #GST_ARRAY_TYPE
285  *
286  * Gets the number of values contained in @value.
287  *
288  * Returns: the number of values
289  */
290 guint
291 gst_value_list_get_size (const GValue * value)
292 {
293   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value)
294       || GST_VALUE_HOLDS_ARRAY (value), 0);
295
296   return ((GArray *) value->data[0].v_pointer)->len;
297 }
298
299 /**
300  * gst_value_list_get_value:
301  * @value: a #GValue of type #GST_LIST_TYPE or #GST_ARRAY_TYPE
302  * @index: index of value to get from the list
303  *
304  * Gets the value that is a member of the list contained in @value and
305  * has the index @index.
306  *
307  * Returns: the value at the given index
308  */
309 const GValue *
310 gst_value_list_get_value (const GValue * value, guint index)
311 {
312   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value)
313       || GST_VALUE_HOLDS_ARRAY (value), NULL);
314   g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
315
316   return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
317       GValue, index);
318 }
319
320 /**
321  * gst_value_list_concat:
322  * @dest: an uninitialized #GValue to take the result
323  * @value1: first value to put into the union
324  * @value2: second value to put into the union
325  *
326  * Concatenates copies of value1 and value2 into a list.  The value
327  * @dest is initialized to the type GST_TYPE_LIST.
328  */
329 void
330 gst_value_list_concat (GValue * dest, const GValue * value1,
331     const GValue * value2)
332 {
333   guint i, value1_length, value2_length;
334   GArray *array;
335
336   g_return_if_fail (dest != NULL);
337   g_return_if_fail (G_VALUE_TYPE (dest) == 0);
338   g_return_if_fail (G_IS_VALUE (value1));
339   g_return_if_fail (G_IS_VALUE (value2));
340
341   value1_length =
342       (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
343   value2_length =
344       (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
345   g_value_init (dest, GST_TYPE_LIST);
346   array = (GArray *) dest->data[0].v_pointer;
347   g_array_set_size (array, value1_length + value2_length);
348
349   if (GST_VALUE_HOLDS_LIST (value1)) {
350     for (i = 0; i < value1_length; i++) {
351       gst_value_init_and_copy (&g_array_index (array, GValue, i),
352           gst_value_list_get_value (value1, i));
353     }
354   } else {
355     gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
356   }
357
358   if (GST_VALUE_HOLDS_LIST (value2)) {
359     for (i = 0; i < value2_length; i++) {
360       gst_value_init_and_copy (&g_array_index (array, GValue,
361               i + value1_length), gst_value_list_get_value (value2, i));
362     }
363   } else {
364     gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
365         value2);
366   }
367 }
368
369 static void
370 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
371 {
372   gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
373 }
374
375 static void
376 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
377 {
378   gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
379 }
380
381 static int
382 gst_value_compare_list (const GValue * value1, const GValue * value2)
383 {
384   int i, j;
385   GArray *array1 = value1->data[0].v_pointer;
386   GArray *array2 = value2->data[0].v_pointer;
387   GValue *v1;
388   GValue *v2;
389
390   if (array1->len != array2->len)
391     return GST_VALUE_UNORDERED;
392
393   for (i = 0; i < array1->len; i++) {
394     v1 = &g_array_index (array1, GValue, i);
395     for (j = 0; j < array1->len; j++) {
396       v2 = &g_array_index (array2, GValue, j);
397       if (gst_value_compare (v1, v2) == GST_VALUE_EQUAL)
398         break;
399     }
400     if (j == array1->len) {
401       return GST_VALUE_UNORDERED;
402     }
403   }
404
405   return GST_VALUE_EQUAL;
406 }
407
408 static char *
409 gst_value_serialize_list (const GValue * value)
410 {
411   return gst_value_serialize_any_list (value, "{ ", " }");
412 }
413
414 static gboolean
415 gst_value_deserialize_list (GValue * dest, const char *s)
416 {
417   g_warning ("unimplemented");
418   return FALSE;
419 }
420
421 static char *
422 gst_value_serialize_array (const GValue * value)
423 {
424   return gst_value_serialize_any_list (value, "< ", " >");
425 }
426
427 static gboolean
428 gst_value_deserialize_array (GValue * dest, const char *s)
429 {
430   g_warning ("unimplemented");
431   return FALSE;
432 }
433
434 /**********
435  * fourcc *
436  **********/
437
438 static void
439 gst_value_init_fourcc (GValue * value)
440 {
441   value->data[0].v_int = 0;
442 }
443
444 static void
445 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
446 {
447   dest_value->data[0].v_int = src_value->data[0].v_int;
448 }
449
450 static gchar *
451 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
452     GTypeCValue * collect_values, guint collect_flags)
453 {
454   value->data[0].v_int = collect_values[0].v_int;
455
456   return NULL;
457 }
458
459 static gchar *
460 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
461     GTypeCValue * collect_values, guint collect_flags)
462 {
463   guint32 *fourcc_p = collect_values[0].v_pointer;
464
465   if (!fourcc_p)
466     return g_strdup_printf ("value location for `%s' passed as NULL",
467         G_VALUE_TYPE_NAME (value));
468
469   *fourcc_p = value->data[0].v_int;
470
471   return NULL;
472 }
473
474 /**
475  * gst_value_set_fourcc:
476  * @value: a GValue initialized to #GST_TYPE_FOURCC
477  * @fourcc: the #guint32 fourcc to set
478  *
479  * Sets @value to @fourcc.
480  */
481 void
482 gst_value_set_fourcc (GValue * value, guint32 fourcc)
483 {
484   g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
485
486   value->data[0].v_int = fourcc;
487 }
488
489 /**
490  * gst_value_get_fourcc:
491  * @value: a GValue initialized to #GST_TYPE_FOURCC
492  *
493  * Gets the #guint32 fourcc contained in @value.
494  *
495  * Returns: the #guint32 fourcc contained in @value.
496  */
497 guint32
498 gst_value_get_fourcc (const GValue * value)
499 {
500   g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
501
502   return value->data[0].v_int;
503 }
504
505 static void
506 gst_value_transform_fourcc_string (const GValue * src_value,
507     GValue * dest_value)
508 {
509   guint32 fourcc = src_value->data[0].v_int;
510
511   if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
512       g_ascii_isprint ((fourcc >> 8) & 0xff) &&
513       g_ascii_isprint ((fourcc >> 16) & 0xff) &&
514       g_ascii_isprint ((fourcc >> 24) & 0xff)) {
515     dest_value->data[0].v_pointer =
516         g_strdup_printf (GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
517   } else {
518     dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
519   }
520 }
521
522 static int
523 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
524 {
525   if (value2->data[0].v_int == value1->data[0].v_int)
526     return GST_VALUE_EQUAL;
527   return GST_VALUE_UNORDERED;
528 }
529
530 static char *
531 gst_value_serialize_fourcc (const GValue * value)
532 {
533   guint32 fourcc = value->data[0].v_int;
534
535   if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
536       g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
537       g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
538       g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
539     return g_strdup_printf (GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
540   } else {
541     return g_strdup_printf ("0x%08x", fourcc);
542   }
543 }
544
545 static gboolean
546 gst_value_deserialize_fourcc (GValue * dest, const char *s)
547 {
548   gboolean ret = FALSE;
549   guint32 fourcc = 0;
550   char *end;
551
552   if (strlen (s) == 4) {
553     fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
554     ret = TRUE;
555   } else if (g_ascii_isdigit (*s)) {
556     fourcc = strtoul (s, &end, 0);
557     if (*end == 0) {
558       ret = TRUE;
559     }
560   }
561   gst_value_set_fourcc (dest, fourcc);
562
563   return ret;
564 }
565
566 /*************
567  * int range *
568  *************/
569
570 static void
571 gst_value_init_int_range (GValue * value)
572 {
573   value->data[0].v_int = 0;
574   value->data[1].v_int = 0;
575 }
576
577 static void
578 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
579 {
580   dest_value->data[0].v_int = src_value->data[0].v_int;
581   dest_value->data[1].v_int = src_value->data[1].v_int;
582 }
583
584 static gchar *
585 gst_value_collect_int_range (GValue * value, guint n_collect_values,
586     GTypeCValue * collect_values, guint collect_flags)
587 {
588   value->data[0].v_int = collect_values[0].v_int;
589   value->data[1].v_int = collect_values[1].v_int;
590
591   return NULL;
592 }
593
594 static gchar *
595 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
596     GTypeCValue * collect_values, guint collect_flags)
597 {
598   guint32 *int_range_start = collect_values[0].v_pointer;
599   guint32 *int_range_end = collect_values[1].v_pointer;
600
601   if (!int_range_start)
602     return g_strdup_printf ("start value location for `%s' passed as NULL",
603         G_VALUE_TYPE_NAME (value));
604   if (!int_range_end)
605     return g_strdup_printf ("end value location for `%s' passed as NULL",
606         G_VALUE_TYPE_NAME (value));
607
608   *int_range_start = value->data[0].v_int;
609   *int_range_end = value->data[1].v_int;
610
611   return NULL;
612 }
613
614 /**
615  * gst_value_set_int_range:
616  * @value: a GValue initialized to GST_TYPE_INT_RANGE
617  * @start: the start of the range
618  * @end: the end of the range
619  *
620  * Sets @value to the range specified by @start and @end.
621  */
622 void
623 gst_value_set_int_range (GValue * value, int start, int end)
624 {
625   g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
626   g_return_if_fail (start < end);
627
628   value->data[0].v_int = start;
629   value->data[1].v_int = end;
630 }
631
632 /**
633  * gst_value_get_int_range_min:
634  * @value: a GValue initialized to GST_TYPE_INT_RANGE
635  *
636  * Gets the minimum of the range specified by @value.
637  *
638  * Returns: the minimum of the range
639  */
640 int
641 gst_value_get_int_range_min (const GValue * value)
642 {
643   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
644
645   return value->data[0].v_int;
646 }
647
648 /**
649  * gst_value_get_int_range_max:
650  * @value: a GValue initialized to GST_TYPE_INT_RANGE
651  *
652  * Gets the maximum of the range specified by @value.
653  *
654  * Returns: the maxumum of the range
655  */
656 int
657 gst_value_get_int_range_max (const GValue * value)
658 {
659   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
660
661   return value->data[1].v_int;
662 }
663
664 static void
665 gst_value_transform_int_range_string (const GValue * src_value,
666     GValue * dest_value)
667 {
668   dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
669       (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
670 }
671
672 static int
673 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
674 {
675   if (value2->data[0].v_int == value1->data[0].v_int &&
676       value2->data[1].v_int == value1->data[1].v_int)
677     return GST_VALUE_EQUAL;
678   return GST_VALUE_UNORDERED;
679 }
680
681 static char *
682 gst_value_serialize_int_range (const GValue * value)
683 {
684   return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
685       value->data[1].v_int);
686 }
687
688 static gboolean
689 gst_value_deserialize_int_range (GValue * dest, const char *s)
690 {
691   g_warning ("unimplemented");
692   return FALSE;
693 }
694
695 /****************
696  * double range *
697  ****************/
698
699 static void
700 gst_value_init_double_range (GValue * value)
701 {
702   value->data[0].v_double = 0;
703   value->data[1].v_double = 0;
704 }
705
706 static void
707 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
708 {
709   dest_value->data[0].v_double = src_value->data[0].v_double;
710   dest_value->data[1].v_double = src_value->data[1].v_double;
711 }
712
713 static gchar *
714 gst_value_collect_double_range (GValue * value, guint n_collect_values,
715     GTypeCValue * collect_values, guint collect_flags)
716 {
717   value->data[0].v_double = collect_values[0].v_double;
718   value->data[1].v_double = collect_values[1].v_double;
719
720   return NULL;
721 }
722
723 static gchar *
724 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
725     GTypeCValue * collect_values, guint collect_flags)
726 {
727   gdouble *double_range_start = collect_values[0].v_pointer;
728   gdouble *double_range_end = collect_values[1].v_pointer;
729
730   if (!double_range_start)
731     return g_strdup_printf ("start value location for `%s' passed as NULL",
732         G_VALUE_TYPE_NAME (value));
733   if (!double_range_end)
734     return g_strdup_printf ("end value location for `%s' passed as NULL",
735         G_VALUE_TYPE_NAME (value));
736
737   *double_range_start = value->data[0].v_double;
738   *double_range_end = value->data[1].v_double;
739
740   return NULL;
741 }
742
743 /**
744  * gst_value_set_double_range:
745  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
746  * @start: the start of the range
747  * @end: the end of the range
748  *
749  * Sets @value to the range specified by @start and @end.
750  */
751 void
752 gst_value_set_double_range (GValue * value, double start, double end)
753 {
754   g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
755
756   value->data[0].v_double = start;
757   value->data[1].v_double = end;
758 }
759
760 /**
761  * gst_value_get_double_range_min:
762  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
763  *
764  * Gets the minimum of the range specified by @value.
765  *
766  * Returns: the minumum of the range
767  */
768 double
769 gst_value_get_double_range_min (const GValue * value)
770 {
771   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
772
773   return value->data[0].v_double;
774 }
775
776 /**
777  * gst_value_get_double_range_max:
778  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
779  *
780  * Gets the maximum of the range specified by @value.
781  *
782  * Returns: the maxumum of the range
783  */
784 double
785 gst_value_get_double_range_max (const GValue * value)
786 {
787   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
788
789   return value->data[1].v_double;
790 }
791
792 static void
793 gst_value_transform_double_range_string (const GValue * src_value,
794     GValue * dest_value)
795 {
796   char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
797
798   dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
799       g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
800           src_value->data[0].v_double),
801       g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
802           src_value->data[1].v_double));
803 }
804
805 static int
806 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
807 {
808   if (value2->data[0].v_double == value1->data[0].v_double &&
809       value2->data[0].v_double == value1->data[0].v_double)
810     return GST_VALUE_EQUAL;
811   return GST_VALUE_UNORDERED;
812 }
813
814 static char *
815 gst_value_serialize_double_range (const GValue * value)
816 {
817   char d1[G_ASCII_DTOSTR_BUF_SIZE];
818   char d2[G_ASCII_DTOSTR_BUF_SIZE];
819
820   g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
821   g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
822   return g_strdup_printf ("[ %s, %s ]", d1, d2);
823 }
824
825 static gboolean
826 gst_value_deserialize_double_range (GValue * dest, const char *s)
827 {
828   g_warning ("unimplemented");
829   return FALSE;
830 }
831
832 /***********
833  * GstCaps *
834  ***********/
835
836 /**
837  * gst_value_set_caps:
838  * @value: a GValue initialized to GST_TYPE_CAPS
839  * @caps: the caps to set the value to
840  *
841  * Sets the contents of @value to coorespond to @caps.  The actual
842  * #GstCaps structure is copied before it is used.
843  */
844 void
845 gst_value_set_caps (GValue * value, const GstCaps * caps)
846 {
847   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
848
849   g_value_set_boxed (value, caps);
850 }
851
852 /**
853  * gst_value_get_caps:
854  * @value: a GValue initialized to GST_TYPE_CAPS
855  *
856  * Gets the contents of @value.
857  *
858  * Returns: the contents of @value
859  */
860 const GstCaps *
861 gst_value_get_caps (const GValue * value)
862 {
863   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
864
865   return (GstCaps *) g_value_get_boxed (value);
866 }
867
868 static char *
869 gst_value_serialize_caps (const GValue * value)
870 {
871   GstCaps *caps = g_value_get_boxed (value);
872
873   return gst_caps_to_string (caps);
874 }
875
876 static gboolean
877 gst_value_deserialize_caps (GValue * dest, const char *s)
878 {
879   GstCaps *caps;
880
881   caps = gst_caps_from_string (s);
882
883   if (caps) {
884     g_value_set_boxed (dest, caps);
885     return TRUE;
886   }
887   return FALSE;
888 }
889
890
891 /*************
892  * GstBuffer *
893  *************/
894
895 static int
896 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
897 {
898   GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
899   GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
900
901   if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
902     return GST_VALUE_UNORDERED;
903   if (GST_BUFFER_SIZE (buf1) == 0)
904     return GST_VALUE_EQUAL;
905   g_assert (GST_BUFFER_DATA (buf1));
906   g_assert (GST_BUFFER_DATA (buf2));
907   if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
908           GST_BUFFER_SIZE (buf1)) == 0)
909     return GST_VALUE_EQUAL;
910
911   return GST_VALUE_UNORDERED;
912 }
913
914 static char *
915 gst_value_serialize_buffer (const GValue * value)
916 {
917   guint8 *data;
918   int i;
919   int size;
920   char *string;
921   GstBuffer *buffer = GST_BUFFER (gst_value_get_mini_object (value));
922
923   data = GST_BUFFER_DATA (buffer);
924   size = GST_BUFFER_SIZE (buffer);
925
926   string = g_malloc (size * 2 + 1);
927   for (i = 0; i < size; i++) {
928     sprintf (string + i * 2, "%02x", data[i]);
929   }
930   string[size * 2] = 0;
931
932   return string;
933 }
934
935 static gboolean
936 gst_value_deserialize_buffer (GValue * dest, const char *s)
937 {
938   GstBuffer *buffer;
939   gboolean ret = TRUE;
940   int len;
941   char ts[3];
942   guint8 *data;
943   int i;
944
945   len = strlen (s);
946   if (len & 1)
947     return FALSE;
948   buffer = gst_buffer_new_and_alloc (len / 2);
949   data = GST_BUFFER_DATA (buffer);
950   for (i = 0; i < len / 2; i++) {
951     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1])) {
952       ret = FALSE;
953       break;
954     }
955     ts[0] = s[i * 2 + 0];
956     ts[1] = s[i * 2 + 1];
957     ts[2] = 0;
958
959     data[i] = strtoul (ts, NULL, 16);
960   }
961
962   if (ret) {
963     gst_value_take_mini_object (dest, GST_MINI_OBJECT (buffer));
964     return TRUE;
965   } else {
966     gst_buffer_unref (buffer);
967     return FALSE;
968   }
969 }
970
971
972 /***********
973  * boolean *
974  ***********/
975
976 static int
977 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
978 {
979   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
980     return GST_VALUE_EQUAL;
981   return GST_VALUE_UNORDERED;
982 }
983
984 static char *
985 gst_value_serialize_boolean (const GValue * value)
986 {
987   if (value->data[0].v_int) {
988     return g_strdup ("true");
989   }
990   return g_strdup ("false");
991 }
992
993 static gboolean
994 gst_value_deserialize_boolean (GValue * dest, const char *s)
995 {
996   gboolean ret = FALSE;
997
998   if (g_ascii_strcasecmp (s, "true") == 0 ||
999       g_ascii_strcasecmp (s, "yes") == 0 ||
1000       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1001     g_value_set_boolean (dest, TRUE);
1002     ret = TRUE;
1003   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1004       g_ascii_strcasecmp (s, "no") == 0 ||
1005       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1006     g_value_set_boolean (dest, FALSE);
1007     ret = TRUE;
1008   }
1009
1010   return ret;
1011 }
1012
1013 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
1014 static gint                                                             \
1015 gst_value_compare_ ## _type                                             \
1016 (const GValue * value1, const GValue * value2)                          \
1017 {                                                                       \
1018   g ## _type val1 = g_value_get_ ## _type (value1);                     \
1019   g ## _type val2 = g_value_get_ ## _type (value2);                     \
1020   if (val1 > val2)                                                      \
1021     return GST_VALUE_GREATER_THAN;                                      \
1022   if (val1 < val2)                                                      \
1023     return GST_VALUE_LESS_THAN;                                         \
1024   return GST_VALUE_EQUAL;                                               \
1025 }                                                                       \
1026                                                                         \
1027 static char *                                                           \
1028 gst_value_serialize_ ## _type (const GValue * value)                    \
1029 {                                                                       \
1030   GValue val = { 0, };                                                  \
1031   g_value_init (&val, G_TYPE_STRING);                                   \
1032   if (!g_value_transform (value, &val))                                 \
1033     g_assert_not_reached ();                                            \
1034   /* NO_COPY_MADNESS!!! */                                              \
1035   return (char *) g_value_get_string (&val);                            \
1036 }
1037
1038 /* deserialize the given s into to as a gint64.
1039  * check if the result is actually storeable in the given size number of
1040  * bytes.
1041  */
1042 static gboolean
1043 gst_value_deserialize_int_helper (gint64 * to, const char *s,
1044     gint64 min, gint64 max, int size)
1045 {
1046   gboolean ret = FALSE;
1047   char *end;
1048   gint64 mask = -1;
1049
1050   errno = 0;
1051   *to = g_ascii_strtoull (s, &end, 0);
1052   /* a range error is a definitive no-no */
1053   if (errno == ERANGE) {
1054     return FALSE;
1055   }
1056
1057   if (*end == 0) {
1058     ret = TRUE;
1059   } else {
1060     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1061       *to = G_LITTLE_ENDIAN;
1062       ret = TRUE;
1063     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1064       *to = G_BIG_ENDIAN;
1065       ret = TRUE;
1066     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1067       *to = G_BYTE_ORDER;
1068       ret = TRUE;
1069     } else if (g_ascii_strcasecmp (s, "min") == 0) {
1070       *to = min;
1071       ret = TRUE;
1072     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1073       *to = max;
1074       ret = TRUE;
1075     }
1076   }
1077   if (ret) {
1078     /* by definition, a gint64 fits into a gint64; so ignore those */
1079     if (size != sizeof (mask)) {
1080       if (*to >= 0) {
1081         /* for positive numbers, we create a mask of 1's outside of the range
1082          * and 0's inside the range.  An and will thus keep only 1 bits
1083          * outside of the range */
1084         mask <<= (size * 8);
1085         if ((mask & *to) != 0) {
1086           ret = FALSE;
1087         }
1088       } else {
1089         /* for negative numbers, we do a 2's complement version */
1090         mask <<= ((size * 8) - 1);
1091         if ((mask & *to) != mask) {
1092           ret = FALSE;
1093         }
1094       }
1095     }
1096   }
1097   return ret;
1098 }
1099
1100 #define CREATE_SERIALIZATION(_type,_macro)                              \
1101 CREATE_SERIALIZATION_START(_type,_macro)                                \
1102                                                                         \
1103 static gboolean                                                         \
1104 gst_value_deserialize_ ## _type (GValue * dest, const char *s)          \
1105 {                                                                       \
1106   gint64 x;                                                             \
1107                                                                         \
1108   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
1109       G_MAX ## _macro, sizeof (g ## _type))) {                          \
1110     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
1111     return TRUE;                                                        \
1112   } else {                                                              \
1113     return FALSE;                                                       \
1114   }                                                                     \
1115 }
1116
1117 #define CREATE_USERIALIZATION(_type,_macro)                             \
1118 CREATE_SERIALIZATION_START(_type,_macro)                                \
1119                                                                         \
1120 static gboolean                                                         \
1121 gst_value_deserialize_ ## _type (GValue * dest, const char *s)          \
1122 {                                                                       \
1123   gint64 x;                                                             \
1124   char *end;                                                            \
1125   gboolean ret = FALSE;                                                 \
1126                                                                         \
1127   errno = 0;                                                            \
1128   x = g_ascii_strtoull (s, &end, 0);                                    \
1129   /* a range error is a definitive no-no */                             \
1130   if (errno == ERANGE) {                                                \
1131     return FALSE;                                                       \
1132   }                                                                     \
1133   /* the cast ensures the range check later on makes sense */           \
1134   x = (g ## _type) x;                                                   \
1135   if (*end == 0) {                                                      \
1136     ret = TRUE;                                                         \
1137   } else {                                                              \
1138     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
1139       x = G_LITTLE_ENDIAN;                                              \
1140       ret = TRUE;                                                       \
1141     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
1142       x = G_BIG_ENDIAN;                                                 \
1143       ret = TRUE;                                                       \
1144     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
1145       x = G_BYTE_ORDER;                                                 \
1146       ret = TRUE;                                                       \
1147     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
1148       x = 0;                                                            \
1149       ret = TRUE;                                                       \
1150     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
1151       x = G_MAX ## _macro;                                              \
1152       ret = TRUE;                                                       \
1153     }                                                                   \
1154   }                                                                     \
1155   if (ret) {                                                            \
1156     if (x > G_MAX ## _macro) {                                          \
1157       ret = FALSE;                                                      \
1158     } else {                                                            \
1159       g_value_set_ ## _type (dest, x);                                  \
1160     }                                                                   \
1161   }                                                                     \
1162   return ret;                                                           \
1163 }
1164
1165 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
1166 G_STMT_START {                                                          \
1167   static const GstValueTable gst_value = {                              \
1168     _gtype,                                                             \
1169     gst_value_compare_ ## _type,                                        \
1170     gst_value_serialize_ ## _type,                                      \
1171     gst_value_deserialize_ ## _type,                                    \
1172   };                                                                    \
1173                                                                         \
1174   gst_value_register (&gst_value);                                      \
1175 } G_STMT_END
1176
1177 CREATE_SERIALIZATION (int, INT);
1178 CREATE_SERIALIZATION (int64, INT64);
1179 CREATE_SERIALIZATION (long, LONG);
1180
1181 CREATE_USERIALIZATION (uint, UINT);
1182 CREATE_USERIALIZATION (uint64, UINT64);
1183 CREATE_USERIALIZATION (ulong, ULONG);
1184
1185 /**********
1186  * double *
1187  **********/
1188 static int
1189 gst_value_compare_double (const GValue * value1, const GValue * value2)
1190 {
1191   if (value1->data[0].v_double > value2->data[0].v_double)
1192     return GST_VALUE_GREATER_THAN;
1193   if (value1->data[0].v_double < value2->data[0].v_double)
1194     return GST_VALUE_LESS_THAN;
1195   if (value1->data[0].v_double == value2->data[0].v_double)
1196     return GST_VALUE_EQUAL;
1197   return GST_VALUE_UNORDERED;
1198 }
1199
1200 static char *
1201 gst_value_serialize_double (const GValue * value)
1202 {
1203   char d[G_ASCII_DTOSTR_BUF_SIZE];
1204
1205   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1206   return g_strdup (d);
1207 }
1208
1209 static gboolean
1210 gst_value_deserialize_double (GValue * dest, const char *s)
1211 {
1212   double x;
1213   gboolean ret = FALSE;
1214   char *end;
1215
1216   x = g_ascii_strtod (s, &end);
1217   if (*end == 0) {
1218     ret = TRUE;
1219   } else {
1220     if (g_ascii_strcasecmp (s, "min") == 0) {
1221       x = -G_MAXDOUBLE;
1222       ret = TRUE;
1223     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1224       x = G_MAXDOUBLE;
1225       ret = TRUE;
1226     }
1227   }
1228   if (ret) {
1229     g_value_set_double (dest, x);
1230   }
1231   return ret;
1232 }
1233
1234 /*********
1235  * float *
1236  *********/
1237
1238 static int
1239 gst_value_compare_float (const GValue * value1, const GValue * value2)
1240 {
1241   if (value1->data[0].v_float > value2->data[0].v_float)
1242     return GST_VALUE_GREATER_THAN;
1243   if (value1->data[0].v_float < value2->data[0].v_float)
1244     return GST_VALUE_LESS_THAN;
1245   if (value1->data[0].v_float == value2->data[0].v_float)
1246     return GST_VALUE_EQUAL;
1247   return GST_VALUE_UNORDERED;
1248 }
1249
1250 static char *
1251 gst_value_serialize_float (const GValue * value)
1252 {
1253   char d[G_ASCII_DTOSTR_BUF_SIZE];
1254
1255   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1256   return g_strdup (d);
1257 }
1258
1259 static gboolean
1260 gst_value_deserialize_float (GValue * dest, const char *s)
1261 {
1262   double x;
1263   gboolean ret = FALSE;
1264   char *end;
1265
1266   x = g_ascii_strtod (s, &end);
1267   if (*end == 0) {
1268     ret = TRUE;
1269   } else {
1270     if (g_ascii_strcasecmp (s, "min") == 0) {
1271       x = -G_MAXFLOAT;
1272       ret = TRUE;
1273     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1274       x = G_MAXFLOAT;
1275       ret = TRUE;
1276     }
1277   }
1278   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1279     ret = FALSE;
1280   if (ret) {
1281     g_value_set_float (dest, x);
1282   }
1283   return ret;
1284 }
1285
1286 /**********
1287  * string *
1288  **********/
1289
1290 static int
1291 gst_value_compare_string (const GValue * value1, const GValue * value2)
1292 {
1293   int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1294
1295   if (x < 0)
1296     return GST_VALUE_LESS_THAN;
1297   if (x > 0)
1298     return GST_VALUE_GREATER_THAN;
1299   return GST_VALUE_EQUAL;
1300 }
1301
1302 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1303     ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1304     ((c) == '.'))
1305
1306 static gchar *
1307 gst_string_wrap (const char *s)
1308 {
1309   const gchar *t;
1310   int len;
1311   gchar *d, *e;
1312   gboolean wrap = FALSE;
1313
1314   len = 0;
1315   t = s;
1316   if (!s)
1317     return g_strdup ("");
1318   while (*t) {
1319     if (GST_ASCII_IS_STRING (*t)) {
1320       len++;
1321     } else if (*t < 0x20 || *t >= 0x7f) {
1322       wrap = TRUE;
1323       len += 4;
1324     } else {
1325       wrap = TRUE;
1326       len += 2;
1327     }
1328     t++;
1329   }
1330
1331   if (!wrap)
1332     return g_strdup (s);
1333
1334   e = d = g_malloc (len + 3);
1335
1336   *e++ = '\"';
1337   t = s;
1338   while (*t) {
1339     if (GST_ASCII_IS_STRING (*t)) {
1340       *e++ = *t++;
1341     } else if (*t < 0x20 || *t >= 0x7f) {
1342       *e++ = '\\';
1343       *e++ = '0' + ((*(guchar *) t) >> 6);
1344       *e++ = '0' + (((*t) >> 3) & 0x7);
1345       *e++ = '0' + ((*t++) & 0x7);
1346     } else {
1347       *e++ = '\\';
1348       *e++ = *t++;
1349     }
1350   }
1351   *e++ = '\"';
1352   *e = 0;
1353
1354   return d;
1355 }
1356
1357 /* 
1358  * This function takes a string delimited with double quotes (")
1359  * and unescapes any \xxx octal numbers.
1360  *
1361  * If sequences of \y are found where y is not in the range of
1362  * 0->3, y is copied unescaped.
1363  *
1364  * If \xyy is found where x is an octal number but y is not, an
1365  * error is encountered and NULL is returned.
1366  *
1367  * the input string must be \0 terminated.
1368  */
1369 static char *
1370 gst_string_unwrap (const gchar * s)
1371 {
1372   gchar *ret;
1373   gchar *read, *write;
1374
1375   /* NULL string returns NULL */
1376   if (s == NULL)
1377     return NULL;
1378
1379   /* strings not starting with " are invalid */
1380   if (*s != '"')
1381     return NULL;
1382
1383   /* make copy of original string to hold the result. This
1384    * string will always be smaller than the original */
1385   ret = g_strdup (s);
1386   read = ret;
1387   write = ret;
1388
1389   /* need to move to the next position as we parsed the " */
1390   read++;
1391
1392   while (*read) {
1393     if (GST_ASCII_IS_STRING (*read)) {
1394       /* normal chars are just copied */
1395       *write++ = *read++;
1396     } else if (*read == '"') {
1397       /* quote marks end of string */
1398       break;
1399     } else if (*read == '\\') {
1400       /* got an escape char, move to next position to read a tripplet
1401        * of octal numbers */
1402       read++;
1403       /* is the next char a possible first octal number? */
1404       if (*read >= '0' && *read <= '3') {
1405         /* parse other 2 numbers, if one of them is not in the range of
1406          * an octal number, we error. We also catch the case where a zero
1407          * byte is found here. */
1408         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1409           goto beach;
1410
1411         /* now convert the octal number to a byte again. */
1412         *write++ = ((read[0] - '0') << 6) +
1413             ((read[1] - '0') << 3) + (read[2] - '0');
1414
1415         read += 3;
1416       } else {
1417         /* if we run into a \0 here, we definately won't get a quote later */
1418         if (*read == 0)
1419           goto beach;
1420
1421         /* else copy \X sequence */
1422         *write++ = *read++;
1423       }
1424     } else {
1425       /* weird character, error */
1426       goto beach;
1427     }
1428   }
1429   /* if the string is not ending in " and zero terminated, we error */
1430   if (*read != '"' || read[1] != '\0')
1431     goto beach;
1432
1433   /* null terminate result string and return */
1434   *write++ = '\0';
1435   return ret;
1436
1437 beach:
1438   g_free (ret);
1439   return NULL;
1440 }
1441
1442 static char *
1443 gst_value_serialize_string (const GValue * value)
1444 {
1445   return gst_string_wrap (value->data[0].v_pointer);
1446 }
1447
1448 static gboolean
1449 gst_value_deserialize_string (GValue * dest, const char *s)
1450 {
1451   if (*s != '"') {
1452     if (!g_utf8_validate (s, -1, NULL))
1453       return FALSE;
1454     g_value_set_string (dest, s);
1455     return TRUE;
1456   } else {
1457     gchar *str = gst_string_unwrap (s);
1458
1459     if (!str)
1460       return FALSE;
1461     g_value_take_string (dest, str);
1462   }
1463
1464   return TRUE;
1465 }
1466
1467 /********
1468  * enum *
1469  ********/
1470
1471 static int
1472 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1473 {
1474   GEnumValue *en1, *en2;
1475   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1476   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1477
1478   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1479   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1480   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1481   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
1482   g_type_class_unref (klass1);
1483   g_type_class_unref (klass2);
1484   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
1485   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
1486   if (en1->value < en2->value)
1487     return GST_VALUE_LESS_THAN;
1488   if (en1->value > en2->value)
1489     return GST_VALUE_GREATER_THAN;
1490
1491   return GST_VALUE_EQUAL;
1492 }
1493
1494 static char *
1495 gst_value_serialize_enum (const GValue * value)
1496 {
1497   GEnumValue *en;
1498   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
1499
1500   g_return_val_if_fail (klass, NULL);
1501   en = g_enum_get_value (klass, g_value_get_enum (value));
1502   g_type_class_unref (klass);
1503   g_return_val_if_fail (en, NULL);
1504   return g_strdup (en->value_name);
1505 }
1506
1507 static gboolean
1508 gst_value_deserialize_enum (GValue * dest, const char *s)
1509 {
1510   GEnumValue *en;
1511   gchar *endptr = NULL;
1512   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1513
1514   g_return_val_if_fail (klass, FALSE);
1515   if (!(en = g_enum_get_value_by_name (klass, s))) {
1516     if (!(en = g_enum_get_value_by_nick (klass, s))) {
1517       gint i = strtol (s, &endptr, 0);
1518
1519       if (endptr && *endptr == '\0') {
1520         en = g_enum_get_value (klass, i);
1521       }
1522     }
1523   }
1524   g_type_class_unref (klass);
1525   g_return_val_if_fail (en, FALSE);
1526   g_value_set_enum (dest, en->value);
1527   return TRUE;
1528 }
1529
1530 /********
1531  * flags *
1532  ********/
1533
1534 /* we just compare the value here */
1535 static int
1536 gst_value_compare_flags (const GValue * value1, const GValue * value2)
1537 {
1538   guint fl1, fl2;
1539   GFlagsClass *klass1 =
1540       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1541   GFlagsClass *klass2 =
1542       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1543
1544   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1545   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1546   fl1 = g_value_get_flags (value1);
1547   fl2 = g_value_get_flags (value2);
1548   g_type_class_unref (klass1);
1549   g_type_class_unref (klass2);
1550   if (fl1 < fl2)
1551     return GST_VALUE_LESS_THAN;
1552   if (fl1 > fl2)
1553     return GST_VALUE_GREATER_THAN;
1554
1555   return GST_VALUE_EQUAL;
1556 }
1557
1558 /* the different flags are serialized separated with a + */
1559 static char *
1560 gst_value_serialize_flags (const GValue * value)
1561 {
1562   guint flags;
1563   GFlagsValue *fl;
1564   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
1565   gchar *result, *tmp;
1566   gboolean first = TRUE;
1567
1568   g_return_val_if_fail (klass, NULL);
1569
1570   result = g_strdup ("");
1571   flags = g_value_get_flags (value);
1572   while (flags) {
1573     fl = g_flags_get_first_value (klass, flags);
1574     if (fl != NULL) {
1575       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
1576       g_free (result);
1577       result = tmp;
1578       first = FALSE;
1579     }
1580     /* clear flag */
1581     flags &= ~fl->value;
1582   }
1583   g_type_class_unref (klass);
1584
1585   return result;
1586 }
1587
1588 static gboolean
1589 gst_value_deserialize_flags (GValue * dest, const char *s)
1590 {
1591   GFlagsValue *fl;
1592   gchar *endptr = NULL;
1593   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1594   gchar **split;
1595   guint flags;
1596   gint i;
1597
1598   g_return_val_if_fail (klass, FALSE);
1599
1600   /* split into parts delimited with + */
1601   split = g_strsplit (s, "+", 0);
1602
1603   flags = 0;
1604   i = 0;
1605   /* loop over each part */
1606   while (split[i]) {
1607     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
1608       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
1609         gint val = strtol (split[i], &endptr, 0);
1610
1611         /* just or numeric value */
1612         if (endptr && *endptr == '\0') {
1613           flags |= val;
1614         }
1615       }
1616     }
1617     if (fl) {
1618       flags |= fl->value;
1619     }
1620     i++;
1621   }
1622   g_strfreev (split);
1623   g_type_class_unref (klass);
1624   g_value_set_flags (dest, flags);
1625
1626   return TRUE;
1627 }
1628
1629 /*********
1630  * union *
1631  *********/
1632
1633 static gboolean
1634 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
1635     const GValue * src2)
1636 {
1637   if (src2->data[0].v_int <= src1->data[0].v_int &&
1638       src2->data[1].v_int >= src1->data[0].v_int) {
1639     gst_value_init_and_copy (dest, src2);
1640     return TRUE;
1641   }
1642   return FALSE;
1643 }
1644
1645 static gboolean
1646 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
1647     const GValue * src2)
1648 {
1649   int min;
1650   int max;
1651
1652   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
1653   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
1654
1655   if (min <= max) {
1656     g_value_init (dest, GST_TYPE_INT_RANGE);
1657     gst_value_set_int_range (dest,
1658         MIN (src1->data[0].v_int, src2->data[0].v_int),
1659         MAX (src1->data[1].v_int, src2->data[1].v_int));
1660     return TRUE;
1661   }
1662
1663   return FALSE;
1664 }
1665
1666 /****************
1667  * intersection *
1668  ****************/
1669
1670 static gboolean
1671 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
1672     const GValue * src2)
1673 {
1674   if (src2->data[0].v_int <= src1->data[0].v_int &&
1675       src2->data[1].v_int >= src1->data[0].v_int) {
1676     gst_value_init_and_copy (dest, src1);
1677     return TRUE;
1678   }
1679
1680   return FALSE;
1681 }
1682
1683 static gboolean
1684 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
1685     const GValue * src2)
1686 {
1687   int min;
1688   int max;
1689
1690   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
1691   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
1692
1693   if (min < max) {
1694     g_value_init (dest, GST_TYPE_INT_RANGE);
1695     gst_value_set_int_range (dest, min, max);
1696     return TRUE;
1697   }
1698   if (min == max) {
1699     g_value_init (dest, G_TYPE_INT);
1700     g_value_set_int (dest, min);
1701     return TRUE;
1702   }
1703
1704   return FALSE;
1705 }
1706
1707 static gboolean
1708 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
1709     const GValue * src2)
1710 {
1711   if (src2->data[0].v_double <= src1->data[0].v_double &&
1712       src2->data[1].v_double >= src1->data[0].v_double) {
1713     gst_value_init_and_copy (dest, src1);
1714     return TRUE;
1715   }
1716
1717   return FALSE;
1718 }
1719
1720 static gboolean
1721 gst_value_intersect_double_range_double_range (GValue * dest,
1722     const GValue * src1, const GValue * src2)
1723 {
1724   double min;
1725   double max;
1726
1727   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
1728   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
1729
1730   if (min < max) {
1731     g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
1732     gst_value_set_double_range (dest, min, max);
1733     return TRUE;
1734   }
1735   if (min == max) {
1736     g_value_init (dest, G_TYPE_DOUBLE);
1737     g_value_set_int (dest, min);
1738     return TRUE;
1739   }
1740
1741   return FALSE;
1742 }
1743
1744 static gboolean
1745 gst_value_intersect_list (GValue * dest, const GValue * value1,
1746     const GValue * value2)
1747 {
1748   guint i, size;
1749   GValue intersection = { 0, };
1750   gboolean ret = FALSE;
1751
1752   size = gst_value_list_get_size (value1);
1753   for (i = 0; i < size; i++) {
1754     const GValue *cur = gst_value_list_get_value (value1, i);
1755
1756     if (gst_value_intersect (&intersection, cur, value2)) {
1757       /* append value */
1758       if (!ret) {
1759         gst_value_init_and_copy (dest, &intersection);
1760         ret = TRUE;
1761       } else if (GST_VALUE_HOLDS_LIST (dest)) {
1762         gst_value_list_append_value (dest, &intersection);
1763       } else {
1764         GValue temp = { 0, };
1765
1766         gst_value_init_and_copy (&temp, dest);
1767         g_value_unset (dest);
1768         gst_value_list_concat (dest, &temp, &intersection);
1769         g_value_unset (&temp);
1770       }
1771       g_value_unset (&intersection);
1772     }
1773   }
1774
1775   return ret;
1776 }
1777
1778 static gboolean
1779 gst_value_intersect_array (GValue * dest, const GValue * src1,
1780     const GValue * src2)
1781 {
1782   gint size, n;
1783   GValue val = { 0 };
1784
1785   /* only works on similar-sized arrays */
1786   size = gst_value_list_get_size (src1);
1787   if (size != gst_value_list_get_size (src2))
1788     return FALSE;
1789   g_value_init (dest, GST_TYPE_ARRAY);
1790
1791   for (n = 0; n < size; n++) {
1792     if (!gst_value_intersect (&val, gst_value_list_get_value (src1, n),
1793             gst_value_list_get_value (src2, n))) {
1794       g_value_unset (dest);
1795       return FALSE;
1796     }
1797     gst_value_list_append_value (dest, &val);
1798     g_value_unset (&val);
1799   }
1800
1801   return TRUE;
1802 }
1803
1804 /***************
1805  * subtraction *
1806  ***************/
1807
1808 static gboolean
1809 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
1810     const GValue * subtrahend)
1811 {
1812   int min = gst_value_get_int_range_min (subtrahend);
1813   int max = gst_value_get_int_range_max (subtrahend);
1814   int val = g_value_get_int (minuend);
1815
1816   /* subtracting a range from an int only works if the int is not in the
1817    * range */
1818   if (val < min || val > max) {
1819     /* and the result is the int */
1820     gst_value_init_and_copy (dest, minuend);
1821     return TRUE;
1822   }
1823   return FALSE;
1824 }
1825
1826 /* creates a new int range based on input values. 
1827  */
1828 static gboolean
1829 gst_value_create_new_range (GValue * dest, int min1, int max1, int min2,
1830     int max2)
1831 {
1832   GValue v1 = { 0, };
1833   GValue v2 = { 0, };
1834   GValue *pv1, *pv2;            /* yeah, hungarian! */
1835
1836   if (min1 <= max1 && min2 <= max2) {
1837     pv1 = &v1;
1838     pv2 = &v2;
1839   } else if (min1 <= max1) {
1840     pv1 = dest;
1841     pv2 = NULL;
1842   } else if (min2 <= max2) {
1843     pv1 = NULL;
1844     pv2 = dest;
1845   } else {
1846     return FALSE;
1847   }
1848
1849   if (min1 < max1) {
1850     g_value_init (pv1, GST_TYPE_INT_RANGE);
1851     gst_value_set_int_range (pv1, min1, max1);
1852   } else if (min1 == max1) {
1853     g_value_init (pv1, G_TYPE_INT);
1854     g_value_set_int (pv1, min1);
1855   }
1856   if (min2 < max2) {
1857     g_value_init (pv2, GST_TYPE_INT_RANGE);
1858     gst_value_set_int_range (pv2, min2, max2);
1859   } else if (min2 == max2) {
1860     g_value_init (pv2, G_TYPE_INT);
1861     g_value_set_int (pv2, min2);
1862   }
1863
1864   if (min1 <= max1 && min2 <= max2) {
1865     gst_value_list_concat (dest, pv1, pv2);
1866     g_value_unset (pv1);
1867     g_value_unset (pv2);
1868   }
1869   return TRUE;
1870 }
1871
1872 static gboolean
1873 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
1874     const GValue * subtrahend)
1875 {
1876   int min = gst_value_get_int_range_min (minuend);
1877   int max = gst_value_get_int_range_max (minuend);
1878   int val = g_value_get_int (subtrahend);
1879
1880   g_return_val_if_fail (min < max, FALSE);
1881
1882   /* value is outside of the range, return range unchanged */
1883   if (val < min || val > max) {
1884     gst_value_init_and_copy (dest, minuend);
1885     return TRUE;
1886   } else {
1887     /* max must be MAXINT too as val <= max */
1888     if (val == G_MAXINT) {
1889       max--;
1890       val--;
1891     }
1892     /* min must be MININT too as val >= max */
1893     if (val == G_MININT) {
1894       min++;
1895       val++;
1896     }
1897     gst_value_create_new_range (dest, min, val - 1, val + 1, max);
1898   }
1899   return TRUE;
1900 }
1901
1902 static gboolean
1903 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
1904     const GValue * subtrahend)
1905 {
1906   int min1 = gst_value_get_int_range_min (minuend);
1907   int max1 = gst_value_get_int_range_max (minuend);
1908   int min2 = gst_value_get_int_range_min (subtrahend);
1909   int max2 = gst_value_get_int_range_max (subtrahend);
1910
1911   if (max2 == G_MAXINT && min2 == G_MININT) {
1912     return FALSE;
1913   } else if (max2 == G_MAXINT) {
1914     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
1915   } else if (min2 == G_MININT) {
1916     return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
1917   } else {
1918     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
1919         MAX (max2 + 1, min1), max1);
1920   }
1921 }
1922
1923 static gboolean
1924 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
1925     const GValue * subtrahend)
1926 {
1927   double min = gst_value_get_double_range_min (subtrahend);
1928   double max = gst_value_get_double_range_max (subtrahend);
1929   double val = g_value_get_double (minuend);
1930
1931   if (val < min || val > max) {
1932     gst_value_init_and_copy (dest, minuend);
1933     return TRUE;
1934   }
1935   return FALSE;
1936 }
1937
1938 static gboolean
1939 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
1940     const GValue * subtrahend)
1941 {
1942   /* since we don't have open ranges, we cannot create a hole in
1943    * a double range. We return the original range */
1944   gst_value_init_and_copy (dest, minuend);
1945   return TRUE;
1946 }
1947
1948 static gboolean
1949 gst_value_subtract_double_range_double_range (GValue * dest,
1950     const GValue * minuend, const GValue * subtrahend)
1951 {
1952   /* since we don't have open ranges, we have to approximate */
1953   /* done like with ints */
1954   double min1 = gst_value_get_double_range_min (minuend);
1955   double max2 = gst_value_get_double_range_max (minuend);
1956   double max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
1957   double min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
1958   GValue v1 = { 0, };
1959   GValue v2 = { 0, };
1960   GValue *pv1, *pv2;            /* yeah, hungarian! */
1961
1962   if (min1 < max1 && min2 < max2) {
1963     pv1 = &v1;
1964     pv2 = &v2;
1965   } else if (min1 < max1) {
1966     pv1 = dest;
1967     pv2 = NULL;
1968   } else if (min2 < max2) {
1969     pv1 = NULL;
1970     pv2 = dest;
1971   } else {
1972     return FALSE;
1973   }
1974
1975   if (min1 < max1) {
1976     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
1977     gst_value_set_double_range (pv1, min1, max1);
1978   }
1979   if (min2 < max2) {
1980     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
1981     gst_value_set_double_range (pv2, min2, max2);
1982   }
1983
1984   if (min1 < max1 && min2 < max2) {
1985     gst_value_list_concat (dest, pv1, pv2);
1986     g_value_unset (pv1);
1987     g_value_unset (pv2);
1988   }
1989   return TRUE;
1990 }
1991
1992 static gboolean
1993 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
1994     const GValue * subtrahend)
1995 {
1996   guint i, size;
1997   GValue subtraction = { 0, };
1998   gboolean ret = FALSE;
1999
2000   size = gst_value_list_get_size (minuend);
2001   for (i = 0; i < size; i++) {
2002     const GValue *cur = gst_value_list_get_value (minuend, i);
2003
2004     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2005       if (!ret) {
2006         gst_value_init_and_copy (dest, &subtraction);
2007         ret = TRUE;
2008       } else if (GST_VALUE_HOLDS_LIST (dest)
2009           && GST_VALUE_HOLDS_LIST (&subtraction)) {
2010         /* unroll */
2011         GValue unroll = { 0, };
2012
2013         gst_value_init_and_copy (&unroll, dest);
2014         g_value_unset (dest);
2015         gst_value_list_concat (dest, &unroll, &subtraction);
2016       } else if (GST_VALUE_HOLDS_LIST (dest)) {
2017         gst_value_list_append_value (dest, &subtraction);
2018       } else {
2019         GValue temp = { 0, };
2020
2021         gst_value_init_and_copy (&temp, dest);
2022         g_value_unset (dest);
2023         gst_value_list_concat (dest, &temp, &subtraction);
2024         g_value_unset (&temp);
2025       }
2026       g_value_unset (&subtraction);
2027     }
2028   }
2029   return ret;
2030 }
2031
2032 static gboolean
2033 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2034     const GValue * subtrahend)
2035 {
2036   guint i, size;
2037   GValue data[2] = { {0,}, {0,} };
2038   GValue *subtraction = &data[0], *result = &data[1];
2039
2040   gst_value_init_and_copy (result, minuend);
2041   size = gst_value_list_get_size (subtrahend);
2042   for (i = 0; i < size; i++) {
2043     const GValue *cur = gst_value_list_get_value (subtrahend, i);
2044
2045     if (gst_value_subtract (subtraction, result, cur)) {
2046       GValue *temp = result;
2047
2048       result = subtraction;
2049       subtraction = temp;
2050       g_value_unset (subtraction);
2051     } else {
2052       g_value_unset (result);
2053       return FALSE;
2054     }
2055   }
2056   gst_value_init_and_copy (dest, result);
2057   g_value_unset (result);
2058   return TRUE;
2059 }
2060
2061
2062 /**************
2063  * comparison *
2064  **************/
2065
2066 /**
2067  * gst_value_can_compare:
2068  * @value1: a value to compare
2069  * @value2: another value to compare
2070  *
2071  * Determines if @value1 and @value2 can be compared.
2072  *
2073  * Returns: TRUE if the values can be compared
2074  */
2075 gboolean
2076 gst_value_can_compare (const GValue * value1, const GValue * value2)
2077 {
2078   GstValueTable *table;
2079   int i;
2080
2081   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2082     return FALSE;
2083
2084   for (i = 0; i < gst_value_table->len; i++) {
2085     table = &g_array_index (gst_value_table, GstValueTable, i);
2086     if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2087       return TRUE;
2088   }
2089
2090   return FALSE;
2091 }
2092
2093 /**
2094  * gst_value_compare:
2095  * @value1: a value to compare
2096  * @value2: another value to compare
2097  *
2098  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
2099  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
2100  * if @value1 is greater than @value2, GST_VALUE_GREATER is returned.
2101  * If @value1 is less than @value2, GST_VALUE_LESSER is returned.
2102  * If the values are equal, GST_VALUE_EQUAL is returned.
2103  *
2104  * Returns: A GstValueCompareType value
2105  */
2106 int
2107 gst_value_compare (const GValue * value1, const GValue * value2)
2108 {
2109   GstValueTable *table, *best = NULL;
2110   int i;
2111
2112   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2113     return GST_VALUE_UNORDERED;
2114
2115   for (i = 0; i < gst_value_table->len; i++) {
2116     table = &g_array_index (gst_value_table, GstValueTable, i);
2117     if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2118       best = table;
2119       break;
2120     }
2121     if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2122       if (!best || g_type_is_a (table->type, best->type))
2123         best = table;
2124     }
2125   }
2126   if (best) {
2127     return best->compare (value1, value2);
2128   }
2129
2130   g_critical ("unable to compare values of type %s\n",
2131       g_type_name (G_VALUE_TYPE (value1)));
2132   return GST_VALUE_UNORDERED;
2133 }
2134
2135 /* union */
2136
2137 /**
2138  * gst_value_can_union:
2139  * @value1: a value to union
2140  * @value2: another value to union
2141  *
2142  * Determines if @value1 and @value2 can be non-trivially unioned.
2143  * Any two values can be trivially unioned by adding both of them
2144  * to a GstValueList.  However, certain types have the possibility
2145  * to be unioned in a simpler way.  For example, an integer range
2146  * and an integer can be unioned if the integer is a subset of the
2147  * integer range.  If there is the possibility that two values can
2148  * be unioned, this function returns TRUE.
2149  *
2150  * Returns: TRUE if there is a function allowing the two values to
2151  * be unioned.
2152  */
2153 gboolean
2154 gst_value_can_union (const GValue * value1, const GValue * value2)
2155 {
2156   GstValueUnionInfo *union_info;
2157   int i;
2158
2159   for (i = 0; i < gst_value_union_funcs->len; i++) {
2160     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2161     if (union_info->type1 == G_VALUE_TYPE (value1) &&
2162         union_info->type2 == G_VALUE_TYPE (value2))
2163       return TRUE;
2164     if (union_info->type1 == G_VALUE_TYPE (value2) &&
2165         union_info->type2 == G_VALUE_TYPE (value1))
2166       return TRUE;
2167   }
2168
2169   return FALSE;
2170 }
2171
2172 /**
2173  * gst_value_union:
2174  * @dest: the destination value
2175  * @value1: a value to union
2176  * @value2: another value to union
2177  *
2178  * Creates a GValue cooresponding to the union of @value1 and @value2.
2179  *
2180  * Returns: TRUE if the values could be unioned
2181  */
2182 gboolean
2183 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2184 {
2185   GstValueUnionInfo *union_info;
2186   int i;
2187
2188   for (i = 0; i < gst_value_union_funcs->len; i++) {
2189     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2190     if (union_info->type1 == G_VALUE_TYPE (value1) &&
2191         union_info->type2 == G_VALUE_TYPE (value2)) {
2192       if (union_info->func (dest, value1, value2)) {
2193         return TRUE;
2194       }
2195     }
2196     if (union_info->type1 == G_VALUE_TYPE (value2) &&
2197         union_info->type2 == G_VALUE_TYPE (value1)) {
2198       if (union_info->func (dest, value2, value1)) {
2199         return TRUE;
2200       }
2201     }
2202   }
2203
2204   gst_value_list_concat (dest, value1, value2);
2205   return TRUE;
2206 }
2207
2208 /**
2209  * gst_value_register_union_func:
2210  * @type1: a type to union
2211  * @type2: another type to union
2212  * @func: a function that implments creating a union between the two types
2213  *
2214  * Registers a union function that can create a union between GValues
2215  * of the type @type1 and @type2.
2216  *
2217  */
2218 void
2219 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
2220 {
2221   GstValueUnionInfo union_info;
2222
2223   union_info.type1 = type1;
2224   union_info.type2 = type2;
2225   union_info.func = func;
2226
2227   g_array_append_val (gst_value_union_funcs, union_info);
2228 }
2229
2230 /* intersection */
2231
2232 /**
2233  * gst_value_can_intersect:
2234  * @value1: a value to intersect
2235  * @value2: another value to intersect
2236  *
2237  * Determines if intersecting two values will produce a valid result.
2238  * Two values will produce a valid intersection if they have the same
2239  * type, or if there is a method (registered by
2240  * #gst_value_register_intersection_func) to calculate the intersection.
2241  *
2242  * Returns: TRUE if the values can intersect
2243  */
2244 gboolean
2245 gst_value_can_intersect (const GValue * value1, const GValue * value2)
2246 {
2247   GstValueIntersectInfo *intersect_info;
2248   int i;
2249
2250   /* special cases */
2251   if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
2252     return TRUE;
2253
2254   for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2255     intersect_info = &g_array_index (gst_value_intersect_funcs,
2256         GstValueIntersectInfo, i);
2257     if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2258         intersect_info->type2 == G_VALUE_TYPE (value2))
2259       if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
2260           intersect_info->type1 == G_VALUE_TYPE (value2))
2261         return TRUE;
2262   }
2263
2264   return gst_value_can_compare (value1, value2);
2265 }
2266
2267 /**
2268  * gst_value_intersect:
2269  * @dest: a uninitialized #GValue that will hold the calculated
2270  * intersection value
2271  * @value1: a value to intersect
2272  * @value2: another value to intersect
2273  *
2274  * Calculates the intersection of two values.  If the values have
2275  * a non-empty intersection, the value representing the intersection
2276  * is placed in @dest.  If the intersection is non-empty, @dest is
2277  * not modified.
2278  *
2279  * Returns: TRUE if the intersection is non-empty
2280  */
2281 gboolean
2282 gst_value_intersect (GValue * dest, const GValue * value1,
2283     const GValue * value2)
2284 {
2285   GstValueIntersectInfo *intersect_info;
2286   int i;
2287   int ret = FALSE;
2288
2289   /* special cases first */
2290   if (GST_VALUE_HOLDS_LIST (value1))
2291     return gst_value_intersect_list (dest, value1, value2);
2292   if (GST_VALUE_HOLDS_LIST (value2))
2293     return gst_value_intersect_list (dest, value2, value1);
2294
2295   for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2296     intersect_info = &g_array_index (gst_value_intersect_funcs,
2297         GstValueIntersectInfo, i);
2298     if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2299         intersect_info->type2 == G_VALUE_TYPE (value2)) {
2300       ret = intersect_info->func (dest, value1, value2);
2301       return ret;
2302     }
2303     if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
2304         intersect_info->type2 == G_VALUE_TYPE (value1)) {
2305       ret = intersect_info->func (dest, value2, value1);
2306       return ret;
2307     }
2308   }
2309
2310   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
2311     gst_value_init_and_copy (dest, value1);
2312     ret = TRUE;
2313   }
2314
2315   return ret;
2316 }
2317
2318 /**
2319  * gst_value_register_intersection_func:
2320  * @type1: the first type to intersect
2321  * @type2: the second type to intersect
2322  * @func: the intersection function
2323  *
2324  * Registers a function that is called to calculate the intersection
2325  * of the values having the types @type1 and @type2.
2326  */
2327 /**
2328  * GstValueIntersectFunc:
2329  * @dest: a uninitialized #GValue that will hold the calculated
2330  * intersection value
2331  * @value1: a value to intersect
2332  * @value2: another value to intersect
2333  *
2334  * Functions having this type calculate the intersection of @value1
2335  * and @value2.  If the intersection is non-empty, the result is
2336  * placed in @dest and TRUE is returned.  If the intersection is
2337  * empty, @dest is unmodified and FALSE is returned.
2338  *
2339  * Returns: TRUE if the intersection is non-empty, FALSE otherwise
2340  */
2341 void
2342 gst_value_register_intersect_func (GType type1, GType type2,
2343     GstValueIntersectFunc func)
2344 {
2345   GstValueIntersectInfo intersect_info;
2346
2347   intersect_info.type1 = type1;
2348   intersect_info.type2 = type2;
2349   intersect_info.func = func;
2350
2351   g_array_append_val (gst_value_intersect_funcs, intersect_info);
2352 }
2353
2354
2355 /* subtraction */
2356
2357 /**
2358  * gst_value_subtract:
2359  * @dest: the destination value for the result if the subtraction is not empty
2360  * @minuend: the value to subtract from
2361  * @subtrahend: the value to subtract
2362  *
2363  * Subtracts @subtrahend from @minuend and stores the result in @dest.
2364  * Note that this means subtraction as in sets, not as in mathematics.
2365  *
2366  * Returns: TRUE if the subtraction is not empty
2367  */
2368 gboolean
2369 gst_value_subtract (GValue * dest, const GValue * minuend,
2370     const GValue * subtrahend)
2371 {
2372   GstValueSubtractInfo *info;
2373   int i;
2374
2375   /* special cases first */
2376   if (GST_VALUE_HOLDS_LIST (minuend))
2377     return gst_value_subtract_from_list (dest, minuend, subtrahend);
2378   if (GST_VALUE_HOLDS_LIST (subtrahend))
2379     return gst_value_subtract_list (dest, minuend, subtrahend);
2380
2381   for (i = 0; i < gst_value_subtract_funcs->len; i++) {
2382     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
2383     if (info->minuend == G_VALUE_TYPE (minuend) &&
2384         info->subtrahend == G_VALUE_TYPE (subtrahend)) {
2385       return info->func (dest, minuend, subtrahend);
2386     }
2387   }
2388
2389   if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
2390     gst_value_init_and_copy (dest, minuend);
2391     return TRUE;
2392   }
2393
2394   return FALSE;
2395 }
2396
2397 #if 0
2398 gboolean
2399 gst_value_subtract (GValue * dest, const GValue * minuend,
2400     const GValue * subtrahend)
2401 {
2402   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
2403
2404   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
2405       gst_value_serialize (subtrahend),
2406       ret ? gst_value_serialize (dest) : "---");
2407   return ret;
2408 }
2409 #endif
2410
2411 /**
2412  * gst_value_can_subtract:
2413  * @minuend: the value to subtract from
2414  * @subtrahend: the value to subtract
2415  *
2416  * Checks if it's possible to subtract @subtrahend from @minuend.
2417  *
2418  * Returns: TRUE if a subtraction is possible
2419  */
2420 gboolean
2421 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
2422 {
2423   GstValueSubtractInfo *info;
2424   int i;
2425
2426   /* special cases */
2427   if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
2428     return TRUE;
2429
2430   for (i = 0; i < gst_value_subtract_funcs->len; i++) {
2431     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
2432     if (info->minuend == G_VALUE_TYPE (minuend) &&
2433         info->subtrahend == G_VALUE_TYPE (subtrahend))
2434       return TRUE;
2435   }
2436
2437   return gst_value_can_compare (minuend, subtrahend);
2438 }
2439
2440 /**
2441  * gst_value_register_subtract_func:
2442  * @minuend_type: type of the minuend
2443  * @subtrahend_type: type of the subtrahend
2444  * @func: function to use
2445  *
2446  * Registers @func as a function capable of subtracting the values of 
2447  * @subtrahend_type from values of @minuend_type.
2448  */
2449 void
2450 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
2451     GstValueSubtractFunc func)
2452 {
2453   GstValueSubtractInfo info;
2454
2455   /* one type must be unfixed, other subtractions can be done as comparisons */
2456   g_return_if_fail (!gst_type_is_fixed (minuend_type)
2457       || !gst_type_is_fixed (subtrahend_type));
2458
2459   info.minuend = minuend_type;
2460   info.subtrahend = subtrahend_type;
2461   info.func = func;
2462
2463   g_array_append_val (gst_value_subtract_funcs, info);
2464 }
2465
2466 /**
2467  * gst_value_register:
2468  * @table: structure containing functions to register
2469  *
2470  * Registers functions to perform calculations on #GValues of a given
2471  * type.
2472  */
2473 /**
2474  * GstValueTable:
2475  * @type: GType that the functions operate on.
2476  * @compare: A function that compares two values of this type.
2477  * @serialize: A function that transforms a value of this type to a
2478  * string.  Strings created by this function must be unique and should
2479  * be human readable.
2480  * @deserialize: A function that transforms a string to a value of
2481  * this type.  This function must transform strings created by the
2482  * serialize function back to the original value.  This function may
2483  * optionally transform other strings into values.
2484  */
2485 void
2486 gst_value_register (const GstValueTable * table)
2487 {
2488   g_array_append_val (gst_value_table, *table);
2489 }
2490
2491 /**
2492  * gst_value_init_and_copy:
2493  * @dest: the target value
2494  * @src: the source value
2495  *
2496  * Initialises the target value to be of the same type as source and then copies
2497  * the contents from source to target.
2498  */
2499 void
2500 gst_value_init_and_copy (GValue * dest, const GValue * src)
2501 {
2502   g_value_init (dest, G_VALUE_TYPE (src));
2503   g_value_copy (src, dest);
2504 }
2505
2506 /**
2507  * gst_value_serialize:
2508  * @value: a #GValue to serialize
2509  *
2510  * tries to transform the given @value into a string representation that allows
2511  * getting back this string later on using gst_value_deserialize().
2512  *
2513  * Returns: the serialization for @value or NULL if none exists
2514  */
2515 gchar *
2516 gst_value_serialize (const GValue * value)
2517 {
2518   int i;
2519   GValue s_val = { 0 };
2520   GstValueTable *table, *best = NULL;
2521   char *s;
2522
2523   g_return_val_if_fail (G_IS_VALUE (value), NULL);
2524
2525   for (i = 0; i < gst_value_table->len; i++) {
2526     table = &g_array_index (gst_value_table, GstValueTable, i);
2527     if (table->serialize == NULL)
2528       continue;
2529     if (table->type == G_VALUE_TYPE (value)) {
2530       best = table;
2531       break;
2532     }
2533     if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
2534       if (!best || g_type_is_a (table->type, best->type))
2535         best = table;
2536     }
2537   }
2538   if (best)
2539     return best->serialize (value);
2540
2541   g_value_init (&s_val, G_TYPE_STRING);
2542   if (g_value_transform (value, &s_val)) {
2543     s = gst_string_wrap (g_value_get_string (&s_val));
2544   } else {
2545     s = NULL;
2546   }
2547   g_value_unset (&s_val);
2548
2549   return s;
2550 }
2551
2552 /**
2553  * gst_value_deserialize:
2554  * @dest: #GValue to fill with contents of deserialization
2555  * @src: string to deserialize
2556  *
2557  * Tries to deserialize a string into the type specified by the given GValue.
2558  * If the operation succeeds, TRUE is returned, FALSE otherwise.
2559  *
2560  * Returns: TRUE on success
2561  */
2562 gboolean
2563 gst_value_deserialize (GValue * dest, const gchar * src)
2564 {
2565   GstValueTable *table, *best = NULL;
2566   int i;
2567
2568   g_return_val_if_fail (src != NULL, FALSE);
2569   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
2570
2571   for (i = 0; i < gst_value_table->len; i++) {
2572     table = &g_array_index (gst_value_table, GstValueTable, i);
2573     if (table->serialize == NULL)
2574       continue;
2575
2576     if (table->type == G_VALUE_TYPE (dest)) {
2577       best = table;
2578       break;
2579     }
2580
2581     if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
2582       if (!best || g_type_is_a (table->type, best->type))
2583         best = table;
2584     }
2585   }
2586   if (best) {
2587     return best->deserialize (dest, src);
2588   }
2589
2590   return FALSE;
2591 }
2592
2593 /**
2594  * gst_value_is_fixed:
2595  * @value: the #GValue to check
2596  *
2597  * Tests if the given GValue, if available in a GstStructure (or any other
2598  * container) contains a "fixed" (which means: one value) or an "unfixed"
2599  * (which means: multiple possible values, such as data lists or data
2600  * ranges) value.
2601  *
2602  * Returns: true if the value is "fixed".
2603  */
2604
2605 gboolean
2606 gst_value_is_fixed (const GValue * value)
2607 {
2608   GType type = G_VALUE_TYPE (value);
2609
2610   if (type == GST_TYPE_ARRAY) {
2611     gboolean fixed = TRUE;
2612     gint size, n;
2613     const GValue *kid;
2614
2615     /* check recursively */
2616     size = gst_value_list_get_size (value);
2617     for (n = 0; n < size; n++) {
2618       kid = gst_value_list_get_value (value, n);
2619       fixed &= gst_value_is_fixed (kid);
2620     }
2621
2622     return fixed;
2623   }
2624
2625   return gst_type_is_fixed (type);
2626 }
2627
2628 /************
2629  * fraction *
2630  ************/
2631
2632 /* helper functions */
2633
2634 /* Finds the greatest common divisor.
2635  * Returns 1 if none other found.
2636  * This is Euclid's algorithm. */
2637 static gint
2638 gst_greatest_common_divisor (gint a, gint b)
2639 {
2640   while (b != 0) {
2641     int temp = a;
2642
2643     a = b;
2644     b = temp % b;
2645   }
2646
2647   return ABS (a);
2648 }
2649
2650 static void
2651 gst_value_init_fraction (GValue * value)
2652 {
2653   value->data[0].v_int = 0;
2654   value->data[1].v_int = 1;
2655 }
2656
2657 static void
2658 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
2659 {
2660   dest_value->data[0].v_int = src_value->data[0].v_int;
2661   dest_value->data[1].v_int = src_value->data[1].v_int;
2662 }
2663
2664 static gchar *
2665 gst_value_collect_fraction (GValue * value, guint n_collect_values,
2666     GTypeCValue * collect_values, guint collect_flags)
2667 {
2668   value->data[0].v_int = collect_values[0].v_int;
2669   value->data[1].v_int = collect_values[1].v_int;
2670
2671   return NULL;
2672 }
2673
2674 static gchar *
2675 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
2676     GTypeCValue * collect_values, guint collect_flags)
2677 {
2678   gint *numerator = collect_values[0].v_pointer;
2679   gint *denominator = collect_values[1].v_pointer;
2680
2681   if (!numerator)
2682     return g_strdup_printf ("numerator for `%s' passed as NULL",
2683         G_VALUE_TYPE_NAME (value));
2684   if (!denominator)
2685     return g_strdup_printf ("denominator for `%s' passed as NULL",
2686         G_VALUE_TYPE_NAME (value));
2687
2688   *numerator = value->data[0].v_int;
2689   *denominator = value->data[1].v_int;
2690
2691   return NULL;
2692 }
2693
2694 /**
2695  * gst_value_set_fraction:
2696  * @value: a GValue initialized to #GST_TYPE_FRACTION
2697  * @numerator: the numerator of the fraction
2698  * @denominator: the denominator of the fraction
2699  *
2700  * Sets @value to the fraction specified by @numerator over @denominator.
2701  * The fraction gets reduced to the smallest numerator and denominator,
2702  * and if necessary the sign is moved to the numerator.
2703  */
2704 void
2705 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
2706 {
2707   gint gcd = 0;
2708
2709   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
2710   g_return_if_fail (denominator != 0);
2711   g_return_if_fail (denominator >= -G_MAXINT);
2712   g_return_if_fail (numerator >= -G_MAXINT);
2713
2714   /* normalize sign */
2715   if (denominator < 0) {
2716     numerator = -numerator;
2717     denominator = -denominator;
2718   }
2719
2720   /* check for reduction */
2721   gcd = gst_greatest_common_divisor (numerator, denominator);
2722   if (gcd) {
2723     numerator /= gcd;
2724     denominator /= gcd;
2725   }
2726   value->data[0].v_int = numerator;
2727   value->data[1].v_int = denominator;
2728 }
2729
2730 /**
2731  * gst_value_get_fraction_numerator:
2732  * @value: a GValue initialized to #GST_TYPE_FRACTION
2733  *
2734  * Gets the numerator of the fraction specified by @value.
2735  *
2736  * Returns: the numerator of the fraction.
2737  */
2738 int
2739 gst_value_get_fraction_numerator (const GValue * value)
2740 {
2741   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
2742
2743   return value->data[0].v_int;
2744 }
2745
2746 /**
2747  * gst_value_get_fraction_denominator:
2748  * @value: a GValue initialized to #GST_TYPE_FRACTION
2749  *
2750  * Gets the denominator of the fraction specified by @value.
2751  *
2752  * Returns: the denominator of the fraction.
2753  */
2754 int
2755 gst_value_get_fraction_denominator (const GValue * value)
2756 {
2757   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
2758
2759   return value->data[1].v_int;
2760 }
2761
2762 /**
2763  * gst_value_fraction_multiply:
2764  * @product: a GValue initialized to #GST_TYPE_FRACTION
2765  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
2766  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
2767  *
2768  * Multiplies the two GValues containing a GstFraction and sets @product
2769  * to the product of the two fractions.
2770  *
2771  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
2772  */
2773 gboolean
2774 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
2775     const GValue * factor2)
2776 {
2777   gint gcd, n1, n2, d1, d2;
2778
2779   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
2780   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
2781
2782   n1 = factor1->data[0].v_int;
2783   n2 = factor2->data[0].v_int;
2784   d1 = factor1->data[1].v_int;
2785   d2 = factor2->data[1].v_int;
2786
2787   gcd = gst_greatest_common_divisor (n1, d2);
2788   n1 /= gcd;
2789   d2 /= gcd;
2790   gcd = gst_greatest_common_divisor (n2, d1);
2791   n2 /= gcd;
2792   d1 /= gcd;
2793
2794   g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
2795   g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
2796
2797   gst_value_set_fraction (product, n1 * n2, d1 * d2);
2798
2799   return TRUE;
2800 }
2801
2802 static char *
2803 gst_value_serialize_fraction (const GValue * value)
2804 {
2805   gint32 numerator = value->data[0].v_int;
2806   gint32 denominator = value->data[1].v_int;
2807   gboolean positive = TRUE;
2808
2809   /* get the sign and make components absolute */
2810   if (numerator < 0) {
2811     numerator = -numerator;
2812     positive = !positive;
2813   }
2814   if (denominator < 0) {
2815     denominator = -denominator;
2816     positive = !positive;
2817   }
2818
2819   return g_strdup_printf ("%s%d/%d",
2820       positive ? "" : "-", numerator, denominator);
2821 }
2822
2823 static gboolean
2824 gst_value_deserialize_fraction (GValue * dest, const char *s)
2825 {
2826   gint num, den;
2827
2828   if (s && sscanf (s, "%d/%d", &num, &den) == 2) {
2829     gst_value_set_fraction (dest, num, den);
2830     return TRUE;
2831   }
2832
2833   return FALSE;
2834 }
2835
2836 static void
2837 gst_value_transform_fraction_string (const GValue * src_value,
2838     GValue * dest_value)
2839 {
2840   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
2841 }
2842
2843 static void
2844 gst_value_transform_string_fraction (const GValue * src_value,
2845     GValue * dest_value)
2846 {
2847   gst_value_deserialize_fraction (dest_value, src_value->data[0].v_pointer);
2848 }
2849
2850 #define MAX_TERMS       30
2851 #define MIN_DIVISOR     1.0e-10
2852 #define MAX_ERROR       1.0e-20
2853
2854 /* use continued fractions to transform a double into a fraction,
2855  * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
2856  * This algorithm takes care of overflows.
2857  */
2858 static void
2859 gst_value_transform_double_fraction (const GValue * src_value,
2860     GValue * dest_value)
2861 {
2862   gdouble V, F;                 /* double being converted */
2863   gint N, D;                    /* will contain the result */
2864   gint A;                       /* current term in continued fraction */
2865   gint64 N1, D1;                /* numerator, denominator of last approx */
2866   gint64 N2, D2;                /* numerator, denominator of previous approx */
2867   gint i;
2868   gboolean negative = FALSE;
2869
2870   /* initialize fraction being converted */
2871   F = src_value->data[0].v_double;
2872   if (F < 0.0) {
2873     F = -F;
2874     negative = TRUE;
2875   }
2876
2877   V = F;
2878   /* initialize fractions with 1/0, 0/1 */
2879   N1 = 1;
2880   D1 = 0;
2881   N2 = 0;
2882   D2 = 1;
2883   N = 1;
2884   D = 1;
2885
2886   for (i = 0; i < MAX_TERMS; i++) {
2887     /* get next term */
2888     A = floor (F);
2889     /* get new divisor */
2890     F = F - A;
2891
2892     /* calculate new fraction in temp */
2893     N2 = N1 * A + N2;
2894     D2 = D1 * A + D2;
2895
2896     /* guard against overflow */
2897     if (N2 > G_MAXINT || D2 > G_MAXINT) {
2898       break;
2899     }
2900
2901     N = N2;
2902     D = D2;
2903
2904     /* save last two fractions */
2905     N2 = N1;
2906     D2 = D1;
2907     N1 = N;
2908     D1 = D;
2909
2910     /* quit if dividing by zero or close enough to target */
2911     if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
2912       break;
2913     }
2914
2915     /* Take reciprocal */
2916     F = 1 / F;
2917   }
2918   /* fix for overflow */
2919   if (D == 0) {
2920     N = G_MAXINT;
2921     D = 1;
2922   }
2923   /* fix for negative */
2924   if (negative)
2925     N = -N;
2926
2927   /* will also simplify */
2928   gst_value_set_fraction (dest_value, N, D);
2929 }
2930
2931 static void
2932 gst_value_transform_fraction_double (const GValue * src_value,
2933     GValue * dest_value)
2934 {
2935   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
2936       ((double) src_value->data[1].v_int);
2937 }
2938
2939 static int
2940 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
2941 {
2942   gint n1, n2;
2943   gint d1, d2;
2944
2945   gint64 new_num_1;
2946   gint64 new_num_2;
2947
2948   n1 = value1->data[0].v_int;
2949   n2 = value2->data[0].v_int;
2950   d1 = value1->data[1].v_int;
2951   d2 = value2->data[1].v_int;
2952
2953   /* fractions are reduced when set, so we can quickly see if they're equal */
2954   if (n1 == n2 && d1 == d2)
2955     return GST_VALUE_EQUAL;
2956
2957   /* extend to 64 bits */
2958   new_num_1 = ((gint64) n1) * d2;
2959   new_num_2 = ((gint64) n2) * d1;
2960   if (new_num_1 < new_num_2)
2961     return GST_VALUE_LESS_THAN;
2962   if (new_num_1 > new_num_2)
2963     return GST_VALUE_GREATER_THAN;
2964
2965   g_assert_not_reached ();
2966   return GST_VALUE_UNORDERED;
2967 }
2968
2969 /*********
2970  * GDate *
2971  *********/
2972
2973 /**
2974  * gst_value_set_date:
2975  * @value: a GValue initialized to GST_TYPE_DATE
2976  * @date: the date to set the value to
2977  *
2978  * Sets the contents of @value to coorespond to @date.  The actual
2979  * #GDate structure is copied before it is used.
2980  */
2981 void
2982 gst_value_set_date (GValue * value, const GDate * date)
2983 {
2984   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
2985
2986   g_value_set_boxed (value, date);
2987 }
2988
2989 /**
2990  * gst_value_get_date:
2991  * @value: a GValue initialized to GST_TYPE_DATE
2992  *
2993  * Gets the contents of @value.
2994  *
2995  * Returns: the contents of @value
2996  */
2997 const GDate *
2998 gst_value_get_date (const GValue * value)
2999 {
3000   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3001
3002   return (const GDate *) g_value_get_boxed (value);
3003 }
3004
3005 static gpointer
3006 gst_date_copy (gpointer boxed)
3007 {
3008   const GDate *date = (const GDate *) boxed;
3009
3010   return g_date_new_julian (g_date_get_julian (date));
3011 }
3012
3013 static int
3014 gst_value_compare_date (const GValue * value1, const GValue * value2)
3015 {
3016   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3017   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3018   guint32 j1, j2;
3019
3020   if (date1 == date2)
3021     return GST_VALUE_EQUAL;
3022
3023   if ((date1 == NULL || !g_date_valid (date1))
3024       && (date2 != NULL && g_date_valid (date2))) {
3025     return GST_VALUE_LESS_THAN;
3026   }
3027
3028   if ((date2 == NULL || !g_date_valid (date2))
3029       && (date1 != NULL && g_date_valid (date1))) {
3030     return GST_VALUE_GREATER_THAN;
3031   }
3032
3033   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3034       || !g_date_valid (date2)) {
3035     return GST_VALUE_UNORDERED;
3036   }
3037
3038   j1 = g_date_get_julian (date1);
3039   j2 = g_date_get_julian (date2);
3040
3041   if (j1 == j2)
3042     return GST_VALUE_EQUAL;
3043   else if (j1 < j2)
3044     return GST_VALUE_LESS_THAN;
3045   else
3046     return GST_VALUE_GREATER_THAN;
3047 }
3048
3049 static char *
3050 gst_value_serialize_date (const GValue * val)
3051 {
3052   const GDate *date = (const GDate *) g_value_get_boxed (val);
3053
3054   if (date == NULL || !g_date_valid (date))
3055     return g_strdup ("9999-99-99");
3056
3057   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3058       g_date_get_month (date), g_date_get_day (date));
3059 }
3060
3061 static gboolean
3062 gst_value_deserialize_date (GValue * dest, const char *s)
3063 {
3064   guint year, month, day;
3065
3066   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3067     return FALSE;
3068
3069   if (!g_date_valid_dmy (day, month, year))
3070     return FALSE;
3071
3072   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3073   return TRUE;
3074 }
3075
3076 static void
3077 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3078 {
3079   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3080 }
3081
3082 static void
3083 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3084 {
3085   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3086 }
3087
3088 void
3089 _gst_value_initialize (void)
3090 {
3091   GTypeInfo info = {
3092     0,
3093     NULL,
3094     NULL,
3095     NULL,
3096     NULL,
3097     NULL,
3098     0,
3099     0,
3100     NULL,
3101     NULL,
3102   };
3103   GTypeFundamentalInfo finfo = {
3104     0
3105   };
3106
3107   //const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
3108
3109   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
3110   gst_value_union_funcs = g_array_new (FALSE, FALSE,
3111       sizeof (GstValueUnionInfo));
3112   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
3113       sizeof (GstValueIntersectInfo));
3114   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
3115       sizeof (GstValueSubtractInfo));
3116
3117   {
3118     static const GTypeValueTable value_table = {
3119       gst_value_init_fourcc,
3120       NULL,
3121       gst_value_copy_fourcc,
3122       NULL,
3123       "i",
3124       gst_value_collect_fourcc,
3125       "p",
3126       gst_value_lcopy_fourcc
3127     };
3128     static GstValueTable gst_value = {
3129       0,
3130       gst_value_compare_fourcc,
3131       gst_value_serialize_fourcc,
3132       gst_value_deserialize_fourcc,
3133     };
3134
3135     info.value_table = &value_table;
3136     gst_type_fourcc = g_type_register_fundamental (g_type_fundamental_next (),
3137         "GstFourcc", &info, &finfo, 0);
3138     gst_value.type = gst_type_fourcc;
3139     gst_value_register (&gst_value);
3140   }
3141
3142   {
3143     static const GTypeValueTable value_table = {
3144       gst_value_init_int_range,
3145       NULL,
3146       gst_value_copy_int_range,
3147       NULL,
3148       "ii",
3149       gst_value_collect_int_range,
3150       "pp",
3151       gst_value_lcopy_int_range
3152     };
3153     static GstValueTable gst_value = {
3154       0,
3155       gst_value_compare_int_range,
3156       gst_value_serialize_int_range,
3157       gst_value_deserialize_int_range,
3158     };
3159
3160     info.value_table = &value_table;
3161     gst_type_int_range =
3162         g_type_register_fundamental (g_type_fundamental_next (), "GstIntRange",
3163         &info, &finfo, 0);
3164     gst_value.type = gst_type_int_range;
3165     gst_value_register (&gst_value);
3166   }
3167
3168   {
3169     static const GTypeValueTable value_table = {
3170       gst_value_init_double_range,
3171       NULL,
3172       gst_value_copy_double_range,
3173       NULL,
3174       "dd",
3175       gst_value_collect_double_range,
3176       "pp",
3177       gst_value_lcopy_double_range
3178     };
3179     static GstValueTable gst_value = {
3180       0,
3181       gst_value_compare_double_range,
3182       gst_value_serialize_double_range,
3183       gst_value_deserialize_double_range,
3184     };
3185
3186     info.value_table = &value_table;
3187     gst_type_double_range =
3188         g_type_register_fundamental (g_type_fundamental_next (),
3189         "GstDoubleRange", &info, &finfo, 0);
3190     gst_value.type = gst_type_double_range;
3191     gst_value_register (&gst_value);
3192   }
3193
3194   {
3195     static const GTypeValueTable value_table = {
3196       gst_value_init_list,
3197       gst_value_free_list,
3198       gst_value_copy_list,
3199       gst_value_list_peek_pointer,
3200       "p",
3201       gst_value_collect_list,
3202       "p",
3203       gst_value_lcopy_list
3204     };
3205     static GstValueTable gst_value = {
3206       0,
3207       gst_value_compare_list,
3208       gst_value_serialize_list,
3209       gst_value_deserialize_list,
3210     };
3211
3212     info.value_table = &value_table;
3213     gst_type_list = g_type_register_fundamental (g_type_fundamental_next (),
3214         "GstValueList", &info, &finfo, 0);
3215     gst_value.type = gst_type_list;
3216     gst_value_register (&gst_value);
3217   }
3218
3219   {
3220     static const GTypeValueTable value_table = {
3221       gst_value_init_list,
3222       gst_value_free_list,
3223       gst_value_copy_list,
3224       gst_value_list_peek_pointer,
3225       "p",
3226       gst_value_collect_list,
3227       "p",
3228       gst_value_lcopy_list
3229     };
3230     static GstValueTable gst_value = {
3231       0,
3232       gst_value_compare_list,
3233       gst_value_serialize_array,
3234       gst_value_deserialize_array,
3235     };
3236
3237     info.value_table = &value_table;
3238     gst_type_array =
3239         g_type_register_fundamental (g_type_fundamental_next (),
3240         "GstValueArray", &info, &finfo, 0);
3241     gst_value.type = gst_type_array;
3242     gst_value_register (&gst_value);
3243   }
3244
3245   {
3246 #if 0
3247     static const GTypeValueTable value_table = {
3248       gst_value_init_buffer,
3249       NULL,
3250       gst_value_copy_buffer,
3251       NULL,
3252       "i",
3253       NULL,                     /*gst_value_collect_buffer, */
3254       "p",
3255       NULL                      /*gst_value_lcopy_buffer */
3256     };
3257 #endif
3258     static GstValueTable gst_value = {
3259       0,
3260       gst_value_compare_buffer,
3261       gst_value_serialize_buffer,
3262       gst_value_deserialize_buffer,
3263     };
3264
3265     gst_value.type = GST_TYPE_BUFFER;
3266     gst_value_register (&gst_value);
3267   }
3268   {
3269     static const GTypeValueTable value_table = {
3270       gst_value_init_fraction,
3271       NULL,
3272       gst_value_copy_fraction,
3273       NULL,
3274       "ii",
3275       gst_value_collect_fraction,
3276       "pp",
3277       gst_value_lcopy_fraction
3278     };
3279     static GstValueTable gst_value = {
3280       0,
3281       gst_value_compare_fraction,
3282       gst_value_serialize_fraction,
3283       gst_value_deserialize_fraction,
3284     };
3285
3286     info.value_table = &value_table;
3287     gst_type_fraction =
3288         g_type_register_fundamental (g_type_fundamental_next (), "GstFraction",
3289         &info, &finfo, 0);
3290     gst_value.type = gst_type_fraction;
3291     gst_value_register (&gst_value);
3292   }
3293   {
3294     static GstValueTable gst_value = {
3295       0,
3296       NULL,
3297       gst_value_serialize_caps,
3298       gst_value_deserialize_caps,
3299     };
3300
3301     gst_value.type = GST_TYPE_CAPS;
3302     gst_value_register (&gst_value);
3303   }
3304   {
3305     static GstValueTable gst_value = {
3306       0,
3307       gst_value_compare_date,
3308       gst_value_serialize_date,
3309       gst_value_deserialize_date,
3310     };
3311
3312     /* Not using G_TYPE_DATE here on purpose, even if we could
3313      * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
3314      * serialised strings to have different type strings depending
3315      * on what version is used, so FIXME in 0.11 when we 
3316      * require GLib-2.8 */
3317     gst_type_date = g_boxed_type_register_static ("GstDate",
3318         (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
3319
3320     gst_value.type = gst_type_date;
3321     gst_value_register (&gst_value);
3322   }
3323
3324   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
3325   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
3326
3327   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
3328   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
3329   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
3330
3331   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
3332
3333   REGISTER_SERIALIZATION (G_TYPE_INT, int);
3334
3335   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
3336   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
3337
3338   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
3339   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
3340   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
3341
3342   g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
3343       gst_value_transform_fourcc_string);
3344   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
3345       gst_value_transform_int_range_string);
3346   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
3347       gst_value_transform_double_range_string);
3348   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
3349       gst_value_transform_list_string);
3350   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
3351       gst_value_transform_array_string);
3352   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
3353       gst_value_transform_fraction_string);
3354   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
3355       gst_value_transform_string_fraction);
3356   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
3357       gst_value_transform_fraction_double);
3358   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
3359       gst_value_transform_double_fraction);
3360   g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
3361       gst_value_transform_date_string);
3362   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
3363       gst_value_transform_string_date);
3364
3365   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3366       gst_value_intersect_int_int_range);
3367   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3368       gst_value_intersect_int_range_int_range);
3369   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
3370       gst_value_intersect_double_double_range);
3371   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
3372       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
3373   gst_value_register_intersect_func (GST_TYPE_ARRAY,
3374       GST_TYPE_ARRAY, gst_value_intersect_array);
3375
3376   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3377       gst_value_subtract_int_int_range);
3378   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
3379       gst_value_subtract_int_range_int);
3380   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3381       gst_value_subtract_int_range_int_range);
3382   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
3383       gst_value_subtract_double_double_range);
3384   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
3385       gst_value_subtract_double_range_double);
3386   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
3387       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
3388
3389 #if GLIB_CHECK_VERSION(2,8,0)
3390   /* see bug #317246, #64994, #65041 */
3391   {
3392     volatile GType date_type = G_TYPE_DATE;
3393
3394     GST_LOG ("Faking out the compiler: %d", date_type);
3395   }
3396 #endif
3397
3398   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3399       gst_value_union_int_int_range);
3400   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3401       gst_value_union_int_range_int_range);
3402 }