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