813eb3b91705523c1111e8d711210f06bd8b534b
[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 long long.
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 (long long *to, const char *s,
1044     long long min, long long max, int size)
1045 {
1046   gboolean ret = FALSE;
1047   char *end;
1048   long long 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 long long fits into a long long; 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   long long 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   guint64 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 CREATE_USERIALIZATION (uint, UINT)
1181 CREATE_USERIALIZATION (uint64, UINT64)
1182 CREATE_USERIALIZATION (ulong, ULONG)
1183
1184 /**********
1185  * double *
1186  **********/
1187      static int
1188          gst_value_compare_double (const GValue * value1, const GValue * value2)
1189 {
1190   if (value1->data[0].v_double > value2->data[0].v_double)
1191     return GST_VALUE_GREATER_THAN;
1192   if (value1->data[0].v_double < value2->data[0].v_double)
1193     return GST_VALUE_LESS_THAN;
1194   if (value1->data[0].v_double == value2->data[0].v_double)
1195     return GST_VALUE_EQUAL;
1196   return GST_VALUE_UNORDERED;
1197 }
1198
1199 static char *
1200 gst_value_serialize_double (const GValue * value)
1201 {
1202   char d[G_ASCII_DTOSTR_BUF_SIZE];
1203
1204   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1205   return g_strdup (d);
1206 }
1207
1208 static gboolean
1209 gst_value_deserialize_double (GValue * dest, const char *s)
1210 {
1211   double x;
1212   gboolean ret = FALSE;
1213   char *end;
1214
1215   x = g_ascii_strtod (s, &end);
1216   if (*end == 0) {
1217     ret = TRUE;
1218   } else {
1219     if (g_ascii_strcasecmp (s, "min") == 0) {
1220       x = -G_MAXDOUBLE;
1221       ret = TRUE;
1222     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1223       x = G_MAXDOUBLE;
1224       ret = TRUE;
1225     }
1226   }
1227   if (ret) {
1228     g_value_set_double (dest, x);
1229   }
1230   return ret;
1231 }
1232
1233 /*********
1234  * float *
1235  *********/
1236
1237 static int
1238 gst_value_compare_float (const GValue * value1, const GValue * value2)
1239 {
1240   if (value1->data[0].v_float > value2->data[0].v_float)
1241     return GST_VALUE_GREATER_THAN;
1242   if (value1->data[0].v_float < value2->data[0].v_float)
1243     return GST_VALUE_LESS_THAN;
1244   if (value1->data[0].v_float == value2->data[0].v_float)
1245     return GST_VALUE_EQUAL;
1246   return GST_VALUE_UNORDERED;
1247 }
1248
1249 static char *
1250 gst_value_serialize_float (const GValue * value)
1251 {
1252   char d[G_ASCII_DTOSTR_BUF_SIZE];
1253
1254   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1255   return g_strdup (d);
1256 }
1257
1258 static gboolean
1259 gst_value_deserialize_float (GValue * dest, const char *s)
1260 {
1261   double x;
1262   gboolean ret = FALSE;
1263   char *end;
1264
1265   x = g_ascii_strtod (s, &end);
1266   if (*end == 0) {
1267     ret = TRUE;
1268   } else {
1269     if (g_ascii_strcasecmp (s, "min") == 0) {
1270       x = -G_MAXFLOAT;
1271       ret = TRUE;
1272     } else if (g_ascii_strcasecmp (s, "max") == 0) {
1273       x = G_MAXFLOAT;
1274       ret = TRUE;
1275     }
1276   }
1277   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1278     ret = FALSE;
1279   if (ret) {
1280     g_value_set_float (dest, x);
1281   }
1282   return ret;
1283 }
1284
1285 /**********
1286  * string *
1287  **********/
1288
1289 static int
1290 gst_value_compare_string (const GValue * value1, const GValue * value2)
1291 {
1292   int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1293
1294   if (x < 0)
1295     return GST_VALUE_LESS_THAN;
1296   if (x > 0)
1297     return GST_VALUE_GREATER_THAN;
1298   return GST_VALUE_EQUAL;
1299 }
1300
1301 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1302     ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1303     ((c) == '.'))
1304
1305 static gchar *
1306 gst_string_wrap (const char *s)
1307 {
1308   const gchar *t;
1309   int len;
1310   gchar *d, *e;
1311   gboolean wrap = FALSE;
1312
1313   len = 0;
1314   t = s;
1315   if (!s)
1316     return g_strdup ("");
1317   while (*t) {
1318     if (GST_ASCII_IS_STRING (*t)) {
1319       len++;
1320     } else if (*t < 0x20 || *t >= 0x7f) {
1321       wrap = TRUE;
1322       len += 4;
1323     } else {
1324       wrap = TRUE;
1325       len += 2;
1326     }
1327     t++;
1328   }
1329
1330   if (!wrap)
1331     return g_strdup (s);
1332
1333   e = d = g_malloc (len + 3);
1334
1335   *e++ = '\"';
1336   t = s;
1337   while (*t) {
1338     if (GST_ASCII_IS_STRING (*t)) {
1339       *e++ = *t++;
1340     } else if (*t < 0x20 || *t >= 0x7f) {
1341       *e++ = '\\';
1342       *e++ = '0' + ((*(guchar *) t) >> 6);
1343       *e++ = '0' + (((*t) >> 3) & 0x7);
1344       *e++ = '0' + ((*t++) & 0x7);
1345     } else {
1346       *e++ = '\\';
1347       *e++ = *t++;
1348     }
1349   }
1350   *e++ = '\"';
1351   *e = 0;
1352
1353   return d;
1354 }
1355
1356 /* 
1357  * This function takes a string delimited with double quotes (")
1358  * and unescapes any \xxx octal numbers.
1359  *
1360  * If sequences of \y are found where y is not in the range of
1361  * 0->3, y is copied unescaped.
1362  *
1363  * If \xyy is found where x is an octal number but y is not, an
1364  * error is encountered and NULL is returned.
1365  *
1366  * the input string must be \0 terminated.
1367  */
1368 static char *
1369 gst_string_unwrap (const gchar * s)
1370 {
1371   gchar *ret;
1372   gchar *read, *write;
1373
1374   /* NULL string returns NULL */
1375   if (s == NULL)
1376     return NULL;
1377
1378   /* strings not starting with " are invalid */
1379   if (*s != '"')
1380     return NULL;
1381
1382   /* make copy of original string to hold the result. This
1383    * string will always be smaller than the original */
1384   ret = g_strdup (s);
1385   read = ret;
1386   write = ret;
1387
1388   /* need to move to the next position as we parsed the " */
1389   read++;
1390
1391   while (*read) {
1392     if (GST_ASCII_IS_STRING (*read)) {
1393       /* normal chars are just copied */
1394       *write++ = *read++;
1395     } else if (*read == '"') {
1396       /* quote marks end of string */
1397       break;
1398     } else if (*read == '\\') {
1399       /* got an escape char, move to next position to read a tripplet
1400        * of octal numbers */
1401       read++;
1402       /* is the next char a possible first octal number? */
1403       if (*read >= '0' && *read <= '3') {
1404         /* parse other 2 numbers, if one of them is not in the range of
1405          * an octal number, we error. We also catch the case where a zero
1406          * byte is found here. */
1407         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1408           goto beach;
1409
1410         /* now convert the octal number to a byte again. */
1411         *write++ = ((read[0] - '0') << 6) +
1412             ((read[1] - '0') << 3) + (read[2] - '0');
1413
1414         read += 3;
1415       } else {
1416         /* if we run into a \0 here, we definately won't get a quote later */
1417         if (*read == 0)
1418           goto beach;
1419
1420         /* else copy \X sequence */
1421         *write++ = *read++;
1422       }
1423     } else {
1424       /* weird character, error */
1425       goto beach;
1426     }
1427   }
1428   /* if the string is not ending in " and zero terminated, we error */
1429   if (*read != '"' || read[1] != '\0')
1430     goto beach;
1431
1432   /* null terminate result string and return */
1433   *write++ = '\0';
1434   return ret;
1435
1436 beach:
1437   g_free (ret);
1438   return NULL;
1439 }
1440
1441 static char *
1442 gst_value_serialize_string (const GValue * value)
1443 {
1444   return gst_string_wrap (value->data[0].v_pointer);
1445 }
1446
1447 static gboolean
1448 gst_value_deserialize_string (GValue * dest, const char *s)
1449 {
1450   if (*s != '"') {
1451     if (!g_utf8_validate (s, -1, NULL))
1452       return FALSE;
1453     g_value_set_string (dest, s);
1454     return TRUE;
1455   } else {
1456     gchar *str = gst_string_unwrap (s);
1457
1458     if (!str)
1459       return FALSE;
1460     g_value_take_string (dest, str);
1461   }
1462
1463   return TRUE;
1464 }
1465
1466 /********
1467  * enum *
1468  ********/
1469
1470 static int
1471 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1472 {
1473   GEnumValue *en1, *en2;
1474   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1475   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1476
1477   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1478   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1479   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1480   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
1481   g_type_class_unref (klass1);
1482   g_type_class_unref (klass2);
1483   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
1484   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
1485   if (en1->value < en2->value)
1486     return GST_VALUE_LESS_THAN;
1487   if (en1->value > en2->value)
1488     return GST_VALUE_GREATER_THAN;
1489
1490   return GST_VALUE_EQUAL;
1491 }
1492
1493 static char *
1494 gst_value_serialize_enum (const GValue * value)
1495 {
1496   GEnumValue *en;
1497   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
1498
1499   g_return_val_if_fail (klass, NULL);
1500   en = g_enum_get_value (klass, g_value_get_enum (value));
1501   g_type_class_unref (klass);
1502   g_return_val_if_fail (en, NULL);
1503   return g_strdup (en->value_name);
1504 }
1505
1506 static gboolean
1507 gst_value_deserialize_enum (GValue * dest, const char *s)
1508 {
1509   GEnumValue *en;
1510   gchar *endptr = NULL;
1511   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1512
1513   g_return_val_if_fail (klass, FALSE);
1514   if (!(en = g_enum_get_value_by_name (klass, s))) {
1515     if (!(en = g_enum_get_value_by_nick (klass, s))) {
1516       gint i = strtol (s, &endptr, 0);
1517
1518       if (endptr && *endptr == '\0') {
1519         en = g_enum_get_value (klass, i);
1520       }
1521     }
1522   }
1523   g_type_class_unref (klass);
1524   g_return_val_if_fail (en, FALSE);
1525   g_value_set_enum (dest, en->value);
1526   return TRUE;
1527 }
1528
1529 /********
1530  * flags *
1531  ********/
1532
1533 /* we just compare the value here */
1534 static int
1535 gst_value_compare_flags (const GValue * value1, const GValue * value2)
1536 {
1537   guint fl1, fl2;
1538   GFlagsClass *klass1 =
1539       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1540   GFlagsClass *klass2 =
1541       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1542
1543   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1544   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1545   fl1 = g_value_get_flags (value1);
1546   fl2 = g_value_get_flags (value2);
1547   g_type_class_unref (klass1);
1548   g_type_class_unref (klass2);
1549   if (fl1 < fl2)
1550     return GST_VALUE_LESS_THAN;
1551   if (fl1 > fl2)
1552     return GST_VALUE_GREATER_THAN;
1553
1554   return GST_VALUE_EQUAL;
1555 }
1556
1557 /* the different flags are serialized separated with a + */
1558 static char *
1559 gst_value_serialize_flags (const GValue * value)
1560 {
1561   guint flags;
1562   GFlagsValue *fl;
1563   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
1564   gchar *result, *tmp;
1565   gboolean first = TRUE;
1566
1567   g_return_val_if_fail (klass, NULL);
1568
1569   result = g_strdup ("");
1570   flags = g_value_get_flags (value);
1571   while (flags) {
1572     fl = g_flags_get_first_value (klass, flags);
1573     if (fl != NULL) {
1574       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
1575       g_free (result);
1576       result = tmp;
1577       first = FALSE;
1578     }
1579     /* clear flag */
1580     flags &= ~fl->value;
1581   }
1582   g_type_class_unref (klass);
1583
1584   return result;
1585 }
1586
1587 static gboolean
1588 gst_value_deserialize_flags (GValue * dest, const char *s)
1589 {
1590   GFlagsValue *fl;
1591   gchar *endptr = NULL;
1592   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1593   gchar **split;
1594   guint flags;
1595   gint i;
1596
1597   g_return_val_if_fail (klass, FALSE);
1598
1599   /* split into parts delimited with + */
1600   split = g_strsplit (s, "+", 0);
1601
1602   flags = 0;
1603   i = 0;
1604   /* loop over each part */
1605   while (split[i]) {
1606     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
1607       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
1608         gint val = strtol (split[i], &endptr, 0);
1609
1610         /* just or numeric value */
1611         if (endptr && *endptr == '\0') {
1612           flags |= val;
1613         }
1614       }
1615     }
1616     if (fl) {
1617       flags |= fl->value;
1618     }
1619     i++;
1620   }
1621   g_strfreev (split);
1622   g_type_class_unref (klass);
1623   g_value_set_flags (dest, flags);
1624
1625   return TRUE;
1626 }
1627
1628 /*********
1629  * union *
1630  *********/
1631
1632 static gboolean
1633 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
1634     const GValue * src2)
1635 {
1636   if (src2->data[0].v_int <= src1->data[0].v_int &&
1637       src2->data[1].v_int >= src1->data[0].v_int) {
1638     gst_value_init_and_copy (dest, src2);
1639     return TRUE;
1640   }
1641   return FALSE;
1642 }
1643
1644 static gboolean
1645 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
1646     const GValue * src2)
1647 {
1648   int min;
1649   int max;
1650
1651   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
1652   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
1653
1654   if (min <= max) {
1655     g_value_init (dest, GST_TYPE_INT_RANGE);
1656     gst_value_set_int_range (dest,
1657         MIN (src1->data[0].v_int, src2->data[0].v_int),
1658         MAX (src1->data[1].v_int, src2->data[1].v_int));
1659     return TRUE;
1660   }
1661
1662   return FALSE;
1663 }
1664
1665 /****************
1666  * intersection *
1667  ****************/
1668
1669 static gboolean
1670 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
1671     const GValue * src2)
1672 {
1673   if (src2->data[0].v_int <= src1->data[0].v_int &&
1674       src2->data[1].v_int >= src1->data[0].v_int) {
1675     gst_value_init_and_copy (dest, src1);
1676     return TRUE;
1677   }
1678
1679   return FALSE;
1680 }
1681
1682 static gboolean
1683 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
1684     const GValue * src2)
1685 {
1686   int min;
1687   int max;
1688
1689   min = MAX (src1->data[0].v_int, src2->data[0].v_int);
1690   max = MIN (src1->data[1].v_int, src2->data[1].v_int);
1691
1692   if (min < max) {
1693     g_value_init (dest, GST_TYPE_INT_RANGE);
1694     gst_value_set_int_range (dest, min, max);
1695     return TRUE;
1696   }
1697   if (min == max) {
1698     g_value_init (dest, G_TYPE_INT);
1699     g_value_set_int (dest, min);
1700     return TRUE;
1701   }
1702
1703   return FALSE;
1704 }
1705
1706 static gboolean
1707 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
1708     const GValue * src2)
1709 {
1710   if (src2->data[0].v_double <= src1->data[0].v_double &&
1711       src2->data[1].v_double >= src1->data[0].v_double) {
1712     gst_value_init_and_copy (dest, src1);
1713     return TRUE;
1714   }
1715
1716   return FALSE;
1717 }
1718
1719 static gboolean
1720 gst_value_intersect_double_range_double_range (GValue * dest,
1721     const GValue * src1, const GValue * src2)
1722 {
1723   double min;
1724   double max;
1725
1726   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
1727   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
1728
1729   if (min < max) {
1730     g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
1731     gst_value_set_double_range (dest, min, max);
1732     return TRUE;
1733   }
1734   if (min == max) {
1735     g_value_init (dest, G_TYPE_DOUBLE);
1736     g_value_set_int (dest, min);
1737     return TRUE;
1738   }
1739
1740   return FALSE;
1741 }
1742
1743 static gboolean
1744 gst_value_intersect_list (GValue * dest, const GValue * value1,
1745     const GValue * value2)
1746 {
1747   guint i, size;
1748   GValue intersection = { 0, };
1749   gboolean ret = FALSE;
1750
1751   size = gst_value_list_get_size (value1);
1752   for (i = 0; i < size; i++) {
1753     const GValue *cur = gst_value_list_get_value (value1, i);
1754
1755     if (gst_value_intersect (&intersection, cur, value2)) {
1756       /* append value */
1757       if (!ret) {
1758         gst_value_init_and_copy (dest, &intersection);
1759         ret = TRUE;
1760       } else if (GST_VALUE_HOLDS_LIST (dest)) {
1761         gst_value_list_append_value (dest, &intersection);
1762       } else {
1763         GValue temp = { 0, };
1764
1765         gst_value_init_and_copy (&temp, dest);
1766         g_value_unset (dest);
1767         gst_value_list_concat (dest, &temp, &intersection);
1768         g_value_unset (&temp);
1769       }
1770       g_value_unset (&intersection);
1771     }
1772   }
1773
1774   return ret;
1775 }
1776
1777 static gboolean
1778 gst_value_intersect_array (GValue * dest, const GValue * src1,
1779     const GValue * src2)
1780 {
1781   gint size, n;
1782   GValue val = { 0 };
1783
1784   /* only works on similar-sized arrays */
1785   size = gst_value_list_get_size (src1);
1786   if (size != gst_value_list_get_size (src2))
1787     return FALSE;
1788   g_value_init (dest, GST_TYPE_ARRAY);
1789
1790   for (n = 0; n < size; n++) {
1791     if (!gst_value_intersect (&val, gst_value_list_get_value (src1, n),
1792             gst_value_list_get_value (src2, n))) {
1793       g_value_unset (dest);
1794       return FALSE;
1795     }
1796     gst_value_list_append_value (dest, &val);
1797     g_value_unset (&val);
1798   }
1799
1800   return TRUE;
1801 }
1802
1803 /***************
1804  * subtraction *
1805  ***************/
1806
1807 static gboolean
1808 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
1809     const GValue * subtrahend)
1810 {
1811   int min = gst_value_get_int_range_min (subtrahend);
1812   int max = gst_value_get_int_range_max (subtrahend);
1813   int val = g_value_get_int (minuend);
1814
1815   /* subtracting a range from an int only works if the int is not in the
1816    * range */
1817   if (val < min || val > max) {
1818     /* and the result is the int */
1819     gst_value_init_and_copy (dest, minuend);
1820     return TRUE;
1821   }
1822   return FALSE;
1823 }
1824
1825 /* creates a new int range based on input values. 
1826  */
1827 static gboolean
1828 gst_value_create_new_range (GValue * dest, int min1, int max1, int min2,
1829     int max2)
1830 {
1831   GValue v1 = { 0, };
1832   GValue v2 = { 0, };
1833   GValue *pv1, *pv2;            /* yeah, hungarian! */
1834
1835   if (min1 <= max1 && min2 <= max2) {
1836     pv1 = &v1;
1837     pv2 = &v2;
1838   } else if (min1 <= max1) {
1839     pv1 = dest;
1840     pv2 = NULL;
1841   } else if (min2 <= max2) {
1842     pv1 = NULL;
1843     pv2 = dest;
1844   } else {
1845     return FALSE;
1846   }
1847
1848   if (min1 < max1) {
1849     g_value_init (pv1, GST_TYPE_INT_RANGE);
1850     gst_value_set_int_range (pv1, min1, max1);
1851   } else if (min1 == max1) {
1852     g_value_init (pv1, G_TYPE_INT);
1853     g_value_set_int (pv1, min1);
1854   }
1855   if (min2 < max2) {
1856     g_value_init (pv2, GST_TYPE_INT_RANGE);
1857     gst_value_set_int_range (pv2, min2, max2);
1858   } else if (min2 == max2) {
1859     g_value_init (pv2, G_TYPE_INT);
1860     g_value_set_int (pv2, min2);
1861   }
1862
1863   if (min1 <= max1 && min2 <= max2) {
1864     gst_value_list_concat (dest, pv1, pv2);
1865     g_value_unset (pv1);
1866     g_value_unset (pv2);
1867   }
1868   return TRUE;
1869 }
1870
1871 static gboolean
1872 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
1873     const GValue * subtrahend)
1874 {
1875   int min = gst_value_get_int_range_min (minuend);
1876   int max = gst_value_get_int_range_max (minuend);
1877   int val = g_value_get_int (subtrahend);
1878
1879   g_return_val_if_fail (min < max, FALSE);
1880
1881   /* value is outside of the range, return range unchanged */
1882   if (val < min || val > max) {
1883     gst_value_init_and_copy (dest, minuend);
1884     return TRUE;
1885   } else {
1886     /* max must be MAXINT too as val <= max */
1887     if (val == G_MAXINT) {
1888       max--;
1889       val--;
1890     }
1891     /* min must be MININT too as val >= max */
1892     if (val == G_MININT) {
1893       min++;
1894       val++;
1895     }
1896     gst_value_create_new_range (dest, min, val - 1, val + 1, max);
1897   }
1898   return TRUE;
1899 }
1900
1901 static gboolean
1902 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
1903     const GValue * subtrahend)
1904 {
1905   int min1 = gst_value_get_int_range_min (minuend);
1906   int max1 = gst_value_get_int_range_max (minuend);
1907   int min2 = gst_value_get_int_range_min (subtrahend);
1908   int max2 = gst_value_get_int_range_max (subtrahend);
1909
1910   if (max2 == G_MAXINT && min2 == G_MININT) {
1911     return FALSE;
1912   } else if (max2 == G_MAXINT) {
1913     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
1914   } else if (min2 == G_MININT) {
1915     return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
1916   } else {
1917     return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
1918         MAX (max2 + 1, min1), max1);
1919   }
1920 }
1921
1922 static gboolean
1923 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
1924     const GValue * subtrahend)
1925 {
1926   double min = gst_value_get_double_range_min (subtrahend);
1927   double max = gst_value_get_double_range_max (subtrahend);
1928   double val = g_value_get_double (minuend);
1929
1930   if (val < min || val > max) {
1931     gst_value_init_and_copy (dest, minuend);
1932     return TRUE;
1933   }
1934   return FALSE;
1935 }
1936
1937 static gboolean
1938 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
1939     const GValue * subtrahend)
1940 {
1941   /* since we don't have open ranges, we cannot create a hole in
1942    * a double range. We return the original range */
1943   gst_value_init_and_copy (dest, minuend);
1944   return TRUE;
1945 }
1946
1947 static gboolean
1948 gst_value_subtract_double_range_double_range (GValue * dest,
1949     const GValue * minuend, const GValue * subtrahend)
1950 {
1951   /* since we don't have open ranges, we have to approximate */
1952   /* done like with ints */
1953   double min1 = gst_value_get_double_range_min (minuend);
1954   double max2 = gst_value_get_double_range_max (minuend);
1955   double max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
1956   double min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
1957   GValue v1 = { 0, };
1958   GValue v2 = { 0, };
1959   GValue *pv1, *pv2;            /* yeah, hungarian! */
1960
1961   if (min1 < max1 && min2 < max2) {
1962     pv1 = &v1;
1963     pv2 = &v2;
1964   } else if (min1 < max1) {
1965     pv1 = dest;
1966     pv2 = NULL;
1967   } else if (min2 < max2) {
1968     pv1 = NULL;
1969     pv2 = dest;
1970   } else {
1971     return FALSE;
1972   }
1973
1974   if (min1 < max1) {
1975     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
1976     gst_value_set_double_range (pv1, min1, max1);
1977   }
1978   if (min2 < max2) {
1979     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
1980     gst_value_set_double_range (pv2, min2, max2);
1981   }
1982
1983   if (min1 < max1 && min2 < max2) {
1984     gst_value_list_concat (dest, pv1, pv2);
1985     g_value_unset (pv1);
1986     g_value_unset (pv2);
1987   }
1988   return TRUE;
1989 }
1990
1991 static gboolean
1992 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
1993     const GValue * subtrahend)
1994 {
1995   guint i, size;
1996   GValue subtraction = { 0, };
1997   gboolean ret = FALSE;
1998
1999   size = gst_value_list_get_size (minuend);
2000   for (i = 0; i < size; i++) {
2001     const GValue *cur = gst_value_list_get_value (minuend, i);
2002
2003     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2004       if (!ret) {
2005         gst_value_init_and_copy (dest, &subtraction);
2006         ret = TRUE;
2007       } else if (GST_VALUE_HOLDS_LIST (dest)
2008           && GST_VALUE_HOLDS_LIST (&subtraction)) {
2009         /* unroll */
2010         GValue unroll = { 0, };
2011
2012         gst_value_init_and_copy (&unroll, dest);
2013         g_value_unset (dest);
2014         gst_value_list_concat (dest, &unroll, &subtraction);
2015       } else if (GST_VALUE_HOLDS_LIST (dest)) {
2016         gst_value_list_append_value (dest, &subtraction);
2017       } else {
2018         GValue temp = { 0, };
2019
2020         gst_value_init_and_copy (&temp, dest);
2021         g_value_unset (dest);
2022         gst_value_list_concat (dest, &temp, &subtraction);
2023         g_value_unset (&temp);
2024       }
2025       g_value_unset (&subtraction);
2026     }
2027   }
2028   return ret;
2029 }
2030
2031 static gboolean
2032 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2033     const GValue * subtrahend)
2034 {
2035   guint i, size;
2036   GValue data[2] = { {0,}, {0,} };
2037   GValue *subtraction = &data[0], *result = &data[1];
2038
2039   gst_value_init_and_copy (result, minuend);
2040   size = gst_value_list_get_size (subtrahend);
2041   for (i = 0; i < size; i++) {
2042     const GValue *cur = gst_value_list_get_value (subtrahend, i);
2043
2044     if (gst_value_subtract (subtraction, result, cur)) {
2045       GValue *temp = result;
2046
2047       result = subtraction;
2048       subtraction = temp;
2049       g_value_unset (subtraction);
2050     } else {
2051       g_value_unset (result);
2052       return FALSE;
2053     }
2054   }
2055   gst_value_init_and_copy (dest, result);
2056   g_value_unset (result);
2057   return TRUE;
2058 }
2059
2060
2061 /**************
2062  * comparison *
2063  **************/
2064
2065 /**
2066  * gst_value_can_compare:
2067  * @value1: a value to compare
2068  * @value2: another value to compare
2069  *
2070  * Determines if @value1 and @value2 can be compared.
2071  *
2072  * Returns: TRUE if the values can be compared
2073  */
2074 gboolean
2075 gst_value_can_compare (const GValue * value1, const GValue * value2)
2076 {
2077   GstValueTable *table;
2078   int i;
2079
2080   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2081     return FALSE;
2082
2083   for (i = 0; i < gst_value_table->len; i++) {
2084     table = &g_array_index (gst_value_table, GstValueTable, i);
2085     if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2086       return TRUE;
2087   }
2088
2089   return FALSE;
2090 }
2091
2092 /**
2093  * gst_value_compare:
2094  * @value1: a value to compare
2095  * @value2: another value to compare
2096  *
2097  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
2098  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
2099  * if @value1 is greater than @value2, GST_VALUE_GREATER is returned.
2100  * If @value1 is less than @value2, GST_VALUE_LESSER is returned.
2101  * If the values are equal, GST_VALUE_EQUAL is returned.
2102  *
2103  * Returns: A GstValueCompareType value
2104  */
2105 int
2106 gst_value_compare (const GValue * value1, const GValue * value2)
2107 {
2108   GstValueTable *table, *best = NULL;
2109   int i;
2110
2111   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2112     return GST_VALUE_UNORDERED;
2113
2114   for (i = 0; i < gst_value_table->len; i++) {
2115     table = &g_array_index (gst_value_table, GstValueTable, i);
2116     if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2117       best = table;
2118       break;
2119     }
2120     if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2121       if (!best || g_type_is_a (table->type, best->type))
2122         best = table;
2123     }
2124   }
2125   if (best) {
2126     return best->compare (value1, value2);
2127   }
2128
2129   g_critical ("unable to compare values of type %s\n",
2130       g_type_name (G_VALUE_TYPE (value1)));
2131   return GST_VALUE_UNORDERED;
2132 }
2133
2134 /* union */
2135
2136 /**
2137  * gst_value_can_union:
2138  * @value1: a value to union
2139  * @value2: another value to union
2140  *
2141  * Determines if @value1 and @value2 can be non-trivially unioned.
2142  * Any two values can be trivially unioned by adding both of them
2143  * to a GstValueList.  However, certain types have the possibility
2144  * to be unioned in a simpler way.  For example, an integer range
2145  * and an integer can be unioned if the integer is a subset of the
2146  * integer range.  If there is the possibility that two values can
2147  * be unioned, this function returns TRUE.
2148  *
2149  * Returns: TRUE if there is a function allowing the two values to
2150  * be unioned.
2151  */
2152 gboolean
2153 gst_value_can_union (const GValue * value1, const GValue * value2)
2154 {
2155   GstValueUnionInfo *union_info;
2156   int i;
2157
2158   for (i = 0; i < gst_value_union_funcs->len; i++) {
2159     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2160     if (union_info->type1 == G_VALUE_TYPE (value1) &&
2161         union_info->type2 == G_VALUE_TYPE (value2))
2162       return TRUE;
2163     if (union_info->type1 == G_VALUE_TYPE (value2) &&
2164         union_info->type2 == G_VALUE_TYPE (value1))
2165       return TRUE;
2166   }
2167
2168   return FALSE;
2169 }
2170
2171 /**
2172  * gst_value_union:
2173  * @dest: the destination value
2174  * @value1: a value to union
2175  * @value2: another value to union
2176  *
2177  * Creates a GValue cooresponding to the union of @value1 and @value2.
2178  *
2179  * Returns: TRUE if the values could be unioned
2180  */
2181 gboolean
2182 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2183 {
2184   GstValueUnionInfo *union_info;
2185   int i;
2186
2187   for (i = 0; i < gst_value_union_funcs->len; i++) {
2188     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2189     if (union_info->type1 == G_VALUE_TYPE (value1) &&
2190         union_info->type2 == G_VALUE_TYPE (value2)) {
2191       if (union_info->func (dest, value1, value2)) {
2192         return TRUE;
2193       }
2194     }
2195     if (union_info->type1 == G_VALUE_TYPE (value2) &&
2196         union_info->type2 == G_VALUE_TYPE (value1)) {
2197       if (union_info->func (dest, value2, value1)) {
2198         return TRUE;
2199       }
2200     }
2201   }
2202
2203   gst_value_list_concat (dest, value1, value2);
2204   return TRUE;
2205 }
2206
2207 /**
2208  * gst_value_register_union_func:
2209  * @type1: a type to union
2210  * @type2: another type to union
2211  * @func: a function that implments creating a union between the two types
2212  *
2213  * Registers a union function that can create a union between GValues
2214  * of the type @type1 and @type2.
2215  *
2216  */
2217 void
2218 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
2219 {
2220   GstValueUnionInfo union_info;
2221
2222   union_info.type1 = type1;
2223   union_info.type2 = type2;
2224   union_info.func = func;
2225
2226   g_array_append_val (gst_value_union_funcs, union_info);
2227 }
2228
2229 /* intersection */
2230
2231 /**
2232  * gst_value_can_intersect:
2233  * @value1: a value to intersect
2234  * @value2: another value to intersect
2235  *
2236  * Determines if intersecting two values will produce a valid result.
2237  * Two values will produce a valid intersection if they have the same
2238  * type, or if there is a method (registered by
2239  * #gst_value_register_intersection_func) to calculate the intersection.
2240  *
2241  * Returns: TRUE if the values can intersect
2242  */
2243 gboolean
2244 gst_value_can_intersect (const GValue * value1, const GValue * value2)
2245 {
2246   GstValueIntersectInfo *intersect_info;
2247   int i;
2248
2249   /* special cases */
2250   if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
2251     return TRUE;
2252
2253   for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2254     intersect_info = &g_array_index (gst_value_intersect_funcs,
2255         GstValueIntersectInfo, i);
2256     if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2257         intersect_info->type2 == G_VALUE_TYPE (value2))
2258       if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
2259           intersect_info->type1 == G_VALUE_TYPE (value2))
2260         return TRUE;
2261   }
2262
2263   return gst_value_can_compare (value1, value2);
2264 }
2265
2266 /**
2267  * gst_value_intersect:
2268  * @dest: a uninitialized #GValue that will hold the calculated
2269  * intersection value
2270  * @value1: a value to intersect
2271  * @value2: another value to intersect
2272  *
2273  * Calculates the intersection of two values.  If the values have
2274  * a non-empty intersection, the value representing the intersection
2275  * is placed in @dest.  If the intersection is non-empty, @dest is
2276  * not modified.
2277  *
2278  * Returns: TRUE if the intersection is non-empty
2279  */
2280 gboolean
2281 gst_value_intersect (GValue * dest, const GValue * value1,
2282     const GValue * value2)
2283 {
2284   GstValueIntersectInfo *intersect_info;
2285   int i;
2286   int ret = FALSE;
2287
2288   /* special cases first */
2289   if (GST_VALUE_HOLDS_LIST (value1))
2290     return gst_value_intersect_list (dest, value1, value2);
2291   if (GST_VALUE_HOLDS_LIST (value2))
2292     return gst_value_intersect_list (dest, value2, value1);
2293
2294   for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2295     intersect_info = &g_array_index (gst_value_intersect_funcs,
2296         GstValueIntersectInfo, i);
2297     if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2298         intersect_info->type2 == G_VALUE_TYPE (value2)) {
2299       ret = intersect_info->func (dest, value1, value2);
2300       return ret;
2301     }
2302     if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
2303         intersect_info->type2 == G_VALUE_TYPE (value1)) {
2304       ret = intersect_info->func (dest, value2, value1);
2305       return ret;
2306     }
2307   }
2308
2309   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
2310     gst_value_init_and_copy (dest, value1);
2311     ret = TRUE;
2312   }
2313
2314   return ret;
2315 }
2316
2317 /**
2318  * gst_value_register_intersection_func:
2319  * @type1: the first type to intersect
2320  * @type2: the second type to intersect
2321  * @func: the intersection function
2322  *
2323  * Registers a function that is called to calculate the intersection
2324  * of the values having the types @type1 and @type2.
2325  */
2326 /**
2327  * GstValueIntersectFunc:
2328  * @dest: a uninitialized #GValue that will hold the calculated
2329  * intersection value
2330  * @value1: a value to intersect
2331  * @value2: another value to intersect
2332  *
2333  * Functions having this type calculate the intersection of @value1
2334  * and @value2.  If the intersection is non-empty, the result is
2335  * placed in @dest and TRUE is returned.  If the intersection is
2336  * empty, @dest is unmodified and FALSE is returned.
2337  *
2338  * Returns: TRUE if the intersection is non-empty, FALSE otherwise
2339  */
2340 void
2341 gst_value_register_intersect_func (GType type1, GType type2,
2342     GstValueIntersectFunc func)
2343 {
2344   GstValueIntersectInfo intersect_info;
2345
2346   intersect_info.type1 = type1;
2347   intersect_info.type2 = type2;
2348   intersect_info.func = func;
2349
2350   g_array_append_val (gst_value_intersect_funcs, intersect_info);
2351 }
2352
2353
2354 /* subtraction */
2355
2356 /**
2357  * gst_value_subtract:
2358  * @dest: the destination value for the result if the subtraction is not empty
2359  * @minuend: the value to subtract from
2360  * @subtrahend: the value to subtract
2361  *
2362  * Subtracts @subtrahend from @minuend and stores the result in @dest.
2363  * Note that this means subtraction as in sets, not as in mathematics.
2364  *
2365  * Returns: TRUE if the subtraction is not empty
2366  */
2367 gboolean
2368 gst_value_subtract (GValue * dest, const GValue * minuend,
2369     const GValue * subtrahend)
2370 {
2371   GstValueSubtractInfo *info;
2372   int i;
2373
2374   /* special cases first */
2375   if (GST_VALUE_HOLDS_LIST (minuend))
2376     return gst_value_subtract_from_list (dest, minuend, subtrahend);
2377   if (GST_VALUE_HOLDS_LIST (subtrahend))
2378     return gst_value_subtract_list (dest, minuend, subtrahend);
2379
2380   for (i = 0; i < gst_value_subtract_funcs->len; i++) {
2381     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
2382     if (info->minuend == G_VALUE_TYPE (minuend) &&
2383         info->subtrahend == G_VALUE_TYPE (subtrahend)) {
2384       return info->func (dest, minuend, subtrahend);
2385     }
2386   }
2387
2388   if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
2389     gst_value_init_and_copy (dest, minuend);
2390     return TRUE;
2391   }
2392
2393   return FALSE;
2394 }
2395
2396 #if 0
2397 gboolean
2398 gst_value_subtract (GValue * dest, const GValue * minuend,
2399     const GValue * subtrahend)
2400 {
2401   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
2402
2403   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
2404       gst_value_serialize (subtrahend),
2405       ret ? gst_value_serialize (dest) : "---");
2406   return ret;
2407 }
2408 #endif
2409
2410 /**
2411  * gst_value_can_subtract:
2412  * @minuend: the value to subtract from
2413  * @subtrahend: the value to subtract
2414  *
2415  * Checks if it's possible to subtract @subtrahend from @minuend.
2416  *
2417  * Returns: TRUE if a subtraction is possible
2418  */
2419 gboolean
2420 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
2421 {
2422   GstValueSubtractInfo *info;
2423   int i;
2424
2425   /* special cases */
2426   if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
2427     return TRUE;
2428
2429   for (i = 0; i < gst_value_subtract_funcs->len; i++) {
2430     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
2431     if (info->minuend == G_VALUE_TYPE (minuend) &&
2432         info->subtrahend == G_VALUE_TYPE (subtrahend))
2433       return TRUE;
2434   }
2435
2436   return gst_value_can_compare (minuend, subtrahend);
2437 }
2438
2439 /**
2440  * gst_value_register_subtract_func:
2441  * @minuend_type: type of the minuend
2442  * @subtrahend_type: type of the subtrahend
2443  * @func: function to use
2444  *
2445  * Registers @func as a function capable of subtracting the values of 
2446  * @subtrahend_type from values of @minuend_type.
2447  */
2448 void
2449 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
2450     GstValueSubtractFunc func)
2451 {
2452   GstValueSubtractInfo info;
2453
2454   /* one type must be unfixed, other subtractions can be done as comparisons */
2455   g_return_if_fail (!gst_type_is_fixed (minuend_type)
2456       || !gst_type_is_fixed (subtrahend_type));
2457
2458   info.minuend = minuend_type;
2459   info.subtrahend = subtrahend_type;
2460   info.func = func;
2461
2462   g_array_append_val (gst_value_subtract_funcs, info);
2463 }
2464
2465 /**
2466  * gst_value_register:
2467  * @table: structure containing functions to register
2468  *
2469  * Registers functions to perform calculations on #GValues of a given
2470  * type.
2471  */
2472 /**
2473  * GstValueTable:
2474  * @type: GType that the functions operate on.
2475  * @compare: A function that compares two values of this type.
2476  * @serialize: A function that transforms a value of this type to a
2477  * string.  Strings created by this function must be unique and should
2478  * be human readable.
2479  * @deserialize: A function that transforms a string to a value of
2480  * this type.  This function must transform strings created by the
2481  * serialize function back to the original value.  This function may
2482  * optionally transform other strings into values.
2483  */
2484 void
2485 gst_value_register (const GstValueTable * table)
2486 {
2487   g_array_append_val (gst_value_table, *table);
2488 }
2489
2490 /**
2491  * gst_value_init_and_copy:
2492  * @dest: the target value
2493  * @src: the source value
2494  *
2495  * Initialises the target value to be of the same type as source and then copies
2496  * the contents from source to target.
2497  */
2498 void
2499 gst_value_init_and_copy (GValue * dest, const GValue * src)
2500 {
2501   g_value_init (dest, G_VALUE_TYPE (src));
2502   g_value_copy (src, dest);
2503 }
2504
2505 /**
2506  * gst_value_serialize:
2507  * @value: a #GValue to serialize
2508  *
2509  * tries to transform the given @value into a string representation that allows
2510  * getting back this string later on using gst_value_deserialize().
2511  *
2512  * Returns: the serialization for @value or NULL if none exists
2513  */
2514 gchar *
2515 gst_value_serialize (const GValue * value)
2516 {
2517   int i;
2518   GValue s_val = { 0 };
2519   GstValueTable *table, *best = NULL;
2520   char *s;
2521
2522   g_return_val_if_fail (G_IS_VALUE (value), NULL);
2523
2524   for (i = 0; i < gst_value_table->len; i++) {
2525     table = &g_array_index (gst_value_table, GstValueTable, i);
2526     if (table->serialize == NULL)
2527       continue;
2528     if (table->type == G_VALUE_TYPE (value)) {
2529       best = table;
2530       break;
2531     }
2532     if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
2533       if (!best || g_type_is_a (table->type, best->type))
2534         best = table;
2535     }
2536   }
2537   if (best)
2538     return best->serialize (value);
2539
2540   g_value_init (&s_val, G_TYPE_STRING);
2541   if (g_value_transform (value, &s_val)) {
2542     s = gst_string_wrap (g_value_get_string (&s_val));
2543   } else {
2544     s = NULL;
2545   }
2546   g_value_unset (&s_val);
2547
2548   return s;
2549 }
2550
2551 /**
2552  * gst_value_deserialize:
2553  * @dest: #GValue to fill with contents of deserialization
2554  * @src: string to deserialize
2555  *
2556  * Tries to deserialize a string into the type specified by the given GValue.
2557  * If the operation succeeds, TRUE is returned, FALSE otherwise.
2558  *
2559  * Returns: TRUE on success
2560  */
2561 gboolean
2562 gst_value_deserialize (GValue * dest, const gchar * src)
2563 {
2564   GstValueTable *table, *best = NULL;
2565   int i;
2566
2567   g_return_val_if_fail (src != NULL, FALSE);
2568   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
2569
2570   for (i = 0; i < gst_value_table->len; i++) {
2571     table = &g_array_index (gst_value_table, GstValueTable, i);
2572     if (table->serialize == NULL)
2573       continue;
2574
2575     if (table->type == G_VALUE_TYPE (dest)) {
2576       best = table;
2577       break;
2578     }
2579
2580     if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
2581       if (!best || g_type_is_a (table->type, best->type))
2582         best = table;
2583     }
2584   }
2585   if (best) {
2586     return best->deserialize (dest, src);
2587   }
2588
2589   return FALSE;
2590 }
2591
2592 /**
2593  * gst_value_is_fixed:
2594  * @value: the #GValue to check
2595  *
2596  * Tests if the given GValue, if available in a GstStructure (or any other
2597  * container) contains a "fixed" (which means: one value) or an "unfixed"
2598  * (which means: multiple possible values, such as data lists or data
2599  * ranges) value.
2600  *
2601  * Returns: true if the value is "fixed".
2602  */
2603
2604 gboolean
2605 gst_value_is_fixed (const GValue * value)
2606 {
2607   GType type = G_VALUE_TYPE (value);
2608
2609   if (type == GST_TYPE_ARRAY) {
2610     gboolean fixed = TRUE;
2611     gint size, n;
2612     const GValue *kid;
2613
2614     /* check recursively */
2615     size = gst_value_list_get_size (value);
2616     for (n = 0; n < size; n++) {
2617       kid = gst_value_list_get_value (value, n);
2618       fixed &= gst_value_is_fixed (kid);
2619     }
2620
2621     return fixed;
2622   }
2623
2624   return gst_type_is_fixed (type);
2625 }
2626
2627 /************
2628  * fraction *
2629  ************/
2630
2631 /* helper functions */
2632
2633 /* Finds the greatest common divisor.
2634  * Returns 1 if none other found.
2635  * This is Euclid's algorithm. */
2636 static gint
2637 gst_greatest_common_divisor (gint a, gint b)
2638 {
2639   while (b != 0) {
2640     int temp = a;
2641
2642     a = b;
2643     b = temp % b;
2644   }
2645
2646   return ABS (a);
2647 }
2648
2649 static void
2650 gst_value_init_fraction (GValue * value)
2651 {
2652   value->data[0].v_int = 0;
2653   value->data[1].v_int = 1;
2654 }
2655
2656 static void
2657 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
2658 {
2659   dest_value->data[0].v_int = src_value->data[0].v_int;
2660   dest_value->data[1].v_int = src_value->data[1].v_int;
2661 }
2662
2663 static gchar *
2664 gst_value_collect_fraction (GValue * value, guint n_collect_values,
2665     GTypeCValue * collect_values, guint collect_flags)
2666 {
2667   value->data[0].v_int = collect_values[0].v_int;
2668   value->data[1].v_int = collect_values[1].v_int;
2669
2670   return NULL;
2671 }
2672
2673 static gchar *
2674 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
2675     GTypeCValue * collect_values, guint collect_flags)
2676 {
2677   gint *numerator = collect_values[0].v_pointer;
2678   gint *denominator = collect_values[1].v_pointer;
2679
2680   if (!numerator)
2681     return g_strdup_printf ("numerator for `%s' passed as NULL",
2682         G_VALUE_TYPE_NAME (value));
2683   if (!denominator)
2684     return g_strdup_printf ("denominator for `%s' passed as NULL",
2685         G_VALUE_TYPE_NAME (value));
2686
2687   *numerator = value->data[0].v_int;
2688   *denominator = value->data[1].v_int;
2689
2690   return NULL;
2691 }
2692
2693 /**
2694  * gst_value_set_fraction:
2695  * @value: a GValue initialized to #GST_TYPE_FRACTION
2696  * @numerator: the numerator of the fraction
2697  * @denominator: the denominator of the fraction
2698  *
2699  * Sets @value to the fraction specified by @numerator over @denominator.
2700  * The fraction gets reduced to the smallest numerator and denominator,
2701  * and if necessary the sign is moved to the numerator.
2702  */
2703 void
2704 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
2705 {
2706   gint gcd = 0;
2707
2708   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
2709   g_return_if_fail (denominator != 0);
2710   g_return_if_fail (denominator >= -G_MAXINT);
2711   g_return_if_fail (numerator >= -G_MAXINT);
2712
2713   /* normalize sign */
2714   if (denominator < 0) {
2715     numerator = -numerator;
2716     denominator = -denominator;
2717   }
2718
2719   /* check for reduction */
2720   gcd = gst_greatest_common_divisor (numerator, denominator);
2721   if (gcd) {
2722     numerator /= gcd;
2723     denominator /= gcd;
2724   }
2725   value->data[0].v_int = numerator;
2726   value->data[1].v_int = denominator;
2727 }
2728
2729 /**
2730  * gst_value_get_fraction_numerator:
2731  * @value: a GValue initialized to #GST_TYPE_FRACTION
2732  *
2733  * Gets the numerator of the fraction specified by @value.
2734  *
2735  * Returns: the numerator of the fraction.
2736  */
2737 int
2738 gst_value_get_fraction_numerator (const GValue * value)
2739 {
2740   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
2741
2742   return value->data[0].v_int;
2743 }
2744
2745 /**
2746  * gst_value_get_fraction_denominator:
2747  * @value: a GValue initialized to #GST_TYPE_FRACTION
2748  *
2749  * Gets the denominator of the fraction specified by @value.
2750  *
2751  * Returns: the denominator of the fraction.
2752  */
2753 int
2754 gst_value_get_fraction_denominator (const GValue * value)
2755 {
2756   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
2757
2758   return value->data[1].v_int;
2759 }
2760
2761 /**
2762  * gst_value_fraction_multiply:
2763  * @product: a GValue initialized to #GST_TYPE_FRACTION
2764  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
2765  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
2766  *
2767  * Multiplies the two GValues containing a GstFraction and sets @product
2768  * to the product of the two fractions.
2769  *
2770  * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
2771  */
2772 gboolean
2773 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
2774     const GValue * factor2)
2775 {
2776   gint gcd, n1, n2, d1, d2;
2777
2778   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
2779   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
2780
2781   n1 = factor1->data[0].v_int;
2782   n2 = factor2->data[0].v_int;
2783   d1 = factor1->data[1].v_int;
2784   d2 = factor2->data[1].v_int;
2785
2786   gcd = gst_greatest_common_divisor (n1, d2);
2787   n1 /= gcd;
2788   d2 /= gcd;
2789   gcd = gst_greatest_common_divisor (n2, d1);
2790   n2 /= gcd;
2791   d1 /= gcd;
2792
2793   g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
2794   g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
2795
2796   gst_value_set_fraction (product, n1 * n2, d1 * d2);
2797
2798   return TRUE;
2799 }
2800
2801 static char *
2802 gst_value_serialize_fraction (const GValue * value)
2803 {
2804   gint32 numerator = value->data[0].v_int;
2805   gint32 denominator = value->data[1].v_int;
2806   gboolean positive = TRUE;
2807
2808   /* get the sign and make components absolute */
2809   if (numerator < 0) {
2810     numerator = -numerator;
2811     positive = !positive;
2812   }
2813   if (denominator < 0) {
2814     denominator = -denominator;
2815     positive = !positive;
2816   }
2817
2818   return g_strdup_printf ("%s%d/%d",
2819       positive ? "" : "-", numerator, denominator);
2820 }
2821
2822 static gboolean
2823 gst_value_deserialize_fraction (GValue * dest, const char *s)
2824 {
2825   gint num, den;
2826
2827   if (s && sscanf (s, "%d/%d", &num, &den) == 2) {
2828     gst_value_set_fraction (dest, num, den);
2829     return TRUE;
2830   }
2831
2832   return FALSE;
2833 }
2834
2835 static void
2836 gst_value_transform_fraction_string (const GValue * src_value,
2837     GValue * dest_value)
2838 {
2839   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
2840 }
2841
2842 static void
2843 gst_value_transform_string_fraction (const GValue * src_value,
2844     GValue * dest_value)
2845 {
2846   gst_value_deserialize_fraction (dest_value, src_value->data[0].v_pointer);
2847 }
2848
2849 #define MAX_TERMS       30
2850 #define MIN_DIVISOR     1.0e-10
2851 #define MAX_ERROR       1.0e-20
2852
2853 /* use continued fractions to transform a double into a fraction,
2854  * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
2855  * This algorithm takes care of overflows.
2856  */
2857 static void
2858 gst_value_transform_double_fraction (const GValue * src_value,
2859     GValue * dest_value)
2860 {
2861   gdouble V, F;                 /* double being converted */
2862   gint N, D;                    /* will contain the result */
2863   gint A;                       /* current term in continued fraction */
2864   gint64 N1, D1;                /* numerator, denominator of last approx */
2865   gint64 N2, D2;                /* numerator, denominator of previous approx */
2866   gint i;
2867   gboolean negative = FALSE;
2868
2869   /* initialize fraction being converted */
2870   F = src_value->data[0].v_double;
2871   if (F < 0.0) {
2872     F = -F;
2873     negative = TRUE;
2874   }
2875
2876   V = F;
2877   /* initialize fractions with 1/0, 0/1 */
2878   N1 = 1;
2879   D1 = 0;
2880   N2 = 0;
2881   D2 = 1;
2882   N = 1;
2883   D = 1;
2884
2885   for (i = 0; i < MAX_TERMS; i++) {
2886     /* get next term */
2887     A = floor (F);
2888     /* get new divisor */
2889     F = F - A;
2890
2891     /* calculate new fraction in temp */
2892     N2 = N1 * A + N2;
2893     D2 = D1 * A + D2;
2894
2895     /* guard against overflow */
2896     if (N2 > G_MAXINT || D2 > G_MAXINT) {
2897       break;
2898     }
2899
2900     N = N2;
2901     D = D2;
2902
2903     /* save last two fractions */
2904     N2 = N1;
2905     D2 = D1;
2906     N1 = N;
2907     D1 = D;
2908
2909     /* quit if dividing by zero or close enough to target */
2910     if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
2911       break;
2912     }
2913
2914     /* Take reciprocal */
2915     F = 1 / F;
2916   }
2917   /* fix for overflow */
2918   if (D == 0) {
2919     N = G_MAXINT;
2920     D = 1;
2921   }
2922   /* fix for negative */
2923   if (negative)
2924     N = -N;
2925
2926   /* will also simplify */
2927   gst_value_set_fraction (dest_value, N, D);
2928 }
2929
2930 static void
2931 gst_value_transform_fraction_double (const GValue * src_value,
2932     GValue * dest_value)
2933 {
2934   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
2935       ((double) src_value->data[1].v_int);
2936 }
2937
2938 static int
2939 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
2940 {
2941   gint n1, n2;
2942   gint d1, d2;
2943
2944   gint64 new_num_1;
2945   gint64 new_num_2;
2946
2947   n1 = value1->data[0].v_int;
2948   n2 = value2->data[0].v_int;
2949   d1 = value1->data[1].v_int;
2950   d2 = value2->data[1].v_int;
2951
2952   /* fractions are reduced when set, so we can quickly see if they're equal */
2953   if (n1 == n2 && d1 == d2)
2954     return GST_VALUE_EQUAL;
2955
2956   /* extend to 64 bits */
2957   new_num_1 = ((gint64) n1) * d2;
2958   new_num_2 = ((gint64) n2) * d1;
2959   if (new_num_1 < new_num_2)
2960     return GST_VALUE_LESS_THAN;
2961   if (new_num_1 > new_num_2)
2962     return GST_VALUE_GREATER_THAN;
2963
2964   g_assert_not_reached ();
2965   return GST_VALUE_UNORDERED;
2966 }
2967
2968 /*********
2969  * GDate *
2970  *********/
2971
2972 /**
2973  * gst_value_set_date:
2974  * @value: a GValue initialized to GST_TYPE_DATE
2975  * @date: the date to set the value to
2976  *
2977  * Sets the contents of @value to coorespond to @date.  The actual
2978  * #GDate structure is copied before it is used.
2979  */
2980 void
2981 gst_value_set_date (GValue * value, const GDate * date)
2982 {
2983   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
2984
2985   g_value_set_boxed (value, date);
2986 }
2987
2988 /**
2989  * gst_value_get_date:
2990  * @value: a GValue initialized to GST_TYPE_DATE
2991  *
2992  * Gets the contents of @value.
2993  *
2994  * Returns: the contents of @value
2995  */
2996 const GDate *
2997 gst_value_get_date (const GValue * value)
2998 {
2999   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3000
3001   return (const GDate *) g_value_get_boxed (value);
3002 }
3003
3004 static gpointer
3005 gst_date_copy (gpointer boxed)
3006 {
3007   const GDate *date = (const GDate *) boxed;
3008
3009   return g_date_new_julian (g_date_get_julian (date));
3010 }
3011
3012 static int
3013 gst_value_compare_date (const GValue * value1, const GValue * value2)
3014 {
3015   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3016   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3017   guint32 j1, j2;
3018
3019   if (date1 == date2)
3020     return GST_VALUE_EQUAL;
3021
3022   if ((date1 == NULL || !g_date_valid (date1))
3023       && (date2 != NULL && g_date_valid (date2))) {
3024     return GST_VALUE_LESS_THAN;
3025   }
3026
3027   if ((date2 == NULL || !g_date_valid (date2))
3028       && (date1 != NULL && g_date_valid (date1))) {
3029     return GST_VALUE_GREATER_THAN;
3030   }
3031
3032   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3033       || !g_date_valid (date2)) {
3034     return GST_VALUE_UNORDERED;
3035   }
3036
3037   j1 = g_date_get_julian (date1);
3038   j2 = g_date_get_julian (date2);
3039
3040   if (j1 == j2)
3041     return GST_VALUE_EQUAL;
3042   else if (j1 < j2)
3043     return GST_VALUE_LESS_THAN;
3044   else
3045     return GST_VALUE_GREATER_THAN;
3046 }
3047
3048 static char *
3049 gst_value_serialize_date (const GValue * val)
3050 {
3051   const GDate *date = (const GDate *) g_value_get_boxed (val);
3052
3053   if (date == NULL || !g_date_valid (date))
3054     return g_strdup ("9999-99-99");
3055
3056   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3057       g_date_get_month (date), g_date_get_day (date));
3058 }
3059
3060 static gboolean
3061 gst_value_deserialize_date (GValue * dest, const char *s)
3062 {
3063   guint year, month, day;
3064
3065   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3066     return FALSE;
3067
3068   if (!g_date_valid_dmy (day, month, year))
3069     return FALSE;
3070
3071   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3072   return TRUE;
3073 }
3074
3075 static void
3076 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3077 {
3078   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3079 }
3080
3081 static void
3082 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3083 {
3084   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3085 }
3086
3087 void
3088 _gst_value_initialize (void)
3089 {
3090   GTypeInfo info = {
3091     0,
3092     NULL,
3093     NULL,
3094     NULL,
3095     NULL,
3096     NULL,
3097     0,
3098     0,
3099     NULL,
3100     NULL,
3101   };
3102   GTypeFundamentalInfo finfo = {
3103     0
3104   };
3105
3106   //const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
3107
3108   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
3109   gst_value_union_funcs = g_array_new (FALSE, FALSE,
3110       sizeof (GstValueUnionInfo));
3111   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
3112       sizeof (GstValueIntersectInfo));
3113   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
3114       sizeof (GstValueSubtractInfo));
3115
3116   {
3117     static const GTypeValueTable value_table = {
3118       gst_value_init_fourcc,
3119       NULL,
3120       gst_value_copy_fourcc,
3121       NULL,
3122       "i",
3123       gst_value_collect_fourcc,
3124       "p",
3125       gst_value_lcopy_fourcc
3126     };
3127     static GstValueTable gst_value = {
3128       0,
3129       gst_value_compare_fourcc,
3130       gst_value_serialize_fourcc,
3131       gst_value_deserialize_fourcc,
3132     };
3133
3134     info.value_table = &value_table;
3135     gst_type_fourcc = g_type_register_fundamental (g_type_fundamental_next (),
3136         "GstFourcc", &info, &finfo, 0);
3137     gst_value.type = gst_type_fourcc;
3138     gst_value_register (&gst_value);
3139   }
3140
3141   {
3142     static const GTypeValueTable value_table = {
3143       gst_value_init_int_range,
3144       NULL,
3145       gst_value_copy_int_range,
3146       NULL,
3147       "ii",
3148       gst_value_collect_int_range,
3149       "pp",
3150       gst_value_lcopy_int_range
3151     };
3152     static GstValueTable gst_value = {
3153       0,
3154       gst_value_compare_int_range,
3155       gst_value_serialize_int_range,
3156       gst_value_deserialize_int_range,
3157     };
3158
3159     info.value_table = &value_table;
3160     gst_type_int_range =
3161         g_type_register_fundamental (g_type_fundamental_next (), "GstIntRange",
3162         &info, &finfo, 0);
3163     gst_value.type = gst_type_int_range;
3164     gst_value_register (&gst_value);
3165   }
3166
3167   {
3168     static const GTypeValueTable value_table = {
3169       gst_value_init_double_range,
3170       NULL,
3171       gst_value_copy_double_range,
3172       NULL,
3173       "dd",
3174       gst_value_collect_double_range,
3175       "pp",
3176       gst_value_lcopy_double_range
3177     };
3178     static GstValueTable gst_value = {
3179       0,
3180       gst_value_compare_double_range,
3181       gst_value_serialize_double_range,
3182       gst_value_deserialize_double_range,
3183     };
3184
3185     info.value_table = &value_table;
3186     gst_type_double_range =
3187         g_type_register_fundamental (g_type_fundamental_next (),
3188         "GstDoubleRange", &info, &finfo, 0);
3189     gst_value.type = gst_type_double_range;
3190     gst_value_register (&gst_value);
3191   }
3192
3193   {
3194     static const GTypeValueTable value_table = {
3195       gst_value_init_list,
3196       gst_value_free_list,
3197       gst_value_copy_list,
3198       gst_value_list_peek_pointer,
3199       "p",
3200       gst_value_collect_list,
3201       "p",
3202       gst_value_lcopy_list
3203     };
3204     static GstValueTable gst_value = {
3205       0,
3206       gst_value_compare_list,
3207       gst_value_serialize_list,
3208       gst_value_deserialize_list,
3209     };
3210
3211     info.value_table = &value_table;
3212     gst_type_list = g_type_register_fundamental (g_type_fundamental_next (),
3213         "GstValueList", &info, &finfo, 0);
3214     gst_value.type = gst_type_list;
3215     gst_value_register (&gst_value);
3216   }
3217
3218   {
3219     static const GTypeValueTable value_table = {
3220       gst_value_init_list,
3221       gst_value_free_list,
3222       gst_value_copy_list,
3223       gst_value_list_peek_pointer,
3224       "p",
3225       gst_value_collect_list,
3226       "p",
3227       gst_value_lcopy_list
3228     };
3229     static GstValueTable gst_value = {
3230       0,
3231       gst_value_compare_list,
3232       gst_value_serialize_array,
3233       gst_value_deserialize_array,
3234     };
3235
3236     info.value_table = &value_table;
3237     gst_type_array =
3238         g_type_register_fundamental (g_type_fundamental_next (),
3239         "GstValueArray", &info, &finfo, 0);
3240     gst_value.type = gst_type_array;
3241     gst_value_register (&gst_value);
3242   }
3243
3244   {
3245 #if 0
3246     static const GTypeValueTable value_table = {
3247       gst_value_init_buffer,
3248       NULL,
3249       gst_value_copy_buffer,
3250       NULL,
3251       "i",
3252       NULL,                     /*gst_value_collect_buffer, */
3253       "p",
3254       NULL                      /*gst_value_lcopy_buffer */
3255     };
3256 #endif
3257     static GstValueTable gst_value = {
3258       0,
3259       gst_value_compare_buffer,
3260       gst_value_serialize_buffer,
3261       gst_value_deserialize_buffer,
3262     };
3263
3264     gst_value.type = GST_TYPE_BUFFER;
3265     gst_value_register (&gst_value);
3266   }
3267   {
3268     static const GTypeValueTable value_table = {
3269       gst_value_init_fraction,
3270       NULL,
3271       gst_value_copy_fraction,
3272       NULL,
3273       "ii",
3274       gst_value_collect_fraction,
3275       "pp",
3276       gst_value_lcopy_fraction
3277     };
3278     static GstValueTable gst_value = {
3279       0,
3280       gst_value_compare_fraction,
3281       gst_value_serialize_fraction,
3282       gst_value_deserialize_fraction,
3283     };
3284
3285     info.value_table = &value_table;
3286     gst_type_fraction =
3287         g_type_register_fundamental (g_type_fundamental_next (), "GstFraction",
3288         &info, &finfo, 0);
3289     gst_value.type = gst_type_fraction;
3290     gst_value_register (&gst_value);
3291   }
3292   {
3293     static GstValueTable gst_value = {
3294       0,
3295       NULL,
3296       gst_value_serialize_caps,
3297       gst_value_deserialize_caps,
3298     };
3299
3300     gst_value.type = GST_TYPE_CAPS;
3301     gst_value_register (&gst_value);
3302   }
3303   {
3304     static GstValueTable gst_value = {
3305       0,
3306       gst_value_compare_date,
3307       gst_value_serialize_date,
3308       gst_value_deserialize_date,
3309     };
3310
3311     /* Not using G_TYPE_DATE here on purpose, even if we could
3312      * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
3313      * serialised strings to have different type strings depending
3314      * on what version is used, so FIXME in 0.11 when we 
3315      * require GLib-2.8 */
3316     gst_type_date = g_boxed_type_register_static ("GstDate",
3317         (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
3318
3319     gst_value.type = gst_type_date;
3320     gst_value_register (&gst_value);
3321   }
3322
3323   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
3324   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
3325
3326   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
3327   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
3328   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
3329
3330   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
3331
3332   REGISTER_SERIALIZATION (G_TYPE_INT, int);
3333
3334   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
3335   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
3336
3337   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
3338   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
3339   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
3340
3341   g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
3342       gst_value_transform_fourcc_string);
3343   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
3344       gst_value_transform_int_range_string);
3345   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
3346       gst_value_transform_double_range_string);
3347   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
3348       gst_value_transform_list_string);
3349   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
3350       gst_value_transform_array_string);
3351   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
3352       gst_value_transform_fraction_string);
3353   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
3354       gst_value_transform_string_fraction);
3355   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
3356       gst_value_transform_fraction_double);
3357   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
3358       gst_value_transform_double_fraction);
3359   g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
3360       gst_value_transform_date_string);
3361   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
3362       gst_value_transform_string_date);
3363
3364   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3365       gst_value_intersect_int_int_range);
3366   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3367       gst_value_intersect_int_range_int_range);
3368   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
3369       gst_value_intersect_double_double_range);
3370   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
3371       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
3372   gst_value_register_intersect_func (GST_TYPE_ARRAY,
3373       GST_TYPE_ARRAY, gst_value_intersect_array);
3374
3375   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3376       gst_value_subtract_int_int_range);
3377   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
3378       gst_value_subtract_int_range_int);
3379   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3380       gst_value_subtract_int_range_int_range);
3381   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
3382       gst_value_subtract_double_double_range);
3383   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
3384       gst_value_subtract_double_range_double);
3385   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
3386       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
3387
3388 #if GLIB_CHECK_VERSION(2,8,0)
3389   /* see bug #317246, #64994, #65041 */
3390   {
3391     volatile GType date_type = G_TYPE_DATE;
3392
3393     GST_LOG ("Faking out the compiler: %d", date_type);
3394   }
3395 #endif
3396
3397   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3398       gst_value_union_int_int_range);
3399   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3400       gst_value_union_int_range_int_range);
3401 }