value: Add a FIXME 2.0 for a fraction ranges optimization
[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 /* FIXME 2.0: Don't leak the internal representation of fraction
1698  * ranges but instead return the numerator and denominator
1699  * separately.
1700  * This would allow to store fraction ranges as
1701  *  data[0] = (min_n << 32) | (min_d)
1702  *  data[1] = (max_n << 32) | (max_d)
1703  * without requiring an additional allocation for each value.
1704  */
1705
1706 /**
1707  * gst_value_get_fraction_range_min:
1708  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1709  *
1710  * Gets the minimum of the range specified by @value.
1711  *
1712  * Returns: the minimum of the range
1713  */
1714 const GValue *
1715 gst_value_get_fraction_range_min (const GValue * value)
1716 {
1717   GValue *vals;
1718
1719   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1720
1721   vals = (GValue *) value->data[0].v_pointer;
1722   if (vals != NULL) {
1723     return &vals[0];
1724   }
1725
1726   return NULL;
1727 }
1728
1729 /**
1730  * gst_value_get_fraction_range_max:
1731  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1732  *
1733  * Gets the maximum of the range specified by @value.
1734  *
1735  * Returns: the maximum of the range
1736  */
1737 const GValue *
1738 gst_value_get_fraction_range_max (const GValue * value)
1739 {
1740   GValue *vals;
1741
1742   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1743
1744   vals = (GValue *) value->data[0].v_pointer;
1745   if (vals != NULL) {
1746     return &vals[1];
1747   }
1748
1749   return NULL;
1750 }
1751
1752 static gchar *
1753 gst_value_serialize_fraction_range (const GValue * value)
1754 {
1755   GValue *vals = (GValue *) value->data[0].v_pointer;
1756   gchar *retval;
1757
1758   if (vals == NULL) {
1759     retval = g_strdup ("[ 0/1, 0/1 ]");
1760   } else {
1761     gchar *start, *end;
1762
1763     start = gst_value_serialize_fraction (&vals[0]);
1764     end = gst_value_serialize_fraction (&vals[1]);
1765
1766     retval = g_strdup_printf ("[ %s, %s ]", start, end);
1767     g_free (start);
1768     g_free (end);
1769   }
1770
1771   return retval;
1772 }
1773
1774 static void
1775 gst_value_transform_fraction_range_string (const GValue * src_value,
1776     GValue * dest_value)
1777 {
1778   dest_value->data[0].v_pointer =
1779       gst_value_serialize_fraction_range (src_value);
1780 }
1781
1782 static gint
1783 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1784 {
1785   GValue *vals1, *vals2;
1786   GstValueCompareFunc compare;
1787
1788   if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1789     return GST_VALUE_EQUAL;     /* Only possible if both are NULL */
1790
1791   if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1792     return GST_VALUE_UNORDERED;
1793
1794   vals1 = (GValue *) value1->data[0].v_pointer;
1795   vals2 = (GValue *) value2->data[0].v_pointer;
1796   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1797     if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1798         GST_VALUE_EQUAL &&
1799         gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1800         GST_VALUE_EQUAL)
1801       return GST_VALUE_EQUAL;
1802   }
1803   return GST_VALUE_UNORDERED;
1804 }
1805
1806 static gboolean
1807 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1808 {
1809   g_warning ("unimplemented");
1810   return FALSE;
1811 }
1812
1813 /***********
1814  * GstCaps *
1815  ***********/
1816
1817 /**
1818  * gst_value_set_caps:
1819  * @value: a GValue initialized to GST_TYPE_CAPS
1820  * @caps: (transfer none): the caps to set the value to
1821  *
1822  * Sets the contents of @value to @caps. A reference to the
1823  * provided @caps will be taken by the @value.
1824  */
1825 void
1826 gst_value_set_caps (GValue * value, const GstCaps * caps)
1827 {
1828   g_return_if_fail (G_IS_VALUE (value));
1829   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1830   g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
1831
1832   g_value_set_boxed (value, caps);
1833 }
1834
1835 /**
1836  * gst_value_get_caps:
1837  * @value: a GValue initialized to GST_TYPE_CAPS
1838  *
1839  * Gets the contents of @value. The reference count of the returned
1840  * #GstCaps will not be modified, therefore the caller must take one
1841  * before getting rid of the @value.
1842  *
1843  * Returns: (transfer none): the contents of @value
1844  */
1845 const GstCaps *
1846 gst_value_get_caps (const GValue * value)
1847 {
1848   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1849   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1850
1851   return (GstCaps *) g_value_get_boxed (value);
1852 }
1853
1854 static gint
1855 gst_value_compare_caps (const GValue * value1, const GValue * value2)
1856 {
1857   GstCaps *caps1 = GST_CAPS (gst_value_get_caps (value1));
1858   GstCaps *caps2 = GST_CAPS (gst_value_get_caps (value2));
1859
1860   if (gst_caps_is_equal (caps1, caps2))
1861     return GST_VALUE_EQUAL;
1862   return GST_VALUE_UNORDERED;
1863 }
1864
1865 static gchar *
1866 gst_value_serialize_caps (const GValue * value)
1867 {
1868   GstCaps *caps = g_value_get_boxed (value);
1869   return gst_string_take_and_wrap (gst_caps_to_string (caps));
1870 }
1871
1872 static gboolean
1873 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1874 {
1875   GstCaps *caps;
1876
1877   if (*s != '"') {
1878     caps = gst_caps_from_string (s);
1879   } else {
1880     gchar *str = gst_string_unwrap (s);
1881
1882     if (G_UNLIKELY (!str))
1883       return FALSE;
1884
1885     caps = gst_caps_from_string (str);
1886     g_free (str);
1887   }
1888
1889   if (caps) {
1890     g_value_take_boxed (dest, caps);
1891     return TRUE;
1892   }
1893   return FALSE;
1894 }
1895
1896 /**************
1897  * GstSegment *
1898  **************/
1899
1900 static gchar *
1901 gst_value_serialize_segment_internal (const GValue * value, gboolean escape)
1902 {
1903   GstSegment *seg = g_value_get_boxed (value);
1904   gchar *t, *res;
1905   GstStructure *s;
1906
1907   s = gst_structure_new ("GstSegment",
1908       "flags", GST_TYPE_SEGMENT_FLAGS, seg->flags,
1909       "rate", G_TYPE_DOUBLE, seg->rate,
1910       "applied-rate", G_TYPE_DOUBLE, seg->applied_rate,
1911       "format", GST_TYPE_FORMAT, seg->format,
1912       "base", G_TYPE_UINT64, seg->base,
1913       "offset", G_TYPE_UINT64, seg->offset,
1914       "start", G_TYPE_UINT64, seg->start,
1915       "stop", G_TYPE_UINT64, seg->stop,
1916       "time", G_TYPE_UINT64, seg->time,
1917       "position", G_TYPE_UINT64, seg->position,
1918       "duration", G_TYPE_UINT64, seg->duration, NULL);
1919   t = gst_structure_to_string (s);
1920   if (escape) {
1921     res = g_strdup_printf ("\"%s\"", t);
1922     g_free (t);
1923   } else {
1924     res = t;
1925   }
1926   gst_structure_free (s);
1927
1928   return res;
1929 }
1930
1931 static gchar *
1932 gst_value_serialize_segment (const GValue * value)
1933 {
1934   return gst_value_serialize_segment_internal (value, TRUE);
1935 }
1936
1937 static gboolean
1938 gst_value_deserialize_segment (GValue * dest, const gchar * s)
1939 {
1940   GstStructure *str;
1941   GstSegment seg;
1942   gboolean res;
1943
1944   str = gst_structure_from_string (s, NULL);
1945   if (str == NULL)
1946     return FALSE;
1947
1948   res = gst_structure_get (str,
1949       "flags", GST_TYPE_SEGMENT_FLAGS, &seg.flags,
1950       "rate", G_TYPE_DOUBLE, &seg.rate,
1951       "applied-rate", G_TYPE_DOUBLE, &seg.applied_rate,
1952       "format", GST_TYPE_FORMAT, &seg.format,
1953       "base", G_TYPE_UINT64, &seg.base,
1954       "offset", G_TYPE_UINT64, &seg.offset,
1955       "start", G_TYPE_UINT64, &seg.start,
1956       "stop", G_TYPE_UINT64, &seg.stop,
1957       "time", G_TYPE_UINT64, &seg.time,
1958       "position", G_TYPE_UINT64, &seg.position,
1959       "duration", G_TYPE_UINT64, &seg.duration, NULL);
1960   gst_structure_free (str);
1961
1962   if (res)
1963     g_value_set_boxed (dest, &seg);
1964
1965   return res;
1966 }
1967
1968 /****************
1969  * GstStructure *
1970  ****************/
1971
1972 /**
1973  * gst_value_set_structure:
1974  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1975  * @structure: the structure to set the value to
1976  *
1977  * Sets the contents of @value to @structure.  The actual
1978  */
1979 void
1980 gst_value_set_structure (GValue * value, const GstStructure * structure)
1981 {
1982   g_return_if_fail (G_IS_VALUE (value));
1983   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1984   g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
1985
1986   g_value_set_boxed (value, structure);
1987 }
1988
1989 /**
1990  * gst_value_get_structure:
1991  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1992  *
1993  * Gets the contents of @value.
1994  *
1995  * Returns: (transfer none): the contents of @value
1996  */
1997 const GstStructure *
1998 gst_value_get_structure (const GValue * value)
1999 {
2000   g_return_val_if_fail (G_IS_VALUE (value), NULL);
2001   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
2002
2003   return (GstStructure *) g_value_get_boxed (value);
2004 }
2005
2006 static gchar *
2007 gst_value_serialize_structure (const GValue * value)
2008 {
2009   GstStructure *structure = g_value_get_boxed (value);
2010
2011   return gst_string_take_and_wrap (gst_structure_to_string (structure));
2012 }
2013
2014 static gboolean
2015 gst_value_deserialize_structure (GValue * dest, const gchar * s)
2016 {
2017   GstStructure *structure;
2018
2019   if (*s != '"') {
2020     structure = gst_structure_from_string (s, NULL);
2021   } else {
2022     gchar *str = gst_string_unwrap (s);
2023
2024     if (G_UNLIKELY (!str))
2025       return FALSE;
2026
2027     structure = gst_structure_from_string (str, NULL);
2028     g_free (str);
2029   }
2030
2031   if (G_LIKELY (structure)) {
2032     g_value_take_boxed (dest, structure);
2033     return TRUE;
2034   }
2035   return FALSE;
2036 }
2037
2038 /*******************
2039  * GstCapsFeatures *
2040  *******************/
2041
2042 /**
2043  * gst_value_set_caps_features:
2044  * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
2045  * @features: the features to set the value to
2046  *
2047  * Sets the contents of @value to @features.
2048  */
2049 void
2050 gst_value_set_caps_features (GValue * value, const GstCapsFeatures * features)
2051 {
2052   g_return_if_fail (G_IS_VALUE (value));
2053   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES);
2054   g_return_if_fail (features == NULL || GST_IS_CAPS_FEATURES (features));
2055
2056   g_value_set_boxed (value, features);
2057 }
2058
2059 /**
2060  * gst_value_get_caps_features:
2061  * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
2062  *
2063  * Gets the contents of @value.
2064  *
2065  * Returns: (transfer none): the contents of @value
2066  */
2067 const GstCapsFeatures *
2068 gst_value_get_caps_features (const GValue * value)
2069 {
2070   g_return_val_if_fail (G_IS_VALUE (value), NULL);
2071   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES, NULL);
2072
2073   return (GstCapsFeatures *) g_value_get_boxed (value);
2074 }
2075
2076 static gchar *
2077 gst_value_serialize_caps_features (const GValue * value)
2078 {
2079   GstCapsFeatures *features = g_value_get_boxed (value);
2080
2081   return gst_string_take_and_wrap (gst_caps_features_to_string (features));
2082 }
2083
2084 static gboolean
2085 gst_value_deserialize_caps_features (GValue * dest, const gchar * s)
2086 {
2087   GstCapsFeatures *features;
2088
2089   if (*s != '"') {
2090     features = gst_caps_features_from_string (s);
2091   } else {
2092     gchar *str = gst_string_unwrap (s);
2093
2094     if (G_UNLIKELY (!str))
2095       return FALSE;
2096
2097     features = gst_caps_features_from_string (str);
2098     g_free (str);
2099   }
2100
2101   if (G_LIKELY (features)) {
2102     g_value_take_boxed (dest, features);
2103     return TRUE;
2104   }
2105   return FALSE;
2106 }
2107
2108 /**************
2109  * GstTagList *
2110  **************/
2111
2112 static gboolean
2113 gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
2114 {
2115   GstTagList *taglist;
2116
2117   if (*s != '"') {
2118     taglist = gst_tag_list_new_from_string (s);
2119   } else {
2120     gchar *str = gst_string_unwrap (s);
2121
2122     if (G_UNLIKELY (!str))
2123       return FALSE;
2124
2125     taglist = gst_tag_list_new_from_string (str);
2126     g_free (str);
2127   }
2128
2129   if (G_LIKELY (taglist != NULL)) {
2130     g_value_take_boxed (dest, taglist);
2131     return TRUE;
2132   }
2133   return FALSE;
2134 }
2135
2136 static gchar *
2137 gst_value_serialize_tag_list (const GValue * value)
2138 {
2139   GstTagList *taglist = g_value_get_boxed (value);
2140
2141   return gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
2142 }
2143
2144
2145 /*************
2146  * GstBuffer *
2147  *************/
2148
2149 static gint
2150 compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
2151 {
2152   gsize size1, size2;
2153   GstMapInfo info1, info2;
2154   gint result, mret;
2155
2156   if (buf1 == buf2)
2157     return GST_VALUE_EQUAL;
2158
2159   size1 = gst_buffer_get_size (buf1);
2160   size2 = gst_buffer_get_size (buf2);
2161
2162   if (size1 != size2)
2163     return GST_VALUE_UNORDERED;
2164
2165   if (size1 == 0)
2166     return GST_VALUE_EQUAL;
2167
2168   if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
2169     return GST_VALUE_UNORDERED;
2170
2171   if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
2172     gst_buffer_unmap (buf1, &info1);
2173     return GST_VALUE_UNORDERED;
2174   }
2175
2176   mret = memcmp (info1.data, info2.data, info1.size);
2177   if (mret == 0)
2178     result = GST_VALUE_EQUAL;
2179   else if (mret < 0)
2180     result = GST_VALUE_LESS_THAN;
2181   else
2182     result = GST_VALUE_GREATER_THAN;
2183
2184   gst_buffer_unmap (buf1, &info1);
2185   gst_buffer_unmap (buf2, &info2);
2186
2187   return result;
2188 }
2189
2190 static gint
2191 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
2192 {
2193   GstBuffer *buf1 = gst_value_get_buffer (value1);
2194   GstBuffer *buf2 = gst_value_get_buffer (value2);
2195
2196   return compare_buffer (buf1, buf2);
2197 }
2198
2199 static gchar *
2200 gst_value_serialize_buffer (const GValue * value)
2201 {
2202   GstMapInfo info;
2203   guint8 *data;
2204   gint i;
2205   gchar *string;
2206   GstBuffer *buffer;
2207
2208   buffer = gst_value_get_buffer (value);
2209   if (buffer == NULL)
2210     return NULL;
2211
2212   if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
2213     return NULL;
2214
2215   data = info.data;
2216
2217   string = g_malloc (info.size * 2 + 1);
2218   for (i = 0; i < info.size; i++) {
2219     sprintf (string + i * 2, "%02x", data[i]);
2220   }
2221   string[info.size * 2] = 0;
2222
2223   gst_buffer_unmap (buffer, &info);
2224
2225   return string;
2226 }
2227
2228 static gboolean
2229 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
2230 {
2231   GstBuffer *buffer;
2232   gint len;
2233   gchar ts[3];
2234   GstMapInfo info;
2235   guint8 *data;
2236   gint i;
2237
2238   len = strlen (s);
2239   if (len & 1)
2240     goto wrong_length;
2241
2242   buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
2243   if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
2244     goto map_failed;
2245   data = info.data;
2246
2247   for (i = 0; i < len / 2; i++) {
2248     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
2249       goto wrong_char;
2250
2251     ts[0] = s[i * 2 + 0];
2252     ts[1] = s[i * 2 + 1];
2253     ts[2] = 0;
2254
2255     data[i] = (guint8) strtoul (ts, NULL, 16);
2256   }
2257   gst_buffer_unmap (buffer, &info);
2258
2259   gst_value_take_buffer (dest, buffer);
2260
2261   return TRUE;
2262
2263   /* ERRORS */
2264 wrong_length:
2265   {
2266     return FALSE;
2267   }
2268 map_failed:
2269   {
2270     return FALSE;
2271   }
2272 wrong_char:
2273   {
2274     gst_buffer_unref (buffer);
2275     gst_buffer_unmap (buffer, &info);
2276     return FALSE;
2277   }
2278 }
2279
2280 /*************
2281  * GstSample *
2282  *************/
2283
2284 /* This function is mostly used for comparing image/buffer tags in taglists */
2285 static gint
2286 gst_value_compare_sample (const GValue * value1, const GValue * value2)
2287 {
2288   GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
2289   GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
2290
2291   /* FIXME: should we take into account anything else such as caps? */
2292   return compare_buffer (buf1, buf2);
2293 }
2294
2295 static gchar *
2296 gst_value_serialize_sample (const GValue * value)
2297 {
2298   const GstStructure *info_structure;
2299   GstSegment *segment;
2300   GstBuffer *buffer;
2301   GstCaps *caps;
2302   GstSample *sample;
2303   GValue val = { 0, };
2304   gchar *info_str, *caps_str, *tmp;
2305   gchar *buf_str, *seg_str, *s;
2306
2307   sample = g_value_get_boxed (value);
2308
2309   buffer = gst_sample_get_buffer (sample);
2310   if (buffer) {
2311     g_value_init (&val, GST_TYPE_BUFFER);
2312     g_value_set_boxed (&val, buffer);
2313     buf_str = gst_value_serialize_buffer (&val);
2314     g_value_unset (&val);
2315   } else {
2316     buf_str = g_strdup ("None");
2317   }
2318
2319   caps = gst_sample_get_caps (sample);
2320   if (caps) {
2321     tmp = gst_caps_to_string (caps);
2322     caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2323     g_strdelimit (caps_str, "=", '_');
2324     g_free (tmp);
2325   } else {
2326     caps_str = g_strdup ("None");
2327   }
2328
2329   segment = gst_sample_get_segment (sample);
2330   if (segment) {
2331     g_value_init (&val, GST_TYPE_SEGMENT);
2332     g_value_set_boxed (&val, segment);
2333     tmp = gst_value_serialize_segment_internal (&val, FALSE);
2334     seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2335     g_strdelimit (seg_str, "=", '_');
2336     g_free (tmp);
2337     g_value_unset (&val);
2338   } else {
2339     seg_str = g_strdup ("None");
2340   }
2341
2342   info_structure = gst_sample_get_info (sample);
2343   if (info_structure) {
2344     tmp = gst_structure_to_string (info_structure);
2345     info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2346     g_strdelimit (info_str, "=", '_');
2347     g_free (tmp);
2348   } else {
2349     info_str = g_strdup ("None");
2350   }
2351
2352   s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
2353   g_free (buf_str);
2354   g_free (caps_str);
2355   g_free (seg_str);
2356   g_free (info_str);
2357
2358   return s;
2359 }
2360
2361 static gboolean
2362 gst_value_deserialize_sample (GValue * dest, const gchar * s)
2363 {
2364   GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
2365   GstStructure *info;
2366   GstSample *sample;
2367   GstCaps *caps;
2368   gboolean ret = FALSE;
2369   gchar **fields;
2370   gsize outlen;
2371   gint len;
2372
2373   GST_TRACE ("deserialize '%s'", s);
2374
2375   fields = g_strsplit (s, ":", -1);
2376   len = g_strv_length (fields);
2377   if (len != 4)
2378     goto wrong_length;
2379
2380   g_value_init (&bval, GST_TYPE_BUFFER);
2381   g_value_init (&sval, GST_TYPE_SEGMENT);
2382
2383   if (!gst_value_deserialize_buffer (&bval, fields[0]))
2384     goto fail;
2385
2386   if (strcmp (fields[1], "None") != 0) {
2387     g_strdelimit (fields[1], "_", '=');
2388     g_base64_decode_inplace (fields[1], &outlen);
2389     GST_TRACE ("caps    : %s", fields[1]);
2390     caps = gst_caps_from_string (fields[1]);
2391     if (caps == NULL)
2392       goto fail;
2393   } else {
2394     caps = NULL;
2395   }
2396
2397   if (strcmp (fields[2], "None") != 0) {
2398     g_strdelimit (fields[2], "_", '=');
2399     g_base64_decode_inplace (fields[2], &outlen);
2400     GST_TRACE ("segment : %s", fields[2]);
2401     if (!gst_value_deserialize_segment (&sval, fields[2]))
2402       goto fail;
2403   }
2404
2405   if (strcmp (fields[3], "None") != 0) {
2406     g_strdelimit (fields[3], "_", '=');
2407     g_base64_decode_inplace (fields[3], &outlen);
2408     GST_TRACE ("info    : %s", fields[3]);
2409     info = gst_structure_from_string (fields[3], NULL);
2410     if (info == NULL)
2411       goto fail;
2412   } else {
2413     info = NULL;
2414   }
2415
2416   sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
2417       g_value_get_boxed (&sval), info);
2418
2419   g_value_take_boxed (dest, sample);
2420
2421   if (caps)
2422     gst_caps_unref (caps);
2423
2424   ret = TRUE;
2425
2426 fail:
2427
2428   g_value_unset (&bval);
2429   g_value_unset (&sval);
2430
2431 wrong_length:
2432
2433   g_strfreev (fields);
2434
2435   return ret;
2436 }
2437
2438 /***********
2439  * boolean *
2440  ***********/
2441
2442 static gint
2443 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
2444 {
2445   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
2446     return GST_VALUE_EQUAL;
2447   return GST_VALUE_UNORDERED;
2448 }
2449
2450 static gchar *
2451 gst_value_serialize_boolean (const GValue * value)
2452 {
2453   if (value->data[0].v_int) {
2454     return g_strdup ("true");
2455   }
2456   return g_strdup ("false");
2457 }
2458
2459 static gboolean
2460 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
2461 {
2462   gboolean ret = FALSE;
2463
2464   if (g_ascii_strcasecmp (s, "true") == 0 ||
2465       g_ascii_strcasecmp (s, "yes") == 0 ||
2466       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
2467     g_value_set_boolean (dest, TRUE);
2468     ret = TRUE;
2469   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
2470       g_ascii_strcasecmp (s, "no") == 0 ||
2471       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
2472     g_value_set_boolean (dest, FALSE);
2473     ret = TRUE;
2474   }
2475
2476   return ret;
2477 }
2478
2479 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
2480 static gint                                                             \
2481 gst_value_compare_ ## _type                                             \
2482 (const GValue * value1, const GValue * value2)                          \
2483 {                                                                       \
2484   g ## _type val1 = g_value_get_ ## _type (value1);                     \
2485   g ## _type val2 = g_value_get_ ## _type (value2);                     \
2486   if (val1 > val2)                                                      \
2487     return GST_VALUE_GREATER_THAN;                                      \
2488   if (val1 < val2)                                                      \
2489     return GST_VALUE_LESS_THAN;                                         \
2490   return GST_VALUE_EQUAL;                                               \
2491 }                                                                       \
2492                                                                         \
2493 static gchar *                                                          \
2494 gst_value_serialize_ ## _type (const GValue * value)                    \
2495 {                                                                       \
2496   GValue val = { 0, };                                                  \
2497   g_value_init (&val, G_TYPE_STRING);                                   \
2498   if (!g_value_transform (value, &val))                                 \
2499     g_assert_not_reached ();                                            \
2500   /* NO_COPY_MADNESS!!! */                                              \
2501   return (char *) g_value_get_string (&val);                            \
2502 }
2503
2504 /* deserialize the given s into to as a gint64.
2505  * check if the result is actually storeable in the given size number of
2506  * bytes.
2507  */
2508 static gboolean
2509 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
2510     gint64 min, gint64 max, gint size)
2511 {
2512   gboolean ret = FALSE;
2513   gchar *end;
2514   guint64 mask = ~0;
2515
2516   errno = 0;
2517   *to = g_ascii_strtoull (s, &end, 0);
2518   /* a range error is a definitive no-no */
2519   if (errno == ERANGE) {
2520     return FALSE;
2521   }
2522
2523   if (*end == 0) {
2524     ret = TRUE;
2525   } else {
2526     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
2527       *to = G_LITTLE_ENDIAN;
2528       ret = TRUE;
2529     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
2530       *to = G_BIG_ENDIAN;
2531       ret = TRUE;
2532     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
2533       *to = G_BYTE_ORDER;
2534       ret = TRUE;
2535     } else if (g_ascii_strcasecmp (s, "min") == 0) {
2536       *to = min;
2537       ret = TRUE;
2538     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2539       *to = max;
2540       ret = TRUE;
2541     }
2542   }
2543   if (ret) {
2544     /* by definition, a gint64 fits into a gint64; so ignore those */
2545     if (size != sizeof (mask)) {
2546       if (*to >= 0) {
2547         /* for positive numbers, we create a mask of 1's outside of the range
2548          * and 0's inside the range.  An and will thus keep only 1 bits
2549          * outside of the range */
2550         mask <<= (size * 8);
2551         if ((mask & *to) != 0) {
2552           ret = FALSE;
2553         }
2554       } else {
2555         /* for negative numbers, we do a 2's complement version */
2556         mask <<= ((size * 8) - 1);
2557         if ((mask & *to) != mask) {
2558           ret = FALSE;
2559         }
2560       }
2561     }
2562   }
2563   return ret;
2564 }
2565
2566 #define CREATE_SERIALIZATION(_type,_macro)                              \
2567 CREATE_SERIALIZATION_START(_type,_macro)                                \
2568                                                                         \
2569 static gboolean                                                         \
2570 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2571 {                                                                       \
2572   gint64 x;                                                             \
2573                                                                         \
2574   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
2575       G_MAX ## _macro, sizeof (g ## _type))) {                          \
2576     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
2577     return TRUE;                                                        \
2578   } else {                                                              \
2579     return FALSE;                                                       \
2580   }                                                                     \
2581 }
2582
2583 #define CREATE_USERIALIZATION(_type,_macro)                             \
2584 CREATE_SERIALIZATION_START(_type,_macro)                                \
2585                                                                         \
2586 static gboolean                                                         \
2587 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2588 {                                                                       \
2589   gint64 x;                                                             \
2590   gchar *end;                                                           \
2591   gboolean ret = FALSE;                                                 \
2592                                                                         \
2593   errno = 0;                                                            \
2594   x = g_ascii_strtoull (s, &end, 0);                                    \
2595   /* a range error is a definitive no-no */                             \
2596   if (errno == ERANGE) {                                                \
2597     return FALSE;                                                       \
2598   }                                                                     \
2599   /* the cast ensures the range check later on makes sense */           \
2600   x = (g ## _type) x;                                                   \
2601   if (*end == 0) {                                                      \
2602     ret = TRUE;                                                         \
2603   } else {                                                              \
2604     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
2605       x = G_LITTLE_ENDIAN;                                              \
2606       ret = TRUE;                                                       \
2607     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
2608       x = G_BIG_ENDIAN;                                                 \
2609       ret = TRUE;                                                       \
2610     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
2611       x = G_BYTE_ORDER;                                                 \
2612       ret = TRUE;                                                       \
2613     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
2614       x = 0;                                                            \
2615       ret = TRUE;                                                       \
2616     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
2617       x = G_MAX ## _macro;                                              \
2618       ret = TRUE;                                                       \
2619     }                                                                   \
2620   }                                                                     \
2621   if (ret) {                                                            \
2622     if (x > G_MAX ## _macro) {                                          \
2623       ret = FALSE;                                                      \
2624     } else {                                                            \
2625       g_value_set_ ## _type (dest, x);                                  \
2626     }                                                                   \
2627   }                                                                     \
2628   return ret;                                                           \
2629 }
2630
2631 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
2632 G_STMT_START {                                                          \
2633   static const GstValueTable gst_value = {                              \
2634     _gtype,                                                             \
2635     gst_value_compare_ ## _type,                                        \
2636     gst_value_serialize_ ## _type,                                      \
2637     gst_value_deserialize_ ## _type,                                    \
2638   };                                                                    \
2639                                                                         \
2640   gst_value_register (&gst_value);                                      \
2641 } G_STMT_END
2642
2643 CREATE_SERIALIZATION (int, INT);
2644 CREATE_SERIALIZATION (int64, INT64);
2645 CREATE_SERIALIZATION (long, LONG);
2646
2647 CREATE_USERIALIZATION (uint, UINT);
2648 CREATE_USERIALIZATION (uint64, UINT64);
2649 CREATE_USERIALIZATION (ulong, ULONG);
2650
2651 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2652 #ifndef G_MAXUCHAR
2653 #define G_MAXUCHAR 255
2654 #endif
2655 CREATE_USERIALIZATION (uchar, UCHAR);
2656
2657 /**********
2658  * double *
2659  **********/
2660 static gint
2661 gst_value_compare_double (const GValue * value1, const GValue * value2)
2662 {
2663   if (value1->data[0].v_double > value2->data[0].v_double)
2664     return GST_VALUE_GREATER_THAN;
2665   if (value1->data[0].v_double < value2->data[0].v_double)
2666     return GST_VALUE_LESS_THAN;
2667   if (value1->data[0].v_double == value2->data[0].v_double)
2668     return GST_VALUE_EQUAL;
2669   return GST_VALUE_UNORDERED;
2670 }
2671
2672 static gchar *
2673 gst_value_serialize_double (const GValue * value)
2674 {
2675   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2676
2677   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2678   return g_strdup (d);
2679 }
2680
2681 static gboolean
2682 gst_value_deserialize_double (GValue * dest, const gchar * s)
2683 {
2684   gdouble x;
2685   gboolean ret = FALSE;
2686   gchar *end;
2687
2688   x = g_ascii_strtod (s, &end);
2689   if (*end == 0) {
2690     ret = TRUE;
2691   } else {
2692     if (g_ascii_strcasecmp (s, "min") == 0) {
2693       x = -G_MAXDOUBLE;
2694       ret = TRUE;
2695     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2696       x = G_MAXDOUBLE;
2697       ret = TRUE;
2698     }
2699   }
2700   if (ret) {
2701     g_value_set_double (dest, x);
2702   }
2703   return ret;
2704 }
2705
2706 /*********
2707  * float *
2708  *********/
2709
2710 static gint
2711 gst_value_compare_float (const GValue * value1, const GValue * value2)
2712 {
2713   if (value1->data[0].v_float > value2->data[0].v_float)
2714     return GST_VALUE_GREATER_THAN;
2715   if (value1->data[0].v_float < value2->data[0].v_float)
2716     return GST_VALUE_LESS_THAN;
2717   if (value1->data[0].v_float == value2->data[0].v_float)
2718     return GST_VALUE_EQUAL;
2719   return GST_VALUE_UNORDERED;
2720 }
2721
2722 static gchar *
2723 gst_value_serialize_float (const GValue * value)
2724 {
2725   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2726
2727   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2728   return g_strdup (d);
2729 }
2730
2731 static gboolean
2732 gst_value_deserialize_float (GValue * dest, const gchar * s)
2733 {
2734   gdouble x;
2735   gboolean ret = FALSE;
2736   gchar *end;
2737
2738   x = g_ascii_strtod (s, &end);
2739   if (*end == 0) {
2740     ret = TRUE;
2741   } else {
2742     if (g_ascii_strcasecmp (s, "min") == 0) {
2743       x = -G_MAXFLOAT;
2744       ret = TRUE;
2745     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2746       x = G_MAXFLOAT;
2747       ret = TRUE;
2748     }
2749   }
2750   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2751     ret = FALSE;
2752   if (ret) {
2753     g_value_set_float (dest, (float) x);
2754   }
2755   return ret;
2756 }
2757
2758 /**********
2759  * string *
2760  **********/
2761
2762 static gint
2763 gst_value_compare_string (const GValue * value1, const GValue * value2)
2764 {
2765   if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2766     /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2767     if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2768       return GST_VALUE_UNORDERED;
2769   } else {
2770     gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2771
2772     if (x < 0)
2773       return GST_VALUE_LESS_THAN;
2774     if (x > 0)
2775       return GST_VALUE_GREATER_THAN;
2776   }
2777
2778   return GST_VALUE_EQUAL;
2779 }
2780
2781 static gint
2782 gst_string_measure_wrapping (const gchar * s)
2783 {
2784   gint len;
2785   gboolean wrap = FALSE;
2786
2787   if (G_UNLIKELY (s == NULL))
2788     return -1;
2789
2790   /* Special case: the actual string NULL needs wrapping */
2791   if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2792     return 4;
2793
2794   len = 0;
2795   while (*s) {
2796     if (GST_ASCII_IS_STRING (*s)) {
2797       len++;
2798     } else if (*s < 0x20 || *s >= 0x7f) {
2799       wrap = TRUE;
2800       len += 4;
2801     } else {
2802       wrap = TRUE;
2803       len += 2;
2804     }
2805     s++;
2806   }
2807
2808   /* Wrap the string if we found something that needs
2809    * wrapping, or the empty string (len == 0) */
2810   return (wrap || len == 0) ? len : -1;
2811 }
2812
2813 static gchar *
2814 gst_string_wrap_inner (const gchar * s, gint len)
2815 {
2816   gchar *d, *e;
2817
2818   e = d = g_malloc (len + 3);
2819
2820   *e++ = '\"';
2821   while (*s) {
2822     if (GST_ASCII_IS_STRING (*s)) {
2823       *e++ = *s++;
2824     } else if (*s < 0x20 || *s >= 0x7f) {
2825       *e++ = '\\';
2826       *e++ = '0' + ((*(guchar *) s) >> 6);
2827       *e++ = '0' + (((*s) >> 3) & 0x7);
2828       *e++ = '0' + ((*s++) & 0x7);
2829     } else {
2830       *e++ = '\\';
2831       *e++ = *s++;
2832     }
2833   }
2834   *e++ = '\"';
2835   *e = 0;
2836
2837   g_assert (e - d <= len + 3);
2838   return d;
2839 }
2840
2841 /* Do string wrapping/escaping */
2842 static gchar *
2843 gst_string_wrap (const gchar * s)
2844 {
2845   gint len = gst_string_measure_wrapping (s);
2846
2847   if (G_LIKELY (len < 0))
2848     return g_strdup (s);
2849
2850   return gst_string_wrap_inner (s, len);
2851 }
2852
2853 /* Same as above, but take ownership of the string */
2854 static gchar *
2855 gst_string_take_and_wrap (gchar * s)
2856 {
2857   gchar *out;
2858   gint len = gst_string_measure_wrapping (s);
2859
2860   if (G_LIKELY (len < 0))
2861     return s;
2862
2863   out = gst_string_wrap_inner (s, len);
2864   g_free (s);
2865
2866   return out;
2867 }
2868
2869 /*
2870  * This function takes a string delimited with double quotes (")
2871  * and unescapes any \xxx octal numbers.
2872  *
2873  * If sequences of \y are found where y is not in the range of
2874  * 0->3, y is copied unescaped.
2875  *
2876  * If \xyy is found where x is an octal number but y is not, an
2877  * error is encountered and %NULL is returned.
2878  *
2879  * the input string must be \0 terminated.
2880  */
2881 static gchar *
2882 gst_string_unwrap (const gchar * s)
2883 {
2884   gchar *ret;
2885   gchar *read, *write;
2886
2887   /* NULL string returns NULL */
2888   if (s == NULL)
2889     return NULL;
2890
2891   /* strings not starting with " are invalid */
2892   if (*s != '"')
2893     return NULL;
2894
2895   /* make copy of original string to hold the result. This
2896    * string will always be smaller than the original */
2897   ret = g_strdup (s);
2898   read = ret;
2899   write = ret;
2900
2901   /* need to move to the next position as we parsed the " */
2902   read++;
2903
2904   while (*read) {
2905     if (GST_ASCII_IS_STRING (*read)) {
2906       /* normal chars are just copied */
2907       *write++ = *read++;
2908     } else if (*read == '"') {
2909       /* quote marks end of string */
2910       break;
2911     } else if (*read == '\\') {
2912       /* got an escape char, move to next position to read a tripplet
2913        * of octal numbers */
2914       read++;
2915       /* is the next char a possible first octal number? */
2916       if (*read >= '0' && *read <= '3') {
2917         /* parse other 2 numbers, if one of them is not in the range of
2918          * an octal number, we error. We also catch the case where a zero
2919          * byte is found here. */
2920         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2921           goto beach;
2922
2923         /* now convert the octal number to a byte again. */
2924         *write++ = ((read[0] - '0') << 6) +
2925             ((read[1] - '0') << 3) + (read[2] - '0');
2926
2927         read += 3;
2928       } else {
2929         /* if we run into a \0 here, we definitely won't get a quote later */
2930         if (*read == 0)
2931           goto beach;
2932
2933         /* else copy \X sequence */
2934         *write++ = *read++;
2935       }
2936     } else {
2937       /* weird character, error */
2938       goto beach;
2939     }
2940   }
2941   /* if the string is not ending in " and zero terminated, we error */
2942   if (*read != '"' || read[1] != '\0')
2943     goto beach;
2944
2945   /* null terminate result string and return */
2946   *write = '\0';
2947   return ret;
2948
2949 beach:
2950   g_free (ret);
2951   return NULL;
2952 }
2953
2954 static gchar *
2955 gst_value_serialize_string (const GValue * value)
2956 {
2957   return gst_string_wrap (value->data[0].v_pointer);
2958 }
2959
2960 static gboolean
2961 gst_value_deserialize_string (GValue * dest, const gchar * s)
2962 {
2963   if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2964     g_value_set_string (dest, NULL);
2965     return TRUE;
2966   } else if (G_LIKELY (*s != '"')) {
2967     if (!g_utf8_validate (s, -1, NULL))
2968       return FALSE;
2969     g_value_set_string (dest, s);
2970     return TRUE;
2971   } else {
2972     gchar *str = gst_string_unwrap (s);
2973     if (G_UNLIKELY (!str))
2974       return FALSE;
2975     g_value_take_string (dest, str);
2976   }
2977
2978   return TRUE;
2979 }
2980
2981 /********
2982  * enum *
2983  ********/
2984
2985 static gint
2986 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2987 {
2988   GEnumValue *en1, *en2;
2989   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2990   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2991
2992   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2993   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2994   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2995   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2996   g_type_class_unref (klass1);
2997   g_type_class_unref (klass2);
2998   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2999   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
3000   if (en1->value < en2->value)
3001     return GST_VALUE_LESS_THAN;
3002   if (en1->value > en2->value)
3003     return GST_VALUE_GREATER_THAN;
3004
3005   return GST_VALUE_EQUAL;
3006 }
3007
3008 static gchar *
3009 gst_value_serialize_enum (const GValue * value)
3010 {
3011   GEnumValue *en;
3012   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
3013
3014   g_return_val_if_fail (klass, NULL);
3015   en = g_enum_get_value (klass, g_value_get_enum (value));
3016   g_type_class_unref (klass);
3017
3018   /* might be one of the custom formats registered later */
3019   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
3020     const GstFormatDefinition *format_def;
3021
3022     format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
3023     g_return_val_if_fail (format_def != NULL, NULL);
3024     return g_strdup (format_def->description);
3025   }
3026
3027   g_return_val_if_fail (en, NULL);
3028   return g_strdup (en->value_name);
3029 }
3030
3031 static gint
3032 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
3033     const gchar * s)
3034 {
3035   const GstFormatDefinition *format_def =
3036       g_value_get_pointer (format_def_value);
3037
3038   if (g_ascii_strcasecmp (s, format_def->nick) == 0)
3039     return 0;
3040
3041   return g_ascii_strcasecmp (s, format_def->description);
3042 }
3043
3044 static gboolean
3045 gst_value_deserialize_enum (GValue * dest, const gchar * s)
3046 {
3047   GEnumValue *en;
3048   gchar *endptr = NULL;
3049   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3050
3051   g_return_val_if_fail (klass, FALSE);
3052   if (!(en = g_enum_get_value_by_name (klass, s))) {
3053     if (!(en = g_enum_get_value_by_nick (klass, s))) {
3054       gint i = strtol (s, &endptr, 0);
3055
3056       if (endptr && *endptr == '\0') {
3057         en = g_enum_get_value (klass, i);
3058       }
3059     }
3060   }
3061   g_type_class_unref (klass);
3062
3063   /* might be one of the custom formats registered later */
3064   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
3065     GValue res = { 0, };
3066     const GstFormatDefinition *format_def;
3067     GstIterator *iter;
3068     gboolean found;
3069
3070     iter = gst_format_iterate_definitions ();
3071
3072     found = gst_iterator_find_custom (iter,
3073         (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
3074
3075     if (found) {
3076       format_def = g_value_get_pointer (&res);
3077       g_return_val_if_fail (format_def != NULL, FALSE);
3078       g_value_set_enum (dest, (gint) format_def->value);
3079       g_value_unset (&res);
3080     }
3081     gst_iterator_free (iter);
3082     return found;
3083   }
3084
3085   /* enum name/nick not found */
3086   if (en == NULL)
3087     return FALSE;
3088
3089   g_value_set_enum (dest, en->value);
3090   return TRUE;
3091 }
3092
3093 /********
3094  * flags *
3095  ********/
3096
3097 /* we just compare the value here */
3098 static gint
3099 gst_value_compare_flags (const GValue * value1, const GValue * value2)
3100 {
3101   guint fl1, fl2;
3102   GFlagsClass *klass1 =
3103       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
3104   GFlagsClass *klass2 =
3105       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
3106
3107   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
3108   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
3109   fl1 = g_value_get_flags (value1);
3110   fl2 = g_value_get_flags (value2);
3111   g_type_class_unref (klass1);
3112   g_type_class_unref (klass2);
3113   if (fl1 < fl2)
3114     return GST_VALUE_LESS_THAN;
3115   if (fl1 > fl2)
3116     return GST_VALUE_GREATER_THAN;
3117
3118   return GST_VALUE_EQUAL;
3119 }
3120
3121 /* the different flags are serialized separated with a + */
3122 static gchar *
3123 gst_value_serialize_flags (const GValue * value)
3124 {
3125   guint flags;
3126   GFlagsValue *fl;
3127   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
3128   gchar *result, *tmp;
3129   gboolean first = TRUE;
3130
3131   g_return_val_if_fail (klass, NULL);
3132
3133   flags = g_value_get_flags (value);
3134
3135   /* if no flags are set, try to serialize to the _NONE string */
3136   if (!flags) {
3137     fl = g_flags_get_first_value (klass, flags);
3138     if (fl)
3139       return g_strdup (fl->value_name);
3140     else
3141       return g_strdup ("0");
3142   }
3143
3144   /* some flags are set, so serialize one by one */
3145   result = g_strdup ("");
3146   while (flags) {
3147     fl = g_flags_get_first_value (klass, flags);
3148     if (fl != NULL) {
3149       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
3150       g_free (result);
3151       result = tmp;
3152       first = FALSE;
3153
3154       /* clear flag */
3155       flags &= ~fl->value;
3156     }
3157   }
3158   g_type_class_unref (klass);
3159
3160   return result;
3161 }
3162
3163 static gboolean
3164 gst_value_deserialize_flags (GValue * dest, const gchar * s)
3165 {
3166   GFlagsValue *fl;
3167   gchar *endptr = NULL;
3168   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3169   gchar **split;
3170   guint flags;
3171   gint i;
3172
3173   g_return_val_if_fail (klass, FALSE);
3174
3175   /* split into parts delimited with + */
3176   split = g_strsplit (s, "+", 0);
3177
3178   flags = 0;
3179   i = 0;
3180   /* loop over each part */
3181   while (split[i]) {
3182     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
3183       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
3184         gint val = strtol (split[i], &endptr, 0);
3185
3186         /* just or numeric value */
3187         if (endptr && *endptr == '\0') {
3188           flags |= val;
3189         }
3190       }
3191     }
3192     if (fl) {
3193       flags |= fl->value;
3194     }
3195     i++;
3196   }
3197   g_strfreev (split);
3198   g_type_class_unref (klass);
3199   g_value_set_flags (dest, flags);
3200
3201   return TRUE;
3202 }
3203
3204 /****************
3205  * subset *
3206  ****************/
3207
3208 static gboolean
3209 gst_value_is_subset_int_range_int_range (const GValue * value1,
3210     const GValue * value2)
3211 {
3212   gint gcd;
3213
3214   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
3215   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
3216
3217   if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
3218       INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
3219     return FALSE;
3220   if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
3221       INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
3222     return FALSE;
3223
3224   if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
3225     if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
3226         INT_RANGE_STEP (value1))
3227       return FALSE;
3228     return TRUE;
3229   }
3230
3231   gcd =
3232       gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
3233       INT_RANGE_STEP (value2));
3234   if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
3235     return FALSE;
3236
3237   return TRUE;
3238 }
3239
3240 static gboolean
3241 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
3242     const GValue * value2)
3243 {
3244   gint64 gcd;
3245
3246   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
3247   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
3248
3249   if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
3250     return FALSE;
3251   if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
3252     return FALSE;
3253
3254   if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
3255     if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
3256         INT64_RANGE_STEP (value1))
3257       return FALSE;
3258     return TRUE;
3259   }
3260
3261   gcd =
3262       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
3263       INT64_RANGE_STEP (value2));
3264   if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
3265     return FALSE;
3266
3267   return TRUE;
3268 }
3269
3270 /**
3271  * gst_value_is_subset:
3272  * @value1: a #GValue
3273  * @value2: a #GValue
3274  *
3275  * Check that @value1 is a subset of @value2.
3276  *
3277  * Return: %TRUE is @value1 is a subset of @value2
3278  */
3279 gboolean
3280 gst_value_is_subset (const GValue * value1, const GValue * value2)
3281 {
3282   /* special case for int/int64 ranges, since we cannot compute
3283      the difference for those when they have different steps,
3284      and it's actually a lot simpler to compute whether a range
3285      is a subset of another. */
3286   if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
3287     return gst_value_is_subset_int_range_int_range (value1, value2);
3288   } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
3289       && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
3290     return gst_value_is_subset_int64_range_int64_range (value1, value2);
3291   }
3292
3293   /*
3294    * 1 - [1,2] = empty
3295    * -> !subset
3296    *
3297    * [1,2] - 1 = 2
3298    *  -> 1 - [1,2] = empty
3299    *  -> subset
3300    *
3301    * [1,3] - [1,2] = 3
3302    * -> [1,2] - [1,3] = empty
3303    * -> subset
3304    *
3305    * {1,2} - {1,3} = 2
3306    * -> {1,3} - {1,2} = 3
3307    * -> !subset
3308    *
3309    *  First caps subtraction needs to return a non-empty set, second
3310    *  subtractions needs to give en empty set.
3311    *  Both substractions are switched below, as it's faster that way.
3312    */
3313   if (!gst_value_subtract (NULL, value1, value2)) {
3314     if (gst_value_subtract (NULL, value2, value1)) {
3315       return TRUE;
3316     }
3317   }
3318   return FALSE;
3319 }
3320
3321 /*********
3322  * union *
3323  *********/
3324
3325 static gboolean
3326 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
3327     const GValue * src2)
3328 {
3329   gint v = src1->data[0].v_int;
3330
3331   /* check if it's already in the range */
3332   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
3333       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
3334       v % INT_RANGE_STEP (src2) == 0) {
3335     if (dest)
3336       gst_value_init_and_copy (dest, src2);
3337     return TRUE;
3338   }
3339
3340   /* check if it extends the range */
3341   if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
3342     if (dest) {
3343       guint64 new_min =
3344           (guint) ((INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2));
3345       guint64 new_max = (guint) (INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3346
3347       gst_value_init_and_copy (dest, src2);
3348       dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3349     }
3350     return TRUE;
3351   }
3352   if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
3353     if (dest) {
3354       guint64 new_min = (guint) (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3355       guint64 new_max =
3356           (guint) ((INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2));
3357
3358       gst_value_init_and_copy (dest, src2);
3359       dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3360     }
3361     return TRUE;
3362   }
3363
3364   return FALSE;
3365 }
3366
3367 static gboolean
3368 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
3369     const GValue * src2)
3370 {
3371   /* We can union in several special cases:
3372      1 - one is a subset of another
3373      2 - same step and not disjoint
3374      3 - different step, at least one with one value which matches a 'next' or 'previous'
3375      - anything else ?
3376    */
3377
3378   /* 1 - subset */
3379   if (gst_value_is_subset_int_range_int_range (src1, src2)) {
3380     if (dest)
3381       gst_value_init_and_copy (dest, src2);
3382     return TRUE;
3383   }
3384   if (gst_value_is_subset_int_range_int_range (src2, src1)) {
3385     if (dest)
3386       gst_value_init_and_copy (dest, src1);
3387     return TRUE;
3388   }
3389
3390   /* 2 - same step and not disjoint */
3391   if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
3392     if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
3393             INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
3394         (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
3395             INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
3396       if (dest) {
3397         gint step = INT_RANGE_STEP (src1);
3398         gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
3399         gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
3400         g_value_init (dest, GST_TYPE_INT_RANGE);
3401         gst_value_set_int_range_step (dest, min, max, step);
3402       }
3403       return TRUE;
3404     }
3405   }
3406
3407   /* 3 - single value matches next or previous */
3408   if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
3409     gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
3410     gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
3411     if (n1 == 1 || n2 == 1) {
3412       const GValue *range_value = NULL;
3413       gint scalar = 0;
3414       if (n1 == 1) {
3415         range_value = src2;
3416         scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
3417       } else if (n2 == 1) {
3418         range_value = src1;
3419         scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
3420       }
3421
3422       if (scalar ==
3423           (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
3424         if (dest) {
3425           guint64 new_min = (guint)
3426               ((INT_RANGE_MIN (range_value) -
3427                   1) * INT_RANGE_STEP (range_value));
3428           guint64 new_max = (guint)
3429               (INT_RANGE_MAX (range_value) * INT_RANGE_STEP (range_value));
3430
3431           gst_value_init_and_copy (dest, range_value);
3432           dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3433         }
3434         return TRUE;
3435       } else if (scalar ==
3436           (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
3437         if (dest) {
3438           guint64 new_min = (guint)
3439               (INT_RANGE_MIN (range_value) * INT_RANGE_STEP (range_value));
3440           guint64 new_max = (guint)
3441               ((INT_RANGE_MAX (range_value) +
3442                   1) * INT_RANGE_STEP (range_value));
3443           gst_value_init_and_copy (dest, range_value);
3444           dest->data[0].v_uint64 = (new_min << 32) | (new_max);
3445         }
3446         return TRUE;
3447       }
3448     }
3449   }
3450
3451   /* If we get there, we did not find a way to make a union that can be
3452      represented with our simplistic model. */
3453   return FALSE;
3454 }
3455
3456 /****************
3457  * intersection *
3458  ****************/
3459
3460 static gboolean
3461 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
3462     const GValue * src2)
3463 {
3464   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
3465       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
3466       src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
3467     if (dest)
3468       gst_value_init_and_copy (dest, src1);
3469     return TRUE;
3470   }
3471
3472   return FALSE;
3473 }
3474
3475 static gboolean
3476 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
3477     const GValue * src2)
3478 {
3479   gint min;
3480   gint max;
3481   gint step;
3482
3483   step =
3484       INT_RANGE_STEP (src1) /
3485       gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
3486       INT_RANGE_STEP (src2));
3487   if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
3488     return FALSE;
3489   step *= INT_RANGE_STEP (src2);
3490
3491   min =
3492       MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
3493       INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3494   min = (min + step - 1) / step * step;
3495   max =
3496       MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
3497       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3498   max = max / step * step;
3499
3500   if (min < max) {
3501     if (dest) {
3502       g_value_init (dest, GST_TYPE_INT_RANGE);
3503       gst_value_set_int_range_step (dest, min, max, step);
3504     }
3505     return TRUE;
3506   }
3507   if (min == max) {
3508     if (dest) {
3509       g_value_init (dest, G_TYPE_INT);
3510       g_value_set_int (dest, min);
3511     }
3512     return TRUE;
3513   }
3514
3515   return FALSE;
3516 }
3517
3518 #define INT64_RANGE_MIN_VAL(v) (INT64_RANGE_MIN (v) * INT64_RANGE_STEP (v))
3519 #define INT64_RANGE_MAX_VAL(v) (INT64_RANGE_MAX (v) * INT64_RANGE_STEP (v))
3520
3521 static gboolean
3522 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
3523     const GValue * src2)
3524 {
3525   if (INT64_RANGE_MIN_VAL (src2) <= src1->data[0].v_int64 &&
3526       INT64_RANGE_MAX_VAL (src2) >= src1->data[0].v_int64 &&
3527       src1->data[0].v_int64 % INT64_RANGE_STEP (src2) == 0) {
3528     if (dest)
3529       gst_value_init_and_copy (dest, src1);
3530     return TRUE;
3531   }
3532
3533   return FALSE;
3534 }
3535
3536 static gboolean
3537 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
3538     const GValue * src2)
3539 {
3540   gint64 min;
3541   gint64 max;
3542   gint64 step;
3543
3544   step =
3545       INT64_RANGE_STEP (src1) /
3546       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
3547       INT64_RANGE_STEP (src2));
3548   if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
3549     return FALSE;
3550   step *= INT64_RANGE_STEP (src2);
3551
3552   min =
3553       MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
3554       INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
3555   min = (min + step - 1) / step * step;
3556   max =
3557       MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
3558       INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
3559   max = max / step * step;
3560
3561   if (min < max) {
3562     if (dest) {
3563       g_value_init (dest, GST_TYPE_INT64_RANGE);
3564       gst_value_set_int64_range_step (dest, min, max, step);
3565     }
3566     return TRUE;
3567   }
3568   if (min == max) {
3569     if (dest) {
3570       g_value_init (dest, G_TYPE_INT64);
3571       g_value_set_int64 (dest, min);
3572     }
3573     return TRUE;
3574   }
3575
3576   return FALSE;
3577 }
3578
3579 static gboolean
3580 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
3581     const GValue * src2)
3582 {
3583   if (src2->data[0].v_double <= src1->data[0].v_double &&
3584       src2->data[1].v_double >= src1->data[0].v_double) {
3585     if (dest)
3586       gst_value_init_and_copy (dest, src1);
3587     return TRUE;
3588   }
3589
3590   return FALSE;
3591 }
3592
3593 static gboolean
3594 gst_value_intersect_double_range_double_range (GValue * dest,
3595     const GValue * src1, const GValue * src2)
3596 {
3597   gdouble min;
3598   gdouble max;
3599
3600   min = MAX (src1->data[0].v_double, src2->data[0].v_double);
3601   max = MIN (src1->data[1].v_double, src2->data[1].v_double);
3602
3603   if (min < max) {
3604     if (dest) {
3605       g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
3606       gst_value_set_double_range (dest, min, max);
3607     }
3608     return TRUE;
3609   }
3610   if (min == max) {
3611     if (dest) {
3612       g_value_init (dest, G_TYPE_DOUBLE);
3613       g_value_set_int (dest, (int) min);
3614     }
3615     return TRUE;
3616   }
3617
3618   return FALSE;
3619 }
3620
3621 static gboolean
3622 gst_value_intersect_list (GValue * dest, const GValue * value1,
3623     const GValue * value2)
3624 {
3625   guint i, size;
3626   GValue intersection = { 0, };
3627   gboolean ret = FALSE;
3628
3629   size = VALUE_LIST_SIZE (value1);
3630   for (i = 0; i < size; i++) {
3631     const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
3632
3633     /* quicker version when we don't need the resulting set */
3634     if (!dest) {
3635       if (gst_value_intersect (NULL, cur, value2)) {
3636         ret = TRUE;
3637         break;
3638       }
3639       continue;
3640     }
3641
3642     if (gst_value_intersect (&intersection, cur, value2)) {
3643       /* append value */
3644       if (!ret) {
3645         gst_value_move (dest, &intersection);
3646         ret = TRUE;
3647       } else if (GST_VALUE_HOLDS_LIST (dest)) {
3648         _gst_value_list_append_and_take_value (dest, &intersection);
3649       } else {
3650         GValue temp;
3651
3652         gst_value_move (&temp, dest);
3653         gst_value_list_merge (dest, &temp, &intersection);
3654         g_value_unset (&temp);
3655         g_value_unset (&intersection);
3656       }
3657     }
3658   }
3659
3660   return ret;
3661 }
3662
3663 static gboolean
3664 gst_value_intersect_array (GValue * dest, const GValue * src1,
3665     const GValue * src2)
3666 {
3667   guint size;
3668   guint n;
3669   GValue val = { 0 };
3670
3671   /* only works on similar-sized arrays */
3672   size = gst_value_array_get_size (src1);
3673   if (size != gst_value_array_get_size (src2))
3674     return FALSE;
3675
3676   /* quicker value when we don't need the resulting set */
3677   if (!dest) {
3678     for (n = 0; n < size; n++) {
3679       if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
3680               gst_value_array_get_value (src2, n))) {
3681         return FALSE;
3682       }
3683     }
3684     return TRUE;
3685   }
3686
3687   g_value_init (dest, GST_TYPE_ARRAY);
3688
3689   for (n = 0; n < size; n++) {
3690     if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
3691             gst_value_array_get_value (src2, n))) {
3692       g_value_unset (dest);
3693       return FALSE;
3694     }
3695     _gst_value_array_append_and_take_value (dest, &val);
3696   }
3697
3698   return TRUE;
3699 }
3700
3701 static gboolean
3702 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
3703     const GValue * src2)
3704 {
3705   gint res1, res2;
3706   GValue *vals;
3707   GstValueCompareFunc compare;
3708
3709   vals = src2->data[0].v_pointer;
3710
3711   if (vals == NULL)
3712     return FALSE;
3713
3714   if ((compare = gst_value_get_compare_func (src1))) {
3715     res1 = gst_value_compare_with_func (&vals[0], src1, compare);
3716     res2 = gst_value_compare_with_func (&vals[1], src1, compare);
3717
3718     if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
3719         (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
3720       if (dest)
3721         gst_value_init_and_copy (dest, src1);
3722       return TRUE;
3723     }
3724   }
3725
3726   return FALSE;
3727 }
3728
3729 static gboolean
3730 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
3731     const GValue * src1, const GValue * src2)
3732 {
3733   GValue *min;
3734   GValue *max;
3735   gint res;
3736   GValue *vals1, *vals2;
3737   GstValueCompareFunc compare;
3738
3739   vals1 = src1->data[0].v_pointer;
3740   vals2 = src2->data[0].v_pointer;
3741   g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
3742
3743   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
3744     /* min = MAX (src1.start, src2.start) */
3745     res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
3746     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3747     if (res == GST_VALUE_LESS_THAN)
3748       min = &vals2[0];          /* Take the max of the 2 */
3749     else
3750       min = &vals1[0];
3751
3752     /* max = MIN (src1.end, src2.end) */
3753     res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
3754     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3755     if (res == GST_VALUE_GREATER_THAN)
3756       max = &vals2[1];          /* Take the min of the 2 */
3757     else
3758       max = &vals1[1];
3759
3760     res = gst_value_compare_with_func (min, max, compare);
3761     g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3762     if (res == GST_VALUE_LESS_THAN) {
3763       if (dest) {
3764         g_value_init (dest, GST_TYPE_FRACTION_RANGE);
3765         vals1 = dest->data[0].v_pointer;
3766         g_value_copy (min, &vals1[0]);
3767         g_value_copy (max, &vals1[1]);
3768       }
3769       return TRUE;
3770     }
3771     if (res == GST_VALUE_EQUAL) {
3772       if (dest)
3773         gst_value_init_and_copy (dest, min);
3774       return TRUE;
3775     }
3776   }
3777
3778   return FALSE;
3779 }
3780
3781 /***************
3782  * subtraction *
3783  ***************/
3784
3785 static gboolean
3786 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
3787     const GValue * subtrahend)
3788 {
3789   gint min = gst_value_get_int_range_min (subtrahend);
3790   gint max = gst_value_get_int_range_max (subtrahend);
3791   gint step = gst_value_get_int_range_step (subtrahend);
3792   gint val = g_value_get_int (minuend);
3793
3794   if (step == 0)
3795     return FALSE;
3796
3797   /* subtracting a range from an int only works if the int is not in the
3798    * range */
3799   if (val < min || val > max || val % step) {
3800     /* and the result is the int */
3801     if (dest)
3802       gst_value_init_and_copy (dest, minuend);
3803     return TRUE;
3804   }
3805   return FALSE;
3806 }
3807
3808 /* creates a new int range based on input values.
3809  */
3810 static gboolean
3811 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
3812     gint max2, gint step)
3813 {
3814   GValue v1 = { 0, };
3815   GValue v2 = { 0, };
3816   GValue *pv1, *pv2;            /* yeah, hungarian! */
3817
3818   g_return_val_if_fail (step > 0, FALSE);
3819   g_return_val_if_fail (min1 % step == 0, FALSE);
3820   g_return_val_if_fail (max1 % step == 0, FALSE);
3821   g_return_val_if_fail (min2 % step == 0, FALSE);
3822   g_return_val_if_fail (max2 % step == 0, FALSE);
3823
3824   if (min1 <= max1 && min2 <= max2) {
3825     pv1 = &v1;
3826     pv2 = &v2;
3827   } else if (min1 <= max1) {
3828     pv1 = dest;
3829     pv2 = NULL;
3830   } else if (min2 <= max2) {
3831     pv1 = NULL;
3832     pv2 = dest;
3833   } else {
3834     return FALSE;
3835   }
3836
3837   if (!dest)
3838     return TRUE;
3839
3840   if (min1 < max1) {
3841     g_value_init (pv1, GST_TYPE_INT_RANGE);
3842     gst_value_set_int_range_step (pv1, min1, max1, step);
3843   } else if (min1 == max1) {
3844     g_value_init (pv1, G_TYPE_INT);
3845     g_value_set_int (pv1, min1);
3846   }
3847   if (min2 < max2) {
3848     g_value_init (pv2, GST_TYPE_INT_RANGE);
3849     gst_value_set_int_range_step (pv2, min2, max2, step);
3850   } else if (min2 == max2) {
3851     g_value_init (pv2, G_TYPE_INT);
3852     g_value_set_int (pv2, min2);
3853   }
3854
3855   if (min1 <= max1 && min2 <= max2) {
3856     gst_value_list_concat (dest, pv1, pv2);
3857     g_value_unset (pv1);
3858     g_value_unset (pv2);
3859   }
3860   return TRUE;
3861 }
3862
3863 static gboolean
3864 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
3865     const GValue * subtrahend)
3866 {
3867   gint min = gst_value_get_int_range_min (minuend);
3868   gint max = gst_value_get_int_range_max (minuend);
3869   gint step = gst_value_get_int_range_step (minuend);
3870   gint val = g_value_get_int (subtrahend);
3871
3872   g_return_val_if_fail (min < max, FALSE);
3873
3874   if (step == 0)
3875     return FALSE;
3876
3877   /* value is outside of the range, return range unchanged */
3878   if (val < min || val > max || val % step) {
3879     if (dest)
3880       gst_value_init_and_copy (dest, minuend);
3881     return TRUE;
3882   } else {
3883     /* max must be MAXINT too as val <= max */
3884     if (val >= G_MAXINT - step + 1) {
3885       max -= step;
3886       val -= step;
3887     }
3888     /* min must be MININT too as val >= max */
3889     if (val <= G_MININT + step - 1) {
3890       min += step;
3891       val += step;
3892     }
3893     if (dest)
3894       gst_value_create_new_range (dest, min, val - step, val + step, max, step);
3895   }
3896   return TRUE;
3897 }
3898
3899 static gboolean
3900 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
3901     const GValue * subtrahend)
3902 {
3903   gint min1 = gst_value_get_int_range_min (minuend);
3904   gint max1 = gst_value_get_int_range_max (minuend);
3905   gint step1 = gst_value_get_int_range_step (minuend);
3906   gint min2 = gst_value_get_int_range_min (subtrahend);
3907   gint max2 = gst_value_get_int_range_max (subtrahend);
3908   gint step2 = gst_value_get_int_range_step (subtrahend);
3909   gint step;
3910
3911   if (step1 != step2) {
3912     /* ENOIMPL */
3913     g_assert (FALSE);
3914     return FALSE;
3915   }
3916   step = step1;
3917
3918   if (step == 0)
3919     return FALSE;
3920
3921   if (max2 >= max1 && min2 <= min1) {
3922     return FALSE;
3923   } else if (max2 >= max1) {
3924     return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3925         step, 0, step);
3926   } else if (min2 <= min1) {
3927     return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
3928         step, 0, step);
3929   } else {
3930     return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3931         MAX (max2 + step, min1), max1, step);
3932   }
3933 }
3934
3935 static gboolean
3936 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
3937     const GValue * subtrahend)
3938 {
3939   gint64 min = gst_value_get_int64_range_min (subtrahend);
3940   gint64 max = gst_value_get_int64_range_max (subtrahend);
3941   gint64 step = gst_value_get_int64_range_step (subtrahend);
3942   gint64 val = g_value_get_int64 (minuend);
3943
3944   if (step == 0)
3945     return FALSE;
3946   /* subtracting a range from an int64 only works if the int64 is not in the
3947    * range */
3948   if (val < min || val > max || val % step) {
3949     /* and the result is the int64 */
3950     if (dest)
3951       gst_value_init_and_copy (dest, minuend);
3952     return TRUE;
3953   }
3954   return FALSE;
3955 }
3956
3957 /* creates a new int64 range based on input values.
3958  */
3959 static gboolean
3960 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
3961     gint64 min2, gint64 max2, gint64 step)
3962 {
3963   GValue v1 = { 0, };
3964   GValue v2 = { 0, };
3965   GValue *pv1, *pv2;            /* yeah, hungarian! */
3966
3967   g_return_val_if_fail (step > 0, FALSE);
3968   g_return_val_if_fail (min1 % step == 0, FALSE);
3969   g_return_val_if_fail (max1 % step == 0, FALSE);
3970   g_return_val_if_fail (min2 % step == 0, FALSE);
3971   g_return_val_if_fail (max2 % step == 0, FALSE);
3972
3973   if (min1 <= max1 && min2 <= max2) {
3974     pv1 = &v1;
3975     pv2 = &v2;
3976   } else if (min1 <= max1) {
3977     pv1 = dest;
3978     pv2 = NULL;
3979   } else if (min2 <= max2) {
3980     pv1 = NULL;
3981     pv2 = dest;
3982   } else {
3983     return FALSE;
3984   }
3985
3986   if (!dest)
3987     return TRUE;
3988
3989   if (min1 < max1) {
3990     g_value_init (pv1, GST_TYPE_INT64_RANGE);
3991     gst_value_set_int64_range_step (pv1, min1, max1, step);
3992   } else if (min1 == max1) {
3993     g_value_init (pv1, G_TYPE_INT64);
3994     g_value_set_int64 (pv1, min1);
3995   }
3996   if (min2 < max2) {
3997     g_value_init (pv2, GST_TYPE_INT64_RANGE);
3998     gst_value_set_int64_range_step (pv2, min2, max2, step);
3999   } else if (min2 == max2) {
4000     g_value_init (pv2, G_TYPE_INT64);
4001     g_value_set_int64 (pv2, min2);
4002   }
4003
4004   if (min1 <= max1 && min2 <= max2) {
4005     gst_value_list_concat (dest, pv1, pv2);
4006     g_value_unset (pv1);
4007     g_value_unset (pv2);
4008   }
4009   return TRUE;
4010 }
4011
4012 static gboolean
4013 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
4014     const GValue * subtrahend)
4015 {
4016   gint64 min = gst_value_get_int64_range_min (minuend);
4017   gint64 max = gst_value_get_int64_range_max (minuend);
4018   gint64 step = gst_value_get_int64_range_step (minuend);
4019   gint64 val = g_value_get_int64 (subtrahend);
4020
4021   g_return_val_if_fail (min < max, FALSE);
4022
4023   if (step == 0)
4024     return FALSE;
4025
4026   /* value is outside of the range, return range unchanged */
4027   if (val < min || val > max || val % step) {
4028     if (dest)
4029       gst_value_init_and_copy (dest, minuend);
4030     return TRUE;
4031   } else {
4032     /* max must be MAXINT64 too as val <= max */
4033     if (val >= G_MAXINT64 - step + 1) {
4034       max -= step;
4035       val -= step;
4036     }
4037     /* min must be MININT64 too as val >= max */
4038     if (val <= G_MININT64 + step - 1) {
4039       min += step;
4040       val += step;
4041     }
4042     if (dest)
4043       gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
4044           step);
4045   }
4046   return TRUE;
4047 }
4048
4049 static gboolean
4050 gst_value_subtract_int64_range_int64_range (GValue * dest,
4051     const GValue * minuend, const GValue * subtrahend)
4052 {
4053   gint64 min1 = gst_value_get_int64_range_min (minuend);
4054   gint64 max1 = gst_value_get_int64_range_max (minuend);
4055   gint64 step1 = gst_value_get_int64_range_step (minuend);
4056   gint64 min2 = gst_value_get_int64_range_min (subtrahend);
4057   gint64 max2 = gst_value_get_int64_range_max (subtrahend);
4058   gint64 step2 = gst_value_get_int64_range_step (subtrahend);
4059   gint64 step;
4060
4061   if (step1 != step2) {
4062     /* ENOIMPL */
4063     g_assert (FALSE);
4064     return FALSE;
4065   }
4066
4067   if (step1 == 0)
4068     return FALSE;
4069
4070   step = step1;
4071
4072   if (max2 >= max1 && min2 <= min1) {
4073     return FALSE;
4074   } else if (max2 >= max1) {
4075     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
4076             max1), step, 0, step);
4077   } else if (min2 <= min1) {
4078     return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
4079         max1, step, 0, step);
4080   } else {
4081     return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
4082             max1), MAX (max2 + step, min1), max1, step);
4083   }
4084 }
4085
4086 static gboolean
4087 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
4088     const GValue * subtrahend)
4089 {
4090   gdouble min = gst_value_get_double_range_min (subtrahend);
4091   gdouble max = gst_value_get_double_range_max (subtrahend);
4092   gdouble val = g_value_get_double (minuend);
4093
4094   if (val < min || val > max) {
4095     if (dest)
4096       gst_value_init_and_copy (dest, minuend);
4097     return TRUE;
4098   }
4099   return FALSE;
4100 }
4101
4102 static gboolean
4103 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
4104     const GValue * subtrahend)
4105 {
4106   /* since we don't have open ranges, we cannot create a hole in
4107    * a double range. We return the original range */
4108   if (dest)
4109     gst_value_init_and_copy (dest, minuend);
4110   return TRUE;
4111 }
4112
4113 static gboolean
4114 gst_value_subtract_double_range_double_range (GValue * dest,
4115     const GValue * minuend, const GValue * subtrahend)
4116 {
4117   /* since we don't have open ranges, we have to approximate */
4118   /* done like with ints */
4119   gdouble min1 = gst_value_get_double_range_min (minuend);
4120   gdouble max2 = gst_value_get_double_range_max (minuend);
4121   gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
4122   gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
4123   GValue v1 = { 0, };
4124   GValue v2 = { 0, };
4125   GValue *pv1, *pv2;            /* yeah, hungarian! */
4126
4127   if (min1 < max1 && min2 < max2) {
4128     pv1 = &v1;
4129     pv2 = &v2;
4130   } else if (min1 < max1) {
4131     pv1 = dest;
4132     pv2 = NULL;
4133   } else if (min2 < max2) {
4134     pv1 = NULL;
4135     pv2 = dest;
4136   } else {
4137     return FALSE;
4138   }
4139
4140   if (!dest)
4141     return TRUE;
4142
4143   if (min1 < max1) {
4144     g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
4145     gst_value_set_double_range (pv1, min1, max1);
4146   }
4147   if (min2 < max2) {
4148     g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
4149     gst_value_set_double_range (pv2, min2, max2);
4150   }
4151
4152   if (min1 < max1 && min2 < max2) {
4153     gst_value_list_concat (dest, pv1, pv2);
4154     g_value_unset (pv1);
4155     g_value_unset (pv2);
4156   }
4157   return TRUE;
4158 }
4159
4160 static gboolean
4161 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
4162     const GValue * subtrahend)
4163 {
4164   guint i, size;
4165   GValue subtraction = { 0, };
4166   gboolean ret = FALSE;
4167   GType ltype;
4168
4169   ltype = gst_value_list_get_type ();
4170
4171   size = VALUE_LIST_SIZE (minuend);
4172   for (i = 0; i < size; i++) {
4173     const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
4174
4175     /* quicker version when we can discard the result */
4176     if (!dest) {
4177       if (gst_value_subtract (NULL, cur, subtrahend)) {
4178         ret = TRUE;
4179         break;
4180       }
4181       continue;
4182     }
4183
4184     if (gst_value_subtract (&subtraction, cur, subtrahend)) {
4185       if (!ret) {
4186         gst_value_move (dest, &subtraction);
4187         ret = TRUE;
4188       } else if (G_VALUE_HOLDS (dest, ltype)
4189           && !G_VALUE_HOLDS (&subtraction, ltype)) {
4190         _gst_value_list_append_and_take_value (dest, &subtraction);
4191       } else {
4192         GValue temp;
4193
4194         gst_value_move (&temp, dest);
4195         gst_value_list_concat (dest, &temp, &subtraction);
4196         g_value_unset (&temp);
4197         g_value_unset (&subtraction);
4198       }
4199     }
4200   }
4201   return ret;
4202 }
4203
4204 static gboolean
4205 gst_value_subtract_list (GValue * dest, const GValue * minuend,
4206     const GValue * subtrahend)
4207 {
4208   guint i, size;
4209   GValue data[2] = { {0,}, {0,} };
4210   GValue *subtraction = &data[0], *result = &data[1];
4211
4212   gst_value_init_and_copy (result, minuend);
4213   size = VALUE_LIST_SIZE (subtrahend);
4214   for (i = 0; i < size; i++) {
4215     const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
4216
4217     if (gst_value_subtract (subtraction, result, cur)) {
4218       GValue *temp = result;
4219
4220       result = subtraction;
4221       subtraction = temp;
4222       g_value_unset (subtraction);
4223     } else {
4224       g_value_unset (result);
4225       return FALSE;
4226     }
4227   }
4228   if (dest) {
4229     gst_value_move (dest, result);
4230   } else {
4231     g_value_unset (result);
4232   }
4233   return TRUE;
4234 }
4235
4236 static gboolean
4237 gst_value_subtract_fraction_fraction_range (GValue * dest,
4238     const GValue * minuend, const GValue * subtrahend)
4239 {
4240   const GValue *min = gst_value_get_fraction_range_min (subtrahend);
4241   const GValue *max = gst_value_get_fraction_range_max (subtrahend);
4242   GstValueCompareFunc compare;
4243
4244   if ((compare = gst_value_get_compare_func (minuend))) {
4245     /* subtracting a range from an fraction only works if the fraction
4246      * is not in the range */
4247     if (gst_value_compare_with_func (minuend, min, compare) ==
4248         GST_VALUE_LESS_THAN ||
4249         gst_value_compare_with_func (minuend, max, compare) ==
4250         GST_VALUE_GREATER_THAN) {
4251       /* and the result is the value */
4252       if (dest)
4253         gst_value_init_and_copy (dest, minuend);
4254       return TRUE;
4255     }
4256   }
4257   return FALSE;
4258 }
4259
4260 static gboolean
4261 gst_value_subtract_fraction_range_fraction (GValue * dest,
4262     const GValue * minuend, const GValue * subtrahend)
4263 {
4264   /* since we don't have open ranges, we cannot create a hole in
4265    * a range. We return the original range */
4266   if (dest)
4267     gst_value_init_and_copy (dest, minuend);
4268   return TRUE;
4269 }
4270
4271 static gboolean
4272 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
4273     const GValue * minuend, const GValue * subtrahend)
4274 {
4275   /* since we don't have open ranges, we have to approximate */
4276   /* done like with ints and doubles. Creates a list of 2 fraction ranges */
4277   const GValue *min1 = gst_value_get_fraction_range_min (minuend);
4278   const GValue *max2 = gst_value_get_fraction_range_max (minuend);
4279   const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
4280   const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
4281   gint cmp1, cmp2;
4282   GValue v1 = { 0, };
4283   GValue v2 = { 0, };
4284   GValue *pv1, *pv2;            /* yeah, hungarian! */
4285   GstValueCompareFunc compare;
4286
4287   g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
4288   g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
4289
4290   compare = gst_value_get_compare_func (min1);
4291   g_return_val_if_fail (compare, FALSE);
4292
4293   cmp1 = gst_value_compare_with_func (max2, max1, compare);
4294   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
4295   if (cmp1 == GST_VALUE_LESS_THAN)
4296     max1 = max2;
4297   cmp1 = gst_value_compare_with_func (min1, min2, compare);
4298   g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
4299   if (cmp1 == GST_VALUE_GREATER_THAN)
4300     min2 = min1;
4301
4302   cmp1 = gst_value_compare_with_func (min1, max1, compare);
4303   cmp2 = gst_value_compare_with_func (min2, max2, compare);
4304
4305   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
4306     pv1 = &v1;
4307     pv2 = &v2;
4308   } else if (cmp1 == GST_VALUE_LESS_THAN) {
4309     pv1 = dest;
4310     pv2 = NULL;
4311   } else if (cmp2 == GST_VALUE_LESS_THAN) {
4312     pv1 = NULL;
4313     pv2 = dest;
4314   } else {
4315     return FALSE;
4316   }
4317
4318   if (!dest)
4319     return TRUE;
4320
4321   if (cmp1 == GST_VALUE_LESS_THAN) {
4322     g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
4323     gst_value_set_fraction_range (pv1, min1, max1);
4324   }
4325   if (cmp2 == GST_VALUE_LESS_THAN) {
4326     g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
4327     gst_value_set_fraction_range (pv2, min2, max2);
4328   }
4329
4330   if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
4331     gst_value_list_concat (dest, pv1, pv2);
4332     g_value_unset (pv1);
4333     g_value_unset (pv2);
4334   }
4335   return TRUE;
4336 }
4337
4338
4339 /**************
4340  * comparison *
4341  **************/
4342
4343 /*
4344  * gst_value_get_compare_func:
4345  * @value1: a value to get the compare function for
4346  *
4347  * Determines the compare function to be used with values of the same type as
4348  * @value1. The function can be given to gst_value_compare_with_func().
4349  *
4350  * Returns: A #GstValueCompareFunc value
4351  */
4352 static GstValueCompareFunc
4353 gst_value_get_compare_func (const GValue * value1)
4354 {
4355   GstValueTable *table, *best = NULL;
4356   guint i;
4357   GType type1;
4358
4359   type1 = G_VALUE_TYPE (value1);
4360
4361   /* this is a fast check */
4362   best = gst_value_hash_lookup_type (type1);
4363
4364   /* slower checks */
4365   if (G_UNLIKELY (!best || !best->compare)) {
4366     guint len = gst_value_table->len;
4367
4368     best = NULL;
4369     for (i = 0; i < len; i++) {
4370       table = &g_array_index (gst_value_table, GstValueTable, i);
4371       if (table->compare && g_type_is_a (type1, table->type)) {
4372         if (!best || g_type_is_a (table->type, best->type))
4373           best = table;
4374       }
4375     }
4376   }
4377   if (G_LIKELY (best))
4378     return best->compare;
4379
4380   return NULL;
4381 }
4382
4383 /**
4384  * gst_value_can_compare:
4385  * @value1: a value to compare
4386  * @value2: another value to compare
4387  *
4388  * Determines if @value1 and @value2 can be compared.
4389  *
4390  * Returns: %TRUE if the values can be compared
4391  */
4392 gboolean
4393 gst_value_can_compare (const GValue * value1, const GValue * value2)
4394 {
4395   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4396   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4397
4398   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4399     return FALSE;
4400
4401   return gst_value_get_compare_func (value1) != NULL;
4402 }
4403
4404 static gboolean
4405 gst_value_list_equals_range (const GValue * list, const GValue * value)
4406 {
4407   const GValue *first;
4408   guint list_size, n;
4409
4410   g_return_val_if_fail (G_IS_VALUE (list), FALSE);
4411   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
4412   g_return_val_if_fail (GST_VALUE_HOLDS_LIST (list), FALSE);
4413
4414   /* TODO: compare against an empty list ? No type though... */
4415   list_size = VALUE_LIST_SIZE (list);
4416   if (list_size == 0)
4417     return FALSE;
4418
4419   /* compare the basic types - they have to match */
4420   first = VALUE_LIST_GET_VALUE (list, 0);
4421 #define CHECK_TYPES(type,prefix) \
4422   (prefix##_VALUE_HOLDS_##type(first) && GST_VALUE_HOLDS_##type##_RANGE (value))
4423   if (CHECK_TYPES (INT, G)) {
4424     const gint rmin = gst_value_get_int_range_min (value);
4425     const gint rmax = gst_value_get_int_range_max (value);
4426     const gint rstep = gst_value_get_int_range_step (value);
4427     if (rstep == 0)
4428       return FALSE;
4429     /* note: this will overflow for min 0 and max INT_MAX, but this
4430        would only be equal to a list of INT_MAX elements, which seems
4431        very unlikely */
4432     if (list_size != rmax / rstep - rmin / rstep + 1)
4433       return FALSE;
4434     for (n = 0; n < list_size; ++n) {
4435       gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
4436       if (v < rmin || v > rmax || v % rstep) {
4437         return FALSE;
4438       }
4439     }
4440     return TRUE;
4441   } else if (CHECK_TYPES (INT64, G)) {
4442     const gint64 rmin = gst_value_get_int64_range_min (value);
4443     const gint64 rmax = gst_value_get_int64_range_max (value);
4444     const gint64 rstep = gst_value_get_int64_range_step (value);
4445     GST_DEBUG ("List/range of int64s");
4446     if (rstep == 0)
4447       return FALSE;
4448     if (list_size != rmax / rstep - rmin / rstep + 1)
4449       return FALSE;
4450     for (n = 0; n < list_size; ++n) {
4451       gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
4452       if (v < rmin || v > rmax || v % rstep)
4453         return FALSE;
4454     }
4455     return TRUE;
4456   }
4457 #undef CHECK_TYPES
4458
4459   /* other combinations don't make sense for equality */
4460   return FALSE;
4461 }
4462
4463 /* "Pure" variant of gst_value_compare which is guaranteed to
4464  * not have list arguments and therefore does basic comparisions
4465  */
4466 static inline gint
4467 _gst_value_compare_nolist (const GValue * value1, const GValue * value2)
4468 {
4469   GstValueCompareFunc compare;
4470
4471   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4472     return GST_VALUE_UNORDERED;
4473
4474   compare = gst_value_get_compare_func (value1);
4475   if (compare) {
4476     return compare (value1, value2);
4477   }
4478
4479   g_critical ("unable to compare values of type %s\n",
4480       g_type_name (G_VALUE_TYPE (value1)));
4481   return GST_VALUE_UNORDERED;
4482 }
4483
4484 /**
4485  * gst_value_compare:
4486  * @value1: a value to compare
4487  * @value2: another value to compare
4488  *
4489  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
4490  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
4491  * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
4492  * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
4493  * If the values are equal, GST_VALUE_EQUAL is returned.
4494  *
4495  * Returns: comparison result
4496  */
4497 gint
4498 gst_value_compare (const GValue * value1, const GValue * value2)
4499 {
4500   GType ltype;
4501
4502   g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
4503   g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
4504
4505   /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
4506      as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
4507   ltype = gst_value_list_get_type ();
4508   if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)) {
4509     gint i, n, ret;
4510
4511     if (gst_value_list_equals_range (value1, value2)) {
4512       return GST_VALUE_EQUAL;
4513     }
4514
4515     n = gst_value_list_get_size (value1);
4516     if (n == 0)
4517       return GST_VALUE_UNORDERED;
4518
4519     for (i = 0; i < n; i++) {
4520       const GValue *elt;
4521
4522       elt = gst_value_list_get_value (value1, i);
4523       ret = gst_value_compare (elt, value2);
4524       if (ret != GST_VALUE_EQUAL && n == 1)
4525         return ret;
4526       else if (ret != GST_VALUE_EQUAL)
4527         return GST_VALUE_UNORDERED;
4528     }
4529
4530     return GST_VALUE_EQUAL;
4531   } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)) {
4532     gint i, n, ret;
4533
4534     if (gst_value_list_equals_range (value2, value1)) {
4535       return GST_VALUE_EQUAL;
4536     }
4537
4538     n = gst_value_list_get_size (value2);
4539     if (n == 0)
4540       return GST_VALUE_UNORDERED;
4541
4542     for (i = 0; i < n; i++) {
4543       const GValue *elt;
4544
4545       elt = gst_value_list_get_value (value2, i);
4546       ret = gst_value_compare (elt, value1);
4547       if (ret != GST_VALUE_EQUAL && n == 1)
4548         return ret;
4549       else if (ret != GST_VALUE_EQUAL)
4550         return GST_VALUE_UNORDERED;
4551     }
4552
4553     return GST_VALUE_EQUAL;
4554   }
4555
4556   /* And now handle the generic case */
4557   return _gst_value_compare_nolist (value1, value2);
4558 }
4559
4560 /*
4561  * gst_value_compare_with_func:
4562  * @value1: a value to compare
4563  * @value2: another value to compare
4564  * @compare: compare function
4565  *
4566  * Compares @value1 and @value2 using the @compare function. Works like
4567  * gst_value_compare() but allows to save time determining the compare function
4568  * a multiple times. 
4569  *
4570  * Returns: comparison result
4571  */
4572 static gint
4573 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
4574     GstValueCompareFunc compare)
4575 {
4576   g_assert (compare);
4577
4578   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4579     return GST_VALUE_UNORDERED;
4580
4581   return compare (value1, value2);
4582 }
4583
4584 /* union */
4585
4586 /**
4587  * gst_value_can_union:
4588  * @value1: a value to union
4589  * @value2: another value to union
4590  *
4591  * Determines if @value1 and @value2 can be non-trivially unioned.
4592  * Any two values can be trivially unioned by adding both of them
4593  * to a #GstValueList.  However, certain types have the possibility
4594  * to be unioned in a simpler way.  For example, an integer range
4595  * and an integer can be unioned if the integer is a subset of the
4596  * integer range.  If there is the possibility that two values can
4597  * be unioned, this function returns %TRUE.
4598  *
4599  * Returns: %TRUE if there is a function allowing the two values to
4600  * be unioned.
4601  */
4602 gboolean
4603 gst_value_can_union (const GValue * value1, const GValue * value2)
4604 {
4605   GstValueUnionInfo *union_info;
4606   guint i, len;
4607
4608   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4609   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4610
4611   len = gst_value_union_funcs->len;
4612
4613   for (i = 0; i < len; i++) {
4614     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4615     if (union_info->type1 == G_VALUE_TYPE (value1) &&
4616         union_info->type2 == G_VALUE_TYPE (value2))
4617       return TRUE;
4618     if (union_info->type1 == G_VALUE_TYPE (value2) &&
4619         union_info->type2 == G_VALUE_TYPE (value1))
4620       return TRUE;
4621   }
4622
4623   return FALSE;
4624 }
4625
4626 /**
4627  * gst_value_union:
4628  * @dest: (out caller-allocates): the destination value
4629  * @value1: a value to union
4630  * @value2: another value to union
4631  *
4632  * Creates a GValue corresponding to the union of @value1 and @value2.
4633  *
4634  * Returns: %TRUE if the union succeeded.
4635  */
4636 gboolean
4637 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
4638 {
4639   const GstValueUnionInfo *union_info;
4640   guint i, len;
4641   GType type1, type2;
4642
4643   g_return_val_if_fail (dest != NULL, FALSE);
4644   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4645   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4646   g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
4647       FALSE);
4648
4649   len = gst_value_union_funcs->len;
4650   type1 = G_VALUE_TYPE (value1);
4651   type2 = G_VALUE_TYPE (value2);
4652
4653   for (i = 0; i < len; i++) {
4654     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4655     if (union_info->type1 == type1 && union_info->type2 == type2) {
4656       return union_info->func (dest, value1, value2);
4657     }
4658     if (union_info->type1 == type2 && union_info->type2 == type1) {
4659       return union_info->func (dest, value2, value1);
4660     }
4661   }
4662
4663   gst_value_list_concat (dest, value1, value2);
4664   return TRUE;
4665 }
4666
4667 /* gst_value_register_union_func: (skip)
4668  * @type1: a type to union
4669  * @type2: another type to union
4670  * @func: a function that implements creating a union between the two types
4671  *
4672  * Registers a union function that can create a union between #GValue items
4673  * of the type @type1 and @type2.
4674  *
4675  * Union functions should be registered at startup before any pipelines are
4676  * started, as gst_value_register_union_func() is not thread-safe and cannot
4677  * be used at the same time as gst_value_union() or gst_value_can_union().
4678  */
4679 static void
4680 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
4681 {
4682   GstValueUnionInfo union_info;
4683
4684   union_info.type1 = type1;
4685   union_info.type2 = type2;
4686   union_info.func = func;
4687
4688   g_array_append_val (gst_value_union_funcs, union_info);
4689 }
4690
4691 /* intersection */
4692
4693 /**
4694  * gst_value_can_intersect:
4695  * @value1: a value to intersect
4696  * @value2: another value to intersect
4697  *
4698  * Determines if intersecting two values will produce a valid result.
4699  * Two values will produce a valid intersection if they have the same
4700  * type.
4701  *
4702  * Returns: %TRUE if the values can intersect
4703  */
4704 gboolean
4705 gst_value_can_intersect (const GValue * value1, const GValue * value2)
4706 {
4707   GstValueIntersectInfo *intersect_info;
4708   guint i, len;
4709   GType ltype, type1, type2;
4710
4711   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4712   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4713
4714   ltype = gst_value_list_get_type ();
4715
4716   /* special cases */
4717   if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
4718     return TRUE;
4719
4720   type1 = G_VALUE_TYPE (value1);
4721   type2 = G_VALUE_TYPE (value2);
4722
4723   /* practically all GstValue types have a compare function (_can_compare=TRUE)
4724    * GstStructure and GstCaps have npot, but are intersectable */
4725   if (type1 == type2)
4726     return TRUE;
4727
4728   /* check registered intersect functions */
4729   len = gst_value_intersect_funcs->len;
4730   for (i = 0; i < len; i++) {
4731     intersect_info = &g_array_index (gst_value_intersect_funcs,
4732         GstValueIntersectInfo, i);
4733     if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
4734         (intersect_info->type1 == type2 && intersect_info->type2 == type1))
4735       return TRUE;
4736   }
4737
4738   return gst_value_can_compare (value1, value2);
4739 }
4740
4741 /**
4742  * gst_value_intersect:
4743  * @dest: (out caller-allocates) (transfer full): a uninitialized #GValue that will hold the calculated
4744  * intersection value. May be %NULL if the resulting set if not needed.
4745  * @value1: a value to intersect
4746  * @value2: another value to intersect
4747  *
4748  * Calculates the intersection of two values.  If the values have
4749  * a non-empty intersection, the value representing the intersection
4750  * is placed in @dest, unless %NULL.  If the intersection is non-empty,
4751  * @dest is not modified.
4752  *
4753  * Returns: %TRUE if the intersection is non-empty
4754  */
4755 gboolean
4756 gst_value_intersect (GValue * dest, const GValue * value1,
4757     const GValue * value2)
4758 {
4759   GstValueIntersectInfo *intersect_info;
4760   guint i, len;
4761   GType ltype, type1, type2;
4762
4763   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4764   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4765
4766   ltype = gst_value_list_get_type ();
4767
4768   /* special cases first */
4769   if (G_VALUE_HOLDS (value1, ltype))
4770     return gst_value_intersect_list (dest, value1, value2);
4771   if (G_VALUE_HOLDS (value2, ltype))
4772     return gst_value_intersect_list (dest, value2, value1);
4773
4774   if (_gst_value_compare_nolist (value1, value2) == GST_VALUE_EQUAL) {
4775     if (dest)
4776       gst_value_init_and_copy (dest, value1);
4777     return TRUE;
4778   }
4779
4780   type1 = G_VALUE_TYPE (value1);
4781   type2 = G_VALUE_TYPE (value2);
4782
4783   len = gst_value_intersect_funcs->len;
4784   for (i = 0; i < len; i++) {
4785     intersect_info = &g_array_index (gst_value_intersect_funcs,
4786         GstValueIntersectInfo, i);
4787     if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
4788       return intersect_info->func (dest, value1, value2);
4789     }
4790     if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
4791       return intersect_info->func (dest, value2, value1);
4792     }
4793   }
4794   return FALSE;
4795 }
4796
4797
4798
4799 /* gst_value_register_intersect_func: (skip)
4800  * @type1: the first type to intersect
4801  * @type2: the second type to intersect
4802  * @func: the intersection function
4803  *
4804  * Registers a function that is called to calculate the intersection
4805  * of the values having the types @type1 and @type2.
4806  *
4807  * Intersect functions should be registered at startup before any pipelines are
4808  * started, as gst_value_register_intersect_func() is not thread-safe and
4809  * cannot be used at the same time as gst_value_intersect() or
4810  * gst_value_can_intersect().
4811  */
4812 static void
4813 gst_value_register_intersect_func (GType type1, GType type2,
4814     GstValueIntersectFunc func)
4815 {
4816   GstValueIntersectInfo intersect_info;
4817
4818   intersect_info.type1 = type1;
4819   intersect_info.type2 = type2;
4820   intersect_info.func = func;
4821
4822   g_array_append_val (gst_value_intersect_funcs, intersect_info);
4823 }
4824
4825
4826 /* subtraction */
4827
4828 /**
4829  * gst_value_subtract:
4830  * @dest: (out caller-allocates): the destination value for the result if the
4831  *     subtraction is not empty. May be %NULL, in which case the resulting set
4832  *     will not be computed, which can give a fair speedup.
4833  * @minuend: the value to subtract from
4834  * @subtrahend: the value to subtract
4835  *
4836  * Subtracts @subtrahend from @minuend and stores the result in @dest.
4837  * Note that this means subtraction as in sets, not as in mathematics.
4838  *
4839  * Returns: %TRUE if the subtraction is not empty
4840  */
4841 gboolean
4842 gst_value_subtract (GValue * dest, const GValue * minuend,
4843     const GValue * subtrahend)
4844 {
4845   GstValueSubtractInfo *info;
4846   guint i, len;
4847   GType ltype, mtype, stype;
4848
4849   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4850   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4851
4852   ltype = gst_value_list_get_type ();
4853
4854   /* special cases first */
4855   if (G_VALUE_HOLDS (minuend, ltype))
4856     return gst_value_subtract_from_list (dest, minuend, subtrahend);
4857   if (G_VALUE_HOLDS (subtrahend, ltype))
4858     return gst_value_subtract_list (dest, minuend, subtrahend);
4859
4860   mtype = G_VALUE_TYPE (minuend);
4861   stype = G_VALUE_TYPE (subtrahend);
4862
4863   len = gst_value_subtract_funcs->len;
4864   for (i = 0; i < len; i++) {
4865     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4866     if (info->minuend == mtype && info->subtrahend == stype) {
4867       return info->func (dest, minuend, subtrahend);
4868     }
4869   }
4870
4871   if (_gst_value_compare_nolist (minuend, subtrahend) != GST_VALUE_EQUAL) {
4872     if (dest)
4873       gst_value_init_and_copy (dest, minuend);
4874     return TRUE;
4875   }
4876
4877   return FALSE;
4878 }
4879
4880 #if 0
4881 gboolean
4882 gst_value_subtract (GValue * dest, const GValue * minuend,
4883     const GValue * subtrahend)
4884 {
4885   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
4886
4887   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
4888       gst_value_serialize (subtrahend),
4889       ret ? gst_value_serialize (dest) : "---");
4890   return ret;
4891 }
4892 #endif
4893
4894 /**
4895  * gst_value_can_subtract:
4896  * @minuend: the value to subtract from
4897  * @subtrahend: the value to subtract
4898  *
4899  * Checks if it's possible to subtract @subtrahend from @minuend.
4900  *
4901  * Returns: %TRUE if a subtraction is possible
4902  */
4903 gboolean
4904 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
4905 {
4906   GstValueSubtractInfo *info;
4907   guint i, len;
4908   GType ltype, mtype, stype;
4909
4910   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4911   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4912
4913   ltype = gst_value_list_get_type ();
4914
4915   /* special cases */
4916   if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
4917     return TRUE;
4918
4919   mtype = G_VALUE_TYPE (minuend);
4920   stype = G_VALUE_TYPE (subtrahend);
4921
4922   len = gst_value_subtract_funcs->len;
4923   for (i = 0; i < len; i++) {
4924     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4925     if (info->minuend == mtype && info->subtrahend == stype)
4926       return TRUE;
4927   }
4928
4929   return gst_value_can_compare (minuend, subtrahend);
4930 }
4931
4932 /* gst_value_register_subtract_func: (skip)
4933  * @minuend_type: type of the minuend
4934  * @subtrahend_type: type of the subtrahend
4935  * @func: function to use
4936  *
4937  * Registers @func as a function capable of subtracting the values of
4938  * @subtrahend_type from values of @minuend_type.
4939  *
4940  * Subtract functions should be registered at startup before any pipelines are
4941  * started, as gst_value_register_subtract_func() is not thread-safe and
4942  * cannot be used at the same time as gst_value_subtract().
4943  */
4944 static void
4945 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
4946     GstValueSubtractFunc func)
4947 {
4948   GstValueSubtractInfo info;
4949
4950   g_return_if_fail (!gst_type_is_fixed (minuend_type)
4951       || !gst_type_is_fixed (subtrahend_type));
4952
4953   info.minuend = minuend_type;
4954   info.subtrahend = subtrahend_type;
4955   info.func = func;
4956
4957   g_array_append_val (gst_value_subtract_funcs, info);
4958 }
4959
4960 /**
4961  * gst_value_register:
4962  * @table: structure containing functions to register
4963  *
4964  * Registers functions to perform calculations on #GValue items of a given
4965  * type. Each type can only be added once.
4966  */
4967 void
4968 gst_value_register (const GstValueTable * table)
4969 {
4970   GstValueTable *found;
4971
4972   g_return_if_fail (table != NULL);
4973
4974   g_array_append_val (gst_value_table, *table);
4975
4976   found = gst_value_hash_lookup_type (table->type);
4977   if (found)
4978     g_warning ("adding type %s multiple times", g_type_name (table->type));
4979
4980   /* FIXME: we're not really doing the const justice, we assume the table is
4981    * static */
4982   gst_value_hash_add_type (table->type, table);
4983 }
4984
4985 /**
4986  * gst_value_init_and_copy:
4987  * @dest: (out caller-allocates): the target value
4988  * @src: the source value
4989  *
4990  * Initialises the target value to be of the same type as source and then copies
4991  * the contents from source to target.
4992  */
4993 void
4994 gst_value_init_and_copy (GValue * dest, const GValue * src)
4995 {
4996   g_return_if_fail (G_IS_VALUE (src));
4997   g_return_if_fail (dest != NULL);
4998
4999   g_value_init (dest, G_VALUE_TYPE (src));
5000   g_value_copy (src, dest);
5001 }
5002
5003 /* move src into dest and clear src */
5004 static void
5005 gst_value_move (GValue * dest, GValue * src)
5006 {
5007   g_assert (G_IS_VALUE (src));
5008   g_assert (dest != NULL);
5009
5010   *dest = *src;
5011   memset (src, 0, sizeof (GValue));
5012 }
5013
5014 /**
5015  * gst_value_serialize:
5016  * @value: a #GValue to serialize
5017  *
5018  * tries to transform the given @value into a string representation that allows
5019  * getting back this string later on using gst_value_deserialize().
5020  *
5021  * Free-function: g_free
5022  *
5023  * Returns: (transfer full): the serialization for @value or %NULL if none exists
5024  */
5025 gchar *
5026 gst_value_serialize (const GValue * value)
5027 {
5028   guint i, len;
5029   GValue s_val = { 0 };
5030   GstValueTable *table, *best;
5031   gchar *s;
5032   GType type;
5033
5034   g_return_val_if_fail (G_IS_VALUE (value), NULL);
5035
5036   type = G_VALUE_TYPE (value);
5037
5038   best = gst_value_hash_lookup_type (type);
5039
5040   if (G_UNLIKELY (!best || !best->serialize)) {
5041     len = gst_value_table->len;
5042     best = NULL;
5043     for (i = 0; i < len; i++) {
5044       table = &g_array_index (gst_value_table, GstValueTable, i);
5045       if (table->serialize && g_type_is_a (type, table->type)) {
5046         if (!best || g_type_is_a (table->type, best->type))
5047           best = table;
5048       }
5049     }
5050   }
5051   if (G_LIKELY (best))
5052     return best->serialize (value);
5053
5054   g_value_init (&s_val, G_TYPE_STRING);
5055   if (g_value_transform (value, &s_val)) {
5056     s = gst_string_wrap (g_value_get_string (&s_val));
5057   } else {
5058     s = NULL;
5059   }
5060   g_value_unset (&s_val);
5061
5062   return s;
5063 }
5064
5065 /**
5066  * gst_value_deserialize:
5067  * @dest: (out caller-allocates): #GValue to fill with contents of
5068  *     deserialization
5069  * @src: string to deserialize
5070  *
5071  * Tries to deserialize a string into the type specified by the given GValue.
5072  * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
5073  *
5074  * Returns: %TRUE on success
5075  */
5076 gboolean
5077 gst_value_deserialize (GValue * dest, const gchar * src)
5078 {
5079   GstValueTable *table, *best;
5080   guint i, len;
5081   GType type;
5082
5083   g_return_val_if_fail (src != NULL, FALSE);
5084   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
5085
5086   type = G_VALUE_TYPE (dest);
5087
5088   best = gst_value_hash_lookup_type (type);
5089   if (G_UNLIKELY (!best || !best->deserialize)) {
5090     len = gst_value_table->len;
5091     best = NULL;
5092     for (i = 0; i < len; i++) {
5093       table = &g_array_index (gst_value_table, GstValueTable, i);
5094       if (table->deserialize && g_type_is_a (type, table->type)) {
5095         if (!best || g_type_is_a (table->type, best->type))
5096           best = table;
5097       }
5098     }
5099   }
5100   if (G_LIKELY (best))
5101     return best->deserialize (dest, src);
5102
5103   return FALSE;
5104 }
5105
5106 /**
5107  * gst_value_is_fixed:
5108  * @value: the #GValue to check
5109  *
5110  * Tests if the given GValue, if available in a GstStructure (or any other
5111  * container) contains a "fixed" (which means: one value) or an "unfixed"
5112  * (which means: multiple possible values, such as data lists or data
5113  * ranges) value.
5114  *
5115  * Returns: true if the value is "fixed".
5116  */
5117
5118 gboolean
5119 gst_value_is_fixed (const GValue * value)
5120 {
5121   GType type;
5122
5123   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
5124
5125   type = G_VALUE_TYPE (value);
5126
5127   /* the most common types are just basic plain glib types */
5128   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
5129     return TRUE;
5130   }
5131
5132   if (type == GST_TYPE_ARRAY) {
5133     gint size, n;
5134     const GValue *kid;
5135
5136     /* check recursively */
5137     size = gst_value_array_get_size (value);
5138     for (n = 0; n < size; n++) {
5139       kid = gst_value_array_get_value (value, n);
5140       if (!gst_value_is_fixed (kid))
5141         return FALSE;
5142     }
5143     return TRUE;
5144   }
5145   return gst_type_is_fixed (type);
5146 }
5147
5148 /**
5149  * gst_value_fixate:
5150  * @dest: the #GValue destination
5151  * @src: the #GValue to fixate
5152  *
5153  * Fixate @src into a new value @dest.
5154  * For ranges, the first element is taken. For lists and arrays, the
5155  * first item is fixated and returned.
5156  * If @src is already fixed, this function returns %FALSE.
5157  *
5158  * Returns: %TRUE if @dest contains a fixated version of @src.
5159  */
5160 gboolean
5161 gst_value_fixate (GValue * dest, const GValue * src)
5162 {
5163   g_return_val_if_fail (G_IS_VALUE (src), FALSE);
5164   g_return_val_if_fail (dest != NULL, FALSE);
5165
5166   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
5167     g_value_init (dest, G_TYPE_INT);
5168     g_value_set_int (dest, gst_value_get_int_range_min (src));
5169   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
5170     g_value_init (dest, G_TYPE_DOUBLE);
5171     g_value_set_double (dest, gst_value_get_double_range_min (src));
5172   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
5173     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
5174   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
5175     GValue temp = { 0 };
5176
5177     /* list could be empty */
5178     if (gst_value_list_get_size (src) <= 0)
5179       return FALSE;
5180
5181     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
5182
5183     if (!gst_value_fixate (dest, &temp)) {
5184       gst_value_move (dest, &temp);
5185     } else {
5186       g_value_unset (&temp);
5187     }
5188   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
5189     gboolean res = FALSE;
5190     guint n, len;
5191
5192     len = gst_value_array_get_size (src);
5193     g_value_init (dest, GST_TYPE_ARRAY);
5194     for (n = 0; n < len; n++) {
5195       GValue kid = { 0 };
5196       const GValue *orig_kid = gst_value_array_get_value (src, n);
5197
5198       if (!gst_value_fixate (&kid, orig_kid))
5199         gst_value_init_and_copy (&kid, orig_kid);
5200       else
5201         res = TRUE;
5202       _gst_value_array_append_and_take_value (dest, &kid);
5203     }
5204
5205     if (!res)
5206       g_value_unset (dest);
5207
5208     return res;
5209   } else {
5210     return FALSE;
5211   }
5212   return TRUE;
5213 }
5214
5215
5216 /************
5217  * fraction *
5218  ************/
5219
5220 /* helper functions */
5221 static void
5222 gst_value_init_fraction (GValue * value)
5223 {
5224   value->data[0].v_int = 0;
5225   value->data[1].v_int = 1;
5226 }
5227
5228 static void
5229 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
5230 {
5231   dest_value->data[0].v_int = src_value->data[0].v_int;
5232   dest_value->data[1].v_int = src_value->data[1].v_int;
5233 }
5234
5235 static gchar *
5236 gst_value_collect_fraction (GValue * value, guint n_collect_values,
5237     GTypeCValue * collect_values, guint collect_flags)
5238 {
5239   if (n_collect_values != 2)
5240     return g_strdup_printf ("not enough value locations for `%s' passed",
5241         G_VALUE_TYPE_NAME (value));
5242   if (collect_values[1].v_int == 0)
5243     return g_strdup_printf ("passed '0' as denominator for `%s'",
5244         G_VALUE_TYPE_NAME (value));
5245   if (collect_values[0].v_int < -G_MAXINT)
5246     return
5247         g_strdup_printf
5248         ("passed value smaller than -G_MAXINT as numerator for `%s'",
5249         G_VALUE_TYPE_NAME (value));
5250   if (collect_values[1].v_int < -G_MAXINT)
5251     return
5252         g_strdup_printf
5253         ("passed value smaller than -G_MAXINT as denominator for `%s'",
5254         G_VALUE_TYPE_NAME (value));
5255
5256   gst_value_set_fraction (value,
5257       collect_values[0].v_int, collect_values[1].v_int);
5258
5259   return NULL;
5260 }
5261
5262 static gchar *
5263 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
5264     GTypeCValue * collect_values, guint collect_flags)
5265 {
5266   gint *numerator = collect_values[0].v_pointer;
5267   gint *denominator = collect_values[1].v_pointer;
5268
5269   if (!numerator)
5270     return g_strdup_printf ("numerator for `%s' passed as NULL",
5271         G_VALUE_TYPE_NAME (value));
5272   if (!denominator)
5273     return g_strdup_printf ("denominator for `%s' passed as NULL",
5274         G_VALUE_TYPE_NAME (value));
5275
5276   *numerator = value->data[0].v_int;
5277   *denominator = value->data[1].v_int;
5278
5279   return NULL;
5280 }
5281
5282 /**
5283  * gst_value_set_fraction:
5284  * @value: a GValue initialized to #GST_TYPE_FRACTION
5285  * @numerator: the numerator of the fraction
5286  * @denominator: the denominator of the fraction
5287  *
5288  * Sets @value to the fraction specified by @numerator over @denominator.
5289  * The fraction gets reduced to the smallest numerator and denominator,
5290  * and if necessary the sign is moved to the numerator.
5291  */
5292 void
5293 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
5294 {
5295   gint gcd = 0;
5296
5297   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
5298   g_return_if_fail (denominator != 0);
5299   g_return_if_fail (denominator >= -G_MAXINT);
5300   g_return_if_fail (numerator >= -G_MAXINT);
5301
5302   /* normalize sign */
5303   if (denominator < 0) {
5304     numerator = -numerator;
5305     denominator = -denominator;
5306   }
5307
5308   /* check for reduction */
5309   gcd = gst_util_greatest_common_divisor (numerator, denominator);
5310   if (gcd) {
5311     numerator /= gcd;
5312     denominator /= gcd;
5313   }
5314
5315   g_assert (denominator > 0);
5316
5317   value->data[0].v_int = numerator;
5318   value->data[1].v_int = denominator;
5319 }
5320
5321 /**
5322  * gst_value_get_fraction_numerator:
5323  * @value: a GValue initialized to #GST_TYPE_FRACTION
5324  *
5325  * Gets the numerator of the fraction specified by @value.
5326  *
5327  * Returns: the numerator of the fraction.
5328  */
5329 gint
5330 gst_value_get_fraction_numerator (const GValue * value)
5331 {
5332   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
5333
5334   return value->data[0].v_int;
5335 }
5336
5337 /**
5338  * gst_value_get_fraction_denominator:
5339  * @value: a GValue initialized to #GST_TYPE_FRACTION
5340  *
5341  * Gets the denominator of the fraction specified by @value.
5342  *
5343  * Returns: the denominator of the fraction.
5344  */
5345 gint
5346 gst_value_get_fraction_denominator (const GValue * value)
5347 {
5348   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
5349
5350   return value->data[1].v_int;
5351 }
5352
5353 /**
5354  * gst_value_fraction_multiply:
5355  * @product: a GValue initialized to #GST_TYPE_FRACTION
5356  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
5357  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
5358  *
5359  * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
5360  * @product to the product of the two fractions.
5361  *
5362  * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
5363  */
5364 gboolean
5365 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
5366     const GValue * factor2)
5367 {
5368   gint n1, n2, d1, d2;
5369   gint res_n, res_d;
5370
5371   g_return_val_if_fail (product != NULL, FALSE);
5372   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
5373   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
5374
5375   n1 = factor1->data[0].v_int;
5376   n2 = factor2->data[0].v_int;
5377   d1 = factor1->data[1].v_int;
5378   d2 = factor2->data[1].v_int;
5379
5380   if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
5381     return FALSE;
5382
5383   gst_value_set_fraction (product, res_n, res_d);
5384
5385   return TRUE;
5386 }
5387
5388 /**
5389  * gst_value_fraction_subtract:
5390  * @dest: a GValue initialized to #GST_TYPE_FRACTION
5391  * @minuend: a GValue initialized to #GST_TYPE_FRACTION
5392  * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
5393  *
5394  * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
5395  *
5396  * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
5397  */
5398 gboolean
5399 gst_value_fraction_subtract (GValue * dest,
5400     const GValue * minuend, const GValue * subtrahend)
5401 {
5402   gint n1, n2, d1, d2;
5403   gint res_n, res_d;
5404
5405   g_return_val_if_fail (dest != NULL, FALSE);
5406   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
5407   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
5408
5409   n1 = minuend->data[0].v_int;
5410   n2 = subtrahend->data[0].v_int;
5411   d1 = minuend->data[1].v_int;
5412   d2 = subtrahend->data[1].v_int;
5413
5414   if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
5415     return FALSE;
5416   gst_value_set_fraction (dest, res_n, res_d);
5417
5418   return TRUE;
5419 }
5420
5421 static gchar *
5422 gst_value_serialize_fraction (const GValue * value)
5423 {
5424   gint32 numerator = value->data[0].v_int;
5425   gint32 denominator = value->data[1].v_int;
5426   gboolean positive = TRUE;
5427
5428   /* get the sign and make components absolute */
5429   if (numerator < 0) {
5430     numerator = -numerator;
5431     positive = !positive;
5432   }
5433   if (denominator < 0) {
5434     denominator = -denominator;
5435     positive = !positive;
5436   }
5437
5438   return g_strdup_printf ("%s%d/%d",
5439       positive ? "" : "-", numerator, denominator);
5440 }
5441
5442 static gboolean
5443 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
5444 {
5445   gint num, den;
5446   gint num_chars;
5447
5448   if (G_UNLIKELY (s == NULL))
5449     return FALSE;
5450
5451   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
5452     return FALSE;
5453
5454   if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
5455     if (s[num_chars] != 0)
5456       return FALSE;
5457     if (den == 0)
5458       return FALSE;
5459
5460     gst_value_set_fraction (dest, num, den);
5461     return TRUE;
5462   } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
5463     gst_value_set_fraction (dest, 1, G_MAXINT);
5464     return TRUE;
5465   } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
5466     if (s[num_chars] != 0)
5467       return FALSE;
5468     gst_value_set_fraction (dest, num, 1);
5469     return TRUE;
5470   } else if (g_ascii_strcasecmp (s, "min") == 0) {
5471     gst_value_set_fraction (dest, -G_MAXINT, 1);
5472     return TRUE;
5473   } else if (g_ascii_strcasecmp (s, "max") == 0) {
5474     gst_value_set_fraction (dest, G_MAXINT, 1);
5475     return TRUE;
5476   }
5477
5478   return FALSE;
5479 }
5480
5481 static void
5482 gst_value_transform_fraction_string (const GValue * src_value,
5483     GValue * dest_value)
5484 {
5485   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
5486 }
5487
5488 static void
5489 gst_value_transform_string_fraction (const GValue * src_value,
5490     GValue * dest_value)
5491 {
5492   if (!gst_value_deserialize_fraction (dest_value,
5493           src_value->data[0].v_pointer))
5494     /* If the deserialize fails, ensure we leave the fraction in a
5495      * valid, if incorrect, state */
5496     gst_value_set_fraction (dest_value, 0, 1);
5497 }
5498
5499 static void
5500 gst_value_transform_double_fraction (const GValue * src_value,
5501     GValue * dest_value)
5502 {
5503   gdouble src = g_value_get_double (src_value);
5504   gint n, d;
5505
5506   gst_util_double_to_fraction (src, &n, &d);
5507   gst_value_set_fraction (dest_value, n, d);
5508 }
5509
5510 static void
5511 gst_value_transform_float_fraction (const GValue * src_value,
5512     GValue * dest_value)
5513 {
5514   gfloat src = g_value_get_float (src_value);
5515   gint n, d;
5516
5517   gst_util_double_to_fraction (src, &n, &d);
5518   gst_value_set_fraction (dest_value, n, d);
5519 }
5520
5521 static void
5522 gst_value_transform_fraction_double (const GValue * src_value,
5523     GValue * dest_value)
5524 {
5525   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
5526       ((double) src_value->data[1].v_int);
5527 }
5528
5529 static void
5530 gst_value_transform_fraction_float (const GValue * src_value,
5531     GValue * dest_value)
5532 {
5533   dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
5534       ((float) src_value->data[1].v_int);
5535 }
5536
5537 static gint
5538 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
5539 {
5540   gint n1, n2;
5541   gint d1, d2;
5542   gint ret;
5543
5544   n1 = value1->data[0].v_int;
5545   n2 = value2->data[0].v_int;
5546   d1 = value1->data[1].v_int;
5547   d2 = value2->data[1].v_int;
5548
5549   /* fractions are reduced when set, so we can quickly see if they're equal */
5550   if (n1 == n2 && d1 == d2)
5551     return GST_VALUE_EQUAL;
5552
5553   if (d1 == 0 && d2 == 0)
5554     return GST_VALUE_UNORDERED;
5555   else if (d1 == 0)
5556     return GST_VALUE_GREATER_THAN;
5557   else if (d2 == 0)
5558     return GST_VALUE_LESS_THAN;
5559
5560   ret = gst_util_fraction_compare (n1, d1, n2, d2);
5561   if (ret == -1)
5562     return GST_VALUE_LESS_THAN;
5563   else if (ret == 1)
5564     return GST_VALUE_GREATER_THAN;
5565
5566   /* Equality can't happen here because we check for that
5567    * first already */
5568   g_return_val_if_reached (GST_VALUE_UNORDERED);
5569 }
5570
5571 /*********
5572  * GDate *
5573  *********/
5574
5575 static gint
5576 gst_value_compare_date (const GValue * value1, const GValue * value2)
5577 {
5578   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
5579   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
5580   guint32 j1, j2;
5581
5582   if (date1 == date2)
5583     return GST_VALUE_EQUAL;
5584
5585   if ((date1 == NULL || !g_date_valid (date1))
5586       && (date2 != NULL && g_date_valid (date2))) {
5587     return GST_VALUE_LESS_THAN;
5588   }
5589
5590   if ((date2 == NULL || !g_date_valid (date2))
5591       && (date1 != NULL && g_date_valid (date1))) {
5592     return GST_VALUE_GREATER_THAN;
5593   }
5594
5595   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
5596       || !g_date_valid (date2)) {
5597     return GST_VALUE_UNORDERED;
5598   }
5599
5600   j1 = g_date_get_julian (date1);
5601   j2 = g_date_get_julian (date2);
5602
5603   if (j1 == j2)
5604     return GST_VALUE_EQUAL;
5605   else if (j1 < j2)
5606     return GST_VALUE_LESS_THAN;
5607   else
5608     return GST_VALUE_GREATER_THAN;
5609 }
5610
5611 static gchar *
5612 gst_value_serialize_date (const GValue * val)
5613 {
5614   const GDate *date = (const GDate *) g_value_get_boxed (val);
5615
5616   if (date == NULL || !g_date_valid (date))
5617     return g_strdup ("9999-99-99");
5618
5619   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
5620       g_date_get_month (date), g_date_get_day (date));
5621 }
5622
5623 static gboolean
5624 gst_value_deserialize_date (GValue * dest, const gchar * s)
5625 {
5626   guint year, month, day;
5627
5628   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
5629     return FALSE;
5630
5631   if (!g_date_valid_dmy (day, month, year))
5632     return FALSE;
5633
5634   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
5635   return TRUE;
5636 }
5637
5638 /*************
5639  * GstDateTime *
5640  *************/
5641
5642 static gint
5643 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
5644 {
5645   const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
5646   const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
5647
5648   if (date1 == date2)
5649     return GST_VALUE_EQUAL;
5650
5651   if ((date1 == NULL) && (date2 != NULL)) {
5652     return GST_VALUE_LESS_THAN;
5653   }
5654   if ((date2 == NULL) && (date1 != NULL)) {
5655     return GST_VALUE_LESS_THAN;
5656   }
5657
5658   /* returns GST_VALUE_* */
5659   return __gst_date_time_compare (date1, date2);
5660 }
5661
5662 static gchar *
5663 gst_value_serialize_date_time (const GValue * val)
5664 {
5665   GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
5666
5667   if (date == NULL)
5668     return g_strdup ("null");
5669
5670   return __gst_date_time_serialize (date, TRUE);
5671 }
5672
5673 static gboolean
5674 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
5675 {
5676   GstDateTime *datetime;
5677
5678   if (!s || strcmp (s, "null") == 0) {
5679     return FALSE;
5680   }
5681
5682   datetime = gst_date_time_new_from_iso8601_string (s);
5683   if (datetime != NULL) {
5684     g_value_take_boxed (dest, datetime);
5685     return TRUE;
5686   }
5687   GST_WARNING ("Failed to deserialize date time string '%s'", s);
5688   return FALSE;
5689 }
5690
5691 static void
5692 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
5693 {
5694   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
5695 }
5696
5697 static void
5698 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
5699 {
5700   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
5701 }
5702
5703
5704 /************
5705  * bitmask *
5706  ************/
5707
5708 /* helper functions */
5709 static void
5710 gst_value_init_bitmask (GValue * value)
5711 {
5712   value->data[0].v_uint64 = 0;
5713 }
5714
5715 static void
5716 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
5717 {
5718   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5719 }
5720
5721 static gchar *
5722 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
5723     GTypeCValue * collect_values, guint collect_flags)
5724 {
5725   if (n_collect_values != 1)
5726     return g_strdup_printf ("not enough value locations for `%s' passed",
5727         G_VALUE_TYPE_NAME (value));
5728
5729   gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
5730
5731   return NULL;
5732 }
5733
5734 static gchar *
5735 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
5736     GTypeCValue * collect_values, guint collect_flags)
5737 {
5738   guint64 *bitmask = collect_values[0].v_pointer;
5739
5740   if (!bitmask)
5741     return g_strdup_printf ("value for `%s' passed as NULL",
5742         G_VALUE_TYPE_NAME (value));
5743
5744   *bitmask = value->data[0].v_uint64;
5745
5746   return NULL;
5747 }
5748
5749 /**
5750  * gst_value_set_bitmask:
5751  * @value: a GValue initialized to #GST_TYPE_BITMASK
5752  * @bitmask: the bitmask
5753  *
5754  * Sets @value to the bitmask specified by @bitmask.
5755  */
5756 void
5757 gst_value_set_bitmask (GValue * value, guint64 bitmask)
5758 {
5759   g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
5760
5761   value->data[0].v_uint64 = bitmask;
5762 }
5763
5764 /**
5765  * gst_value_get_bitmask:
5766  * @value: a GValue initialized to #GST_TYPE_BITMASK
5767  *
5768  * Gets the bitmask specified by @value.
5769  *
5770  * Returns: the bitmask.
5771  */
5772 guint64
5773 gst_value_get_bitmask (const GValue * value)
5774 {
5775   g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
5776
5777   return value->data[0].v_uint64;
5778 }
5779
5780 static gchar *
5781 gst_value_serialize_bitmask (const GValue * value)
5782 {
5783   guint64 bitmask = value->data[0].v_uint64;
5784
5785   return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
5786 }
5787
5788 static gboolean
5789 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
5790 {
5791   gchar *endptr = NULL;
5792   guint64 val;
5793
5794   if (G_UNLIKELY (s == NULL))
5795     return FALSE;
5796
5797   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
5798     return FALSE;
5799
5800   val = g_ascii_strtoull (s, &endptr, 16);
5801   if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
5802     return FALSE;
5803   if (val == 0 && endptr == s)
5804     return FALSE;
5805
5806   gst_value_set_bitmask (dest, val);
5807
5808   return TRUE;
5809 }
5810
5811 static void
5812 gst_value_transform_bitmask_string (const GValue * src_value,
5813     GValue * dest_value)
5814 {
5815   dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
5816 }
5817
5818 static void
5819 gst_value_transform_string_bitmask (const GValue * src_value,
5820     GValue * dest_value)
5821 {
5822   if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
5823     gst_value_set_bitmask (dest_value, 0);
5824 }
5825
5826 static void
5827 gst_value_transform_uint64_bitmask (const GValue * src_value,
5828     GValue * dest_value)
5829 {
5830   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5831 }
5832
5833 static void
5834 gst_value_transform_bitmask_uint64 (const GValue * src_value,
5835     GValue * dest_value)
5836 {
5837   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5838 }
5839
5840 static gint
5841 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
5842 {
5843   guint64 v1, v2;
5844
5845   v1 = value1->data[0].v_uint64;
5846   v2 = value2->data[0].v_uint64;
5847
5848   if (v1 == v2)
5849     return GST_VALUE_EQUAL;
5850
5851   return GST_VALUE_UNORDERED;
5852 }
5853
5854
5855 /***********************
5856  * GstAllocationParams *
5857  ***********************/
5858 static gint
5859 gst_value_compare_allocation_params (const GValue * value1,
5860     const GValue * value2)
5861 {
5862   GstAllocationParams *v1, *v2;
5863
5864   v1 = value1->data[0].v_pointer;
5865   v2 = value2->data[0].v_pointer;
5866
5867   if (v1 == NULL && v1 == v2)
5868     return GST_VALUE_EQUAL;
5869
5870   if (v1 == NULL || v2 == NULL)
5871     return GST_VALUE_UNORDERED;
5872
5873   if (v1->flags == v2->flags && v1->align == v2->align &&
5874       v1->prefix == v2->prefix && v1->padding == v2->padding)
5875     return GST_VALUE_EQUAL;
5876
5877   return GST_VALUE_UNORDERED;
5878 }
5879
5880
5881 /************
5882  * GObject *
5883  ************/
5884
5885 static gint
5886 gst_value_compare_object (const GValue * value1, const GValue * value2)
5887 {
5888   gpointer v1, v2;
5889
5890   v1 = value1->data[0].v_pointer;
5891   v2 = value2->data[0].v_pointer;
5892
5893   if (v1 == v2)
5894     return GST_VALUE_EQUAL;
5895
5896   return GST_VALUE_UNORDERED;
5897 }
5898
5899 static void
5900 gst_value_transform_object_string (const GValue * src_value,
5901     GValue * dest_value)
5902 {
5903   GstObject *obj;
5904   gchar *str;
5905
5906   obj = g_value_get_object (src_value);
5907   if (obj) {
5908     str =
5909         g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
5910         GST_OBJECT_NAME (obj));
5911   } else {
5912     str = g_strdup ("NULL");
5913   }
5914
5915   dest_value->data[0].v_pointer = str;
5916 }
5917
5918 static GTypeInfo _info = {
5919   0,
5920   NULL,
5921   NULL,
5922   NULL,
5923   NULL,
5924   NULL,
5925   0,
5926   0,
5927   NULL,
5928   NULL,
5929 };
5930
5931 static GTypeFundamentalInfo _finfo = {
5932   0
5933 };
5934
5935 #define FUNC_VALUE_GET_TYPE(type, name)                         \
5936 GType _gst_ ## type ## _type = 0;                               \
5937                                                                 \
5938 GType gst_ ## type ## _get_type (void)                          \
5939 {                                                               \
5940   static volatile GType gst_ ## type ## _type = 0;              \
5941                                                                 \
5942   if (g_once_init_enter (&gst_ ## type ## _type)) {             \
5943     GType _type;                                                \
5944     _info.value_table = & _gst_ ## type ## _value_table;        \
5945     _type = g_type_register_fundamental (                       \
5946         g_type_fundamental_next (),                             \
5947         name, &_info, &_finfo, 0);                              \
5948     _gst_ ## type ## _type = _type;                              \
5949     g_once_init_leave(&gst_ ## type ## _type, _type);           \
5950   }                                                             \
5951                                                                 \
5952   return gst_ ## type ## _type;                                 \
5953 }
5954
5955 static const GTypeValueTable _gst_int_range_value_table = {
5956   gst_value_init_int_range,
5957   NULL,
5958   gst_value_copy_int_range,
5959   NULL,
5960   (char *) "ii",
5961   gst_value_collect_int_range,
5962   (char *) "pp",
5963   gst_value_lcopy_int_range
5964 };
5965
5966 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
5967
5968 static const GTypeValueTable _gst_int64_range_value_table = {
5969   gst_value_init_int64_range,
5970   gst_value_free_int64_range,
5971   gst_value_copy_int64_range,
5972   NULL,
5973   (char *) "qq",
5974   gst_value_collect_int64_range,
5975   (char *) "pp",
5976   gst_value_lcopy_int64_range
5977 };
5978
5979 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
5980
5981 static const GTypeValueTable _gst_double_range_value_table = {
5982   gst_value_init_double_range,
5983   NULL,
5984   gst_value_copy_double_range,
5985   NULL,
5986   (char *) "dd",
5987   gst_value_collect_double_range,
5988   (char *) "pp",
5989   gst_value_lcopy_double_range
5990 };
5991
5992 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
5993
5994 static const GTypeValueTable _gst_fraction_range_value_table = {
5995   gst_value_init_fraction_range,
5996   gst_value_free_fraction_range,
5997   gst_value_copy_fraction_range,
5998   NULL,
5999   (char *) "iiii",
6000   gst_value_collect_fraction_range,
6001   (char *) "pppp",
6002   gst_value_lcopy_fraction_range
6003 };
6004
6005 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
6006
6007 static const GTypeValueTable _gst_value_list_value_table = {
6008   gst_value_init_list_or_array,
6009   gst_value_free_list_or_array,
6010   gst_value_copy_list_or_array,
6011   gst_value_list_or_array_peek_pointer,
6012   (char *) "p",
6013   gst_value_collect_list_or_array,
6014   (char *) "p",
6015   gst_value_lcopy_list_or_array
6016 };
6017
6018 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
6019
6020 static const GTypeValueTable _gst_value_array_value_table = {
6021   gst_value_init_list_or_array,
6022   gst_value_free_list_or_array,
6023   gst_value_copy_list_or_array,
6024   gst_value_list_or_array_peek_pointer,
6025   (char *) "p",
6026   gst_value_collect_list_or_array,
6027   (char *) "p",
6028   gst_value_lcopy_list_or_array
6029 };
6030
6031 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
6032
6033 static const GTypeValueTable _gst_fraction_value_table = {
6034   gst_value_init_fraction,
6035   NULL,
6036   gst_value_copy_fraction,
6037   NULL,
6038   (char *) "ii",
6039   gst_value_collect_fraction,
6040   (char *) "pp",
6041   gst_value_lcopy_fraction
6042 };
6043
6044 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
6045
6046 static const GTypeValueTable _gst_bitmask_value_table = {
6047   gst_value_init_bitmask,
6048   NULL,
6049   gst_value_copy_bitmask,
6050   NULL,
6051   (char *) "q",
6052   gst_value_collect_bitmask,
6053   (char *) "p",
6054   gst_value_lcopy_bitmask
6055 };
6056
6057 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
6058
6059 GType
6060 gst_g_thread_get_type (void)
6061 {
6062 #if GLIB_CHECK_VERSION(2,35,3)
6063   return G_TYPE_THREAD;
6064 #else
6065   static volatile gsize type_id = 0;
6066
6067   if (g_once_init_enter (&type_id)) {
6068     GType tmp =
6069         g_boxed_type_register_static (g_intern_static_string ("GstGThread"),
6070         (GBoxedCopyFunc) g_thread_ref,
6071         (GBoxedFreeFunc) g_thread_unref);
6072     g_once_init_leave (&type_id, tmp);
6073   }
6074
6075   return type_id;
6076 #endif
6077 }
6078
6079 void
6080 _priv_gst_value_initialize (void)
6081 {
6082   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
6083   gst_value_hash = g_hash_table_new (NULL, NULL);
6084   gst_value_union_funcs = g_array_new (FALSE, FALSE,
6085       sizeof (GstValueUnionInfo));
6086   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
6087       sizeof (GstValueIntersectInfo));
6088   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
6089       sizeof (GstValueSubtractInfo));
6090
6091   {
6092     static GstValueTable gst_value = {
6093       0,
6094       gst_value_compare_int_range,
6095       gst_value_serialize_int_range,
6096       gst_value_deserialize_int_range,
6097     };
6098
6099     gst_value.type = gst_int_range_get_type ();
6100     gst_value_register (&gst_value);
6101   }
6102
6103   {
6104     static GstValueTable gst_value = {
6105       0,
6106       gst_value_compare_int64_range,
6107       gst_value_serialize_int64_range,
6108       gst_value_deserialize_int64_range,
6109     };
6110
6111     gst_value.type = gst_int64_range_get_type ();
6112     gst_value_register (&gst_value);
6113   }
6114
6115   {
6116     static GstValueTable gst_value = {
6117       0,
6118       gst_value_compare_double_range,
6119       gst_value_serialize_double_range,
6120       gst_value_deserialize_double_range,
6121     };
6122
6123     gst_value.type = gst_double_range_get_type ();
6124     gst_value_register (&gst_value);
6125   }
6126
6127   {
6128     static GstValueTable gst_value = {
6129       0,
6130       gst_value_compare_fraction_range,
6131       gst_value_serialize_fraction_range,
6132       gst_value_deserialize_fraction_range,
6133     };
6134
6135     gst_value.type = gst_fraction_range_get_type ();
6136     gst_value_register (&gst_value);
6137   }
6138
6139   {
6140     static GstValueTable gst_value = {
6141       0,
6142       gst_value_compare_list,
6143       gst_value_serialize_list,
6144       gst_value_deserialize_list,
6145     };
6146
6147     gst_value.type = gst_value_list_get_type ();
6148     gst_value_register (&gst_value);
6149   }
6150
6151   {
6152     static GstValueTable gst_value = {
6153       0,
6154       gst_value_compare_array,
6155       gst_value_serialize_array,
6156       gst_value_deserialize_array,
6157     };
6158
6159     gst_value.type = gst_value_array_get_type ();
6160     gst_value_register (&gst_value);
6161   }
6162
6163   {
6164 #if 0
6165     static const GTypeValueTable value_table = {
6166       gst_value_init_buffer,
6167       NULL,
6168       gst_value_copy_buffer,
6169       NULL,
6170       "i",
6171       NULL,                     /*gst_value_collect_buffer, */
6172       "p",
6173       NULL                      /*gst_value_lcopy_buffer */
6174     };
6175 #endif
6176     static GstValueTable gst_value = {
6177       0,
6178       gst_value_compare_buffer,
6179       gst_value_serialize_buffer,
6180       gst_value_deserialize_buffer,
6181     };
6182
6183     gst_value.type = GST_TYPE_BUFFER;
6184     gst_value_register (&gst_value);
6185   }
6186   {
6187     static GstValueTable gst_value = {
6188       0,
6189       gst_value_compare_sample,
6190       gst_value_serialize_sample,
6191       gst_value_deserialize_sample,
6192     };
6193
6194     gst_value.type = GST_TYPE_SAMPLE;
6195     gst_value_register (&gst_value);
6196   }
6197   {
6198     static GstValueTable gst_value = {
6199       0,
6200       gst_value_compare_fraction,
6201       gst_value_serialize_fraction,
6202       gst_value_deserialize_fraction,
6203     };
6204
6205     gst_value.type = gst_fraction_get_type ();
6206     gst_value_register (&gst_value);
6207   }
6208   {
6209     static GstValueTable gst_value = {
6210       0,
6211       gst_value_compare_caps,
6212       gst_value_serialize_caps,
6213       gst_value_deserialize_caps,
6214     };
6215
6216     gst_value.type = GST_TYPE_CAPS;
6217     gst_value_register (&gst_value);
6218   }
6219   {
6220     static GstValueTable gst_value = {
6221       0,
6222       NULL,
6223       gst_value_serialize_segment,
6224       gst_value_deserialize_segment,
6225     };
6226
6227     gst_value.type = GST_TYPE_SEGMENT;
6228     gst_value_register (&gst_value);
6229   }
6230   {
6231     static GstValueTable gst_value = {
6232       0,
6233       NULL,
6234       gst_value_serialize_structure,
6235       gst_value_deserialize_structure,
6236     };
6237
6238     gst_value.type = GST_TYPE_STRUCTURE;
6239     gst_value_register (&gst_value);
6240   }
6241   {
6242     static GstValueTable gst_value = {
6243       0,
6244       NULL,
6245       gst_value_serialize_caps_features,
6246       gst_value_deserialize_caps_features,
6247     };
6248
6249     gst_value.type = GST_TYPE_CAPS_FEATURES;
6250     gst_value_register (&gst_value);
6251   }
6252   {
6253     static GstValueTable gst_value = {
6254       0,
6255       NULL,
6256       gst_value_serialize_tag_list,
6257       gst_value_deserialize_tag_list,
6258     };
6259
6260     gst_value.type = GST_TYPE_TAG_LIST;
6261     gst_value_register (&gst_value);
6262   }
6263   {
6264     static GstValueTable gst_value = {
6265       0,
6266       gst_value_compare_date,
6267       gst_value_serialize_date,
6268       gst_value_deserialize_date,
6269     };
6270
6271     gst_value.type = G_TYPE_DATE;
6272     gst_value_register (&gst_value);
6273   }
6274   {
6275     static GstValueTable gst_value = {
6276       0,
6277       gst_value_compare_date_time,
6278       gst_value_serialize_date_time,
6279       gst_value_deserialize_date_time,
6280     };
6281
6282     gst_value.type = gst_date_time_get_type ();
6283     gst_value_register (&gst_value);
6284   }
6285
6286   {
6287     static GstValueTable gst_value = {
6288       0,
6289       gst_value_compare_bitmask,
6290       gst_value_serialize_bitmask,
6291       gst_value_deserialize_bitmask,
6292     };
6293
6294     gst_value.type = gst_bitmask_get_type ();
6295     gst_value_register (&gst_value);
6296   }
6297
6298   {
6299     static GstValueTable gst_value = {
6300       0,
6301       gst_value_compare_allocation_params,
6302       NULL,
6303       NULL,
6304     };
6305
6306     gst_value.type = gst_allocation_params_get_type ();
6307     gst_value_register (&gst_value);
6308   }
6309
6310   {
6311     static GstValueTable gst_value = {
6312       0,
6313       gst_value_compare_object,
6314       NULL,
6315       NULL,
6316     };
6317
6318     gst_value.type = G_TYPE_OBJECT;
6319     gst_value_register (&gst_value);
6320   }
6321
6322   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
6323   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
6324
6325   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
6326   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
6327   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
6328
6329   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
6330
6331   REGISTER_SERIALIZATION (G_TYPE_INT, int);
6332
6333   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
6334   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
6335
6336   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
6337   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
6338   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
6339
6340   REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
6341
6342   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
6343       gst_value_transform_int_range_string);
6344   g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
6345       gst_value_transform_int64_range_string);
6346   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
6347       gst_value_transform_double_range_string);
6348   g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
6349       gst_value_transform_fraction_range_string);
6350   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
6351       gst_value_transform_list_string);
6352   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
6353       gst_value_transform_array_string);
6354   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
6355       gst_value_transform_fraction_string);
6356   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
6357       gst_value_transform_string_fraction);
6358   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
6359       gst_value_transform_fraction_double);
6360   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
6361       gst_value_transform_fraction_float);
6362   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
6363       gst_value_transform_double_fraction);
6364   g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
6365       gst_value_transform_float_fraction);
6366   g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
6367       gst_value_transform_date_string);
6368   g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
6369       gst_value_transform_string_date);
6370   g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
6371       gst_value_transform_object_string);
6372   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
6373       gst_value_transform_bitmask_uint64);
6374   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
6375       gst_value_transform_bitmask_string);
6376   g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
6377       gst_value_transform_uint64_bitmask);
6378   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
6379       gst_value_transform_string_bitmask);
6380
6381   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6382       gst_value_intersect_int_int_range);
6383   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6384       gst_value_intersect_int_range_int_range);
6385   gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6386       gst_value_intersect_int64_int64_range);
6387   gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6388       gst_value_intersect_int64_range_int64_range);
6389   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6390       gst_value_intersect_double_double_range);
6391   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
6392       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
6393   gst_value_register_intersect_func (GST_TYPE_ARRAY,
6394       GST_TYPE_ARRAY, gst_value_intersect_array);
6395   gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6396       gst_value_intersect_fraction_fraction_range);
6397   gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
6398       GST_TYPE_FRACTION_RANGE,
6399       gst_value_intersect_fraction_range_fraction_range);
6400
6401   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6402       gst_value_subtract_int_int_range);
6403   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
6404       gst_value_subtract_int_range_int);
6405   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6406       gst_value_subtract_int_range_int_range);
6407   gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6408       gst_value_subtract_int64_int64_range);
6409   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
6410       gst_value_subtract_int64_range_int64);
6411   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6412       gst_value_subtract_int64_range_int64_range);
6413   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6414       gst_value_subtract_double_double_range);
6415   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
6416       gst_value_subtract_double_range_double);
6417   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
6418       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
6419   gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6420       gst_value_subtract_fraction_fraction_range);
6421   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
6422       gst_value_subtract_fraction_range_fraction);
6423   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
6424       GST_TYPE_FRACTION_RANGE,
6425       gst_value_subtract_fraction_range_fraction_range);
6426
6427   /* see bug #317246, #64994, #65041 */
6428   {
6429     volatile GType date_type = G_TYPE_DATE;
6430
6431     g_type_name (date_type);
6432   }
6433
6434   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6435       gst_value_union_int_int_range);
6436   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6437       gst_value_union_int_range_int_range);
6438
6439 #if 0
6440   /* Implement these if needed */
6441   gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6442       gst_value_union_fraction_fraction_range);
6443   gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
6444       GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
6445 #endif
6446 }