value: Make sure to cast int range values to guints before storing them
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:gstvalue
22  * @short_description: GValue implementations specific
23  * to GStreamer
24  *
25  * GValue implementations specific to GStreamer.
26  *
27  * Note that operations on the same #GValue from multiple threads may lead to
28  * undefined behaviour.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 #include <math.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39
40 #include "gst_private.h"
41 #include "glib-compat-private.h"
42 #include <gst/gst.h>
43 #include <gobject/gvaluecollector.h>
44 #include "gstutils.h"
45
46 /* GstValueUnionFunc:
47  * @dest: a #GValue for the result
48  * @value1: a #GValue operand
49  * @value2: a #GValue operand
50  *
51  * Used by gst_value_union() to perform unification for a specific #GValue
52  * type. Register a new implementation with gst_value_register_union_func().
53  *
54  * Returns: %TRUE if a union was successful
55  */
56 typedef gboolean (*GstValueUnionFunc) (GValue * dest,
57     const GValue * value1, const GValue * value2);
58
59 /* GstValueIntersectFunc:
60  * @dest: (out caller-allocates): a #GValue for the result
61  * @value1: a #GValue operand
62  * @value2: a #GValue operand
63  *
64  * Used by gst_value_intersect() to perform intersection for a specific #GValue
65  * type. If the intersection is non-empty, the result is
66  * placed in @dest and %TRUE is returned.  If the intersection is
67  * empty, @dest is unmodified and %FALSE is returned.
68  * Register a new implementation with gst_value_register_intersect_func().
69  *
70  * Returns: %TRUE if the values can intersect
71  */
72 typedef gboolean (*GstValueIntersectFunc) (GValue * dest,
73     const GValue * value1, const GValue * value2);
74
75 /* GstValueSubtractFunc:
76  * @dest: (out caller-allocates): a #GValue for the result
77  * @minuend: a #GValue operand
78  * @subtrahend: a #GValue operand
79  *
80  * Used by gst_value_subtract() to perform subtraction for a specific #GValue
81  * type. Register a new implementation with gst_value_register_subtract_func().
82  *
83  * Returns: %TRUE if the subtraction is not empty
84  */
85 typedef gboolean (*GstValueSubtractFunc) (GValue * dest,
86     const GValue * minuend, const GValue * subtrahend);
87
88 static void gst_value_register_union_func (GType type1,
89     GType type2, GstValueUnionFunc func);
90 static void gst_value_register_intersect_func (GType type1,
91     GType type2, GstValueIntersectFunc func);
92 static void gst_value_register_subtract_func (GType minuend_type,
93     GType subtrahend_type, GstValueSubtractFunc func);
94
95 typedef struct _GstValueUnionInfo GstValueUnionInfo;
96 struct _GstValueUnionInfo
97 {
98   GType type1;
99   GType type2;
100   GstValueUnionFunc func;
101 };
102
103 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
104 struct _GstValueIntersectInfo
105 {
106   GType type1;
107   GType type2;
108   GstValueIntersectFunc func;
109 };
110
111 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
112 struct _GstValueSubtractInfo
113 {
114   GType minuend;
115   GType subtrahend;
116   GstValueSubtractFunc func;
117 };
118
119 #define FUNDAMENTAL_TYPE_ID_MAX \
120     (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
121 #define FUNDAMENTAL_TYPE_ID(type) \
122     ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
123
124 #define VALUE_LIST_SIZE(v) (((GArray *) (v)->data[0].v_pointer)->len)
125 #define VALUE_LIST_GET_VALUE(v, index) ((const GValue *) &g_array_index ((GArray *) (v)->data[0].v_pointer, GValue, (index)))
126
127 static GArray *gst_value_table;
128 static GHashTable *gst_value_hash;
129 static GstValueTable *gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID_MAX + 1];
130 static GArray *gst_value_union_funcs;
131 static GArray *gst_value_intersect_funcs;
132 static GArray *gst_value_subtract_funcs;
133
134 /* Forward declarations */
135 static gchar *gst_value_serialize_fraction (const GValue * value);
136
137 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
138 static gint gst_value_compare_with_func (const GValue * value1,
139     const GValue * value2, GstValueCompareFunc compare);
140
141 static gchar *gst_string_wrap (const gchar * s);
142 static gchar *gst_string_take_and_wrap (gchar * s);
143 static gchar *gst_string_unwrap (const gchar * s);
144
145 static void gst_value_move (GValue * dest, GValue * src);
146 static void _gst_value_list_append_and_take_value (GValue * value,
147     GValue * append_value);
148 static void _gst_value_array_append_and_take_value (GValue * value,
149     GValue * append_value);
150
151 static inline GstValueTable *
152 gst_value_hash_lookup_type (GType type)
153 {
154   if (G_LIKELY (G_TYPE_IS_FUNDAMENTAL (type)))
155     return gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)];
156   else
157     return g_hash_table_lookup (gst_value_hash, (gpointer) type);
158 }
159
160 static void
161 gst_value_hash_add_type (GType type, const GstValueTable * table)
162 {
163   if (G_TYPE_IS_FUNDAMENTAL (type))
164     gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)] = (gpointer) table;
165
166   g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table);
167 }
168
169 /********
170  * list *
171  ********/
172
173 /* two helper functions to serialize/stringify any type of list
174  * regular lists are done with { }, arrays with < >
175  */
176 static gchar *
177 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
178     const gchar * end)
179 {
180   guint i;
181   GArray *array = value->data[0].v_pointer;
182   GString *s;
183   GValue *v;
184   gchar *s_val;
185   guint alen = array->len;
186
187   /* estimate minimum string length to minimise re-allocs in GString */
188   s = g_string_sized_new (2 + (6 * alen) + 2);
189   g_string_append (s, begin);
190   for (i = 0; i < alen; i++) {
191     v = &g_array_index (array, GValue, i);
192     s_val = gst_value_serialize (v);
193     if (s_val != NULL) {
194       g_string_append (s, s_val);
195       g_free (s_val);
196       if (i < alen - 1) {
197         g_string_append_len (s, ", ", 2);
198       }
199     } else {
200       GST_WARNING ("Could not serialize list/array value of type '%s'",
201           G_VALUE_TYPE_NAME (v));
202     }
203   }
204   g_string_append (s, end);
205   return g_string_free (s, FALSE);
206 }
207
208 static void
209 gst_value_transform_any_list_string (const GValue * src_value,
210     GValue * dest_value, const gchar * begin, const gchar * end)
211 {
212   GValue *list_value;
213   GArray *array;
214   GString *s;
215   guint i;
216   gchar *list_s;
217   guint alen;
218
219   array = src_value->data[0].v_pointer;
220   alen = array->len;
221
222   /* estimate minimum string length to minimise re-allocs in GString */
223   s = g_string_sized_new (2 + (10 * alen) + 2);
224   g_string_append (s, begin);
225   for (i = 0; i < alen; i++) {
226     list_value = &g_array_index (array, GValue, i);
227
228     if (i != 0) {
229       g_string_append_len (s, ", ", 2);
230     }
231     list_s = g_strdup_value_contents (list_value);
232     g_string_append (s, list_s);
233     g_free (list_s);
234   }
235   g_string_append (s, end);
236
237   dest_value->data[0].v_pointer = g_string_free (s, FALSE);
238 }
239
240 /*
241  * helper function to see if a type is fixed. Is used internally here and
242  * there. Do not export, since it doesn't work for types where the content
243  * decides the fixedness (e.g. GST_TYPE_ARRAY).
244  */
245 static gboolean
246 gst_type_is_fixed (GType type)
247 {
248   /* the basic int, string, double types */
249   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
250     return TRUE;
251   }
252   /* our fundamental types that are certainly not fixed */
253   if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
254       type == GST_TYPE_INT64_RANGE ||
255       type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
256     return FALSE;
257   }
258   /* other (boxed) types that are fixed */
259   if (type == GST_TYPE_BUFFER) {
260     return TRUE;
261   }
262   /* heavy checks */
263   if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
264       G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
265     return TRUE;
266   }
267
268   return FALSE;
269 }
270
271 /* GValue functions usable for both regular lists and arrays */
272 static void
273 gst_value_init_list_or_array (GValue * value)
274 {
275   value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
276 }
277
278 static GArray *
279 copy_garray_of_gstvalue (const GArray * src)
280 {
281   GArray *dest;
282   guint i, len;
283
284   len = src->len;
285   dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), len);
286   g_array_set_size (dest, len);
287   for (i = 0; i < len; i++) {
288     gst_value_init_and_copy (&g_array_index (dest, GValue, i),
289         &g_array_index (src, GValue, i));
290   }
291
292   return dest;
293 }
294
295 static void
296 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
297 {
298   dest_value->data[0].v_pointer =
299       copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
300 }
301
302 static void
303 gst_value_free_list_or_array (GValue * value)
304 {
305   guint i, len;
306   GArray *src = (GArray *) value->data[0].v_pointer;
307   len = src->len;
308
309   if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
310     for (i = 0; i < len; i++) {
311       g_value_unset (&g_array_index (src, GValue, i));
312     }
313     g_array_free (src, TRUE);
314   }
315 }
316
317 static gpointer
318 gst_value_list_or_array_peek_pointer (const GValue * value)
319 {
320   return value->data[0].v_pointer;
321 }
322
323 static gchar *
324 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
325     GTypeCValue * collect_values, guint collect_flags)
326 {
327   if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
328     value->data[0].v_pointer = collect_values[0].v_pointer;
329     value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
330   } else {
331     value->data[0].v_pointer =
332         copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
333   }
334   return NULL;
335 }
336
337 static gchar *
338 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
339     GTypeCValue * collect_values, guint collect_flags)
340 {
341   GArray **dest = collect_values[0].v_pointer;
342
343   if (!dest)
344     return g_strdup_printf ("value location for `%s' passed as NULL",
345         G_VALUE_TYPE_NAME (value));
346   if (!value->data[0].v_pointer)
347     return g_strdup_printf ("invalid value given for `%s'",
348         G_VALUE_TYPE_NAME (value));
349   if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
350     *dest = (GArray *) value->data[0].v_pointer;
351   } else {
352     *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
353   }
354   return NULL;
355 }
356
357 static gboolean
358 gst_value_list_or_array_get_basic_type (const GValue * value, GType * type)
359 {
360   if (G_UNLIKELY (value == NULL))
361     return FALSE;
362
363   if (GST_VALUE_HOLDS_LIST (value)) {
364     if (VALUE_LIST_SIZE (value) == 0)
365       return FALSE;
366     return gst_value_list_or_array_get_basic_type (VALUE_LIST_GET_VALUE (value,
367             0), type);
368   }
369   if (GST_VALUE_HOLDS_ARRAY (value)) {
370     const GArray *array = (const GArray *) value->data[0].v_pointer;
371     if (array->len == 0)
372       return FALSE;
373     return gst_value_list_or_array_get_basic_type (&g_array_index (array,
374             GValue, 0), type);
375   }
376
377   *type = G_VALUE_TYPE (value);
378
379   return TRUE;
380 }
381
382 #define IS_RANGE_COMPAT(type1,type2,t1,t2) \
383   (((t1) == (type1) && (t2) == (type2)) || ((t2) == (type1) && (t1) == (type2)))
384
385 static gboolean
386 gst_value_list_or_array_are_compatible (const GValue * value1,
387     const GValue * value2)
388 {
389   GType basic_type1, basic_type2;
390
391   /* empty or same type is OK */
392   if (!gst_value_list_or_array_get_basic_type (value1, &basic_type1) ||
393       !gst_value_list_or_array_get_basic_type (value2, &basic_type2) ||
394       basic_type1 == basic_type2)
395     return TRUE;
396
397   /* ranges are distinct types for each bound type... */
398   if (IS_RANGE_COMPAT (G_TYPE_INT, GST_TYPE_INT_RANGE, basic_type1,
399           basic_type2))
400     return TRUE;
401   if (IS_RANGE_COMPAT (G_TYPE_INT64, GST_TYPE_INT64_RANGE, basic_type1,
402           basic_type2))
403     return TRUE;
404   if (IS_RANGE_COMPAT (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE, basic_type1,
405           basic_type2))
406     return TRUE;
407   if (IS_RANGE_COMPAT (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, basic_type1,
408           basic_type2))
409     return TRUE;
410
411   return FALSE;
412 }
413
414 static inline void
415 _gst_value_list_append_and_take_value (GValue * value, GValue * append_value)
416 {
417   g_array_append_vals ((GArray *) value->data[0].v_pointer, append_value, 1);
418   memset (append_value, 0, sizeof (GValue));
419 }
420
421 /**
422  * gst_value_list_append_and_take_value:
423  * @value: a #GValue of type #GST_TYPE_LIST
424  * @append_value: (transfer full): the value to append
425  *
426  * Appends @append_value to the GstValueList in @value.
427  *
428  * Since: 1.2
429  */
430 void
431 gst_value_list_append_and_take_value (GValue * value, GValue * append_value)
432 {
433   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
434   g_return_if_fail (G_IS_VALUE (append_value));
435   g_return_if_fail (gst_value_list_or_array_are_compatible (value,
436           append_value));
437
438   _gst_value_list_append_and_take_value (value, append_value);
439 }
440
441 /**
442  * gst_value_list_append_value:
443  * @value: a #GValue of type #GST_TYPE_LIST
444  * @append_value: (transfer none): the value to append
445  *
446  * Appends @append_value to the GstValueList in @value.
447  */
448 void
449 gst_value_list_append_value (GValue * value, const GValue * append_value)
450 {
451   GValue val = { 0, };
452
453   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
454   g_return_if_fail (G_IS_VALUE (append_value));
455   g_return_if_fail (gst_value_list_or_array_are_compatible (value,
456           append_value));
457
458   gst_value_init_and_copy (&val, append_value);
459   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
460 }
461
462 /**
463  * gst_value_list_prepend_value:
464  * @value: a #GValue of type #GST_TYPE_LIST
465  * @prepend_value: the value to prepend
466  *
467  * Prepends @prepend_value to the GstValueList in @value.
468  */
469 void
470 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
471 {
472   GValue val = { 0, };
473
474   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
475   g_return_if_fail (G_IS_VALUE (prepend_value));
476   g_return_if_fail (gst_value_list_or_array_are_compatible (value,
477           prepend_value));
478
479   gst_value_init_and_copy (&val, prepend_value);
480   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
481 }
482
483 /**
484  * gst_value_list_concat:
485  * @dest: (out caller-allocates): an uninitialized #GValue to take the result
486  * @value1: a #GValue
487  * @value2: a #GValue
488  *
489  * Concatenates copies of @value1 and @value2 into a list.  Values that are not
490  * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
491  * @dest will be initialized to the type #GST_TYPE_LIST.
492  */
493 void
494 gst_value_list_concat (GValue * dest, const GValue * value1,
495     const GValue * value2)
496 {
497   guint i, value1_length, value2_length;
498   GArray *array;
499
500   g_return_if_fail (dest != NULL);
501   g_return_if_fail (G_VALUE_TYPE (dest) == 0);
502   g_return_if_fail (G_IS_VALUE (value1));
503   g_return_if_fail (G_IS_VALUE (value2));
504   g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
505
506   value1_length =
507       (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
508   value2_length =
509       (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
510   g_value_init (dest, GST_TYPE_LIST);
511   array = (GArray *) dest->data[0].v_pointer;
512   g_array_set_size (array, value1_length + value2_length);
513
514   if (GST_VALUE_HOLDS_LIST (value1)) {
515     for (i = 0; i < value1_length; i++) {
516       gst_value_init_and_copy (&g_array_index (array, GValue, i),
517           VALUE_LIST_GET_VALUE (value1, i));
518     }
519   } else {
520     gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
521   }
522
523   if (GST_VALUE_HOLDS_LIST (value2)) {
524     for (i = 0; i < value2_length; i++) {
525       gst_value_init_and_copy (&g_array_index (array, GValue,
526               i + value1_length), VALUE_LIST_GET_VALUE (value2, i));
527     }
528   } else {
529     gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
530         value2);
531   }
532 }
533
534 /**
535  * gst_value_list_merge:
536  * @dest: (out caller-allocates): an uninitialized #GValue to take the result
537  * @value1: a #GValue
538  * @value2: a #GValue
539  *
540  * Merges copies of @value1 and @value2.  Values that are not
541  * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
542  *
543  * The result will be put into @dest and will either be a list that will not
544  * contain any duplicates, or a non-list type (if @value1 and @value2
545  * were equal).
546  */
547 void
548 gst_value_list_merge (GValue * dest, const GValue * value1,
549     const GValue * value2)
550 {
551   guint i, j, k, value1_length, value2_length, skipped;
552   const GValue *src;
553   gboolean skip;
554   GArray *array;
555
556   g_return_if_fail (dest != NULL);
557   g_return_if_fail (G_VALUE_TYPE (dest) == 0);
558   g_return_if_fail (G_IS_VALUE (value1));
559   g_return_if_fail (G_IS_VALUE (value2));
560   g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
561
562   value1_length =
563       (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
564   value2_length =
565       (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
566   g_value_init (dest, GST_TYPE_LIST);
567   array = (GArray *) dest->data[0].v_pointer;
568   g_array_set_size (array, value1_length + value2_length);
569
570   if (GST_VALUE_HOLDS_LIST (value1)) {
571     for (i = 0; i < value1_length; i++) {
572       gst_value_init_and_copy (&g_array_index (array, GValue, i),
573           VALUE_LIST_GET_VALUE (value1, i));
574     }
575   } else {
576     gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
577   }
578
579   j = value1_length;
580   skipped = 0;
581   if (GST_VALUE_HOLDS_LIST (value2)) {
582     for (i = 0; i < value2_length; i++) {
583       skip = FALSE;
584       src = VALUE_LIST_GET_VALUE (value2, i);
585       for (k = 0; k < value1_length; k++) {
586         if (gst_value_compare (&g_array_index (array, GValue, k),
587                 src) == GST_VALUE_EQUAL) {
588           skip = TRUE;
589           skipped++;
590           break;
591         }
592       }
593       if (!skip) {
594         gst_value_init_and_copy (&g_array_index (array, GValue, j), src);
595         j++;
596       }
597     }
598   } else {
599     skip = FALSE;
600     for (k = 0; k < value1_length; k++) {
601       if (gst_value_compare (&g_array_index (array, GValue, k),
602               value2) == GST_VALUE_EQUAL) {
603         skip = TRUE;
604         skipped++;
605         break;
606       }
607     }
608     if (!skip) {
609       gst_value_init_and_copy (&g_array_index (array, GValue, j), value2);
610     }
611   }
612   if (skipped) {
613     guint new_size = value1_length + (value2_length - skipped);
614
615     if (new_size > 1) {
616       /* shrink list */
617       g_array_set_size (array, new_size);
618     } else {
619       GValue single_dest;
620
621       /* size is 1, take single value in list and make it new dest */
622       single_dest = g_array_index (array, GValue, 0);
623
624       /* clean up old value allocations: must set array size to 0, because
625        * allocated values are not inited meaning g_value_unset() will not
626        * work on them */
627       g_array_set_size (array, 0);
628       g_value_unset (dest);
629
630       /* the single value is our new result */
631       *dest = single_dest;
632     }
633   }
634 }
635
636 /**
637  * gst_value_list_get_size:
638  * @value: a #GValue of type #GST_TYPE_LIST
639  *
640  * Gets the number of values contained in @value.
641  *
642  * Returns: the number of values
643  */
644 guint
645 gst_value_list_get_size (const GValue * value)
646 {
647   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
648
649   return ((GArray *) value->data[0].v_pointer)->len;
650 }
651
652 /**
653  * gst_value_list_get_value:
654  * @value: a #GValue of type #GST_TYPE_LIST
655  * @index: index of value to get from the list
656  *
657  * Gets the value that is a member of the list contained in @value and
658  * has the index @index.
659  *
660  * Returns: (transfer none): the value at the given index
661  */
662 const GValue *
663 gst_value_list_get_value (const GValue * value, guint index)
664 {
665   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
666   g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
667
668   return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
669       GValue, index);
670 }
671
672 /**
673  * gst_value_array_append_value:
674  * @value: a #GValue of type #GST_TYPE_ARRAY
675  * @append_value: the value to append
676  *
677  * Appends @append_value to the GstValueArray in @value.
678  */
679 void
680 gst_value_array_append_value (GValue * value, const GValue * append_value)
681 {
682   GValue val = { 0, };
683
684   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
685   g_return_if_fail (G_IS_VALUE (append_value));
686   g_return_if_fail (gst_value_list_or_array_are_compatible (value,
687           append_value));
688
689   gst_value_init_and_copy (&val, append_value);
690   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
691 }
692
693 static inline void
694 _gst_value_array_append_and_take_value (GValue * value, GValue * append_value)
695 {
696   g_array_append_vals ((GArray *) value->data[0].v_pointer, append_value, 1);
697   memset (append_value, 0, sizeof (GValue));
698 }
699
700 /**
701  * gst_value_array_append_and_take_value:
702  * @value: a #GValue of type #GST_TYPE_ARRAY
703  * @append_value: (transfer full): the value to append
704  *
705  * Appends @append_value to the GstValueArray in @value.
706  *
707  * Since: 1.2
708  */
709 void
710 gst_value_array_append_and_take_value (GValue * value, GValue * append_value)
711 {
712   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
713   g_return_if_fail (G_IS_VALUE (append_value));
714   g_return_if_fail (gst_value_list_or_array_are_compatible (value,
715           append_value));
716
717   _gst_value_array_append_and_take_value (value, append_value);
718 }
719
720 /**
721  * gst_value_array_prepend_value:
722  * @value: a #GValue of type #GST_TYPE_ARRAY
723  * @prepend_value: the value to prepend
724  *
725  * Prepends @prepend_value to the GstValueArray in @value.
726  */
727 void
728 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
729 {
730   GValue val = { 0, };
731
732   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
733   g_return_if_fail (G_IS_VALUE (prepend_value));
734   g_return_if_fail (gst_value_list_or_array_are_compatible (value,
735           prepend_value));
736
737   gst_value_init_and_copy (&val, prepend_value);
738   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
739 }
740
741 /**
742  * gst_value_array_get_size:
743  * @value: a #GValue of type #GST_TYPE_ARRAY
744  *
745  * Gets the number of values contained in @value.
746  *
747  * Returns: the number of values
748  */
749 guint
750 gst_value_array_get_size (const GValue * value)
751 {
752   g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
753
754   return ((GArray *) value->data[0].v_pointer)->len;
755 }
756
757 /**
758  * gst_value_array_get_value:
759  * @value: a #GValue of type #GST_TYPE_ARRAY
760  * @index: index of value to get from the array
761  *
762  * Gets the value that is a member of the array contained in @value and
763  * has the index @index.
764  *
765  * Returns: (transfer none): the value at the given index
766  */
767 const GValue *
768 gst_value_array_get_value (const GValue * value, guint index)
769 {
770   g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
771   g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
772
773   return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
774       GValue, index);
775 }
776
777 static void
778 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
779 {
780   gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
781 }
782
783 static void
784 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
785 {
786   gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
787 }
788
789 /* Do an unordered compare of the contents of a list */
790 static gint
791 gst_value_compare_list (const GValue * value1, const GValue * value2)
792 {
793   guint i, j;
794   GArray *array1 = value1->data[0].v_pointer;
795   GArray *array2 = value2->data[0].v_pointer;
796   GValue *v1;
797   GValue *v2;
798   gint len, to_remove;
799   guint8 *removed;
800   GstValueCompareFunc compare;
801
802   /* get length and do initial length check. */
803   len = array1->len;
804   if (len != array2->len)
805     return GST_VALUE_UNORDERED;
806
807   /* place to mark removed value indices of array2 */
808   removed = g_newa (guint8, len);
809   memset (removed, 0, len);
810   to_remove = len;
811
812   /* loop over array1, all items should be in array2. When we find an
813    * item in array2, remove it from array2 by marking it as removed */
814   for (i = 0; i < len; i++) {
815     v1 = &g_array_index (array1, GValue, i);
816     if ((compare = gst_value_get_compare_func (v1))) {
817       for (j = 0; j < len; j++) {
818         /* item is removed, we can skip it */
819         if (removed[j])
820           continue;
821         v2 = &g_array_index (array2, GValue, j);
822         if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
823           /* mark item as removed now that we found it in array2 and 
824            * decrement the number of remaining items in array2. */
825           removed[j] = 1;
826           to_remove--;
827           break;
828         }
829       }
830       /* item in array1 and not in array2, UNORDERED */
831       if (j == len)
832         return GST_VALUE_UNORDERED;
833     } else
834       return GST_VALUE_UNORDERED;
835   }
836   /* if not all items were removed, array2 contained something not in array1 */
837   if (to_remove != 0)
838     return GST_VALUE_UNORDERED;
839
840   /* arrays are equal */
841   return GST_VALUE_EQUAL;
842 }
843
844 /* Perform an ordered comparison of the contents of an array */
845 static gint
846 gst_value_compare_array (const GValue * value1, const GValue * value2)
847 {
848   guint i;
849   GArray *array1 = value1->data[0].v_pointer;
850   GArray *array2 = value2->data[0].v_pointer;
851   guint len = array1->len;
852   GValue *v1;
853   GValue *v2;
854
855   if (len != array2->len)
856     return GST_VALUE_UNORDERED;
857
858   for (i = 0; i < len; i++) {
859     v1 = &g_array_index (array1, GValue, i);
860     v2 = &g_array_index (array2, GValue, i);
861     if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
862       return GST_VALUE_UNORDERED;
863   }
864
865   return GST_VALUE_EQUAL;
866 }
867
868 static gchar *
869 gst_value_serialize_list (const GValue * value)
870 {
871   return gst_value_serialize_any_list (value, "{ ", " }");
872 }
873
874 static gboolean
875 gst_value_deserialize_list (GValue * dest, const gchar * s)
876 {
877   g_warning ("gst_value_deserialize_list: unimplemented");
878   return FALSE;
879 }
880
881 static gchar *
882 gst_value_serialize_array (const GValue * value)
883 {
884   return gst_value_serialize_any_list (value, "< ", " >");
885 }
886
887 static gboolean
888 gst_value_deserialize_array (GValue * dest, const gchar * s)
889 {
890   g_warning ("gst_value_deserialize_array: unimplemented");
891   return FALSE;
892 }
893
894 /*************
895  * int range *
896  *
897  * Values in the range are defined as any value greater or equal
898  * to min*step, AND lesser or equal to max*step.
899  * For step == 1, this falls back to the traditional range semantics.
900  *
901  * data[0] = (min << 32) | (max)
902  * data[1] = step
903  *
904  *************/
905
906 #define INT_RANGE_MIN(v) ((gint) (((v)->data[0].v_uint64) >> 32))
907 #define INT_RANGE_MAX(v) ((gint) (((v)->data[0].v_uint64) & 0xffffffff))
908 #define INT_RANGE_STEP(v) ((v)->data[1].v_int)
909
910 static void
911 gst_value_init_int_range (GValue * value)
912 {
913   G_STATIC_ASSERT (sizeof (gint) <= 2 * sizeof (guint64));
914
915   value->data[0].v_uint64 = 0;
916   value->data[1].v_int = 1;
917 }
918
919 static void
920 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
921 {
922   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
923   dest_value->data[1].v_int = src_value->data[1].v_int;
924 }
925
926 static gchar *
927 gst_value_collect_int_range (GValue * value, guint n_collect_values,
928     GTypeCValue * collect_values, guint collect_flags)
929 {
930   if (n_collect_values != 2)
931     return g_strdup_printf ("not enough value locations for `%s' passed",
932         G_VALUE_TYPE_NAME (value));
933   if (collect_values[0].v_int >= collect_values[1].v_int)
934     return g_strdup_printf ("range start is not smaller than end for `%s'",
935         G_VALUE_TYPE_NAME (value));
936
937   gst_value_set_int_range_step (value, collect_values[0].v_int,
938       collect_values[1].v_int, 1);
939
940   return NULL;
941 }
942
943 static gchar *
944 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
945     GTypeCValue * collect_values, guint collect_flags)
946 {
947   guint32 *int_range_start = collect_values[0].v_pointer;
948   guint32 *int_range_end = collect_values[1].v_pointer;
949
950   if (!int_range_start)
951     return g_strdup_printf ("start value location for `%s' passed as NULL",
952         G_VALUE_TYPE_NAME (value));
953   if (!int_range_end)
954     return g_strdup_printf ("end value location for `%s' passed as NULL",
955         G_VALUE_TYPE_NAME (value));
956
957   *int_range_start = INT_RANGE_MIN (value);
958   *int_range_end = INT_RANGE_MAX (value);
959
960   return NULL;
961 }
962
963 /**
964  * gst_value_set_int_range_step:
965  * @value: a GValue initialized to GST_TYPE_INT_RANGE
966  * @start: the start of the range
967  * @end: the end of the range
968  * @step: the step of the range
969  *
970  * Sets @value to the range specified by @start, @end and @step.
971  */
972 void
973 gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
974 {
975   guint64 sstart, sstop;
976
977   g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
978   g_return_if_fail (start < end);
979   g_return_if_fail (step > 0);
980   g_return_if_fail (start % step == 0);
981   g_return_if_fail (end % step == 0);
982
983   sstart = (guint) (start / step);
984   sstop = (guint) (end / step);
985   value->data[0].v_uint64 = (sstart << 32) | sstop;
986   value->data[1].v_int = step;
987 }
988
989 /**
990  * gst_value_set_int_range:
991  * @value: a GValue initialized to GST_TYPE_INT_RANGE
992  * @start: the start of the range
993  * @end: the end of the range
994  *
995  * Sets @value to the range specified by @start and @end.
996  */
997 void
998 gst_value_set_int_range (GValue * value, gint start, gint end)
999 {
1000   gst_value_set_int_range_step (value, start, end, 1);
1001 }
1002
1003 /**
1004  * gst_value_get_int_range_min:
1005  * @value: a GValue initialized to GST_TYPE_INT_RANGE
1006  *
1007  * Gets the minimum of the range specified by @value.
1008  *
1009  * Returns: the minimum of the range
1010  */
1011 gint
1012 gst_value_get_int_range_min (const GValue * value)
1013 {
1014   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1015
1016   return INT_RANGE_MIN (value) * INT_RANGE_STEP (value);
1017 }
1018
1019 /**
1020  * gst_value_get_int_range_max:
1021  * @value: a GValue initialized to GST_TYPE_INT_RANGE
1022  *
1023  * Gets the maximum of the range specified by @value.
1024  *
1025  * Returns: the maximum of the range
1026  */
1027 gint
1028 gst_value_get_int_range_max (const GValue * value)
1029 {
1030   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1031
1032   return INT_RANGE_MAX (value) * INT_RANGE_STEP (value);
1033 }
1034
1035 /**
1036  * gst_value_get_int_range_step:
1037  * @value: a GValue initialized to GST_TYPE_INT_RANGE
1038  *
1039  * Gets the step of the range specified by @value.
1040  *
1041  * Returns: the step of the range
1042  */
1043 gint
1044 gst_value_get_int_range_step (const GValue * value)
1045 {
1046   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1047
1048   return INT_RANGE_STEP (value);
1049 }
1050
1051 static void
1052 gst_value_transform_int_range_string (const GValue * src_value,
1053     GValue * dest_value)
1054 {
1055   if (INT_RANGE_STEP (src_value) == 1)
1056     dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
1057         INT_RANGE_MIN (src_value), INT_RANGE_MAX (src_value));
1058   else
1059     dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d,%d]",
1060         INT_RANGE_MIN (src_value) * INT_RANGE_STEP (src_value),
1061         INT_RANGE_MAX (src_value) * INT_RANGE_STEP (src_value),
1062         INT_RANGE_STEP (src_value));
1063 }
1064
1065 static gint
1066 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
1067 {
1068   /* calculate the number of values in each range */
1069   gint n1 = INT_RANGE_MAX (value1) - INT_RANGE_MIN (value1) + 1;
1070   gint n2 = INT_RANGE_MAX (value2) - INT_RANGE_MIN (value2) + 1;
1071
1072   /* they must be equal */
1073   if (n1 != n2)
1074     return GST_VALUE_UNORDERED;
1075
1076   /* if empty, equal */
1077   if (n1 == 0)
1078     return GST_VALUE_EQUAL;
1079
1080   /* if more than one value, then it is only equal if the step is equal
1081      and bounds lie on the same value */
1082   if (n1 > 1) {
1083     if (INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2) &&
1084         INT_RANGE_MIN (value1) == INT_RANGE_MIN (value2) &&
1085         INT_RANGE_MAX (value1) == INT_RANGE_MAX (value2)) {
1086       return GST_VALUE_EQUAL;
1087     }
1088     return GST_VALUE_UNORDERED;
1089   } else {
1090     /* if just one, only if the value is equal */
1091     if (INT_RANGE_MIN (value1) == INT_RANGE_MIN (value2))
1092       return GST_VALUE_EQUAL;
1093     return GST_VALUE_UNORDERED;
1094   }
1095 }
1096
1097 static gchar *
1098 gst_value_serialize_int_range (const GValue * value)
1099 {
1100   if (INT_RANGE_STEP (value) == 1)
1101     return g_strdup_printf ("[ %d, %d ]", INT_RANGE_MIN (value),
1102         INT_RANGE_MAX (value));
1103   else
1104     return g_strdup_printf ("[ %d, %d, %d ]",
1105         INT_RANGE_MIN (value) * INT_RANGE_STEP (value),
1106         INT_RANGE_MAX (value) * INT_RANGE_STEP (value), INT_RANGE_STEP (value));
1107 }
1108
1109 static gboolean
1110 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1111 {
1112   g_warning ("unimplemented");
1113   return FALSE;
1114 }
1115
1116 /***************
1117  * int64 range *
1118  *
1119  * Values in the range are defined as any value greater or equal
1120  * to min*step, AND lesser or equal to max*step.
1121  * For step == 1, this falls back to the traditional range semantics.
1122  ***************/
1123
1124 #define INT64_RANGE_MIN(v) (((gint64 *)((v)->data[0].v_pointer))[0])
1125 #define INT64_RANGE_MAX(v) (((gint64 *)((v)->data[0].v_pointer))[1])
1126 #define INT64_RANGE_STEP(v) (((gint64 *)((v)->data[0].v_pointer))[2])
1127
1128 static void
1129 gst_value_init_int64_range (GValue * value)
1130 {
1131   gint64 *vals = g_slice_alloc0 (3 * sizeof (gint64));
1132   value->data[0].v_pointer = vals;
1133   INT64_RANGE_MIN (value) = 0;
1134   INT64_RANGE_MAX (value) = 0;
1135   INT64_RANGE_STEP (value) = 1;
1136 }
1137
1138 static void
1139 gst_value_free_int64_range (GValue * value)
1140 {
1141   g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1142   g_slice_free1 (3 * sizeof (gint64), value->data[0].v_pointer);
1143   value->data[0].v_pointer = NULL;
1144 }
1145
1146 static void
1147 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1148 {
1149   gint64 *vals = (gint64 *) dest_value->data[0].v_pointer;
1150   gint64 *src_vals = (gint64 *) src_value->data[0].v_pointer;
1151
1152   if (vals == NULL) {
1153     gst_value_init_int64_range (dest_value);
1154   }
1155
1156   if (src_vals != NULL) {
1157     INT64_RANGE_MIN (dest_value) = INT64_RANGE_MIN (src_value);
1158     INT64_RANGE_MAX (dest_value) = INT64_RANGE_MAX (src_value);
1159     INT64_RANGE_STEP (dest_value) = INT64_RANGE_STEP (src_value);
1160   }
1161 }
1162
1163 static gchar *
1164 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1165     GTypeCValue * collect_values, guint collect_flags)
1166 {
1167   gint64 *vals = value->data[0].v_pointer;
1168
1169   if (n_collect_values != 2)
1170     return g_strdup_printf ("not enough value locations for `%s' passed",
1171         G_VALUE_TYPE_NAME (value));
1172   if (collect_values[0].v_int64 >= collect_values[1].v_int64)
1173     return g_strdup_printf ("range start is not smaller than end for `%s'",
1174         G_VALUE_TYPE_NAME (value));
1175
1176   if (vals == NULL) {
1177     gst_value_init_int64_range (value);
1178   }
1179
1180   gst_value_set_int64_range_step (value, collect_values[0].v_int64,
1181       collect_values[1].v_int64, 1);
1182
1183   return NULL;
1184 }
1185
1186 static gchar *
1187 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1188     GTypeCValue * collect_values, guint collect_flags)
1189 {
1190   guint64 *int_range_start = collect_values[0].v_pointer;
1191   guint64 *int_range_end = collect_values[1].v_pointer;
1192   guint64 *int_range_step = collect_values[2].v_pointer;
1193   gint64 *vals = (gint64 *) value->data[0].v_pointer;
1194
1195   if (!int_range_start)
1196     return g_strdup_printf ("start value location for `%s' passed as NULL",
1197         G_VALUE_TYPE_NAME (value));
1198   if (!int_range_end)
1199     return g_strdup_printf ("end value location for `%s' passed as NULL",
1200         G_VALUE_TYPE_NAME (value));
1201   if (!int_range_step)
1202     return g_strdup_printf ("step value location for `%s' passed as NULL",
1203         G_VALUE_TYPE_NAME (value));
1204
1205   if (G_UNLIKELY (vals == NULL)) {
1206     return g_strdup_printf ("Uninitialised `%s' passed",
1207         G_VALUE_TYPE_NAME (value));
1208   }
1209
1210   *int_range_start = INT64_RANGE_MIN (value);
1211   *int_range_end = INT64_RANGE_MAX (value);
1212   *int_range_step = INT64_RANGE_STEP (value);
1213
1214   return NULL;
1215 }
1216
1217 /**
1218  * gst_value_set_int64_range_step:
1219  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1220  * @start: the start of the range
1221  * @end: the end of the range
1222  * @step: the step of the range
1223  *
1224  * Sets @value to the range specified by @start, @end and @step.
1225  */
1226 void
1227 gst_value_set_int64_range_step (GValue * value, gint64 start, gint64 end,
1228     gint64 step)
1229 {
1230   g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1231   g_return_if_fail (start < end);
1232   g_return_if_fail (step > 0);
1233   g_return_if_fail (start % step == 0);
1234   g_return_if_fail (end % step == 0);
1235
1236   INT64_RANGE_MIN (value) = start / step;
1237   INT64_RANGE_MAX (value) = end / step;
1238   INT64_RANGE_STEP (value) = step;
1239 }
1240
1241 /**
1242  * gst_value_set_int64_range:
1243  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1244  * @start: the start of the range
1245  * @end: the end of the range
1246  *
1247  * Sets @value to the range specified by @start and @end.
1248  */
1249 void
1250 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1251 {
1252   gst_value_set_int64_range_step (value, start, end, 1);
1253 }
1254
1255 /**
1256  * gst_value_get_int64_range_min:
1257  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1258  *
1259  * Gets the minimum of the range specified by @value.
1260  *
1261  * Returns: the minimum of the range
1262  */
1263 gint64
1264 gst_value_get_int64_range_min (const GValue * value)
1265 {
1266   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1267
1268   return INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value);
1269 }
1270
1271 /**
1272  * gst_value_get_int64_range_max:
1273  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1274  *
1275  * Gets the maximum of the range specified by @value.
1276  *
1277  * Returns: the maximum of the range
1278  */
1279 gint64
1280 gst_value_get_int64_range_max (const GValue * value)
1281 {
1282   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1283
1284   return INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value);
1285 }
1286
1287 /**
1288  * gst_value_get_int64_range_step:
1289  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1290  *
1291  * Gets the step of the range specified by @value.
1292  *
1293  * Returns: the step of the range
1294  */
1295 gint64
1296 gst_value_get_int64_range_step (const GValue * value)
1297 {
1298   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1299
1300   return INT64_RANGE_STEP (value);
1301 }
1302
1303 static void
1304 gst_value_transform_int64_range_string (const GValue * src_value,
1305     GValue * dest_value)
1306 {
1307   if (INT64_RANGE_STEP (src_value) == 1)
1308     dest_value->data[0].v_pointer =
1309         g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1310         INT64_RANGE_MIN (src_value), INT64_RANGE_MAX (src_value));
1311   else
1312     dest_value->data[0].v_pointer =
1313         g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT
1314         ",%" G_GINT64_FORMAT "]",
1315         INT64_RANGE_MIN (src_value) * INT64_RANGE_STEP (src_value),
1316         INT64_RANGE_MAX (src_value) * INT64_RANGE_STEP (src_value),
1317         INT64_RANGE_STEP (src_value));
1318 }
1319
1320 static gint
1321 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1322 {
1323   /* calculate the number of values in each range */
1324   gint64 n1 = INT64_RANGE_MAX (value1) - INT64_RANGE_MIN (value1) + 1;
1325   gint64 n2 = INT64_RANGE_MAX (value2) - INT64_RANGE_MIN (value2) + 1;
1326
1327   /* they must be equal */
1328   if (n1 != n2)
1329     return GST_VALUE_UNORDERED;
1330
1331   /* if empty, equal */
1332   if (n1 == 0)
1333     return GST_VALUE_EQUAL;
1334
1335   /* if more than one value, then it is only equal if the step is equal
1336      and bounds lie on the same value */
1337   if (n1 > 1) {
1338     if (INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2) &&
1339         INT64_RANGE_MIN (value1) == INT64_RANGE_MIN (value2) &&
1340         INT64_RANGE_MAX (value1) == INT64_RANGE_MAX (value2)) {
1341       return GST_VALUE_EQUAL;
1342     }
1343     return GST_VALUE_UNORDERED;
1344   } else {
1345     /* if just one, only if the value is equal */
1346     if (INT64_RANGE_MIN (value1) == INT64_RANGE_MIN (value2))
1347       return GST_VALUE_EQUAL;
1348     return GST_VALUE_UNORDERED;
1349   }
1350 }
1351
1352 static gchar *
1353 gst_value_serialize_int64_range (const GValue * value)
1354 {
1355   if (INT64_RANGE_STEP (value) == 1)
1356     return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1357         INT64_RANGE_MIN (value), INT64_RANGE_MAX (value));
1358   else
1359     return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT ", %"
1360         G_GINT64_FORMAT " ]",
1361         INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value),
1362         INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value),
1363         INT64_RANGE_STEP (value));
1364 }
1365
1366 static gboolean
1367 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1368 {
1369   g_warning ("unimplemented");
1370   return FALSE;
1371 }
1372
1373 /****************
1374  * double range *
1375  ****************/
1376
1377 static void
1378 gst_value_init_double_range (GValue * value)
1379 {
1380   value->data[0].v_double = 0;
1381   value->data[1].v_double = 0;
1382 }
1383
1384 static void
1385 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1386 {
1387   dest_value->data[0].v_double = src_value->data[0].v_double;
1388   dest_value->data[1].v_double = src_value->data[1].v_double;
1389 }
1390
1391 static gchar *
1392 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1393     GTypeCValue * collect_values, guint collect_flags)
1394 {
1395   if (n_collect_values != 2)
1396     return g_strdup_printf ("not enough value locations for `%s' passed",
1397         G_VALUE_TYPE_NAME (value));
1398   if (collect_values[0].v_double >= collect_values[1].v_double)
1399     return g_strdup_printf ("range start is not smaller than end for `%s'",
1400         G_VALUE_TYPE_NAME (value));
1401
1402   value->data[0].v_double = collect_values[0].v_double;
1403   value->data[1].v_double = collect_values[1].v_double;
1404
1405   return NULL;
1406 }
1407
1408 static gchar *
1409 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1410     GTypeCValue * collect_values, guint collect_flags)
1411 {
1412   gdouble *double_range_start = collect_values[0].v_pointer;
1413   gdouble *double_range_end = collect_values[1].v_pointer;
1414
1415   if (!double_range_start)
1416     return g_strdup_printf ("start value location for `%s' passed as NULL",
1417         G_VALUE_TYPE_NAME (value));
1418   if (!double_range_end)
1419     return g_strdup_printf ("end value location for `%s' passed as NULL",
1420         G_VALUE_TYPE_NAME (value));
1421
1422   *double_range_start = value->data[0].v_double;
1423   *double_range_end = value->data[1].v_double;
1424
1425   return NULL;
1426 }
1427
1428 /**
1429  * gst_value_set_double_range:
1430  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1431  * @start: the start of the range
1432  * @end: the end of the range
1433  *
1434  * Sets @value to the range specified by @start and @end.
1435  */
1436 void
1437 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1438 {
1439   g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1440   g_return_if_fail (start < end);
1441
1442   value->data[0].v_double = start;
1443   value->data[1].v_double = end;
1444 }
1445
1446 /**
1447  * gst_value_get_double_range_min:
1448  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1449  *
1450  * Gets the minimum of the range specified by @value.
1451  *
1452  * Returns: the minimum of the range
1453  */
1454 gdouble
1455 gst_value_get_double_range_min (const GValue * value)
1456 {
1457   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1458
1459   return value->data[0].v_double;
1460 }
1461
1462 /**
1463  * gst_value_get_double_range_max:
1464  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1465  *
1466  * Gets the maximum of the range specified by @value.
1467  *
1468  * Returns: the maximum of the range
1469  */
1470 gdouble
1471 gst_value_get_double_range_max (const GValue * value)
1472 {
1473   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1474
1475   return value->data[1].v_double;
1476 }
1477
1478 static void
1479 gst_value_transform_double_range_string (const GValue * src_value,
1480     GValue * dest_value)
1481 {
1482   gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1483
1484   dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1485       g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1486           src_value->data[0].v_double),
1487       g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1488           src_value->data[1].v_double));
1489 }
1490
1491 static gint
1492 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1493 {
1494   if (value2->data[0].v_double == value1->data[0].v_double &&
1495       value2->data[1].v_double == value1->data[1].v_double)
1496     return GST_VALUE_EQUAL;
1497   return GST_VALUE_UNORDERED;
1498 }
1499
1500 static gchar *
1501 gst_value_serialize_double_range (const GValue * value)
1502 {
1503   gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1504   gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1505
1506   g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1507   g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1508   return g_strdup_printf ("[ %s, %s ]", d1, d2);
1509 }
1510
1511 static gboolean
1512 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1513 {
1514   g_warning ("unimplemented");
1515   return FALSE;
1516 }
1517
1518 /****************
1519  * fraction range *
1520  ****************/
1521
1522 static void
1523 gst_value_init_fraction_range (GValue * value)
1524 {
1525   GValue *vals;
1526   GType ftype;
1527
1528   ftype = GST_TYPE_FRACTION;
1529
1530   value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1531   g_value_init (&vals[0], ftype);
1532   g_value_init (&vals[1], ftype);
1533 }
1534
1535 static void
1536 gst_value_free_fraction_range (GValue * value)
1537 {
1538   GValue *vals = (GValue *) value->data[0].v_pointer;
1539
1540   if (vals != NULL) {
1541     /* we know the two values contain fractions without internal allocs */
1542     /* g_value_unset (&vals[0]); */
1543     /* g_value_unset (&vals[1]); */
1544     g_slice_free1 (2 * sizeof (GValue), vals);
1545     value->data[0].v_pointer = NULL;
1546   }
1547 }
1548
1549 static void
1550 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1551 {
1552   GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1553   GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1554
1555   if (vals == NULL) {
1556     gst_value_init_fraction_range (dest_value);
1557     vals = dest_value->data[0].v_pointer;
1558   }
1559   if (src_vals != NULL) {
1560     g_value_copy (&src_vals[0], &vals[0]);
1561     g_value_copy (&src_vals[1], &vals[1]);
1562   }
1563 }
1564
1565 static gchar *
1566 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1567     GTypeCValue * collect_values, guint collect_flags)
1568 {
1569   GValue *vals = (GValue *) value->data[0].v_pointer;
1570
1571   if (n_collect_values != 4)
1572     return g_strdup_printf ("not enough value locations for `%s' passed",
1573         G_VALUE_TYPE_NAME (value));
1574   if (collect_values[1].v_int == 0)
1575     return g_strdup_printf ("passed '0' as first denominator for `%s'",
1576         G_VALUE_TYPE_NAME (value));
1577   if (collect_values[3].v_int == 0)
1578     return g_strdup_printf ("passed '0' as second denominator for `%s'",
1579         G_VALUE_TYPE_NAME (value));
1580   if (gst_util_fraction_compare (collect_values[0].v_int,
1581           collect_values[1].v_int, collect_values[2].v_int,
1582           collect_values[3].v_int) >= 0)
1583     return g_strdup_printf ("range start is not smaller than end for `%s'",
1584         G_VALUE_TYPE_NAME (value));
1585
1586   if (vals == NULL) {
1587     gst_value_init_fraction_range (value);
1588     vals = value->data[0].v_pointer;
1589   }
1590
1591   gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1592       collect_values[1].v_int);
1593   gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1594       collect_values[3].v_int);
1595
1596   return NULL;
1597 }
1598
1599 static gchar *
1600 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1601     GTypeCValue * collect_values, guint collect_flags)
1602 {
1603   gint i;
1604   gint *dest_values[4];
1605   GValue *vals = (GValue *) value->data[0].v_pointer;
1606
1607   if (G_UNLIKELY (n_collect_values != 4))
1608     return g_strdup_printf ("not enough value locations for `%s' passed",
1609         G_VALUE_TYPE_NAME (value));
1610
1611   for (i = 0; i < 4; i++) {
1612     if (G_UNLIKELY (collect_values[i].v_pointer == NULL)) {
1613       return g_strdup_printf ("value location for `%s' passed as NULL",
1614           G_VALUE_TYPE_NAME (value));
1615     }
1616     dest_values[i] = collect_values[i].v_pointer;
1617   }
1618
1619   if (G_UNLIKELY (vals == NULL)) {
1620     return g_strdup_printf ("Uninitialised `%s' passed",
1621         G_VALUE_TYPE_NAME (value));
1622   }
1623
1624   dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1625   dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1626   dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1627   dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1628   return NULL;
1629 }
1630
1631 /**
1632  * gst_value_set_fraction_range:
1633  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1634  * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1635  * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1636  *
1637  * Sets @value to the range specified by @start and @end.
1638  */
1639 void
1640 gst_value_set_fraction_range (GValue * value, const GValue * start,
1641     const GValue * end)
1642 {
1643   GValue *vals;
1644
1645   g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1646   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
1647   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
1648   g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
1649           start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
1650
1651   vals = (GValue *) value->data[0].v_pointer;
1652   if (vals == NULL) {
1653     gst_value_init_fraction_range (value);
1654     vals = value->data[0].v_pointer;
1655   }
1656   g_value_copy (start, &vals[0]);
1657   g_value_copy (end, &vals[1]);
1658 }
1659
1660 /**
1661  * gst_value_set_fraction_range_full:
1662  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1663  * @numerator_start: the numerator start of the range
1664  * @denominator_start: the denominator start of the range
1665  * @numerator_end: the numerator end of the range
1666  * @denominator_end: the denominator end of the range
1667  *
1668  * Sets @value to the range specified by @numerator_start/@denominator_start
1669  * and @numerator_end/@denominator_end.
1670  */
1671 void
1672 gst_value_set_fraction_range_full (GValue * value,
1673     gint numerator_start, gint denominator_start,
1674     gint numerator_end, gint denominator_end)
1675 {
1676   GValue start = { 0 };
1677   GValue end = { 0 };
1678
1679   g_return_if_fail (value != NULL);
1680   g_return_if_fail (denominator_start != 0);
1681   g_return_if_fail (denominator_end != 0);
1682   g_return_if_fail (gst_util_fraction_compare (numerator_start,
1683           denominator_start, numerator_end, denominator_end) < 0);
1684
1685   g_value_init (&start, GST_TYPE_FRACTION);
1686   g_value_init (&end, GST_TYPE_FRACTION);
1687
1688   gst_value_set_fraction (&start, numerator_start, denominator_start);
1689   gst_value_set_fraction (&end, numerator_end, denominator_end);
1690   gst_value_set_fraction_range (value, &start, &end);
1691
1692   /* we know the two values contain fractions without internal allocs */
1693   /* g_value_unset (&start); */
1694   /* g_value_unset (&end);   */
1695 }
1696
1697 /**
1698  * gst_value_get_fraction_range_min:
1699  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1700  *
1701  * Gets the minimum of the range specified by @value.
1702  *
1703  * Returns: the minimum of the range
1704  */
1705 const GValue *
1706 gst_value_get_fraction_range_min (const GValue * value)
1707 {
1708   GValue *vals;
1709
1710   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1711
1712   vals = (GValue *) value->data[0].v_pointer;
1713   if (vals != NULL) {
1714     return &vals[0];
1715   }
1716
1717   return NULL;
1718 }
1719
1720 /**
1721  * gst_value_get_fraction_range_max:
1722  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1723  *
1724  * Gets the maximum of the range specified by @value.
1725  *
1726  * Returns: the maximum of the range
1727  */
1728 const GValue *
1729 gst_value_get_fraction_range_max (const GValue * value)
1730 {
1731   GValue *vals;
1732
1733   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1734
1735   vals = (GValue *) value->data[0].v_pointer;
1736   if (vals != NULL) {
1737     return &vals[1];
1738   }
1739
1740   return NULL;
1741 }
1742
1743 static gchar *
1744 gst_value_serialize_fraction_range (const GValue * value)
1745 {
1746   GValue *vals = (GValue *) value->data[0].v_pointer;
1747   gchar *retval;
1748
1749   if (vals == NULL) {
1750     retval = g_strdup ("[ 0/1, 0/1 ]");
1751   } else {
1752     gchar *start, *end;
1753
1754     start = gst_value_serialize_fraction (&vals[0]);
1755     end = gst_value_serialize_fraction (&vals[1]);
1756
1757     retval = g_strdup_printf ("[ %s, %s ]", start, end);
1758     g_free (start);
1759     g_free (end);
1760   }
1761
1762   return retval;
1763 }
1764
1765 static void
1766 gst_value_transform_fraction_range_string (const GValue * src_value,
1767     GValue * dest_value)
1768 {
1769   dest_value->data[0].v_pointer =
1770       gst_value_serialize_fraction_range (src_value);
1771 }
1772
1773 static gint
1774 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1775 {
1776   GValue *vals1, *vals2;
1777   GstValueCompareFunc compare;
1778
1779   if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1780     return GST_VALUE_EQUAL;     /* Only possible if both are NULL */
1781
1782   if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1783     return GST_VALUE_UNORDERED;
1784
1785   vals1 = (GValue *) value1->data[0].v_pointer;
1786   vals2 = (GValue *) value2->data[0].v_pointer;
1787   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1788     if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1789         GST_VALUE_EQUAL &&
1790         gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1791         GST_VALUE_EQUAL)
1792       return GST_VALUE_EQUAL;
1793   }
1794   return GST_VALUE_UNORDERED;
1795 }
1796
1797 static gboolean
1798 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1799 {
1800   g_warning ("unimplemented");
1801   return FALSE;
1802 }
1803
1804 /***********
1805  * GstCaps *
1806  ***********/
1807
1808 /**
1809  * gst_value_set_caps:
1810  * @value: a GValue initialized to GST_TYPE_CAPS
1811  * @caps: (transfer none): the caps to set the value to
1812  *
1813  * Sets the contents of @value to @caps. A reference to the
1814  * provided @caps will be taken by the @value.
1815  */
1816 void
1817 gst_value_set_caps (GValue * value, const GstCaps * caps)
1818 {
1819   g_return_if_fail (G_IS_VALUE (value));
1820   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1821   g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
1822
1823   g_value_set_boxed (value, caps);
1824 }
1825
1826 /**
1827  * gst_value_get_caps:
1828  * @value: a GValue initialized to GST_TYPE_CAPS
1829  *
1830  * Gets the contents of @value. The reference count of the returned
1831  * #GstCaps will not be modified, therefore the caller must take one
1832  * before getting rid of the @value.
1833  *
1834  * Returns: (transfer none): the contents of @value
1835  */
1836 const GstCaps *
1837 gst_value_get_caps (const GValue * value)
1838 {
1839   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1840   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1841
1842   return (GstCaps *) g_value_get_boxed (value);
1843 }
1844
1845 static gint
1846 gst_value_compare_caps (const GValue * value1, const GValue * value2)
1847 {
1848   GstCaps *caps1 = GST_CAPS (gst_value_get_caps (value1));
1849   GstCaps *caps2 = GST_CAPS (gst_value_get_caps (value2));
1850
1851   if (gst_caps_is_equal (caps1, caps2))
1852     return GST_VALUE_EQUAL;
1853   return GST_VALUE_UNORDERED;
1854 }
1855
1856 static gchar *
1857 gst_value_serialize_caps (const GValue * value)
1858 {
1859   GstCaps *caps = g_value_get_boxed (value);
1860   return gst_string_take_and_wrap (gst_caps_to_string (caps));
1861 }
1862
1863 static gboolean
1864 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1865 {
1866   GstCaps *caps;
1867
1868   if (*s != '"') {
1869     caps = gst_caps_from_string (s);
1870   } else {
1871     gchar *str = gst_string_unwrap (s);
1872
1873     if (G_UNLIKELY (!str))
1874       return FALSE;
1875
1876     caps = gst_caps_from_string (str);
1877     g_free (str);
1878   }
1879
1880   if (caps) {
1881     g_value_take_boxed (dest, caps);
1882     return TRUE;
1883   }
1884   return FALSE;
1885 }
1886
1887 /**************
1888  * GstSegment *
1889  **************/
1890
1891 static gchar *
1892 gst_value_serialize_segment_internal (const GValue * value, gboolean escape)
1893 {
1894   GstSegment *seg = g_value_get_boxed (value);
1895   gchar *t, *res;
1896   GstStructure *s;
1897
1898   s = gst_structure_new ("GstSegment",
1899       "flags", GST_TYPE_SEGMENT_FLAGS, seg->flags,
1900       "rate", G_TYPE_DOUBLE, seg->rate,
1901       "applied-rate", G_TYPE_DOUBLE, seg->applied_rate,
1902       "format", GST_TYPE_FORMAT, seg->format,
1903       "base", G_TYPE_UINT64, seg->base,
1904       "offset", G_TYPE_UINT64, seg->offset,
1905       "start", G_TYPE_UINT64, seg->start,
1906       "stop", G_TYPE_UINT64, seg->stop,
1907       "time", G_TYPE_UINT64, seg->time,
1908       "position", G_TYPE_UINT64, seg->position,
1909       "duration", G_TYPE_UINT64, seg->duration, NULL);
1910   t = gst_structure_to_string (s);
1911   if (escape) {
1912     res = g_strdup_printf ("\"%s\"", t);
1913     g_free (t);
1914   } else {
1915     res = t;
1916   }
1917   gst_structure_free (s);
1918
1919   return res;
1920 }
1921
1922 static gchar *
1923 gst_value_serialize_segment (const GValue * value)
1924 {
1925   return gst_value_serialize_segment_internal (value, TRUE);
1926 }
1927
1928 static gboolean
1929 gst_value_deserialize_segment (GValue * dest, const gchar * s)
1930 {
1931   GstStructure *str;
1932   GstSegment seg;
1933   gboolean res;
1934
1935   str = gst_structure_from_string (s, NULL);
1936   if (str == NULL)
1937     return FALSE;
1938
1939   res = gst_structure_get (str,
1940       "flags", GST_TYPE_SEGMENT_FLAGS, &seg.flags,
1941       "rate", G_TYPE_DOUBLE, &seg.rate,
1942       "applied-rate", G_TYPE_DOUBLE, &seg.applied_rate,
1943       "format", GST_TYPE_FORMAT, &seg.format,
1944       "base", G_TYPE_UINT64, &seg.base,
1945       "offset", G_TYPE_UINT64, &seg.offset,
1946       "start", G_TYPE_UINT64, &seg.start,
1947       "stop", G_TYPE_UINT64, &seg.stop,
1948       "time", G_TYPE_UINT64, &seg.time,
1949       "position", G_TYPE_UINT64, &seg.position,
1950       "duration", G_TYPE_UINT64, &seg.duration, NULL);
1951   gst_structure_free (str);
1952
1953   if (res)
1954     g_value_set_boxed (dest, &seg);
1955
1956   return res;
1957 }
1958
1959 /****************
1960  * GstStructure *
1961  ****************/
1962
1963 /**
1964  * gst_value_set_structure:
1965  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1966  * @structure: the structure to set the value to
1967  *
1968  * Sets the contents of @value to @structure.  The actual
1969  */
1970 void
1971 gst_value_set_structure (GValue * value, const GstStructure * structure)
1972 {
1973   g_return_if_fail (G_IS_VALUE (value));
1974   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1975   g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
1976
1977   g_value_set_boxed (value, structure);
1978 }
1979
1980 /**
1981  * gst_value_get_structure:
1982  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1983  *
1984  * Gets the contents of @value.
1985  *
1986  * Returns: (transfer none): the contents of @value
1987  */
1988 const GstStructure *
1989 gst_value_get_structure (const GValue * value)
1990 {
1991   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1992   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1993
1994   return (GstStructure *) g_value_get_boxed (value);
1995 }
1996
1997 static gchar *
1998 gst_value_serialize_structure (const GValue * value)
1999 {
2000   GstStructure *structure = g_value_get_boxed (value);
2001
2002   return gst_string_take_and_wrap (gst_structure_to_string (structure));
2003 }
2004
2005 static gboolean
2006 gst_value_deserialize_structure (GValue * dest, const gchar * s)
2007 {
2008   GstStructure *structure;
2009
2010   if (*s != '"') {
2011     structure = gst_structure_from_string (s, NULL);
2012   } else {
2013     gchar *str = gst_string_unwrap (s);
2014
2015     if (G_UNLIKELY (!str))
2016       return FALSE;
2017
2018     structure = gst_structure_from_string (str, NULL);
2019     g_free (str);
2020   }
2021
2022   if (G_LIKELY (structure)) {
2023     g_value_take_boxed (dest, structure);
2024     return TRUE;
2025   }
2026   return FALSE;
2027 }
2028
2029 /*******************
2030  * GstCapsFeatures *
2031  *******************/
2032
2033 /**
2034  * gst_value_set_caps_features:
2035  * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
2036  * @features: the features to set the value to
2037  *
2038  * Sets the contents of @value to @features.
2039  */
2040 void
2041 gst_value_set_caps_features (GValue * value, const GstCapsFeatures * features)
2042 {
2043   g_return_if_fail (G_IS_VALUE (value));
2044   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES);
2045   g_return_if_fail (features == NULL || GST_IS_CAPS_FEATURES (features));
2046
2047   g_value_set_boxed (value, features);
2048 }
2049
2050 /**
2051  * gst_value_get_caps_features:
2052  * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
2053  *
2054  * Gets the contents of @value.
2055  *
2056  * Returns: (transfer none): the contents of @value
2057  */
2058 const GstCapsFeatures *
2059 gst_value_get_caps_features (const GValue * value)
2060 {
2061   g_return_val_if_fail (G_IS_VALUE (value), NULL);
2062   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES, NULL);
2063
2064   return (GstCapsFeatures *) g_value_get_boxed (value);
2065 }
2066
2067 static gchar *
2068 gst_value_serialize_caps_features (const GValue * value)
2069 {
2070   GstCapsFeatures *features = g_value_get_boxed (value);
2071
2072   return gst_string_take_and_wrap (gst_caps_features_to_string (features));
2073 }
2074
2075 static gboolean
2076 gst_value_deserialize_caps_features (GValue * dest, const gchar * s)
2077 {
2078   GstCapsFeatures *features;
2079
2080   if (*s != '"') {
2081     features = gst_caps_features_from_string (s);
2082   } else {
2083     gchar *str = gst_string_unwrap (s);
2084
2085     if (G_UNLIKELY (!str))
2086       return FALSE;
2087
2088     features = gst_caps_features_from_string (str);
2089     g_free (str);
2090   }
2091
2092   if (G_LIKELY (features)) {
2093     g_value_take_boxed (dest, features);
2094     return TRUE;
2095   }
2096   return FALSE;
2097 }
2098
2099 /**************
2100  * GstTagList *
2101  **************/
2102
2103 static gboolean
2104 gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
2105 {
2106   GstTagList *taglist;
2107
2108   if (*s != '"') {
2109     taglist = gst_tag_list_new_from_string (s);
2110   } else {
2111     gchar *str = gst_string_unwrap (s);
2112
2113     if (G_UNLIKELY (!str))
2114       return FALSE;
2115
2116     taglist = gst_tag_list_new_from_string (str);
2117     g_free (str);
2118   }
2119
2120   if (G_LIKELY (taglist != NULL)) {
2121     g_value_take_boxed (dest, taglist);
2122     return TRUE;
2123   }
2124   return FALSE;
2125 }
2126
2127 static gchar *
2128 gst_value_serialize_tag_list (const GValue * value)
2129 {
2130   GstTagList *taglist = g_value_get_boxed (value);
2131
2132   return gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
2133 }
2134
2135
2136 /*************
2137  * GstBuffer *
2138  *************/
2139
2140 static gint
2141 compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
2142 {
2143   gsize size1, size2;
2144   GstMapInfo info1, info2;
2145   gint result, mret;
2146
2147   if (buf1 == buf2)
2148     return GST_VALUE_EQUAL;
2149
2150   size1 = gst_buffer_get_size (buf1);
2151   size2 = gst_buffer_get_size (buf2);
2152
2153   if (size1 != size2)
2154     return GST_VALUE_UNORDERED;
2155
2156   if (size1 == 0)
2157     return GST_VALUE_EQUAL;
2158
2159   if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
2160     return GST_VALUE_UNORDERED;
2161
2162   if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
2163     gst_buffer_unmap (buf1, &info1);
2164     return GST_VALUE_UNORDERED;
2165   }
2166
2167   mret = memcmp (info1.data, info2.data, info1.size);
2168   if (mret == 0)
2169     result = GST_VALUE_EQUAL;
2170   else if (mret < 0)
2171     result = GST_VALUE_LESS_THAN;
2172   else
2173     result = GST_VALUE_GREATER_THAN;
2174
2175   gst_buffer_unmap (buf1, &info1);
2176   gst_buffer_unmap (buf2, &info2);
2177
2178   return result;
2179 }
2180
2181 static gint
2182 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
2183 {
2184   GstBuffer *buf1 = gst_value_get_buffer (value1);
2185   GstBuffer *buf2 = gst_value_get_buffer (value2);
2186
2187   return compare_buffer (buf1, buf2);
2188 }
2189
2190 static gchar *
2191 gst_value_serialize_buffer (const GValue * value)
2192 {
2193   GstMapInfo info;
2194   guint8 *data;
2195   gint i;
2196   gchar *string;
2197   GstBuffer *buffer;
2198
2199   buffer = gst_value_get_buffer (value);
2200   if (buffer == NULL)
2201     return NULL;
2202
2203   if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
2204     return NULL;
2205
2206   data = info.data;
2207
2208   string = g_malloc (info.size * 2 + 1);
2209   for (i = 0; i < info.size; i++) {
2210     sprintf (string + i * 2, "%02x", data[i]);
2211   }
2212   string[info.size * 2] = 0;
2213
2214   gst_buffer_unmap (buffer, &info);
2215
2216   return string;
2217 }
2218
2219 static gboolean
2220 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
2221 {
2222   GstBuffer *buffer;
2223   gint len;
2224   gchar ts[3];
2225   GstMapInfo info;
2226   guint8 *data;
2227   gint i;
2228
2229   len = strlen (s);
2230   if (len & 1)
2231     goto wrong_length;
2232
2233   buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
2234   if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
2235     goto map_failed;
2236   data = info.data;
2237
2238   for (i = 0; i < len / 2; i++) {
2239     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
2240       goto wrong_char;
2241
2242     ts[0] = s[i * 2 + 0];
2243     ts[1] = s[i * 2 + 1];
2244     ts[2] = 0;
2245
2246     data[i] = (guint8) strtoul (ts, NULL, 16);
2247   }
2248   gst_buffer_unmap (buffer, &info);
2249
2250   gst_value_take_buffer (dest, buffer);
2251
2252   return TRUE;
2253
2254   /* ERRORS */
2255 wrong_length:
2256   {
2257     return FALSE;
2258   }
2259 map_failed:
2260   {
2261     return FALSE;
2262   }
2263 wrong_char:
2264   {
2265     gst_buffer_unref (buffer);
2266     gst_buffer_unmap (buffer, &info);
2267     return FALSE;
2268   }
2269 }
2270
2271 /*************
2272  * GstSample *
2273  *************/
2274
2275 /* This function is mostly used for comparing image/buffer tags in taglists */
2276 static gint
2277 gst_value_compare_sample (const GValue * value1, const GValue * value2)
2278 {
2279   GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
2280   GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
2281
2282   /* FIXME: should we take into account anything else such as caps? */
2283   return compare_buffer (buf1, buf2);
2284 }
2285
2286 static gchar *
2287 gst_value_serialize_sample (const GValue * value)
2288 {
2289   const GstStructure *info_structure;
2290   GstSegment *segment;
2291   GstBuffer *buffer;
2292   GstCaps *caps;
2293   GstSample *sample;
2294   GValue val = { 0, };
2295   gchar *info_str, *caps_str, *tmp;
2296   gchar *buf_str, *seg_str, *s;
2297
2298   sample = g_value_get_boxed (value);
2299
2300   buffer = gst_sample_get_buffer (sample);
2301   if (buffer) {
2302     g_value_init (&val, GST_TYPE_BUFFER);
2303     g_value_set_boxed (&val, buffer);
2304     buf_str = gst_value_serialize_buffer (&val);
2305     g_value_unset (&val);
2306   } else {
2307     buf_str = g_strdup ("None");
2308   }
2309
2310   caps = gst_sample_get_caps (sample);
2311   if (caps) {
2312     tmp = gst_caps_to_string (caps);
2313     caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2314     g_strdelimit (caps_str, "=", '_');
2315     g_free (tmp);
2316   } else {
2317     caps_str = g_strdup ("None");
2318   }
2319
2320   segment = gst_sample_get_segment (sample);
2321   if (segment) {
2322     g_value_init (&val, GST_TYPE_SEGMENT);
2323     g_value_set_boxed (&val, segment);
2324     tmp = gst_value_serialize_segment_internal (&val, FALSE);
2325     seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2326     g_strdelimit (seg_str, "=", '_');
2327     g_free (tmp);
2328     g_value_unset (&val);
2329   } else {
2330     seg_str = g_strdup ("None");
2331   }
2332
2333   info_structure = gst_sample_get_info (sample);
2334   if (info_structure) {
2335     tmp = gst_structure_to_string (info_structure);
2336     info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2337     g_strdelimit (info_str, "=", '_');
2338     g_free (tmp);
2339   } else {
2340     info_str = g_strdup ("None");
2341   }
2342
2343   s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
2344   g_free (buf_str);
2345   g_free (caps_str);
2346   g_free (seg_str);
2347   g_free (info_str);
2348
2349   return s;
2350 }
2351
2352 static gboolean
2353 gst_value_deserialize_sample (GValue * dest, const gchar * s)
2354 {
2355   GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
2356   GstStructure *info;
2357   GstSample *sample;
2358   GstCaps *caps;
2359   gboolean ret = FALSE;
2360   gchar **fields;
2361   gsize outlen;
2362   gint len;
2363
2364   GST_TRACE ("deserialize '%s'", s);
2365
2366   fields = g_strsplit (s, ":", -1);
2367   len = g_strv_length (fields);
2368   if (len != 4)
2369     goto wrong_length;
2370
2371   g_value_init (&bval, GST_TYPE_BUFFER);
2372   g_value_init (&sval, GST_TYPE_SEGMENT);
2373
2374   if (!gst_value_deserialize_buffer (&bval, fields[0]))
2375     goto fail;
2376
2377   if (strcmp (fields[1], "None") != 0) {
2378     g_strdelimit (fields[1], "_", '=');
2379     g_base64_decode_inplace (fields[1], &outlen);
2380     GST_TRACE ("caps    : %s", fields[1]);
2381     caps = gst_caps_from_string (fields[1]);
2382     if (caps == NULL)
2383       goto fail;
2384   } else {
2385     caps = NULL;
2386   }
2387
2388   if (strcmp (fields[2], "None") != 0) {
2389     g_strdelimit (fields[2], "_", '=');
2390     g_base64_decode_inplace (fields[2], &outlen);
2391     GST_TRACE ("segment : %s", fields[2]);
2392     if (!gst_value_deserialize_segment (&sval, fields[2]))
2393       goto fail;
2394   }
2395
2396   if (strcmp (fields[3], "None") != 0) {
2397     g_strdelimit (fields[3], "_", '=');
2398     g_base64_decode_inplace (fields[3], &outlen);
2399     GST_TRACE ("info    : %s", fields[3]);
2400     info = gst_structure_from_string (fields[3], NULL);
2401     if (info == NULL)
2402       goto fail;
2403   } else {
2404     info = NULL;
2405   }
2406
2407   sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
2408       g_value_get_boxed (&sval), info);
2409
2410   g_value_take_boxed (dest, sample);
2411
2412   if (caps)
2413     gst_caps_unref (caps);
2414
2415   ret = TRUE;
2416
2417 fail:
2418
2419   g_value_unset (&bval);
2420   g_value_unset (&sval);
2421
2422 wrong_length:
2423
2424   g_strfreev (fields);
2425
2426   return ret;
2427 }
2428
2429 /***********
2430  * boolean *
2431  ***********/
2432
2433 static gint
2434 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
2435 {
2436   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
2437     return GST_VALUE_EQUAL;
2438   return GST_VALUE_UNORDERED;
2439 }
2440
2441 static gchar *
2442 gst_value_serialize_boolean (const GValue * value)
2443 {
2444   if (value->data[0].v_int) {
2445     return g_strdup ("true");
2446   }
2447   return g_strdup ("false");
2448 }
2449
2450 static gboolean
2451 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
2452 {
2453   gboolean ret = FALSE;
2454
2455   if (g_ascii_strcasecmp (s, "true") == 0 ||
2456       g_ascii_strcasecmp (s, "yes") == 0 ||
2457       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
2458     g_value_set_boolean (dest, TRUE);
2459     ret = TRUE;
2460   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
2461       g_ascii_strcasecmp (s, "no") == 0 ||
2462       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
2463     g_value_set_boolean (dest, FALSE);
2464     ret = TRUE;
2465   }
2466
2467   return ret;
2468 }
2469
2470 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
2471 static gint                                                             \
2472 gst_value_compare_ ## _type                                             \
2473 (const GValue * value1, const GValue * value2)                          \
2474 {                                                                       \
2475   g ## _type val1 = g_value_get_ ## _type (value1);                     \
2476   g ## _type val2 = g_value_get_ ## _type (value2);                     \
2477   if (val1 > val2)                                                      \
2478     return GST_VALUE_GREATER_THAN;                                      \
2479   if (val1 < val2)                                                      \
2480     return GST_VALUE_LESS_THAN;                                         \
2481   return GST_VALUE_EQUAL;                                               \
2482 }                                                                       \
2483                                                                         \
2484 static gchar *                                                          \
2485 gst_value_serialize_ ## _type (const GValue * value)                    \
2486 {                                                                       \
2487   GValue val = { 0, };                                                  \
2488   g_value_init (&val, G_TYPE_STRING);                                   \
2489   if (!g_value_transform (value, &val))                                 \
2490     g_assert_not_reached ();                                            \
2491   /* NO_COPY_MADNESS!!! */                                              \
2492   return (char *) g_value_get_string (&val);                            \
2493 }
2494
2495 /* deserialize the given s into to as a gint64.
2496  * check if the result is actually storeable in the given size number of
2497  * bytes.
2498  */
2499 static gboolean
2500 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
2501     gint64 min, gint64 max, gint size)
2502 {
2503   gboolean ret = FALSE;
2504   gchar *end;
2505   guint64 mask = ~0;
2506
2507   errno = 0;
2508   *to = g_ascii_strtoull (s, &end, 0);
2509   /* a range error is a definitive no-no */
2510   if (errno == ERANGE) {
2511     return FALSE;
2512   }
2513
2514   if (*end == 0) {
2515     ret = TRUE;
2516   } else {
2517     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
2518       *to = G_LITTLE_ENDIAN;
2519       ret = TRUE;
2520     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
2521       *to = G_BIG_ENDIAN;
2522       ret = TRUE;
2523     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
2524       *to = G_BYTE_ORDER;
2525       ret = TRUE;
2526     } else if (g_ascii_strcasecmp (s, "min") == 0) {
2527       *to = min;
2528       ret = TRUE;
2529     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2530       *to = max;
2531       ret = TRUE;
2532     }
2533   }
2534   if (ret) {
2535     /* by definition, a gint64 fits into a gint64; so ignore those */
2536     if (size != sizeof (mask)) {
2537       if (*to >= 0) {
2538         /* for positive numbers, we create a mask of 1's outside of the range
2539          * and 0's inside the range.  An and will thus keep only 1 bits
2540          * outside of the range */
2541         mask <<= (size * 8);
2542         if ((mask & *to) != 0) {
2543           ret = FALSE;
2544         }
2545       } else {
2546         /* for negative numbers, we do a 2's complement version */
2547         mask <<= ((size * 8) - 1);
2548         if ((mask & *to) != mask) {
2549           ret = FALSE;
2550         }
2551       }
2552     }
2553   }
2554   return ret;
2555 }
2556
2557 #define CREATE_SERIALIZATION(_type,_macro)                              \
2558 CREATE_SERIALIZATION_START(_type,_macro)                                \
2559                                                                         \
2560 static gboolean                                                         \
2561 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2562 {                                                                       \
2563   gint64 x;                                                             \
2564                                                                         \
2565   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
2566       G_MAX ## _macro, sizeof (g ## _type))) {                          \
2567     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
2568     return TRUE;                                                        \
2569   } else {                                                              \
2570     return FALSE;                                                       \
2571   }                                                                     \
2572 }
2573
2574 #define CREATE_USERIALIZATION(_type,_macro)                             \
2575 CREATE_SERIALIZATION_START(_type,_macro)                                \
2576                                                                         \
2577 static gboolean                                                         \
2578 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2579 {                                                                       \
2580   gint64 x;                                                             \
2581   gchar *end;                                                           \
2582   gboolean ret = FALSE;                                                 \
2583                                                                         \
2584   errno = 0;                                                            \
2585   x = g_ascii_strtoull (s, &end, 0);                                    \
2586   /* a range error is a definitive no-no */                             \
2587   if (errno == ERANGE) {                                                \
2588     return FALSE;                                                       \
2589   }                                                                     \
2590   /* the cast ensures the range check later on makes sense */           \
2591   x = (g ## _type) x;                                                   \
2592   if (*end == 0) {                                                      \
2593     ret = TRUE;                                                         \
2594   } else {                                                              \
2595     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
2596       x = G_LITTLE_ENDIAN;                                              \
2597       ret = TRUE;                                                       \
2598     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
2599       x = G_BIG_ENDIAN;                                                 \
2600       ret = TRUE;                                                       \
2601     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
2602       x = G_BYTE_ORDER;                                                 \
2603       ret = TRUE;                                                       \
2604     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
2605       x = 0;                                                            \
2606       ret = TRUE;                                                       \
2607     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
2608       x = G_MAX ## _macro;                                              \
2609       ret = TRUE;                                                       \
2610     }                                                                   \
2611   }                                                                     \
2612   if (ret) {                                                            \
2613     if (x > G_MAX ## _macro) {                                          \
2614       ret = FALSE;                                                      \
2615     } else {                                                            \
2616       g_value_set_ ## _type (dest, x);                                  \
2617     }                                                                   \
2618   }                                                                     \
2619   return ret;                                                           \
2620 }
2621
2622 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
2623 G_STMT_START {                                                          \
2624   static const GstValueTable gst_value = {                              \
2625     _gtype,                                                             \
2626     gst_value_compare_ ## _type,                                        \
2627     gst_value_serialize_ ## _type,                                      \
2628     gst_value_deserialize_ ## _type,                                    \
2629   };                                                                    \
2630                                                                         \
2631   gst_value_register (&gst_value);                                      \
2632 } G_STMT_END
2633
2634 CREATE_SERIALIZATION (int, INT);
2635 CREATE_SERIALIZATION (int64, INT64);
2636 CREATE_SERIALIZATION (long, LONG);
2637
2638 CREATE_USERIALIZATION (uint, UINT);
2639 CREATE_USERIALIZATION (uint64, UINT64);
2640 CREATE_USERIALIZATION (ulong, ULONG);
2641
2642 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2643 #ifndef G_MAXUCHAR
2644 #define G_MAXUCHAR 255
2645 #endif
2646 CREATE_USERIALIZATION (uchar, UCHAR);
2647
2648 /**********
2649  * double *
2650  **********/
2651 static gint
2652 gst_value_compare_double (const GValue * value1, const GValue * value2)
2653 {
2654   if (value1->data[0].v_double > value2->data[0].v_double)
2655     return GST_VALUE_GREATER_THAN;
2656   if (value1->data[0].v_double < value2->data[0].v_double)
2657     return GST_VALUE_LESS_THAN;
2658   if (value1->data[0].v_double == value2->data[0].v_double)
2659     return GST_VALUE_EQUAL;
2660   return GST_VALUE_UNORDERED;
2661 }
2662
2663 static gchar *
2664 gst_value_serialize_double (const GValue * value)
2665 {
2666   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2667
2668   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2669   return g_strdup (d);
2670 }
2671
2672 static gboolean
2673 gst_value_deserialize_double (GValue * dest, const gchar * s)
2674 {
2675   gdouble x;
2676   gboolean ret = FALSE;
2677   gchar *end;
2678
2679   x = g_ascii_strtod (s, &end);
2680   if (*end == 0) {
2681     ret = TRUE;
2682   } else {
2683     if (g_ascii_strcasecmp (s, "min") == 0) {
2684       x = -G_MAXDOUBLE;
2685       ret = TRUE;
2686     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2687       x = G_MAXDOUBLE;
2688       ret = TRUE;
2689     }
2690   }
2691   if (ret) {
2692     g_value_set_double (dest, x);
2693   }
2694   return ret;
2695 }
2696
2697 /*********
2698  * float *
2699  *********/
2700
2701 static gint
2702 gst_value_compare_float (const GValue * value1, const GValue * value2)
2703 {
2704   if (value1->data[0].v_float > value2->data[0].v_float)
2705     return GST_VALUE_GREATER_THAN;
2706   if (value1->data[0].v_float < value2->data[0].v_float)
2707     return GST_VALUE_LESS_THAN;
2708   if (value1->data[0].v_float == value2->data[0].v_float)
2709     return GST_VALUE_EQUAL;
2710   return GST_VALUE_UNORDERED;
2711 }
2712
2713 static gchar *
2714 gst_value_serialize_float (const GValue * value)
2715 {
2716   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2717
2718   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2719   return g_strdup (d);
2720 }
2721
2722 static gboolean
2723 gst_value_deserialize_float (GValue * dest, const gchar * s)
2724 {
2725   gdouble x;
2726   gboolean ret = FALSE;
2727   gchar *end;
2728
2729   x = g_ascii_strtod (s, &end);
2730   if (*end == 0) {
2731     ret = TRUE;
2732   } else {
2733     if (g_ascii_strcasecmp (s, "min") == 0) {
2734       x = -G_MAXFLOAT;
2735       ret = TRUE;
2736     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2737       x = G_MAXFLOAT;
2738       ret = TRUE;
2739     }
2740   }
2741   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2742     ret = FALSE;
2743   if (ret) {
2744     g_value_set_float (dest, (float) x);
2745   }
2746   return ret;
2747 }
2748
2749 /**********
2750  * string *
2751  **********/
2752
2753 static gint
2754 gst_value_compare_string (const GValue * value1, const GValue * value2)
2755 {
2756   if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2757     /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2758     if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2759       return GST_VALUE_UNORDERED;
2760   } else {
2761     gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2762
2763     if (x < 0)
2764       return GST_VALUE_LESS_THAN;
2765     if (x > 0)
2766       return GST_VALUE_GREATER_THAN;
2767   }
2768
2769   return GST_VALUE_EQUAL;
2770 }
2771
2772 static gint
2773 gst_string_measure_wrapping (const gchar * s)
2774 {
2775   gint len;
2776   gboolean wrap = FALSE;
2777
2778   if (G_UNLIKELY (s == NULL))
2779     return -1;
2780
2781   /* Special case: the actual string NULL needs wrapping */
2782   if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2783     return 4;
2784
2785   len = 0;
2786   while (*s) {
2787     if (GST_ASCII_IS_STRING (*s)) {
2788       len++;
2789     } else if (*s < 0x20 || *s >= 0x7f) {
2790       wrap = TRUE;
2791       len += 4;
2792     } else {
2793       wrap = TRUE;
2794       len += 2;
2795     }
2796     s++;
2797   }
2798
2799   /* Wrap the string if we found something that needs
2800    * wrapping, or the empty string (len == 0) */
2801   return (wrap || len == 0) ? len : -1;
2802 }
2803
2804 static gchar *
2805 gst_string_wrap_inner (const gchar * s, gint len)
2806 {
2807   gchar *d, *e;
2808
2809   e = d = g_malloc (len + 3);
2810
2811   *e++ = '\"';
2812   while (*s) {
2813     if (GST_ASCII_IS_STRING (*s)) {
2814       *e++ = *s++;
2815     } else if (*s < 0x20 || *s >= 0x7f) {
2816       *e++ = '\\';
2817       *e++ = '0' + ((*(guchar *) s) >> 6);
2818       *e++ = '0' + (((*s) >> 3) & 0x7);
2819       *e++ = '0' + ((*s++) & 0x7);
2820     } else {
2821       *e++ = '\\';
2822       *e++ = *s++;
2823     }
2824   }
2825   *e++ = '\"';
2826   *e = 0;
2827
2828   g_assert (e - d <= len + 3);
2829   return d;
2830 }
2831
2832 /* Do string wrapping/escaping */
2833 static gchar *
2834 gst_string_wrap (const gchar * s)
2835 {
2836   gint len = gst_string_measure_wrapping (s);
2837
2838   if (G_LIKELY (len < 0))
2839     return g_strdup (s);
2840
2841   return gst_string_wrap_inner (s, len);
2842 }
2843
2844 /* Same as above, but take ownership of the string */
2845 static gchar *
2846 gst_string_take_and_wrap (gchar * s)
2847 {
2848   gchar *out;
2849   gint len = gst_string_measure_wrapping (s);
2850
2851   if (G_LIKELY (len < 0))
2852     return s;
2853
2854   out = gst_string_wrap_inner (s, len);
2855   g_free (s);
2856
2857   return out;
2858 }
2859
2860 /*
2861  * This function takes a string delimited with double quotes (")
2862  * and unescapes any \xxx octal numbers.
2863  *
2864  * If sequences of \y are found where y is not in the range of
2865  * 0->3, y is copied unescaped.
2866  *
2867  * If \xyy is found where x is an octal number but y is not, an
2868  * error is encountered and %NULL is returned.
2869  *
2870  * the input string must be \0 terminated.
2871  */
2872 static gchar *
2873 gst_string_unwrap (const gchar * s)
2874 {
2875   gchar *ret;
2876   gchar *read, *write;
2877
2878   /* NULL string returns NULL */
2879   if (s == NULL)
2880     return NULL;
2881
2882   /* strings not starting with " are invalid */
2883   if (*s != '"')
2884     return NULL;
2885
2886   /* make copy of original string to hold the result. This
2887    * string will always be smaller than the original */
2888   ret = g_strdup (s);
2889   read = ret;
2890   write = ret;
2891
2892   /* need to move to the next position as we parsed the " */
2893   read++;
2894
2895   while (*read) {
2896     if (GST_ASCII_IS_STRING (*read)) {
2897       /* normal chars are just copied */
2898       *write++ = *read++;
2899     } else if (*read == '"') {
2900       /* quote marks end of string */
2901       break;
2902     } else if (*read == '\\') {
2903       /* got an escape char, move to next position to read a tripplet
2904        * of octal numbers */
2905       read++;
2906       /* is the next char a possible first octal number? */
2907       if (*read >= '0' && *read <= '3') {
2908         /* parse other 2 numbers, if one of them is not in the range of
2909          * an octal number, we error. We also catch the case where a zero
2910          * byte is found here. */
2911         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2912           goto beach;
2913
2914         /* now convert the octal number to a byte again. */
2915         *write++ = ((read[0] - '0') << 6) +
2916             ((read[1] - '0') << 3) + (read[2] - '0');
2917
2918         read += 3;
2919       } else {
2920         /* if we run into a \0 here, we definitely won't get a quote later */
2921         if (*read == 0)
2922           goto beach;
2923
2924         /* else copy \X sequence */
2925         *write++ = *read++;
2926       }
2927     } else {
2928       /* weird character, error */
2929       goto beach;
2930     }
2931   }
2932   /* if the string is not ending in " and zero terminated, we error */
2933   if (*read != '"' || read[1] != '\0')
2934     goto beach;
2935
2936   /* null terminate result string and return */
2937   *write = '\0';
2938   return ret;
2939
2940 beach:
2941   g_free (ret);
2942   return NULL;
2943 }
2944
2945 static gchar *
2946 gst_value_serialize_string (const GValue * value)
2947 {
2948   return gst_string_wrap (value->data[0].v_pointer);
2949 }
2950
2951 static gboolean
2952 gst_value_deserialize_string (GValue * dest, const gchar * s)
2953 {
2954   if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2955     g_value_set_string (dest, NULL);
2956     return TRUE;
2957   } else if (G_LIKELY (*s != '"')) {
2958     if (!g_utf8_validate (s, -1, NULL))
2959       return FALSE;
2960     g_value_set_string (dest, s);
2961     return TRUE;
2962   } else {
2963     gchar *str = gst_string_unwrap (s);
2964     if (G_UNLIKELY (!str))
2965       return FALSE;
2966     g_value_take_string (dest, str);
2967   }
2968
2969   return TRUE;
2970 }
2971
2972 /********
2973  * enum *
2974  ********/
2975
2976 static gint
2977 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2978 {
2979   GEnumValue *en1, *en2;
2980   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2981   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2982
2983   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2984   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2985   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2986   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2987   g_type_class_unref (klass1);
2988   g_type_class_unref (klass2);
2989   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2990   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2991   if (en1->value < en2->value)
2992     return GST_VALUE_LESS_THAN;
2993   if (en1->value > en2->value)
2994     return GST_VALUE_GREATER_THAN;
2995
2996   return GST_VALUE_EQUAL;
2997 }
2998
2999 static gchar *
3000 gst_value_serialize_enum (const GValue * value)
3001 {
3002   GEnumValue *en;
3003   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
3004
3005   g_return_val_if_fail (klass, NULL);
3006   en = g_enum_get_value (klass, g_value_get_enum (value));
3007   g_type_class_unref (klass);
3008
3009   /* might be one of the custom formats registered later */
3010   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
3011     const GstFormatDefinition *format_def;
3012
3013     format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
3014     g_return_val_if_fail (format_def != NULL, NULL);
3015     return g_strdup (format_def->description);
3016   }
3017
3018   g_return_val_if_fail (en, NULL);
3019   return g_strdup (en->value_name);
3020 }
3021
3022 static gint
3023 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
3024     const gchar * s)
3025 {
3026   const GstFormatDefinition *format_def =
3027       g_value_get_pointer (format_def_value);
3028
3029   if (g_ascii_strcasecmp (s, format_def->nick) == 0)
3030     return 0;
3031
3032   return g_ascii_strcasecmp (s, format_def->description);
3033 }
3034
3035 static gboolean
3036 gst_value_deserialize_enum (GValue * dest, const gchar * s)
3037 {
3038   GEnumValue *en;
3039   gchar *endptr = NULL;
3040   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3041
3042   g_return_val_if_fail (klass, FALSE);
3043   if (!(en = g_enum_get_value_by_name (klass, s))) {
3044     if (!(en = g_enum_get_value_by_nick (klass, s))) {
3045       gint i = strtol (s, &endptr, 0);
3046
3047       if (endptr && *endptr == '\0') {
3048         en = g_enum_get_value (klass, i);
3049       }
3050     }
3051   }
3052   g_type_class_unref (klass);
3053
3054   /* might be one of the custom formats registered later */
3055   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
3056     GValue res = { 0, };
3057     const GstFormatDefinition *format_def;
3058     GstIterator *iter;
3059     gboolean found;
3060
3061     iter = gst_format_iterate_definitions ();
3062
3063     found = gst_iterator_find_custom (iter,
3064         (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
3065
3066     if (found) {
3067       format_def = g_value_get_pointer (&res);
3068       g_return_val_if_fail (format_def != NULL, FALSE);
3069       g_value_set_enum (dest, (gint) format_def->value);
3070       g_value_unset (&res);
3071     }
3072     gst_iterator_free (iter);
3073     return found;
3074   }
3075
3076   /* enum name/nick not found */
3077   if (en == NULL)
3078     return FALSE;
3079
3080   g_value_set_enum (dest, en->value);
3081   return TRUE;
3082 }
3083
3084 /********
3085  * flags *
3086  ********/
3087
3088 /* we just compare the value here */
3089 static gint
3090 gst_value_compare_flags (const GValue * value1, const GValue * value2)
3091 {
3092   guint fl1, fl2;
3093   GFlagsClass *klass1 =
3094       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
3095   GFlagsClass *klass2 =
3096       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
3097
3098   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
3099   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
3100   fl1 = g_value_get_flags (value1);
3101   fl2 = g_value_get_flags (value2);
3102   g_type_class_unref (klass1);
3103   g_type_class_unref (klass2);
3104   if (fl1 < fl2)
3105     return GST_VALUE_LESS_THAN;
3106   if (fl1 > fl2)
3107     return GST_VALUE_GREATER_THAN;
3108
3109   return GST_VALUE_EQUAL;
3110 }
3111
3112 /* the different flags are serialized separated with a + */
3113 static gchar *
3114 gst_value_serialize_flags (const GValue * value)
3115 {
3116   guint flags;
3117   GFlagsValue *fl;
3118   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
3119   gchar *result, *tmp;
3120   gboolean first = TRUE;
3121
3122   g_return_val_if_fail (klass, NULL);
3123
3124   flags = g_value_get_flags (value);
3125
3126   /* if no flags are set, try to serialize to the _NONE string */
3127   if (!flags) {
3128     fl = g_flags_get_first_value (klass, flags);
3129     if (fl)
3130       return g_strdup (fl->value_name);
3131     else
3132       return g_strdup ("0");
3133   }
3134
3135   /* some flags are set, so serialize one by one */
3136   result = g_strdup ("");
3137   while (flags) {
3138     fl = g_flags_get_first_value (klass, flags);
3139     if (fl != NULL) {
3140       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
3141       g_free (result);
3142       result = tmp;
3143       first = FALSE;
3144
3145       /* clear flag */
3146       flags &= ~fl->value;
3147     }
3148   }
3149   g_type_class_unref (klass);
3150
3151   return result;
3152 }
3153
3154 static gboolean
3155 gst_value_deserialize_flags (GValue * dest, const gchar * s)
3156 {
3157   GFlagsValue *fl;
3158   gchar *endptr = NULL;
3159   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3160   gchar **split;
3161   guint flags;
3162   gint i;
3163
3164   g_return_val_if_fail (klass, FALSE);
3165
3166   /* split into parts delimited with + */
3167   split = g_strsplit (s, "+", 0);
3168
3169   flags = 0;
3170   i = 0;
3171   /* loop over each part */
3172   while (split[i]) {
3173     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
3174       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
3175         gint val = strtol (split[i], &endptr, 0);
3176
3177         /* just or numeric value */
3178         if (endptr && *endptr == '\0') {
3179           flags |= val;
3180         }
3181       }
3182     }
3183     if (fl) {
3184       flags |= fl->value;
3185     }
3186     i++;
3187   }
3188   g_strfreev (split);
3189   g_type_class_unref (klass);
3190   g_value_set_flags (dest, flags);
3191
3192   return TRUE;
3193 }
3194
3195 /****************
3196  * subset *
3197  ****************/
3198
3199 static gboolean
3200 gst_value_is_subset_int_range_int_range (const GValue * value1,
3201     const GValue * value2)
3202 {
3203   gint gcd;
3204
3205   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
3206   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
3207
3208   if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
3209       INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
3210     return FALSE;
3211   if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
3212       INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
3213     return FALSE;
3214
3215   if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
3216     if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
3217         INT_RANGE_STEP (value1))
3218       return FALSE;
3219     return TRUE;
3220   }
3221
3222   gcd =
3223       gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
3224       INT_RANGE_STEP (value2));
3225   if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
3226     return FALSE;
3227
3228   return TRUE;
3229 }
3230
3231 static gboolean
3232 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
3233     const GValue * value2)
3234 {
3235   gint64 gcd;
3236
3237   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
3238   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
3239
3240   if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
3241     return FALSE;
3242   if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
3243     return FALSE;
3244
3245   if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
3246     if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
3247         INT64_RANGE_STEP (value1))
3248       return FALSE;
3249     return TRUE;
3250   }
3251
3252   gcd =
3253       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
3254       INT64_RANGE_STEP (value2));
3255   if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
3256     return FALSE;
3257
3258   return TRUE;
3259 }
3260
3261 /**
3262  * gst_value_is_subset:
3263  * @value1: a #GValue
3264  * @value2: a #GValue
3265  *
3266  * Check that @value1 is a subset of @value2.
3267  *
3268  * Return: %TRUE is @value1 is a subset of @value2
3269  */
3270 gboolean
3271 gst_value_is_subset (const GValue * value1, const GValue * value2)
3272 {
3273   /* special case for int/int64 ranges, since we cannot compute
3274      the difference for those when they have different steps,
3275      and it's actually a lot simpler to compute whether a range
3276      is a subset of another. */
3277   if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
3278     return gst_value_is_subset_int_range_int_range (value1, value2);
3279   } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
3280       && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
3281     return gst_value_is_subset_int64_range_int64_range (value1, value2);
3282   }
3283
3284   /*
3285    * 1 - [1,2] = empty
3286    * -> !subset
3287    *
3288    * [1,2] - 1 = 2
3289    *  -> 1 - [1,2] = empty
3290    *  -> subset
3291    *
3292    * [1,3] - [1,2] = 3
3293    * -> [1,2] - [1,3] = empty
3294    * -> subset
3295    *
3296    * {1,2} - {1,3} = 2
3297    * -> {1,3} - {1,2} = 3
3298    * -> !subset
3299    *
3300    *  First caps subtraction needs to return a non-empty set, second
3301    *  subtractions needs to give en empty set.
3302    *  Both substractions are switched below, as it's faster that way.
3303    */
3304   if (!gst_value_subtract (NULL, value1, value2)) {
3305     if (gst_value_subtract (NULL, value2, value1)) {
3306       return TRUE;
3307     }
3308   }
3309   return FALSE;
3310 }
3311
3312 /*********
3313  * union *
3314  *********/
3315
3316 static gboolean
3317 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
3318     const GValue * src2)
3319 {
3320   gint v = src1->data[0].v_int;
3321
3322   /* check if it's already in the range */
3323   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
3324       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
3325       v % INT_RANGE_STEP (src2) == 0) {
3326     if (dest)
3327       gst_value_init_and_copy (dest, src2);
3328     return TRUE;
3329   }
3330
3331   /* check if it extends the range */
3332   if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
3333     if (dest) {
3334       guint64 new_min =
3335           (guint) ((INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2));
3336       guint64 new_max = (guint) (INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3337
3338       gst_value_init_and_copy (dest, src2);
3339       dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3340     }
3341     return TRUE;
3342   }
3343   if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
3344     if (dest) {
3345       guint64 new_min = (guint) (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3346       guint64 new_max =
3347           (guint) ((INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2));
3348
3349       gst_value_init_and_copy (dest, src2);
3350       dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3351     }
3352     return TRUE;
3353   }
3354
3355   return FALSE;
3356 }
3357
3358 static gboolean
3359 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
3360     const GValue * src2)
3361 {
3362   /* We can union in several special cases:
3363      1 - one is a subset of another
3364      2 - same step and not disjoint
3365      3 - different step, at least one with one value which matches a 'next' or 'previous'
3366      - anything else ?
3367    */
3368
3369   /* 1 - subset */
3370   if (gst_value_is_subset_int_range_int_range (src1, src2)) {
3371     if (dest)
3372       gst_value_init_and_copy (dest, src2);
3373     return TRUE;
3374   }
3375   if (gst_value_is_subset_int_range_int_range (src2, src1)) {
3376     if (dest)
3377       gst_value_init_and_copy (dest, src1);
3378     return TRUE;
3379   }
3380
3381   /* 2 - same step and not disjoint */
3382   if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
3383     if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
3384             INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
3385         (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
3386             INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
3387       if (dest) {
3388         gint step = INT_RANGE_STEP (src1);
3389         gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
3390         gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
3391         g_value_init (dest, GST_TYPE_INT_RANGE);
3392         gst_value_set_int_range_step (dest, min, max, step);
3393       }
3394       return TRUE;
3395     }
3396   }
3397
3398   /* 3 - single value matches next or previous */
3399   if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
3400     gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
3401     gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
3402     if (n1 == 1 || n2 == 1) {
3403       const GValue *range_value = NULL;
3404       gint scalar = 0;
3405       if (n1 == 1) {
3406         range_value = src2;
3407         scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
3408       } else if (n2 == 1) {
3409         range_value = src1;
3410         scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
3411       }
3412
3413       if (scalar ==
3414           (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
3415         if (dest) {
3416           guint64 new_min = (guint)
3417               ((INT_RANGE_MIN (range_value) -
3418                   1) * INT_RANGE_STEP (range_value));
3419           guint64 new_max = (guint)
3420               (INT_RANGE_MAX (range_value) * INT_RANGE_STEP (range_value));
3421
3422           gst_value_init_and_copy (dest, range_value);
3423           dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3424         }
3425         return TRUE;
3426       } else if (scalar ==
3427           (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
3428         if (dest) {
3429           guint64 new_min = (guint)
3430               (INT_RANGE_MIN (range_value) * INT_RANGE_STEP (range_value));
3431           guint64 new_max = (guint)
3432               ((INT_RANGE_MAX (range_value) +
3433                   1) * INT_RANGE_STEP (range_value));
3434           gst_value_init_and_copy (dest, range_value);
3435           dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3436         }
3437         return TRUE;
3438       }
3439     }
3440   }
3441
3442   /* If we get there, we did not find a way to make a union that can be
3443      represented with our simplistic model. */
3444   return FALSE;
3445 }
3446
3447 /****************
3448  * intersection *
3449  ****************/
3450
3451 static gboolean
3452 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
3453     const GValue * src2)
3454 {
3455   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
3456       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
3457       src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
3458     if (dest)
3459       gst_value_init_and_copy (dest, src1);
3460     return TRUE;
3461   }
3462
3463   return FALSE;
3464 }
3465
3466 static gboolean
3467 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
3468     const GValue * src2)
3469 {
3470   gint min;
3471   gint max;
3472   gint step;
3473
3474   step =
3475       INT_RANGE_STEP (src1) /
3476       gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
3477       INT_RANGE_STEP (src2));
3478   if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
3479     return FALSE;
3480   step *= INT_RANGE_STEP (src2);
3481
3482   min =
3483       MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
3484       INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3485   min = (min + step - 1) / step * step;
3486   max =
3487       MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
3488       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3489   max = max / step * step;
3490
3491   if (min < max) {
3492     if (dest) {
3493       g_value_init (dest, GST_TYPE_INT_RANGE);
3494       gst_value_set_int_range_step (dest, min, max, step);
3495     }
3496     return TRUE;
3497   }
3498   if (min == max) {
3499     if (dest) {
3500       g_value_init (dest, G_TYPE_INT);
3501       g_value_set_int (dest, min);
3502     }
3503     return TRUE;
3504   }
3505
3506   return FALSE;
3507 }
3508
3509 #define INT64_RANGE_MIN_VAL(v) (INT64_RANGE_MIN (v) * INT64_RANGE_STEP (v))
3510 #define INT64_RANGE_MAX_VAL(v) (INT64_RANGE_MAX (v) * INT64_RANGE_STEP (v))
3511
3512 static gboolean
3513 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
3514     const GValue * src2)
3515 {
3516   if (INT64_RANGE_MIN_VAL (src2) <= src1->data[0].v_int64 &&
3517       INT64_RANGE_MAX_VAL (src2) >= src1->data[0].v_int64 &&
3518       src1->data[0].v_int64 % INT64_RANGE_STEP (src2) == 0) {
3519     if (dest)
3520       gst_value_init_and_copy (dest, src1);
3521     return TRUE;
3522   }
3523
3524   return FALSE;
3525 }
3526
3527 static gboolean
3528 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
3529     const GValue * src2)
3530 {
3531   gint64 min;
3532   gint64 max;
3533   gint64 step;
3534
3535   step =
3536       INT64_RANGE_STEP (src1) /
3537       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
3538       INT64_RANGE_STEP (src2));
3539   if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
3540     return FALSE;
3541   step *= INT64_RANGE_STEP (src2);
3542
3543   min =
3544       MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
3545       INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
3546   min = (min + step - 1) / step * step;
3547   max =
3548       MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
3549       INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
3550   max = max / step * step;
3551
3552   if (min < max) {
3553     if (dest) {
3554       g_value_init (dest, GST_TYPE_INT64_RANGE);
3555       gst_value_set_int64_range_step (dest, min, max, step);
3556     }
3557     return TRUE;
3558   }
3559   if (min == max) {
3560     if (dest) {
3561       g_value_init (dest, G_TYPE_INT64);
3562       g_value_set_int64 (dest, min);
3563     }
3564     return TRUE;
3565   }
3566
3567   return FALSE;
3568 }
3569
3570 static gboolean
3571 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
3572     const GValue * src2)
3573 {
3574   if (src2->data[0].v_double <= src1->data[0].v_double &&
3575       src2->data[1].v_double >= src1->data[0].v_double) {
3576     if (dest)
3577       gst_value_init_and_copy (dest, src1);
3578     return TRUE;
3579   }
3580
3581   return FALSE;
3582 }
3583
3584 static gboolean
3585 gst_value_intersect_double_range_double_range (GValue * dest,
3586     const GValue * src1, const GValue * src2)
3587 {
3588   gdouble min;
3589   gdouble max;
3590
3591   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
3592   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
3593
3594   if (min < max) {
3595     if (dest) {
3596       g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
3597       gst_value_set_double_range (dest, min, max);
3598     }
3599     return TRUE;
3600   }
3601   if (min == max) {
3602     if (dest) {
3603       g_value_init (dest, G_TYPE_DOUBLE);
3604       g_value_set_int (dest, (int) min);
3605     }
3606     return TRUE;
3607   }
3608
3609   return FALSE;
3610 }
3611
3612 static gboolean
3613 gst_value_intersect_list (GValue * dest, const GValue * value1,
3614     const GValue * value2)
3615 {
3616   guint i, size;
3617   GValue intersection = { 0, };
3618   gboolean ret = FALSE;
3619
3620   size = VALUE_LIST_SIZE (value1);
3621   for (i = 0; i < size; i++) {
3622     const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
3623
3624     /* quicker version when we don't need the resulting set */
3625     if (!dest) {
3626       if (gst_value_intersect (NULL, cur, value2)) {
3627         ret = TRUE;
3628         break;
3629       }
3630       continue;
3631     }
3632
3633     if (gst_value_intersect (&intersection, cur, value2)) {
3634       /* append value */
3635       if (!ret) {
3636         gst_value_move (dest, &intersection);
3637         ret = TRUE;
3638       } else if (GST_VALUE_HOLDS_LIST (dest)) {
3639         _gst_value_list_append_and_take_value (dest, &intersection);
3640       } else {
3641         GValue temp;
3642
3643         gst_value_move (&temp, dest);
3644         gst_value_list_merge (dest, &temp, &intersection);
3645         g_value_unset (&temp);
3646         g_value_unset (&intersection);
3647       }
3648     }
3649   }
3650
3651   return ret;
3652 }
3653
3654 static gboolean
3655 gst_value_intersect_array (GValue * dest, const GValue * src1,
3656     const GValue * src2)
3657 {
3658   guint size;
3659   guint n;
3660   GValue val = { 0 };
3661
3662   /* only works on similar-sized arrays */
3663   size = gst_value_array_get_size (src1);
3664   if (size != gst_value_array_get_size (src2))
3665     return FALSE;
3666
3667   /* quicker value when we don't need the resulting set */
3668   if (!dest) {
3669     for (n = 0; n < size; n++) {
3670       if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
3671               gst_value_array_get_value (src2, n))) {
3672         return FALSE;
3673       }
3674     }
3675     return TRUE;
3676   }
3677
3678   g_value_init (dest, GST_TYPE_ARRAY);
3679
3680   for (n = 0; n < size; n++) {
3681     if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
3682             gst_value_array_get_value (src2, n))) {
3683       g_value_unset (dest);
3684       return FALSE;
3685     }
3686     _gst_value_array_append_and_take_value (dest, &val);
3687   }
3688
3689   return TRUE;
3690 }
3691
3692 static gboolean
3693 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
3694     const GValue * src2)
3695 {
3696   gint res1, res2;
3697   GValue *vals;
3698   GstValueCompareFunc compare;
3699
3700   vals = src2->data[0].v_pointer;
3701
3702   if (vals == NULL)
3703     return FALSE;
3704
3705   if ((compare = gst_value_get_compare_func (src1))) {
3706     res1 = gst_value_compare_with_func (&vals[0], src1, compare);
3707     res2 = gst_value_compare_with_func (&vals[1], src1, compare);
3708
3709     if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
3710         (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
3711       if (dest)
3712         gst_value_init_and_copy (dest, src1);
3713       return TRUE;
3714     }
3715   }
3716
3717   return FALSE;
3718 }
3719
3720 static gboolean
3721 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
3722     const GValue * src1, const GValue * src2)
3723 {
3724   GValue *min;
3725   GValue *max;
3726   gint res;
3727   GValue *vals1, *vals2;
3728   GstValueCompareFunc compare;
3729
3730   vals1 = src1->data[0].v_pointer;
3731   vals2 = src2->data[0].v_pointer;
3732   g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
3733
3734   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
3735     /* min = MAX (src1.start, src2.start) */
3736     res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
3737     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3738     if (res == GST_VALUE_LESS_THAN)
3739       min = &vals2[0];          /* Take the max of the 2 */
3740     else
3741       min = &vals1[0];
3742
3743     /* max = MIN (src1.end, src2.end) */
3744     res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
3745     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3746     if (res == GST_VALUE_GREATER_THAN)
3747       max = &vals2[1];          /* Take the min of the 2 */
3748     else
3749       max = &vals1[1];
3750
3751     res = gst_value_compare_with_func (min, max, compare);
3752     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3753     if (res == GST_VALUE_LESS_THAN) {
3754       if (dest) {
3755         g_value_init (dest, GST_TYPE_FRACTION_RANGE);
3756         vals1 = dest->data[0].v_pointer;
3757         g_value_copy (min, &vals1[0]);
3758         g_value_copy (max, &vals1[1]);
3759       }
3760       return TRUE;
3761     }
3762     if (res == GST_VALUE_EQUAL) {
3763       if (dest)
3764         gst_value_init_and_copy (dest, min);
3765       return TRUE;
3766     }
3767   }
3768
3769   return FALSE;
3770 }
3771
3772 /***************
3773  * subtraction *
3774  ***************/
3775
3776 static gboolean
3777 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
3778     const GValue * subtrahend)
3779 {
3780   gint min = gst_value_get_int_range_min (subtrahend);
3781   gint max = gst_value_get_int_range_max (subtrahend);
3782   gint step = gst_value_get_int_range_step (subtrahend);
3783   gint val = g_value_get_int (minuend);
3784
3785   if (step == 0)
3786     return FALSE;
3787
3788   /* subtracting a range from an int only works if the int is not in the
3789    * range */
3790   if (val < min || val > max || val % step) {
3791     /* and the result is the int */
3792     if (dest)
3793       gst_value_init_and_copy (dest, minuend);
3794     return TRUE;
3795   }
3796   return FALSE;
3797 }
3798
3799 /* creates a new int range based on input values.
3800  */
3801 static gboolean
3802 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
3803     gint max2, gint step)
3804 {
3805   GValue v1 = { 0, };
3806   GValue v2 = { 0, };
3807   GValue *pv1, *pv2;            /* yeah, hungarian! */
3808
3809   g_return_val_if_fail (step > 0, FALSE);
3810   g_return_val_if_fail (min1 % step == 0, FALSE);
3811   g_return_val_if_fail (max1 % step == 0, FALSE);
3812   g_return_val_if_fail (min2 % step == 0, FALSE);
3813   g_return_val_if_fail (max2 % step == 0, FALSE);
3814
3815   if (min1 <= max1 && min2 <= max2) {
3816     pv1 = &v1;
3817     pv2 = &v2;
3818   } else if (min1 <= max1) {
3819     pv1 = dest;
3820     pv2 = NULL;
3821   } else if (min2 <= max2) {
3822     pv1 = NULL;
3823     pv2 = dest;
3824   } else {
3825     return FALSE;
3826   }
3827
3828   if (!dest)
3829     return TRUE;
3830
3831   if (min1 < max1) {
3832     g_value_init (pv1, GST_TYPE_INT_RANGE);
3833     gst_value_set_int_range_step (pv1, min1, max1, step);
3834   } else if (min1 == max1) {
3835     g_value_init (pv1, G_TYPE_INT);
3836     g_value_set_int (pv1, min1);
3837   }
3838   if (min2 < max2) {
3839     g_value_init (pv2, GST_TYPE_INT_RANGE);
3840     gst_value_set_int_range_step (pv2, min2, max2, step);
3841   } else if (min2 == max2) {
3842     g_value_init (pv2, G_TYPE_INT);
3843     g_value_set_int (pv2, min2);
3844   }
3845
3846   if (min1 <= max1 && min2 <= max2) {
3847     gst_value_list_concat (dest, pv1, pv2);
3848     g_value_unset (pv1);
3849     g_value_unset (pv2);
3850   }
3851   return TRUE;
3852 }
3853
3854 static gboolean
3855 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
3856     const GValue * subtrahend)
3857 {
3858   gint min = gst_value_get_int_range_min (minuend);
3859   gint max = gst_value_get_int_range_max (minuend);
3860   gint step = gst_value_get_int_range_step (minuend);
3861   gint val = g_value_get_int (subtrahend);
3862
3863   g_return_val_if_fail (min < max, FALSE);
3864
3865   if (step == 0)
3866     return FALSE;
3867
3868   /* value is outside of the range, return range unchanged */
3869   if (val < min || val > max || val % step) {
3870     if (dest)
3871       gst_value_init_and_copy (dest, minuend);
3872     return TRUE;
3873   } else {
3874     /* max must be MAXINT too as val <= max */
3875     if (val >= G_MAXINT - step + 1) {
3876       max -= step;
3877       val -= step;
3878     }
3879     /* min must be MININT too as val >= max */
3880     if (val <= G_MININT + step - 1) {
3881       min += step;
3882       val += step;
3883     }
3884     if (dest)
3885       gst_value_create_new_range (dest, min, val - step, val + step, max, step);
3886   }
3887   return TRUE;
3888 }
3889
3890 static gboolean
3891 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
3892     const GValue * subtrahend)
3893 {
3894   gint min1 = gst_value_get_int_range_min (minuend);
3895   gint max1 = gst_value_get_int_range_max (minuend);
3896   gint step1 = gst_value_get_int_range_step (minuend);
3897   gint min2 = gst_value_get_int_range_min (subtrahend);
3898   gint max2 = gst_value_get_int_range_max (subtrahend);
3899   gint step2 = gst_value_get_int_range_step (subtrahend);
3900   gint step;
3901
3902   if (step1 != step2) {
3903     /* ENOIMPL */
3904     g_assert (FALSE);
3905     return FALSE;
3906   }
3907   step = step1;
3908
3909   if (step == 0)
3910     return FALSE;
3911
3912   if (max2 >= max1 && min2 <= min1) {
3913     return FALSE;
3914   } else if (max2 >= max1) {
3915     return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3916         step, 0, step);
3917   } else if (min2 <= min1) {
3918     return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
3919         step, 0, step);
3920   } else {
3921     return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3922         MAX (max2 + step, min1), max1, step);
3923   }
3924 }
3925
3926 static gboolean
3927 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
3928     const GValue * subtrahend)
3929 {
3930   gint64 min = gst_value_get_int64_range_min (subtrahend);
3931   gint64 max = gst_value_get_int64_range_max (subtrahend);
3932   gint64 step = gst_value_get_int64_range_step (subtrahend);
3933   gint64 val = g_value_get_int64 (minuend);
3934
3935   if (step == 0)
3936     return FALSE;
3937   /* subtracting a range from an int64 only works if the int64 is not in the
3938    * range */
3939   if (val < min || val > max || val % step) {
3940     /* and the result is the int64 */
3941     if (dest)
3942       gst_value_init_and_copy (dest, minuend);
3943     return TRUE;
3944   }
3945   return FALSE;
3946 }
3947
3948 /* creates a new int64 range based on input values.
3949  */
3950 static gboolean
3951 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
3952     gint64 min2, gint64 max2, gint64 step)
3953 {
3954   GValue v1 = { 0, };
3955   GValue v2 = { 0, };
3956   GValue *pv1, *pv2;            /* yeah, hungarian! */
3957
3958   g_return_val_if_fail (step > 0, FALSE);
3959   g_return_val_if_fail (min1 % step == 0, FALSE);
3960   g_return_val_if_fail (max1 % step == 0, FALSE);
3961   g_return_val_if_fail (min2 % step == 0, FALSE);
3962   g_return_val_if_fail (max2 % step == 0, FALSE);
3963
3964   if (min1 <= max1 && min2 <= max2) {
3965     pv1 = &v1;
3966     pv2 = &v2;
3967   } else if (min1 <= max1) {
3968     pv1 = dest;
3969     pv2 = NULL;
3970   } else if (min2 <= max2) {
3971     pv1 = NULL;
3972     pv2 = dest;
3973   } else {
3974     return FALSE;
3975   }
3976
3977   if (!dest)
3978     return TRUE;
3979
3980   if (min1 < max1) {
3981     g_value_init (pv1, GST_TYPE_INT64_RANGE);
3982     gst_value_set_int64_range_step (pv1, min1, max1, step);
3983   } else if (min1 == max1) {
3984     g_value_init (pv1, G_TYPE_INT64);
3985     g_value_set_int64 (pv1, min1);
3986   }
3987   if (min2 < max2) {
3988     g_value_init (pv2, GST_TYPE_INT64_RANGE);
3989     gst_value_set_int64_range_step (pv2, min2, max2, step);
3990   } else if (min2 == max2) {
3991     g_value_init (pv2, G_TYPE_INT64);
3992     g_value_set_int64 (pv2, min2);
3993   }
3994
3995   if (min1 <= max1 && min2 <= max2) {
3996     gst_value_list_concat (dest, pv1, pv2);
3997     g_value_unset (pv1);
3998     g_value_unset (pv2);
3999   }
4000   return TRUE;
4001 }
4002
4003 static gboolean
4004 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
4005     const GValue * subtrahend)
4006 {
4007   gint64 min = gst_value_get_int64_range_min (minuend);
4008   gint64 max = gst_value_get_int64_range_max (minuend);
4009   gint64 step = gst_value_get_int64_range_step (minuend);
4010   gint64 val = g_value_get_int64 (subtrahend);
4011
4012   g_return_val_if_fail (min < max, FALSE);
4013
4014   if (step == 0)
4015     return FALSE;
4016
4017   /* value is outside of the range, return range unchanged */
4018   if (val < min || val > max || val % step) {
4019     if (dest)
4020       gst_value_init_and_copy (dest, minuend);
4021     return TRUE;
4022   } else {
4023     /* max must be MAXINT64 too as val <= max */
4024     if (val >= G_MAXINT64 - step + 1) {
4025       max -= step;
4026       val -= step;
4027     }
4028     /* min must be MININT64 too as val >= max */
4029     if (val <= G_MININT64 + step - 1) {
4030       min += step;
4031       val += step;
4032     }
4033     if (dest)
4034       gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
4035           step);
4036   }
4037   return TRUE;
4038 }
4039
4040 static gboolean
4041 gst_value_subtract_int64_range_int64_range (GValue * dest,
4042     const GValue * minuend, const GValue * subtrahend)
4043 {
4044   gint64 min1 = gst_value_get_int64_range_min (minuend);
4045   gint64 max1 = gst_value_get_int64_range_max (minuend);
4046   gint64 step1 = gst_value_get_int64_range_step (minuend);
4047   gint64 min2 = gst_value_get_int64_range_min (subtrahend);
4048   gint64 max2 = gst_value_get_int64_range_max (subtrahend);
4049   gint64 step2 = gst_value_get_int64_range_step (subtrahend);
4050   gint64 step;
4051
4052   if (step1 != step2) {
4053     /* ENOIMPL */
4054     g_assert (FALSE);
4055     return FALSE;
4056   }
4057
4058   if (step1 == 0)
4059     return FALSE;
4060
4061   step = step1;
4062
4063   if (max2 >= max1 && min2 <= min1) {
4064     return FALSE;
4065   } else if (max2 >= max1) {
4066     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
4067             max1), step, 0, step);
4068   } else if (min2 <= min1) {
4069     return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
4070         max1, step, 0, step);
4071   } else {
4072     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
4073             max1), MAX (max2 + step, min1), max1, step);
4074   }
4075 }
4076
4077 static gboolean
4078 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
4079     const GValue * subtrahend)
4080 {
4081   gdouble min = gst_value_get_double_range_min (subtrahend);
4082   gdouble max = gst_value_get_double_range_max (subtrahend);
4083   gdouble val = g_value_get_double (minuend);
4084
4085   if (val < min || val > max) {
4086     if (dest)
4087       gst_value_init_and_copy (dest, minuend);
4088     return TRUE;
4089   }
4090   return FALSE;
4091 }
4092
4093 static gboolean
4094 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
4095     const GValue * subtrahend)
4096 {
4097   /* since we don't have open ranges, we cannot create a hole in
4098    * a double range. We return the original range */
4099   if (dest)
4100     gst_value_init_and_copy (dest, minuend);
4101   return TRUE;
4102 }
4103
4104 static gboolean
4105 gst_value_subtract_double_range_double_range (GValue * dest,
4106     const GValue * minuend, const GValue * subtrahend)
4107 {
4108   /* since we don't have open ranges, we have to approximate */
4109   /* done like with ints */
4110   gdouble min1 = gst_value_get_double_range_min (minuend);
4111   gdouble max2 = gst_value_get_double_range_max (minuend);
4112   gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
4113   gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
4114   GValue v1 = { 0, };
4115   GValue v2 = { 0, };
4116   GValue *pv1, *pv2;            /* yeah, hungarian! */
4117
4118   if (min1 < max1 && min2 < max2) {
4119     pv1 = &v1;
4120     pv2 = &v2;
4121   } else if (min1 < max1) {
4122     pv1 = dest;
4123     pv2 = NULL;
4124   } else if (min2 < max2) {
4125     pv1 = NULL;
4126     pv2 = dest;
4127   } else {
4128     return FALSE;
4129   }
4130
4131   if (!dest)
4132     return TRUE;
4133
4134   if (min1 < max1) {
4135     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
4136     gst_value_set_double_range (pv1, min1, max1);
4137   }
4138   if (min2 < max2) {
4139     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
4140     gst_value_set_double_range (pv2, min2, max2);
4141   }
4142
4143   if (min1 < max1 && min2 < max2) {
4144     gst_value_list_concat (dest, pv1, pv2);
4145     g_value_unset (pv1);
4146     g_value_unset (pv2);
4147   }
4148   return TRUE;
4149 }
4150
4151 static gboolean
4152 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
4153     const GValue * subtrahend)
4154 {
4155   guint i, size;
4156   GValue subtraction = { 0, };
4157   gboolean ret = FALSE;
4158   GType ltype;
4159
4160   ltype = gst_value_list_get_type ();
4161
4162   size = VALUE_LIST_SIZE (minuend);
4163   for (i = 0; i < size; i++) {
4164     const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
4165
4166     /* quicker version when we can discard the result */
4167     if (!dest) {
4168       if (gst_value_subtract (NULL, cur, subtrahend)) {
4169         ret = TRUE;
4170         break;
4171       }
4172       continue;
4173     }
4174
4175     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
4176       if (!ret) {
4177         gst_value_move (dest, &subtraction);
4178         ret = TRUE;
4179       } else if (G_VALUE_HOLDS (dest, ltype)
4180           && !G_VALUE_HOLDS (&subtraction, ltype)) {
4181         _gst_value_list_append_and_take_value (dest, &subtraction);
4182       } else {
4183         GValue temp;
4184
4185         gst_value_move (&temp, dest);
4186         gst_value_list_concat (dest, &temp, &subtraction);
4187         g_value_unset (&temp);
4188         g_value_unset (&subtraction);
4189       }
4190     }
4191   }
4192   return ret;
4193 }
4194
4195 static gboolean
4196 gst_value_subtract_list (GValue * dest, const GValue * minuend,
4197     const GValue * subtrahend)
4198 {
4199   guint i, size;
4200   GValue data[2] = { {0,}, {0,} };
4201   GValue *subtraction = &data[0], *result = &data[1];
4202
4203   gst_value_init_and_copy (result, minuend);
4204   size = VALUE_LIST_SIZE (subtrahend);
4205   for (i = 0; i < size; i++) {
4206     const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
4207
4208     if (gst_value_subtract (subtraction, result, cur)) {
4209       GValue *temp = result;
4210
4211       result = subtraction;
4212       subtraction = temp;
4213       g_value_unset (subtraction);
4214     } else {
4215       g_value_unset (result);
4216       return FALSE;
4217     }
4218   }
4219   if (dest) {
4220     gst_value_move (dest, result);
4221   } else {
4222     g_value_unset (result);
4223   }
4224   return TRUE;
4225 }
4226
4227 static gboolean
4228 gst_value_subtract_fraction_fraction_range (GValue * dest,
4229     const GValue * minuend, const GValue * subtrahend)
4230 {
4231   const GValue *min = gst_value_get_fraction_range_min (subtrahend);
4232   const GValue *max = gst_value_get_fraction_range_max (subtrahend);
4233   GstValueCompareFunc compare;
4234
4235   if ((compare = gst_value_get_compare_func (minuend))) {
4236     /* subtracting a range from an fraction only works if the fraction
4237      * is not in the range */
4238     if (gst_value_compare_with_func (minuend, min, compare) ==
4239         GST_VALUE_LESS_THAN ||
4240         gst_value_compare_with_func (minuend, max, compare) ==
4241         GST_VALUE_GREATER_THAN) {
4242       /* and the result is the value */
4243       if (dest)
4244         gst_value_init_and_copy (dest, minuend);
4245       return TRUE;
4246     }
4247   }
4248   return FALSE;
4249 }
4250
4251 static gboolean
4252 gst_value_subtract_fraction_range_fraction (GValue * dest,
4253     const GValue * minuend, const GValue * subtrahend)
4254 {
4255   /* since we don't have open ranges, we cannot create a hole in
4256    * a range. We return the original range */
4257   if (dest)
4258     gst_value_init_and_copy (dest, minuend);
4259   return TRUE;
4260 }
4261
4262 static gboolean
4263 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
4264     const GValue * minuend, const GValue * subtrahend)
4265 {
4266   /* since we don't have open ranges, we have to approximate */
4267   /* done like with ints and doubles. Creates a list of 2 fraction ranges */
4268   const GValue *min1 = gst_value_get_fraction_range_min (minuend);
4269   const GValue *max2 = gst_value_get_fraction_range_max (minuend);
4270   const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
4271   const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
4272   gint cmp1, cmp2;
4273   GValue v1 = { 0, };
4274   GValue v2 = { 0, };
4275   GValue *pv1, *pv2;            /* yeah, hungarian! */
4276   GstValueCompareFunc compare;
4277
4278   g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
4279   g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
4280
4281   compare = gst_value_get_compare_func (min1);
4282   g_return_val_if_fail (compare, FALSE);
4283
4284   cmp1 = gst_value_compare_with_func (max2, max1, compare);
4285   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
4286   if (cmp1 == GST_VALUE_LESS_THAN)
4287     max1 = max2;
4288   cmp1 = gst_value_compare_with_func (min1, min2, compare);
4289   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
4290   if (cmp1 == GST_VALUE_GREATER_THAN)
4291     min2 = min1;
4292
4293   cmp1 = gst_value_compare_with_func (min1, max1, compare);
4294   cmp2 = gst_value_compare_with_func (min2, max2, compare);
4295
4296   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
4297     pv1 = &v1;
4298     pv2 = &v2;
4299   } else if (cmp1 == GST_VALUE_LESS_THAN) {
4300     pv1 = dest;
4301     pv2 = NULL;
4302   } else if (cmp2 == GST_VALUE_LESS_THAN) {
4303     pv1 = NULL;
4304     pv2 = dest;
4305   } else {
4306     return FALSE;
4307   }
4308
4309   if (!dest)
4310     return TRUE;
4311
4312   if (cmp1 == GST_VALUE_LESS_THAN) {
4313     g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
4314     gst_value_set_fraction_range (pv1, min1, max1);
4315   }
4316   if (cmp2 == GST_VALUE_LESS_THAN) {
4317     g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
4318     gst_value_set_fraction_range (pv2, min2, max2);
4319   }
4320
4321   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
4322     gst_value_list_concat (dest, pv1, pv2);
4323     g_value_unset (pv1);
4324     g_value_unset (pv2);
4325   }
4326   return TRUE;
4327 }
4328
4329
4330 /**************
4331  * comparison *
4332  **************/
4333
4334 /*
4335  * gst_value_get_compare_func:
4336  * @value1: a value to get the compare function for
4337  *
4338  * Determines the compare function to be used with values of the same type as
4339  * @value1. The function can be given to gst_value_compare_with_func().
4340  *
4341  * Returns: A #GstValueCompareFunc value
4342  */
4343 static GstValueCompareFunc
4344 gst_value_get_compare_func (const GValue * value1)
4345 {
4346   GstValueTable *table, *best = NULL;
4347   guint i;
4348   GType type1;
4349
4350   type1 = G_VALUE_TYPE (value1);
4351
4352   /* this is a fast check */
4353   best = gst_value_hash_lookup_type (type1);
4354
4355   /* slower checks */
4356   if (G_UNLIKELY (!best || !best->compare)) {
4357     guint len = gst_value_table->len;
4358
4359     best = NULL;
4360     for (i = 0; i < len; i++) {
4361       table = &g_array_index (gst_value_table, GstValueTable, i);
4362       if (table->compare && g_type_is_a (type1, table->type)) {
4363         if (!best || g_type_is_a (table->type, best->type))
4364           best = table;
4365       }
4366     }
4367   }
4368   if (G_LIKELY (best))
4369     return best->compare;
4370
4371   return NULL;
4372 }
4373
4374 /**
4375  * gst_value_can_compare:
4376  * @value1: a value to compare
4377  * @value2: another value to compare
4378  *
4379  * Determines if @value1 and @value2 can be compared.
4380  *
4381  * Returns: %TRUE if the values can be compared
4382  */
4383 gboolean
4384 gst_value_can_compare (const GValue * value1, const GValue * value2)
4385 {
4386   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4387   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4388
4389   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4390     return FALSE;
4391
4392   return gst_value_get_compare_func (value1) != NULL;
4393 }
4394
4395 static gboolean
4396 gst_value_list_equals_range (const GValue * list, const GValue * value)
4397 {
4398   const GValue *first;
4399   guint list_size, n;
4400
4401   g_return_val_if_fail (G_IS_VALUE (list), FALSE);
4402   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
4403   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (list), FALSE);
4404
4405   /* TODO: compare against an empty list ? No type though... */
4406   list_size = VALUE_LIST_SIZE (list);
4407   if (list_size == 0)
4408     return FALSE;
4409
4410   /* compare the basic types - they have to match */
4411   first = VALUE_LIST_GET_VALUE (list, 0);
4412 #define CHECK_TYPES(type,prefix) \
4413   (prefix##_VALUE_HOLDS_##type(first) && GST_VALUE_HOLDS_##type##_RANGE (value))
4414   if (CHECK_TYPES (INT, G)) {
4415     const gint rmin = gst_value_get_int_range_min (value);
4416     const gint rmax = gst_value_get_int_range_max (value);
4417     const gint rstep = gst_value_get_int_range_step (value);
4418     if (rstep == 0)
4419       return FALSE;
4420     /* note: this will overflow for min 0 and max INT_MAX, but this
4421        would only be equal to a list of INT_MAX elements, which seems
4422        very unlikely */
4423     if (list_size != rmax / rstep - rmin / rstep + 1)
4424       return FALSE;
4425     for (n = 0; n < list_size; ++n) {
4426       gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
4427       if (v < rmin || v > rmax || v % rstep) {
4428         return FALSE;
4429       }
4430     }
4431     return TRUE;
4432   } else if (CHECK_TYPES (INT64, G)) {
4433     const gint64 rmin = gst_value_get_int64_range_min (value);
4434     const gint64 rmax = gst_value_get_int64_range_max (value);
4435     const gint64 rstep = gst_value_get_int64_range_step (value);
4436     GST_DEBUG ("List/range of int64s");
4437     if (rstep == 0)
4438       return FALSE;
4439     if (list_size != rmax / rstep - rmin / rstep + 1)
4440       return FALSE;
4441     for (n = 0; n < list_size; ++n) {
4442       gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
4443       if (v < rmin || v > rmax || v % rstep)
4444         return FALSE;
4445     }
4446     return TRUE;
4447   }
4448 #undef CHECK_TYPES
4449
4450   /* other combinations don't make sense for equality */
4451   return FALSE;
4452 }
4453
4454 /* "Pure" variant of gst_value_compare which is guaranteed to
4455  * not have list arguments and therefore does basic comparisions
4456  */
4457 static inline gint
4458 _gst_value_compare_nolist (const GValue * value1, const GValue * value2)
4459 {
4460   GstValueCompareFunc compare;
4461
4462   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4463     return GST_VALUE_UNORDERED;
4464
4465   compare = gst_value_get_compare_func (value1);
4466   if (compare) {
4467     return compare (value1, value2);
4468   }
4469
4470   g_critical ("unable to compare values of type %s\n",
4471       g_type_name (G_VALUE_TYPE (value1)));
4472   return GST_VALUE_UNORDERED;
4473 }
4474
4475 /**
4476  * gst_value_compare:
4477  * @value1: a value to compare
4478  * @value2: another value to compare
4479  *
4480  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
4481  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
4482  * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
4483  * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
4484  * If the values are equal, GST_VALUE_EQUAL is returned.
4485  *
4486  * Returns: comparison result
4487  */
4488 gint
4489 gst_value_compare (const GValue * value1, const GValue * value2)
4490 {
4491   GType ltype;
4492
4493   g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
4494   g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
4495
4496   /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
4497      as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
4498   ltype = gst_value_list_get_type ();
4499   if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)) {
4500     gint i, n, ret;
4501
4502     if (gst_value_list_equals_range (value1, value2)) {
4503       return GST_VALUE_EQUAL;
4504     }
4505
4506     n = gst_value_list_get_size (value1);
4507     if (n == 0)
4508       return GST_VALUE_UNORDERED;
4509
4510     for (i = 0; i < n; i++) {
4511       const GValue *elt;
4512
4513       elt = gst_value_list_get_value (value1, i);
4514       ret = gst_value_compare (elt, value2);
4515       if (ret != GST_VALUE_EQUAL && n == 1)
4516         return ret;
4517       else if (ret != GST_VALUE_EQUAL)
4518         return GST_VALUE_UNORDERED;
4519     }
4520
4521     return GST_VALUE_EQUAL;
4522   } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)) {
4523     gint i, n, ret;
4524
4525     if (gst_value_list_equals_range (value2, value1)) {
4526       return GST_VALUE_EQUAL;
4527     }
4528
4529     n = gst_value_list_get_size (value2);
4530     if (n == 0)
4531       return GST_VALUE_UNORDERED;
4532
4533     for (i = 0; i < n; i++) {
4534       const GValue *elt;
4535
4536       elt = gst_value_list_get_value (value2, i);
4537       ret = gst_value_compare (elt, value1);
4538       if (ret != GST_VALUE_EQUAL && n == 1)
4539         return ret;
4540       else if (ret != GST_VALUE_EQUAL)
4541         return GST_VALUE_UNORDERED;
4542     }
4543
4544     return GST_VALUE_EQUAL;
4545   }
4546
4547   /* And now handle the generic case */
4548   return _gst_value_compare_nolist (value1, value2);
4549 }
4550
4551 /*
4552  * gst_value_compare_with_func:
4553  * @value1: a value to compare
4554  * @value2: another value to compare
4555  * @compare: compare function
4556  *
4557  * Compares @value1 and @value2 using the @compare function. Works like
4558  * gst_value_compare() but allows to save time determining the compare function
4559  * a multiple times. 
4560  *
4561  * Returns: comparison result
4562  */
4563 static gint
4564 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
4565     GstValueCompareFunc compare)
4566 {
4567   g_assert (compare);
4568
4569   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4570     return GST_VALUE_UNORDERED;
4571
4572   return compare (value1, value2);
4573 }
4574
4575 /* union */
4576
4577 /**
4578  * gst_value_can_union:
4579  * @value1: a value to union
4580  * @value2: another value to union
4581  *
4582  * Determines if @value1 and @value2 can be non-trivially unioned.
4583  * Any two values can be trivially unioned by adding both of them
4584  * to a #GstValueList.  However, certain types have the possibility
4585  * to be unioned in a simpler way.  For example, an integer range
4586  * and an integer can be unioned if the integer is a subset of the
4587  * integer range.  If there is the possibility that two values can
4588  * be unioned, this function returns %TRUE.
4589  *
4590  * Returns: %TRUE if there is a function allowing the two values to
4591  * be unioned.
4592  */
4593 gboolean
4594 gst_value_can_union (const GValue * value1, const GValue * value2)
4595 {
4596   GstValueUnionInfo *union_info;
4597   guint i, len;
4598
4599   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4600   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4601
4602   len = gst_value_union_funcs->len;
4603
4604   for (i = 0; i < len; i++) {
4605     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4606     if (union_info->type1 == G_VALUE_TYPE (value1) &&
4607         union_info->type2 == G_VALUE_TYPE (value2))
4608       return TRUE;
4609     if (union_info->type1 == G_VALUE_TYPE (value2) &&
4610         union_info->type2 == G_VALUE_TYPE (value1))
4611       return TRUE;
4612   }
4613
4614   return FALSE;
4615 }
4616
4617 /**
4618  * gst_value_union:
4619  * @dest: (out caller-allocates): the destination value
4620  * @value1: a value to union
4621  * @value2: another value to union
4622  *
4623  * Creates a GValue corresponding to the union of @value1 and @value2.
4624  *
4625  * Returns: %TRUE if the union succeeded.
4626  */
4627 gboolean
4628 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
4629 {
4630   const GstValueUnionInfo *union_info;
4631   guint i, len;
4632   GType type1, type2;
4633
4634   g_return_val_if_fail (dest != NULL, FALSE);
4635   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4636   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4637   g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
4638       FALSE);
4639
4640   len = gst_value_union_funcs->len;
4641   type1 = G_VALUE_TYPE (value1);
4642   type2 = G_VALUE_TYPE (value2);
4643
4644   for (i = 0; i < len; i++) {
4645     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4646     if (union_info->type1 == type1 && union_info->type2 == type2) {
4647       return union_info->func (dest, value1, value2);
4648     }
4649     if (union_info->type1 == type2 && union_info->type2 == type1) {
4650       return union_info->func (dest, value2, value1);
4651     }
4652   }
4653
4654   gst_value_list_concat (dest, value1, value2);
4655   return TRUE;
4656 }
4657
4658 /* gst_value_register_union_func: (skip)
4659  * @type1: a type to union
4660  * @type2: another type to union
4661  * @func: a function that implements creating a union between the two types
4662  *
4663  * Registers a union function that can create a union between #GValue items
4664  * of the type @type1 and @type2.
4665  *
4666  * Union functions should be registered at startup before any pipelines are
4667  * started, as gst_value_register_union_func() is not thread-safe and cannot
4668  * be used at the same time as gst_value_union() or gst_value_can_union().
4669  */
4670 static void
4671 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
4672 {
4673   GstValueUnionInfo union_info;
4674
4675   union_info.type1 = type1;
4676   union_info.type2 = type2;
4677   union_info.func = func;
4678
4679   g_array_append_val (gst_value_union_funcs, union_info);
4680 }
4681
4682 /* intersection */
4683
4684 /**
4685  * gst_value_can_intersect:
4686  * @value1: a value to intersect
4687  * @value2: another value to intersect
4688  *
4689  * Determines if intersecting two values will produce a valid result.
4690  * Two values will produce a valid intersection if they have the same
4691  * type.
4692  *
4693  * Returns: %TRUE if the values can intersect
4694  */
4695 gboolean
4696 gst_value_can_intersect (const GValue * value1, const GValue * value2)
4697 {
4698   GstValueIntersectInfo *intersect_info;
4699   guint i, len;
4700   GType ltype, type1, type2;
4701
4702   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4703   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4704
4705   ltype = gst_value_list_get_type ();
4706
4707   /* special cases */
4708   if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
4709     return TRUE;
4710
4711   type1 = G_VALUE_TYPE (value1);
4712   type2 = G_VALUE_TYPE (value2);
4713
4714   /* practically all GstValue types have a compare function (_can_compare=TRUE)
4715    * GstStructure and GstCaps have npot, but are intersectable */
4716   if (type1 == type2)
4717     return TRUE;
4718
4719   /* check registered intersect functions */
4720   len = gst_value_intersect_funcs->len;
4721   for (i = 0; i < len; i++) {
4722     intersect_info = &g_array_index (gst_value_intersect_funcs,
4723         GstValueIntersectInfo, i);
4724     if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
4725         (intersect_info->type1 == type2 && intersect_info->type2 == type1))
4726       return TRUE;
4727   }
4728
4729   return gst_value_can_compare (value1, value2);
4730 }
4731
4732 /**
4733  * gst_value_intersect:
4734  * @dest: (out caller-allocates) (transfer full): a uninitialized #GValue that will hold the calculated
4735  * intersection value. May be %NULL if the resulting set if not needed.
4736  * @value1: a value to intersect
4737  * @value2: another value to intersect
4738  *
4739  * Calculates the intersection of two values.  If the values have
4740  * a non-empty intersection, the value representing the intersection
4741  * is placed in @dest, unless %NULL.  If the intersection is non-empty,
4742  * @dest is not modified.
4743  *
4744  * Returns: %TRUE if the intersection is non-empty
4745  */
4746 gboolean
4747 gst_value_intersect (GValue * dest, const GValue * value1,
4748     const GValue * value2)
4749 {
4750   GstValueIntersectInfo *intersect_info;
4751   guint i, len;
4752   GType ltype, type1, type2;
4753
4754   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4755   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4756
4757   ltype = gst_value_list_get_type ();
4758
4759   /* special cases first */
4760   if (G_VALUE_HOLDS (value1, ltype))
4761     return gst_value_intersect_list (dest, value1, value2);
4762   if (G_VALUE_HOLDS (value2, ltype))
4763     return gst_value_intersect_list (dest, value2, value1);
4764
4765   if (_gst_value_compare_nolist (value1, value2) == GST_VALUE_EQUAL) {
4766     if (dest)
4767       gst_value_init_and_copy (dest, value1);
4768     return TRUE;
4769   }
4770
4771   type1 = G_VALUE_TYPE (value1);
4772   type2 = G_VALUE_TYPE (value2);
4773
4774   len = gst_value_intersect_funcs->len;
4775   for (i = 0; i < len; i++) {
4776     intersect_info = &g_array_index (gst_value_intersect_funcs,
4777         GstValueIntersectInfo, i);
4778     if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
4779       return intersect_info->func (dest, value1, value2);
4780     }
4781     if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
4782       return intersect_info->func (dest, value2, value1);
4783     }
4784   }
4785   return FALSE;
4786 }
4787
4788
4789
4790 /* gst_value_register_intersect_func: (skip)
4791  * @type1: the first type to intersect
4792  * @type2: the second type to intersect
4793  * @func: the intersection function
4794  *
4795  * Registers a function that is called to calculate the intersection
4796  * of the values having the types @type1 and @type2.
4797  *
4798  * Intersect functions should be registered at startup before any pipelines are
4799  * started, as gst_value_register_intersect_func() is not thread-safe and
4800  * cannot be used at the same time as gst_value_intersect() or
4801  * gst_value_can_intersect().
4802  */
4803 static void
4804 gst_value_register_intersect_func (GType type1, GType type2,
4805     GstValueIntersectFunc func)
4806 {
4807   GstValueIntersectInfo intersect_info;
4808
4809   intersect_info.type1 = type1;
4810   intersect_info.type2 = type2;
4811   intersect_info.func = func;
4812
4813   g_array_append_val (gst_value_intersect_funcs, intersect_info);
4814 }
4815
4816
4817 /* subtraction */
4818
4819 /**
4820  * gst_value_subtract:
4821  * @dest: (out caller-allocates): the destination value for the result if the
4822  *     subtraction is not empty. May be %NULL, in which case the resulting set
4823  *     will not be computed, which can give a fair speedup.
4824  * @minuend: the value to subtract from
4825  * @subtrahend: the value to subtract
4826  *
4827  * Subtracts @subtrahend from @minuend and stores the result in @dest.
4828  * Note that this means subtraction as in sets, not as in mathematics.
4829  *
4830  * Returns: %TRUE if the subtraction is not empty
4831  */
4832 gboolean
4833 gst_value_subtract (GValue * dest, const GValue * minuend,
4834     const GValue * subtrahend)
4835 {
4836   GstValueSubtractInfo *info;
4837   guint i, len;
4838   GType ltype, mtype, stype;
4839
4840   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4841   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4842
4843   ltype = gst_value_list_get_type ();
4844
4845   /* special cases first */
4846   if (G_VALUE_HOLDS (minuend, ltype))
4847     return gst_value_subtract_from_list (dest, minuend, subtrahend);
4848   if (G_VALUE_HOLDS (subtrahend, ltype))
4849     return gst_value_subtract_list (dest, minuend, subtrahend);
4850
4851   mtype = G_VALUE_TYPE (minuend);
4852   stype = G_VALUE_TYPE (subtrahend);
4853
4854   len = gst_value_subtract_funcs->len;
4855   for (i = 0; i < len; i++) {
4856     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4857     if (info->minuend == mtype && info->subtrahend == stype) {
4858       return info->func (dest, minuend, subtrahend);
4859     }
4860   }
4861
4862   if (_gst_value_compare_nolist (minuend, subtrahend) != GST_VALUE_EQUAL) {
4863     if (dest)
4864       gst_value_init_and_copy (dest, minuend);
4865     return TRUE;
4866   }
4867
4868   return FALSE;
4869 }
4870
4871 #if 0
4872 gboolean
4873 gst_value_subtract (GValue * dest, const GValue * minuend,
4874     const GValue * subtrahend)
4875 {
4876   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
4877
4878   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
4879       gst_value_serialize (subtrahend),
4880       ret ? gst_value_serialize (dest) : "---");
4881   return ret;
4882 }
4883 #endif
4884
4885 /**
4886  * gst_value_can_subtract:
4887  * @minuend: the value to subtract from
4888  * @subtrahend: the value to subtract
4889  *
4890  * Checks if it's possible to subtract @subtrahend from @minuend.
4891  *
4892  * Returns: %TRUE if a subtraction is possible
4893  */
4894 gboolean
4895 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
4896 {
4897   GstValueSubtractInfo *info;
4898   guint i, len;
4899   GType ltype, mtype, stype;
4900
4901   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4902   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4903
4904   ltype = gst_value_list_get_type ();
4905
4906   /* special cases */
4907   if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
4908     return TRUE;
4909
4910   mtype = G_VALUE_TYPE (minuend);
4911   stype = G_VALUE_TYPE (subtrahend);
4912
4913   len = gst_value_subtract_funcs->len;
4914   for (i = 0; i < len; i++) {
4915     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4916     if (info->minuend == mtype && info->subtrahend == stype)
4917       return TRUE;
4918   }
4919
4920   return gst_value_can_compare (minuend, subtrahend);
4921 }
4922
4923 /* gst_value_register_subtract_func: (skip)
4924  * @minuend_type: type of the minuend
4925  * @subtrahend_type: type of the subtrahend
4926  * @func: function to use
4927  *
4928  * Registers @func as a function capable of subtracting the values of
4929  * @subtrahend_type from values of @minuend_type.
4930  *
4931  * Subtract functions should be registered at startup before any pipelines are
4932  * started, as gst_value_register_subtract_func() is not thread-safe and
4933  * cannot be used at the same time as gst_value_subtract().
4934  */
4935 static void
4936 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
4937     GstValueSubtractFunc func)
4938 {
4939   GstValueSubtractInfo info;
4940
4941   g_return_if_fail (!gst_type_is_fixed (minuend_type)
4942       || !gst_type_is_fixed (subtrahend_type));
4943
4944   info.minuend = minuend_type;
4945   info.subtrahend = subtrahend_type;
4946   info.func = func;
4947
4948   g_array_append_val (gst_value_subtract_funcs, info);
4949 }
4950
4951 /**
4952  * gst_value_register:
4953  * @table: structure containing functions to register
4954  *
4955  * Registers functions to perform calculations on #GValue items of a given
4956  * type. Each type can only be added once.
4957  */
4958 void
4959 gst_value_register (const GstValueTable * table)
4960 {
4961   GstValueTable *found;
4962
4963   g_return_if_fail (table != NULL);
4964
4965   g_array_append_val (gst_value_table, *table);
4966
4967   found = gst_value_hash_lookup_type (table->type);
4968   if (found)
4969     g_warning ("adding type %s multiple times", g_type_name (table->type));
4970
4971   /* FIXME: we're not really doing the const justice, we assume the table is
4972    * static */
4973   gst_value_hash_add_type (table->type, table);
4974 }
4975
4976 /**
4977  * gst_value_init_and_copy:
4978  * @dest: (out caller-allocates): the target value
4979  * @src: the source value
4980  *
4981  * Initialises the target value to be of the same type as source and then copies
4982  * the contents from source to target.
4983  */
4984 void
4985 gst_value_init_and_copy (GValue * dest, const GValue * src)
4986 {
4987   g_return_if_fail (G_IS_VALUE (src));
4988   g_return_if_fail (dest != NULL);
4989
4990   g_value_init (dest, G_VALUE_TYPE (src));
4991   g_value_copy (src, dest);
4992 }
4993
4994 /* move src into dest and clear src */
4995 static void
4996 gst_value_move (GValue * dest, GValue * src)
4997 {
4998   g_assert (G_IS_VALUE (src));
4999   g_assert (dest != NULL);
5000
5001   *dest = *src;
5002   memset (src, 0, sizeof (GValue));
5003 }
5004
5005 /**
5006  * gst_value_serialize:
5007  * @value: a #GValue to serialize
5008  *
5009  * tries to transform the given @value into a string representation that allows
5010  * getting back this string later on using gst_value_deserialize().
5011  *
5012  * Free-function: g_free
5013  *
5014  * Returns: (transfer full): the serialization for @value or %NULL if none exists
5015  */
5016 gchar *
5017 gst_value_serialize (const GValue * value)
5018 {
5019   guint i, len;
5020   GValue s_val = { 0 };
5021   GstValueTable *table, *best;
5022   gchar *s;
5023   GType type;
5024
5025   g_return_val_if_fail (G_IS_VALUE (value), NULL);
5026
5027   type = G_VALUE_TYPE (value);
5028
5029   best = gst_value_hash_lookup_type (type);
5030
5031   if (G_UNLIKELY (!best || !best->serialize)) {
5032     len = gst_value_table->len;
5033     best = NULL;
5034     for (i = 0; i < len; i++) {
5035       table = &g_array_index (gst_value_table, GstValueTable, i);
5036       if (table->serialize && g_type_is_a (type, table->type)) {
5037         if (!best || g_type_is_a (table->type, best->type))
5038           best = table;
5039       }
5040     }
5041   }
5042   if (G_LIKELY (best))
5043     return best->serialize (value);
5044
5045   g_value_init (&s_val, G_TYPE_STRING);
5046   if (g_value_transform (value, &s_val)) {
5047     s = gst_string_wrap (g_value_get_string (&s_val));
5048   } else {
5049     s = NULL;
5050   }
5051   g_value_unset (&s_val);
5052
5053   return s;
5054 }
5055
5056 /**
5057  * gst_value_deserialize:
5058  * @dest: (out caller-allocates): #GValue to fill with contents of
5059  *     deserialization
5060  * @src: string to deserialize
5061  *
5062  * Tries to deserialize a string into the type specified by the given GValue.
5063  * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
5064  *
5065  * Returns: %TRUE on success
5066  */
5067 gboolean
5068 gst_value_deserialize (GValue * dest, const gchar * src)
5069 {
5070   GstValueTable *table, *best;
5071   guint i, len;
5072   GType type;
5073
5074   g_return_val_if_fail (src != NULL, FALSE);
5075   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
5076
5077   type = G_VALUE_TYPE (dest);
5078
5079   best = gst_value_hash_lookup_type (type);
5080   if (G_UNLIKELY (!best || !best->deserialize)) {
5081     len = gst_value_table->len;
5082     best = NULL;
5083     for (i = 0; i < len; i++) {
5084       table = &g_array_index (gst_value_table, GstValueTable, i);
5085       if (table->deserialize && g_type_is_a (type, table->type)) {
5086         if (!best || g_type_is_a (table->type, best->type))
5087           best = table;
5088       }
5089     }
5090   }
5091   if (G_LIKELY (best))
5092     return best->deserialize (dest, src);
5093
5094   return FALSE;
5095 }
5096
5097 /**
5098  * gst_value_is_fixed:
5099  * @value: the #GValue to check
5100  *
5101  * Tests if the given GValue, if available in a GstStructure (or any other
5102  * container) contains a "fixed" (which means: one value) or an "unfixed"
5103  * (which means: multiple possible values, such as data lists or data
5104  * ranges) value.
5105  *
5106  * Returns: true if the value is "fixed".
5107  */
5108
5109 gboolean
5110 gst_value_is_fixed (const GValue * value)
5111 {
5112   GType type;
5113
5114   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
5115
5116   type = G_VALUE_TYPE (value);
5117
5118   /* the most common types are just basic plain glib types */
5119   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
5120     return TRUE;
5121   }
5122
5123   if (type == GST_TYPE_ARRAY) {
5124     gint size, n;
5125     const GValue *kid;
5126
5127     /* check recursively */
5128     size = gst_value_array_get_size (value);
5129     for (n = 0; n < size; n++) {
5130       kid = gst_value_array_get_value (value, n);
5131       if (!gst_value_is_fixed (kid))
5132         return FALSE;
5133     }
5134     return TRUE;
5135   }
5136   return gst_type_is_fixed (type);
5137 }
5138
5139 /**
5140  * gst_value_fixate:
5141  * @dest: the #GValue destination
5142  * @src: the #GValue to fixate
5143  *
5144  * Fixate @src into a new value @dest.
5145  * For ranges, the first element is taken. For lists and arrays, the
5146  * first item is fixated and returned.
5147  * If @src is already fixed, this function returns %FALSE.
5148  *
5149  * Returns: %TRUE if @dest contains a fixated version of @src.
5150  */
5151 gboolean
5152 gst_value_fixate (GValue * dest, const GValue * src)
5153 {
5154   g_return_val_if_fail (G_IS_VALUE (src), FALSE);
5155   g_return_val_if_fail (dest != NULL, FALSE);
5156
5157   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
5158     g_value_init (dest, G_TYPE_INT);
5159     g_value_set_int (dest, gst_value_get_int_range_min (src));
5160   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
5161     g_value_init (dest, G_TYPE_DOUBLE);
5162     g_value_set_double (dest, gst_value_get_double_range_min (src));
5163   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
5164     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
5165   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
5166     GValue temp = { 0 };
5167
5168     /* list could be empty */
5169     if (gst_value_list_get_size (src) <= 0)
5170       return FALSE;
5171
5172     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
5173
5174     if (!gst_value_fixate (dest, &temp)) {
5175       gst_value_move (dest, &temp);
5176     } else {
5177       g_value_unset (&temp);
5178     }
5179   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
5180     gboolean res = FALSE;
5181     guint n, len;
5182
5183     len = gst_value_array_get_size (src);
5184     g_value_init (dest, GST_TYPE_ARRAY);
5185     for (n = 0; n < len; n++) {
5186       GValue kid = { 0 };
5187       const GValue *orig_kid = gst_value_array_get_value (src, n);
5188
5189       if (!gst_value_fixate (&kid, orig_kid))
5190         gst_value_init_and_copy (&kid, orig_kid);
5191       else
5192         res = TRUE;
5193       _gst_value_array_append_and_take_value (dest, &kid);
5194     }
5195
5196     if (!res)
5197       g_value_unset (dest);
5198
5199     return res;
5200   } else {
5201     return FALSE;
5202   }
5203   return TRUE;
5204 }
5205
5206
5207 /************
5208  * fraction *
5209  ************/
5210
5211 /* helper functions */
5212 static void
5213 gst_value_init_fraction (GValue * value)
5214 {
5215   value->data[0].v_int = 0;
5216   value->data[1].v_int = 1;
5217 }
5218
5219 static void
5220 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
5221 {
5222   dest_value->data[0].v_int = src_value->data[0].v_int;
5223   dest_value->data[1].v_int = src_value->data[1].v_int;
5224 }
5225
5226 static gchar *
5227 gst_value_collect_fraction (GValue * value, guint n_collect_values,
5228     GTypeCValue * collect_values, guint collect_flags)
5229 {
5230   if (n_collect_values != 2)
5231     return g_strdup_printf ("not enough value locations for `%s' passed",
5232         G_VALUE_TYPE_NAME (value));
5233   if (collect_values[1].v_int == 0)
5234     return g_strdup_printf ("passed '0' as denominator for `%s'",
5235         G_VALUE_TYPE_NAME (value));
5236   if (collect_values[0].v_int < -G_MAXINT)
5237     return
5238         g_strdup_printf
5239         ("passed value smaller than -G_MAXINT as numerator for `%s'",
5240         G_VALUE_TYPE_NAME (value));
5241   if (collect_values[1].v_int < -G_MAXINT)
5242     return
5243         g_strdup_printf
5244         ("passed value smaller than -G_MAXINT as denominator for `%s'",
5245         G_VALUE_TYPE_NAME (value));
5246
5247   gst_value_set_fraction (value,
5248       collect_values[0].v_int, collect_values[1].v_int);
5249
5250   return NULL;
5251 }
5252
5253 static gchar *
5254 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
5255     GTypeCValue * collect_values, guint collect_flags)
5256 {
5257   gint *numerator = collect_values[0].v_pointer;
5258   gint *denominator = collect_values[1].v_pointer;
5259
5260   if (!numerator)
5261     return g_strdup_printf ("numerator for `%s' passed as NULL",
5262         G_VALUE_TYPE_NAME (value));
5263   if (!denominator)
5264     return g_strdup_printf ("denominator for `%s' passed as NULL",
5265         G_VALUE_TYPE_NAME (value));
5266
5267   *numerator = value->data[0].v_int;
5268   *denominator = value->data[1].v_int;
5269
5270   return NULL;
5271 }
5272
5273 /**
5274  * gst_value_set_fraction:
5275  * @value: a GValue initialized to #GST_TYPE_FRACTION
5276  * @numerator: the numerator of the fraction
5277  * @denominator: the denominator of the fraction
5278  *
5279  * Sets @value to the fraction specified by @numerator over @denominator.
5280  * The fraction gets reduced to the smallest numerator and denominator,
5281  * and if necessary the sign is moved to the numerator.
5282  */
5283 void
5284 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
5285 {
5286   gint gcd = 0;
5287
5288   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
5289   g_return_if_fail (denominator != 0);
5290   g_return_if_fail (denominator >= -G_MAXINT);
5291   g_return_if_fail (numerator >= -G_MAXINT);
5292
5293   /* normalize sign */
5294   if (denominator < 0) {
5295     numerator = -numerator;
5296     denominator = -denominator;
5297   }
5298
5299   /* check for reduction */
5300   gcd = gst_util_greatest_common_divisor (numerator, denominator);
5301   if (gcd) {
5302     numerator /= gcd;
5303     denominator /= gcd;
5304   }
5305
5306   g_assert (denominator > 0);
5307
5308   value->data[0].v_int = numerator;
5309   value->data[1].v_int = denominator;
5310 }
5311
5312 /**
5313  * gst_value_get_fraction_numerator:
5314  * @value: a GValue initialized to #GST_TYPE_FRACTION
5315  *
5316  * Gets the numerator of the fraction specified by @value.
5317  *
5318  * Returns: the numerator of the fraction.
5319  */
5320 gint
5321 gst_value_get_fraction_numerator (const GValue * value)
5322 {
5323   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
5324
5325   return value->data[0].v_int;
5326 }
5327
5328 /**
5329  * gst_value_get_fraction_denominator:
5330  * @value: a GValue initialized to #GST_TYPE_FRACTION
5331  *
5332  * Gets the denominator of the fraction specified by @value.
5333  *
5334  * Returns: the denominator of the fraction.
5335  */
5336 gint
5337 gst_value_get_fraction_denominator (const GValue * value)
5338 {
5339   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
5340
5341   return value->data[1].v_int;
5342 }
5343
5344 /**
5345  * gst_value_fraction_multiply:
5346  * @product: a GValue initialized to #GST_TYPE_FRACTION
5347  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
5348  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
5349  *
5350  * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
5351  * @product to the product of the two fractions.
5352  *
5353  * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
5354  */
5355 gboolean
5356 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
5357     const GValue * factor2)
5358 {
5359   gint n1, n2, d1, d2;
5360   gint res_n, res_d;
5361
5362   g_return_val_if_fail (product != NULL, FALSE);
5363   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
5364   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
5365
5366   n1 = factor1->data[0].v_int;
5367   n2 = factor2->data[0].v_int;
5368   d1 = factor1->data[1].v_int;
5369   d2 = factor2->data[1].v_int;
5370
5371   if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
5372     return FALSE;
5373
5374   gst_value_set_fraction (product, res_n, res_d);
5375
5376   return TRUE;
5377 }
5378
5379 /**
5380  * gst_value_fraction_subtract:
5381  * @dest: a GValue initialized to #GST_TYPE_FRACTION
5382  * @minuend: a GValue initialized to #GST_TYPE_FRACTION
5383  * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
5384  *
5385  * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
5386  *
5387  * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
5388  */
5389 gboolean
5390 gst_value_fraction_subtract (GValue * dest,
5391     const GValue * minuend, const GValue * subtrahend)
5392 {
5393   gint n1, n2, d1, d2;
5394   gint res_n, res_d;
5395
5396   g_return_val_if_fail (dest != NULL, FALSE);
5397   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
5398   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
5399
5400   n1 = minuend->data[0].v_int;
5401   n2 = subtrahend->data[0].v_int;
5402   d1 = minuend->data[1].v_int;
5403   d2 = subtrahend->data[1].v_int;
5404
5405   if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
5406     return FALSE;
5407   gst_value_set_fraction (dest, res_n, res_d);
5408
5409   return TRUE;
5410 }
5411
5412 static gchar *
5413 gst_value_serialize_fraction (const GValue * value)
5414 {
5415   gint32 numerator = value->data[0].v_int;
5416   gint32 denominator = value->data[1].v_int;
5417   gboolean positive = TRUE;
5418
5419   /* get the sign and make components absolute */
5420   if (numerator < 0) {
5421     numerator = -numerator;
5422     positive = !positive;
5423   }
5424   if (denominator < 0) {
5425     denominator = -denominator;
5426     positive = !positive;
5427   }
5428
5429   return g_strdup_printf ("%s%d/%d",
5430       positive ? "" : "-", numerator, denominator);
5431 }
5432
5433 static gboolean
5434 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
5435 {
5436   gint num, den;
5437   gint num_chars;
5438
5439   if (G_UNLIKELY (s == NULL))
5440     return FALSE;
5441
5442   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
5443     return FALSE;
5444
5445   if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
5446     if (s[num_chars] != 0)
5447       return FALSE;
5448     if (den == 0)
5449       return FALSE;
5450
5451     gst_value_set_fraction (dest, num, den);
5452     return TRUE;
5453   } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
5454     gst_value_set_fraction (dest, 1, G_MAXINT);
5455     return TRUE;
5456   } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
5457     if (s[num_chars] != 0)
5458       return FALSE;
5459     gst_value_set_fraction (dest, num, 1);
5460     return TRUE;
5461   } else if (g_ascii_strcasecmp (s, "min") == 0) {
5462     gst_value_set_fraction (dest, -G_MAXINT, 1);
5463     return TRUE;
5464   } else if (g_ascii_strcasecmp (s, "max") == 0) {
5465     gst_value_set_fraction (dest, G_MAXINT, 1);
5466     return TRUE;
5467   }
5468
5469   return FALSE;
5470 }
5471
5472 static void
5473 gst_value_transform_fraction_string (const GValue * src_value,
5474     GValue * dest_value)
5475 {
5476   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
5477 }
5478
5479 static void
5480 gst_value_transform_string_fraction (const GValue * src_value,
5481     GValue * dest_value)
5482 {
5483   if (!gst_value_deserialize_fraction (dest_value,
5484           src_value->data[0].v_pointer))
5485     /* If the deserialize fails, ensure we leave the fraction in a
5486      * valid, if incorrect, state */
5487     gst_value_set_fraction (dest_value, 0, 1);
5488 }
5489
5490 static void
5491 gst_value_transform_double_fraction (const GValue * src_value,
5492     GValue * dest_value)
5493 {
5494   gdouble src = g_value_get_double (src_value);
5495   gint n, d;
5496
5497   gst_util_double_to_fraction (src, &n, &d);
5498   gst_value_set_fraction (dest_value, n, d);
5499 }
5500
5501 static void
5502 gst_value_transform_float_fraction (const GValue * src_value,
5503     GValue * dest_value)
5504 {
5505   gfloat src = g_value_get_float (src_value);
5506   gint n, d;
5507
5508   gst_util_double_to_fraction (src, &n, &d);
5509   gst_value_set_fraction (dest_value, n, d);
5510 }
5511
5512 static void
5513 gst_value_transform_fraction_double (const GValue * src_value,
5514     GValue * dest_value)
5515 {
5516   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
5517       ((double) src_value->data[1].v_int);
5518 }
5519
5520 static void
5521 gst_value_transform_fraction_float (const GValue * src_value,
5522     GValue * dest_value)
5523 {
5524   dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
5525       ((float) src_value->data[1].v_int);
5526 }
5527
5528 static gint
5529 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
5530 {
5531   gint n1, n2;
5532   gint d1, d2;
5533   gint ret;
5534
5535   n1 = value1->data[0].v_int;
5536   n2 = value2->data[0].v_int;
5537   d1 = value1->data[1].v_int;
5538   d2 = value2->data[1].v_int;
5539
5540   /* fractions are reduced when set, so we can quickly see if they're equal */
5541   if (n1 == n2 && d1 == d2)
5542     return GST_VALUE_EQUAL;
5543
5544   if (d1 == 0 && d2 == 0)
5545     return GST_VALUE_UNORDERED;
5546   else if (d1 == 0)
5547     return GST_VALUE_GREATER_THAN;
5548   else if (d2 == 0)
5549     return GST_VALUE_LESS_THAN;
5550
5551   ret = gst_util_fraction_compare (n1, d1, n2, d2);
5552   if (ret == -1)
5553     return GST_VALUE_LESS_THAN;
5554   else if (ret == 1)
5555     return GST_VALUE_GREATER_THAN;
5556
5557   /* Equality can't happen here because we check for that
5558    * first already */
5559   g_return_val_if_reached (GST_VALUE_UNORDERED);
5560 }
5561
5562 /*********
5563  * GDate *
5564  *********/
5565
5566 static gint
5567 gst_value_compare_date (const GValue * value1, const GValue * value2)
5568 {
5569   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
5570   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
5571   guint32 j1, j2;
5572
5573   if (date1 == date2)
5574     return GST_VALUE_EQUAL;
5575
5576   if ((date1 == NULL || !g_date_valid (date1))
5577       && (date2 != NULL && g_date_valid (date2))) {
5578     return GST_VALUE_LESS_THAN;
5579   }
5580
5581   if ((date2 == NULL || !g_date_valid (date2))
5582       && (date1 != NULL && g_date_valid (date1))) {
5583     return GST_VALUE_GREATER_THAN;
5584   }
5585
5586   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
5587       || !g_date_valid (date2)) {
5588     return GST_VALUE_UNORDERED;
5589   }
5590
5591   j1 = g_date_get_julian (date1);
5592   j2 = g_date_get_julian (date2);
5593
5594   if (j1 == j2)
5595     return GST_VALUE_EQUAL;
5596   else if (j1 < j2)
5597     return GST_VALUE_LESS_THAN;
5598   else
5599     return GST_VALUE_GREATER_THAN;
5600 }
5601
5602 static gchar *
5603 gst_value_serialize_date (const GValue * val)
5604 {
5605   const GDate *date = (const GDate *) g_value_get_boxed (val);
5606
5607   if (date == NULL || !g_date_valid (date))
5608     return g_strdup ("9999-99-99");
5609
5610   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
5611       g_date_get_month (date), g_date_get_day (date));
5612 }
5613
5614 static gboolean
5615 gst_value_deserialize_date (GValue * dest, const gchar * s)
5616 {
5617   guint year, month, day;
5618
5619   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
5620     return FALSE;
5621
5622   if (!g_date_valid_dmy (day, month, year))
5623     return FALSE;
5624
5625   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
5626   return TRUE;
5627 }
5628
5629 /*************
5630  * GstDateTime *
5631  *************/
5632
5633 static gint
5634 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
5635 {
5636   const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
5637   const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
5638
5639   if (date1 == date2)
5640     return GST_VALUE_EQUAL;
5641
5642   if ((date1 == NULL) && (date2 != NULL)) {
5643     return GST_VALUE_LESS_THAN;
5644   }
5645   if ((date2 == NULL) && (date1 != NULL)) {
5646     return GST_VALUE_LESS_THAN;
5647   }
5648
5649   /* returns GST_VALUE_* */
5650   return __gst_date_time_compare (date1, date2);
5651 }
5652
5653 static gchar *
5654 gst_value_serialize_date_time (const GValue * val)
5655 {
5656   GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
5657
5658   if (date == NULL)
5659     return g_strdup ("null");
5660
5661   return __gst_date_time_serialize (date, TRUE);
5662 }
5663
5664 static gboolean
5665 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
5666 {
5667   GstDateTime *datetime;
5668
5669   if (!s || strcmp (s, "null") == 0) {
5670     return FALSE;
5671   }
5672
5673   datetime = gst_date_time_new_from_iso8601_string (s);
5674   if (datetime != NULL) {
5675     g_value_take_boxed (dest, datetime);
5676     return TRUE;
5677   }
5678   GST_WARNING ("Failed to deserialize date time string '%s'", s);
5679   return FALSE;
5680 }
5681
5682 static void
5683 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
5684 {
5685   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
5686 }
5687
5688 static void
5689 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
5690 {
5691   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
5692 }
5693
5694
5695 /************
5696  * bitmask *
5697  ************/
5698
5699 /* helper functions */
5700 static void
5701 gst_value_init_bitmask (GValue * value)
5702 {
5703   value->data[0].v_uint64 = 0;
5704 }
5705
5706 static void
5707 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
5708 {
5709   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5710 }
5711
5712 static gchar *
5713 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
5714     GTypeCValue * collect_values, guint collect_flags)
5715 {
5716   if (n_collect_values != 1)
5717     return g_strdup_printf ("not enough value locations for `%s' passed",
5718         G_VALUE_TYPE_NAME (value));
5719
5720   gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
5721
5722   return NULL;
5723 }
5724
5725 static gchar *
5726 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
5727     GTypeCValue * collect_values, guint collect_flags)
5728 {
5729   guint64 *bitmask = collect_values[0].v_pointer;
5730
5731   if (!bitmask)
5732     return g_strdup_printf ("value for `%s' passed as NULL",
5733         G_VALUE_TYPE_NAME (value));
5734
5735   *bitmask = value->data[0].v_uint64;
5736
5737   return NULL;
5738 }
5739
5740 /**
5741  * gst_value_set_bitmask:
5742  * @value: a GValue initialized to #GST_TYPE_BITMASK
5743  * @bitmask: the bitmask
5744  *
5745  * Sets @value to the bitmask specified by @bitmask.
5746  */
5747 void
5748 gst_value_set_bitmask (GValue * value, guint64 bitmask)
5749 {
5750   g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
5751
5752   value->data[0].v_uint64 = bitmask;
5753 }
5754
5755 /**
5756  * gst_value_get_bitmask:
5757  * @value: a GValue initialized to #GST_TYPE_BITMASK
5758  *
5759  * Gets the bitmask specified by @value.
5760  *
5761  * Returns: the bitmask.
5762  */
5763 guint64
5764 gst_value_get_bitmask (const GValue * value)
5765 {
5766   g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
5767
5768   return value->data[0].v_uint64;
5769 }
5770
5771 static gchar *
5772 gst_value_serialize_bitmask (const GValue * value)
5773 {
5774   guint64 bitmask = value->data[0].v_uint64;
5775
5776   return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
5777 }
5778
5779 static gboolean
5780 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
5781 {
5782   gchar *endptr = NULL;
5783   guint64 val;
5784
5785   if (G_UNLIKELY (s == NULL))
5786     return FALSE;
5787
5788   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
5789     return FALSE;
5790
5791   val = g_ascii_strtoull (s, &endptr, 16);
5792   if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
5793     return FALSE;
5794   if (val == 0 && endptr == s)
5795     return FALSE;
5796
5797   gst_value_set_bitmask (dest, val);
5798
5799   return TRUE;
5800 }
5801
5802 static void
5803 gst_value_transform_bitmask_string (const GValue * src_value,
5804     GValue * dest_value)
5805 {
5806   dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
5807 }
5808
5809 static void
5810 gst_value_transform_string_bitmask (const GValue * src_value,
5811     GValue * dest_value)
5812 {
5813   if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
5814     gst_value_set_bitmask (dest_value, 0);
5815 }
5816
5817 static void
5818 gst_value_transform_uint64_bitmask (const GValue * src_value,
5819     GValue * dest_value)
5820 {
5821   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5822 }
5823
5824 static void
5825 gst_value_transform_bitmask_uint64 (const GValue * src_value,
5826     GValue * dest_value)
5827 {
5828   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5829 }
5830
5831 static gint
5832 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
5833 {
5834   guint64 v1, v2;
5835
5836   v1 = value1->data[0].v_uint64;
5837   v2 = value2->data[0].v_uint64;
5838
5839   if (v1 == v2)
5840     return GST_VALUE_EQUAL;
5841
5842   return GST_VALUE_UNORDERED;
5843 }
5844
5845
5846 /***********************
5847  * GstAllocationParams *
5848  ***********************/
5849 static gint
5850 gst_value_compare_allocation_params (const GValue * value1,
5851     const GValue * value2)
5852 {
5853   GstAllocationParams *v1, *v2;
5854
5855   v1 = value1->data[0].v_pointer;
5856   v2 = value2->data[0].v_pointer;
5857
5858   if (v1 == NULL && v1 == v2)
5859     return GST_VALUE_EQUAL;
5860
5861   if (v1 == NULL || v2 == NULL)
5862     return GST_VALUE_UNORDERED;
5863
5864   if (v1->flags == v2->flags && v1->align == v2->align &&
5865       v1->prefix == v2->prefix && v1->padding == v2->padding)
5866     return GST_VALUE_EQUAL;
5867
5868   return GST_VALUE_UNORDERED;
5869 }
5870
5871
5872 /************
5873  * GObject *
5874  ************/
5875
5876 static gint
5877 gst_value_compare_object (const GValue * value1, const GValue * value2)
5878 {
5879   gpointer v1, v2;
5880
5881   v1 = value1->data[0].v_pointer;
5882   v2 = value2->data[0].v_pointer;
5883
5884   if (v1 == v2)
5885     return GST_VALUE_EQUAL;
5886
5887   return GST_VALUE_UNORDERED;
5888 }
5889
5890 static void
5891 gst_value_transform_object_string (const GValue * src_value,
5892     GValue * dest_value)
5893 {
5894   GstObject *obj;
5895   gchar *str;
5896
5897   obj = g_value_get_object (src_value);
5898   if (obj) {
5899     str =
5900         g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
5901         GST_OBJECT_NAME (obj));
5902   } else {
5903     str = g_strdup ("NULL");
5904   }
5905
5906   dest_value->data[0].v_pointer = str;
5907 }
5908
5909 static GTypeInfo _info = {
5910   0,
5911   NULL,
5912   NULL,
5913   NULL,
5914   NULL,
5915   NULL,
5916   0,
5917   0,
5918   NULL,
5919   NULL,
5920 };
5921
5922 static GTypeFundamentalInfo _finfo = {
5923   0
5924 };
5925
5926 #define FUNC_VALUE_GET_TYPE(type, name)                         \
5927 GType _gst_ ## type ## _type = 0;                               \
5928                                                                 \
5929 GType gst_ ## type ## _get_type (void)                          \
5930 {                                                               \
5931   static volatile GType gst_ ## type ## _type = 0;              \
5932                                                                 \
5933   if (g_once_init_enter (&gst_ ## type ## _type)) {             \
5934     GType _type;                                                \
5935     _info.value_table = & _gst_ ## type ## _value_table;        \
5936     _type = g_type_register_fundamental (                       \
5937         g_type_fundamental_next (),                             \
5938         name, &_info, &_finfo, 0);                              \
5939     _gst_ ## type ## _type = _type;                              \
5940     g_once_init_leave(&gst_ ## type ## _type, _type);           \
5941   }                                                             \
5942                                                                 \
5943   return gst_ ## type ## _type;                                 \
5944 }
5945
5946 static const GTypeValueTable _gst_int_range_value_table = {
5947   gst_value_init_int_range,
5948   NULL,
5949   gst_value_copy_int_range,
5950   NULL,
5951   (char *) "ii",
5952   gst_value_collect_int_range,
5953   (char *) "pp",
5954   gst_value_lcopy_int_range
5955 };
5956
5957 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
5958
5959 static const GTypeValueTable _gst_int64_range_value_table = {
5960   gst_value_init_int64_range,
5961   gst_value_free_int64_range,
5962   gst_value_copy_int64_range,
5963   NULL,
5964   (char *) "qq",
5965   gst_value_collect_int64_range,
5966   (char *) "pp",
5967   gst_value_lcopy_int64_range
5968 };
5969
5970 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
5971
5972 static const GTypeValueTable _gst_double_range_value_table = {
5973   gst_value_init_double_range,
5974   NULL,
5975   gst_value_copy_double_range,
5976   NULL,
5977   (char *) "dd",
5978   gst_value_collect_double_range,
5979   (char *) "pp",
5980   gst_value_lcopy_double_range
5981 };
5982
5983 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
5984
5985 static const GTypeValueTable _gst_fraction_range_value_table = {
5986   gst_value_init_fraction_range,
5987   gst_value_free_fraction_range,
5988   gst_value_copy_fraction_range,
5989   NULL,
5990   (char *) "iiii",
5991   gst_value_collect_fraction_range,
5992   (char *) "pppp",
5993   gst_value_lcopy_fraction_range
5994 };
5995
5996 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
5997
5998 static const GTypeValueTable _gst_value_list_value_table = {
5999   gst_value_init_list_or_array,
6000   gst_value_free_list_or_array,
6001   gst_value_copy_list_or_array,
6002   gst_value_list_or_array_peek_pointer,
6003   (char *) "p",
6004   gst_value_collect_list_or_array,
6005   (char *) "p",
6006   gst_value_lcopy_list_or_array
6007 };
6008
6009 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
6010
6011 static const GTypeValueTable _gst_value_array_value_table = {
6012   gst_value_init_list_or_array,
6013   gst_value_free_list_or_array,
6014   gst_value_copy_list_or_array,
6015   gst_value_list_or_array_peek_pointer,
6016   (char *) "p",
6017   gst_value_collect_list_or_array,
6018   (char *) "p",
6019   gst_value_lcopy_list_or_array
6020 };
6021
6022 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
6023
6024 static const GTypeValueTable _gst_fraction_value_table = {
6025   gst_value_init_fraction,
6026   NULL,
6027   gst_value_copy_fraction,
6028   NULL,
6029   (char *) "ii",
6030   gst_value_collect_fraction,
6031   (char *) "pp",
6032   gst_value_lcopy_fraction
6033 };
6034
6035 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
6036
6037 static const GTypeValueTable _gst_bitmask_value_table = {
6038   gst_value_init_bitmask,
6039   NULL,
6040   gst_value_copy_bitmask,
6041   NULL,
6042   (char *) "q",
6043   gst_value_collect_bitmask,
6044   (char *) "p",
6045   gst_value_lcopy_bitmask
6046 };
6047
6048 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
6049
6050 GType
6051 gst_g_thread_get_type (void)
6052 {
6053 #if GLIB_CHECK_VERSION(2,35,3)
6054   return G_TYPE_THREAD;
6055 #else
6056   static volatile gsize type_id = 0;
6057
6058   if (g_once_init_enter (&type_id)) {
6059     GType tmp =
6060         g_boxed_type_register_static (g_intern_static_string ("GstGThread"),
6061         (GBoxedCopyFunc) g_thread_ref,
6062         (GBoxedFreeFunc) g_thread_unref);
6063     g_once_init_leave (&type_id, tmp);
6064   }
6065
6066   return type_id;
6067 #endif
6068 }
6069
6070 void
6071 _priv_gst_value_initialize (void)
6072 {
6073   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
6074   gst_value_hash = g_hash_table_new (NULL, NULL);
6075   gst_value_union_funcs = g_array_new (FALSE, FALSE,
6076       sizeof (GstValueUnionInfo));
6077   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
6078       sizeof (GstValueIntersectInfo));
6079   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
6080       sizeof (GstValueSubtractInfo));
6081
6082   {
6083     static GstValueTable gst_value = {
6084       0,
6085       gst_value_compare_int_range,
6086       gst_value_serialize_int_range,
6087       gst_value_deserialize_int_range,
6088     };
6089
6090     gst_value.type = gst_int_range_get_type ();
6091     gst_value_register (&gst_value);
6092   }
6093
6094   {
6095     static GstValueTable gst_value = {
6096       0,
6097       gst_value_compare_int64_range,
6098       gst_value_serialize_int64_range,
6099       gst_value_deserialize_int64_range,
6100     };
6101
6102     gst_value.type = gst_int64_range_get_type ();
6103     gst_value_register (&gst_value);
6104   }
6105
6106   {
6107     static GstValueTable gst_value = {
6108       0,
6109       gst_value_compare_double_range,
6110       gst_value_serialize_double_range,
6111       gst_value_deserialize_double_range,
6112     };
6113
6114     gst_value.type = gst_double_range_get_type ();
6115     gst_value_register (&gst_value);
6116   }
6117
6118   {
6119     static GstValueTable gst_value = {
6120       0,
6121       gst_value_compare_fraction_range,
6122       gst_value_serialize_fraction_range,
6123       gst_value_deserialize_fraction_range,
6124     };
6125
6126     gst_value.type = gst_fraction_range_get_type ();
6127     gst_value_register (&gst_value);
6128   }
6129
6130   {
6131     static GstValueTable gst_value = {
6132       0,
6133       gst_value_compare_list,
6134       gst_value_serialize_list,
6135       gst_value_deserialize_list,
6136     };
6137
6138     gst_value.type = gst_value_list_get_type ();
6139     gst_value_register (&gst_value);
6140   }
6141
6142   {
6143     static GstValueTable gst_value = {
6144       0,
6145       gst_value_compare_array,
6146       gst_value_serialize_array,
6147       gst_value_deserialize_array,
6148     };
6149
6150     gst_value.type = gst_value_array_get_type ();
6151     gst_value_register (&gst_value);
6152   }
6153
6154   {
6155 #if 0
6156     static const GTypeValueTable value_table = {
6157       gst_value_init_buffer,
6158       NULL,
6159       gst_value_copy_buffer,
6160       NULL,
6161       "i",
6162       NULL,                     /*gst_value_collect_buffer, */
6163       "p",
6164       NULL                      /*gst_value_lcopy_buffer */
6165     };
6166 #endif
6167     static GstValueTable gst_value = {
6168       0,
6169       gst_value_compare_buffer,
6170       gst_value_serialize_buffer,
6171       gst_value_deserialize_buffer,
6172     };
6173
6174     gst_value.type = GST_TYPE_BUFFER;
6175     gst_value_register (&gst_value);
6176   }
6177   {
6178     static GstValueTable gst_value = {
6179       0,
6180       gst_value_compare_sample,
6181       gst_value_serialize_sample,
6182       gst_value_deserialize_sample,
6183     };
6184
6185     gst_value.type = GST_TYPE_SAMPLE;
6186     gst_value_register (&gst_value);
6187   }
6188   {
6189     static GstValueTable gst_value = {
6190       0,
6191       gst_value_compare_fraction,
6192       gst_value_serialize_fraction,
6193       gst_value_deserialize_fraction,
6194     };
6195
6196     gst_value.type = gst_fraction_get_type ();
6197     gst_value_register (&gst_value);
6198   }
6199   {
6200     static GstValueTable gst_value = {
6201       0,
6202       gst_value_compare_caps,
6203       gst_value_serialize_caps,
6204       gst_value_deserialize_caps,
6205     };
6206
6207     gst_value.type = GST_TYPE_CAPS;
6208     gst_value_register (&gst_value);
6209   }
6210   {
6211     static GstValueTable gst_value = {
6212       0,
6213       NULL,
6214       gst_value_serialize_segment,
6215       gst_value_deserialize_segment,
6216     };
6217
6218     gst_value.type = GST_TYPE_SEGMENT;
6219     gst_value_register (&gst_value);
6220   }
6221   {
6222     static GstValueTable gst_value = {
6223       0,
6224       NULL,
6225       gst_value_serialize_structure,
6226       gst_value_deserialize_structure,
6227     };
6228
6229     gst_value.type = GST_TYPE_STRUCTURE;
6230     gst_value_register (&gst_value);
6231   }
6232   {
6233     static GstValueTable gst_value = {
6234       0,
6235       NULL,
6236       gst_value_serialize_caps_features,
6237       gst_value_deserialize_caps_features,
6238     };
6239
6240     gst_value.type = GST_TYPE_CAPS_FEATURES;
6241     gst_value_register (&gst_value);
6242   }
6243   {
6244     static GstValueTable gst_value = {
6245       0,
6246       NULL,
6247       gst_value_serialize_tag_list,
6248       gst_value_deserialize_tag_list,
6249     };
6250
6251     gst_value.type = GST_TYPE_TAG_LIST;
6252     gst_value_register (&gst_value);
6253   }
6254   {
6255     static GstValueTable gst_value = {
6256       0,
6257       gst_value_compare_date,
6258       gst_value_serialize_date,
6259       gst_value_deserialize_date,
6260     };
6261
6262     gst_value.type = G_TYPE_DATE;
6263     gst_value_register (&gst_value);
6264   }
6265   {
6266     static GstValueTable gst_value = {
6267       0,
6268       gst_value_compare_date_time,
6269       gst_value_serialize_date_time,
6270       gst_value_deserialize_date_time,
6271     };
6272
6273     gst_value.type = gst_date_time_get_type ();
6274     gst_value_register (&gst_value);
6275   }
6276
6277   {
6278     static GstValueTable gst_value = {
6279       0,
6280       gst_value_compare_bitmask,
6281       gst_value_serialize_bitmask,
6282       gst_value_deserialize_bitmask,
6283     };
6284
6285     gst_value.type = gst_bitmask_get_type ();
6286     gst_value_register (&gst_value);
6287   }
6288
6289   {
6290     static GstValueTable gst_value = {
6291       0,
6292       gst_value_compare_allocation_params,
6293       NULL,
6294       NULL,
6295     };
6296
6297     gst_value.type = gst_allocation_params_get_type ();
6298     gst_value_register (&gst_value);
6299   }
6300
6301   {
6302     static GstValueTable gst_value = {
6303       0,
6304       gst_value_compare_object,
6305       NULL,
6306       NULL,
6307     };
6308
6309     gst_value.type = G_TYPE_OBJECT;
6310     gst_value_register (&gst_value);
6311   }
6312
6313   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
6314   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
6315
6316   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
6317   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
6318   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
6319
6320   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
6321
6322   REGISTER_SERIALIZATION (G_TYPE_INT, int);
6323
6324   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
6325   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
6326
6327   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
6328   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
6329   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
6330
6331   REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
6332
6333   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
6334       gst_value_transform_int_range_string);
6335   g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
6336       gst_value_transform_int64_range_string);
6337   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
6338       gst_value_transform_double_range_string);
6339   g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
6340       gst_value_transform_fraction_range_string);
6341   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
6342       gst_value_transform_list_string);
6343   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
6344       gst_value_transform_array_string);
6345   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
6346       gst_value_transform_fraction_string);
6347   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
6348       gst_value_transform_string_fraction);
6349   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
6350       gst_value_transform_fraction_double);
6351   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
6352       gst_value_transform_fraction_float);
6353   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
6354       gst_value_transform_double_fraction);
6355   g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
6356       gst_value_transform_float_fraction);
6357   g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
6358       gst_value_transform_date_string);
6359   g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
6360       gst_value_transform_string_date);
6361   g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
6362       gst_value_transform_object_string);
6363   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
6364       gst_value_transform_bitmask_uint64);
6365   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
6366       gst_value_transform_bitmask_string);
6367   g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
6368       gst_value_transform_uint64_bitmask);
6369   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
6370       gst_value_transform_string_bitmask);
6371
6372   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6373       gst_value_intersect_int_int_range);
6374   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6375       gst_value_intersect_int_range_int_range);
6376   gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6377       gst_value_intersect_int64_int64_range);
6378   gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6379       gst_value_intersect_int64_range_int64_range);
6380   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6381       gst_value_intersect_double_double_range);
6382   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
6383       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
6384   gst_value_register_intersect_func (GST_TYPE_ARRAY,
6385       GST_TYPE_ARRAY, gst_value_intersect_array);
6386   gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6387       gst_value_intersect_fraction_fraction_range);
6388   gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
6389       GST_TYPE_FRACTION_RANGE,
6390       gst_value_intersect_fraction_range_fraction_range);
6391
6392   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6393       gst_value_subtract_int_int_range);
6394   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
6395       gst_value_subtract_int_range_int);
6396   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6397       gst_value_subtract_int_range_int_range);
6398   gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6399       gst_value_subtract_int64_int64_range);
6400   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
6401       gst_value_subtract_int64_range_int64);
6402   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6403       gst_value_subtract_int64_range_int64_range);
6404   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6405       gst_value_subtract_double_double_range);
6406   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
6407       gst_value_subtract_double_range_double);
6408   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
6409       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
6410   gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6411       gst_value_subtract_fraction_fraction_range);
6412   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
6413       gst_value_subtract_fraction_range_fraction);
6414   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
6415       GST_TYPE_FRACTION_RANGE,
6416       gst_value_subtract_fraction_range_fraction_range);
6417
6418   /* see bug #317246, #64994, #65041 */
6419   {
6420     volatile GType date_type = G_TYPE_DATE;
6421
6422     g_type_name (date_type);
6423   }
6424
6425   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6426       gst_value_union_int_int_range);
6427   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6428       gst_value_union_int_range_int_range);
6429
6430 #if 0
6431   /* Implement these if needed */
6432   gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6433       gst_value_union_fraction_fraction_range);
6434   gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
6435       GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
6436 #endif
6437 }