docs: convert NULL, TRUE, and FALSE to %NULL, %TRUE, and %FALSE
[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
902 #define INT_RANGE_MIN(v) (((gint *)((v)->data[0].v_pointer))[0])
903 #define INT_RANGE_MAX(v) (((gint *)((v)->data[0].v_pointer))[1])
904 #define INT_RANGE_STEP(v) (((gint *)((v)->data[0].v_pointer))[2])
905
906 static void
907 gst_value_init_int_range (GValue * value)
908 {
909   gint *vals = g_slice_alloc0 (3 * sizeof (gint));
910   value->data[0].v_pointer = vals;
911   INT_RANGE_MIN (value) = 0;
912   INT_RANGE_MAX (value) = 0;
913   INT_RANGE_STEP (value) = 1;
914 }
915
916 static void
917 gst_value_free_int_range (GValue * value)
918 {
919   g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
920   g_slice_free1 (3 * sizeof (gint), value->data[0].v_pointer);
921   value->data[0].v_pointer = NULL;
922 }
923
924 static void
925 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
926 {
927   gint *vals = (gint *) dest_value->data[0].v_pointer;
928   gint *src_vals = (gint *) src_value->data[0].v_pointer;
929
930   if (vals == NULL) {
931     gst_value_init_int_range (dest_value);
932   }
933   if (src_vals != NULL) {
934     INT_RANGE_MIN (dest_value) = INT_RANGE_MIN (src_value);
935     INT_RANGE_MAX (dest_value) = INT_RANGE_MAX (src_value);
936     INT_RANGE_STEP (dest_value) = INT_RANGE_STEP (src_value);
937   }
938 }
939
940 static gchar *
941 gst_value_collect_int_range (GValue * value, guint n_collect_values,
942     GTypeCValue * collect_values, guint collect_flags)
943 {
944   gint *vals = value->data[0].v_pointer;
945
946   if (n_collect_values != 2)
947     return g_strdup_printf ("not enough value locations for `%s' passed",
948         G_VALUE_TYPE_NAME (value));
949   if (collect_values[0].v_int >= collect_values[1].v_int)
950     return g_strdup_printf ("range start is not smaller than end for `%s'",
951         G_VALUE_TYPE_NAME (value));
952
953   if (vals == NULL) {
954     gst_value_init_int_range (value);
955   }
956
957   gst_value_set_int_range_step (value, collect_values[0].v_int,
958       collect_values[1].v_int, 1);
959
960   return NULL;
961 }
962
963 static gchar *
964 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
965     GTypeCValue * collect_values, guint collect_flags)
966 {
967   guint32 *int_range_start = collect_values[0].v_pointer;
968   guint32 *int_range_end = collect_values[1].v_pointer;
969   guint32 *int_range_step = collect_values[2].v_pointer;
970   gint *vals = (gint *) value->data[0].v_pointer;
971
972   if (!int_range_start)
973     return g_strdup_printf ("start value location for `%s' passed as NULL",
974         G_VALUE_TYPE_NAME (value));
975   if (!int_range_end)
976     return g_strdup_printf ("end value location for `%s' passed as NULL",
977         G_VALUE_TYPE_NAME (value));
978   if (!int_range_step)
979     return g_strdup_printf ("step value location for `%s' passed as NULL",
980         G_VALUE_TYPE_NAME (value));
981
982   if (G_UNLIKELY (vals == NULL)) {
983     return g_strdup_printf ("Uninitialised `%s' passed",
984         G_VALUE_TYPE_NAME (value));
985   }
986
987   *int_range_start = INT_RANGE_MIN (value);
988   *int_range_end = INT_RANGE_MAX (value);
989   *int_range_step = INT_RANGE_STEP (value);
990
991   return NULL;
992 }
993
994 /**
995  * gst_value_set_int_range_step:
996  * @value: a GValue initialized to GST_TYPE_INT_RANGE
997  * @start: the start of the range
998  * @end: the end of the range
999  * @step: the step of the range
1000  *
1001  * Sets @value to the range specified by @start, @end and @step.
1002  */
1003 void
1004 gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
1005 {
1006   g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
1007   g_return_if_fail (start < end);
1008   g_return_if_fail (step > 0);
1009   g_return_if_fail (start % step == 0);
1010   g_return_if_fail (end % step == 0);
1011
1012   INT_RANGE_MIN (value) = start / step;
1013   INT_RANGE_MAX (value) = end / step;
1014   INT_RANGE_STEP (value) = step;
1015 }
1016
1017 /**
1018  * gst_value_set_int_range:
1019  * @value: a GValue initialized to GST_TYPE_INT_RANGE
1020  * @start: the start of the range
1021  * @end: the end of the range
1022  *
1023  * Sets @value to the range specified by @start and @end.
1024  */
1025 void
1026 gst_value_set_int_range (GValue * value, gint start, gint end)
1027 {
1028   gst_value_set_int_range_step (value, start, end, 1);
1029 }
1030
1031 /**
1032  * gst_value_get_int_range_min:
1033  * @value: a GValue initialized to GST_TYPE_INT_RANGE
1034  *
1035  * Gets the minimum of the range specified by @value.
1036  *
1037  * Returns: the minimum of the range
1038  */
1039 gint
1040 gst_value_get_int_range_min (const GValue * value)
1041 {
1042   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1043
1044   return INT_RANGE_MIN (value) * INT_RANGE_STEP (value);
1045 }
1046
1047 /**
1048  * gst_value_get_int_range_max:
1049  * @value: a GValue initialized to GST_TYPE_INT_RANGE
1050  *
1051  * Gets the maximum of the range specified by @value.
1052  *
1053  * Returns: the maximum of the range
1054  */
1055 gint
1056 gst_value_get_int_range_max (const GValue * value)
1057 {
1058   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1059
1060   return INT_RANGE_MAX (value) * INT_RANGE_STEP (value);
1061 }
1062
1063 /**
1064  * gst_value_get_int_range_step:
1065  * @value: a GValue initialized to GST_TYPE_INT_RANGE
1066  *
1067  * Gets the step of the range specified by @value.
1068  *
1069  * Returns: the step of the range
1070  */
1071 gint
1072 gst_value_get_int_range_step (const GValue * value)
1073 {
1074   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1075
1076   return INT_RANGE_STEP (value);
1077 }
1078
1079 static void
1080 gst_value_transform_int_range_string (const GValue * src_value,
1081     GValue * dest_value)
1082 {
1083   if (INT_RANGE_STEP (src_value) == 1)
1084     dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
1085         INT_RANGE_MIN (src_value), INT_RANGE_MAX (src_value));
1086   else
1087     dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d,%d]",
1088         INT_RANGE_MIN (src_value) * INT_RANGE_STEP (src_value),
1089         INT_RANGE_MAX (src_value) * INT_RANGE_STEP (src_value),
1090         INT_RANGE_STEP (src_value));
1091 }
1092
1093 static gint
1094 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
1095 {
1096   /* calculate the number of values in each range */
1097   gint n1 = INT_RANGE_MAX (value1) - INT_RANGE_MIN (value1) + 1;
1098   gint n2 = INT_RANGE_MAX (value2) - INT_RANGE_MIN (value2) + 1;
1099
1100   /* they must be equal */
1101   if (n1 != n2)
1102     return GST_VALUE_UNORDERED;
1103
1104   /* if empty, equal */
1105   if (n1 == 0)
1106     return GST_VALUE_EQUAL;
1107
1108   /* if more than one value, then it is only equal if the step is equal
1109      and bounds lie on the same value */
1110   if (n1 > 1) {
1111     if (INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2) &&
1112         INT_RANGE_MIN (value1) == INT_RANGE_MIN (value2) &&
1113         INT_RANGE_MAX (value1) == INT_RANGE_MAX (value2)) {
1114       return GST_VALUE_EQUAL;
1115     }
1116     return GST_VALUE_UNORDERED;
1117   } else {
1118     /* if just one, only if the value is equal */
1119     if (INT_RANGE_MIN (value1) == INT_RANGE_MIN (value2))
1120       return GST_VALUE_EQUAL;
1121     return GST_VALUE_UNORDERED;
1122   }
1123 }
1124
1125 static gchar *
1126 gst_value_serialize_int_range (const GValue * value)
1127 {
1128   if (INT_RANGE_STEP (value) == 1)
1129     return g_strdup_printf ("[ %d, %d ]", INT_RANGE_MIN (value),
1130         INT_RANGE_MAX (value));
1131   else
1132     return g_strdup_printf ("[ %d, %d, %d ]",
1133         INT_RANGE_MIN (value) * INT_RANGE_STEP (value),
1134         INT_RANGE_MAX (value) * INT_RANGE_STEP (value), INT_RANGE_STEP (value));
1135 }
1136
1137 static gboolean
1138 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1139 {
1140   g_warning ("unimplemented");
1141   return FALSE;
1142 }
1143
1144 /***************
1145  * int64 range *
1146  *
1147  * Values in the range are defined as any value greater or equal
1148  * to min*step, AND lesser or equal to max*step.
1149  * For step == 1, this falls back to the traditional range semantics.
1150  ***************/
1151
1152 #define INT64_RANGE_MIN(v) (((gint64 *)((v)->data[0].v_pointer))[0])
1153 #define INT64_RANGE_MAX(v) (((gint64 *)((v)->data[0].v_pointer))[1])
1154 #define INT64_RANGE_STEP(v) (((gint64 *)((v)->data[0].v_pointer))[2])
1155
1156 static void
1157 gst_value_init_int64_range (GValue * value)
1158 {
1159   gint64 *vals = g_slice_alloc0 (3 * sizeof (gint64));
1160   value->data[0].v_pointer = vals;
1161   INT64_RANGE_MIN (value) = 0;
1162   INT64_RANGE_MAX (value) = 0;
1163   INT64_RANGE_STEP (value) = 1;
1164 }
1165
1166 static void
1167 gst_value_free_int64_range (GValue * value)
1168 {
1169   g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1170   g_slice_free1 (3 * sizeof (gint64), value->data[0].v_pointer);
1171   value->data[0].v_pointer = NULL;
1172 }
1173
1174 static void
1175 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1176 {
1177   gint64 *vals = (gint64 *) dest_value->data[0].v_pointer;
1178   gint64 *src_vals = (gint64 *) src_value->data[0].v_pointer;
1179
1180   if (vals == NULL) {
1181     gst_value_init_int64_range (dest_value);
1182   }
1183
1184   if (src_vals != NULL) {
1185     INT64_RANGE_MIN (dest_value) = INT64_RANGE_MIN (src_value);
1186     INT64_RANGE_MAX (dest_value) = INT64_RANGE_MAX (src_value);
1187     INT64_RANGE_STEP (dest_value) = INT64_RANGE_STEP (src_value);
1188   }
1189 }
1190
1191 static gchar *
1192 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1193     GTypeCValue * collect_values, guint collect_flags)
1194 {
1195   gint64 *vals = value->data[0].v_pointer;
1196
1197   if (n_collect_values != 2)
1198     return g_strdup_printf ("not enough value locations for `%s' passed",
1199         G_VALUE_TYPE_NAME (value));
1200   if (collect_values[0].v_int64 >= collect_values[1].v_int64)
1201     return g_strdup_printf ("range start is not smaller than end for `%s'",
1202         G_VALUE_TYPE_NAME (value));
1203
1204   if (vals == NULL) {
1205     gst_value_init_int64_range (value);
1206   }
1207
1208   gst_value_set_int64_range_step (value, collect_values[0].v_int64,
1209       collect_values[1].v_int64, 1);
1210
1211   return NULL;
1212 }
1213
1214 static gchar *
1215 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1216     GTypeCValue * collect_values, guint collect_flags)
1217 {
1218   guint64 *int_range_start = collect_values[0].v_pointer;
1219   guint64 *int_range_end = collect_values[1].v_pointer;
1220   guint64 *int_range_step = collect_values[2].v_pointer;
1221   gint64 *vals = (gint64 *) value->data[0].v_pointer;
1222
1223   if (!int_range_start)
1224     return g_strdup_printf ("start value location for `%s' passed as NULL",
1225         G_VALUE_TYPE_NAME (value));
1226   if (!int_range_end)
1227     return g_strdup_printf ("end value location for `%s' passed as NULL",
1228         G_VALUE_TYPE_NAME (value));
1229   if (!int_range_step)
1230     return g_strdup_printf ("step value location for `%s' passed as NULL",
1231         G_VALUE_TYPE_NAME (value));
1232
1233   if (G_UNLIKELY (vals == NULL)) {
1234     return g_strdup_printf ("Uninitialised `%s' passed",
1235         G_VALUE_TYPE_NAME (value));
1236   }
1237
1238   *int_range_start = INT64_RANGE_MIN (value);
1239   *int_range_end = INT64_RANGE_MAX (value);
1240   *int_range_step = INT64_RANGE_STEP (value);
1241
1242   return NULL;
1243 }
1244
1245 /**
1246  * gst_value_set_int64_range_step:
1247  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1248  * @start: the start of the range
1249  * @end: the end of the range
1250  * @step: the step of the range
1251  *
1252  * Sets @value to the range specified by @start, @end and @step.
1253  */
1254 void
1255 gst_value_set_int64_range_step (GValue * value, gint64 start, gint64 end,
1256     gint64 step)
1257 {
1258   g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1259   g_return_if_fail (start < end);
1260   g_return_if_fail (step > 0);
1261   g_return_if_fail (start % step == 0);
1262   g_return_if_fail (end % step == 0);
1263
1264   INT64_RANGE_MIN (value) = start / step;
1265   INT64_RANGE_MAX (value) = end / step;
1266   INT64_RANGE_STEP (value) = step;
1267 }
1268
1269 /**
1270  * gst_value_set_int64_range:
1271  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1272  * @start: the start of the range
1273  * @end: the end of the range
1274  *
1275  * Sets @value to the range specified by @start and @end.
1276  */
1277 void
1278 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1279 {
1280   gst_value_set_int64_range_step (value, start, end, 1);
1281 }
1282
1283 /**
1284  * gst_value_get_int64_range_min:
1285  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1286  *
1287  * Gets the minimum of the range specified by @value.
1288  *
1289  * Returns: the minimum of the range
1290  */
1291 gint64
1292 gst_value_get_int64_range_min (const GValue * value)
1293 {
1294   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1295
1296   return INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value);
1297 }
1298
1299 /**
1300  * gst_value_get_int64_range_max:
1301  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1302  *
1303  * Gets the maximum of the range specified by @value.
1304  *
1305  * Returns: the maximum of the range
1306  */
1307 gint64
1308 gst_value_get_int64_range_max (const GValue * value)
1309 {
1310   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1311
1312   return INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value);
1313 }
1314
1315 /**
1316  * gst_value_get_int64_range_step:
1317  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1318  *
1319  * Gets the step of the range specified by @value.
1320  *
1321  * Returns: the step of the range
1322  */
1323 gint64
1324 gst_value_get_int64_range_step (const GValue * value)
1325 {
1326   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1327
1328   return INT64_RANGE_STEP (value);
1329 }
1330
1331 static void
1332 gst_value_transform_int64_range_string (const GValue * src_value,
1333     GValue * dest_value)
1334 {
1335   if (INT64_RANGE_STEP (src_value) == 1)
1336     dest_value->data[0].v_pointer =
1337         g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1338         INT64_RANGE_MIN (src_value), INT64_RANGE_MAX (src_value));
1339   else
1340     dest_value->data[0].v_pointer =
1341         g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT
1342         ",%" G_GINT64_FORMAT "]",
1343         INT64_RANGE_MIN (src_value) * INT64_RANGE_STEP (src_value),
1344         INT64_RANGE_MAX (src_value) * INT64_RANGE_STEP (src_value),
1345         INT64_RANGE_STEP (src_value));
1346 }
1347
1348 static gint
1349 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1350 {
1351   /* calculate the number of values in each range */
1352   gint64 n1 = INT64_RANGE_MAX (value1) - INT64_RANGE_MIN (value1) + 1;
1353   gint64 n2 = INT64_RANGE_MAX (value2) - INT64_RANGE_MIN (value2) + 1;
1354
1355   /* they must be equal */
1356   if (n1 != n2)
1357     return GST_VALUE_UNORDERED;
1358
1359   /* if empty, equal */
1360   if (n1 == 0)
1361     return GST_VALUE_EQUAL;
1362
1363   /* if more than one value, then it is only equal if the step is equal
1364      and bounds lie on the same value */
1365   if (n1 > 1) {
1366     if (INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2) &&
1367         INT64_RANGE_MIN (value1) == INT64_RANGE_MIN (value2) &&
1368         INT64_RANGE_MAX (value1) == INT64_RANGE_MAX (value2)) {
1369       return GST_VALUE_EQUAL;
1370     }
1371     return GST_VALUE_UNORDERED;
1372   } else {
1373     /* if just one, only if the value is equal */
1374     if (INT64_RANGE_MIN (value1) == INT64_RANGE_MIN (value2))
1375       return GST_VALUE_EQUAL;
1376     return GST_VALUE_UNORDERED;
1377   }
1378 }
1379
1380 static gchar *
1381 gst_value_serialize_int64_range (const GValue * value)
1382 {
1383   if (INT64_RANGE_STEP (value) == 1)
1384     return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1385         INT64_RANGE_MIN (value), INT64_RANGE_MAX (value));
1386   else
1387     return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT ", %"
1388         G_GINT64_FORMAT " ]",
1389         INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value),
1390         INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value),
1391         INT64_RANGE_STEP (value));
1392 }
1393
1394 static gboolean
1395 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1396 {
1397   g_warning ("unimplemented");
1398   return FALSE;
1399 }
1400
1401 /****************
1402  * double range *
1403  ****************/
1404
1405 static void
1406 gst_value_init_double_range (GValue * value)
1407 {
1408   value->data[0].v_double = 0;
1409   value->data[1].v_double = 0;
1410 }
1411
1412 static void
1413 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1414 {
1415   dest_value->data[0].v_double = src_value->data[0].v_double;
1416   dest_value->data[1].v_double = src_value->data[1].v_double;
1417 }
1418
1419 static gchar *
1420 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1421     GTypeCValue * collect_values, guint collect_flags)
1422 {
1423   if (n_collect_values != 2)
1424     return g_strdup_printf ("not enough value locations for `%s' passed",
1425         G_VALUE_TYPE_NAME (value));
1426   if (collect_values[0].v_double >= collect_values[1].v_double)
1427     return g_strdup_printf ("range start is not smaller than end for `%s'",
1428         G_VALUE_TYPE_NAME (value));
1429
1430   value->data[0].v_double = collect_values[0].v_double;
1431   value->data[1].v_double = collect_values[1].v_double;
1432
1433   return NULL;
1434 }
1435
1436 static gchar *
1437 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1438     GTypeCValue * collect_values, guint collect_flags)
1439 {
1440   gdouble *double_range_start = collect_values[0].v_pointer;
1441   gdouble *double_range_end = collect_values[1].v_pointer;
1442
1443   if (!double_range_start)
1444     return g_strdup_printf ("start value location for `%s' passed as NULL",
1445         G_VALUE_TYPE_NAME (value));
1446   if (!double_range_end)
1447     return g_strdup_printf ("end value location for `%s' passed as NULL",
1448         G_VALUE_TYPE_NAME (value));
1449
1450   *double_range_start = value->data[0].v_double;
1451   *double_range_end = value->data[1].v_double;
1452
1453   return NULL;
1454 }
1455
1456 /**
1457  * gst_value_set_double_range:
1458  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1459  * @start: the start of the range
1460  * @end: the end of the range
1461  *
1462  * Sets @value to the range specified by @start and @end.
1463  */
1464 void
1465 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1466 {
1467   g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1468   g_return_if_fail (start < end);
1469
1470   value->data[0].v_double = start;
1471   value->data[1].v_double = end;
1472 }
1473
1474 /**
1475  * gst_value_get_double_range_min:
1476  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1477  *
1478  * Gets the minimum of the range specified by @value.
1479  *
1480  * Returns: the minimum of the range
1481  */
1482 gdouble
1483 gst_value_get_double_range_min (const GValue * value)
1484 {
1485   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1486
1487   return value->data[0].v_double;
1488 }
1489
1490 /**
1491  * gst_value_get_double_range_max:
1492  * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1493  *
1494  * Gets the maximum of the range specified by @value.
1495  *
1496  * Returns: the maximum of the range
1497  */
1498 gdouble
1499 gst_value_get_double_range_max (const GValue * value)
1500 {
1501   g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1502
1503   return value->data[1].v_double;
1504 }
1505
1506 static void
1507 gst_value_transform_double_range_string (const GValue * src_value,
1508     GValue * dest_value)
1509 {
1510   gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1511
1512   dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1513       g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1514           src_value->data[0].v_double),
1515       g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1516           src_value->data[1].v_double));
1517 }
1518
1519 static gint
1520 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1521 {
1522   if (value2->data[0].v_double == value1->data[0].v_double &&
1523       value2->data[1].v_double == value1->data[1].v_double)
1524     return GST_VALUE_EQUAL;
1525   return GST_VALUE_UNORDERED;
1526 }
1527
1528 static gchar *
1529 gst_value_serialize_double_range (const GValue * value)
1530 {
1531   gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1532   gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1533
1534   g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1535   g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1536   return g_strdup_printf ("[ %s, %s ]", d1, d2);
1537 }
1538
1539 static gboolean
1540 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1541 {
1542   g_warning ("unimplemented");
1543   return FALSE;
1544 }
1545
1546 /****************
1547  * fraction range *
1548  ****************/
1549
1550 static void
1551 gst_value_init_fraction_range (GValue * value)
1552 {
1553   GValue *vals;
1554   GType ftype;
1555
1556   ftype = GST_TYPE_FRACTION;
1557
1558   value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1559   g_value_init (&vals[0], ftype);
1560   g_value_init (&vals[1], ftype);
1561 }
1562
1563 static void
1564 gst_value_free_fraction_range (GValue * value)
1565 {
1566   GValue *vals = (GValue *) value->data[0].v_pointer;
1567
1568   if (vals != NULL) {
1569     /* we know the two values contain fractions without internal allocs */
1570     /* g_value_unset (&vals[0]); */
1571     /* g_value_unset (&vals[1]); */
1572     g_slice_free1 (2 * sizeof (GValue), vals);
1573     value->data[0].v_pointer = NULL;
1574   }
1575 }
1576
1577 static void
1578 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1579 {
1580   GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1581   GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1582
1583   if (vals == NULL) {
1584     gst_value_init_fraction_range (dest_value);
1585     vals = dest_value->data[0].v_pointer;
1586   }
1587   if (src_vals != NULL) {
1588     g_value_copy (&src_vals[0], &vals[0]);
1589     g_value_copy (&src_vals[1], &vals[1]);
1590   }
1591 }
1592
1593 static gchar *
1594 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1595     GTypeCValue * collect_values, guint collect_flags)
1596 {
1597   GValue *vals = (GValue *) value->data[0].v_pointer;
1598
1599   if (n_collect_values != 4)
1600     return g_strdup_printf ("not enough value locations for `%s' passed",
1601         G_VALUE_TYPE_NAME (value));
1602   if (collect_values[1].v_int == 0)
1603     return g_strdup_printf ("passed '0' as first denominator for `%s'",
1604         G_VALUE_TYPE_NAME (value));
1605   if (collect_values[3].v_int == 0)
1606     return g_strdup_printf ("passed '0' as second denominator for `%s'",
1607         G_VALUE_TYPE_NAME (value));
1608   if (gst_util_fraction_compare (collect_values[0].v_int,
1609           collect_values[1].v_int, collect_values[2].v_int,
1610           collect_values[3].v_int) >= 0)
1611     return g_strdup_printf ("range start is not smaller than end for `%s'",
1612         G_VALUE_TYPE_NAME (value));
1613
1614   if (vals == NULL) {
1615     gst_value_init_fraction_range (value);
1616     vals = value->data[0].v_pointer;
1617   }
1618
1619   gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1620       collect_values[1].v_int);
1621   gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1622       collect_values[3].v_int);
1623
1624   return NULL;
1625 }
1626
1627 static gchar *
1628 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1629     GTypeCValue * collect_values, guint collect_flags)
1630 {
1631   gint i;
1632   gint *dest_values[4];
1633   GValue *vals = (GValue *) value->data[0].v_pointer;
1634
1635   if (G_UNLIKELY (n_collect_values != 4))
1636     return g_strdup_printf ("not enough value locations for `%s' passed",
1637         G_VALUE_TYPE_NAME (value));
1638
1639   for (i = 0; i < 4; i++) {
1640     if (G_UNLIKELY (collect_values[i].v_pointer == NULL)) {
1641       return g_strdup_printf ("value location for `%s' passed as NULL",
1642           G_VALUE_TYPE_NAME (value));
1643     }
1644     dest_values[i] = collect_values[i].v_pointer;
1645   }
1646
1647   if (G_UNLIKELY (vals == NULL)) {
1648     return g_strdup_printf ("Uninitialised `%s' passed",
1649         G_VALUE_TYPE_NAME (value));
1650   }
1651
1652   dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1653   dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1654   dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1655   dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1656   return NULL;
1657 }
1658
1659 /**
1660  * gst_value_set_fraction_range:
1661  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1662  * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1663  * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1664  *
1665  * Sets @value to the range specified by @start and @end.
1666  */
1667 void
1668 gst_value_set_fraction_range (GValue * value, const GValue * start,
1669     const GValue * end)
1670 {
1671   GValue *vals;
1672
1673   g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1674   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
1675   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
1676   g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
1677           start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
1678
1679   vals = (GValue *) value->data[0].v_pointer;
1680   if (vals == NULL) {
1681     gst_value_init_fraction_range (value);
1682     vals = value->data[0].v_pointer;
1683   }
1684   g_value_copy (start, &vals[0]);
1685   g_value_copy (end, &vals[1]);
1686 }
1687
1688 /**
1689  * gst_value_set_fraction_range_full:
1690  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1691  * @numerator_start: the numerator start of the range
1692  * @denominator_start: the denominator start of the range
1693  * @numerator_end: the numerator end of the range
1694  * @denominator_end: the denominator end of the range
1695  *
1696  * Sets @value to the range specified by @numerator_start/@denominator_start
1697  * and @numerator_end/@denominator_end.
1698  */
1699 void
1700 gst_value_set_fraction_range_full (GValue * value,
1701     gint numerator_start, gint denominator_start,
1702     gint numerator_end, gint denominator_end)
1703 {
1704   GValue start = { 0 };
1705   GValue end = { 0 };
1706
1707   g_return_if_fail (value != NULL);
1708   g_return_if_fail (denominator_start != 0);
1709   g_return_if_fail (denominator_end != 0);
1710   g_return_if_fail (gst_util_fraction_compare (numerator_start,
1711           denominator_start, numerator_end, denominator_end) < 0);
1712
1713   g_value_init (&start, GST_TYPE_FRACTION);
1714   g_value_init (&end, GST_TYPE_FRACTION);
1715
1716   gst_value_set_fraction (&start, numerator_start, denominator_start);
1717   gst_value_set_fraction (&end, numerator_end, denominator_end);
1718   gst_value_set_fraction_range (value, &start, &end);
1719
1720   /* we know the two values contain fractions without internal allocs */
1721   /* g_value_unset (&start); */
1722   /* g_value_unset (&end);   */
1723 }
1724
1725 /**
1726  * gst_value_get_fraction_range_min:
1727  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1728  *
1729  * Gets the minimum of the range specified by @value.
1730  *
1731  * Returns: the minimum of the range
1732  */
1733 const GValue *
1734 gst_value_get_fraction_range_min (const GValue * value)
1735 {
1736   GValue *vals;
1737
1738   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1739
1740   vals = (GValue *) value->data[0].v_pointer;
1741   if (vals != NULL) {
1742     return &vals[0];
1743   }
1744
1745   return NULL;
1746 }
1747
1748 /**
1749  * gst_value_get_fraction_range_max:
1750  * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1751  *
1752  * Gets the maximum of the range specified by @value.
1753  *
1754  * Returns: the maximum of the range
1755  */
1756 const GValue *
1757 gst_value_get_fraction_range_max (const GValue * value)
1758 {
1759   GValue *vals;
1760
1761   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1762
1763   vals = (GValue *) value->data[0].v_pointer;
1764   if (vals != NULL) {
1765     return &vals[1];
1766   }
1767
1768   return NULL;
1769 }
1770
1771 static gchar *
1772 gst_value_serialize_fraction_range (const GValue * value)
1773 {
1774   GValue *vals = (GValue *) value->data[0].v_pointer;
1775   gchar *retval;
1776
1777   if (vals == NULL) {
1778     retval = g_strdup ("[ 0/1, 0/1 ]");
1779   } else {
1780     gchar *start, *end;
1781
1782     start = gst_value_serialize_fraction (&vals[0]);
1783     end = gst_value_serialize_fraction (&vals[1]);
1784
1785     retval = g_strdup_printf ("[ %s, %s ]", start, end);
1786     g_free (start);
1787     g_free (end);
1788   }
1789
1790   return retval;
1791 }
1792
1793 static void
1794 gst_value_transform_fraction_range_string (const GValue * src_value,
1795     GValue * dest_value)
1796 {
1797   dest_value->data[0].v_pointer =
1798       gst_value_serialize_fraction_range (src_value);
1799 }
1800
1801 static gint
1802 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1803 {
1804   GValue *vals1, *vals2;
1805   GstValueCompareFunc compare;
1806
1807   if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1808     return GST_VALUE_EQUAL;     /* Only possible if both are NULL */
1809
1810   if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1811     return GST_VALUE_UNORDERED;
1812
1813   vals1 = (GValue *) value1->data[0].v_pointer;
1814   vals2 = (GValue *) value2->data[0].v_pointer;
1815   if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1816     if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1817         GST_VALUE_EQUAL &&
1818         gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1819         GST_VALUE_EQUAL)
1820       return GST_VALUE_EQUAL;
1821   }
1822   return GST_VALUE_UNORDERED;
1823 }
1824
1825 static gboolean
1826 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1827 {
1828   g_warning ("unimplemented");
1829   return FALSE;
1830 }
1831
1832 /***********
1833  * GstCaps *
1834  ***********/
1835
1836 /**
1837  * gst_value_set_caps:
1838  * @value: a GValue initialized to GST_TYPE_CAPS
1839  * @caps: (transfer none): the caps to set the value to
1840  *
1841  * Sets the contents of @value to @caps. A reference to the
1842  * provided @caps will be taken by the @value.
1843  */
1844 void
1845 gst_value_set_caps (GValue * value, const GstCaps * caps)
1846 {
1847   g_return_if_fail (G_IS_VALUE (value));
1848   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1849   g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
1850
1851   g_value_set_boxed (value, caps);
1852 }
1853
1854 /**
1855  * gst_value_get_caps:
1856  * @value: a GValue initialized to GST_TYPE_CAPS
1857  *
1858  * Gets the contents of @value. The reference count of the returned
1859  * #GstCaps will not be modified, therefore the caller must take one
1860  * before getting rid of the @value.
1861  *
1862  * Returns: (transfer none): the contents of @value
1863  */
1864 const GstCaps *
1865 gst_value_get_caps (const GValue * value)
1866 {
1867   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1868   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1869
1870   return (GstCaps *) g_value_get_boxed (value);
1871 }
1872
1873 static gint
1874 gst_value_compare_caps (const GValue * value1, const GValue * value2)
1875 {
1876   GstCaps *caps1 = GST_CAPS (gst_value_get_caps (value1));
1877   GstCaps *caps2 = GST_CAPS (gst_value_get_caps (value2));
1878
1879   if (gst_caps_is_equal (caps1, caps2))
1880     return GST_VALUE_EQUAL;
1881   return GST_VALUE_UNORDERED;
1882 }
1883
1884 static gchar *
1885 gst_value_serialize_caps (const GValue * value)
1886 {
1887   GstCaps *caps = g_value_get_boxed (value);
1888   return gst_string_take_and_wrap (gst_caps_to_string (caps));
1889 }
1890
1891 static gboolean
1892 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1893 {
1894   GstCaps *caps;
1895
1896   if (*s != '"') {
1897     caps = gst_caps_from_string (s);
1898   } else {
1899     gchar *str = gst_string_unwrap (s);
1900
1901     if (G_UNLIKELY (!str))
1902       return FALSE;
1903
1904     caps = gst_caps_from_string (str);
1905     g_free (str);
1906   }
1907
1908   if (caps) {
1909     g_value_take_boxed (dest, caps);
1910     return TRUE;
1911   }
1912   return FALSE;
1913 }
1914
1915 /**************
1916  * GstSegment *
1917  **************/
1918
1919 static gchar *
1920 gst_value_serialize_segment_internal (const GValue * value, gboolean escape)
1921 {
1922   GstSegment *seg = g_value_get_boxed (value);
1923   gchar *t, *res;
1924   GstStructure *s;
1925
1926   s = gst_structure_new ("GstSegment",
1927       "flags", GST_TYPE_SEGMENT_FLAGS, seg->flags,
1928       "rate", G_TYPE_DOUBLE, seg->rate,
1929       "applied-rate", G_TYPE_DOUBLE, seg->applied_rate,
1930       "format", GST_TYPE_FORMAT, seg->format,
1931       "base", G_TYPE_UINT64, seg->base,
1932       "offset", G_TYPE_UINT64, seg->offset,
1933       "start", G_TYPE_UINT64, seg->start,
1934       "stop", G_TYPE_UINT64, seg->stop,
1935       "time", G_TYPE_UINT64, seg->time,
1936       "position", G_TYPE_UINT64, seg->position,
1937       "duration", G_TYPE_UINT64, seg->duration, NULL);
1938   t = gst_structure_to_string (s);
1939   if (escape) {
1940     res = g_strdup_printf ("\"%s\"", t);
1941     g_free (t);
1942   } else {
1943     res = t;
1944   }
1945   gst_structure_free (s);
1946
1947   return res;
1948 }
1949
1950 static gchar *
1951 gst_value_serialize_segment (const GValue * value)
1952 {
1953   return gst_value_serialize_segment_internal (value, TRUE);
1954 }
1955
1956 static gboolean
1957 gst_value_deserialize_segment (GValue * dest, const gchar * s)
1958 {
1959   GstStructure *str;
1960   GstSegment seg;
1961   gboolean res;
1962
1963   str = gst_structure_from_string (s, NULL);
1964   if (str == NULL)
1965     return FALSE;
1966
1967   res = gst_structure_get (str,
1968       "flags", GST_TYPE_SEGMENT_FLAGS, &seg.flags,
1969       "rate", G_TYPE_DOUBLE, &seg.rate,
1970       "applied-rate", G_TYPE_DOUBLE, &seg.applied_rate,
1971       "format", GST_TYPE_FORMAT, &seg.format,
1972       "base", G_TYPE_UINT64, &seg.base,
1973       "offset", G_TYPE_UINT64, &seg.offset,
1974       "start", G_TYPE_UINT64, &seg.start,
1975       "stop", G_TYPE_UINT64, &seg.stop,
1976       "time", G_TYPE_UINT64, &seg.time,
1977       "position", G_TYPE_UINT64, &seg.position,
1978       "duration", G_TYPE_UINT64, &seg.duration, NULL);
1979   gst_structure_free (str);
1980
1981   if (res)
1982     g_value_set_boxed (dest, &seg);
1983
1984   return res;
1985 }
1986
1987 /****************
1988  * GstStructure *
1989  ****************/
1990
1991 /**
1992  * gst_value_set_structure:
1993  * @value: a GValue initialized to GST_TYPE_STRUCTURE
1994  * @structure: the structure to set the value to
1995  *
1996  * Sets the contents of @value to @structure.  The actual
1997  */
1998 void
1999 gst_value_set_structure (GValue * value, const GstStructure * structure)
2000 {
2001   g_return_if_fail (G_IS_VALUE (value));
2002   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
2003   g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
2004
2005   g_value_set_boxed (value, structure);
2006 }
2007
2008 /**
2009  * gst_value_get_structure:
2010  * @value: a GValue initialized to GST_TYPE_STRUCTURE
2011  *
2012  * Gets the contents of @value.
2013  *
2014  * Returns: (transfer none): the contents of @value
2015  */
2016 const GstStructure *
2017 gst_value_get_structure (const GValue * value)
2018 {
2019   g_return_val_if_fail (G_IS_VALUE (value), NULL);
2020   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
2021
2022   return (GstStructure *) g_value_get_boxed (value);
2023 }
2024
2025 static gchar *
2026 gst_value_serialize_structure (const GValue * value)
2027 {
2028   GstStructure *structure = g_value_get_boxed (value);
2029
2030   return gst_string_take_and_wrap (gst_structure_to_string (structure));
2031 }
2032
2033 static gboolean
2034 gst_value_deserialize_structure (GValue * dest, const gchar * s)
2035 {
2036   GstStructure *structure;
2037
2038   if (*s != '"') {
2039     structure = gst_structure_from_string (s, NULL);
2040   } else {
2041     gchar *str = gst_string_unwrap (s);
2042
2043     if (G_UNLIKELY (!str))
2044       return FALSE;
2045
2046     structure = gst_structure_from_string (str, NULL);
2047     g_free (str);
2048   }
2049
2050   if (G_LIKELY (structure)) {
2051     g_value_take_boxed (dest, structure);
2052     return TRUE;
2053   }
2054   return FALSE;
2055 }
2056
2057 /*******************
2058  * GstCapsFeatures *
2059  *******************/
2060
2061 /**
2062  * gst_value_set_caps_features:
2063  * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
2064  * @features: the features to set the value to
2065  *
2066  * Sets the contents of @value to @features.
2067  */
2068 void
2069 gst_value_set_caps_features (GValue * value, const GstCapsFeatures * features)
2070 {
2071   g_return_if_fail (G_IS_VALUE (value));
2072   g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES);
2073   g_return_if_fail (features == NULL || GST_IS_CAPS_FEATURES (features));
2074
2075   g_value_set_boxed (value, features);
2076 }
2077
2078 /**
2079  * gst_value_get_caps_features:
2080  * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
2081  *
2082  * Gets the contents of @value.
2083  *
2084  * Returns: (transfer none): the contents of @value
2085  */
2086 const GstCapsFeatures *
2087 gst_value_get_caps_features (const GValue * value)
2088 {
2089   g_return_val_if_fail (G_IS_VALUE (value), NULL);
2090   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES, NULL);
2091
2092   return (GstCapsFeatures *) g_value_get_boxed (value);
2093 }
2094
2095 static gchar *
2096 gst_value_serialize_caps_features (const GValue * value)
2097 {
2098   GstCapsFeatures *features = g_value_get_boxed (value);
2099
2100   return gst_string_take_and_wrap (gst_caps_features_to_string (features));
2101 }
2102
2103 static gboolean
2104 gst_value_deserialize_caps_features (GValue * dest, const gchar * s)
2105 {
2106   GstCapsFeatures *features;
2107
2108   if (*s != '"') {
2109     features = gst_caps_features_from_string (s);
2110   } else {
2111     gchar *str = gst_string_unwrap (s);
2112
2113     if (G_UNLIKELY (!str))
2114       return FALSE;
2115
2116     features = gst_caps_features_from_string (str);
2117     g_free (str);
2118   }
2119
2120   if (G_LIKELY (features)) {
2121     g_value_take_boxed (dest, features);
2122     return TRUE;
2123   }
2124   return FALSE;
2125 }
2126
2127 /**************
2128  * GstTagList *
2129  **************/
2130
2131 static gboolean
2132 gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
2133 {
2134   GstTagList *taglist;
2135
2136   if (*s != '"') {
2137     taglist = gst_tag_list_new_from_string (s);
2138   } else {
2139     gchar *str = gst_string_unwrap (s);
2140
2141     if (G_UNLIKELY (!str))
2142       return FALSE;
2143
2144     taglist = gst_tag_list_new_from_string (str);
2145     g_free (str);
2146   }
2147
2148   if (G_LIKELY (taglist != NULL)) {
2149     g_value_take_boxed (dest, taglist);
2150     return TRUE;
2151   }
2152   return FALSE;
2153 }
2154
2155 static gchar *
2156 gst_value_serialize_tag_list (const GValue * value)
2157 {
2158   GstTagList *taglist = g_value_get_boxed (value);
2159
2160   return gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
2161 }
2162
2163
2164 /*************
2165  * GstBuffer *
2166  *************/
2167
2168 static gint
2169 compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
2170 {
2171   gsize size1, size2;
2172   GstMapInfo info1, info2;
2173   gint result, mret;
2174
2175   if (buf1 == buf2)
2176     return GST_VALUE_EQUAL;
2177
2178   size1 = gst_buffer_get_size (buf1);
2179   size2 = gst_buffer_get_size (buf2);
2180
2181   if (size1 != size2)
2182     return GST_VALUE_UNORDERED;
2183
2184   if (size1 == 0)
2185     return GST_VALUE_EQUAL;
2186
2187   if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
2188     return GST_VALUE_UNORDERED;
2189
2190   if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
2191     gst_buffer_unmap (buf1, &info1);
2192     return GST_VALUE_UNORDERED;
2193   }
2194
2195   mret = memcmp (info1.data, info2.data, info1.size);
2196   if (mret == 0)
2197     result = GST_VALUE_EQUAL;
2198   else if (mret < 0)
2199     result = GST_VALUE_LESS_THAN;
2200   else
2201     result = GST_VALUE_GREATER_THAN;
2202
2203   gst_buffer_unmap (buf1, &info1);
2204   gst_buffer_unmap (buf2, &info2);
2205
2206   return result;
2207 }
2208
2209 static gint
2210 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
2211 {
2212   GstBuffer *buf1 = gst_value_get_buffer (value1);
2213   GstBuffer *buf2 = gst_value_get_buffer (value2);
2214
2215   return compare_buffer (buf1, buf2);
2216 }
2217
2218 static gchar *
2219 gst_value_serialize_buffer (const GValue * value)
2220 {
2221   GstMapInfo info;
2222   guint8 *data;
2223   gint i;
2224   gchar *string;
2225   GstBuffer *buffer;
2226
2227   buffer = gst_value_get_buffer (value);
2228   if (buffer == NULL)
2229     return NULL;
2230
2231   if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
2232     return NULL;
2233
2234   data = info.data;
2235
2236   string = g_malloc (info.size * 2 + 1);
2237   for (i = 0; i < info.size; i++) {
2238     sprintf (string + i * 2, "%02x", data[i]);
2239   }
2240   string[info.size * 2] = 0;
2241
2242   gst_buffer_unmap (buffer, &info);
2243
2244   return string;
2245 }
2246
2247 static gboolean
2248 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
2249 {
2250   GstBuffer *buffer;
2251   gint len;
2252   gchar ts[3];
2253   GstMapInfo info;
2254   guint8 *data;
2255   gint i;
2256
2257   len = strlen (s);
2258   if (len & 1)
2259     goto wrong_length;
2260
2261   buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
2262   if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
2263     goto map_failed;
2264   data = info.data;
2265
2266   for (i = 0; i < len / 2; i++) {
2267     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
2268       goto wrong_char;
2269
2270     ts[0] = s[i * 2 + 0];
2271     ts[1] = s[i * 2 + 1];
2272     ts[2] = 0;
2273
2274     data[i] = (guint8) strtoul (ts, NULL, 16);
2275   }
2276   gst_buffer_unmap (buffer, &info);
2277
2278   gst_value_take_buffer (dest, buffer);
2279
2280   return TRUE;
2281
2282   /* ERRORS */
2283 wrong_length:
2284   {
2285     return FALSE;
2286   }
2287 map_failed:
2288   {
2289     return FALSE;
2290   }
2291 wrong_char:
2292   {
2293     gst_buffer_unref (buffer);
2294     gst_buffer_unmap (buffer, &info);
2295     return FALSE;
2296   }
2297 }
2298
2299 /*************
2300  * GstSample *
2301  *************/
2302
2303 /* This function is mostly used for comparing image/buffer tags in taglists */
2304 static gint
2305 gst_value_compare_sample (const GValue * value1, const GValue * value2)
2306 {
2307   GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
2308   GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
2309
2310   /* FIXME: should we take into account anything else such as caps? */
2311   return compare_buffer (buf1, buf2);
2312 }
2313
2314 static gchar *
2315 gst_value_serialize_sample (const GValue * value)
2316 {
2317   const GstStructure *info_structure;
2318   GstSegment *segment;
2319   GstBuffer *buffer;
2320   GstCaps *caps;
2321   GstSample *sample;
2322   GValue val = { 0, };
2323   gchar *info_str, *caps_str, *tmp;
2324   gchar *buf_str, *seg_str, *s;
2325
2326   sample = g_value_get_boxed (value);
2327
2328   buffer = gst_sample_get_buffer (sample);
2329   if (buffer) {
2330     g_value_init (&val, GST_TYPE_BUFFER);
2331     g_value_set_boxed (&val, buffer);
2332     buf_str = gst_value_serialize_buffer (&val);
2333     g_value_unset (&val);
2334   } else {
2335     buf_str = g_strdup ("None");
2336   }
2337
2338   caps = gst_sample_get_caps (sample);
2339   if (caps) {
2340     tmp = gst_caps_to_string (caps);
2341     caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2342     g_strdelimit (caps_str, "=", '_');
2343     g_free (tmp);
2344   } else {
2345     caps_str = g_strdup ("None");
2346   }
2347
2348   segment = gst_sample_get_segment (sample);
2349   if (segment) {
2350     g_value_init (&val, GST_TYPE_SEGMENT);
2351     g_value_set_boxed (&val, segment);
2352     tmp = gst_value_serialize_segment_internal (&val, FALSE);
2353     seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2354     g_strdelimit (seg_str, "=", '_');
2355     g_free (tmp);
2356     g_value_unset (&val);
2357   } else {
2358     seg_str = g_strdup ("None");
2359   }
2360
2361   info_structure = gst_sample_get_info (sample);
2362   if (info_structure) {
2363     tmp = gst_structure_to_string (info_structure);
2364     info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
2365     g_strdelimit (info_str, "=", '_');
2366     g_free (tmp);
2367   } else {
2368     info_str = g_strdup ("None");
2369   }
2370
2371   s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
2372   g_free (buf_str);
2373   g_free (caps_str);
2374   g_free (seg_str);
2375   g_free (info_str);
2376
2377   return s;
2378 }
2379
2380 static gboolean
2381 gst_value_deserialize_sample (GValue * dest, const gchar * s)
2382 {
2383   GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
2384   GstStructure *info;
2385   GstSample *sample;
2386   GstCaps *caps;
2387   gboolean ret = FALSE;
2388   gchar **fields;
2389   gsize outlen;
2390   gint len;
2391
2392   GST_TRACE ("deserialize '%s'", s);
2393
2394   fields = g_strsplit (s, ":", -1);
2395   len = g_strv_length (fields);
2396   if (len != 4)
2397     goto wrong_length;
2398
2399   g_value_init (&bval, GST_TYPE_BUFFER);
2400   g_value_init (&sval, GST_TYPE_SEGMENT);
2401
2402   if (!gst_value_deserialize_buffer (&bval, fields[0]))
2403     goto fail;
2404
2405   if (strcmp (fields[1], "None") != 0) {
2406     g_strdelimit (fields[1], "_", '=');
2407     g_base64_decode_inplace (fields[1], &outlen);
2408     GST_TRACE ("caps    : %s", fields[1]);
2409     caps = gst_caps_from_string (fields[1]);
2410     if (caps == NULL)
2411       goto fail;
2412   } else {
2413     caps = NULL;
2414   }
2415
2416   if (strcmp (fields[2], "None") != 0) {
2417     g_strdelimit (fields[2], "_", '=');
2418     g_base64_decode_inplace (fields[2], &outlen);
2419     GST_TRACE ("segment : %s", fields[2]);
2420     if (!gst_value_deserialize_segment (&sval, fields[2]))
2421       goto fail;
2422   }
2423
2424   if (strcmp (fields[3], "None") != 0) {
2425     g_strdelimit (fields[3], "_", '=');
2426     g_base64_decode_inplace (fields[3], &outlen);
2427     GST_TRACE ("info    : %s", fields[3]);
2428     info = gst_structure_from_string (fields[3], NULL);
2429     if (info == NULL)
2430       goto fail;
2431   } else {
2432     info = NULL;
2433   }
2434
2435   sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
2436       g_value_get_boxed (&sval), info);
2437
2438   g_value_take_boxed (dest, sample);
2439
2440   if (caps)
2441     gst_caps_unref (caps);
2442
2443   ret = TRUE;
2444
2445 fail:
2446
2447   g_value_unset (&bval);
2448   g_value_unset (&sval);
2449
2450 wrong_length:
2451
2452   g_strfreev (fields);
2453
2454   return ret;
2455 }
2456
2457 /***********
2458  * boolean *
2459  ***********/
2460
2461 static gint
2462 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
2463 {
2464   if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
2465     return GST_VALUE_EQUAL;
2466   return GST_VALUE_UNORDERED;
2467 }
2468
2469 static gchar *
2470 gst_value_serialize_boolean (const GValue * value)
2471 {
2472   if (value->data[0].v_int) {
2473     return g_strdup ("true");
2474   }
2475   return g_strdup ("false");
2476 }
2477
2478 static gboolean
2479 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
2480 {
2481   gboolean ret = FALSE;
2482
2483   if (g_ascii_strcasecmp (s, "true") == 0 ||
2484       g_ascii_strcasecmp (s, "yes") == 0 ||
2485       g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
2486     g_value_set_boolean (dest, TRUE);
2487     ret = TRUE;
2488   } else if (g_ascii_strcasecmp (s, "false") == 0 ||
2489       g_ascii_strcasecmp (s, "no") == 0 ||
2490       g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
2491     g_value_set_boolean (dest, FALSE);
2492     ret = TRUE;
2493   }
2494
2495   return ret;
2496 }
2497
2498 #define CREATE_SERIALIZATION_START(_type,_macro)                        \
2499 static gint                                                             \
2500 gst_value_compare_ ## _type                                             \
2501 (const GValue * value1, const GValue * value2)                          \
2502 {                                                                       \
2503   g ## _type val1 = g_value_get_ ## _type (value1);                     \
2504   g ## _type val2 = g_value_get_ ## _type (value2);                     \
2505   if (val1 > val2)                                                      \
2506     return GST_VALUE_GREATER_THAN;                                      \
2507   if (val1 < val2)                                                      \
2508     return GST_VALUE_LESS_THAN;                                         \
2509   return GST_VALUE_EQUAL;                                               \
2510 }                                                                       \
2511                                                                         \
2512 static gchar *                                                          \
2513 gst_value_serialize_ ## _type (const GValue * value)                    \
2514 {                                                                       \
2515   GValue val = { 0, };                                                  \
2516   g_value_init (&val, G_TYPE_STRING);                                   \
2517   if (!g_value_transform (value, &val))                                 \
2518     g_assert_not_reached ();                                            \
2519   /* NO_COPY_MADNESS!!! */                                              \
2520   return (char *) g_value_get_string (&val);                            \
2521 }
2522
2523 /* deserialize the given s into to as a gint64.
2524  * check if the result is actually storeable in the given size number of
2525  * bytes.
2526  */
2527 static gboolean
2528 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
2529     gint64 min, gint64 max, gint size)
2530 {
2531   gboolean ret = FALSE;
2532   gchar *end;
2533   guint64 mask = ~0;
2534
2535   errno = 0;
2536   *to = g_ascii_strtoull (s, &end, 0);
2537   /* a range error is a definitive no-no */
2538   if (errno == ERANGE) {
2539     return FALSE;
2540   }
2541
2542   if (*end == 0) {
2543     ret = TRUE;
2544   } else {
2545     if (g_ascii_strcasecmp (s, "little_endian") == 0) {
2546       *to = G_LITTLE_ENDIAN;
2547       ret = TRUE;
2548     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
2549       *to = G_BIG_ENDIAN;
2550       ret = TRUE;
2551     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
2552       *to = G_BYTE_ORDER;
2553       ret = TRUE;
2554     } else if (g_ascii_strcasecmp (s, "min") == 0) {
2555       *to = min;
2556       ret = TRUE;
2557     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2558       *to = max;
2559       ret = TRUE;
2560     }
2561   }
2562   if (ret) {
2563     /* by definition, a gint64 fits into a gint64; so ignore those */
2564     if (size != sizeof (mask)) {
2565       if (*to >= 0) {
2566         /* for positive numbers, we create a mask of 1's outside of the range
2567          * and 0's inside the range.  An and will thus keep only 1 bits
2568          * outside of the range */
2569         mask <<= (size * 8);
2570         if ((mask & *to) != 0) {
2571           ret = FALSE;
2572         }
2573       } else {
2574         /* for negative numbers, we do a 2's complement version */
2575         mask <<= ((size * 8) - 1);
2576         if ((mask & *to) != mask) {
2577           ret = FALSE;
2578         }
2579       }
2580     }
2581   }
2582   return ret;
2583 }
2584
2585 #define CREATE_SERIALIZATION(_type,_macro)                              \
2586 CREATE_SERIALIZATION_START(_type,_macro)                                \
2587                                                                         \
2588 static gboolean                                                         \
2589 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2590 {                                                                       \
2591   gint64 x;                                                             \
2592                                                                         \
2593   if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro,         \
2594       G_MAX ## _macro, sizeof (g ## _type))) {                          \
2595     g_value_set_ ## _type (dest, /*(g ## _type)*/ x);                   \
2596     return TRUE;                                                        \
2597   } else {                                                              \
2598     return FALSE;                                                       \
2599   }                                                                     \
2600 }
2601
2602 #define CREATE_USERIALIZATION(_type,_macro)                             \
2603 CREATE_SERIALIZATION_START(_type,_macro)                                \
2604                                                                         \
2605 static gboolean                                                         \
2606 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s)         \
2607 {                                                                       \
2608   gint64 x;                                                             \
2609   gchar *end;                                                           \
2610   gboolean ret = FALSE;                                                 \
2611                                                                         \
2612   errno = 0;                                                            \
2613   x = g_ascii_strtoull (s, &end, 0);                                    \
2614   /* a range error is a definitive no-no */                             \
2615   if (errno == ERANGE) {                                                \
2616     return FALSE;                                                       \
2617   }                                                                     \
2618   /* the cast ensures the range check later on makes sense */           \
2619   x = (g ## _type) x;                                                   \
2620   if (*end == 0) {                                                      \
2621     ret = TRUE;                                                         \
2622   } else {                                                              \
2623     if (g_ascii_strcasecmp (s, "little_endian") == 0) {                 \
2624       x = G_LITTLE_ENDIAN;                                              \
2625       ret = TRUE;                                                       \
2626     } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {             \
2627       x = G_BIG_ENDIAN;                                                 \
2628       ret = TRUE;                                                       \
2629     } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {             \
2630       x = G_BYTE_ORDER;                                                 \
2631       ret = TRUE;                                                       \
2632     } else if (g_ascii_strcasecmp (s, "min") == 0) {                    \
2633       x = 0;                                                            \
2634       ret = TRUE;                                                       \
2635     } else if (g_ascii_strcasecmp (s, "max") == 0) {                    \
2636       x = G_MAX ## _macro;                                              \
2637       ret = TRUE;                                                       \
2638     }                                                                   \
2639   }                                                                     \
2640   if (ret) {                                                            \
2641     if (x > G_MAX ## _macro) {                                          \
2642       ret = FALSE;                                                      \
2643     } else {                                                            \
2644       g_value_set_ ## _type (dest, x);                                  \
2645     }                                                                   \
2646   }                                                                     \
2647   return ret;                                                           \
2648 }
2649
2650 #define REGISTER_SERIALIZATION(_gtype, _type)                           \
2651 G_STMT_START {                                                          \
2652   static const GstValueTable gst_value = {                              \
2653     _gtype,                                                             \
2654     gst_value_compare_ ## _type,                                        \
2655     gst_value_serialize_ ## _type,                                      \
2656     gst_value_deserialize_ ## _type,                                    \
2657   };                                                                    \
2658                                                                         \
2659   gst_value_register (&gst_value);                                      \
2660 } G_STMT_END
2661
2662 CREATE_SERIALIZATION (int, INT);
2663 CREATE_SERIALIZATION (int64, INT64);
2664 CREATE_SERIALIZATION (long, LONG);
2665
2666 CREATE_USERIALIZATION (uint, UINT);
2667 CREATE_USERIALIZATION (uint64, UINT64);
2668 CREATE_USERIALIZATION (ulong, ULONG);
2669
2670 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2671 #ifndef G_MAXUCHAR
2672 #define G_MAXUCHAR 255
2673 #endif
2674 CREATE_USERIALIZATION (uchar, UCHAR);
2675
2676 /**********
2677  * double *
2678  **********/
2679 static gint
2680 gst_value_compare_double (const GValue * value1, const GValue * value2)
2681 {
2682   if (value1->data[0].v_double > value2->data[0].v_double)
2683     return GST_VALUE_GREATER_THAN;
2684   if (value1->data[0].v_double < value2->data[0].v_double)
2685     return GST_VALUE_LESS_THAN;
2686   if (value1->data[0].v_double == value2->data[0].v_double)
2687     return GST_VALUE_EQUAL;
2688   return GST_VALUE_UNORDERED;
2689 }
2690
2691 static gchar *
2692 gst_value_serialize_double (const GValue * value)
2693 {
2694   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2695
2696   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2697   return g_strdup (d);
2698 }
2699
2700 static gboolean
2701 gst_value_deserialize_double (GValue * dest, const gchar * s)
2702 {
2703   gdouble x;
2704   gboolean ret = FALSE;
2705   gchar *end;
2706
2707   x = g_ascii_strtod (s, &end);
2708   if (*end == 0) {
2709     ret = TRUE;
2710   } else {
2711     if (g_ascii_strcasecmp (s, "min") == 0) {
2712       x = -G_MAXDOUBLE;
2713       ret = TRUE;
2714     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2715       x = G_MAXDOUBLE;
2716       ret = TRUE;
2717     }
2718   }
2719   if (ret) {
2720     g_value_set_double (dest, x);
2721   }
2722   return ret;
2723 }
2724
2725 /*********
2726  * float *
2727  *********/
2728
2729 static gint
2730 gst_value_compare_float (const GValue * value1, const GValue * value2)
2731 {
2732   if (value1->data[0].v_float > value2->data[0].v_float)
2733     return GST_VALUE_GREATER_THAN;
2734   if (value1->data[0].v_float < value2->data[0].v_float)
2735     return GST_VALUE_LESS_THAN;
2736   if (value1->data[0].v_float == value2->data[0].v_float)
2737     return GST_VALUE_EQUAL;
2738   return GST_VALUE_UNORDERED;
2739 }
2740
2741 static gchar *
2742 gst_value_serialize_float (const GValue * value)
2743 {
2744   gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2745
2746   g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2747   return g_strdup (d);
2748 }
2749
2750 static gboolean
2751 gst_value_deserialize_float (GValue * dest, const gchar * s)
2752 {
2753   gdouble x;
2754   gboolean ret = FALSE;
2755   gchar *end;
2756
2757   x = g_ascii_strtod (s, &end);
2758   if (*end == 0) {
2759     ret = TRUE;
2760   } else {
2761     if (g_ascii_strcasecmp (s, "min") == 0) {
2762       x = -G_MAXFLOAT;
2763       ret = TRUE;
2764     } else if (g_ascii_strcasecmp (s, "max") == 0) {
2765       x = G_MAXFLOAT;
2766       ret = TRUE;
2767     }
2768   }
2769   if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2770     ret = FALSE;
2771   if (ret) {
2772     g_value_set_float (dest, (float) x);
2773   }
2774   return ret;
2775 }
2776
2777 /**********
2778  * string *
2779  **********/
2780
2781 static gint
2782 gst_value_compare_string (const GValue * value1, const GValue * value2)
2783 {
2784   if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2785     /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2786     if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2787       return GST_VALUE_UNORDERED;
2788   } else {
2789     gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2790
2791     if (x < 0)
2792       return GST_VALUE_LESS_THAN;
2793     if (x > 0)
2794       return GST_VALUE_GREATER_THAN;
2795   }
2796
2797   return GST_VALUE_EQUAL;
2798 }
2799
2800 static gint
2801 gst_string_measure_wrapping (const gchar * s)
2802 {
2803   gint len;
2804   gboolean wrap = FALSE;
2805
2806   if (G_UNLIKELY (s == NULL))
2807     return -1;
2808
2809   /* Special case: the actual string NULL needs wrapping */
2810   if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2811     return 4;
2812
2813   len = 0;
2814   while (*s) {
2815     if (GST_ASCII_IS_STRING (*s)) {
2816       len++;
2817     } else if (*s < 0x20 || *s >= 0x7f) {
2818       wrap = TRUE;
2819       len += 4;
2820     } else {
2821       wrap = TRUE;
2822       len += 2;
2823     }
2824     s++;
2825   }
2826
2827   /* Wrap the string if we found something that needs
2828    * wrapping, or the empty string (len == 0) */
2829   return (wrap || len == 0) ? len : -1;
2830 }
2831
2832 static gchar *
2833 gst_string_wrap_inner (const gchar * s, gint len)
2834 {
2835   gchar *d, *e;
2836
2837   e = d = g_malloc (len + 3);
2838
2839   *e++ = '\"';
2840   while (*s) {
2841     if (GST_ASCII_IS_STRING (*s)) {
2842       *e++ = *s++;
2843     } else if (*s < 0x20 || *s >= 0x7f) {
2844       *e++ = '\\';
2845       *e++ = '0' + ((*(guchar *) s) >> 6);
2846       *e++ = '0' + (((*s) >> 3) & 0x7);
2847       *e++ = '0' + ((*s++) & 0x7);
2848     } else {
2849       *e++ = '\\';
2850       *e++ = *s++;
2851     }
2852   }
2853   *e++ = '\"';
2854   *e = 0;
2855
2856   g_assert (e - d <= len + 3);
2857   return d;
2858 }
2859
2860 /* Do string wrapping/escaping */
2861 static gchar *
2862 gst_string_wrap (const gchar * s)
2863 {
2864   gint len = gst_string_measure_wrapping (s);
2865
2866   if (G_LIKELY (len < 0))
2867     return g_strdup (s);
2868
2869   return gst_string_wrap_inner (s, len);
2870 }
2871
2872 /* Same as above, but take ownership of the string */
2873 static gchar *
2874 gst_string_take_and_wrap (gchar * s)
2875 {
2876   gchar *out;
2877   gint len = gst_string_measure_wrapping (s);
2878
2879   if (G_LIKELY (len < 0))
2880     return s;
2881
2882   out = gst_string_wrap_inner (s, len);
2883   g_free (s);
2884
2885   return out;
2886 }
2887
2888 /*
2889  * This function takes a string delimited with double quotes (")
2890  * and unescapes any \xxx octal numbers.
2891  *
2892  * If sequences of \y are found where y is not in the range of
2893  * 0->3, y is copied unescaped.
2894  *
2895  * If \xyy is found where x is an octal number but y is not, an
2896  * error is encountered and %NULL is returned.
2897  *
2898  * the input string must be \0 terminated.
2899  */
2900 static gchar *
2901 gst_string_unwrap (const gchar * s)
2902 {
2903   gchar *ret;
2904   gchar *read, *write;
2905
2906   /* NULL string returns NULL */
2907   if (s == NULL)
2908     return NULL;
2909
2910   /* strings not starting with " are invalid */
2911   if (*s != '"')
2912     return NULL;
2913
2914   /* make copy of original string to hold the result. This
2915    * string will always be smaller than the original */
2916   ret = g_strdup (s);
2917   read = ret;
2918   write = ret;
2919
2920   /* need to move to the next position as we parsed the " */
2921   read++;
2922
2923   while (*read) {
2924     if (GST_ASCII_IS_STRING (*read)) {
2925       /* normal chars are just copied */
2926       *write++ = *read++;
2927     } else if (*read == '"') {
2928       /* quote marks end of string */
2929       break;
2930     } else if (*read == '\\') {
2931       /* got an escape char, move to next position to read a tripplet
2932        * of octal numbers */
2933       read++;
2934       /* is the next char a possible first octal number? */
2935       if (*read >= '0' && *read <= '3') {
2936         /* parse other 2 numbers, if one of them is not in the range of
2937          * an octal number, we error. We also catch the case where a zero
2938          * byte is found here. */
2939         if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2940           goto beach;
2941
2942         /* now convert the octal number to a byte again. */
2943         *write++ = ((read[0] - '0') << 6) +
2944             ((read[1] - '0') << 3) + (read[2] - '0');
2945
2946         read += 3;
2947       } else {
2948         /* if we run into a \0 here, we definitely won't get a quote later */
2949         if (*read == 0)
2950           goto beach;
2951
2952         /* else copy \X sequence */
2953         *write++ = *read++;
2954       }
2955     } else {
2956       /* weird character, error */
2957       goto beach;
2958     }
2959   }
2960   /* if the string is not ending in " and zero terminated, we error */
2961   if (*read != '"' || read[1] != '\0')
2962     goto beach;
2963
2964   /* null terminate result string and return */
2965   *write = '\0';
2966   return ret;
2967
2968 beach:
2969   g_free (ret);
2970   return NULL;
2971 }
2972
2973 static gchar *
2974 gst_value_serialize_string (const GValue * value)
2975 {
2976   return gst_string_wrap (value->data[0].v_pointer);
2977 }
2978
2979 static gboolean
2980 gst_value_deserialize_string (GValue * dest, const gchar * s)
2981 {
2982   if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2983     g_value_set_string (dest, NULL);
2984     return TRUE;
2985   } else if (G_LIKELY (*s != '"')) {
2986     if (!g_utf8_validate (s, -1, NULL))
2987       return FALSE;
2988     g_value_set_string (dest, s);
2989     return TRUE;
2990   } else {
2991     gchar *str = gst_string_unwrap (s);
2992     if (G_UNLIKELY (!str))
2993       return FALSE;
2994     g_value_take_string (dest, str);
2995   }
2996
2997   return TRUE;
2998 }
2999
3000 /********
3001  * enum *
3002  ********/
3003
3004 static gint
3005 gst_value_compare_enum (const GValue * value1, const GValue * value2)
3006 {
3007   GEnumValue *en1, *en2;
3008   GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
3009   GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
3010
3011   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
3012   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
3013   en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
3014   en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
3015   g_type_class_unref (klass1);
3016   g_type_class_unref (klass2);
3017   g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
3018   g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
3019   if (en1->value < en2->value)
3020     return GST_VALUE_LESS_THAN;
3021   if (en1->value > en2->value)
3022     return GST_VALUE_GREATER_THAN;
3023
3024   return GST_VALUE_EQUAL;
3025 }
3026
3027 static gchar *
3028 gst_value_serialize_enum (const GValue * value)
3029 {
3030   GEnumValue *en;
3031   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
3032
3033   g_return_val_if_fail (klass, NULL);
3034   en = g_enum_get_value (klass, g_value_get_enum (value));
3035   g_type_class_unref (klass);
3036
3037   /* might be one of the custom formats registered later */
3038   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
3039     const GstFormatDefinition *format_def;
3040
3041     format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
3042     g_return_val_if_fail (format_def != NULL, NULL);
3043     return g_strdup (format_def->description);
3044   }
3045
3046   g_return_val_if_fail (en, NULL);
3047   return g_strdup (en->value_name);
3048 }
3049
3050 static gint
3051 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
3052     const gchar * s)
3053 {
3054   const GstFormatDefinition *format_def =
3055       g_value_get_pointer (format_def_value);
3056
3057   if (g_ascii_strcasecmp (s, format_def->nick) == 0)
3058     return 0;
3059
3060   return g_ascii_strcasecmp (s, format_def->description);
3061 }
3062
3063 static gboolean
3064 gst_value_deserialize_enum (GValue * dest, const gchar * s)
3065 {
3066   GEnumValue *en;
3067   gchar *endptr = NULL;
3068   GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3069
3070   g_return_val_if_fail (klass, FALSE);
3071   if (!(en = g_enum_get_value_by_name (klass, s))) {
3072     if (!(en = g_enum_get_value_by_nick (klass, s))) {
3073       gint i = strtol (s, &endptr, 0);
3074
3075       if (endptr && *endptr == '\0') {
3076         en = g_enum_get_value (klass, i);
3077       }
3078     }
3079   }
3080   g_type_class_unref (klass);
3081
3082   /* might be one of the custom formats registered later */
3083   if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
3084     GValue res = { 0, };
3085     const GstFormatDefinition *format_def;
3086     GstIterator *iter;
3087     gboolean found;
3088
3089     iter = gst_format_iterate_definitions ();
3090
3091     found = gst_iterator_find_custom (iter,
3092         (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
3093
3094     if (found) {
3095       format_def = g_value_get_pointer (&res);
3096       g_return_val_if_fail (format_def != NULL, FALSE);
3097       g_value_set_enum (dest, (gint) format_def->value);
3098       g_value_unset (&res);
3099     }
3100     gst_iterator_free (iter);
3101     return found;
3102   }
3103
3104   /* enum name/nick not found */
3105   if (en == NULL)
3106     return FALSE;
3107
3108   g_value_set_enum (dest, en->value);
3109   return TRUE;
3110 }
3111
3112 /********
3113  * flags *
3114  ********/
3115
3116 /* we just compare the value here */
3117 static gint
3118 gst_value_compare_flags (const GValue * value1, const GValue * value2)
3119 {
3120   guint fl1, fl2;
3121   GFlagsClass *klass1 =
3122       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
3123   GFlagsClass *klass2 =
3124       (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
3125
3126   g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
3127   g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
3128   fl1 = g_value_get_flags (value1);
3129   fl2 = g_value_get_flags (value2);
3130   g_type_class_unref (klass1);
3131   g_type_class_unref (klass2);
3132   if (fl1 < fl2)
3133     return GST_VALUE_LESS_THAN;
3134   if (fl1 > fl2)
3135     return GST_VALUE_GREATER_THAN;
3136
3137   return GST_VALUE_EQUAL;
3138 }
3139
3140 /* the different flags are serialized separated with a + */
3141 static gchar *
3142 gst_value_serialize_flags (const GValue * value)
3143 {
3144   guint flags;
3145   GFlagsValue *fl;
3146   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
3147   gchar *result, *tmp;
3148   gboolean first = TRUE;
3149
3150   g_return_val_if_fail (klass, NULL);
3151
3152   flags = g_value_get_flags (value);
3153
3154   /* if no flags are set, try to serialize to the _NONE string */
3155   if (!flags) {
3156     fl = g_flags_get_first_value (klass, flags);
3157     if (fl)
3158       return g_strdup (fl->value_name);
3159     else
3160       return g_strdup ("0");
3161   }
3162
3163   /* some flags are set, so serialize one by one */
3164   result = g_strdup ("");
3165   while (flags) {
3166     fl = g_flags_get_first_value (klass, flags);
3167     if (fl != NULL) {
3168       tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
3169       g_free (result);
3170       result = tmp;
3171       first = FALSE;
3172
3173       /* clear flag */
3174       flags &= ~fl->value;
3175     }
3176   }
3177   g_type_class_unref (klass);
3178
3179   return result;
3180 }
3181
3182 static gboolean
3183 gst_value_deserialize_flags (GValue * dest, const gchar * s)
3184 {
3185   GFlagsValue *fl;
3186   gchar *endptr = NULL;
3187   GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
3188   gchar **split;
3189   guint flags;
3190   gint i;
3191
3192   g_return_val_if_fail (klass, FALSE);
3193
3194   /* split into parts delimited with + */
3195   split = g_strsplit (s, "+", 0);
3196
3197   flags = 0;
3198   i = 0;
3199   /* loop over each part */
3200   while (split[i]) {
3201     if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
3202       if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
3203         gint val = strtol (split[i], &endptr, 0);
3204
3205         /* just or numeric value */
3206         if (endptr && *endptr == '\0') {
3207           flags |= val;
3208         }
3209       }
3210     }
3211     if (fl) {
3212       flags |= fl->value;
3213     }
3214     i++;
3215   }
3216   g_strfreev (split);
3217   g_type_class_unref (klass);
3218   g_value_set_flags (dest, flags);
3219
3220   return TRUE;
3221 }
3222
3223 /****************
3224  * subset *
3225  ****************/
3226
3227 static gboolean
3228 gst_value_is_subset_int_range_int_range (const GValue * value1,
3229     const GValue * value2)
3230 {
3231   gint gcd;
3232
3233   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
3234   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
3235
3236   if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
3237       INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
3238     return FALSE;
3239   if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
3240       INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
3241     return FALSE;
3242
3243   if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
3244     if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
3245         INT_RANGE_STEP (value1))
3246       return FALSE;
3247     return TRUE;
3248   }
3249
3250   gcd =
3251       gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
3252       INT_RANGE_STEP (value2));
3253   if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
3254     return FALSE;
3255
3256   return TRUE;
3257 }
3258
3259 static gboolean
3260 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
3261     const GValue * value2)
3262 {
3263   gint64 gcd;
3264
3265   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
3266   g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
3267
3268   if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
3269     return FALSE;
3270   if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
3271     return FALSE;
3272
3273   if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
3274     if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
3275         INT64_RANGE_STEP (value1))
3276       return FALSE;
3277     return TRUE;
3278   }
3279
3280   gcd =
3281       gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
3282       INT64_RANGE_STEP (value2));
3283   if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
3284     return FALSE;
3285
3286   return TRUE;
3287 }
3288
3289 /**
3290  * gst_value_is_subset:
3291  * @value1: a #GValue
3292  * @value2: a #GValue
3293  *
3294  * Check that @value1 is a subset of @value2.
3295  *
3296  * Return: %TRUE is @value1 is a subset of @value2
3297  */
3298 gboolean
3299 gst_value_is_subset (const GValue * value1, const GValue * value2)
3300 {
3301   /* special case for int/int64 ranges, since we cannot compute
3302      the difference for those when they have different steps,
3303      and it's actually a lot simpler to compute whether a range
3304      is a subset of another. */
3305   if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
3306     return gst_value_is_subset_int_range_int_range (value1, value2);
3307   } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
3308       && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
3309     return gst_value_is_subset_int64_range_int64_range (value1, value2);
3310   }
3311
3312   /*
3313    * 1 - [1,2] = empty
3314    * -> !subset
3315    *
3316    * [1,2] - 1 = 2
3317    *  -> 1 - [1,2] = empty
3318    *  -> subset
3319    *
3320    * [1,3] - [1,2] = 3
3321    * -> [1,2] - [1,3] = empty
3322    * -> subset
3323    *
3324    * {1,2} - {1,3} = 2
3325    * -> {1,3} - {1,2} = 3
3326    * -> !subset
3327    *
3328    *  First caps subtraction needs to return a non-empty set, second
3329    *  subtractions needs to give en empty set.
3330    *  Both substractions are switched below, as it's faster that way.
3331    */
3332   if (!gst_value_subtract (NULL, value1, value2)) {
3333     if (gst_value_subtract (NULL, value2, value1)) {
3334       return TRUE;
3335     }
3336   }
3337   return FALSE;
3338 }
3339
3340 /*********
3341  * union *
3342  *********/
3343
3344 static gboolean
3345 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
3346     const GValue * src2)
3347 {
3348   gint v = src1->data[0].v_int;
3349
3350   /* check if it's already in the range */
3351   if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
3352       INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
3353       v % INT_RANGE_STEP (src2) == 0) {
3354     if (dest)
3355       gst_value_init_and_copy (dest, src2);
3356     return TRUE;
3357   }
3358
3359   /* check if it extends the range */
3360   if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
3361     if (dest) {
3362       gst_value_init_and_copy (dest, src2);
3363       --INT_RANGE_MIN (src2);
3364     }
3365     return TRUE;
3366   }
3367   if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
3368     if (dest) {
3369       gst_value_init_and_copy (dest, src2);
3370       ++INT_RANGE_MAX (src2);
3371     }
3372     return TRUE;
3373   }
3374
3375   return FALSE;
3376 }
3377
3378 static gboolean
3379 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
3380     const GValue * src2)
3381 {
3382   /* We can union in several special cases:
3383      1 - one is a subset of another
3384      2 - same step and not disjoint
3385      3 - different step, at least one with one value which matches a 'next' or 'previous'
3386      - anything else ?
3387    */
3388
3389   /* 1 - subset */
3390   if (gst_value_is_subset_int_range_int_range (src1, src2)) {
3391     if (dest)
3392       gst_value_init_and_copy (dest, src2);
3393     return TRUE;
3394   }
3395   if (gst_value_is_subset_int_range_int_range (src2, src1)) {
3396     if (dest)
3397       gst_value_init_and_copy (dest, src1);
3398     return TRUE;
3399   }
3400
3401   /* 2 - same step and not disjoint */
3402   if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
3403     if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
3404             INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
3405         (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
3406             INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
3407       if (dest) {
3408         gint step = INT_RANGE_STEP (src1);
3409         gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
3410         gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
3411         g_value_init (dest, GST_TYPE_INT_RANGE);
3412         gst_value_set_int_range_step (dest, min, max, step);
3413       }
3414       return TRUE;
3415     }
3416   }
3417
3418   /* 3 - single value matches next or previous */
3419   if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
3420     gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
3421     gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
3422     if (n1 == 1 || n2 == 1) {
3423       const GValue *range_value = NULL;
3424       gint scalar = 0;
3425       if (n1 == 1) {
3426         range_value = src2;
3427         scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
3428       } else if (n2 == 1) {
3429         range_value = src1;
3430         scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
3431       }
3432
3433       if (scalar ==
3434           (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
3435         if (dest) {
3436           gst_value_init_and_copy (dest, range_value);
3437           --INT_RANGE_MIN (range_value);
3438         }
3439         return TRUE;
3440       } else if (scalar ==
3441           (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
3442         if (dest) {
3443           gst_value_init_and_copy (dest, range_value);
3444           ++INT_RANGE_MIN (range_value);
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 /**
4464  * gst_value_compare:
4465  * @value1: a value to compare
4466  * @value2: another value to compare
4467  *
4468  * Compares @value1 and @value2.  If @value1 and @value2 cannot be
4469  * compared, the function returns GST_VALUE_UNORDERED.  Otherwise,
4470  * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
4471  * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
4472  * If the values are equal, GST_VALUE_EQUAL is returned.
4473  *
4474  * Returns: comparison result
4475  */
4476 gint
4477 gst_value_compare (const GValue * value1, const GValue * value2)
4478 {
4479   GstValueCompareFunc compare;
4480   GType ltype;
4481
4482   g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
4483   g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
4484
4485   /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
4486      as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
4487   ltype = gst_value_list_get_type ();
4488   if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)) {
4489     gint i, n, ret;
4490
4491     if (gst_value_list_equals_range (value1, value2)) {
4492       return GST_VALUE_EQUAL;
4493     }
4494
4495     n = gst_value_list_get_size (value1);
4496     if (n == 0)
4497       return GST_VALUE_UNORDERED;
4498
4499     for (i = 0; i < n; i++) {
4500       const GValue *elt;
4501
4502       elt = gst_value_list_get_value (value1, i);
4503       ret = gst_value_compare (elt, value2);
4504       if (ret != GST_VALUE_EQUAL && n == 1)
4505         return ret;
4506       else if (ret != GST_VALUE_EQUAL)
4507         return GST_VALUE_UNORDERED;
4508     }
4509
4510     return GST_VALUE_EQUAL;
4511   } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)) {
4512     gint i, n, ret;
4513
4514     if (gst_value_list_equals_range (value2, value1)) {
4515       return GST_VALUE_EQUAL;
4516     }
4517
4518     n = gst_value_list_get_size (value2);
4519     if (n == 0)
4520       return GST_VALUE_UNORDERED;
4521
4522     for (i = 0; i < n; i++) {
4523       const GValue *elt;
4524
4525       elt = gst_value_list_get_value (value2, i);
4526       ret = gst_value_compare (elt, value1);
4527       if (ret != GST_VALUE_EQUAL && n == 1)
4528         return ret;
4529       else if (ret != GST_VALUE_EQUAL)
4530         return GST_VALUE_UNORDERED;
4531     }
4532
4533     return GST_VALUE_EQUAL;
4534   }
4535
4536   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4537     return GST_VALUE_UNORDERED;
4538
4539   compare = gst_value_get_compare_func (value1);
4540   if (compare) {
4541     return compare (value1, value2);
4542   }
4543
4544   g_critical ("unable to compare values of type %s\n",
4545       g_type_name (G_VALUE_TYPE (value1)));
4546   return GST_VALUE_UNORDERED;
4547 }
4548
4549 /*
4550  * gst_value_compare_with_func:
4551  * @value1: a value to compare
4552  * @value2: another value to compare
4553  * @compare: compare function
4554  *
4555  * Compares @value1 and @value2 using the @compare function. Works like
4556  * gst_value_compare() but allows to save time determining the compare function
4557  * a multiple times. 
4558  *
4559  * Returns: comparison result
4560  */
4561 static gint
4562 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
4563     GstValueCompareFunc compare)
4564 {
4565   g_assert (compare);
4566
4567   if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4568     return GST_VALUE_UNORDERED;
4569
4570   return compare (value1, value2);
4571 }
4572
4573 /* union */
4574
4575 /**
4576  * gst_value_can_union:
4577  * @value1: a value to union
4578  * @value2: another value to union
4579  *
4580  * Determines if @value1 and @value2 can be non-trivially unioned.
4581  * Any two values can be trivially unioned by adding both of them
4582  * to a #GstValueList.  However, certain types have the possibility
4583  * to be unioned in a simpler way.  For example, an integer range
4584  * and an integer can be unioned if the integer is a subset of the
4585  * integer range.  If there is the possibility that two values can
4586  * be unioned, this function returns %TRUE.
4587  *
4588  * Returns: %TRUE if there is a function allowing the two values to
4589  * be unioned.
4590  */
4591 gboolean
4592 gst_value_can_union (const GValue * value1, const GValue * value2)
4593 {
4594   GstValueUnionInfo *union_info;
4595   guint i, len;
4596
4597   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4598   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4599
4600   len = gst_value_union_funcs->len;
4601
4602   for (i = 0; i < len; i++) {
4603     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4604     if (union_info->type1 == G_VALUE_TYPE (value1) &&
4605         union_info->type2 == G_VALUE_TYPE (value2))
4606       return TRUE;
4607     if (union_info->type1 == G_VALUE_TYPE (value2) &&
4608         union_info->type2 == G_VALUE_TYPE (value1))
4609       return TRUE;
4610   }
4611
4612   return FALSE;
4613 }
4614
4615 /**
4616  * gst_value_union:
4617  * @dest: (out caller-allocates): the destination value
4618  * @value1: a value to union
4619  * @value2: another value to union
4620  *
4621  * Creates a GValue corresponding to the union of @value1 and @value2.
4622  *
4623  * Returns: %TRUE if the union succeeded.
4624  */
4625 gboolean
4626 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
4627 {
4628   const GstValueUnionInfo *union_info;
4629   guint i, len;
4630   GType type1, type2;
4631
4632   g_return_val_if_fail (dest != NULL, FALSE);
4633   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4634   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4635   g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
4636       FALSE);
4637
4638   len = gst_value_union_funcs->len;
4639   type1 = G_VALUE_TYPE (value1);
4640   type2 = G_VALUE_TYPE (value2);
4641
4642   for (i = 0; i < len; i++) {
4643     union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4644     if (union_info->type1 == type1 && union_info->type2 == type2) {
4645       return union_info->func (dest, value1, value2);
4646     }
4647     if (union_info->type1 == type2 && union_info->type2 == type1) {
4648       return union_info->func (dest, value2, value1);
4649     }
4650   }
4651
4652   gst_value_list_concat (dest, value1, value2);
4653   return TRUE;
4654 }
4655
4656 /* gst_value_register_union_func: (skip)
4657  * @type1: a type to union
4658  * @type2: another type to union
4659  * @func: a function that implements creating a union between the two types
4660  *
4661  * Registers a union function that can create a union between #GValue items
4662  * of the type @type1 and @type2.
4663  *
4664  * Union functions should be registered at startup before any pipelines are
4665  * started, as gst_value_register_union_func() is not thread-safe and cannot
4666  * be used at the same time as gst_value_union() or gst_value_can_union().
4667  */
4668 static void
4669 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
4670 {
4671   GstValueUnionInfo union_info;
4672
4673   union_info.type1 = type1;
4674   union_info.type2 = type2;
4675   union_info.func = func;
4676
4677   g_array_append_val (gst_value_union_funcs, union_info);
4678 }
4679
4680 /* intersection */
4681
4682 /**
4683  * gst_value_can_intersect:
4684  * @value1: a value to intersect
4685  * @value2: another value to intersect
4686  *
4687  * Determines if intersecting two values will produce a valid result.
4688  * Two values will produce a valid intersection if they have the same
4689  * type.
4690  *
4691  * Returns: %TRUE if the values can intersect
4692  */
4693 gboolean
4694 gst_value_can_intersect (const GValue * value1, const GValue * value2)
4695 {
4696   GstValueIntersectInfo *intersect_info;
4697   guint i, len;
4698   GType ltype, type1, type2;
4699
4700   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4701   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4702
4703   ltype = gst_value_list_get_type ();
4704
4705   /* special cases */
4706   if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
4707     return TRUE;
4708
4709   type1 = G_VALUE_TYPE (value1);
4710   type2 = G_VALUE_TYPE (value2);
4711
4712   /* practically all GstValue types have a compare function (_can_compare=TRUE)
4713    * GstStructure and GstCaps have npot, but are intersectable */
4714   if (type1 == type2)
4715     return TRUE;
4716
4717   /* check registered intersect functions */
4718   len = gst_value_intersect_funcs->len;
4719   for (i = 0; i < len; i++) {
4720     intersect_info = &g_array_index (gst_value_intersect_funcs,
4721         GstValueIntersectInfo, i);
4722     if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
4723         (intersect_info->type1 == type2 && intersect_info->type2 == type1))
4724       return TRUE;
4725   }
4726
4727   return gst_value_can_compare (value1, value2);
4728 }
4729
4730 /**
4731  * gst_value_intersect:
4732  * @dest: (out caller-allocates) (transfer full): a uninitialized #GValue that will hold the calculated
4733  * intersection value. May be %NULL if the resulting set if not needed.
4734  * @value1: a value to intersect
4735  * @value2: another value to intersect
4736  *
4737  * Calculates the intersection of two values.  If the values have
4738  * a non-empty intersection, the value representing the intersection
4739  * is placed in @dest, unless %NULL.  If the intersection is non-empty,
4740  * @dest is not modified.
4741  *
4742  * Returns: %TRUE if the intersection is non-empty
4743  */
4744 gboolean
4745 gst_value_intersect (GValue * dest, const GValue * value1,
4746     const GValue * value2)
4747 {
4748   GstValueIntersectInfo *intersect_info;
4749   guint i, len;
4750   GType ltype, type1, type2;
4751
4752   g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4753   g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4754
4755   ltype = gst_value_list_get_type ();
4756
4757   /* special cases first */
4758   if (G_VALUE_HOLDS (value1, ltype))
4759     return gst_value_intersect_list (dest, value1, value2);
4760   if (G_VALUE_HOLDS (value2, ltype))
4761     return gst_value_intersect_list (dest, value2, value1);
4762
4763   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
4764     if (dest)
4765       gst_value_init_and_copy (dest, value1);
4766     return TRUE;
4767   }
4768
4769   type1 = G_VALUE_TYPE (value1);
4770   type2 = G_VALUE_TYPE (value2);
4771
4772   len = gst_value_intersect_funcs->len;
4773   for (i = 0; i < len; i++) {
4774     intersect_info = &g_array_index (gst_value_intersect_funcs,
4775         GstValueIntersectInfo, i);
4776     if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
4777       return intersect_info->func (dest, value1, value2);
4778     }
4779     if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
4780       return intersect_info->func (dest, value2, value1);
4781     }
4782   }
4783   return FALSE;
4784 }
4785
4786
4787
4788 /* gst_value_register_intersect_func: (skip)
4789  * @type1: the first type to intersect
4790  * @type2: the second type to intersect
4791  * @func: the intersection function
4792  *
4793  * Registers a function that is called to calculate the intersection
4794  * of the values having the types @type1 and @type2.
4795  *
4796  * Intersect functions should be registered at startup before any pipelines are
4797  * started, as gst_value_register_intersect_func() is not thread-safe and
4798  * cannot be used at the same time as gst_value_intersect() or
4799  * gst_value_can_intersect().
4800  */
4801 static void
4802 gst_value_register_intersect_func (GType type1, GType type2,
4803     GstValueIntersectFunc func)
4804 {
4805   GstValueIntersectInfo intersect_info;
4806
4807   intersect_info.type1 = type1;
4808   intersect_info.type2 = type2;
4809   intersect_info.func = func;
4810
4811   g_array_append_val (gst_value_intersect_funcs, intersect_info);
4812 }
4813
4814
4815 /* subtraction */
4816
4817 /**
4818  * gst_value_subtract:
4819  * @dest: (out caller-allocates): the destination value for the result if the
4820  *     subtraction is not empty. May be %NULL, in which case the resulting set
4821  *     will not be computed, which can give a fair speedup.
4822  * @minuend: the value to subtract from
4823  * @subtrahend: the value to subtract
4824  *
4825  * Subtracts @subtrahend from @minuend and stores the result in @dest.
4826  * Note that this means subtraction as in sets, not as in mathematics.
4827  *
4828  * Returns: %TRUE if the subtraction is not empty
4829  */
4830 gboolean
4831 gst_value_subtract (GValue * dest, const GValue * minuend,
4832     const GValue * subtrahend)
4833 {
4834   GstValueSubtractInfo *info;
4835   guint i, len;
4836   GType ltype, mtype, stype;
4837
4838   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4839   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4840
4841   ltype = gst_value_list_get_type ();
4842
4843   /* special cases first */
4844   if (G_VALUE_HOLDS (minuend, ltype))
4845     return gst_value_subtract_from_list (dest, minuend, subtrahend);
4846   if (G_VALUE_HOLDS (subtrahend, ltype))
4847     return gst_value_subtract_list (dest, minuend, subtrahend);
4848
4849   mtype = G_VALUE_TYPE (minuend);
4850   stype = G_VALUE_TYPE (subtrahend);
4851
4852   len = gst_value_subtract_funcs->len;
4853   for (i = 0; i < len; i++) {
4854     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4855     if (info->minuend == mtype && info->subtrahend == stype) {
4856       return info->func (dest, minuend, subtrahend);
4857     }
4858   }
4859
4860   if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
4861     if (dest)
4862       gst_value_init_and_copy (dest, minuend);
4863     return TRUE;
4864   }
4865
4866   return FALSE;
4867 }
4868
4869 #if 0
4870 gboolean
4871 gst_value_subtract (GValue * dest, const GValue * minuend,
4872     const GValue * subtrahend)
4873 {
4874   gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
4875
4876   g_printerr ("\"%s\"  -  \"%s\"  =  \"%s\"\n", gst_value_serialize (minuend),
4877       gst_value_serialize (subtrahend),
4878       ret ? gst_value_serialize (dest) : "---");
4879   return ret;
4880 }
4881 #endif
4882
4883 /**
4884  * gst_value_can_subtract:
4885  * @minuend: the value to subtract from
4886  * @subtrahend: the value to subtract
4887  *
4888  * Checks if it's possible to subtract @subtrahend from @minuend.
4889  *
4890  * Returns: %TRUE if a subtraction is possible
4891  */
4892 gboolean
4893 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
4894 {
4895   GstValueSubtractInfo *info;
4896   guint i, len;
4897   GType ltype, mtype, stype;
4898
4899   g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4900   g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4901
4902   ltype = gst_value_list_get_type ();
4903
4904   /* special cases */
4905   if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
4906     return TRUE;
4907
4908   mtype = G_VALUE_TYPE (minuend);
4909   stype = G_VALUE_TYPE (subtrahend);
4910
4911   len = gst_value_subtract_funcs->len;
4912   for (i = 0; i < len; i++) {
4913     info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4914     if (info->minuend == mtype && info->subtrahend == stype)
4915       return TRUE;
4916   }
4917
4918   return gst_value_can_compare (minuend, subtrahend);
4919 }
4920
4921 /* gst_value_register_subtract_func: (skip)
4922  * @minuend_type: type of the minuend
4923  * @subtrahend_type: type of the subtrahend
4924  * @func: function to use
4925  *
4926  * Registers @func as a function capable of subtracting the values of
4927  * @subtrahend_type from values of @minuend_type.
4928  *
4929  * Subtract functions should be registered at startup before any pipelines are
4930  * started, as gst_value_register_subtract_func() is not thread-safe and
4931  * cannot be used at the same time as gst_value_subtract().
4932  */
4933 static void
4934 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
4935     GstValueSubtractFunc func)
4936 {
4937   GstValueSubtractInfo info;
4938
4939   g_return_if_fail (!gst_type_is_fixed (minuend_type)
4940       || !gst_type_is_fixed (subtrahend_type));
4941
4942   info.minuend = minuend_type;
4943   info.subtrahend = subtrahend_type;
4944   info.func = func;
4945
4946   g_array_append_val (gst_value_subtract_funcs, info);
4947 }
4948
4949 /**
4950  * gst_value_register:
4951  * @table: structure containing functions to register
4952  *
4953  * Registers functions to perform calculations on #GValue items of a given
4954  * type. Each type can only be added once.
4955  */
4956 void
4957 gst_value_register (const GstValueTable * table)
4958 {
4959   GstValueTable *found;
4960
4961   g_return_if_fail (table != NULL);
4962
4963   g_array_append_val (gst_value_table, *table);
4964
4965   found = gst_value_hash_lookup_type (table->type);
4966   if (found)
4967     g_warning ("adding type %s multiple times", g_type_name (table->type));
4968
4969   /* FIXME: we're not really doing the const justice, we assume the table is
4970    * static */
4971   gst_value_hash_add_type (table->type, table);
4972 }
4973
4974 /**
4975  * gst_value_init_and_copy:
4976  * @dest: (out caller-allocates): the target value
4977  * @src: the source value
4978  *
4979  * Initialises the target value to be of the same type as source and then copies
4980  * the contents from source to target.
4981  */
4982 void
4983 gst_value_init_and_copy (GValue * dest, const GValue * src)
4984 {
4985   g_return_if_fail (G_IS_VALUE (src));
4986   g_return_if_fail (dest != NULL);
4987
4988   g_value_init (dest, G_VALUE_TYPE (src));
4989   g_value_copy (src, dest);
4990 }
4991
4992 /* move src into dest and clear src */
4993 static void
4994 gst_value_move (GValue * dest, GValue * src)
4995 {
4996   g_assert (G_IS_VALUE (src));
4997   g_assert (dest != NULL);
4998
4999   *dest = *src;
5000   memset (src, 0, sizeof (GValue));
5001 }
5002
5003 /**
5004  * gst_value_serialize:
5005  * @value: a #GValue to serialize
5006  *
5007  * tries to transform the given @value into a string representation that allows
5008  * getting back this string later on using gst_value_deserialize().
5009  *
5010  * Free-function: g_free
5011  *
5012  * Returns: (transfer full): the serialization for @value or %NULL if none exists
5013  */
5014 gchar *
5015 gst_value_serialize (const GValue * value)
5016 {
5017   guint i, len;
5018   GValue s_val = { 0 };
5019   GstValueTable *table, *best;
5020   gchar *s;
5021   GType type;
5022
5023   g_return_val_if_fail (G_IS_VALUE (value), NULL);
5024
5025   type = G_VALUE_TYPE (value);
5026
5027   best = gst_value_hash_lookup_type (type);
5028
5029   if (G_UNLIKELY (!best || !best->serialize)) {
5030     len = gst_value_table->len;
5031     best = NULL;
5032     for (i = 0; i < len; i++) {
5033       table = &g_array_index (gst_value_table, GstValueTable, i);
5034       if (table->serialize && g_type_is_a (type, table->type)) {
5035         if (!best || g_type_is_a (table->type, best->type))
5036           best = table;
5037       }
5038     }
5039   }
5040   if (G_LIKELY (best))
5041     return best->serialize (value);
5042
5043   g_value_init (&s_val, G_TYPE_STRING);
5044   if (g_value_transform (value, &s_val)) {
5045     s = gst_string_wrap (g_value_get_string (&s_val));
5046   } else {
5047     s = NULL;
5048   }
5049   g_value_unset (&s_val);
5050
5051   return s;
5052 }
5053
5054 /**
5055  * gst_value_deserialize:
5056  * @dest: (out caller-allocates): #GValue to fill with contents of
5057  *     deserialization
5058  * @src: string to deserialize
5059  *
5060  * Tries to deserialize a string into the type specified by the given GValue.
5061  * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
5062  *
5063  * Returns: %TRUE on success
5064  */
5065 gboolean
5066 gst_value_deserialize (GValue * dest, const gchar * src)
5067 {
5068   GstValueTable *table, *best;
5069   guint i, len;
5070   GType type;
5071
5072   g_return_val_if_fail (src != NULL, FALSE);
5073   g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
5074
5075   type = G_VALUE_TYPE (dest);
5076
5077   best = gst_value_hash_lookup_type (type);
5078   if (G_UNLIKELY (!best || !best->deserialize)) {
5079     len = gst_value_table->len;
5080     best = NULL;
5081     for (i = 0; i < len; i++) {
5082       table = &g_array_index (gst_value_table, GstValueTable, i);
5083       if (table->deserialize && g_type_is_a (type, table->type)) {
5084         if (!best || g_type_is_a (table->type, best->type))
5085           best = table;
5086       }
5087     }
5088   }
5089   if (G_LIKELY (best))
5090     return best->deserialize (dest, src);
5091
5092   return FALSE;
5093 }
5094
5095 /**
5096  * gst_value_is_fixed:
5097  * @value: the #GValue to check
5098  *
5099  * Tests if the given GValue, if available in a GstStructure (or any other
5100  * container) contains a "fixed" (which means: one value) or an "unfixed"
5101  * (which means: multiple possible values, such as data lists or data
5102  * ranges) value.
5103  *
5104  * Returns: true if the value is "fixed".
5105  */
5106
5107 gboolean
5108 gst_value_is_fixed (const GValue * value)
5109 {
5110   GType type;
5111
5112   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
5113
5114   type = G_VALUE_TYPE (value);
5115
5116   /* the most common types are just basic plain glib types */
5117   if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
5118     return TRUE;
5119   }
5120
5121   if (type == GST_TYPE_ARRAY) {
5122     gint size, n;
5123     const GValue *kid;
5124
5125     /* check recursively */
5126     size = gst_value_array_get_size (value);
5127     for (n = 0; n < size; n++) {
5128       kid = gst_value_array_get_value (value, n);
5129       if (!gst_value_is_fixed (kid))
5130         return FALSE;
5131     }
5132     return TRUE;
5133   }
5134   return gst_type_is_fixed (type);
5135 }
5136
5137 /**
5138  * gst_value_fixate:
5139  * @dest: the #GValue destination
5140  * @src: the #GValue to fixate
5141  *
5142  * Fixate @src into a new value @dest.
5143  * For ranges, the first element is taken. For lists and arrays, the
5144  * first item is fixated and returned.
5145  * If @src is already fixed, this function returns %FALSE.
5146  *
5147  * Returns: %TRUE if @dest contains a fixated version of @src.
5148  */
5149 gboolean
5150 gst_value_fixate (GValue * dest, const GValue * src)
5151 {
5152   g_return_val_if_fail (G_IS_VALUE (src), FALSE);
5153   g_return_val_if_fail (dest != NULL, FALSE);
5154
5155   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
5156     g_value_init (dest, G_TYPE_INT);
5157     g_value_set_int (dest, gst_value_get_int_range_min (src));
5158   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
5159     g_value_init (dest, G_TYPE_DOUBLE);
5160     g_value_set_double (dest, gst_value_get_double_range_min (src));
5161   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
5162     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
5163   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
5164     GValue temp = { 0 };
5165
5166     /* list could be empty */
5167     if (gst_value_list_get_size (src) <= 0)
5168       return FALSE;
5169
5170     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
5171
5172     if (!gst_value_fixate (dest, &temp)) {
5173       gst_value_move (dest, &temp);
5174     } else {
5175       g_value_unset (&temp);
5176     }
5177   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
5178     gboolean res = FALSE;
5179     guint n, len;
5180
5181     len = gst_value_array_get_size (src);
5182     g_value_init (dest, GST_TYPE_ARRAY);
5183     for (n = 0; n < len; n++) {
5184       GValue kid = { 0 };
5185       const GValue *orig_kid = gst_value_array_get_value (src, n);
5186
5187       if (!gst_value_fixate (&kid, orig_kid))
5188         gst_value_init_and_copy (&kid, orig_kid);
5189       else
5190         res = TRUE;
5191       _gst_value_array_append_and_take_value (dest, &kid);
5192     }
5193
5194     if (!res)
5195       g_value_unset (dest);
5196
5197     return res;
5198   } else {
5199     return FALSE;
5200   }
5201   return TRUE;
5202 }
5203
5204
5205 /************
5206  * fraction *
5207  ************/
5208
5209 /* helper functions */
5210 static void
5211 gst_value_init_fraction (GValue * value)
5212 {
5213   value->data[0].v_int = 0;
5214   value->data[1].v_int = 1;
5215 }
5216
5217 static void
5218 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
5219 {
5220   dest_value->data[0].v_int = src_value->data[0].v_int;
5221   dest_value->data[1].v_int = src_value->data[1].v_int;
5222 }
5223
5224 static gchar *
5225 gst_value_collect_fraction (GValue * value, guint n_collect_values,
5226     GTypeCValue * collect_values, guint collect_flags)
5227 {
5228   if (n_collect_values != 2)
5229     return g_strdup_printf ("not enough value locations for `%s' passed",
5230         G_VALUE_TYPE_NAME (value));
5231   if (collect_values[1].v_int == 0)
5232     return g_strdup_printf ("passed '0' as denominator for `%s'",
5233         G_VALUE_TYPE_NAME (value));
5234   if (collect_values[0].v_int < -G_MAXINT)
5235     return
5236         g_strdup_printf
5237         ("passed value smaller than -G_MAXINT as numerator for `%s'",
5238         G_VALUE_TYPE_NAME (value));
5239   if (collect_values[1].v_int < -G_MAXINT)
5240     return
5241         g_strdup_printf
5242         ("passed value smaller than -G_MAXINT as denominator for `%s'",
5243         G_VALUE_TYPE_NAME (value));
5244
5245   gst_value_set_fraction (value,
5246       collect_values[0].v_int, collect_values[1].v_int);
5247
5248   return NULL;
5249 }
5250
5251 static gchar *
5252 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
5253     GTypeCValue * collect_values, guint collect_flags)
5254 {
5255   gint *numerator = collect_values[0].v_pointer;
5256   gint *denominator = collect_values[1].v_pointer;
5257
5258   if (!numerator)
5259     return g_strdup_printf ("numerator for `%s' passed as NULL",
5260         G_VALUE_TYPE_NAME (value));
5261   if (!denominator)
5262     return g_strdup_printf ("denominator for `%s' passed as NULL",
5263         G_VALUE_TYPE_NAME (value));
5264
5265   *numerator = value->data[0].v_int;
5266   *denominator = value->data[1].v_int;
5267
5268   return NULL;
5269 }
5270
5271 /**
5272  * gst_value_set_fraction:
5273  * @value: a GValue initialized to #GST_TYPE_FRACTION
5274  * @numerator: the numerator of the fraction
5275  * @denominator: the denominator of the fraction
5276  *
5277  * Sets @value to the fraction specified by @numerator over @denominator.
5278  * The fraction gets reduced to the smallest numerator and denominator,
5279  * and if necessary the sign is moved to the numerator.
5280  */
5281 void
5282 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
5283 {
5284   gint gcd = 0;
5285
5286   g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
5287   g_return_if_fail (denominator != 0);
5288   g_return_if_fail (denominator >= -G_MAXINT);
5289   g_return_if_fail (numerator >= -G_MAXINT);
5290
5291   /* normalize sign */
5292   if (denominator < 0) {
5293     numerator = -numerator;
5294     denominator = -denominator;
5295   }
5296
5297   /* check for reduction */
5298   gcd = gst_util_greatest_common_divisor (numerator, denominator);
5299   if (gcd) {
5300     numerator /= gcd;
5301     denominator /= gcd;
5302   }
5303
5304   g_assert (denominator > 0);
5305
5306   value->data[0].v_int = numerator;
5307   value->data[1].v_int = denominator;
5308 }
5309
5310 /**
5311  * gst_value_get_fraction_numerator:
5312  * @value: a GValue initialized to #GST_TYPE_FRACTION
5313  *
5314  * Gets the numerator of the fraction specified by @value.
5315  *
5316  * Returns: the numerator of the fraction.
5317  */
5318 gint
5319 gst_value_get_fraction_numerator (const GValue * value)
5320 {
5321   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
5322
5323   return value->data[0].v_int;
5324 }
5325
5326 /**
5327  * gst_value_get_fraction_denominator:
5328  * @value: a GValue initialized to #GST_TYPE_FRACTION
5329  *
5330  * Gets the denominator of the fraction specified by @value.
5331  *
5332  * Returns: the denominator of the fraction.
5333  */
5334 gint
5335 gst_value_get_fraction_denominator (const GValue * value)
5336 {
5337   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
5338
5339   return value->data[1].v_int;
5340 }
5341
5342 /**
5343  * gst_value_fraction_multiply:
5344  * @product: a GValue initialized to #GST_TYPE_FRACTION
5345  * @factor1: a GValue initialized to #GST_TYPE_FRACTION
5346  * @factor2: a GValue initialized to #GST_TYPE_FRACTION
5347  *
5348  * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
5349  * @product to the product of the two fractions.
5350  *
5351  * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
5352  */
5353 gboolean
5354 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
5355     const GValue * factor2)
5356 {
5357   gint n1, n2, d1, d2;
5358   gint res_n, res_d;
5359
5360   g_return_val_if_fail (product != NULL, FALSE);
5361   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
5362   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
5363
5364   n1 = factor1->data[0].v_int;
5365   n2 = factor2->data[0].v_int;
5366   d1 = factor1->data[1].v_int;
5367   d2 = factor2->data[1].v_int;
5368
5369   if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
5370     return FALSE;
5371
5372   gst_value_set_fraction (product, res_n, res_d);
5373
5374   return TRUE;
5375 }
5376
5377 /**
5378  * gst_value_fraction_subtract:
5379  * @dest: a GValue initialized to #GST_TYPE_FRACTION
5380  * @minuend: a GValue initialized to #GST_TYPE_FRACTION
5381  * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
5382  *
5383  * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
5384  *
5385  * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
5386  */
5387 gboolean
5388 gst_value_fraction_subtract (GValue * dest,
5389     const GValue * minuend, const GValue * subtrahend)
5390 {
5391   gint n1, n2, d1, d2;
5392   gint res_n, res_d;
5393
5394   g_return_val_if_fail (dest != NULL, FALSE);
5395   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
5396   g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
5397
5398   n1 = minuend->data[0].v_int;
5399   n2 = subtrahend->data[0].v_int;
5400   d1 = minuend->data[1].v_int;
5401   d2 = subtrahend->data[1].v_int;
5402
5403   if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
5404     return FALSE;
5405   gst_value_set_fraction (dest, res_n, res_d);
5406
5407   return TRUE;
5408 }
5409
5410 static gchar *
5411 gst_value_serialize_fraction (const GValue * value)
5412 {
5413   gint32 numerator = value->data[0].v_int;
5414   gint32 denominator = value->data[1].v_int;
5415   gboolean positive = TRUE;
5416
5417   /* get the sign and make components absolute */
5418   if (numerator < 0) {
5419     numerator = -numerator;
5420     positive = !positive;
5421   }
5422   if (denominator < 0) {
5423     denominator = -denominator;
5424     positive = !positive;
5425   }
5426
5427   return g_strdup_printf ("%s%d/%d",
5428       positive ? "" : "-", numerator, denominator);
5429 }
5430
5431 static gboolean
5432 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
5433 {
5434   gint num, den;
5435   gint num_chars;
5436
5437   if (G_UNLIKELY (s == NULL))
5438     return FALSE;
5439
5440   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
5441     return FALSE;
5442
5443   if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
5444     if (s[num_chars] != 0)
5445       return FALSE;
5446     if (den == 0)
5447       return FALSE;
5448
5449     gst_value_set_fraction (dest, num, den);
5450     return TRUE;
5451   } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
5452     gst_value_set_fraction (dest, 1, G_MAXINT);
5453     return TRUE;
5454   } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
5455     if (s[num_chars] != 0)
5456       return FALSE;
5457     gst_value_set_fraction (dest, num, 1);
5458     return TRUE;
5459   } else if (g_ascii_strcasecmp (s, "min") == 0) {
5460     gst_value_set_fraction (dest, -G_MAXINT, 1);
5461     return TRUE;
5462   } else if (g_ascii_strcasecmp (s, "max") == 0) {
5463     gst_value_set_fraction (dest, G_MAXINT, 1);
5464     return TRUE;
5465   }
5466
5467   return FALSE;
5468 }
5469
5470 static void
5471 gst_value_transform_fraction_string (const GValue * src_value,
5472     GValue * dest_value)
5473 {
5474   dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
5475 }
5476
5477 static void
5478 gst_value_transform_string_fraction (const GValue * src_value,
5479     GValue * dest_value)
5480 {
5481   if (!gst_value_deserialize_fraction (dest_value,
5482           src_value->data[0].v_pointer))
5483     /* If the deserialize fails, ensure we leave the fraction in a
5484      * valid, if incorrect, state */
5485     gst_value_set_fraction (dest_value, 0, 1);
5486 }
5487
5488 static void
5489 gst_value_transform_double_fraction (const GValue * src_value,
5490     GValue * dest_value)
5491 {
5492   gdouble src = g_value_get_double (src_value);
5493   gint n, d;
5494
5495   gst_util_double_to_fraction (src, &n, &d);
5496   gst_value_set_fraction (dest_value, n, d);
5497 }
5498
5499 static void
5500 gst_value_transform_float_fraction (const GValue * src_value,
5501     GValue * dest_value)
5502 {
5503   gfloat src = g_value_get_float (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_fraction_double (const GValue * src_value,
5512     GValue * dest_value)
5513 {
5514   dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
5515       ((double) src_value->data[1].v_int);
5516 }
5517
5518 static void
5519 gst_value_transform_fraction_float (const GValue * src_value,
5520     GValue * dest_value)
5521 {
5522   dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
5523       ((float) src_value->data[1].v_int);
5524 }
5525
5526 static gint
5527 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
5528 {
5529   gint n1, n2;
5530   gint d1, d2;
5531   gint ret;
5532
5533   n1 = value1->data[0].v_int;
5534   n2 = value2->data[0].v_int;
5535   d1 = value1->data[1].v_int;
5536   d2 = value2->data[1].v_int;
5537
5538   /* fractions are reduced when set, so we can quickly see if they're equal */
5539   if (n1 == n2 && d1 == d2)
5540     return GST_VALUE_EQUAL;
5541
5542   if (d1 == 0 && d2 == 0)
5543     return GST_VALUE_UNORDERED;
5544   else if (d1 == 0)
5545     return GST_VALUE_GREATER_THAN;
5546   else if (d2 == 0)
5547     return GST_VALUE_LESS_THAN;
5548
5549   ret = gst_util_fraction_compare (n1, d1, n2, d2);
5550   if (ret == -1)
5551     return GST_VALUE_LESS_THAN;
5552   else if (ret == 1)
5553     return GST_VALUE_GREATER_THAN;
5554
5555   /* Equality can't happen here because we check for that
5556    * first already */
5557   g_return_val_if_reached (GST_VALUE_UNORDERED);
5558 }
5559
5560 /*********
5561  * GDate *
5562  *********/
5563
5564 static gint
5565 gst_value_compare_date (const GValue * value1, const GValue * value2)
5566 {
5567   const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
5568   const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
5569   guint32 j1, j2;
5570
5571   if (date1 == date2)
5572     return GST_VALUE_EQUAL;
5573
5574   if ((date1 == NULL || !g_date_valid (date1))
5575       && (date2 != NULL && g_date_valid (date2))) {
5576     return GST_VALUE_LESS_THAN;
5577   }
5578
5579   if ((date2 == NULL || !g_date_valid (date2))
5580       && (date1 != NULL && g_date_valid (date1))) {
5581     return GST_VALUE_GREATER_THAN;
5582   }
5583
5584   if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
5585       || !g_date_valid (date2)) {
5586     return GST_VALUE_UNORDERED;
5587   }
5588
5589   j1 = g_date_get_julian (date1);
5590   j2 = g_date_get_julian (date2);
5591
5592   if (j1 == j2)
5593     return GST_VALUE_EQUAL;
5594   else if (j1 < j2)
5595     return GST_VALUE_LESS_THAN;
5596   else
5597     return GST_VALUE_GREATER_THAN;
5598 }
5599
5600 static gchar *
5601 gst_value_serialize_date (const GValue * val)
5602 {
5603   const GDate *date = (const GDate *) g_value_get_boxed (val);
5604
5605   if (date == NULL || !g_date_valid (date))
5606     return g_strdup ("9999-99-99");
5607
5608   return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
5609       g_date_get_month (date), g_date_get_day (date));
5610 }
5611
5612 static gboolean
5613 gst_value_deserialize_date (GValue * dest, const gchar * s)
5614 {
5615   guint year, month, day;
5616
5617   if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
5618     return FALSE;
5619
5620   if (!g_date_valid_dmy (day, month, year))
5621     return FALSE;
5622
5623   g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
5624   return TRUE;
5625 }
5626
5627 /*************
5628  * GstDateTime *
5629  *************/
5630
5631 static gint
5632 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
5633 {
5634   const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
5635   const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
5636
5637   if (date1 == date2)
5638     return GST_VALUE_EQUAL;
5639
5640   if ((date1 == NULL) && (date2 != NULL)) {
5641     return GST_VALUE_LESS_THAN;
5642   }
5643   if ((date2 == NULL) && (date1 != NULL)) {
5644     return GST_VALUE_LESS_THAN;
5645   }
5646
5647   /* returns GST_VALUE_* */
5648   return __gst_date_time_compare (date1, date2);
5649 }
5650
5651 static gchar *
5652 gst_value_serialize_date_time (const GValue * val)
5653 {
5654   GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
5655
5656   if (date == NULL)
5657     return g_strdup ("null");
5658
5659   return __gst_date_time_serialize (date, TRUE);
5660 }
5661
5662 static gboolean
5663 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
5664 {
5665   GstDateTime *datetime;
5666
5667   if (!s || strcmp (s, "null") == 0) {
5668     return FALSE;
5669   }
5670
5671   datetime = gst_date_time_new_from_iso8601_string (s);
5672   if (datetime != NULL) {
5673     g_value_take_boxed (dest, datetime);
5674     return TRUE;
5675   }
5676   GST_WARNING ("Failed to deserialize date time string '%s'", s);
5677   return FALSE;
5678 }
5679
5680 static void
5681 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
5682 {
5683   dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
5684 }
5685
5686 static void
5687 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
5688 {
5689   gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
5690 }
5691
5692
5693 /************
5694  * bitmask *
5695  ************/
5696
5697 /* helper functions */
5698 static void
5699 gst_value_init_bitmask (GValue * value)
5700 {
5701   value->data[0].v_uint64 = 0;
5702 }
5703
5704 static void
5705 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
5706 {
5707   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5708 }
5709
5710 static gchar *
5711 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
5712     GTypeCValue * collect_values, guint collect_flags)
5713 {
5714   if (n_collect_values != 1)
5715     return g_strdup_printf ("not enough value locations for `%s' passed",
5716         G_VALUE_TYPE_NAME (value));
5717
5718   gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
5719
5720   return NULL;
5721 }
5722
5723 static gchar *
5724 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
5725     GTypeCValue * collect_values, guint collect_flags)
5726 {
5727   guint64 *bitmask = collect_values[0].v_pointer;
5728
5729   if (!bitmask)
5730     return g_strdup_printf ("value for `%s' passed as NULL",
5731         G_VALUE_TYPE_NAME (value));
5732
5733   *bitmask = value->data[0].v_uint64;
5734
5735   return NULL;
5736 }
5737
5738 /**
5739  * gst_value_set_bitmask:
5740  * @value: a GValue initialized to #GST_TYPE_BITMASK
5741  * @bitmask: the bitmask
5742  *
5743  * Sets @value to the bitmask specified by @bitmask.
5744  */
5745 void
5746 gst_value_set_bitmask (GValue * value, guint64 bitmask)
5747 {
5748   g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
5749
5750   value->data[0].v_uint64 = bitmask;
5751 }
5752
5753 /**
5754  * gst_value_get_bitmask:
5755  * @value: a GValue initialized to #GST_TYPE_BITMASK
5756  *
5757  * Gets the bitmask specified by @value.
5758  *
5759  * Returns: the bitmask.
5760  */
5761 guint64
5762 gst_value_get_bitmask (const GValue * value)
5763 {
5764   g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
5765
5766   return value->data[0].v_uint64;
5767 }
5768
5769 static gchar *
5770 gst_value_serialize_bitmask (const GValue * value)
5771 {
5772   guint64 bitmask = value->data[0].v_uint64;
5773
5774   return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
5775 }
5776
5777 static gboolean
5778 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
5779 {
5780   gchar *endptr = NULL;
5781   guint64 val;
5782
5783   if (G_UNLIKELY (s == NULL))
5784     return FALSE;
5785
5786   if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
5787     return FALSE;
5788
5789   val = g_ascii_strtoull (s, &endptr, 16);
5790   if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
5791     return FALSE;
5792   if (val == 0 && endptr == s)
5793     return FALSE;
5794
5795   gst_value_set_bitmask (dest, val);
5796
5797   return TRUE;
5798 }
5799
5800 static void
5801 gst_value_transform_bitmask_string (const GValue * src_value,
5802     GValue * dest_value)
5803 {
5804   dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
5805 }
5806
5807 static void
5808 gst_value_transform_string_bitmask (const GValue * src_value,
5809     GValue * dest_value)
5810 {
5811   if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
5812     gst_value_set_bitmask (dest_value, 0);
5813 }
5814
5815 static void
5816 gst_value_transform_uint64_bitmask (const GValue * src_value,
5817     GValue * dest_value)
5818 {
5819   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5820 }
5821
5822 static void
5823 gst_value_transform_bitmask_uint64 (const GValue * src_value,
5824     GValue * dest_value)
5825 {
5826   dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5827 }
5828
5829 static gint
5830 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
5831 {
5832   guint64 v1, v2;
5833
5834   v1 = value1->data[0].v_uint64;
5835   v2 = value2->data[0].v_uint64;
5836
5837   if (v1 == v2)
5838     return GST_VALUE_EQUAL;
5839
5840   return GST_VALUE_UNORDERED;
5841 }
5842
5843
5844 /***********************
5845  * GstAllocationParams *
5846  ***********************/
5847 static gint
5848 gst_value_compare_allocation_params (const GValue * value1,
5849     const GValue * value2)
5850 {
5851   GstAllocationParams *v1, *v2;
5852
5853   v1 = value1->data[0].v_pointer;
5854   v2 = value2->data[0].v_pointer;
5855
5856   if (v1 == NULL && v1 == v2)
5857     return GST_VALUE_EQUAL;
5858
5859   if (v1 == NULL || v2 == NULL)
5860     return GST_VALUE_UNORDERED;
5861
5862   if (v1->flags == v2->flags && v1->align == v2->align &&
5863       v1->prefix == v2->prefix && v1->padding == v2->padding)
5864     return GST_VALUE_EQUAL;
5865
5866   return GST_VALUE_UNORDERED;
5867 }
5868
5869
5870 /************
5871  * GObject *
5872  ************/
5873
5874 static gint
5875 gst_value_compare_object (const GValue * value1, const GValue * value2)
5876 {
5877   gpointer v1, v2;
5878
5879   v1 = value1->data[0].v_pointer;
5880   v2 = value2->data[0].v_pointer;
5881
5882   if (v1 == v2)
5883     return GST_VALUE_EQUAL;
5884
5885   return GST_VALUE_UNORDERED;
5886 }
5887
5888 static void
5889 gst_value_transform_object_string (const GValue * src_value,
5890     GValue * dest_value)
5891 {
5892   GstObject *obj;
5893   gchar *str;
5894
5895   obj = g_value_get_object (src_value);
5896   if (obj) {
5897     str =
5898         g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
5899         GST_OBJECT_NAME (obj));
5900   } else {
5901     str = g_strdup ("NULL");
5902   }
5903
5904   dest_value->data[0].v_pointer = str;
5905 }
5906
5907 static GTypeInfo _info = {
5908   0,
5909   NULL,
5910   NULL,
5911   NULL,
5912   NULL,
5913   NULL,
5914   0,
5915   0,
5916   NULL,
5917   NULL,
5918 };
5919
5920 static GTypeFundamentalInfo _finfo = {
5921   0
5922 };
5923
5924 #define FUNC_VALUE_GET_TYPE(type, name)                         \
5925 GType gst_ ## type ## _get_type (void)                          \
5926 {                                                               \
5927   static volatile GType gst_ ## type ## _type = 0;                       \
5928                                                                 \
5929   if (g_once_init_enter (&gst_ ## type ## _type)) {             \
5930     GType _type;                                        \
5931     _info.value_table = & _gst_ ## type ## _value_table;        \
5932     _type = g_type_register_fundamental (       \
5933         g_type_fundamental_next (),                             \
5934         name, &_info, &_finfo, 0);                              \
5935     g_once_init_leave(&gst_ ## type ## _type, _type);   \
5936   }                                                             \
5937                                                                 \
5938   return gst_ ## type ## _type;                                 \
5939 }
5940
5941 static const GTypeValueTable _gst_int_range_value_table = {
5942   gst_value_init_int_range,
5943   gst_value_free_int_range,
5944   gst_value_copy_int_range,
5945   NULL,
5946   (char *) "ii",
5947   gst_value_collect_int_range,
5948   (char *) "pp",
5949   gst_value_lcopy_int_range
5950 };
5951
5952 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
5953
5954 static const GTypeValueTable _gst_int64_range_value_table = {
5955   gst_value_init_int64_range,
5956   gst_value_free_int64_range,
5957   gst_value_copy_int64_range,
5958   NULL,
5959   (char *) "qq",
5960   gst_value_collect_int64_range,
5961   (char *) "pp",
5962   gst_value_lcopy_int64_range
5963 };
5964
5965 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
5966
5967 static const GTypeValueTable _gst_double_range_value_table = {
5968   gst_value_init_double_range,
5969   NULL,
5970   gst_value_copy_double_range,
5971   NULL,
5972   (char *) "dd",
5973   gst_value_collect_double_range,
5974   (char *) "pp",
5975   gst_value_lcopy_double_range
5976 };
5977
5978 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
5979
5980 static const GTypeValueTable _gst_fraction_range_value_table = {
5981   gst_value_init_fraction_range,
5982   gst_value_free_fraction_range,
5983   gst_value_copy_fraction_range,
5984   NULL,
5985   (char *) "iiii",
5986   gst_value_collect_fraction_range,
5987   (char *) "pppp",
5988   gst_value_lcopy_fraction_range
5989 };
5990
5991 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
5992
5993 static const GTypeValueTable _gst_value_list_value_table = {
5994   gst_value_init_list_or_array,
5995   gst_value_free_list_or_array,
5996   gst_value_copy_list_or_array,
5997   gst_value_list_or_array_peek_pointer,
5998   (char *) "p",
5999   gst_value_collect_list_or_array,
6000   (char *) "p",
6001   gst_value_lcopy_list_or_array
6002 };
6003
6004 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
6005
6006 static const GTypeValueTable _gst_value_array_value_table = {
6007   gst_value_init_list_or_array,
6008   gst_value_free_list_or_array,
6009   gst_value_copy_list_or_array,
6010   gst_value_list_or_array_peek_pointer,
6011   (char *) "p",
6012   gst_value_collect_list_or_array,
6013   (char *) "p",
6014   gst_value_lcopy_list_or_array
6015 };
6016
6017 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
6018
6019 static const GTypeValueTable _gst_fraction_value_table = {
6020   gst_value_init_fraction,
6021   NULL,
6022   gst_value_copy_fraction,
6023   NULL,
6024   (char *) "ii",
6025   gst_value_collect_fraction,
6026   (char *) "pp",
6027   gst_value_lcopy_fraction
6028 };
6029
6030 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
6031
6032 G_DEFINE_BOXED_TYPE (GstDateTime, gst_date_time,
6033     (GBoxedCopyFunc) gst_date_time_ref, (GBoxedFreeFunc) gst_date_time_unref);
6034
6035 static const GTypeValueTable _gst_bitmask_value_table = {
6036   gst_value_init_bitmask,
6037   NULL,
6038   gst_value_copy_bitmask,
6039   NULL,
6040   (char *) "q",
6041   gst_value_collect_bitmask,
6042   (char *) "p",
6043   gst_value_lcopy_bitmask
6044 };
6045
6046 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
6047
6048 GType
6049 gst_g_thread_get_type (void)
6050 {
6051 #if GLIB_CHECK_VERSION(2,35,3)
6052   return G_TYPE_THREAD;
6053 #else
6054   static volatile gsize type_id = 0;
6055
6056   if (g_once_init_enter (&type_id)) {
6057     GType tmp =
6058         g_boxed_type_register_static (g_intern_static_string ("GstGThread"),
6059         (GBoxedCopyFunc) g_thread_ref,
6060         (GBoxedFreeFunc) g_thread_unref);
6061     g_once_init_leave (&type_id, tmp);
6062   }
6063
6064   return type_id;
6065 #endif
6066 }
6067
6068 void
6069 _priv_gst_value_initialize (void)
6070 {
6071   gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
6072   gst_value_hash = g_hash_table_new (NULL, NULL);
6073   gst_value_union_funcs = g_array_new (FALSE, FALSE,
6074       sizeof (GstValueUnionInfo));
6075   gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
6076       sizeof (GstValueIntersectInfo));
6077   gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
6078       sizeof (GstValueSubtractInfo));
6079
6080   {
6081     static GstValueTable gst_value = {
6082       0,
6083       gst_value_compare_int_range,
6084       gst_value_serialize_int_range,
6085       gst_value_deserialize_int_range,
6086     };
6087
6088     gst_value.type = gst_int_range_get_type ();
6089     gst_value_register (&gst_value);
6090   }
6091
6092   {
6093     static GstValueTable gst_value = {
6094       0,
6095       gst_value_compare_int64_range,
6096       gst_value_serialize_int64_range,
6097       gst_value_deserialize_int64_range,
6098     };
6099
6100     gst_value.type = gst_int64_range_get_type ();
6101     gst_value_register (&gst_value);
6102   }
6103
6104   {
6105     static GstValueTable gst_value = {
6106       0,
6107       gst_value_compare_double_range,
6108       gst_value_serialize_double_range,
6109       gst_value_deserialize_double_range,
6110     };
6111
6112     gst_value.type = gst_double_range_get_type ();
6113     gst_value_register (&gst_value);
6114   }
6115
6116   {
6117     static GstValueTable gst_value = {
6118       0,
6119       gst_value_compare_fraction_range,
6120       gst_value_serialize_fraction_range,
6121       gst_value_deserialize_fraction_range,
6122     };
6123
6124     gst_value.type = gst_fraction_range_get_type ();
6125     gst_value_register (&gst_value);
6126   }
6127
6128   {
6129     static GstValueTable gst_value = {
6130       0,
6131       gst_value_compare_list,
6132       gst_value_serialize_list,
6133       gst_value_deserialize_list,
6134     };
6135
6136     gst_value.type = gst_value_list_get_type ();
6137     gst_value_register (&gst_value);
6138   }
6139
6140   {
6141     static GstValueTable gst_value = {
6142       0,
6143       gst_value_compare_array,
6144       gst_value_serialize_array,
6145       gst_value_deserialize_array,
6146     };
6147
6148     gst_value.type = gst_value_array_get_type ();
6149     gst_value_register (&gst_value);
6150   }
6151
6152   {
6153 #if 0
6154     static const GTypeValueTable value_table = {
6155       gst_value_init_buffer,
6156       NULL,
6157       gst_value_copy_buffer,
6158       NULL,
6159       "i",
6160       NULL,                     /*gst_value_collect_buffer, */
6161       "p",
6162       NULL                      /*gst_value_lcopy_buffer */
6163     };
6164 #endif
6165     static GstValueTable gst_value = {
6166       0,
6167       gst_value_compare_buffer,
6168       gst_value_serialize_buffer,
6169       gst_value_deserialize_buffer,
6170     };
6171
6172     gst_value.type = GST_TYPE_BUFFER;
6173     gst_value_register (&gst_value);
6174   }
6175   {
6176     static GstValueTable gst_value = {
6177       0,
6178       gst_value_compare_sample,
6179       gst_value_serialize_sample,
6180       gst_value_deserialize_sample,
6181     };
6182
6183     gst_value.type = GST_TYPE_SAMPLE;
6184     gst_value_register (&gst_value);
6185   }
6186   {
6187     static GstValueTable gst_value = {
6188       0,
6189       gst_value_compare_fraction,
6190       gst_value_serialize_fraction,
6191       gst_value_deserialize_fraction,
6192     };
6193
6194     gst_value.type = gst_fraction_get_type ();
6195     gst_value_register (&gst_value);
6196   }
6197   {
6198     static GstValueTable gst_value = {
6199       0,
6200       gst_value_compare_caps,
6201       gst_value_serialize_caps,
6202       gst_value_deserialize_caps,
6203     };
6204
6205     gst_value.type = GST_TYPE_CAPS;
6206     gst_value_register (&gst_value);
6207   }
6208   {
6209     static GstValueTable gst_value = {
6210       0,
6211       NULL,
6212       gst_value_serialize_segment,
6213       gst_value_deserialize_segment,
6214     };
6215
6216     gst_value.type = GST_TYPE_SEGMENT;
6217     gst_value_register (&gst_value);
6218   }
6219   {
6220     static GstValueTable gst_value = {
6221       0,
6222       NULL,
6223       gst_value_serialize_structure,
6224       gst_value_deserialize_structure,
6225     };
6226
6227     gst_value.type = GST_TYPE_STRUCTURE;
6228     gst_value_register (&gst_value);
6229   }
6230   {
6231     static GstValueTable gst_value = {
6232       0,
6233       NULL,
6234       gst_value_serialize_caps_features,
6235       gst_value_deserialize_caps_features,
6236     };
6237
6238     gst_value.type = GST_TYPE_CAPS_FEATURES;
6239     gst_value_register (&gst_value);
6240   }
6241   {
6242     static GstValueTable gst_value = {
6243       0,
6244       NULL,
6245       gst_value_serialize_tag_list,
6246       gst_value_deserialize_tag_list,
6247     };
6248
6249     gst_value.type = GST_TYPE_TAG_LIST;
6250     gst_value_register (&gst_value);
6251   }
6252   {
6253     static GstValueTable gst_value = {
6254       0,
6255       gst_value_compare_date,
6256       gst_value_serialize_date,
6257       gst_value_deserialize_date,
6258     };
6259
6260     gst_value.type = G_TYPE_DATE;
6261     gst_value_register (&gst_value);
6262   }
6263   {
6264     static GstValueTable gst_value = {
6265       0,
6266       gst_value_compare_date_time,
6267       gst_value_serialize_date_time,
6268       gst_value_deserialize_date_time,
6269     };
6270
6271     gst_value.type = gst_date_time_get_type ();
6272     gst_value_register (&gst_value);
6273   }
6274
6275   {
6276     static GstValueTable gst_value = {
6277       0,
6278       gst_value_compare_bitmask,
6279       gst_value_serialize_bitmask,
6280       gst_value_deserialize_bitmask,
6281     };
6282
6283     gst_value.type = gst_bitmask_get_type ();
6284     gst_value_register (&gst_value);
6285   }
6286
6287   {
6288     static GstValueTable gst_value = {
6289       0,
6290       gst_value_compare_allocation_params,
6291       NULL,
6292       NULL,
6293     };
6294
6295     gst_value.type = gst_allocation_params_get_type ();
6296     gst_value_register (&gst_value);
6297   }
6298
6299   {
6300     static GstValueTable gst_value = {
6301       0,
6302       gst_value_compare_object,
6303       NULL,
6304       NULL,
6305     };
6306
6307     gst_value.type = G_TYPE_OBJECT;
6308     gst_value_register (&gst_value);
6309   }
6310
6311   REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
6312   REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
6313
6314   REGISTER_SERIALIZATION (G_TYPE_STRING, string);
6315   REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
6316   REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
6317
6318   REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
6319
6320   REGISTER_SERIALIZATION (G_TYPE_INT, int);
6321
6322   REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
6323   REGISTER_SERIALIZATION (G_TYPE_LONG, long);
6324
6325   REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
6326   REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
6327   REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
6328
6329   REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
6330
6331   g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
6332       gst_value_transform_int_range_string);
6333   g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
6334       gst_value_transform_int64_range_string);
6335   g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
6336       gst_value_transform_double_range_string);
6337   g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
6338       gst_value_transform_fraction_range_string);
6339   g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
6340       gst_value_transform_list_string);
6341   g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
6342       gst_value_transform_array_string);
6343   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
6344       gst_value_transform_fraction_string);
6345   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
6346       gst_value_transform_string_fraction);
6347   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
6348       gst_value_transform_fraction_double);
6349   g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
6350       gst_value_transform_fraction_float);
6351   g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
6352       gst_value_transform_double_fraction);
6353   g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
6354       gst_value_transform_float_fraction);
6355   g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
6356       gst_value_transform_date_string);
6357   g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
6358       gst_value_transform_string_date);
6359   g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
6360       gst_value_transform_object_string);
6361   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
6362       gst_value_transform_bitmask_uint64);
6363   g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
6364       gst_value_transform_bitmask_string);
6365   g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
6366       gst_value_transform_uint64_bitmask);
6367   g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
6368       gst_value_transform_string_bitmask);
6369
6370   gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6371       gst_value_intersect_int_int_range);
6372   gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6373       gst_value_intersect_int_range_int_range);
6374   gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6375       gst_value_intersect_int64_int64_range);
6376   gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6377       gst_value_intersect_int64_range_int64_range);
6378   gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6379       gst_value_intersect_double_double_range);
6380   gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
6381       GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
6382   gst_value_register_intersect_func (GST_TYPE_ARRAY,
6383       GST_TYPE_ARRAY, gst_value_intersect_array);
6384   gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6385       gst_value_intersect_fraction_fraction_range);
6386   gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
6387       GST_TYPE_FRACTION_RANGE,
6388       gst_value_intersect_fraction_range_fraction_range);
6389
6390   gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6391       gst_value_subtract_int_int_range);
6392   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
6393       gst_value_subtract_int_range_int);
6394   gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6395       gst_value_subtract_int_range_int_range);
6396   gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
6397       gst_value_subtract_int64_int64_range);
6398   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
6399       gst_value_subtract_int64_range_int64);
6400   gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
6401       gst_value_subtract_int64_range_int64_range);
6402   gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
6403       gst_value_subtract_double_double_range);
6404   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
6405       gst_value_subtract_double_range_double);
6406   gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
6407       GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
6408   gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6409       gst_value_subtract_fraction_fraction_range);
6410   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
6411       gst_value_subtract_fraction_range_fraction);
6412   gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
6413       GST_TYPE_FRACTION_RANGE,
6414       gst_value_subtract_fraction_range_fraction_range);
6415
6416   /* see bug #317246, #64994, #65041 */
6417   {
6418     volatile GType date_type = G_TYPE_DATE;
6419
6420     g_type_name (date_type);
6421   }
6422
6423   gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
6424       gst_value_union_int_int_range);
6425   gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
6426       gst_value_union_int_range_int_range);
6427
6428 #if 0
6429   /* Implement these if needed */
6430   gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
6431       gst_value_union_fraction_fraction_range);
6432   gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
6433       GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
6434 #endif
6435 }