2 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
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.
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.
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.
23 * @short_description: GValue implementations specific
26 * GValue implementations specific to GStreamer.
28 * Note that operations on the same #GValue from multiple threads may lead to
29 * undefined behaviour.
32 /* Suppress warnings for GValueAraray */
33 #define GLIB_DISABLE_DEPRECATION_WARNINGS
44 #include "gst_private.h"
45 #include "glib-compat-private.h"
47 #include <gobject/gvaluecollector.h>
52 * @dest: a #GValue for the result
53 * @value1: a #GValue operand
54 * @value2: a #GValue operand
56 * Used by gst_value_union() to perform unification for a specific #GValue
57 * type. Register a new implementation with gst_value_register_union_func().
59 * Returns: %TRUE if a union was successful
61 typedef gboolean (*GstValueUnionFunc) (GValue * dest,
62 const GValue * value1, const GValue * value2);
64 /* GstValueIntersectFunc:
65 * @dest: (out caller-allocates): a #GValue for the result
66 * @value1: a #GValue operand
67 * @value2: a #GValue operand
69 * Used by gst_value_intersect() to perform intersection for a specific #GValue
70 * type. If the intersection is non-empty, the result is
71 * placed in @dest and %TRUE is returned. If the intersection is
72 * empty, @dest is unmodified and %FALSE is returned.
73 * Register a new implementation with gst_value_register_intersect_func().
75 * Returns: %TRUE if the values can intersect
77 typedef gboolean (*GstValueIntersectFunc) (GValue * dest,
78 const GValue * value1, const GValue * value2);
80 /* GstValueSubtractFunc:
81 * @dest: (out caller-allocates): a #GValue for the result
82 * @minuend: a #GValue operand
83 * @subtrahend: a #GValue operand
85 * Used by gst_value_subtract() to perform subtraction for a specific #GValue
86 * type. Register a new implementation with gst_value_register_subtract_func().
88 * Returns: %TRUE if the subtraction is not empty
90 typedef gboolean (*GstValueSubtractFunc) (GValue * dest,
91 const GValue * minuend, const GValue * subtrahend);
93 static void gst_value_register_union_func (GType type1,
94 GType type2, GstValueUnionFunc func);
95 static void gst_value_register_intersect_func (GType type1,
96 GType type2, GstValueIntersectFunc func);
97 static void gst_value_register_subtract_func (GType minuend_type,
98 GType subtrahend_type, GstValueSubtractFunc func);
100 static gboolean _priv_gst_value_parse_list (gchar * s, gchar ** after,
101 GValue * value, GType type, GParamSpec * pspec);
102 static gboolean _priv_gst_value_parse_array (gchar * s, gchar ** after,
103 GValue * value, GType type, GParamSpec * pspec);
105 typedef struct _GstValueUnionInfo GstValueUnionInfo;
106 struct _GstValueUnionInfo
110 GstValueUnionFunc func;
113 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
114 struct _GstValueIntersectInfo
118 GstValueIntersectFunc func;
121 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
122 struct _GstValueSubtractInfo
126 GstValueSubtractFunc func;
129 struct _GstFlagSetClass
132 GType flags_type; /* Type of the GFlags this flagset carries (can be 0) */
135 typedef struct _GstFlagSetClass GstFlagSetClass;
137 typedef struct _GstValueAbbreviation GstValueAbbreviation;
139 struct _GstValueAbbreviation
141 const gchar *type_name;
145 /* Actual internal implementation of "GstValueList" and
147 typedef struct _GstValueList GstValueList;
151 /* These 2 fields must remain the same so that they match the public
152 * GArray structure (which was the former implementation) just in
153 * case someone calls `gst_value_peek_pointer` to access the
154 * array/list (such as in gststructure.c) */
162 #define FUNDAMENTAL_TYPE_ID_MAX \
163 (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
164 #define FUNDAMENTAL_TYPE_ID(type) \
165 ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
167 #define VALUE_LIST_ARRAY(v) ((GstValueList *) (v)->data[0].v_pointer)
168 #define VALUE_LIST_SIZE(v) (VALUE_LIST_ARRAY(v)->len)
169 #define VALUE_LIST_GET_VALUE(v, index) ((const GValue *) &(VALUE_LIST_ARRAY(v)->fields[index]))
170 #define VALUE_LIST_IS_USING_DYNAMIC_ARRAY(array) ((array)->fields != &(array)->arr[0])
172 static GArray *gst_value_table;
173 static GHashTable *gst_value_hash;
174 static GstValueTable *gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID_MAX + 1];
175 static GArray *gst_value_union_funcs;
176 static GArray *gst_value_intersect_funcs;
177 static GArray *gst_value_subtract_funcs;
179 /* Forward declarations */
180 static gchar *gst_value_serialize_fraction (const GValue * value);
181 static gint gst_value_compare_fraction (const GValue * value1,
182 const GValue * value2);
184 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
186 static gchar *gst_string_wrap (const gchar * s);
187 static gchar *gst_string_unwrap (const gchar * s);
189 static void gst_value_move (GValue * dest, GValue * src);
190 static void _gst_value_list_append_and_take_value (GValue * value,
191 GValue * append_value);
192 static void _gst_value_array_append_and_take_value (GValue * value,
193 GValue * append_value);
195 static inline GstValueTable *
196 gst_value_hash_lookup_type (GType type)
198 if (G_LIKELY (G_TYPE_IS_FUNDAMENTAL (type)))
199 return gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)];
201 return g_hash_table_lookup (gst_value_hash, (gpointer) type);
205 gst_value_hash_add_type (GType type, const GstValueTable * table)
207 if (G_TYPE_IS_FUNDAMENTAL (type))
208 gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)] = (gpointer) table;
210 g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table);
218 resize_value_list (GstValueList * vlist)
222 if (G_UNLIKELY (vlist->allocated > (G_MAXUINT / 2)))
223 g_error ("Growing GstValueList would result in overflow");
225 want_alloc = MAX (GST_ROUND_UP_8 (vlist->len + 1), vlist->allocated * 2);
227 if (VALUE_LIST_IS_USING_DYNAMIC_ARRAY (vlist)) {
228 vlist->fields = g_renew (GValue, vlist->fields, want_alloc);
230 vlist->fields = g_new0 (GValue, want_alloc);
231 memcpy (vlist->fields, &vlist->arr[0], vlist->len * sizeof (GValue));
232 GST_CAT_LOG (GST_CAT_PERFORMANCE, "Exceeding pre-allocated array");
234 vlist->allocated = want_alloc;
237 /* Replacement for g_array_append_val */
239 _gst_value_list_append_val (GstValueList * vlist, GValue * val)
241 /* resize if needed */
242 if (G_UNLIKELY (vlist->len == vlist->allocated))
243 resize_value_list (vlist);
245 /* Finally set value */
246 vlist->fields[vlist->len++] = *val;
249 /* Replacement for g_array_prepend_val */
251 _gst_value_list_prepend_val (GstValueList * vlist, GValue * val)
253 /* resize if needed */
254 if (G_UNLIKELY (vlist->len == vlist->allocated))
255 resize_value_list (vlist);
257 /* Shift everything */
258 memmove (&vlist->fields[1], &vlist->fields[0],
259 (vlist->len) * sizeof (GValue));
261 vlist->fields[0] = *val;
265 static GstValueList *
266 _gst_value_list_new (guint prealloc)
274 n_alloc = GST_ROUND_UP_8 (prealloc);
275 res = g_malloc0 (sizeof (GstValueList) + (n_alloc - 1) * sizeof (GValue));
278 res->allocated = n_alloc;
279 res->fields = &res->arr[0];
285 _gst_value_list_init (GValue * value, guint prealloc)
287 value->g_type = GST_TYPE_LIST;
288 memset (value->data, 0, sizeof (value->data));
289 value->data[0].v_pointer = _gst_value_list_new (prealloc);
293 * gst_value_list_init:
294 * @value: A zero-filled (uninitialized) #GValue structure
295 * @prealloc: The number of entries to pre-allocate in the list
297 * Initializes and pre-allocates a #GValue of type #GST_TYPE_LIST.
299 * Returns: (transfer none): The #GValue structure that has been passed in
305 gst_value_list_init (GValue * value, guint prealloc)
307 g_return_val_if_fail (value != NULL, NULL);
308 g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL);
310 _gst_value_list_init (value, prealloc);
316 _gst_value_array_init (GValue * value, guint prealloc)
318 value->g_type = GST_TYPE_ARRAY;
319 memset (value->data, 0, sizeof (value->data));
320 value->data[0].v_pointer = _gst_value_list_new (prealloc);
324 * gst_value_array_init:
325 * @value: A zero-filled (uninitialized) #GValue structure
326 * @prealloc: The number of entries to pre-allocate in the array
328 * Initializes and pre-allocates a #GValue of type #GST_TYPE_ARRAY.
330 * Returns: (transfer none): The #GValue structure that has been passed in
336 gst_value_array_init (GValue * value, guint prealloc)
338 g_return_val_if_fail (value != NULL, NULL);
339 g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL);
341 _gst_value_array_init (value, prealloc);
346 /* two helper functions to serialize/stringify any type of list
347 * regular lists are done with { }, arrays with < >
350 _priv_gst_value_serialize_any_list (const GValue * value, const gchar * begin,
351 const gchar * end, gboolean print_type)
354 GstValueList *vlist = value->data[0].v_pointer;
358 guint alen = vlist->len;
360 /* estimate minimum string length to minimise re-allocs in GString */
361 s = g_string_sized_new (2 + (6 * alen) + 2);
362 g_string_append (s, begin);
363 for (i = 0; i < alen; i++) {
364 v = &vlist->fields[i];
365 s_val = gst_value_serialize (v);
368 g_string_append_c (s, '(');
369 g_string_append (s, _priv_gst_value_gtype_to_abbr (G_VALUE_TYPE (v)));
370 g_string_append_c (s, ')');
372 g_string_append (s, s_val);
375 g_string_append_len (s, ", ", 2);
378 GST_WARNING ("Could not serialize list/array value of type '%s'",
379 G_VALUE_TYPE_NAME (v));
382 g_string_append (s, end);
383 return g_string_free (s, FALSE);
387 gst_value_transform_any_list_string (const GValue * src_value,
388 GValue * dest_value, const gchar * begin, const gchar * end)
397 array = src_value->data[0].v_pointer;
400 /* estimate minimum string length to minimise re-allocs in GString */
401 s = g_string_sized_new (2 + (10 * alen) + 2);
402 g_string_append (s, begin);
403 for (i = 0; i < alen; i++) {
404 list_value = &array->fields[i];
407 g_string_append_len (s, ", ", 2);
409 list_s = g_strdup_value_contents (list_value);
410 g_string_append (s, list_s);
413 g_string_append (s, end);
415 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
419 _gst_value_serialize_g_value_array (const GValue * value, const gchar * begin,
423 GValueArray *array = value->data[0].v_pointer;
430 alen = array->n_values;
432 /* estimate minimum string length to minimise re-allocs in GString */
433 s = g_string_sized_new (2 + (6 * alen) + 2);
434 g_string_append (s, begin);
435 for (i = 0; i < alen; i++) {
436 v = g_value_array_get_nth (array, i);
437 s_val = gst_value_serialize (v);
439 g_string_append (s, s_val);
442 g_string_append_len (s, ", ", 2);
445 GST_WARNING ("Could not serialize list/array value of type '%s'",
446 G_VALUE_TYPE_NAME (v));
449 g_string_append (s, end);
450 return g_string_free (s, FALSE);
454 _gst_value_transform_g_value_array_string (const GValue * src_value,
455 GValue * dest_value, const gchar * begin, const gchar * end)
464 array = src_value->data[0].v_pointer;
465 alen = array->n_values;
467 /* estimate minimum string length to minimise re-allocs in GString */
468 s = g_string_sized_new (2 + (10 * alen) + 2);
469 g_string_append (s, begin);
470 for (i = 0; i < alen; i++) {
471 list_value = g_value_array_get_nth (array, i);
474 g_string_append_len (s, ", ", 2);
476 list_s = g_strdup_value_contents (list_value);
477 g_string_append (s, list_s);
480 g_string_append (s, end);
482 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
486 * helper function to see if a type is fixed. Is used internally here and
487 * there. Do not export, since it doesn't work for types where the content
488 * decides the fixedness (e.g. GST_TYPE_ARRAY).
491 gst_type_is_fixed (GType type)
493 /* the basic int, string, double types */
494 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
497 /* our fundamental types that are certainly not fixed */
498 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
499 type == GST_TYPE_INT64_RANGE ||
500 type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE ||
501 type == GST_TYPE_STRUCTURE) {
504 /* other (boxed) types that are fixed */
505 if (type == GST_TYPE_BUFFER) {
509 if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
510 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
517 /* GValue functions usable for both regular lists and arrays */
519 gst_value_init_list_or_array (GValue * value)
521 value->data[0].v_pointer = _gst_value_list_new (0);
524 static GstValueList *
525 copy_gst_value_list (const GstValueList * src)
531 dest = _gst_value_list_new (len);
533 for (i = 0; i < len; i++) {
534 gst_value_init_and_copy (&dest->fields[i], &src->fields[i]);
541 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
543 dest_value->data[0].v_pointer =
544 copy_gst_value_list (VALUE_LIST_ARRAY (src_value));
548 gst_value_free_list_or_array (GValue * value)
551 GstValueList *src = VALUE_LIST_ARRAY (value);
554 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
555 for (i = 0; i < len; i++) {
556 g_value_unset (&src->fields[i]);
558 if (VALUE_LIST_IS_USING_DYNAMIC_ARRAY (src)) {
559 g_free (src->fields);
566 gst_value_list_or_array_peek_pointer (const GValue * value)
568 return value->data[0].v_pointer;
572 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
573 GTypeCValue * collect_values, guint collect_flags)
575 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
576 value->data[0].v_pointer = collect_values[0].v_pointer;
577 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
579 value->data[0].v_pointer =
580 copy_gst_value_list ((GstValueList *) collect_values[0].v_pointer);
586 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
587 GTypeCValue * collect_values, guint collect_flags)
589 GstValueList **dest = collect_values[0].v_pointer;
591 g_return_val_if_fail (dest != NULL,
592 g_strdup_printf ("value location for `%s' passed as NULL",
593 G_VALUE_TYPE_NAME (value)));
594 g_return_val_if_fail (value->data[0].v_pointer != NULL,
595 g_strdup_printf ("invalid value given for `%s'",
596 G_VALUE_TYPE_NAME (value)));
598 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
599 *dest = (GstValueList *) value->data[0].v_pointer;
601 *dest = copy_gst_value_list (VALUE_LIST_ARRAY (value));
607 gst_value_list_or_array_get_basic_type (const GValue * value, GType * type)
609 if (G_UNLIKELY (value == NULL))
612 if (GST_VALUE_HOLDS_LIST (value) || GST_VALUE_HOLDS_ARRAY (value)) {
613 if (VALUE_LIST_SIZE (value) == 0)
615 return gst_value_list_or_array_get_basic_type (VALUE_LIST_GET_VALUE (value,
619 *type = G_VALUE_TYPE (value);
624 #define IS_RANGE_COMPAT(type1,type2,t1,t2) \
625 (((t1) == (type1) && (t2) == (type2)) || ((t2) == (type1) && (t1) == (type2)))
628 gst_value_list_or_array_are_compatible (const GValue * value1,
629 const GValue * value2)
631 GType basic_type1, basic_type2;
633 /* empty or same type is OK */
634 if (!gst_value_list_or_array_get_basic_type (value1, &basic_type1) ||
635 !gst_value_list_or_array_get_basic_type (value2, &basic_type2) ||
636 basic_type1 == basic_type2)
639 /* ranges are distinct types for each bound type... */
640 if (IS_RANGE_COMPAT (G_TYPE_INT, GST_TYPE_INT_RANGE, basic_type1,
643 if (IS_RANGE_COMPAT (G_TYPE_INT64, GST_TYPE_INT64_RANGE, basic_type1,
646 if (IS_RANGE_COMPAT (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE, basic_type1,
649 if (IS_RANGE_COMPAT (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, basic_type1,
657 _gst_value_list_append_and_take_value (GValue * value, GValue * append_value)
659 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), append_value);
660 memset (append_value, 0, sizeof (GValue));
664 * gst_value_list_append_and_take_value:
665 * @value: a #GValue of type #GST_TYPE_LIST
666 * @append_value: (transfer full): the value to append
668 * Appends @append_value to the GstValueList in @value.
673 gst_value_list_append_and_take_value (GValue * value, GValue * append_value)
675 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
676 g_return_if_fail (G_IS_VALUE (append_value));
677 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
680 _gst_value_list_append_and_take_value (value, append_value);
684 * gst_value_list_append_value:
685 * @value: a #GValue of type #GST_TYPE_LIST
686 * @append_value: (transfer none): the value to append
688 * Appends @append_value to the GstValueList in @value.
691 gst_value_list_append_value (GValue * value, const GValue * append_value)
695 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
696 g_return_if_fail (G_IS_VALUE (append_value));
697 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
700 gst_value_init_and_copy (&val, append_value);
701 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), &val);
705 * gst_value_list_prepend_value:
706 * @value: a #GValue of type #GST_TYPE_LIST
707 * @prepend_value: the value to prepend
709 * Prepends @prepend_value to the GstValueList in @value.
712 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
716 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
717 g_return_if_fail (G_IS_VALUE (prepend_value));
718 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
721 gst_value_init_and_copy (&val, prepend_value);
722 _gst_value_list_prepend_val (VALUE_LIST_ARRAY (value), &val);
726 * gst_value_list_concat:
727 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
731 * Concatenates copies of @value1 and @value2 into a list. Values that are not
732 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
733 * @dest will be initialized to the type #GST_TYPE_LIST.
736 gst_value_list_concat (GValue * dest, const GValue * value1,
737 const GValue * value2)
739 guint i, value1_length, value2_length;
742 g_return_if_fail (dest != NULL);
743 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
744 g_return_if_fail (G_IS_VALUE (value1));
745 g_return_if_fail (G_IS_VALUE (value2));
746 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
749 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
751 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
753 _gst_value_list_init (dest, value1_length + value2_length);
754 vlist = VALUE_LIST_ARRAY (dest);
755 vlist->len = value1_length + value2_length;
757 if (GST_VALUE_HOLDS_LIST (value1)) {
758 for (i = 0; i < value1_length; i++) {
759 gst_value_init_and_copy (&vlist->fields[i],
760 VALUE_LIST_GET_VALUE (value1, i));
763 gst_value_init_and_copy (&vlist->fields[0], value1);
766 if (GST_VALUE_HOLDS_LIST (value2)) {
767 for (i = 0; i < value2_length; i++) {
768 gst_value_init_and_copy (&vlist->fields[i + value1_length],
769 VALUE_LIST_GET_VALUE (value2, i));
772 gst_value_init_and_copy (&vlist->fields[value1_length], value2);
776 /* same as gst_value_list_concat() but takes ownership of GValues */
778 gst_value_list_concat_and_take_values (GValue * dest, GValue * val1,
781 guint i, val1_length, val2_length;
782 gboolean val1_is_list;
783 gboolean val2_is_list;
786 g_assert (dest != NULL);
787 g_assert (G_VALUE_TYPE (dest) == 0);
788 g_assert (G_IS_VALUE (val1));
789 g_assert (G_IS_VALUE (val2));
790 g_assert (gst_value_list_or_array_are_compatible (val1, val2));
792 val1_is_list = GST_VALUE_HOLDS_LIST (val1);
793 val1_length = (val1_is_list ? VALUE_LIST_SIZE (val1) : 1);
795 val2_is_list = GST_VALUE_HOLDS_LIST (val2);
796 val2_length = (val2_is_list ? VALUE_LIST_SIZE (val2) : 1);
798 /* Overidding the default initialization to have a list of the target size */
799 _gst_value_list_init (dest, val1_length + val2_length);
800 vlist = VALUE_LIST_ARRAY (dest);
801 vlist->len = val1_length + val2_length;
804 for (i = 0; i < val1_length; i++) {
805 vlist->fields[i] = *VALUE_LIST_GET_VALUE (val1, i);
807 VALUE_LIST_ARRAY (val1)->len = 0;
808 g_value_unset (val1);
810 vlist->fields[0] = *val1;
811 G_VALUE_TYPE (val1) = G_TYPE_INVALID;
815 for (i = 0; i < val2_length; i++) {
816 const GValue *v2 = VALUE_LIST_GET_VALUE (val2, i);
817 vlist->fields[i + val1_length] = *v2;
820 VALUE_LIST_ARRAY (val2)->len = 0;
821 g_value_unset (val2);
823 vlist->fields[val1_length] = *val2;
824 G_VALUE_TYPE (val2) = G_TYPE_INVALID;
829 * gst_value_list_merge:
830 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
834 * Merges copies of @value1 and @value2. Values that are not
835 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
837 * The result will be put into @dest and will either be a list that will not
838 * contain any duplicates, or a non-list type (if @value1 and @value2
842 gst_value_list_merge (GValue * dest, const GValue * value1,
843 const GValue * value2)
845 guint i, j, k, value1_length, value2_length, skipped;
850 g_return_if_fail (dest != NULL);
851 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
852 g_return_if_fail (G_IS_VALUE (value1));
853 g_return_if_fail (G_IS_VALUE (value2));
854 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
857 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
859 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
861 _gst_value_list_init (dest, value1_length + value2_length);
862 vlist = VALUE_LIST_ARRAY (dest);
863 vlist->len = value1_length + value2_length;
865 if (GST_VALUE_HOLDS_LIST (value1)) {
866 for (i = 0; i < value1_length; i++) {
867 gst_value_init_and_copy (&vlist->fields[i], VALUE_LIST_GET_VALUE (value1,
871 gst_value_init_and_copy (&vlist->fields[0], value1);
876 if (GST_VALUE_HOLDS_LIST (value2)) {
877 for (i = 0; i < value2_length; i++) {
879 src = VALUE_LIST_GET_VALUE (value2, i);
880 for (k = 0; k < value1_length; k++) {
881 if (gst_value_compare (&vlist->fields[k], src) == GST_VALUE_EQUAL) {
888 gst_value_init_and_copy (&vlist->fields[j], src);
894 for (k = 0; k < value1_length; k++) {
895 if (gst_value_compare (&vlist->fields[k], value2) == GST_VALUE_EQUAL) {
902 gst_value_init_and_copy (&vlist->fields[j], value2);
906 guint new_size = value1_length + (value2_length - skipped);
910 vlist->len = new_size;
914 /* size is 1, take single value in list and make it new dest */
915 single_dest = vlist->fields[0];
917 /* clean up old value allocations: must set array size to 0, because
918 * allocated values are not inited meaning g_value_unset() will not
921 g_value_unset (dest);
923 /* the single value is our new result */
930 * gst_value_list_get_size:
931 * @value: a #GValue of type #GST_TYPE_LIST
933 * Gets the number of values contained in @value.
935 * Returns: the number of values
938 gst_value_list_get_size (const GValue * value)
940 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
942 return VALUE_LIST_SIZE (value);
946 * gst_value_list_get_value:
947 * @value: a #GValue of type #GST_TYPE_LIST
948 * @index: index of value to get from the list
950 * Gets the value that is a member of the list contained in @value and
951 * has the index @index.
953 * Returns: (transfer none): the value at the given index
956 gst_value_list_get_value (const GValue * value, guint index)
958 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
959 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
961 return VALUE_LIST_GET_VALUE (value, index);
965 * gst_value_array_append_value:
966 * @value: a #GValue of type #GST_TYPE_ARRAY
967 * @append_value: the value to append
969 * Appends @append_value to the GstValueArray in @value.
972 gst_value_array_append_value (GValue * value, const GValue * append_value)
976 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
977 g_return_if_fail (G_IS_VALUE (append_value));
978 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
981 gst_value_init_and_copy (&val, append_value);
982 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), &val);
986 _gst_value_array_append_and_take_value (GValue * value, GValue * append_value)
988 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), append_value);
989 memset (append_value, 0, sizeof (GValue));
993 * gst_value_array_append_and_take_value:
994 * @value: a #GValue of type #GST_TYPE_ARRAY
995 * @append_value: (transfer full): the value to append
997 * Appends @append_value to the GstValueArray in @value.
1002 gst_value_array_append_and_take_value (GValue * value, GValue * append_value)
1004 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
1005 g_return_if_fail (G_IS_VALUE (append_value));
1006 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
1009 _gst_value_array_append_and_take_value (value, append_value);
1013 * gst_value_array_prepend_value:
1014 * @value: a #GValue of type #GST_TYPE_ARRAY
1015 * @prepend_value: the value to prepend
1017 * Prepends @prepend_value to the GstValueArray in @value.
1020 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
1022 GValue val = { 0, };
1024 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
1025 g_return_if_fail (G_IS_VALUE (prepend_value));
1026 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
1029 gst_value_init_and_copy (&val, prepend_value);
1030 _gst_value_list_prepend_val (VALUE_LIST_ARRAY (value), &val);
1034 * gst_value_array_get_size:
1035 * @value: a #GValue of type #GST_TYPE_ARRAY
1037 * Gets the number of values contained in @value.
1039 * Returns: the number of values
1042 gst_value_array_get_size (const GValue * value)
1044 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
1046 return VALUE_LIST_SIZE (value);
1050 * gst_value_array_get_value:
1051 * @value: a #GValue of type #GST_TYPE_ARRAY
1052 * @index: index of value to get from the array
1054 * Gets the value that is a member of the array contained in @value and
1055 * has the index @index.
1057 * Returns: (transfer none): the value at the given index
1060 gst_value_array_get_value (const GValue * value, guint index)
1062 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
1063 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
1065 return VALUE_LIST_GET_VALUE (value, index);
1069 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
1071 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
1075 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
1077 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
1081 gst_value_transform_g_value_array_string (const GValue * src_value,
1082 GValue * dest_value)
1084 _gst_value_transform_g_value_array_string (src_value, dest_value, "< ", " >");
1088 gst_value_transform_g_value_array_any_list (const GValue * src_value,
1089 GValue * dest_value)
1091 const GValueArray *varray;
1092 GstValueList *vlist;
1095 varray = g_value_get_boxed (src_value);
1097 /* GLib will unset the value, memset to 0 the data instead of doing a proper
1098 * reset. That's why we need to allocate the array here */
1099 vlist = dest_value->data[0].v_pointer =
1100 _gst_value_list_new (varray->n_values);
1102 for (i = 0; i < varray->n_values; i++) {
1103 GValue val = G_VALUE_INIT;
1104 gst_value_init_and_copy (&val, &varray->values[i]);
1105 _gst_value_list_append_val (vlist, &val);
1110 gst_value_transform_any_list_g_value_array (const GValue * src_value,
1111 GValue * dest_value)
1113 GValueArray *varray;
1114 GstValueList *vlist;
1117 vlist = VALUE_LIST_ARRAY (src_value);
1118 varray = g_value_array_new (vlist->len);
1120 for (i = 0; i < vlist->len; i++)
1121 g_value_array_append (varray, &vlist->fields[i]);
1123 g_value_take_boxed (dest_value, varray);
1126 /* Do an unordered compare of the contents of a list */
1128 gst_value_compare_value_list (const GValue * value1, const GValue * value2)
1131 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
1132 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
1135 gint len, to_remove;
1137 GstValueCompareFunc compare;
1139 /* get length and do initial length check. */
1141 if (len != vlist2->len)
1142 return GST_VALUE_UNORDERED;
1144 /* Empty lists are equal */
1146 return GST_VALUE_EQUAL;
1148 /* We know lists are not empty. do sanity check on first values */
1149 if (G_VALUE_TYPE (&vlist1->fields[0]) != G_VALUE_TYPE (&vlist2->fields[0]))
1150 return GST_VALUE_UNORDERED;
1152 /* Get the compare function */
1153 if (!(compare = gst_value_get_compare_func (&vlist1->fields[0])))
1154 return GST_VALUE_UNORDERED;
1156 /* place to mark removed value indices of array2 */
1157 removed = g_newa (guint8, len);
1158 memset (removed, 0, len);
1161 /* loop over array1, all items should be in array2. When we find an
1162 * item in array2, remove it from array2 by marking it as removed */
1163 for (i = 0; i < len; i++) {
1164 v1 = &vlist1->fields[i];
1166 for (j = 0; j < len; j++) {
1167 /* item is removed, we can skip it */
1170 v2 = &vlist2->fields[j];
1171 /* Note: compare function can be called directly since we know the types
1173 if (compare (v1, v2) == GST_VALUE_EQUAL) {
1174 /* mark item as removed now that we found it in array2 and
1175 * decrement the number of remaining items in array2. */
1181 /* item in array1 and not in array2, UNORDERED */
1183 return GST_VALUE_UNORDERED;
1185 /* if not all items were removed, array2 contained something not in array1 */
1187 return GST_VALUE_UNORDERED;
1189 /* arrays are equal */
1190 return GST_VALUE_EQUAL;
1193 /* Perform an ordered comparison of the contents of an array */
1195 gst_value_compare_value_array (const GValue * value1, const GValue * value2)
1198 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
1199 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
1200 guint len = vlist1->len;
1204 if (len != vlist2->len)
1205 return GST_VALUE_UNORDERED;
1207 for (i = 0; i < len; i++) {
1208 v1 = &vlist1->fields[i];
1209 v2 = &vlist2->fields[i];
1210 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
1211 return GST_VALUE_UNORDERED;
1214 return GST_VALUE_EQUAL;
1218 gst_value_compare_g_value_array (const GValue * value1, const GValue * value2)
1221 GValueArray *array1 = value1->data[0].v_pointer;
1222 GValueArray *array2 = value2->data[0].v_pointer;
1223 guint len = array1 ? array1->n_values : 0;
1227 if (len != (array2 ? array2->n_values : 0))
1228 return GST_VALUE_UNORDERED;
1230 for (i = 0; i < len; i++) {
1231 v1 = g_value_array_get_nth (array1, i);
1232 v2 = g_value_array_get_nth (array2, i);
1233 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
1234 return GST_VALUE_UNORDERED;
1237 return GST_VALUE_EQUAL;
1241 gst_value_serialize_value_list (const GValue * value)
1243 return _priv_gst_value_serialize_any_list (value, "{ ", " }", TRUE);
1247 gst_value_deserialize_value_list (GValue * dest, const gchar * s,
1250 gchar *s2 = (gchar *) s;
1251 return _priv_gst_value_parse_list (s2, &s2, dest, G_TYPE_INVALID, pspec);
1255 gst_value_serialize_value_array (const GValue * value)
1257 return _priv_gst_value_serialize_any_list (value, "< ", " >", TRUE);
1261 gst_value_deserialize_value_array (GValue * dest, const gchar * s,
1264 gchar *s2 = (gchar *) s;
1265 return _priv_gst_value_parse_array (s2, &s2, dest, G_TYPE_INVALID, pspec);
1269 gst_value_serialize_g_value_array (const GValue * value)
1271 return _gst_value_serialize_g_value_array (value, "< ", " >");
1275 gst_value_deserialize_g_value_array (GValue * dest, const gchar * s)
1277 g_warning ("gst_value_deserialize_g_value_array: unimplemented");
1284 * Values in the range are defined as any value greater or equal
1285 * to min*step, AND lesser or equal to max*step.
1286 * For step == 1, this falls back to the traditional range semantics.
1288 * data[0] = (min << 32) | (max)
1293 #define INT_RANGE_MIN(v) ((gint) (((v)->data[0].v_uint64) >> 32))
1294 #define INT_RANGE_MAX(v) ((gint) (((v)->data[0].v_uint64) & 0xffffffff))
1295 #define INT_RANGE_STEP(v) ((v)->data[1].v_int)
1298 gst_value_init_int_range (GValue * value)
1300 G_STATIC_ASSERT (sizeof (gint) <= 2 * sizeof (guint64));
1302 value->data[0].v_uint64 = 0;
1303 value->data[1].v_int = 1;
1307 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
1309 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
1310 dest_value->data[1].v_int = src_value->data[1].v_int;
1314 gst_value_collect_int_range (GValue * value, guint n_collect_values,
1315 GTypeCValue * collect_values, guint collect_flags)
1317 g_return_val_if_fail (n_collect_values == 2,
1318 g_strdup_printf ("not enough value locations for `%s' passed",
1319 G_VALUE_TYPE_NAME (value)));
1320 g_return_val_if_fail (collect_values[0].v_int < collect_values[1].v_int,
1321 g_strdup_printf ("range start is not smaller than end for `%s'",
1322 G_VALUE_TYPE_NAME (value)));
1324 gst_value_set_int_range_step (value, collect_values[0].v_int,
1325 collect_values[1].v_int, 1);
1331 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
1332 GTypeCValue * collect_values, guint collect_flags)
1334 guint32 *int_range_start = collect_values[0].v_pointer;
1335 guint32 *int_range_end = collect_values[1].v_pointer;
1337 g_return_val_if_fail (int_range_start != NULL,
1338 g_strdup_printf ("start value location for `%s' passed as NULL",
1339 G_VALUE_TYPE_NAME (value)));
1340 g_return_val_if_fail (int_range_end != NULL,
1341 g_strdup_printf ("end value location for `%s' passed as NULL",
1342 G_VALUE_TYPE_NAME (value)));
1344 *int_range_start = INT_RANGE_MIN (value);
1345 *int_range_end = INT_RANGE_MAX (value);
1351 * gst_value_set_int_range_step:
1352 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1353 * @start: the start of the range
1354 * @end: the end of the range
1355 * @step: the step of the range
1357 * Sets @value to the range specified by @start, @end and @step.
1360 gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
1362 guint64 sstart, sstop;
1364 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
1365 g_return_if_fail (start < end);
1366 g_return_if_fail (step > 0);
1367 g_return_if_fail (start % step == 0);
1368 g_return_if_fail (end % step == 0);
1370 sstart = (guint) (start / step);
1371 sstop = (guint) (end / step);
1372 value->data[0].v_uint64 = (sstart << 32) | sstop;
1373 value->data[1].v_int = step;
1377 * gst_value_set_int_range:
1378 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1379 * @start: the start of the range
1380 * @end: the end of the range
1382 * Sets @value to the range specified by @start and @end.
1385 gst_value_set_int_range (GValue * value, gint start, gint end)
1387 gst_value_set_int_range_step (value, start, end, 1);
1391 * gst_value_get_int_range_min:
1392 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1394 * Gets the minimum of the range specified by @value.
1396 * Returns: the minimum of the range
1399 gst_value_get_int_range_min (const GValue * value)
1401 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1403 return INT_RANGE_MIN (value) * INT_RANGE_STEP (value);
1407 * gst_value_get_int_range_max:
1408 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1410 * Gets the maximum of the range specified by @value.
1412 * Returns: the maximum of the range
1415 gst_value_get_int_range_max (const GValue * value)
1417 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1419 return INT_RANGE_MAX (value) * INT_RANGE_STEP (value);
1423 * gst_value_get_int_range_step:
1424 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1426 * Gets the step of the range specified by @value.
1428 * Returns: the step of the range
1431 gst_value_get_int_range_step (const GValue * value)
1433 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1435 return INT_RANGE_STEP (value);
1439 gst_value_transform_int_range_string (const GValue * src_value,
1440 GValue * dest_value)
1442 if (INT_RANGE_STEP (src_value) == 1)
1443 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
1444 INT_RANGE_MIN (src_value), INT_RANGE_MAX (src_value));
1446 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d,%d]",
1447 INT_RANGE_MIN (src_value) * INT_RANGE_STEP (src_value),
1448 INT_RANGE_MAX (src_value) * INT_RANGE_STEP (src_value),
1449 INT_RANGE_STEP (src_value));
1453 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
1456 /* Compare the ranges. (Kept for clarity for the below comparision) */
1457 if (INT_RANGE_MIN (value1) != INT_RANGE_MIN (value2) ||
1458 INT_RANGE_MAX (value1) != INT_RANGE_MAX (value2))
1459 return GST_VALUE_UNORDERED;
1461 /* The MIN and MAX of the range are actually stored packed into one 64bit
1462 * value. We can therefore compare them directly */
1463 if (value1->data[0].v_uint64 != value2->data[0].v_uint64)
1464 return GST_VALUE_UNORDERED;
1467 /* The extents are equal */
1468 /* If there is only one value (min == max), we ignore the step for
1470 if (INT_RANGE_MIN (value1) == INT_RANGE_MAX (value1))
1471 return GST_VALUE_EQUAL;
1473 /* Else the ranges are only equal if their step is also equal */
1474 if (INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2))
1475 return GST_VALUE_EQUAL;
1476 return GST_VALUE_UNORDERED;
1480 gst_value_serialize_int_range (const GValue * value)
1482 if (INT_RANGE_STEP (value) == 1)
1483 return g_strdup_printf ("[ %d, %d ]", INT_RANGE_MIN (value),
1484 INT_RANGE_MAX (value));
1486 return g_strdup_printf ("[ %d, %d, %d ]",
1487 INT_RANGE_MIN (value) * INT_RANGE_STEP (value),
1488 INT_RANGE_MAX (value) * INT_RANGE_STEP (value), INT_RANGE_STEP (value));
1492 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1494 g_warning ("unimplemented");
1501 * Values in the range are defined as any value greater or equal
1502 * to min*step, AND lesser or equal to max*step.
1503 * For step == 1, this falls back to the traditional range semantics.
1506 #define INT64_RANGE_MIN(v) (((gint64 *)((v)->data[0].v_pointer))[0])
1507 #define INT64_RANGE_MAX(v) (((gint64 *)((v)->data[0].v_pointer))[1])
1508 #define INT64_RANGE_STEP(v) (((gint64 *)((v)->data[0].v_pointer))[2])
1511 gst_value_init_int64_range (GValue * value)
1513 gint64 *vals = g_slice_alloc0 (3 * sizeof (gint64));
1514 value->data[0].v_pointer = vals;
1515 INT64_RANGE_MIN (value) = 0;
1516 INT64_RANGE_MAX (value) = 0;
1517 INT64_RANGE_STEP (value) = 1;
1521 gst_value_free_int64_range (GValue * value)
1523 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1524 g_slice_free1 (3 * sizeof (gint64), value->data[0].v_pointer);
1525 value->data[0].v_pointer = NULL;
1529 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1531 gint64 *vals = (gint64 *) dest_value->data[0].v_pointer;
1532 gint64 *src_vals = (gint64 *) src_value->data[0].v_pointer;
1535 gst_value_init_int64_range (dest_value);
1538 if (src_vals != NULL) {
1539 INT64_RANGE_MIN (dest_value) = INT64_RANGE_MIN (src_value);
1540 INT64_RANGE_MAX (dest_value) = INT64_RANGE_MAX (src_value);
1541 INT64_RANGE_STEP (dest_value) = INT64_RANGE_STEP (src_value);
1546 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1547 GTypeCValue * collect_values, guint collect_flags)
1549 gint64 *vals = value->data[0].v_pointer;
1551 g_return_val_if_fail (n_collect_values == 2,
1552 g_strdup_printf ("not enough value locations for `%s' passed",
1553 G_VALUE_TYPE_NAME (value)));
1555 g_return_val_if_fail (collect_values[0].v_int64 < collect_values[1].v_int64,
1556 g_strdup_printf ("range start is not smaller than end for `%s'",
1557 G_VALUE_TYPE_NAME (value)));
1560 gst_value_init_int64_range (value);
1563 gst_value_set_int64_range_step (value, collect_values[0].v_int64,
1564 collect_values[1].v_int64, 1);
1570 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1571 GTypeCValue * collect_values, guint collect_flags)
1573 guint64 *int_range_start = collect_values[0].v_pointer;
1574 guint64 *int_range_end = collect_values[1].v_pointer;
1575 guint64 *int_range_step = collect_values[2].v_pointer;
1576 gint64 *vals = (gint64 *) value->data[0].v_pointer;
1578 g_return_val_if_fail (int_range_start != NULL,
1579 g_strdup_printf ("start value location for `%s' passed as NULL",
1580 G_VALUE_TYPE_NAME (value)));
1581 g_return_val_if_fail (int_range_end != NULL,
1582 g_strdup_printf ("end value location for `%s' passed as NULL",
1583 G_VALUE_TYPE_NAME (value)));
1584 g_return_val_if_fail (int_range_step != NULL,
1585 g_strdup_printf ("step value location for `%s' passed as NULL",
1586 G_VALUE_TYPE_NAME (value)));
1588 g_return_val_if_fail (vals != NULL,
1589 g_strdup_printf ("Uninitialised `%s' passed", G_VALUE_TYPE_NAME (value)));
1591 *int_range_start = INT64_RANGE_MIN (value);
1592 *int_range_end = INT64_RANGE_MAX (value);
1593 *int_range_step = INT64_RANGE_STEP (value);
1599 * gst_value_set_int64_range_step:
1600 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1601 * @start: the start of the range
1602 * @end: the end of the range
1603 * @step: the step of the range
1605 * Sets @value to the range specified by @start, @end and @step.
1608 gst_value_set_int64_range_step (GValue * value, gint64 start, gint64 end,
1611 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1612 g_return_if_fail (start < end);
1613 g_return_if_fail (step > 0);
1614 g_return_if_fail (start % step == 0);
1615 g_return_if_fail (end % step == 0);
1617 INT64_RANGE_MIN (value) = start / step;
1618 INT64_RANGE_MAX (value) = end / step;
1619 INT64_RANGE_STEP (value) = step;
1623 * gst_value_set_int64_range:
1624 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1625 * @start: the start of the range
1626 * @end: the end of the range
1628 * Sets @value to the range specified by @start and @end.
1631 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1633 gst_value_set_int64_range_step (value, start, end, 1);
1637 * gst_value_get_int64_range_min:
1638 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1640 * Gets the minimum of the range specified by @value.
1642 * Returns: the minimum of the range
1645 gst_value_get_int64_range_min (const GValue * value)
1647 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1649 return INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value);
1653 * gst_value_get_int64_range_max:
1654 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1656 * Gets the maximum of the range specified by @value.
1658 * Returns: the maximum of the range
1661 gst_value_get_int64_range_max (const GValue * value)
1663 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1665 return INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value);
1669 * gst_value_get_int64_range_step:
1670 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1672 * Gets the step of the range specified by @value.
1674 * Returns: the step of the range
1677 gst_value_get_int64_range_step (const GValue * value)
1679 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1681 return INT64_RANGE_STEP (value);
1685 gst_value_transform_int64_range_string (const GValue * src_value,
1686 GValue * dest_value)
1688 if (INT64_RANGE_STEP (src_value) == 1)
1689 dest_value->data[0].v_pointer =
1690 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1691 INT64_RANGE_MIN (src_value), INT64_RANGE_MAX (src_value));
1693 dest_value->data[0].v_pointer =
1694 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT
1695 ",%" G_GINT64_FORMAT "]",
1696 INT64_RANGE_MIN (src_value) * INT64_RANGE_STEP (src_value),
1697 INT64_RANGE_MAX (src_value) * INT64_RANGE_STEP (src_value),
1698 INT64_RANGE_STEP (src_value));
1702 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1704 /* Compare the ranges. */
1705 if (INT64_RANGE_MIN (value1) != INT64_RANGE_MIN (value2) ||
1706 INT64_RANGE_MAX (value1) != INT64_RANGE_MAX (value2))
1707 return GST_VALUE_UNORDERED;
1709 /* The extents are equal */
1710 /* If there is only one value (min == max), we ignore the step for
1712 if (INT64_RANGE_MIN (value1) == INT64_RANGE_MAX (value1))
1713 return GST_VALUE_EQUAL;
1715 /* Else the ranges are only equal if their step is also equal */
1716 if (INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2))
1717 return GST_VALUE_EQUAL;
1718 return GST_VALUE_UNORDERED;
1722 gst_value_serialize_int64_range (const GValue * value)
1724 if (INT64_RANGE_STEP (value) == 1)
1725 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1726 INT64_RANGE_MIN (value), INT64_RANGE_MAX (value));
1728 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT ", %"
1729 G_GINT64_FORMAT " ]",
1730 INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value),
1731 INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value),
1732 INT64_RANGE_STEP (value));
1736 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1738 g_warning ("unimplemented");
1747 gst_value_init_double_range (GValue * value)
1749 value->data[0].v_double = 0;
1750 value->data[1].v_double = 0;
1754 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1756 dest_value->data[0].v_double = src_value->data[0].v_double;
1757 dest_value->data[1].v_double = src_value->data[1].v_double;
1761 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1762 GTypeCValue * collect_values, guint collect_flags)
1764 g_return_val_if_fail (n_collect_values == 2,
1765 g_strdup_printf ("not enough value locations for `%s' passed",
1766 G_VALUE_TYPE_NAME (value)));
1767 g_return_val_if_fail (collect_values[0].v_double < collect_values[1].v_double,
1768 g_strdup_printf ("range start is not smaller than end for `%s'",
1769 G_VALUE_TYPE_NAME (value)));
1771 value->data[0].v_double = collect_values[0].v_double;
1772 value->data[1].v_double = collect_values[1].v_double;
1778 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1779 GTypeCValue * collect_values, guint collect_flags)
1781 gdouble *double_range_start = collect_values[0].v_pointer;
1782 gdouble *double_range_end = collect_values[1].v_pointer;
1784 g_return_val_if_fail (double_range_start != NULL,
1785 g_strdup_printf ("start value location for `%s' passed as NULL",
1786 G_VALUE_TYPE_NAME (value)));
1787 g_return_val_if_fail (double_range_end != NULL,
1788 g_strdup_printf ("end value location for `%s' passed as NULL",
1789 G_VALUE_TYPE_NAME (value)));
1791 *double_range_start = value->data[0].v_double;
1792 *double_range_end = value->data[1].v_double;
1798 * gst_value_set_double_range:
1799 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1800 * @start: the start of the range
1801 * @end: the end of the range
1803 * Sets @value to the range specified by @start and @end.
1806 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1808 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1809 g_return_if_fail (start < end);
1811 value->data[0].v_double = start;
1812 value->data[1].v_double = end;
1816 * gst_value_get_double_range_min:
1817 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1819 * Gets the minimum of the range specified by @value.
1821 * Returns: the minimum of the range
1824 gst_value_get_double_range_min (const GValue * value)
1826 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1828 return value->data[0].v_double;
1832 * gst_value_get_double_range_max:
1833 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1835 * Gets the maximum of the range specified by @value.
1837 * Returns: the maximum of the range
1840 gst_value_get_double_range_max (const GValue * value)
1842 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1844 return value->data[1].v_double;
1848 gst_value_transform_double_range_string (const GValue * src_value,
1849 GValue * dest_value)
1851 gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1853 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1854 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1855 src_value->data[0].v_double),
1856 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1857 src_value->data[1].v_double));
1861 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1863 if (value2->data[0].v_double == value1->data[0].v_double &&
1864 value2->data[1].v_double == value1->data[1].v_double)
1865 return GST_VALUE_EQUAL;
1866 return GST_VALUE_UNORDERED;
1870 gst_value_serialize_double_range (const GValue * value)
1872 gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1873 gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1875 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1876 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1877 return g_strdup_printf ("[ %s, %s ]", d1, d2);
1881 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1883 g_warning ("unimplemented");
1892 gst_value_init_fraction_range (GValue * value)
1897 ftype = GST_TYPE_FRACTION;
1899 value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1900 g_value_init (&vals[0], ftype);
1901 g_value_init (&vals[1], ftype);
1905 gst_value_free_fraction_range (GValue * value)
1907 GValue *vals = (GValue *) value->data[0].v_pointer;
1910 /* we know the two values contain fractions without internal allocs */
1911 /* g_value_unset (&vals[0]); */
1912 /* g_value_unset (&vals[1]); */
1913 g_slice_free1 (2 * sizeof (GValue), vals);
1914 value->data[0].v_pointer = NULL;
1919 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1921 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1922 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1925 gst_value_init_fraction_range (dest_value);
1926 vals = dest_value->data[0].v_pointer;
1928 if (src_vals != NULL) {
1929 g_value_copy (&src_vals[0], &vals[0]);
1930 g_value_copy (&src_vals[1], &vals[1]);
1935 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1936 GTypeCValue * collect_values, guint collect_flags)
1938 GValue *vals = (GValue *) value->data[0].v_pointer;
1940 g_return_val_if_fail (n_collect_values == 4,
1941 g_strdup_printf ("not enough value locations for `%s' passed",
1942 G_VALUE_TYPE_NAME (value)));
1943 g_return_val_if_fail (collect_values[1].v_int != 0,
1944 g_strdup_printf ("passed '0' as first denominator for `%s'",
1945 G_VALUE_TYPE_NAME (value)));
1946 g_return_val_if_fail (collect_values[3].v_int != 0,
1947 g_strdup_printf ("passed '0' as second denominator for `%s'",
1948 G_VALUE_TYPE_NAME (value)));
1949 g_return_val_if_fail (gst_util_fraction_compare (collect_values[0].v_int,
1950 collect_values[1].v_int, collect_values[2].v_int,
1951 collect_values[3].v_int) < 0,
1952 g_strdup_printf ("range start is not smaller than end for `%s'",
1953 G_VALUE_TYPE_NAME (value)));
1956 gst_value_init_fraction_range (value);
1957 vals = value->data[0].v_pointer;
1960 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1961 collect_values[1].v_int);
1962 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1963 collect_values[3].v_int);
1969 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1970 GTypeCValue * collect_values, guint collect_flags)
1973 gint *dest_values[4];
1974 GValue *vals = (GValue *) value->data[0].v_pointer;
1976 g_return_val_if_fail (n_collect_values == 4,
1977 g_strdup_printf ("not enough value locations for `%s' passed",
1978 G_VALUE_TYPE_NAME (value)));
1979 g_return_val_if_fail (vals != NULL,
1980 g_strdup_printf ("Uninitialised `%s' passed", G_VALUE_TYPE_NAME (value)));
1982 for (i = 0; i < 4; i++) {
1983 g_return_val_if_fail (collect_values[i].v_pointer != NULL,
1984 g_strdup_printf ("value location for `%s' passed as NULL",
1985 G_VALUE_TYPE_NAME (value)));
1987 dest_values[i] = collect_values[i].v_pointer;
1990 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1991 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1992 dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1993 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1998 * gst_value_set_fraction_range:
1999 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2000 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
2001 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
2003 * Sets @value to the range specified by @start and @end.
2006 gst_value_set_fraction_range (GValue * value, const GValue * start,
2011 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
2012 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
2013 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
2014 g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
2015 start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
2017 vals = (GValue *) value->data[0].v_pointer;
2019 gst_value_init_fraction_range (value);
2020 vals = value->data[0].v_pointer;
2022 g_value_copy (start, &vals[0]);
2023 g_value_copy (end, &vals[1]);
2027 * gst_value_set_fraction_range_full:
2028 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2029 * @numerator_start: the numerator start of the range
2030 * @denominator_start: the denominator start of the range
2031 * @numerator_end: the numerator end of the range
2032 * @denominator_end: the denominator end of the range
2034 * Sets @value to the range specified by @numerator_start/@denominator_start
2035 * and @numerator_end/@denominator_end.
2038 gst_value_set_fraction_range_full (GValue * value,
2039 gint numerator_start, gint denominator_start,
2040 gint numerator_end, gint denominator_end)
2042 GValue start = { 0 };
2045 g_return_if_fail (value != NULL);
2046 g_return_if_fail (denominator_start != 0);
2047 g_return_if_fail (denominator_end != 0);
2048 g_return_if_fail (gst_util_fraction_compare (numerator_start,
2049 denominator_start, numerator_end, denominator_end) < 0);
2051 g_value_init (&start, GST_TYPE_FRACTION);
2052 g_value_init (&end, GST_TYPE_FRACTION);
2054 gst_value_set_fraction (&start, numerator_start, denominator_start);
2055 gst_value_set_fraction (&end, numerator_end, denominator_end);
2056 gst_value_set_fraction_range (value, &start, &end);
2058 /* we know the two values contain fractions without internal allocs */
2059 /* g_value_unset (&start); */
2060 /* g_value_unset (&end); */
2063 /* FIXME 2.0: Don't leak the internal representation of fraction
2064 * ranges but instead return the numerator and denominator
2066 * This would allow to store fraction ranges as
2067 * data[0] = (min_n << 32) | (min_d)
2068 * data[1] = (max_n << 32) | (max_d)
2069 * without requiring an additional allocation for each value.
2073 * gst_value_get_fraction_range_min:
2074 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2076 * Gets the minimum of the range specified by @value.
2078 * Returns: (nullable): the minimum of the range
2081 gst_value_get_fraction_range_min (const GValue * value)
2085 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
2087 vals = (GValue *) value->data[0].v_pointer;
2096 * gst_value_get_fraction_range_max:
2097 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2099 * Gets the maximum of the range specified by @value.
2101 * Returns: (nullable): the maximum of the range
2104 gst_value_get_fraction_range_max (const GValue * value)
2108 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
2110 vals = (GValue *) value->data[0].v_pointer;
2119 gst_value_serialize_fraction_range (const GValue * value)
2121 GValue *vals = (GValue *) value->data[0].v_pointer;
2125 retval = g_strdup ("[ 0/1, 0/1 ]");
2129 start = gst_value_serialize_fraction (&vals[0]);
2130 end = gst_value_serialize_fraction (&vals[1]);
2132 retval = g_strdup_printf ("[ %s, %s ]", start, end);
2141 gst_value_transform_fraction_range_string (const GValue * src_value,
2142 GValue * dest_value)
2144 dest_value->data[0].v_pointer =
2145 gst_value_serialize_fraction_range (src_value);
2149 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
2151 GValue *vals1, *vals2;
2153 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
2154 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
2156 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
2157 return GST_VALUE_UNORDERED;
2159 vals1 = (GValue *) value1->data[0].v_pointer;
2160 vals2 = (GValue *) value2->data[0].v_pointer;
2161 if (gst_value_compare_fraction (&vals1[0], &vals2[0]) == GST_VALUE_EQUAL &&
2162 gst_value_compare_fraction (&vals1[1], &vals2[1]) == GST_VALUE_EQUAL)
2163 return GST_VALUE_EQUAL;
2165 return GST_VALUE_UNORDERED;
2169 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
2171 g_warning ("unimplemented");
2180 * gst_value_set_caps:
2181 * @value: a GValue initialized to GST_TYPE_CAPS
2182 * @caps: (transfer none): the caps to set the value to
2184 * Sets the contents of @value to @caps. A reference to the
2185 * provided @caps will be taken by the @value.
2188 gst_value_set_caps (GValue * value, const GstCaps * caps)
2190 g_return_if_fail (G_IS_VALUE (value));
2191 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
2192 g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
2194 g_value_set_boxed (value, caps);
2198 * gst_value_get_caps:
2199 * @value: a GValue initialized to GST_TYPE_CAPS
2201 * Gets the contents of @value. The reference count of the returned
2202 * #GstCaps will not be modified, therefore the caller must take one
2203 * before getting rid of the @value.
2205 * Returns: (transfer none): the contents of @value
2208 gst_value_get_caps (const GValue * value)
2210 g_return_val_if_fail (G_IS_VALUE (value), NULL);
2211 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
2213 return (GstCaps *) g_value_get_boxed (value);
2217 gst_value_compare_caps (const GValue * value1, const GValue * value2)
2219 GstCaps *caps1 = GST_CAPS (gst_value_get_caps (value1));
2220 GstCaps *caps2 = GST_CAPS (gst_value_get_caps (value2));
2223 return GST_VALUE_EQUAL;
2225 if (!caps1 || !caps2)
2226 return GST_VALUE_UNORDERED;
2228 if (gst_caps_is_equal (caps1, caps2))
2229 return GST_VALUE_EQUAL;
2230 return GST_VALUE_UNORDERED;
2234 gst_value_serialize_caps (const GValue * value)
2236 GstCaps *caps = g_value_get_boxed (value);
2237 return priv_gst_string_take_and_wrap (gst_caps_to_string (caps));
2241 gst_value_deserialize_caps (GValue * dest, const gchar * s)
2246 /* this can happen if caps are ANY, EMPTY, or only contains a single
2247 * empty structure */
2248 caps = gst_caps_from_string (s);
2250 gchar *str = gst_string_unwrap (s);
2252 if (G_UNLIKELY (!str))
2255 caps = gst_caps_from_string (str);
2260 g_value_take_boxed (dest, caps);
2266 /********************************************
2267 * Serialization/deserialization of GValues *
2268 ********************************************/
2270 static GstValueAbbreviation *
2271 _priv_gst_value_get_abbrs (gint * n_abbrs)
2273 static GstValueAbbreviation *abbrs = NULL;
2274 static gsize num = 0;
2276 if (g_once_init_enter (&num)) {
2277 /* dynamically generate the array */
2279 GstValueAbbreviation dyn_abbrs[] = {
2284 {"uint", G_TYPE_UINT}
2288 {"float", G_TYPE_FLOAT}
2292 {"double", G_TYPE_DOUBLE}
2294 {"d", G_TYPE_DOUBLE}
2296 {"buffer", GST_TYPE_BUFFER}
2298 {"fraction", GST_TYPE_FRACTION}
2300 {"boolean", G_TYPE_BOOLEAN}
2302 {"bool", G_TYPE_BOOLEAN}
2304 {"b", G_TYPE_BOOLEAN}
2306 {"string", G_TYPE_STRING}
2308 {"str", G_TYPE_STRING}
2310 {"s", G_TYPE_STRING}
2312 {"structure", GST_TYPE_STRUCTURE}
2314 {"date", G_TYPE_DATE}
2316 {"datetime", GST_TYPE_DATE_TIME}
2318 {"bitmask", GST_TYPE_BITMASK}
2320 {"flagset", GST_TYPE_FLAG_SET}
2322 {"sample", GST_TYPE_SAMPLE}
2324 {"taglist", GST_TYPE_TAG_LIST}
2326 {"type", G_TYPE_GTYPE}
2328 {"array", GST_TYPE_ARRAY}
2330 {"list", GST_TYPE_LIST}
2332 _num = G_N_ELEMENTS (dyn_abbrs);
2333 /* permanently allocate and copy the array now */
2334 abbrs = g_new0 (GstValueAbbreviation, _num);
2335 memcpy (abbrs, dyn_abbrs, sizeof (GstValueAbbreviation) * _num);
2336 g_once_init_leave (&num, _num);
2343 /* given a type_name that could be a type abbreviation or a registered GType,
2344 * return a matching GType */
2346 _priv_gst_value_gtype_from_abbr (const char *type_name)
2349 GstValueAbbreviation *abbrs;
2353 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
2355 abbrs = _priv_gst_value_get_abbrs (&n_abbrs);
2357 for (i = 0; i < n_abbrs; i++) {
2358 if (strcmp (type_name, abbrs[i].type_name) == 0) {
2359 return abbrs[i].type;
2363 /* this is the fallback */
2364 ret = g_type_from_name (type_name);
2365 /* If not found, try it as a dynamic type */
2366 if (G_UNLIKELY (ret == 0))
2367 ret = gst_dynamic_type_factory_load (type_name);
2373 _priv_gst_value_gtype_to_abbr (GType type)
2376 GstValueAbbreviation *abbrs;
2379 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
2381 abbrs = _priv_gst_value_get_abbrs (&n_abbrs);
2383 for (i = 0; i < n_abbrs; i++) {
2384 if (type == abbrs[i].type) {
2385 return abbrs[i].type_name;
2389 return g_type_name (type);
2393 * _priv_gst_value_parse_string:
2394 * @s: string to parse
2395 * @end: out-pointer to char behind end of string
2396 * @next: out-pointer to start of unread data
2397 * @unescape: @TRUE if the substring is escaped.
2399 * Find the end of a sub-string. If end == next, the string will not be
2400 * null-terminated. In all other cases it will be.
2402 * Note: This function modifies the string in @s (if unescape == @TRUE).
2404 * Returns: @TRUE if a sub-string was found and @FALSE if the string is not
2408 _priv_gst_value_parse_string (gchar * s, gchar ** end, gchar ** next,
2417 int ret = _priv_gst_value_parse_simple_string (s, end);
2423 /* Find the closing quotes */
2428 if (G_UNLIKELY (*s == 0))
2430 if (G_UNLIKELY (*s == '\\')) {
2432 if (G_UNLIKELY (*s == 0))
2443 if (G_UNLIKELY (*s == 0))
2445 if (G_UNLIKELY (*s == '\\')) {
2447 if (G_UNLIKELY (*s == 0))
2463 _priv_gst_value_parse_range (gchar * s, gchar ** after, GValue * value,
2466 GValue value1 = { 0 };
2467 GValue value2 = { 0 };
2468 GValue value3 = { 0 };
2470 gboolean ret, have_step = FALSE;
2476 ret = _priv_gst_value_parse_value (s, &s, &value1, type, NULL);
2480 while (g_ascii_isspace (*s))
2487 while (g_ascii_isspace (*s))
2490 ret = _priv_gst_value_parse_value (s, &s, &value2, type, NULL);
2494 while (g_ascii_isspace (*s))
2497 /* optional step for int and int64 */
2498 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
2499 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2503 while (g_ascii_isspace (*s))
2506 ret = _priv_gst_value_parse_value (s, &s, &value3, type, NULL);
2510 while (g_ascii_isspace (*s))
2521 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2523 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2526 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2527 range_type = GST_TYPE_DOUBLE_RANGE;
2528 g_value_init (value, range_type);
2529 gst_value_set_double_range (value,
2530 gst_g_value_get_double_unchecked (&value1),
2531 gst_g_value_get_double_unchecked (&value2));
2532 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2533 range_type = GST_TYPE_INT_RANGE;
2534 g_value_init (value, range_type);
2536 gst_value_set_int_range_step (value,
2537 gst_g_value_get_int_unchecked (&value1),
2538 gst_g_value_get_int_unchecked (&value2),
2539 gst_g_value_get_int_unchecked (&value3));
2541 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2542 gst_g_value_get_int_unchecked (&value2));
2543 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2544 range_type = GST_TYPE_INT64_RANGE;
2545 g_value_init (value, range_type);
2547 gst_value_set_int64_range_step (value,
2548 gst_g_value_get_int64_unchecked (&value1),
2549 gst_g_value_get_int64_unchecked (&value2),
2550 gst_g_value_get_int64_unchecked (&value3));
2552 gst_value_set_int64_range (value,
2553 gst_g_value_get_int64_unchecked (&value1),
2554 gst_g_value_get_int64_unchecked (&value2));
2555 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2556 range_type = GST_TYPE_FRACTION_RANGE;
2557 g_value_init (value, range_type);
2558 gst_value_set_fraction_range (value, &value1, &value2);
2567 g_value_unset (value);
2568 g_value_unset (&value1);
2569 g_value_unset (&value2);
2570 g_value_unset (&value3);
2575 _priv_gst_value_parse_any_list (gchar * s, gchar ** after, GValue * value,
2576 GType type, char begin, char end, GParamSpec * pspec)
2578 GValue list_value = { 0 };
2580 GstValueList *vlist = VALUE_LIST_ARRAY (value);
2581 GParamSpec *element_spec = NULL;
2584 element_spec = GST_PARAM_SPEC_ARRAY_LIST (pspec)->element_spec;
2590 while (g_ascii_isspace (*s))
2596 while (g_ascii_isspace (*s))
2605 memset (&list_value, 0, sizeof (list_value));
2607 ret = _priv_gst_value_parse_value (s, &s, &list_value, type, element_spec);
2611 _gst_value_list_append_val (vlist, &list_value);
2613 while (g_ascii_isspace (*s))
2616 if (*s != ',' && *s != end)
2627 _priv_gst_value_parse_list (gchar * s, gchar ** after, GValue * value,
2628 GType type, GParamSpec * pspec)
2630 return _priv_gst_value_parse_any_list (s, after, value, type, '{', '}',
2635 _priv_gst_value_parse_array (gchar * s, gchar ** after, GValue * value,
2636 GType type, GParamSpec * pspec)
2638 return _priv_gst_value_parse_any_list (s, after, value, type, '<', '>',
2643 _priv_gst_value_parse_simple_string (gchar * str, gchar ** end)
2647 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2657 _priv_gst_value_parse_struct_or_caps (gchar * str, gchar ** after, GType type,
2661 gboolean ret = FALSE;
2662 gchar *s = str, t, *start, *end, *next;
2686 g_value_init (value, type);
2687 if (priv_gst_structure_parse_name (str, &start, &end, &next, TRUE))
2688 ret = gst_value_deserialize (value, str);
2689 if (G_UNLIKELY (!ret)) {
2691 g_value_unset (value);
2698 _priv_gst_value_parse_range_struct_caps (gchar * s, gchar ** after,
2699 GValue * value, GType type)
2703 gboolean ret = FALSE;
2704 GType try_types[] = {
2709 if (type == GST_TYPE_CAPS || type == GST_TYPE_STRUCTURE)
2710 ret = _priv_gst_value_parse_struct_or_caps (tmp, &tmp, type, value);
2716 ret = _priv_gst_value_parse_range (tmp, &tmp, value, type);
2720 if (type != G_TYPE_INVALID)
2723 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2725 ret = _priv_gst_value_parse_struct_or_caps (tmp, &tmp, try_types[i], value);
2738 _priv_gst_value_parse_value (gchar * str,
2739 gchar ** after, GValue * value, GType default_type, GParamSpec * pspec)
2748 GType type = default_type;
2751 while (g_ascii_isspace (*s))
2754 /* check if there's a (type_name) 'cast' */
2759 while (g_ascii_isspace (*s))
2762 if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &type_end)))
2765 while (g_ascii_isspace (*s))
2767 if (G_UNLIKELY (*s != ')'))
2770 while (g_ascii_isspace (*s))
2775 type = _priv_gst_value_gtype_from_abbr (type_name);
2776 GST_DEBUG ("trying type name '%s'", type_name);
2779 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2780 GST_WARNING ("invalid type");
2784 type = G_PARAM_SPEC_VALUE_TYPE (pspec);
2787 while (g_ascii_isspace (*s))
2790 ret = _priv_gst_value_parse_range_struct_caps (s, &s, value, type);
2791 } else if (*s == '{') {
2792 g_value_init (value, GST_TYPE_LIST);
2793 ret = _priv_gst_value_parse_list (s, &s, value, type, pspec);
2794 } else if (*s == '<') {
2795 g_value_init (value, GST_TYPE_ARRAY);
2796 ret = _priv_gst_value_parse_array (s, &s, value, type, pspec);
2800 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2802 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, GST_TYPE_FLAG_SET,
2803 G_TYPE_BOOLEAN, G_TYPE_STRING
2807 gboolean check_wrapped_non_string;
2809 if (G_UNLIKELY (!_priv_gst_value_parse_string (s, &value_end, &s, FALSE)))
2811 /* Set NULL terminator for deserialization */
2812 value_size = value_end - value_s;
2813 value_s = g_strndup (value_s, value_end - value_s);
2814 /* Keep old broken behavior where "2" could be interpretted as an int */
2815 check_wrapped_non_string = value_s[0] == '"' &&
2816 strlen (value_s) >= 2 && value_end[-1] == '"';
2818 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2819 g_value_init (value, try_types[i]);
2820 if (try_types[i] != G_TYPE_STRING && check_wrapped_non_string) {
2821 value_s[value_size - 1] = '\0';
2822 ret = gst_value_deserialize (value, value_s + 1);
2823 value_s[value_size - 1] = '"';
2825 const gchar *type_name = g_type_name (try_types[i]);
2827 g_warning ("Received a structure string that contains "
2828 "'=%s'. Reading as a %s value, rather than a string "
2829 "value. This is undesired behaviour, and with GStreamer 1.22 "
2830 " onward, this will be interpreted as a string value instead "
2831 "because it is wrapped in '\"' quotes. If you want to "
2832 "guarantee this value is read as a string, before this "
2833 "change, use '=(string)%s' instead. If you want to read "
2834 "in a %s value, leave its value unquoted.",
2835 value_s, type_name, value_s, type_name);
2839 ret = gst_value_deserialize (value, value_s);
2843 g_value_unset (value);
2846 g_value_init (value, type);
2848 if (G_UNLIKELY (!_priv_gst_value_parse_string (s, &value_end, &s, FALSE)))
2850 /* Set NULL terminator for deserialization */
2851 value_s = g_strndup (value_s, value_end - value_s);
2853 ret = gst_value_deserialize_with_pspec (value, value_s, pspec);
2854 if (G_UNLIKELY (!ret))
2855 g_value_unset (value);
2870 gst_value_serialize_segment_internal (const GValue * value, gboolean escape)
2872 GstSegment *seg = g_value_get_boxed (value);
2876 s = gst_structure_new_id (GST_QUARK (SEGMENT),
2877 GST_QUARK (FLAGS), GST_TYPE_SEGMENT_FLAGS, seg->flags,
2878 GST_QUARK (RATE), G_TYPE_DOUBLE, seg->rate,
2879 GST_QUARK (APPLIED_RATE), G_TYPE_DOUBLE, seg->applied_rate,
2880 GST_QUARK (FORMAT), GST_TYPE_FORMAT, seg->format,
2881 GST_QUARK (BASE), G_TYPE_UINT64, seg->base,
2882 GST_QUARK (OFFSET), G_TYPE_UINT64, seg->offset,
2883 GST_QUARK (START), G_TYPE_UINT64, seg->start,
2884 GST_QUARK (STOP), G_TYPE_UINT64, seg->stop,
2885 GST_QUARK (TIME), G_TYPE_UINT64, seg->time,
2886 GST_QUARK (POSITION), G_TYPE_UINT64, seg->position,
2887 GST_QUARK (DURATION), G_TYPE_UINT64, seg->duration, NULL);
2889 t = gst_structure_to_string (s);
2891 res = g_strdup_printf ("\"%s\"", t);
2896 gst_structure_free (s);
2902 gst_value_serialize_segment (const GValue * value)
2904 return gst_value_serialize_segment_internal (value, TRUE);
2908 gst_value_deserialize_segment_internal (GValue * dest, const gchar * s,
2919 if (G_UNLIKELY (*s != '"' || len < 2 || s[len - 1] != '"')) {
2920 /* "\"" is not an accepted string, so len must be at least 2 */
2921 GST_ERROR ("Failed deserializing segement: expected string to start and "
2925 t = g_strdup (s + 1);
2927 /* removed trailing '"' */
2928 str = gst_structure_from_string (t, NULL);
2931 str = gst_structure_from_string (s, NULL);
2933 if (G_UNLIKELY (str == NULL))
2936 res = gst_structure_id_get (str,
2937 GST_QUARK (FLAGS), GST_TYPE_SEGMENT_FLAGS, &seg.flags,
2938 GST_QUARK (RATE), G_TYPE_DOUBLE, &seg.rate,
2939 GST_QUARK (APPLIED_RATE), G_TYPE_DOUBLE, &seg.applied_rate,
2940 GST_QUARK (FORMAT), GST_TYPE_FORMAT, &seg.format,
2941 GST_QUARK (BASE), G_TYPE_UINT64, &seg.base,
2942 GST_QUARK (OFFSET), G_TYPE_UINT64, &seg.offset,
2943 GST_QUARK (START), G_TYPE_UINT64, &seg.start,
2944 GST_QUARK (STOP), G_TYPE_UINT64, &seg.stop,
2945 GST_QUARK (TIME), G_TYPE_UINT64, &seg.time,
2946 GST_QUARK (POSITION), G_TYPE_UINT64, &seg.position,
2947 GST_QUARK (DURATION), G_TYPE_UINT64, &seg.duration, NULL);
2948 gst_structure_free (str);
2951 g_value_set_boxed (dest, &seg);
2957 gst_value_deserialize_segment (GValue * dest, const gchar * s)
2959 return gst_value_deserialize_segment_internal (dest, s, TRUE);
2967 * gst_value_set_structure:
2968 * @value: a GValue initialized to GST_TYPE_STRUCTURE
2969 * @structure: the structure to set the value to
2971 * Sets the contents of @value to @structure.
2974 gst_value_set_structure (GValue * value, const GstStructure * structure)
2976 g_return_if_fail (G_IS_VALUE (value));
2977 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
2978 g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
2980 g_value_set_boxed (value, structure);
2984 * gst_value_get_structure:
2985 * @value: a GValue initialized to GST_TYPE_STRUCTURE
2987 * Gets the contents of @value.
2989 * Returns: (transfer none): the contents of @value
2991 const GstStructure *
2992 gst_value_get_structure (const GValue * value)
2994 g_return_val_if_fail (G_IS_VALUE (value), NULL);
2995 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
2997 return (GstStructure *) g_value_get_boxed (value);
3001 gst_value_serialize_structure (const GValue * value)
3003 GstStructure *structure = g_value_get_boxed (value);
3005 return priv_gst_string_take_and_wrap (gst_structure_to_string (structure));
3006 /* string should always end up being wrapped, since a structure string
3007 * ends in a ';' character */
3011 gst_value_deserialize_structure (GValue * dest, const gchar * s)
3013 GstStructure *structure;
3016 /* the output of gst_value_serialize_structure would never produce
3017 * such a string, but a user may pass to gst_structure_from_string
3019 * name, sub=(GstStructure)sub-name, val=(int)5;
3020 * and expect sub to be read as an *empty* structure with the name
3021 * sub-name. Similar to
3022 * name, caps=(GstCaps)video/x-raw, val=(int)5;
3023 * which gst_structure_to_string can produce. */
3024 structure = gst_structure_from_string (s, NULL);
3026 gchar *str = gst_string_unwrap (s);
3028 if (G_UNLIKELY (!str))
3031 structure = gst_structure_from_string (str, NULL);
3035 if (G_LIKELY (structure)) {
3036 g_value_take_boxed (dest, structure);
3043 gst_value_compare_structure (const GValue * value1, const GValue * value2)
3045 GstStructure *structure1 = GST_STRUCTURE (g_value_get_boxed (value1));
3046 GstStructure *structure2 = GST_STRUCTURE (g_value_get_boxed (value2));
3048 if (structure1 == structure2)
3049 return GST_VALUE_EQUAL;
3051 if (!structure1 || !structure2)
3052 return GST_VALUE_UNORDERED;
3054 if (gst_structure_is_equal (structure1, structure2))
3055 return GST_VALUE_EQUAL;
3057 return GST_VALUE_UNORDERED;
3060 /*******************
3062 *******************/
3065 * gst_value_set_caps_features:
3066 * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
3067 * @features: the features to set the value to
3069 * Sets the contents of @value to @features.
3072 gst_value_set_caps_features (GValue * value, const GstCapsFeatures * features)
3074 g_return_if_fail (G_IS_VALUE (value));
3075 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES);
3076 g_return_if_fail (features == NULL || GST_IS_CAPS_FEATURES (features));
3078 g_value_set_boxed (value, features);
3082 * gst_value_get_caps_features:
3083 * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
3085 * Gets the contents of @value.
3087 * Returns: (transfer none): the contents of @value
3089 const GstCapsFeatures *
3090 gst_value_get_caps_features (const GValue * value)
3092 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3093 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES, NULL);
3095 return (GstCapsFeatures *) g_value_get_boxed (value);
3099 gst_value_serialize_caps_features (const GValue * value)
3101 GstCapsFeatures *features = g_value_get_boxed (value);
3103 return priv_gst_string_take_and_wrap (gst_caps_features_to_string (features));
3107 gst_value_deserialize_caps_features (GValue * dest, const gchar * s)
3109 GstCapsFeatures *features;
3112 /* This can happen if gst_caps_features_to_string only returns
3113 * ALL, NONE, or a single features name, which means it is not
3114 * actually wrapped by priv_gst_string_take_and_wrap */
3115 features = gst_caps_features_from_string (s);
3117 gchar *str = gst_string_unwrap (s);
3119 if (G_UNLIKELY (!str))
3122 features = gst_caps_features_from_string (str);
3126 if (G_LIKELY (features)) {
3127 g_value_take_boxed (dest, features);
3137 gst_value_compare_tag_list (const GValue * value1, const GValue * value2)
3139 GstTagList *taglist1 = GST_TAG_LIST (g_value_get_boxed (value1));
3140 GstTagList *taglist2 = GST_TAG_LIST (g_value_get_boxed (value2));
3142 if (gst_tag_list_is_equal (taglist1, taglist2))
3143 return GST_VALUE_EQUAL;
3144 return GST_VALUE_UNORDERED;
3148 gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
3150 GstTagList *taglist;
3153 /* the output of gst_value_serialize_tag_list would never produce
3154 * such a string, but a user may pass to gst_structure_from_string
3156 * name, list=(GstTagList)taglist, val=(int)5;
3157 * and expect list to be read as an *empty* tag list. Similar to
3158 * name, caps=(GstCaps)video/x-raw, val=(int)5;
3159 * which gst_structure_to_string can produce. */
3160 taglist = gst_tag_list_new_from_string (s);
3162 gchar *str = gst_string_unwrap (s);
3164 if (G_UNLIKELY (!str))
3167 taglist = gst_tag_list_new_from_string (str);
3171 if (G_LIKELY (taglist != NULL)) {
3172 g_value_take_boxed (dest, taglist);
3179 gst_value_serialize_tag_list (const GValue * value)
3181 GstTagList *taglist = g_value_get_boxed (value);
3183 return priv_gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
3184 /* string should always end up being wrapped, since a taglist (structure)
3185 * string ends in a ';' character */
3194 compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
3197 GstMapInfo info1, info2;
3201 return GST_VALUE_EQUAL;
3203 size1 = gst_buffer_get_size (buf1);
3204 size2 = gst_buffer_get_size (buf2);
3207 return GST_VALUE_UNORDERED;
3210 return GST_VALUE_EQUAL;
3212 if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
3213 return GST_VALUE_UNORDERED;
3215 if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
3216 gst_buffer_unmap (buf1, &info1);
3217 return GST_VALUE_UNORDERED;
3220 mret = memcmp (info1.data, info2.data, info1.size);
3222 result = GST_VALUE_EQUAL;
3224 result = GST_VALUE_LESS_THAN;
3226 result = GST_VALUE_GREATER_THAN;
3228 gst_buffer_unmap (buf1, &info1);
3229 gst_buffer_unmap (buf2, &info2);
3235 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
3237 GstBuffer *buf1 = gst_value_get_buffer (value1);
3238 GstBuffer *buf2 = gst_value_get_buffer (value2);
3240 return compare_buffer (buf1, buf2);
3244 gst_value_serialize_buffer (const GValue * value)
3252 buffer = gst_value_get_buffer (value);
3256 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
3261 string = g_malloc (info.size * 2 + 1);
3262 for (i = 0; i < info.size; i++) {
3263 sprintf (string + i * 2, "%02x", data[i]);
3265 string[info.size * 2] = 0;
3267 gst_buffer_unmap (buffer, &info);
3273 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
3286 buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
3287 if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
3291 for (i = 0; i < len / 2; i++) {
3292 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
3295 ts[0] = s[i * 2 + 0];
3296 ts[1] = s[i * 2 + 1];
3299 data[i] = (guint8) strtoul (ts, NULL, 16);
3301 gst_buffer_unmap (buffer, &info);
3303 gst_value_take_buffer (dest, buffer);
3318 gst_buffer_unref (buffer);
3319 gst_buffer_unmap (buffer, &info);
3328 /* This function is mostly used for comparing image/buffer tags in taglists */
3330 gst_value_compare_sample (const GValue * value1, const GValue * value2)
3332 GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
3333 GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
3335 /* FIXME: should we take into account anything else such as caps? */
3336 return compare_buffer (buf1, buf2);
3340 gst_value_serialize_sample (const GValue * value)
3342 const GstStructure *info_structure;
3343 GstSegment *segment;
3347 GValue val = { 0, };
3348 gchar *info_str, *caps_str, *tmp;
3349 gchar *buf_str, *seg_str, *s;
3351 sample = g_value_get_boxed (value);
3353 buffer = gst_sample_get_buffer (sample);
3355 g_value_init (&val, GST_TYPE_BUFFER);
3356 g_value_set_boxed (&val, buffer);
3357 buf_str = gst_value_serialize_buffer (&val);
3358 g_value_unset (&val);
3360 buf_str = g_strdup ("None");
3363 caps = gst_sample_get_caps (sample);
3365 tmp = gst_caps_to_string (caps);
3366 caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3367 g_strdelimit (caps_str, "=", '_');
3370 caps_str = g_strdup ("None");
3373 segment = gst_sample_get_segment (sample);
3375 g_value_init (&val, GST_TYPE_SEGMENT);
3376 g_value_set_boxed (&val, segment);
3377 tmp = gst_value_serialize_segment_internal (&val, FALSE);
3378 seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3379 g_strdelimit (seg_str, "=", '_');
3381 g_value_unset (&val);
3383 seg_str = g_strdup ("None");
3386 info_structure = gst_sample_get_info (sample);
3387 if (info_structure) {
3388 tmp = gst_structure_to_string (info_structure);
3389 info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3390 g_strdelimit (info_str, "=", '_');
3393 info_str = g_strdup ("None");
3396 s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
3406 gst_value_deserialize_sample (GValue * dest, const gchar * s)
3408 GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
3411 GstCaps *caps = NULL;
3412 gboolean ret = FALSE;
3417 GST_TRACE ("deserialize '%s'", s);
3419 fields = g_strsplit (s, ":", -1);
3420 len = g_strv_length (fields);
3424 g_value_init (&bval, GST_TYPE_BUFFER);
3425 g_value_init (&sval, GST_TYPE_SEGMENT);
3427 if (!gst_value_deserialize_buffer (&bval, fields[0]))
3430 if (strcmp (fields[1], "None") != 0) {
3431 g_strdelimit (fields[1], "_", '=');
3432 g_base64_decode_inplace (fields[1], &outlen);
3433 GST_TRACE ("caps : %s", fields[1]);
3434 caps = gst_caps_from_string (fields[1]);
3439 if (strcmp (fields[2], "None") != 0) {
3440 g_strdelimit (fields[2], "_", '=');
3441 g_base64_decode_inplace (fields[2], &outlen);
3442 GST_TRACE ("segment : %s", fields[2]);
3443 if (!gst_value_deserialize_segment_internal (&sval, fields[2], FALSE))
3447 if (strcmp (fields[3], "None") != 0) {
3448 g_strdelimit (fields[3], "_", '=');
3449 g_base64_decode_inplace (fields[3], &outlen);
3450 GST_TRACE ("info : %s", fields[3]);
3451 info = gst_structure_from_string (fields[3], NULL);
3458 sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
3459 g_value_get_boxed (&sval), info);
3461 g_value_take_boxed (dest, sample);
3467 gst_caps_unref (caps);
3468 g_value_unset (&bval);
3469 g_value_unset (&sval);
3473 g_strfreev (fields);
3483 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
3485 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
3486 return GST_VALUE_EQUAL;
3487 return GST_VALUE_UNORDERED;
3491 gst_value_serialize_boolean (const GValue * value)
3493 if (value->data[0].v_int) {
3494 return g_strdup ("true");
3496 return g_strdup ("false");
3500 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
3502 gboolean ret = FALSE;
3504 if (g_ascii_strcasecmp (s, "true") == 0 ||
3505 g_ascii_strcasecmp (s, "yes") == 0 ||
3506 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
3507 g_value_set_boolean (dest, TRUE);
3509 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
3510 g_ascii_strcasecmp (s, "no") == 0 ||
3511 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
3512 g_value_set_boolean (dest, FALSE);
3519 #define CREATE_SERIALIZATION_START(_type,_macro) \
3521 gst_value_compare_ ## _type \
3522 (const GValue * value1, const GValue * value2) \
3524 g ## _type val1 = g_value_get_ ## _type (value1); \
3525 g ## _type val2 = g_value_get_ ## _type (value2); \
3527 return GST_VALUE_GREATER_THAN; \
3529 return GST_VALUE_LESS_THAN; \
3530 return GST_VALUE_EQUAL; \
3534 gst_value_serialize_ ## _type (const GValue * value) \
3536 GValue val = { 0, }; \
3537 g_value_init (&val, G_TYPE_STRING); \
3538 if (!g_value_transform (value, &val)) \
3539 g_assert_not_reached (); \
3540 /* NO_COPY_MADNESS!!! */ \
3541 return (char *) g_value_get_string (&val); \
3544 /* deserialize the given s into to as a gint64.
3545 * check if the result is actually storeable in the given size number of
3549 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
3550 gint64 min, gint64 max, gint size)
3552 gboolean ret = FALSE;
3557 *to = g_ascii_strtoull (s, &end, 0);
3558 /* a range error is a definitive no-no */
3559 if (errno == ERANGE) {
3566 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
3567 *to = G_LITTLE_ENDIAN;
3569 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
3572 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
3575 } else if (g_ascii_strcasecmp (s, "min") == 0) {
3578 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3584 /* by definition, a gint64 fits into a gint64; so ignore those */
3585 if (size != sizeof (mask)) {
3587 /* for positive numbers, we create a mask of 1's outside of the range
3588 * and 0's inside the range. An and will thus keep only 1 bits
3589 * outside of the range */
3590 mask <<= (size * 8);
3591 if ((mask & *to) != 0) {
3595 /* for negative numbers, we do a 2's complement version */
3596 mask <<= ((size * 8) - 1);
3597 if ((mask & *to) != mask) {
3606 #define CREATE_SERIALIZATION(_type,_macro) \
3607 CREATE_SERIALIZATION_START(_type,_macro) \
3610 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
3614 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
3615 G_MAX ## _macro, sizeof (g ## _type))) { \
3616 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
3623 #define CREATE_USERIALIZATION(_type,_macro) \
3624 CREATE_SERIALIZATION_START(_type,_macro) \
3627 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
3631 gboolean ret = FALSE; \
3634 x = g_ascii_strtoull (s, &end, 0); \
3635 /* a range error is a definitive no-no */ \
3636 if (errno == ERANGE) { \
3639 /* the cast ensures the range check later on makes sense */ \
3640 x = (g ## _type) x; \
3644 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
3645 x = G_LITTLE_ENDIAN; \
3647 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
3650 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
3653 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
3656 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
3657 x = G_MAX ## _macro; \
3662 if (x > G_MAX ## _macro) { \
3665 g_value_set_ ## _type (dest, x); \
3671 CREATE_SERIALIZATION (int, INT);
3672 CREATE_SERIALIZATION (int64, INT64);
3673 CREATE_SERIALIZATION (long, LONG);
3675 CREATE_USERIALIZATION (uint, UINT);
3676 CREATE_USERIALIZATION (uint64, UINT64);
3677 CREATE_USERIALIZATION (ulong, ULONG);
3679 /* FIXME 2.0: remove this again, plugins shouldn't have uchar properties */
3681 #define G_MAXUCHAR 255
3683 CREATE_USERIALIZATION (uchar, UCHAR);
3689 gst_value_compare_double (const GValue * value1, const GValue * value2)
3691 if (value1->data[0].v_double > value2->data[0].v_double)
3692 return GST_VALUE_GREATER_THAN;
3693 if (value1->data[0].v_double < value2->data[0].v_double)
3694 return GST_VALUE_LESS_THAN;
3695 if (value1->data[0].v_double == value2->data[0].v_double)
3696 return GST_VALUE_EQUAL;
3697 return GST_VALUE_UNORDERED;
3701 gst_value_serialize_double (const GValue * value)
3703 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
3705 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
3706 return g_strdup (d);
3710 gst_value_deserialize_double (GValue * dest, const gchar * s)
3713 gboolean ret = FALSE;
3716 x = g_ascii_strtod (s, &end);
3720 if (g_ascii_strcasecmp (s, "min") == 0) {
3723 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3729 g_value_set_double (dest, x);
3739 gst_value_compare_float (const GValue * value1, const GValue * value2)
3741 if (value1->data[0].v_float > value2->data[0].v_float)
3742 return GST_VALUE_GREATER_THAN;
3743 if (value1->data[0].v_float < value2->data[0].v_float)
3744 return GST_VALUE_LESS_THAN;
3745 if (value1->data[0].v_float == value2->data[0].v_float)
3746 return GST_VALUE_EQUAL;
3747 return GST_VALUE_UNORDERED;
3751 gst_value_serialize_float (const GValue * value)
3753 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
3755 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
3756 return g_strdup (d);
3760 gst_value_deserialize_float (GValue * dest, const gchar * s)
3763 gboolean ret = FALSE;
3766 x = g_ascii_strtod (s, &end);
3770 if (g_ascii_strcasecmp (s, "min") == 0) {
3773 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3778 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
3781 g_value_set_float (dest, (float) x);
3791 gst_value_compare_string (const GValue * value1, const GValue * value2)
3793 if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
3794 /* if only one is NULL, no match - otherwise both NULL == EQUAL */
3795 if (value1->data[0].v_pointer != value2->data[0].v_pointer)
3796 return GST_VALUE_UNORDERED;
3798 gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
3801 return GST_VALUE_LESS_THAN;
3803 return GST_VALUE_GREATER_THAN;
3806 return GST_VALUE_EQUAL;
3810 gst_string_measure_wrapping (const gchar * s)
3813 gboolean wrap = FALSE;
3815 if (G_UNLIKELY (s == NULL))
3818 /* Special case: the actual string NULL needs wrapping */
3819 if (G_UNLIKELY (strcmp (s, "NULL") == 0))
3824 if (GST_ASCII_IS_STRING (*s)) {
3826 } else if (*s < 0x20 || *s >= 0x7f) {
3836 /* Wrap the string if we found something that needs
3837 * wrapping, or the empty string (len == 0) */
3838 return (wrap || len == 0) ? len : -1;
3842 gst_string_wrap_inner (const gchar * s, gint len)
3846 e = d = g_malloc (len + 3);
3850 if (GST_ASCII_IS_STRING (*s)) {
3852 } else if (*s < 0x20 || *s >= 0x7f) {
3854 *e++ = '0' + ((*(guchar *) s) >> 6);
3855 *e++ = '0' + (((*s) >> 3) & 0x7);
3856 *e++ = '0' + ((*s++) & 0x7);
3865 g_assert (e - d <= len + 3);
3869 /* Do string wrapping/escaping */
3871 gst_string_wrap (const gchar * s)
3873 gint len = gst_string_measure_wrapping (s);
3875 if (G_LIKELY (len < 0))
3876 return g_strdup (s);
3878 return gst_string_wrap_inner (s, len);
3881 /* Same as above, but take ownership of the string */
3883 priv_gst_string_take_and_wrap (gchar * s)
3886 gint len = gst_string_measure_wrapping (s);
3888 if (G_LIKELY (len < 0))
3891 out = gst_string_wrap_inner (s, len);
3898 * This function takes a string delimited with double quotes (")
3899 * and unescapes any \xxx octal numbers.
3901 * If sequences of \y are found where y is not in the range of
3902 * 0->3, y is copied unescaped.
3904 * If \xyy is found where x is an octal number but y is not, an
3905 * error is encountered and %NULL is returned.
3907 * the input string must be \0 terminated.
3910 gst_string_unwrap (const gchar * s)
3913 gchar *read, *write;
3915 /* NULL string returns NULL */
3919 /* strings not starting with " are invalid */
3923 /* make copy of original string to hold the result. This
3924 * string will always be smaller than the original */
3929 /* need to move to the next position as we parsed the " */
3933 if (GST_ASCII_IS_STRING (*read)) {
3934 /* normal chars are just copied */
3936 } else if (*read == '"') {
3937 /* quote marks end of string */
3939 } else if (*read == '\\') {
3940 /* got an escape char, move to next position to read a tripplet
3941 * of octal numbers */
3943 /* is the next char a possible first octal number? */
3944 if (*read >= '0' && *read <= '3') {
3945 /* parse other 2 numbers, if one of them is not in the range of
3946 * an octal number, we error. We also catch the case where a zero
3947 * byte is found here. */
3948 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
3951 /* now convert the octal number to a byte again. */
3952 *write++ = ((read[0] - '0') << 6) +
3953 ((read[1] - '0') << 3) + (read[2] - '0');
3957 /* if we run into a \0 here, we definitely won't get a quote later */
3960 /* else copy \X sequence */
3963 } else if (*read == '\0') {
3969 /* if the string is not ending in " and zero terminated, we error */
3970 if (*read != '"' || read[1] != '\0')
3973 /* null terminate result string and return */
3983 gst_value_serialize_string (const GValue * value)
3985 return gst_string_wrap (value->data[0].v_pointer);
3989 gst_value_deserialize_string (GValue * dest, const gchar * s)
3991 if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
3992 g_value_set_string (dest, NULL);
3994 } else if (G_LIKELY (*s != '"' || s[strlen (s) - 1] != '"')) {
3995 if (!g_utf8_validate (s, -1, NULL))
3997 g_value_set_string (dest, s);
4000 /* strings delimited with double quotes should be unwrapped */
4001 gchar *str = gst_string_unwrap (s);
4002 if (G_UNLIKELY (!str))
4004 if (!g_utf8_validate (str, -1, NULL)) {
4008 g_value_take_string (dest, str);
4019 gst_value_compare_enum (const GValue * value1, const GValue * value2)
4021 GEnumValue *en1, *en2;
4022 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
4023 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
4025 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
4026 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
4027 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
4028 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
4029 g_type_class_unref (klass1);
4030 g_type_class_unref (klass2);
4031 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
4032 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
4033 if (en1->value < en2->value)
4034 return GST_VALUE_LESS_THAN;
4035 if (en1->value > en2->value)
4036 return GST_VALUE_GREATER_THAN;
4038 return GST_VALUE_EQUAL;
4042 gst_value_serialize_enum (const GValue * value)
4045 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
4047 g_return_val_if_fail (klass, NULL);
4048 en = g_enum_get_value (klass, g_value_get_enum (value));
4049 g_type_class_unref (klass);
4051 /* might be one of the custom formats registered later */
4052 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
4053 const GstFormatDefinition *format_def;
4055 format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
4056 g_return_val_if_fail (format_def != NULL, NULL);
4057 return g_strdup (format_def->description);
4060 g_return_val_if_fail (en, NULL);
4061 return g_strdup (en->value_nick);
4065 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
4068 const GstFormatDefinition *format_def =
4069 g_value_get_pointer (format_def_value);
4071 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
4074 return g_ascii_strcasecmp (s, format_def->description);
4078 gst_value_deserialize_enum (GValue * dest, const gchar * s)
4081 gchar *endptr = NULL;
4082 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
4084 g_return_val_if_fail (klass, FALSE);
4085 if (!(en = g_enum_get_value_by_name (klass, s))) {
4086 if (!(en = g_enum_get_value_by_nick (klass, s))) {
4087 gint i = strtol (s, &endptr, 0);
4089 if (endptr && *endptr == '\0') {
4090 en = g_enum_get_value (klass, i);
4094 g_type_class_unref (klass);
4096 /* might be one of the custom formats registered later */
4097 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
4098 GValue res = { 0, };
4099 const GstFormatDefinition *format_def;
4103 iter = gst_format_iterate_definitions ();
4105 found = gst_iterator_find_custom (iter,
4106 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
4109 format_def = g_value_get_pointer (&res);
4110 g_return_val_if_fail (format_def != NULL, FALSE);
4111 g_value_set_enum (dest, (gint) format_def->value);
4112 g_value_unset (&res);
4114 gst_iterator_free (iter);
4118 /* enum name/nick not found */
4122 g_value_set_enum (dest, en->value);
4130 /* we just compare the value here */
4132 gst_value_compare_gflags (const GValue * value1, const GValue * value2)
4135 GFlagsClass *klass1 =
4136 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
4137 GFlagsClass *klass2 =
4138 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
4140 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
4141 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
4142 fl1 = g_value_get_flags (value1);
4143 fl2 = g_value_get_flags (value2);
4144 g_type_class_unref (klass1);
4145 g_type_class_unref (klass2);
4147 return GST_VALUE_LESS_THAN;
4149 return GST_VALUE_GREATER_THAN;
4151 return GST_VALUE_EQUAL;
4154 /* the different flags are serialized separated with a + */
4156 gst_value_serialize_gflags (const GValue * value)
4160 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
4161 gchar *result, *tmp;
4162 gboolean first = TRUE;
4164 g_return_val_if_fail (klass, NULL);
4166 flags = g_value_get_flags (value);
4168 /* if no flags are set, try to serialize to the _NONE string */
4170 fl = g_flags_get_first_value (klass, flags);
4172 return g_strdup (fl->value_name);
4174 return g_strdup ("0");
4177 /* some flags are set, so serialize one by one */
4178 result = g_strdup ("");
4180 fl = g_flags_get_first_value (klass, flags);
4182 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
4188 flags &= ~fl->value;
4191 g_type_class_unref (klass);
4197 gst_value_gflags_str_to_flags (GFlagsClass * klass, const gchar * s,
4198 guint * out_flags, guint * out_mask)
4202 const gchar *pos = NULL;
4204 gchar *cur_str, *endptr;
4209 g_return_val_if_fail (klass, FALSE);
4211 /* split into parts delimited with + or / and
4212 * compose the set of flags and mask. */
4216 goto done; /* Empty string, nothing to do */
4218 /* As a special case if the first char isn't a delimiter, assume
4219 * it's a '+' - for GFlags strings, which don't start with a
4220 * delimiter, while GFlagSet always will */
4221 if (*pos == '/' || *pos == '+') {
4229 /* Find the next delimiter */
4231 while (*next != '\0' && *next != '+' && *next != '/')
4233 cur_str = g_strndup (pos, next - pos);
4235 if ((fl = g_flags_get_value_by_name (klass, cur_str)))
4237 else if ((fl = g_flags_get_value_by_nick (klass, cur_str)))
4240 val = strtoul (cur_str, &endptr, 0);
4241 /* direct numeric value */
4242 if (endptr == NULL || *endptr != '\0') {
4244 return FALSE; /* Invalid numeric or string we can't convert */
4251 if (delimiter == '+')
4255 /* Advance to the next delimiter */
4259 } while (delimiter != '\0');
4272 gst_value_deserialize_gflags (GValue * dest, const gchar * s)
4274 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
4275 gboolean res = FALSE;
4278 if (gst_value_gflags_str_to_flags (klass, s, &flags, NULL)) {
4279 g_value_set_flags (dest, flags);
4283 g_type_class_unref (klass);
4293 gst_value_compare_gtype (const GValue * value1, const GValue * value2)
4295 if (value1->data[0].v_pointer == value2->data[0].v_pointer)
4296 return GST_VALUE_EQUAL;
4297 return GST_VALUE_UNORDERED;
4301 gst_value_serialize_gtype (const GValue * value)
4303 return g_strdup (g_type_name (g_value_get_gtype (value)));
4307 gst_value_deserialize_gtype (GValue * dest, const gchar * s)
4309 GType t = g_type_from_name (s);
4310 gboolean ret = TRUE;
4312 if (t == G_TYPE_INVALID)
4315 g_value_set_gtype (dest, t);
4325 gst_value_is_subset_int_range_int_range (const GValue * value1,
4326 const GValue * value2)
4330 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
4331 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
4333 if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
4334 INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
4336 if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
4337 INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
4340 if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
4341 if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
4342 INT_RANGE_STEP (value1))
4348 gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
4349 INT_RANGE_STEP (value2));
4350 if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
4357 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
4358 const GValue * value2)
4362 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
4363 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
4365 if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
4367 if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
4370 if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
4371 if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
4372 INT64_RANGE_STEP (value1))
4378 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
4379 INT64_RANGE_STEP (value2));
4380 if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
4386 /* A flag set is a subset of another if the superset allows the
4387 * flags of the subset */
4389 gst_value_is_subset_flagset_flagset (const GValue * value1,
4390 const GValue * value2)
4395 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value1), FALSE);
4396 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value2), FALSE);
4398 f1 = value1->data[0].v_uint;
4399 f2 = value2->data[0].v_uint;
4401 m1 = value1->data[1].v_uint;
4402 m2 = value2->data[1].v_uint;
4404 /* Not a subset if masked bits of superset disagree */
4405 if ((f1 & m1) != (f2 & (m1 & m2)))
4412 gst_value_is_subset_structure_structure (const GValue * value1,
4413 const GValue * value2)
4415 const GstStructure *s1, *s2;
4417 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (value1), FALSE);
4418 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (value2), FALSE);
4420 s1 = gst_value_get_structure (value1);
4421 s2 = gst_value_get_structure (value2);
4423 return gst_structure_is_subset (s1, s2);
4427 gst_value_is_subset_list_list (const GValue * value1, const GValue * value2)
4429 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
4430 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
4431 gint it1, len1, it2, len2;
4436 /* A list can't be a subset of a smaller list */
4440 /* Check if all elements of the first list are within the 2nd list */
4441 for (it1 = 0; it1 < len1; it1++) {
4442 const GValue *child1 = &vlist1->fields[it1];
4443 gboolean seen = FALSE;
4445 for (it2 = 0; it2 < len2; it2++) {
4446 const GValue *child2 = &vlist2->fields[it2];
4447 if (gst_value_compare (child1, child2) == GST_VALUE_EQUAL) {
4460 gst_value_is_subset_list (const GValue * value1, const GValue * value2)
4462 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
4467 /* Check whether value1 is within the list */
4468 for (it2 = 0; it2 < len2; it2++) {
4469 const GValue *child2 = &vlist2->fields[it2];
4470 if (gst_value_compare (value1, child2) == GST_VALUE_EQUAL) {
4479 * gst_value_is_subset:
4480 * @value1: a #GValue
4481 * @value2: a #GValue
4483 * Check that @value1 is a subset of @value2.
4485 * Return: %TRUE is @value1 is a subset of @value2
4488 gst_value_is_subset (const GValue * value1, const GValue * value2)
4490 GType type1 = G_VALUE_TYPE (value1);
4491 GType type2 = G_VALUE_TYPE (value2);
4493 /* special case for int/int64 ranges, since we cannot compute
4494 the difference for those when they have different steps,
4495 and it's actually a lot simpler to compute whether a range
4496 is a subset of another. */
4497 if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
4498 return gst_value_is_subset_int_range_int_range (value1, value2);
4499 } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
4500 && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
4501 return gst_value_is_subset_int64_range_int64_range (value1, value2);
4502 } else if (GST_VALUE_HOLDS_FLAG_SET (value1) &&
4503 GST_VALUE_HOLDS_FLAG_SET (value2)) {
4504 return gst_value_is_subset_flagset_flagset (value1, value2);
4505 } else if (GST_VALUE_HOLDS_STRUCTURE (value1)
4506 && GST_VALUE_HOLDS_STRUCTURE (value2)) {
4507 return gst_value_is_subset_structure_structure (value1, value2);
4508 } else if (type2 == GST_TYPE_LIST) {
4509 if (type1 == GST_TYPE_LIST)
4510 return gst_value_is_subset_list_list (value1, value2);
4511 return gst_value_is_subset_list (value1, value2);
4519 * -> 1 - [1,2] = empty
4523 * -> [1,2] - [1,3] = empty
4527 * -> {1,3} - {1,2} = 3
4530 * First caps subtraction needs to return a non-empty set, second
4531 * subtractions needs to give en empty set.
4532 * Both substractions are switched below, as it's faster that way.
4534 if (!gst_value_subtract (NULL, value1, value2)) {
4535 if (gst_value_subtract (NULL, value2, value1)) {
4547 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
4548 const GValue * src2)
4550 gint v = src1->data[0].v_int;
4552 /* check if it's already in the range */
4553 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
4554 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
4555 v % INT_RANGE_STEP (src2) == 0) {
4557 gst_value_init_and_copy (dest, src2);
4561 /* check if it extends the range */
4562 if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
4564 guint64 new_min = INT_RANGE_MIN (src2) - 1;
4565 guint64 new_max = INT_RANGE_MAX (src2);
4567 gst_value_init_and_copy (dest, src2);
4568 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4572 if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
4574 guint64 new_min = INT_RANGE_MIN (src2);
4575 guint64 new_max = INT_RANGE_MAX (src2) + 1;
4577 gst_value_init_and_copy (dest, src2);
4578 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4587 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
4588 const GValue * src2)
4590 /* We can union in several special cases:
4591 1 - one is a subset of another
4592 2 - same step and not disjoint
4593 3 - different step, at least one with one value which matches a 'next' or 'previous'
4598 if (gst_value_is_subset_int_range_int_range (src1, src2)) {
4600 gst_value_init_and_copy (dest, src2);
4603 if (gst_value_is_subset_int_range_int_range (src2, src1)) {
4605 gst_value_init_and_copy (dest, src1);
4609 /* 2 - same step and not disjoint */
4610 if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
4611 if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
4612 INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
4613 (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
4614 INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
4616 gint step = INT_RANGE_STEP (src1);
4617 gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
4618 gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
4619 g_value_init (dest, GST_TYPE_INT_RANGE);
4620 gst_value_set_int_range_step (dest, min, max, step);
4626 /* 3 - single value matches next or previous */
4627 if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
4628 gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
4629 gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
4630 if (n1 == 1 || n2 == 1) {
4631 const GValue *range_value = NULL;
4635 scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
4636 } else if (n2 == 1) {
4638 scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
4642 (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
4644 guint64 new_min = (guint)
4645 ((INT_RANGE_MIN (range_value) -
4646 1) * INT_RANGE_STEP (range_value));
4647 guint64 new_max = (guint)
4648 (INT_RANGE_MAX (range_value) * INT_RANGE_STEP (range_value));
4650 gst_value_init_and_copy (dest, range_value);
4651 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4654 } else if (scalar ==
4655 (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
4657 guint64 new_min = (guint)
4658 (INT_RANGE_MIN (range_value) * INT_RANGE_STEP (range_value));
4659 guint64 new_max = (guint)
4660 ((INT_RANGE_MAX (range_value) +
4661 1) * INT_RANGE_STEP (range_value));
4662 gst_value_init_and_copy (dest, range_value);
4663 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4670 /* If we get there, we did not find a way to make a union that can be
4671 represented with our simplistic model. */
4676 gst_value_union_flagset_flagset (GValue * dest, const GValue * src1,
4677 const GValue * src2)
4679 /* We can union 2 flag sets where they do not disagree on
4680 * required (masked) flag bits */
4684 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src1), FALSE);
4685 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src2), FALSE);
4687 f1 = src1->data[0].v_uint;
4688 f2 = src2->data[0].v_uint;
4690 m1 = src1->data[1].v_uint;
4691 m2 = src2->data[1].v_uint;
4693 /* Can't union if masked bits disagree */
4694 if ((f1 & (m1 & m2)) != (f2 & (m1 & m2)))
4698 g_value_init (dest, GST_TYPE_FLAG_SET);
4699 /* Copy masked bits from src2 to src1 */
4703 gst_value_set_flagset (dest, f1, m1);
4709 /* iterating over the result taking the union with the other structure's value */
4711 structure_field_union_into (GQuark field_id, GValue * val, gpointer user_data)
4713 GstStructure *other = user_data;
4714 const GValue *other_value;
4715 GValue res_value = G_VALUE_INIT;
4717 other_value = gst_structure_id_get_value (other, field_id);
4718 /* no value in the other struct, just keep this value */
4722 if (!gst_value_union (&res_value, val, other_value))
4725 g_value_unset (val);
4726 gst_value_move (val, &res_value);
4730 /* iterating over the other source structure adding missing values */
4732 structure_field_union_from (GQuark field_id, const GValue * other_val,
4735 GstStructure *result = user_data;
4736 const GValue *result_value;
4738 result_value = gst_structure_id_get_value (result, field_id);
4740 gst_structure_id_set_value (result, field_id, other_val);
4746 gst_value_union_structure_structure (GValue * dest, const GValue * src1,
4747 const GValue * src2)
4749 const GstStructure *s1, *s2;
4750 GstStructure *result;
4753 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (src1), FALSE);
4754 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (src2), FALSE);
4756 s1 = gst_value_get_structure (src1);
4757 s2 = gst_value_get_structure (src2);
4759 /* Can't join two structures with different names into a single structure */
4760 if (!gst_structure_has_name (s1, gst_structure_get_name (s2))) {
4761 gst_value_list_concat (dest, src1, src2);
4765 result = gst_structure_copy (s1);
4767 gst_structure_map_in_place (result, structure_field_union_into,
4772 gst_structure_foreach (s2, structure_field_union_from, (gpointer) result);
4775 g_value_init (dest, GST_TYPE_STRUCTURE);
4776 gst_value_set_structure (dest, result);
4780 gst_structure_free (result);
4789 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
4790 const GValue * src2)
4792 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
4793 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
4794 src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
4796 gst_value_init_and_copy (dest, src1);
4804 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
4805 const GValue * src2)
4812 INT_RANGE_STEP (src1) /
4813 gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
4814 INT_RANGE_STEP (src2));
4815 if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
4817 step *= INT_RANGE_STEP (src2);
4820 MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
4821 INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
4822 min = (min + step - 1) / step * step;
4824 MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
4825 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
4826 max = max / step * step;
4830 g_value_init (dest, GST_TYPE_INT_RANGE);
4831 gst_value_set_int_range_step (dest, min, max, step);
4837 g_value_init (dest, G_TYPE_INT);
4838 g_value_set_int (dest, min);
4846 #define INT64_RANGE_MIN_VAL(v) (INT64_RANGE_MIN (v) * INT64_RANGE_STEP (v))
4847 #define INT64_RANGE_MAX_VAL(v) (INT64_RANGE_MAX (v) * INT64_RANGE_STEP (v))
4850 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
4851 const GValue * src2)
4853 if (INT64_RANGE_MIN_VAL (src2) <= src1->data[0].v_int64 &&
4854 INT64_RANGE_MAX_VAL (src2) >= src1->data[0].v_int64 &&
4855 src1->data[0].v_int64 % INT64_RANGE_STEP (src2) == 0) {
4857 gst_value_init_and_copy (dest, src1);
4865 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
4866 const GValue * src2)
4873 INT64_RANGE_STEP (src1) /
4874 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
4875 INT64_RANGE_STEP (src2));
4876 if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
4878 step *= INT64_RANGE_STEP (src2);
4881 MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
4882 INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
4883 min = (min + step - 1) / step * step;
4885 MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
4886 INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
4887 max = max / step * step;
4891 g_value_init (dest, GST_TYPE_INT64_RANGE);
4892 gst_value_set_int64_range_step (dest, min, max, step);
4898 g_value_init (dest, G_TYPE_INT64);
4899 g_value_set_int64 (dest, min);
4908 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
4909 const GValue * src2)
4911 if (src2->data[0].v_double <= src1->data[0].v_double &&
4912 src2->data[1].v_double >= src1->data[0].v_double) {
4914 gst_value_init_and_copy (dest, src1);
4922 gst_value_intersect_double_range_double_range (GValue * dest,
4923 const GValue * src1, const GValue * src2)
4928 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
4929 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
4933 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
4934 gst_value_set_double_range (dest, min, max);
4940 g_value_init (dest, G_TYPE_DOUBLE);
4941 g_value_set_int (dest, (int) min);
4950 gst_value_intersect_list_list (GValue * dest, const GValue * value1,
4951 const GValue * value2)
4953 guint8 tmpfield[16]; /* Check up to 128 values */
4955 gboolean alloc_bitfield = FALSE;
4956 gboolean res = FALSE;
4959 guint it1, len1, start2, it2, len2, itar;
4960 GstValueList *vlist = NULL;
4962 /* If they don't have the same basic type, all bets are off :) */
4963 if (!gst_value_list_or_array_get_basic_type (value1, &type1) ||
4964 !gst_value_list_or_array_get_basic_type (value2, &type2) ||
4968 len1 = VALUE_LIST_SIZE (value1);
4969 len2 = VALUE_LIST_SIZE (value2);
4971 /* Fast-path with no dest (i.e. only interested in knowing whether
4972 * both lists intersected without wanting the result) */
4974 for (it1 = 0; it1 < len1; it1++) {
4975 const GValue *item1 = VALUE_LIST_GET_VALUE (value1, it1);
4976 for (it2 = 0; it2 < len2; it2++) {
4977 const GValue *item2 = VALUE_LIST_GET_VALUE (value2, it2);
4978 if (gst_value_intersect (NULL, item1, item2)) {
4986 #define is_visited(idx) (bitfield[(idx) >> 3] & (1 << ((idx) & 0x7)))
4987 #define mark_visited(idx) (bitfield[(idx) >> 3] |= (1 << ((idx) & 0x7)))
4989 /* Bitfield to avoid double-visiting */
4990 if (G_UNLIKELY (len2 > 128)) {
4991 alloc_bitfield = TRUE;
4992 bitfield = g_malloc0 ((len2 / 8) + 1);
4993 GST_CAT_LOG (GST_CAT_PERFORMANCE,
4994 "Allocation for GstValueList with more than 128 members");
4996 bitfield = &tmpfield[0];
4997 memset (bitfield, 0, 16);
5001 /* When doing list<->list intersections, there is a greater
5002 * probability of ending up with a list than with a single value.
5003 * Furthermore, the biggest that list can be will the smallest list
5004 * (i.e. intersects fully).
5005 * Therefore we pre-allocate a GstValueList of that size. */
5006 vlist = _gst_value_list_new (MIN (len1, len2));
5009 tmp = &vlist->fields[0];
5012 for (it1 = 0; it1 < len1; it1++) {
5013 const GValue *item1 = VALUE_LIST_GET_VALUE (value1, it1);
5014 for (it2 = start2; it2 < len2; it2++) {
5015 const GValue *item2;
5016 if (is_visited (it2))
5018 item2 = VALUE_LIST_GET_VALUE (value2, it2);
5020 if (gst_value_intersect (tmp, item1, item2)) {
5023 /* Increment our inner-loop starting point */
5027 /* Move our collection value */
5029 tmp = &vlist->fields[itar];
5032 /* We can stop iterating the second part now that we've matched */
5042 /* If we end up with a single value in the list, just use that
5043 * value. Else use the list */
5044 if (vlist->len == 1) {
5045 gst_value_move (dest, &vlist->fields[0]);
5048 dest->g_type = GST_TYPE_LIST;
5049 dest->data[0].v_pointer = vlist;
5062 gst_value_intersect_list (GValue * dest, const GValue * value1,
5063 const GValue * value2)
5066 GValue intersection = { 0, };
5067 gboolean ret = FALSE;
5069 /* Use optimized list-list intersection */
5070 if (G_VALUE_TYPE (value2) == GST_TYPE_LIST) {
5071 return gst_value_intersect_list_list (dest, value1, value2);
5074 size = VALUE_LIST_SIZE (value1);
5075 for (i = 0; i < size; i++) {
5076 const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
5078 /* quicker version when we don't need the resulting set */
5080 if (gst_value_intersect (NULL, cur, value2)) {
5087 if (gst_value_intersect (&intersection, cur, value2)) {
5090 gst_value_move (dest, &intersection);
5092 } else if (GST_VALUE_HOLDS_LIST (dest)) {
5093 _gst_value_list_append_and_take_value (dest, &intersection);
5097 gst_value_move (&temp, dest);
5098 gst_value_list_merge (dest, &temp, &intersection);
5099 g_value_unset (&temp);
5100 g_value_unset (&intersection);
5109 gst_value_intersect_array (GValue * dest, const GValue * src1,
5110 const GValue * src2)
5116 /* only works on similar-sized arrays */
5117 size = gst_value_array_get_size (src1);
5118 if (size != gst_value_array_get_size (src2))
5121 /* quicker value when we don't need the resulting set */
5123 for (n = 0; n < size; n++) {
5124 if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
5125 gst_value_array_get_value (src2, n))) {
5132 g_value_init (dest, GST_TYPE_ARRAY);
5134 for (n = 0; n < size; n++) {
5135 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
5136 gst_value_array_get_value (src2, n))) {
5137 g_value_unset (dest);
5140 _gst_value_array_append_and_take_value (dest, &val);
5147 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
5148 const GValue * src2)
5153 vals = src2->data[0].v_pointer;
5158 res1 = gst_value_compare_fraction (&vals[0], src1);
5159 res2 = gst_value_compare_fraction (&vals[1], src1);
5161 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
5162 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
5164 gst_value_init_and_copy (dest, src1);
5171 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
5172 const GValue * src1, const GValue * src2)
5177 GValue *vals1, *vals2;
5179 vals1 = src1->data[0].v_pointer;
5180 vals2 = src2->data[0].v_pointer;
5181 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
5183 /* min = MAX (src1.start, src2.start) */
5184 res = gst_value_compare_fraction (&vals1[0], &vals2[0]);
5185 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5186 if (res == GST_VALUE_LESS_THAN)
5187 min = &vals2[0]; /* Take the max of the 2 */
5191 /* max = MIN (src1.end, src2.end) */
5192 res = gst_value_compare_fraction (&vals1[1], &vals2[1]);
5193 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5194 if (res == GST_VALUE_GREATER_THAN)
5195 max = &vals2[1]; /* Take the min of the 2 */
5199 res = gst_value_compare_fraction (min, max);
5200 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5201 if (res == GST_VALUE_LESS_THAN) {
5203 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
5204 vals1 = dest->data[0].v_pointer;
5205 g_value_copy (min, &vals1[0]);
5206 g_value_copy (max, &vals1[1]);
5210 if (res == GST_VALUE_EQUAL) {
5212 gst_value_init_and_copy (dest, min);
5219 /* Two flagsets intersect if the masked bits in both
5220 * flagsets are exactly equal */
5222 gst_value_intersect_flagset_flagset (GValue * dest,
5223 const GValue * src1, const GValue * src2)
5227 GType type1, type2, flagset_type;
5229 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src1), FALSE);
5230 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src2), FALSE);
5232 f1 = src1->data[0].v_uint;
5233 f2 = src2->data[0].v_uint;
5235 m1 = src1->data[1].v_uint;
5236 m2 = src2->data[1].v_uint;
5238 /* Don't intersect if masked bits disagree */
5239 if ((f1 & (m1 & m2)) != (f2 & (m1 & m2)))
5242 /* Allow intersection with the generic FlagSet type, on one
5243 * side, but not 2 different subtypes - that makes no sense */
5244 type1 = G_VALUE_TYPE (src1);
5245 type2 = G_VALUE_TYPE (src2);
5246 flagset_type = GST_TYPE_FLAG_SET;
5248 if (type1 != type2 && type1 != flagset_type && type2 != flagset_type)
5254 /* Prefer an output type that matches a sub-type,
5255 * rather than the generic type */
5256 if (type1 != flagset_type)
5261 g_value_init (dest, dest_type);
5263 /* The compatible set is all the bits from src1 that it
5264 * cares about and all the bits from src2 that it cares
5266 dest->data[0].v_uint = (f1 & m1) | (f2 & m2);
5267 dest->data[1].v_uint = m1 | m2;
5274 gst_value_intersect_structure_structure (GValue * dest,
5275 const GValue * src1, const GValue * src2)
5277 const GstStructure *s1, *s2;
5280 s1 = gst_value_get_structure (src1);
5281 s2 = gst_value_get_structure (src2);
5283 d1 = gst_structure_intersect (s1, s2);
5288 g_value_init (dest, GST_TYPE_STRUCTURE);
5289 gst_value_set_structure (dest, d1);
5292 gst_structure_free (d1);
5301 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
5302 const GValue * subtrahend)
5304 gint min = gst_value_get_int_range_min (subtrahend);
5305 gint max = gst_value_get_int_range_max (subtrahend);
5306 gint step = gst_value_get_int_range_step (subtrahend);
5307 gint val = g_value_get_int (minuend);
5312 /* subtracting a range from an int only works if the int is not in the
5314 if (val < min || val > max || val % step) {
5315 /* and the result is the int */
5317 gst_value_init_and_copy (dest, minuend);
5323 /* creates a new int range based on input values.
5326 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
5327 gint max2, gint step)
5331 GValue *pv1, *pv2; /* yeah, hungarian! */
5333 g_return_val_if_fail (step > 0, FALSE);
5334 g_return_val_if_fail (min1 % step == 0, FALSE);
5335 g_return_val_if_fail (max1 % step == 0, FALSE);
5336 g_return_val_if_fail (min2 % step == 0, FALSE);
5337 g_return_val_if_fail (max2 % step == 0, FALSE);
5339 if (min1 <= max1 && min2 <= max2) {
5342 } else if (min1 <= max1) {
5345 } else if (min2 <= max2) {
5356 g_value_init (pv1, GST_TYPE_INT_RANGE);
5357 gst_value_set_int_range_step (pv1, min1, max1, step);
5358 } else if (min1 == max1) {
5359 g_value_init (pv1, G_TYPE_INT);
5360 g_value_set_int (pv1, min1);
5363 g_value_init (pv2, GST_TYPE_INT_RANGE);
5364 gst_value_set_int_range_step (pv2, min2, max2, step);
5365 } else if (min2 == max2) {
5366 g_value_init (pv2, G_TYPE_INT);
5367 g_value_set_int (pv2, min2);
5370 if (min1 <= max1 && min2 <= max2) {
5371 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5377 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
5378 const GValue * subtrahend)
5380 gint min = gst_value_get_int_range_min (minuend);
5381 gint max = gst_value_get_int_range_max (minuend);
5382 gint step = gst_value_get_int_range_step (minuend);
5383 gint val = g_value_get_int (subtrahend);
5385 g_return_val_if_fail (min < max, FALSE);
5390 /* value is outside of the range, return range unchanged */
5391 if (val < min || val > max || val % step) {
5393 gst_value_init_and_copy (dest, minuend);
5396 /* max must be MAXINT too as val <= max */
5397 if (val >= G_MAXINT - step + 1) {
5401 /* min must be MININT too as val >= max */
5402 if (val <= G_MININT + step - 1) {
5407 gst_value_create_new_range (dest, min, val - step, val + step, max, step);
5413 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
5414 const GValue * subtrahend)
5416 gint min1 = gst_value_get_int_range_min (minuend);
5417 gint max1 = gst_value_get_int_range_max (minuend);
5418 gint step1 = gst_value_get_int_range_step (minuend);
5419 gint min2 = gst_value_get_int_range_min (subtrahend);
5420 gint max2 = gst_value_get_int_range_max (subtrahend);
5421 gint step2 = gst_value_get_int_range_step (subtrahend);
5424 if (step1 != step2) {
5434 if (max2 >= max1 && min2 <= min1) {
5436 } else if (max2 >= max1) {
5437 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
5439 } else if (min2 <= min1) {
5440 return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
5443 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
5444 MAX (max2 + step, min1), max1, step);
5449 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
5450 const GValue * subtrahend)
5452 gint64 min = gst_value_get_int64_range_min (subtrahend);
5453 gint64 max = gst_value_get_int64_range_max (subtrahend);
5454 gint64 step = gst_value_get_int64_range_step (subtrahend);
5455 gint64 val = g_value_get_int64 (minuend);
5459 /* subtracting a range from an int64 only works if the int64 is not in the
5461 if (val < min || val > max || val % step) {
5462 /* and the result is the int64 */
5464 gst_value_init_and_copy (dest, minuend);
5470 /* creates a new int64 range based on input values.
5473 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
5474 gint64 min2, gint64 max2, gint64 step)
5478 GValue *pv1, *pv2; /* yeah, hungarian! */
5480 g_return_val_if_fail (step > 0, FALSE);
5481 g_return_val_if_fail (min1 % step == 0, FALSE);
5482 g_return_val_if_fail (max1 % step == 0, FALSE);
5483 g_return_val_if_fail (min2 % step == 0, FALSE);
5484 g_return_val_if_fail (max2 % step == 0, FALSE);
5486 if (min1 <= max1 && min2 <= max2) {
5489 } else if (min1 <= max1) {
5492 } else if (min2 <= max2) {
5503 g_value_init (pv1, GST_TYPE_INT64_RANGE);
5504 gst_value_set_int64_range_step (pv1, min1, max1, step);
5505 } else if (min1 == max1) {
5506 g_value_init (pv1, G_TYPE_INT64);
5507 g_value_set_int64 (pv1, min1);
5510 g_value_init (pv2, GST_TYPE_INT64_RANGE);
5511 gst_value_set_int64_range_step (pv2, min2, max2, step);
5512 } else if (min2 == max2) {
5513 g_value_init (pv2, G_TYPE_INT64);
5514 g_value_set_int64 (pv2, min2);
5517 if (min1 <= max1 && min2 <= max2) {
5518 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5524 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
5525 const GValue * subtrahend)
5527 gint64 min = gst_value_get_int64_range_min (minuend);
5528 gint64 max = gst_value_get_int64_range_max (minuend);
5529 gint64 step = gst_value_get_int64_range_step (minuend);
5530 gint64 val = g_value_get_int64 (subtrahend);
5532 g_return_val_if_fail (min < max, FALSE);
5537 /* value is outside of the range, return range unchanged */
5538 if (val < min || val > max || val % step) {
5540 gst_value_init_and_copy (dest, minuend);
5543 /* max must be MAXINT64 too as val <= max */
5544 if (val >= G_MAXINT64 - step + 1) {
5548 /* min must be MININT64 too as val >= max */
5549 if (val <= G_MININT64 + step - 1) {
5554 gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
5561 gst_value_subtract_int64_range_int64_range (GValue * dest,
5562 const GValue * minuend, const GValue * subtrahend)
5564 gint64 min1 = gst_value_get_int64_range_min (minuend);
5565 gint64 max1 = gst_value_get_int64_range_max (minuend);
5566 gint64 step1 = gst_value_get_int64_range_step (minuend);
5567 gint64 min2 = gst_value_get_int64_range_min (subtrahend);
5568 gint64 max2 = gst_value_get_int64_range_max (subtrahend);
5569 gint64 step2 = gst_value_get_int64_range_step (subtrahend);
5572 if (step1 != step2) {
5583 if (max2 >= max1 && min2 <= min1) {
5585 } else if (max2 >= max1) {
5586 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
5587 max1), step, 0, step);
5588 } else if (min2 <= min1) {
5589 return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
5590 max1, step, 0, step);
5592 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
5593 max1), MAX (max2 + step, min1), max1, step);
5598 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
5599 const GValue * subtrahend)
5601 gdouble min = gst_value_get_double_range_min (subtrahend);
5602 gdouble max = gst_value_get_double_range_max (subtrahend);
5603 gdouble val = g_value_get_double (minuend);
5605 if (val < min || val > max) {
5607 gst_value_init_and_copy (dest, minuend);
5614 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
5615 const GValue * subtrahend)
5617 /* since we don't have open ranges, we cannot create a hole in
5618 * a double range. We return the original range */
5620 gst_value_init_and_copy (dest, minuend);
5625 gst_value_subtract_double_range_double_range (GValue * dest,
5626 const GValue * minuend, const GValue * subtrahend)
5628 /* since we don't have open ranges, we have to approximate */
5629 /* done like with ints */
5630 gdouble min1 = gst_value_get_double_range_min (minuend);
5631 gdouble max2 = gst_value_get_double_range_max (minuend);
5632 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
5633 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
5636 GValue *pv1, *pv2; /* yeah, hungarian! */
5638 if (min1 < max1 && min2 < max2) {
5641 } else if (min1 < max1) {
5644 } else if (min2 < max2) {
5655 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
5656 gst_value_set_double_range (pv1, min1, max1);
5659 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
5660 gst_value_set_double_range (pv2, min2, max2);
5663 if (min1 < max1 && min2 < max2) {
5664 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5670 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
5671 const GValue * subtrahend)
5674 GValue subtraction = { 0, };
5675 gboolean ret = FALSE;
5677 size = VALUE_LIST_SIZE (minuend);
5678 for (i = 0; i < size; i++) {
5679 const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
5681 /* quicker version when we can discard the result */
5683 if (gst_value_subtract (NULL, cur, subtrahend)) {
5690 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
5692 gst_value_move (dest, &subtraction);
5694 } else if (G_VALUE_TYPE (dest) == GST_TYPE_LIST
5695 && G_VALUE_TYPE (&subtraction) != GST_TYPE_LIST) {
5696 _gst_value_list_append_and_take_value (dest, &subtraction);
5700 gst_value_move (&temp, dest);
5701 gst_value_list_concat_and_take_values (dest, &temp, &subtraction);
5709 gst_value_subtract_list (GValue * dest, const GValue * minuend,
5710 const GValue * subtrahend)
5713 GValue data[2] = { {0,}, {0,} };
5714 GValue *subtraction = &data[0], *result = &data[1];
5716 gst_value_init_and_copy (result, minuend);
5717 size = VALUE_LIST_SIZE (subtrahend);
5718 for (i = 0; i < size; i++) {
5719 const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
5721 if (gst_value_subtract (subtraction, result, cur)) {
5722 GValue *temp = result;
5724 result = subtraction;
5726 g_value_unset (subtraction);
5728 g_value_unset (result);
5733 gst_value_move (dest, result);
5735 g_value_unset (result);
5741 gst_value_subtract_fraction_fraction_range (GValue * dest,
5742 const GValue * minuend, const GValue * subtrahend)
5744 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
5745 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
5747 /* subtracting a range from an fraction only works if the fraction
5748 * is not in the range */
5749 if (gst_value_compare_fraction (minuend, min) == GST_VALUE_LESS_THAN ||
5750 gst_value_compare_fraction (minuend, max) == GST_VALUE_GREATER_THAN) {
5751 /* and the result is the value */
5753 gst_value_init_and_copy (dest, minuend);
5761 gst_value_subtract_fraction_range_fraction (GValue * dest,
5762 const GValue * minuend, const GValue * subtrahend)
5764 /* since we don't have open ranges, we cannot create a hole in
5765 * a range. We return the original range */
5767 gst_value_init_and_copy (dest, minuend);
5772 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
5773 const GValue * minuend, const GValue * subtrahend)
5775 /* since we don't have open ranges, we have to approximate */
5776 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
5777 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
5778 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
5779 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
5780 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
5784 GValue *pv1, *pv2; /* yeah, hungarian! */
5786 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
5787 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
5789 cmp1 = gst_value_compare_fraction (max2, max1);
5790 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
5791 if (cmp1 == GST_VALUE_LESS_THAN)
5793 cmp1 = gst_value_compare_fraction (min1, min2);
5794 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
5795 if (cmp1 == GST_VALUE_GREATER_THAN)
5798 cmp1 = gst_value_compare_fraction (min1, max1);
5799 cmp2 = gst_value_compare_fraction (min2, max2);
5801 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
5804 } else if (cmp1 == GST_VALUE_LESS_THAN) {
5807 } else if (cmp2 == GST_VALUE_LESS_THAN) {
5817 if (cmp1 == GST_VALUE_LESS_THAN) {
5818 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
5819 gst_value_set_fraction_range (pv1, min1, max1);
5821 if (cmp2 == GST_VALUE_LESS_THAN) {
5822 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
5823 gst_value_set_fraction_range (pv2, min2, max2);
5826 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
5827 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5837 * gst_value_get_compare_func:
5838 * @value1: a value to get the compare function for
5840 * Determines the compare function to be used with values of the same type as
5841 * @value1. The function can be given to gst_value_compare_with_func().
5843 * Returns: A #GstValueCompareFunc value
5845 static GstValueCompareFunc
5846 gst_value_get_compare_func (const GValue * value1)
5848 GstValueTable *table, *best = NULL;
5852 type1 = G_VALUE_TYPE (value1);
5854 /* this is a fast check */
5855 best = gst_value_hash_lookup_type (type1);
5858 if (G_UNLIKELY (!best || !best->compare)) {
5859 guint len = gst_value_table->len;
5862 for (i = 0; i < len; i++) {
5863 table = &g_array_index (gst_value_table, GstValueTable, i);
5864 if (table->compare && g_type_is_a (type1, table->type)) {
5865 if (!best || g_type_is_a (table->type, best->type))
5870 if (G_LIKELY (best))
5871 return best->compare;
5876 static inline gboolean
5877 gst_value_can_compare_unchecked (const GValue * value1, const GValue * value2)
5879 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
5882 return gst_value_get_compare_func (value1) != NULL;
5886 * gst_value_can_compare:
5887 * @value1: a value to compare
5888 * @value2: another value to compare
5890 * Determines if @value1 and @value2 can be compared.
5892 * Returns: %TRUE if the values can be compared
5895 gst_value_can_compare (const GValue * value1, const GValue * value2)
5897 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
5898 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
5900 return gst_value_can_compare_unchecked (value1, value2);
5904 gst_value_list_equals_range (const GValue * list, const GValue * value)
5906 const GValue *first;
5909 /* TODO: compare against an empty list ? No type though... */
5910 list_size = VALUE_LIST_SIZE (list);
5914 /* compare the basic types - they have to match */
5915 first = VALUE_LIST_GET_VALUE (list, 0);
5916 #define CHECK_TYPES(type,prefix) \
5917 ((first) && G_VALUE_TYPE(first) == prefix##_TYPE_##type && GST_VALUE_HOLDS_##type##_RANGE (value))
5918 if (CHECK_TYPES (INT, G)) {
5919 const gint rmin = gst_value_get_int_range_min (value);
5920 const gint rmax = gst_value_get_int_range_max (value);
5921 const gint rstep = gst_value_get_int_range_step (value);
5924 /* note: this will overflow for min 0 and max INT_MAX, but this
5925 would only be equal to a list of INT_MAX elements, which seems
5927 if (list_size != rmax / rstep - rmin / rstep + 1)
5929 for (n = 0; n < list_size; ++n) {
5930 gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
5931 if (v < rmin || v > rmax || v % rstep) {
5936 } else if (CHECK_TYPES (INT64, G)) {
5937 const gint64 rmin = gst_value_get_int64_range_min (value);
5938 const gint64 rmax = gst_value_get_int64_range_max (value);
5939 const gint64 rstep = gst_value_get_int64_range_step (value);
5940 GST_DEBUG ("List/range of int64s");
5943 if (list_size != rmax / rstep - rmin / rstep + 1)
5945 for (n = 0; n < list_size; ++n) {
5946 gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
5947 if (v < rmin || v > rmax || v % rstep)
5954 /* other combinations don't make sense for equality */
5958 /* "Pure" variant of gst_value_compare which is guaranteed to
5959 * not have list arguments and therefore does basic comparisons
5962 _gst_value_compare_nolist (const GValue * value1, const GValue * value2)
5964 GstValueCompareFunc compare;
5966 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
5967 return GST_VALUE_UNORDERED;
5969 compare = gst_value_get_compare_func (value1);
5971 return compare (value1, value2);
5974 g_critical ("unable to compare values of type %s\n",
5975 g_type_name (G_VALUE_TYPE (value1)));
5976 return GST_VALUE_UNORDERED;
5980 * gst_value_compare:
5981 * @value1: a value to compare
5982 * @value2: another value to compare
5984 * Compares @value1 and @value2. If @value1 and @value2 cannot be
5985 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
5986 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
5987 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
5988 * If the values are equal, GST_VALUE_EQUAL is returned.
5990 * Returns: comparison result
5993 gst_value_compare (const GValue * value1, const GValue * value2)
5995 gboolean value1_is_list;
5996 gboolean value2_is_list;
5998 g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
5999 g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
6001 value1_is_list = G_VALUE_TYPE (value1) == GST_TYPE_LIST;
6002 value2_is_list = G_VALUE_TYPE (value2) == GST_TYPE_LIST;
6004 /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
6005 as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
6006 if (value1_is_list && !value2_is_list) {
6009 if (gst_value_list_equals_range (value1, value2)) {
6010 return GST_VALUE_EQUAL;
6013 n = gst_value_list_get_size (value1);
6015 return GST_VALUE_UNORDERED;
6017 for (i = 0; i < n; i++) {
6020 elt = gst_value_list_get_value (value1, i);
6021 ret = gst_value_compare (elt, value2);
6022 if (ret != GST_VALUE_EQUAL && n == 1)
6024 else if (ret != GST_VALUE_EQUAL)
6025 return GST_VALUE_UNORDERED;
6028 return GST_VALUE_EQUAL;
6029 } else if (value2_is_list && !value1_is_list) {
6032 if (gst_value_list_equals_range (value2, value1)) {
6033 return GST_VALUE_EQUAL;
6036 n = gst_value_list_get_size (value2);
6038 return GST_VALUE_UNORDERED;
6040 for (i = 0; i < n; i++) {
6043 elt = gst_value_list_get_value (value2, i);
6044 ret = gst_value_compare (elt, value1);
6045 if (ret != GST_VALUE_EQUAL && n == 1)
6047 else if (ret != GST_VALUE_EQUAL)
6048 return GST_VALUE_UNORDERED;
6051 return GST_VALUE_EQUAL;
6054 /* And now handle the generic case */
6055 return _gst_value_compare_nolist (value1, value2);
6061 * gst_value_can_union:
6062 * @value1: a value to union
6063 * @value2: another value to union
6065 * Determines if @value1 and @value2 can be non-trivially unioned.
6066 * Any two values can be trivially unioned by adding both of them
6067 * to a GstValueList. However, certain types have the possibility
6068 * to be unioned in a simpler way. For example, an integer range
6069 * and an integer can be unioned if the integer is a subset of the
6070 * integer range. If there is the possibility that two values can
6071 * be unioned, this function returns %TRUE.
6073 * Returns: %TRUE if there is a function allowing the two values to
6077 gst_value_can_union (const GValue * value1, const GValue * value2)
6079 GstValueUnionInfo *union_info;
6082 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6083 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6085 len = gst_value_union_funcs->len;
6087 for (i = 0; i < len; i++) {
6088 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
6089 if (union_info->type1 == G_VALUE_TYPE (value1) &&
6090 union_info->type2 == G_VALUE_TYPE (value2))
6092 if (union_info->type1 == G_VALUE_TYPE (value2) &&
6093 union_info->type2 == G_VALUE_TYPE (value1))
6102 * @dest: (out caller-allocates): the destination value
6103 * @value1: a value to union
6104 * @value2: another value to union
6106 * Creates a GValue corresponding to the union of @value1 and @value2.
6108 * Returns: %TRUE if the union succeeded.
6111 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
6113 const GstValueUnionInfo *union_info;
6117 g_return_val_if_fail (dest != NULL, FALSE);
6118 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6119 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6120 g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
6123 len = gst_value_union_funcs->len;
6124 type1 = G_VALUE_TYPE (value1);
6125 type2 = G_VALUE_TYPE (value2);
6127 for (i = 0; i < len; i++) {
6128 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
6129 if (union_info->type1 == type1 && union_info->type2 == type2) {
6130 return union_info->func (dest, value1, value2);
6132 if (union_info->type1 == type2 && union_info->type2 == type1) {
6133 return union_info->func (dest, value2, value1);
6137 gst_value_list_concat (dest, value1, value2);
6141 /* gst_value_register_union_func: (skip)
6142 * @type1: a type to union
6143 * @type2: another type to union
6144 * @func: a function that implements creating a union between the two types
6146 * Registers a union function that can create a union between #GValue items
6147 * of the type @type1 and @type2.
6149 * Union functions should be registered at startup before any pipelines are
6150 * started, as gst_value_register_union_func() is not thread-safe and cannot
6151 * be used at the same time as gst_value_union() or gst_value_can_union().
6154 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
6156 GstValueUnionInfo union_info;
6158 union_info.type1 = type1;
6159 union_info.type2 = type2;
6160 union_info.func = func;
6162 g_array_append_val (gst_value_union_funcs, union_info);
6168 * gst_value_can_intersect:
6169 * @value1: a value to intersect
6170 * @value2: another value to intersect
6172 * Determines if intersecting two values will produce a valid result.
6173 * Two values will produce a valid intersection if they have the same
6176 * Returns: %TRUE if the values can intersect
6179 gst_value_can_intersect (const GValue * value1, const GValue * value2)
6181 GstValueIntersectInfo *intersect_info;
6185 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6186 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6188 type1 = G_VALUE_TYPE (value1);
6189 type2 = G_VALUE_TYPE (value2);
6191 /* practically all GstValue types have a compare function (_can_compare=TRUE)
6192 * GstStructure and GstCaps have not, but are intersectable */
6197 if (type1 == GST_TYPE_LIST || type2 == GST_TYPE_LIST)
6200 if (G_UNLIKELY (GST_VALUE_HOLDS_FLAG_SET (value1) &&
6201 GST_VALUE_HOLDS_FLAG_SET (value2))) {
6204 flagset_type = GST_TYPE_FLAG_SET;
6206 /* Allow intersection with the generic FlagSet type, on one
6207 * side, but not 2 different subtypes - that makes no sense */
6208 if (type1 == flagset_type || type2 == flagset_type)
6212 /* check registered intersect functions (only different gtype are checked at
6214 len = gst_value_intersect_funcs->len;
6215 for (i = 0; i < len; i++) {
6216 intersect_info = &g_array_index (gst_value_intersect_funcs,
6217 GstValueIntersectInfo, i);
6218 if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
6219 (intersect_info->type1 == type2 && intersect_info->type2 == type1))
6223 return gst_value_can_compare_unchecked (value1, value2);
6227 * gst_value_intersect:
6228 * @dest: (out caller-allocates) (transfer full) (allow-none):
6229 * a uninitialized #GValue that will hold the calculated
6230 * intersection value. May be %NULL if the resulting set if not
6232 * @value1: a value to intersect
6233 * @value2: another value to intersect
6235 * Calculates the intersection of two values. If the values have
6236 * a non-empty intersection, the value representing the intersection
6237 * is placed in @dest, unless %NULL. If the intersection is non-empty,
6238 * @dest is not modified.
6240 * Returns: %TRUE if the intersection is non-empty
6243 gst_value_intersect (GValue * dest, const GValue * value1,
6244 const GValue * value2)
6246 GstValueIntersectInfo *intersect_info;
6250 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6251 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6253 type1 = G_VALUE_TYPE (value1);
6254 type2 = G_VALUE_TYPE (value2);
6256 /* special cases first */
6257 if (type1 == GST_TYPE_LIST)
6258 return gst_value_intersect_list (dest, value1, value2);
6259 if (type2 == GST_TYPE_LIST)
6260 return gst_value_intersect_list (dest, value2, value1);
6262 if (_gst_value_compare_nolist (value1, value2) == GST_VALUE_EQUAL) {
6264 gst_value_init_and_copy (dest, value1);
6268 if (type1 == type2) {
6269 /* Equal type comparison */
6270 if (type1 == GST_TYPE_INT_RANGE)
6271 return gst_value_intersect_int_range_int_range (dest, value1, value2);
6272 if (type1 == GST_TYPE_INT64_RANGE)
6273 return gst_value_intersect_int64_range_int64_range (dest, value1, value2);
6274 if (type1 == GST_TYPE_DOUBLE_RANGE)
6275 return gst_value_intersect_double_range_double_range (dest, value1,
6277 if (type1 == GST_TYPE_ARRAY)
6278 return gst_value_intersect_array (dest, value1, value2);
6279 if (type1 == GST_TYPE_FRACTION_RANGE)
6280 return gst_value_intersect_fraction_range_fraction_range (dest, value1,
6282 if (type1 == GST_TYPE_FLAG_SET)
6283 return gst_value_intersect_flagset_flagset (dest, value1, value2);
6284 if (type1 == GST_TYPE_STRUCTURE)
6285 return gst_value_intersect_structure_structure (dest, value1, value2);
6287 /* Different type comparison */
6288 len = gst_value_intersect_funcs->len;
6289 for (i = 0; i < len; i++) {
6290 intersect_info = &g_array_index (gst_value_intersect_funcs,
6291 GstValueIntersectInfo, i);
6292 if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
6293 return intersect_info->func (dest, value1, value2);
6295 if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
6296 return intersect_info->func (dest, value2, value1);
6301 /* Failed to find a direct intersection, check if these are
6302 * GstFlagSet sub-types. */
6303 if (G_UNLIKELY (GST_VALUE_HOLDS_FLAG_SET (value1) &&
6304 GST_VALUE_HOLDS_FLAG_SET (value2))) {
6305 return gst_value_intersect_flagset_flagset (dest, value1, value2);
6313 /* gst_value_register_intersect_func: (skip)
6314 * @type1: the first type to intersect
6315 * @type2: the second type to intersect
6316 * @func: the intersection function
6318 * Registers a function that is called to calculate the intersection
6319 * of the values having the types @type1 and @type2.
6321 * Intersect functions should be registered at startup before any pipelines are
6322 * started, as gst_value_register_intersect_func() is not thread-safe and
6323 * cannot be used at the same time as gst_value_intersect() or
6324 * gst_value_can_intersect().
6327 gst_value_register_intersect_func (GType type1, GType type2,
6328 GstValueIntersectFunc func)
6330 GstValueIntersectInfo intersect_info;
6332 intersect_info.type1 = type1;
6333 intersect_info.type2 = type2;
6334 intersect_info.func = func;
6336 g_array_append_val (gst_value_intersect_funcs, intersect_info);
6343 * gst_value_subtract:
6344 * @dest: (out caller-allocates) (allow-none): the destination value
6345 * for the result if the subtraction is not empty. May be %NULL,
6346 * in which case the resulting set will not be computed, which can
6347 * give a fair speedup.
6348 * @minuend: the value to subtract from
6349 * @subtrahend: the value to subtract
6351 * Subtracts @subtrahend from @minuend and stores the result in @dest.
6352 * Note that this means subtraction as in sets, not as in mathematics.
6354 * Returns: %TRUE if the subtraction is not empty
6357 gst_value_subtract (GValue * dest, const GValue * minuend,
6358 const GValue * subtrahend)
6360 GstValueSubtractInfo *info;
6364 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
6365 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
6367 mtype = G_VALUE_TYPE (minuend);
6368 stype = G_VALUE_TYPE (subtrahend);
6370 /* special cases first */
6371 if (mtype == GST_TYPE_LIST)
6372 return gst_value_subtract_from_list (dest, minuend, subtrahend);
6373 if (stype == GST_TYPE_LIST)
6374 return gst_value_subtract_list (dest, minuend, subtrahend);
6376 len = gst_value_subtract_funcs->len;
6377 for (i = 0; i < len; i++) {
6378 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
6379 if (info->minuend == mtype && info->subtrahend == stype) {
6380 return info->func (dest, minuend, subtrahend);
6384 if (_gst_value_compare_nolist (minuend, subtrahend) != GST_VALUE_EQUAL) {
6386 gst_value_init_and_copy (dest, minuend);
6395 gst_value_subtract (GValue * dest, const GValue * minuend,
6396 const GValue * subtrahend)
6398 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
6400 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
6401 gst_value_serialize (subtrahend),
6402 ret ? gst_value_serialize (dest) : "---");
6408 * gst_value_can_subtract:
6409 * @minuend: the value to subtract from
6410 * @subtrahend: the value to subtract
6412 * Checks if it's possible to subtract @subtrahend from @minuend.
6414 * Returns: %TRUE if a subtraction is possible
6417 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
6419 GstValueSubtractInfo *info;
6423 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
6424 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
6426 mtype = G_VALUE_TYPE (minuend);
6427 stype = G_VALUE_TYPE (subtrahend);
6430 if (mtype == GST_TYPE_LIST || stype == GST_TYPE_LIST)
6432 if (mtype == GST_TYPE_STRUCTURE || stype == GST_TYPE_STRUCTURE)
6435 len = gst_value_subtract_funcs->len;
6436 for (i = 0; i < len; i++) {
6437 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
6438 if (info->minuend == mtype && info->subtrahend == stype)
6442 return gst_value_can_compare_unchecked (minuend, subtrahend);
6445 /* gst_value_register_subtract_func: (skip)
6446 * @minuend_type: type of the minuend
6447 * @subtrahend_type: type of the subtrahend
6448 * @func: function to use
6450 * Registers @func as a function capable of subtracting the values of
6451 * @subtrahend_type from values of @minuend_type.
6453 * Subtract functions should be registered at startup before any pipelines are
6454 * started, as gst_value_register_subtract_func() is not thread-safe and
6455 * cannot be used at the same time as gst_value_subtract().
6458 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
6459 GstValueSubtractFunc func)
6461 GstValueSubtractInfo info;
6463 g_return_if_fail (!gst_type_is_fixed (minuend_type)
6464 || !gst_type_is_fixed (subtrahend_type));
6466 info.minuend = minuend_type;
6467 info.subtrahend = subtrahend_type;
6470 g_array_append_val (gst_value_subtract_funcs, info);
6474 * gst_value_register:
6475 * @table: structure containing functions to register
6477 * Registers functions to perform calculations on #GValue items of a given
6478 * type. Each type can only be added once.
6481 gst_value_register (const GstValueTable * table)
6483 GstValueTable *found;
6485 g_return_if_fail (table != NULL);
6487 g_array_append_val (gst_value_table, *table);
6489 found = gst_value_hash_lookup_type (table->type);
6491 g_warning ("adding type %s multiple times", g_type_name (table->type));
6493 /* FIXME: we're not really doing the const justice, we assume the table is
6495 gst_value_hash_add_type (table->type, table);
6499 * gst_value_init_and_copy:
6500 * @dest: (out caller-allocates): the target value
6501 * @src: the source value
6503 * Initialises the target value to be of the same type as source and then copies
6504 * the contents from source to target.
6507 gst_value_init_and_copy (GValue * dest, const GValue * src)
6511 g_return_if_fail (G_IS_VALUE (src));
6512 g_return_if_fail (dest != NULL);
6514 type = G_VALUE_TYPE (src);
6515 /* We need to shortcut GstValueList/GstValueArray copying because:
6516 * * g_value_init would end up allocating something
6517 * * which g_value_copy would then free and re-alloc.
6519 * Instead directly call the copy */
6520 if (type == GST_TYPE_LIST || type == GST_TYPE_ARRAY) {
6521 dest->g_type = type;
6522 gst_value_copy_list_or_array (src, dest);
6526 g_value_init (dest, type);
6527 g_value_copy (src, dest);
6530 /* move src into dest and clear src */
6532 gst_value_move (GValue * dest, GValue * src)
6534 g_assert (G_IS_VALUE (src));
6535 g_assert (dest != NULL);
6538 memset (src, 0, sizeof (GValue));
6542 * gst_value_serialize:
6543 * @value: a #GValue to serialize
6545 * tries to transform the given @value into a string representation that allows
6546 * getting back this string later on using gst_value_deserialize().
6548 * Free-function: g_free
6550 * Returns: (transfer full) (nullable): the serialization for @value
6551 * or %NULL if none exists
6554 gst_value_serialize (const GValue * value)
6557 GValue s_val = { 0 };
6558 GstValueTable *table, *best;
6562 g_return_val_if_fail (G_IS_VALUE (value), NULL);
6564 type = G_VALUE_TYPE (value);
6566 best = gst_value_hash_lookup_type (type);
6568 if (G_UNLIKELY (!best || !best->serialize)) {
6569 len = gst_value_table->len;
6571 for (i = 0; i < len; i++) {
6572 table = &g_array_index (gst_value_table, GstValueTable, i);
6573 if (table->serialize && g_type_is_a (type, table->type)) {
6574 if (!best || g_type_is_a (table->type, best->type))
6579 if (G_LIKELY (best))
6580 return best->serialize (value);
6582 g_value_init (&s_val, G_TYPE_STRING);
6583 if (g_value_transform (value, &s_val)) {
6584 s = gst_string_wrap (g_value_get_string (&s_val));
6588 g_value_unset (&s_val);
6594 * gst_value_deserialize:
6595 * @dest: (out caller-allocates): #GValue to fill with contents of
6597 * @src: string to deserialize
6599 * Tries to deserialize a string into the type specified by the given GValue.
6600 * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
6602 * Returns: %TRUE on success
6605 gst_value_deserialize (GValue * dest, const gchar * src)
6607 GstValueTable *table, *best;
6611 g_return_val_if_fail (src != NULL, FALSE);
6612 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
6614 type = G_VALUE_TYPE (dest);
6616 best = gst_value_hash_lookup_type (type);
6617 if (G_UNLIKELY (!best || (!best->deserialize
6618 && !best->deserialize_with_pspec))) {
6619 len = gst_value_table->len;
6621 for (i = 0; i < len; i++) {
6622 table = &g_array_index (gst_value_table, GstValueTable, i);
6623 if ((table->deserialize || table->deserialize_with_pspec) &&
6624 g_type_is_a (type, table->type)) {
6625 if (!best || g_type_is_a (table->type, best->type))
6630 if (G_LIKELY (best)) {
6631 if (best->deserialize_with_pspec)
6632 return best->deserialize_with_pspec (dest, src, NULL);
6634 return best->deserialize (dest, src);
6641 * gst_value_deserialize_with_pspec:
6642 * @dest: (out caller-allocates): #GValue to fill with contents of
6644 * @src: string to deserialize
6645 * @pspec: (nullable): the #GParamSpec describing the expected value
6647 * Tries to deserialize a string into the type specified by the given GValue.
6648 * @pspec may be used to guide the deserializing of nested members.
6649 * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
6651 * Returns: %TRUE on success
6655 gst_value_deserialize_with_pspec (GValue * dest, const gchar * src,
6658 GstValueTable *table, *best;
6662 g_return_val_if_fail (src != NULL, FALSE);
6663 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
6666 g_return_val_if_fail (G_VALUE_TYPE (dest) ==
6667 G_PARAM_SPEC_VALUE_TYPE (pspec), FALSE);
6669 type = G_VALUE_TYPE (dest);
6671 best = gst_value_hash_lookup_type (type);
6672 if (G_UNLIKELY (!best || (!best->deserialize
6673 && !best->deserialize_with_pspec))) {
6674 len = gst_value_table->len;
6676 for (i = 0; i < len; i++) {
6677 table = &g_array_index (gst_value_table, GstValueTable, i);
6678 if ((table->deserialize || table->deserialize_with_pspec) &&
6679 g_type_is_a (type, table->type)) {
6680 if (!best || g_type_is_a (table->type, best->type))
6685 if (G_LIKELY (best)) {
6686 if (best->deserialize_with_pspec)
6687 return best->deserialize_with_pspec (dest, src, pspec);
6689 return best->deserialize (dest, src);
6696 structure_field_is_fixed (GQuark field_id, const GValue * val,
6699 return gst_value_is_fixed (val);
6703 * gst_value_is_fixed:
6704 * @value: the #GValue to check
6706 * Tests if the given GValue, if available in a GstStructure (or any other
6707 * container) contains a "fixed" (which means: one value) or an "unfixed"
6708 * (which means: multiple possible values, such as data lists or data
6711 * Returns: true if the value is "fixed".
6715 gst_value_is_fixed (const GValue * value)
6719 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
6721 type = G_VALUE_TYPE (value);
6723 /* the most common types are just basic plain glib types */
6724 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
6728 if (type == GST_TYPE_ARRAY) {
6732 /* check recursively */
6733 size = gst_value_array_get_size (value);
6734 for (n = 0; n < size; n++) {
6735 kid = gst_value_array_get_value (value, n);
6736 if (!gst_value_is_fixed (kid))
6740 } else if (GST_VALUE_HOLDS_FLAG_SET (value)) {
6741 /* Flagsets are only fixed if there are no 'don't care' bits */
6742 return (gst_value_get_flagset_mask (value) == GST_FLAG_SET_MASK_EXACT);
6743 } else if (GST_VALUE_HOLDS_STRUCTURE (value)) {
6744 return gst_structure_foreach (gst_value_get_structure (value),
6745 structure_field_is_fixed, NULL);
6747 return gst_type_is_fixed (type);
6752 * @dest: the #GValue destination
6753 * @src: the #GValue to fixate
6755 * Fixate @src into a new value @dest.
6756 * For ranges, the first element is taken. For lists and arrays, the
6757 * first item is fixated and returned.
6758 * If @src is already fixed, this function returns %FALSE.
6760 * Returns: %TRUE if @dest contains a fixated version of @src.
6763 gst_value_fixate (GValue * dest, const GValue * src)
6765 g_return_val_if_fail (G_IS_VALUE (src), FALSE);
6766 g_return_val_if_fail (dest != NULL, FALSE);
6768 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
6769 g_value_init (dest, G_TYPE_INT);
6770 g_value_set_int (dest, gst_value_get_int_range_min (src));
6771 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
6772 g_value_init (dest, G_TYPE_DOUBLE);
6773 g_value_set_double (dest, gst_value_get_double_range_min (src));
6774 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
6775 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
6776 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
6777 GValue temp = { 0 };
6779 /* list could be empty */
6780 if (gst_value_list_get_size (src) <= 0)
6783 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
6785 if (!gst_value_fixate (dest, &temp)) {
6786 gst_value_move (dest, &temp);
6788 g_value_unset (&temp);
6790 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
6791 gboolean res = FALSE;
6794 len = gst_value_array_get_size (src);
6795 g_value_init (dest, GST_TYPE_ARRAY);
6796 for (n = 0; n < len; n++) {
6798 const GValue *orig_kid = gst_value_array_get_value (src, n);
6800 if (!gst_value_fixate (&kid, orig_kid))
6801 gst_value_init_and_copy (&kid, orig_kid);
6804 _gst_value_array_append_and_take_value (dest, &kid);
6808 g_value_unset (dest);
6811 } else if (GST_VALUE_HOLDS_FLAG_SET (src)) {
6814 if (gst_value_get_flagset_mask (src) == GST_FLAG_SET_MASK_EXACT)
6815 return FALSE; /* Already fixed */
6817 flags = gst_value_get_flagset_flags (src);
6818 g_value_init (dest, G_VALUE_TYPE (src));
6819 gst_value_set_flagset (dest, flags, GST_FLAG_SET_MASK_EXACT);
6821 } else if (GST_VALUE_HOLDS_STRUCTURE (src)) {
6822 const GstStructure *str = (GstStructure *) gst_value_get_structure (src);
6828 kid = gst_structure_copy (str);
6829 gst_structure_fixate (kid);
6830 g_value_init (dest, GST_TYPE_STRUCTURE);
6831 gst_value_set_structure (dest, kid);
6832 gst_structure_free (kid);
6845 /* helper functions */
6847 gst_value_init_fraction (GValue * value)
6849 value->data[0].v_int = 0;
6850 value->data[1].v_int = 1;
6854 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
6856 dest_value->data[0].v_int = src_value->data[0].v_int;
6857 dest_value->data[1].v_int = src_value->data[1].v_int;
6861 gst_value_collect_fraction (GValue * value, guint n_collect_values,
6862 GTypeCValue * collect_values, guint collect_flags)
6864 g_return_val_if_fail (n_collect_values == 2,
6865 g_strdup_printf ("not enough value locations for `%s' passed",
6866 G_VALUE_TYPE_NAME (value)));
6867 g_return_val_if_fail (collect_values[1].v_int != 0,
6868 g_strdup_printf ("passed '0' as denominator for `%s'",
6869 G_VALUE_TYPE_NAME (value)));
6870 g_return_val_if_fail (collect_values[0].v_int >= -G_MAXINT,
6872 ("passed value smaller than -G_MAXINT as numerator for `%s'",
6873 G_VALUE_TYPE_NAME (value)));
6874 g_return_val_if_fail (collect_values[1].v_int >= -G_MAXINT,
6876 ("passed value smaller than -G_MAXINT as denominator for `%s'",
6877 G_VALUE_TYPE_NAME (value)));
6879 gst_value_set_fraction (value,
6880 collect_values[0].v_int, collect_values[1].v_int);
6886 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
6887 GTypeCValue * collect_values, guint collect_flags)
6889 gint *numerator = collect_values[0].v_pointer;
6890 gint *denominator = collect_values[1].v_pointer;
6892 g_return_val_if_fail (numerator != NULL,
6893 g_strdup_printf ("numerator for `%s' passed as NULL",
6894 G_VALUE_TYPE_NAME (value)));
6895 g_return_val_if_fail (denominator != NULL,
6896 g_strdup_printf ("denominator for `%s' passed as NULL",
6897 G_VALUE_TYPE_NAME (value)));
6899 *numerator = value->data[0].v_int;
6900 *denominator = value->data[1].v_int;
6906 * gst_value_set_fraction:
6907 * @value: a GValue initialized to #GST_TYPE_FRACTION
6908 * @numerator: the numerator of the fraction
6909 * @denominator: the denominator of the fraction
6911 * Sets @value to the fraction specified by @numerator over @denominator.
6912 * The fraction gets reduced to the smallest numerator and denominator,
6913 * and if necessary the sign is moved to the numerator.
6916 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
6920 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
6921 g_return_if_fail (denominator != 0);
6922 g_return_if_fail (denominator >= -G_MAXINT);
6923 g_return_if_fail (numerator >= -G_MAXINT);
6925 /* normalize sign */
6926 if (denominator < 0) {
6927 numerator = -numerator;
6928 denominator = -denominator;
6931 /* check for reduction */
6932 gcd = gst_util_greatest_common_divisor (numerator, denominator);
6938 g_assert (denominator > 0);
6940 value->data[0].v_int = numerator;
6941 value->data[1].v_int = denominator;
6945 * gst_value_get_fraction_numerator:
6946 * @value: a GValue initialized to #GST_TYPE_FRACTION
6948 * Gets the numerator of the fraction specified by @value.
6950 * Returns: the numerator of the fraction.
6953 gst_value_get_fraction_numerator (const GValue * value)
6955 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
6957 return value->data[0].v_int;
6961 * gst_value_get_fraction_denominator:
6962 * @value: a GValue initialized to #GST_TYPE_FRACTION
6964 * Gets the denominator of the fraction specified by @value.
6966 * Returns: the denominator of the fraction.
6969 gst_value_get_fraction_denominator (const GValue * value)
6971 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
6973 return value->data[1].v_int;
6977 * gst_value_fraction_multiply:
6978 * @product: a GValue initialized to #GST_TYPE_FRACTION
6979 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
6980 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
6982 * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
6983 * @product to the product of the two fractions.
6985 * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
6988 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
6989 const GValue * factor2)
6991 gint n1, n2, d1, d2;
6994 g_return_val_if_fail (product != NULL, FALSE);
6995 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
6996 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
6998 n1 = factor1->data[0].v_int;
6999 n2 = factor2->data[0].v_int;
7000 d1 = factor1->data[1].v_int;
7001 d2 = factor2->data[1].v_int;
7003 if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
7006 gst_value_set_fraction (product, res_n, res_d);
7012 * gst_value_fraction_subtract:
7013 * @dest: a GValue initialized to #GST_TYPE_FRACTION
7014 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
7015 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
7017 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
7019 * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
7022 gst_value_fraction_subtract (GValue * dest,
7023 const GValue * minuend, const GValue * subtrahend)
7025 gint n1, n2, d1, d2;
7028 g_return_val_if_fail (dest != NULL, FALSE);
7029 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
7030 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
7032 n1 = minuend->data[0].v_int;
7033 n2 = subtrahend->data[0].v_int;
7034 d1 = minuend->data[1].v_int;
7035 d2 = subtrahend->data[1].v_int;
7037 if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
7039 gst_value_set_fraction (dest, res_n, res_d);
7045 gst_value_serialize_fraction (const GValue * value)
7047 gint32 numerator = value->data[0].v_int;
7048 gint32 denominator = value->data[1].v_int;
7049 gboolean positive = TRUE;
7051 /* get the sign and make components absolute */
7052 if (numerator < 0) {
7053 numerator = -numerator;
7054 positive = !positive;
7056 if (denominator < 0) {
7057 denominator = -denominator;
7058 positive = !positive;
7061 return g_strdup_printf ("%s%d/%d",
7062 positive ? "" : "-", numerator, denominator);
7066 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
7071 if (G_UNLIKELY (s == NULL))
7074 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
7077 if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
7078 if (s[num_chars] != 0)
7083 gst_value_set_fraction (dest, num, den);
7085 } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
7086 gst_value_set_fraction (dest, 1, G_MAXINT);
7088 } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
7089 if (s[num_chars] != 0)
7091 gst_value_set_fraction (dest, num, 1);
7093 } else if (g_ascii_strcasecmp (s, "min") == 0) {
7094 gst_value_set_fraction (dest, -G_MAXINT, 1);
7096 } else if (g_ascii_strcasecmp (s, "max") == 0) {
7097 gst_value_set_fraction (dest, G_MAXINT, 1);
7105 gst_value_transform_fraction_string (const GValue * src_value,
7106 GValue * dest_value)
7108 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
7112 gst_value_transform_string_fraction (const GValue * src_value,
7113 GValue * dest_value)
7115 if (!gst_value_deserialize_fraction (dest_value,
7116 src_value->data[0].v_pointer))
7117 /* If the deserialize fails, ensure we leave the fraction in a
7118 * valid, if incorrect, state */
7119 gst_value_set_fraction (dest_value, 0, 1);
7123 gst_value_transform_double_fraction (const GValue * src_value,
7124 GValue * dest_value)
7126 gdouble src = g_value_get_double (src_value);
7129 gst_util_double_to_fraction (src, &n, &d);
7130 gst_value_set_fraction (dest_value, n, d);
7134 gst_value_transform_float_fraction (const GValue * src_value,
7135 GValue * dest_value)
7137 gfloat src = g_value_get_float (src_value);
7140 gst_util_double_to_fraction (src, &n, &d);
7141 gst_value_set_fraction (dest_value, n, d);
7145 gst_value_transform_fraction_double (const GValue * src_value,
7146 GValue * dest_value)
7148 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
7149 ((double) src_value->data[1].v_int);
7153 gst_value_transform_fraction_float (const GValue * src_value,
7154 GValue * dest_value)
7156 dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
7157 ((float) src_value->data[1].v_int);
7161 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
7167 n1 = value1->data[0].v_int;
7168 n2 = value2->data[0].v_int;
7169 d1 = value1->data[1].v_int;
7170 d2 = value2->data[1].v_int;
7172 /* fractions are reduced when set, so we can quickly see if they're equal */
7173 if (n1 == n2 && d1 == d2)
7174 return GST_VALUE_EQUAL;
7176 if (d1 == 0 && d2 == 0)
7177 return GST_VALUE_UNORDERED;
7179 return GST_VALUE_GREATER_THAN;
7181 return GST_VALUE_LESS_THAN;
7183 ret = gst_util_fraction_compare (n1, d1, n2, d2);
7185 return GST_VALUE_LESS_THAN;
7187 return GST_VALUE_GREATER_THAN;
7189 /* Equality can't happen here because we check for that
7191 g_return_val_if_reached (GST_VALUE_UNORDERED);
7199 gst_value_compare_date (const GValue * value1, const GValue * value2)
7201 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
7202 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
7206 return GST_VALUE_EQUAL;
7208 if ((date1 == NULL || !g_date_valid (date1))
7209 && (date2 != NULL && g_date_valid (date2))) {
7210 return GST_VALUE_LESS_THAN;
7213 if ((date2 == NULL || !g_date_valid (date2))
7214 && (date1 != NULL && g_date_valid (date1))) {
7215 return GST_VALUE_GREATER_THAN;
7218 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
7219 || !g_date_valid (date2)) {
7220 return GST_VALUE_UNORDERED;
7223 j1 = g_date_get_julian (date1);
7224 j2 = g_date_get_julian (date2);
7227 return GST_VALUE_EQUAL;
7229 return GST_VALUE_LESS_THAN;
7231 return GST_VALUE_GREATER_THAN;
7235 gst_value_serialize_date (const GValue * val)
7237 const GDate *date = (const GDate *) g_value_get_boxed (val);
7239 if (date == NULL || !g_date_valid (date))
7240 return g_strdup ("9999-99-99");
7242 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
7243 g_date_get_month (date), g_date_get_day (date));
7247 gst_value_deserialize_date (GValue * dest, const gchar * s)
7249 guint year, month, day;
7251 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
7254 if (!g_date_valid_dmy (day, month, year))
7257 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
7266 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
7268 const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
7269 const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
7272 return GST_VALUE_EQUAL;
7274 if ((date1 == NULL) && (date2 != NULL)) {
7275 return GST_VALUE_LESS_THAN;
7277 if ((date2 == NULL) && (date1 != NULL)) {
7278 return GST_VALUE_LESS_THAN;
7281 /* returns GST_VALUE_* */
7282 return __gst_date_time_compare (date1, date2);
7286 gst_value_serialize_date_time (const GValue * val)
7288 GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
7291 return g_strdup ("null");
7293 return __gst_date_time_serialize (date, TRUE);
7297 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
7299 GstDateTime *datetime;
7301 if (!s || strcmp (s, "null") == 0) {
7305 datetime = gst_date_time_new_from_iso8601_string (s);
7306 if (datetime != NULL) {
7307 g_value_take_boxed (dest, datetime);
7310 GST_WARNING ("Failed to deserialize date time string '%s'", s);
7315 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
7317 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
7321 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
7323 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
7332 gst_value_compare_bytes (const GValue * value1, const GValue * value2)
7334 GBytes *bytes1 = (GBytes *) g_value_get_boxed (value1);
7335 GBytes *bytes2 = (GBytes *) g_value_get_boxed (value2);
7337 if (G_UNLIKELY (!bytes1 || !bytes2)) {
7338 if (bytes1 == bytes2)
7339 return GST_VALUE_EQUAL;
7341 return GST_VALUE_UNORDERED;
7344 return g_bytes_compare (bytes1, bytes2);
7348 gst_value_serialize_bytes (const GValue * value)
7350 GBytes *bytes = (GBytes *) g_value_get_boxed (value);
7354 data = bytes ? g_bytes_get_data (bytes, &len) : NULL;
7356 return g_base64_encode (data, len);
7360 gst_value_deserialize_bytes (GValue * dest, const gchar * s)
7366 g_value_set_boxed (dest, g_bytes_new (NULL, 0));
7370 data = g_base64_decode (s, &len);
7371 g_value_set_boxed (dest, g_bytes_new_take (data, len));
7380 /* helper functions */
7382 gst_value_init_bitmask (GValue * value)
7384 value->data[0].v_uint64 = 0;
7388 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
7390 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7394 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
7395 GTypeCValue * collect_values, guint collect_flags)
7397 g_return_val_if_fail (n_collect_values == 1,
7398 g_strdup_printf ("not enough value locations for `%s' passed",
7399 G_VALUE_TYPE_NAME (value)));
7401 gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
7407 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
7408 GTypeCValue * collect_values, guint collect_flags)
7410 guint64 *bitmask = collect_values[0].v_pointer;
7412 g_return_val_if_fail (bitmask != NULL,
7413 g_strdup_printf ("value for `%s' passed as NULL",
7414 G_VALUE_TYPE_NAME (value)));
7416 *bitmask = value->data[0].v_uint64;
7422 * gst_value_set_bitmask:
7423 * @value: a GValue initialized to #GST_TYPE_BITMASK
7424 * @bitmask: the bitmask
7426 * Sets @value to the bitmask specified by @bitmask.
7429 gst_value_set_bitmask (GValue * value, guint64 bitmask)
7431 g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
7433 value->data[0].v_uint64 = bitmask;
7437 * gst_value_get_bitmask:
7438 * @value: a GValue initialized to #GST_TYPE_BITMASK
7440 * Gets the bitmask specified by @value.
7442 * Returns: the bitmask.
7445 gst_value_get_bitmask (const GValue * value)
7447 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
7449 return value->data[0].v_uint64;
7453 gst_value_serialize_bitmask (const GValue * value)
7455 guint64 bitmask = value->data[0].v_uint64;
7457 return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
7461 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
7463 gchar *endptr = NULL;
7466 if (G_UNLIKELY (s == NULL))
7469 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
7473 val = g_ascii_strtoull (s, &endptr, 16);
7474 if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
7476 if (val == 0 && endptr == s)
7479 gst_value_set_bitmask (dest, val);
7485 gst_value_transform_bitmask_string (const GValue * src_value,
7486 GValue * dest_value)
7488 dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
7492 gst_value_transform_string_bitmask (const GValue * src_value,
7493 GValue * dest_value)
7495 if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
7496 gst_value_set_bitmask (dest_value, 0);
7500 gst_value_transform_uint64_bitmask (const GValue * src_value,
7501 GValue * dest_value)
7503 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7507 gst_value_transform_bitmask_uint64 (const GValue * src_value,
7508 GValue * dest_value)
7510 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7514 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
7518 v1 = value1->data[0].v_uint64;
7519 v2 = value2->data[0].v_uint64;
7522 return GST_VALUE_EQUAL;
7524 return GST_VALUE_UNORDERED;
7531 /* helper functions */
7533 gst_value_init_flagset (GValue * value)
7535 value->data[0].v_uint = 0;
7536 value->data[1].v_uint = 0;
7540 gst_value_copy_flagset (const GValue * src_value, GValue * dest_value)
7542 dest_value->data[0].v_uint = src_value->data[0].v_uint;
7543 dest_value->data[1].v_uint = src_value->data[1].v_uint;
7547 gst_value_collect_flagset (GValue * value, guint n_collect_values,
7548 GTypeCValue * collect_values, guint collect_flags)
7550 g_return_val_if_fail (n_collect_values == 2,
7551 g_strdup_printf ("not enough value locations for `%s' passed",
7552 G_VALUE_TYPE_NAME (value)));
7554 gst_value_set_flagset (value,
7555 (guint) collect_values[0].v_int, (guint) collect_values[1].v_int);
7561 gst_value_lcopy_flagset (const GValue * value, guint n_collect_values,
7562 GTypeCValue * collect_values, guint collect_flags)
7564 guint *flags = collect_values[0].v_pointer;
7565 guint *mask = collect_values[1].v_pointer;
7567 *flags = value->data[0].v_uint;
7568 *mask = value->data[1].v_uint;
7574 * gst_value_set_flagset:
7575 * @value: a GValue initialized to %GST_TYPE_FLAG_SET
7576 * @flags: The value of the flags set or unset
7577 * @mask: The mask indicate which flags bits must match for comparisons
7579 * Sets @value to the flags and mask values provided in @flags and @mask.
7580 * The @flags value indicates the values of flags, the @mask represents
7581 * which bits in the flag value have been set, and which are "don't care"
7586 gst_value_set_flagset (GValue * value, guint flags, guint mask)
7588 g_return_if_fail (GST_VALUE_HOLDS_FLAG_SET (value));
7590 /* Normalise and only keep flags mentioned in the mask */
7591 value->data[0].v_uint = flags & mask;
7592 value->data[1].v_uint = mask;
7596 * gst_value_get_flagset_flags:
7597 * @value: a GValue initialized to #GST_TYPE_FLAG_SET
7599 * Retrieve the flags field of a GstFlagSet @value.
7601 * Returns: the flags field of the flagset instance.
7606 gst_value_get_flagset_flags (const GValue * value)
7608 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value), 0);
7610 return value->data[0].v_uint;
7614 * gst_value_get_flagset_mask:
7615 * @value: a GValue initialized to #GST_TYPE_FLAG_SET
7617 * Retrieve the mask field of a GstFlagSet @value.
7619 * Returns: the mask field of the flagset instance.
7624 gst_value_get_flagset_mask (const GValue * value)
7626 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value), 1);
7628 return value->data[1].v_uint;
7632 gst_value_serialize_flagset (const GValue * value)
7634 guint flags = value->data[0].v_uint;
7635 guint mask = value->data[1].v_uint;
7636 GstFlagSetClass *set_klass =
7637 (GstFlagSetClass *) g_type_class_ref (G_VALUE_TYPE (value));
7640 result = g_strdup_printf ("%x:%x", flags, mask);
7642 /* If this flag set class has an associated GFlags GType, and some
7643 * bits in the mask, serialize the bits in human-readable form to
7645 if (mask && set_klass->flags_type) {
7646 GFlagsClass *flags_klass =
7647 (GFlagsClass *) (g_type_class_ref (set_klass->flags_type));
7650 gboolean first = TRUE;
7652 g_return_val_if_fail (flags_klass, NULL);
7654 /* some bits in the mask are set, so serialize one by one, according
7655 * to whether that bit is set or cleared in the flags value */
7657 fl = g_flags_get_first_value (flags_klass, mask);
7659 /* No more bits match in the flags mask - time to stop */
7664 tmp = g_strconcat (result,
7666 (flags & fl->value) ? "+" : "/", fl->value_nick, NULL);
7674 g_type_class_unref (flags_klass);
7677 g_type_class_unref (set_klass);
7683 is_valid_flags_string (const gchar * s)
7685 /* We're looking to match +this/that+other-thing/not-this-thing type strings */
7686 return g_regex_match_simple ("^([\\+\\/][\\w\\d-]+)+$", s, G_REGEX_CASELESS,
7691 gst_value_deserialize_flagset (GValue * dest, const gchar * s)
7693 gboolean res = FALSE;
7697 if (G_UNLIKELY (s == NULL))
7700 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FLAG_SET (dest)))
7703 /* Flagset strings look like %x:%x - hex flags : hex bitmask,
7704 * 32-bit each, or like a concatenated list of flag nicks,
7705 * with either '+' or '/' in front. The first form
7706 * may optionally be followed by ':' and a set of text flag descriptions
7707 * for easier debugging */
7709 /* Try and interpret as hex form first, as it's the most efficient */
7710 /* Read the flags first */
7711 flags = strtoul (s, &next, 16);
7712 if (G_UNLIKELY ((flags == 0 && errno == EINVAL) || s == next))
7713 goto try_as_flags_string;
7714 /* Next char should be a colon */
7720 mask = strtoul (cur, &next, 16);
7721 if (G_UNLIKELY ((mask == 0 && errno == EINVAL) || cur == next))
7722 goto try_as_flags_string;
7724 /* Next char should be NULL terminator, or a ':'. If ':', we need the flag string after */
7725 if (G_UNLIKELY (next[0] == 0)) {
7735 if (g_str_equal (g_type_name (G_VALUE_TYPE (dest)), "GstFlagSet")) {
7736 /* If we're parsing a generic flag set, that can mean we're guessing
7737 * at the type in deserialising a GstStructure so at least check that
7738 * we have a valid-looking string, so we don't cause deserialisation of
7739 * other types of strings like 00:01:00:00 - https://bugzilla.gnome.org/show_bug.cgi?id=779755 */
7740 if (is_valid_flags_string (s)) {
7747 /* Otherwise, we already got a hex string for a valid non-generic flagset type */
7751 try_as_flags_string:
7754 const gchar *set_class = g_type_name (G_VALUE_TYPE (dest));
7755 GFlagsClass *flags_klass = NULL;
7758 if (g_str_equal (set_class, "GstFlagSet")) {
7759 /* There's no hope to parse the fields of generic flag set if we didn't already
7760 * catch a hex-string above */
7764 /* Flags class is the FlagSet class with 'Set' removed from the end */
7765 end = g_strrstr (set_class, "Set");
7768 gchar *class_name = g_strndup (set_class, end - set_class);
7769 GType flags_type = g_type_from_name (class_name);
7770 if (flags_type == 0) {
7771 GST_TRACE ("Looking for dynamic type %s", class_name);
7772 gst_dynamic_type_factory_load (class_name);
7775 if (flags_type != 0) {
7776 flags_klass = g_type_class_ref (flags_type);
7777 GST_TRACE ("Going to parse %s as %s", s, class_name);
7779 g_free (class_name);
7783 res = gst_value_gflags_str_to_flags (flags_klass, s, &flags, &mask);
7784 g_type_class_unref (flags_klass);
7790 gst_value_set_flagset (dest, flags, mask);
7796 gst_value_transform_flagset_string (const GValue * src_value,
7797 GValue * dest_value)
7799 dest_value->data[0].v_pointer = gst_value_serialize_flagset (src_value);
7803 gst_value_transform_string_flagset (const GValue * src_value,
7804 GValue * dest_value)
7806 if (!gst_value_deserialize_flagset (dest_value, src_value->data[0].v_pointer)) {
7807 /* If the deserialize fails, ensure we leave the flags in a
7808 * valid, if incorrect, state */
7809 gst_value_set_flagset (dest_value, 0, 0);
7814 gst_value_compare_flagset (const GValue * value1, const GValue * value2)
7819 v1 = value1->data[0].v_uint;
7820 v2 = value2->data[0].v_uint;
7822 m1 = value1->data[1].v_uint;
7823 m2 = value2->data[1].v_uint;
7825 if (v1 == v2 && m1 == m2)
7826 return GST_VALUE_EQUAL;
7828 return GST_VALUE_UNORDERED;
7831 /***********************
7832 * GstAllocationParams *
7833 ***********************/
7835 gst_value_compare_allocation_params (const GValue * value1,
7836 const GValue * value2)
7838 GstAllocationParams *v1, *v2;
7840 v1 = value1->data[0].v_pointer;
7841 v2 = value2->data[0].v_pointer;
7843 if (v1 == NULL && v1 == v2)
7844 return GST_VALUE_EQUAL;
7846 if (v1 == NULL || v2 == NULL)
7847 return GST_VALUE_UNORDERED;
7849 if (v1->flags == v2->flags && v1->align == v2->align &&
7850 v1->prefix == v2->prefix && v1->padding == v2->padding)
7851 return GST_VALUE_EQUAL;
7853 return GST_VALUE_UNORDERED;
7862 gst_value_compare_object (const GValue * value1, const GValue * value2)
7866 v1 = value1->data[0].v_pointer;
7867 v2 = value2->data[0].v_pointer;
7870 return GST_VALUE_EQUAL;
7872 return GST_VALUE_UNORDERED;
7876 gst_value_transform_object_string (const GValue * src_value,
7877 GValue * dest_value)
7882 obj = g_value_get_object (src_value);
7885 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
7886 GST_OBJECT_NAME (obj));
7888 str = g_strdup ("NULL");
7891 dest_value->data[0].v_pointer = str;
7894 static GTypeInfo _info = {
7895 0, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, NULL,
7898 static GTypeFundamentalInfo _finfo = {
7902 #define FUNC_VALUE_GET_TYPE_CLASSED(type, name, csize, flags) \
7903 GType _gst_ ## type ## _type = 0; \
7905 GType gst_ ## type ## _get_type (void) \
7907 static GType gst_ ## type ## _type = 0; \
7909 if (g_once_init_enter (&gst_ ## type ## _type)) { \
7911 _info.class_size = csize; \
7912 _finfo.type_flags = flags; \
7913 _info.value_table = & _gst_ ## type ## _value_table; \
7914 _type = g_type_register_fundamental ( \
7915 g_type_fundamental_next (), \
7916 name, &_info, &_finfo, 0); \
7917 _gst_ ## type ## _type = _type; \
7918 g_once_init_leave(&gst_ ## type ## _type, _type); \
7921 return gst_ ## type ## _type; \
7924 #define FUNC_VALUE_GET_TYPE(type, name) \
7925 FUNC_VALUE_GET_TYPE_CLASSED(type, name, 0, 0)
7927 static const GTypeValueTable _gst_int_range_value_table = {
7928 gst_value_init_int_range,
7930 gst_value_copy_int_range,
7933 gst_value_collect_int_range, (char *) "pp", gst_value_lcopy_int_range
7936 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
7938 static const GTypeValueTable _gst_int64_range_value_table = {
7939 gst_value_init_int64_range,
7940 gst_value_free_int64_range,
7941 gst_value_copy_int64_range,
7944 gst_value_collect_int64_range,
7945 (char *) "pp", gst_value_lcopy_int64_range
7948 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
7950 static const GTypeValueTable _gst_double_range_value_table = {
7951 gst_value_init_double_range,
7953 gst_value_copy_double_range,
7956 gst_value_collect_double_range,
7957 (char *) "pp", gst_value_lcopy_double_range
7960 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
7962 static const GTypeValueTable _gst_fraction_range_value_table = {
7963 gst_value_init_fraction_range,
7964 gst_value_free_fraction_range,
7965 gst_value_copy_fraction_range,
7968 gst_value_collect_fraction_range,
7969 (char *) "pppp", gst_value_lcopy_fraction_range
7972 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
7974 static const GTypeValueTable _gst_value_list_value_table = {
7975 gst_value_init_list_or_array,
7976 gst_value_free_list_or_array,
7977 gst_value_copy_list_or_array,
7978 gst_value_list_or_array_peek_pointer,
7980 gst_value_collect_list_or_array,
7981 (char *) "p", gst_value_lcopy_list_or_array
7984 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
7986 static const GTypeValueTable _gst_value_array_value_table = {
7987 gst_value_init_list_or_array,
7988 gst_value_free_list_or_array,
7989 gst_value_copy_list_or_array,
7990 gst_value_list_or_array_peek_pointer,
7992 gst_value_collect_list_or_array,
7993 (char *) "p", gst_value_lcopy_list_or_array
7996 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
7998 static const GTypeValueTable _gst_fraction_value_table = {
7999 gst_value_init_fraction,
8001 gst_value_copy_fraction,
8004 gst_value_collect_fraction, (char *) "pp", gst_value_lcopy_fraction
8007 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
8009 static const GTypeValueTable _gst_bitmask_value_table = {
8010 gst_value_init_bitmask,
8012 gst_value_copy_bitmask,
8015 gst_value_collect_bitmask, (char *) "p", gst_value_lcopy_bitmask
8018 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
8020 static const GTypeValueTable _gst_flagset_value_table = {
8021 gst_value_init_flagset,
8023 gst_value_copy_flagset,
8026 gst_value_collect_flagset, (char *) "pp", gst_value_lcopy_flagset
8029 FUNC_VALUE_GET_TYPE_CLASSED (flagset, "GstFlagSet",
8030 sizeof (GstFlagSetClass), G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE);
8033 gst_g_thread_get_type (void)
8035 return G_TYPE_THREAD;
8038 #define SERIAL_VTABLE(t,c,s,d) { t, c, s, d, NULL }
8039 #define SERIAL_VTABLE_PSPEC(t,c,s,d) { t, c, s, NULL, d }
8041 #define REGISTER_SERIALIZATION_CONST(_gtype, _type) \
8043 static const GstValueTable gst_value = \
8044 SERIAL_VTABLE (_gtype, gst_value_compare_ ## _type, \
8045 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8046 gst_value_register (&gst_value); \
8049 #define REGISTER_SERIALIZATION(_gtype, _type) \
8051 static GstValueTable gst_value = \
8052 SERIAL_VTABLE (0, gst_value_compare_ ## _type, \
8053 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8054 gst_value.type = _gtype; \
8055 gst_value_register (&gst_value); \
8058 #define REGISTER_SERIALIZATION_WITH_PSPEC(_gtype, _type) \
8060 static GstValueTable gst_value = \
8061 SERIAL_VTABLE_PSPEC (0, gst_value_compare_ ## _type, \
8062 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8063 gst_value.type = _gtype; \
8064 gst_value_register (&gst_value); \
8067 #define REGISTER_SERIALIZATION_NO_COMPARE(_gtype, _type) \
8069 static GstValueTable gst_value = \
8070 SERIAL_VTABLE (0, NULL, \
8071 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8072 gst_value.type = _gtype; \
8073 gst_value_register (&gst_value); \
8076 #define REGISTER_SERIALIZATION_COMPARE_ONLY(_gtype, _type) \
8078 static GstValueTable gst_value = \
8079 SERIAL_VTABLE (0, gst_value_compare_ ## _type, \
8081 gst_value.type = _gtype; \
8082 gst_value_register (&gst_value); \
8085 /* These initial sizes are used for the tables
8086 * below, and save a couple of reallocs at startup */
8088 static const gint GST_VALUE_TABLE_DEFAULT_SIZE = 40;
8089 static const gint GST_VALUE_UNION_TABLE_DEFAULT_SIZE = 8;
8090 static const gint GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE = 4;
8091 static const gint GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE = 16;
8094 _priv_gst_value_initialize (void)
8097 g_array_sized_new (FALSE, FALSE, sizeof (GstValueTable),
8098 GST_VALUE_TABLE_DEFAULT_SIZE);
8099 gst_value_hash = g_hash_table_new (NULL, NULL);
8100 gst_value_union_funcs = g_array_sized_new (FALSE, FALSE,
8101 sizeof (GstValueUnionInfo), GST_VALUE_UNION_TABLE_DEFAULT_SIZE);
8102 gst_value_intersect_funcs = g_array_sized_new (FALSE, FALSE,
8103 sizeof (GstValueIntersectInfo), GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE);
8104 gst_value_subtract_funcs = g_array_sized_new (FALSE, FALSE,
8105 sizeof (GstValueSubtractInfo), GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE);
8107 REGISTER_SERIALIZATION (gst_int_range_get_type (), int_range);
8108 REGISTER_SERIALIZATION (gst_int64_range_get_type (), int64_range);
8109 REGISTER_SERIALIZATION (gst_double_range_get_type (), double_range);
8110 REGISTER_SERIALIZATION (gst_fraction_range_get_type (), fraction_range);
8111 REGISTER_SERIALIZATION (g_value_array_get_type (), g_value_array);
8112 REGISTER_SERIALIZATION (gst_buffer_get_type (), buffer);
8113 REGISTER_SERIALIZATION (gst_sample_get_type (), sample);
8114 REGISTER_SERIALIZATION (gst_fraction_get_type (), fraction);
8115 REGISTER_SERIALIZATION (gst_caps_get_type (), caps);
8116 REGISTER_SERIALIZATION (gst_tag_list_get_type (), tag_list);
8117 REGISTER_SERIALIZATION (G_TYPE_DATE, date);
8118 REGISTER_SERIALIZATION (G_TYPE_BYTES, bytes);
8119 REGISTER_SERIALIZATION (gst_date_time_get_type (), date_time);
8120 REGISTER_SERIALIZATION (gst_bitmask_get_type (), bitmask);
8121 REGISTER_SERIALIZATION (gst_structure_get_type (), structure);
8122 REGISTER_SERIALIZATION (gst_flagset_get_type (), flagset);
8124 REGISTER_SERIALIZATION_NO_COMPARE (gst_segment_get_type (), segment);
8125 REGISTER_SERIALIZATION_NO_COMPARE (gst_caps_features_get_type (),
8128 REGISTER_SERIALIZATION_COMPARE_ONLY (gst_allocation_params_get_type (),
8130 REGISTER_SERIALIZATION_COMPARE_ONLY (G_TYPE_OBJECT, object);
8132 REGISTER_SERIALIZATION_CONST (G_TYPE_DOUBLE, double);
8133 REGISTER_SERIALIZATION_CONST (G_TYPE_FLOAT, float);
8135 REGISTER_SERIALIZATION_CONST (G_TYPE_STRING, string);
8136 REGISTER_SERIALIZATION_CONST (G_TYPE_BOOLEAN, boolean);
8137 REGISTER_SERIALIZATION_CONST (G_TYPE_ENUM, enum);
8139 REGISTER_SERIALIZATION_CONST (G_TYPE_FLAGS, gflags);
8141 REGISTER_SERIALIZATION_CONST (G_TYPE_INT, int);
8143 REGISTER_SERIALIZATION_CONST (G_TYPE_INT64, int64);
8144 REGISTER_SERIALIZATION_CONST (G_TYPE_LONG, long);
8146 REGISTER_SERIALIZATION_CONST (G_TYPE_UINT, uint);
8147 REGISTER_SERIALIZATION_CONST (G_TYPE_UINT64, uint64);
8148 REGISTER_SERIALIZATION_CONST (G_TYPE_ULONG, ulong);
8150 REGISTER_SERIALIZATION_CONST (G_TYPE_UCHAR, uchar);
8152 REGISTER_SERIALIZATION (G_TYPE_GTYPE, gtype);
8154 REGISTER_SERIALIZATION_WITH_PSPEC (gst_value_list_get_type (), value_list);
8155 REGISTER_SERIALIZATION_WITH_PSPEC (gst_value_array_get_type (), value_array);
8157 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
8158 gst_value_transform_int_range_string);
8159 g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
8160 gst_value_transform_int64_range_string);
8161 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
8162 gst_value_transform_double_range_string);
8163 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
8164 gst_value_transform_fraction_range_string);
8165 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
8166 gst_value_transform_list_string);
8167 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_VALUE_ARRAY,
8168 gst_value_transform_any_list_g_value_array);
8169 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
8170 gst_value_transform_array_string);
8171 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_VALUE_ARRAY,
8172 gst_value_transform_any_list_g_value_array);
8173 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, G_TYPE_STRING,
8174 gst_value_transform_g_value_array_string);
8175 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, GST_TYPE_ARRAY,
8176 gst_value_transform_g_value_array_any_list);
8177 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, GST_TYPE_LIST,
8178 gst_value_transform_g_value_array_any_list);
8179 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
8180 gst_value_transform_fraction_string);
8181 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
8182 gst_value_transform_string_fraction);
8183 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
8184 gst_value_transform_fraction_double);
8185 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
8186 gst_value_transform_fraction_float);
8187 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
8188 gst_value_transform_double_fraction);
8189 g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
8190 gst_value_transform_float_fraction);
8191 g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
8192 gst_value_transform_date_string);
8193 g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
8194 gst_value_transform_string_date);
8195 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
8196 gst_value_transform_object_string);
8197 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
8198 gst_value_transform_bitmask_uint64);
8199 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
8200 gst_value_transform_bitmask_string);
8201 g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
8202 gst_value_transform_uint64_bitmask);
8203 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
8204 gst_value_transform_string_bitmask);
8206 g_value_register_transform_func (GST_TYPE_FLAG_SET, G_TYPE_STRING,
8207 gst_value_transform_flagset_string);
8208 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FLAG_SET,
8209 gst_value_transform_string_flagset);
8211 /* Only register intersection functions for *different* types.
8212 * Identical type intersection should be specified directly in
8213 * gst_value_intersect() */
8214 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8215 gst_value_intersect_int_int_range);
8216 gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
8217 gst_value_intersect_int64_int64_range);
8218 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
8219 gst_value_intersect_double_double_range);
8220 gst_value_register_intersect_func (GST_TYPE_FRACTION,
8221 GST_TYPE_FRACTION_RANGE, gst_value_intersect_fraction_fraction_range);
8223 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8224 gst_value_subtract_int_int_range);
8225 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
8226 gst_value_subtract_int_range_int);
8227 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
8228 gst_value_subtract_int_range_int_range);
8229 gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
8230 gst_value_subtract_int64_int64_range);
8231 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
8232 gst_value_subtract_int64_range_int64);
8233 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE,
8234 GST_TYPE_INT64_RANGE, gst_value_subtract_int64_range_int64_range);
8235 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
8236 gst_value_subtract_double_double_range);
8237 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
8238 gst_value_subtract_double_range_double);
8239 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
8240 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
8241 gst_value_register_subtract_func (GST_TYPE_FRACTION,
8242 GST_TYPE_FRACTION_RANGE, gst_value_subtract_fraction_fraction_range);
8243 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
8244 GST_TYPE_FRACTION, gst_value_subtract_fraction_range_fraction);
8245 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
8246 GST_TYPE_FRACTION_RANGE,
8247 gst_value_subtract_fraction_range_fraction_range);
8250 GType date_type = G_TYPE_DATE;
8252 g_type_name (date_type);
8255 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8256 gst_value_union_int_int_range);
8257 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
8258 gst_value_union_int_range_int_range);
8259 gst_value_register_union_func (GST_TYPE_FLAG_SET, GST_TYPE_FLAG_SET,
8260 gst_value_union_flagset_flagset);
8261 gst_value_register_union_func (GST_TYPE_STRUCTURE, GST_TYPE_STRUCTURE,
8262 gst_value_union_structure_structure);
8264 #if GST_VERSION_NANO == 1
8265 /* If building from git master, check starting array sizes matched actual size
8266 * so we can keep the defines in sync and save a few reallocs on startup */
8267 if (gst_value_table->len > GST_VALUE_TABLE_DEFAULT_SIZE) {
8268 GST_ERROR ("Wrong initial gst_value_table size. "
8269 "Please set GST_VALUE_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8270 gst_value_table->len);
8272 if (gst_value_union_funcs->len > GST_VALUE_UNION_TABLE_DEFAULT_SIZE) {
8273 GST_ERROR ("Wrong initial gst_value_union_funcs table size. "
8274 "Please set GST_VALUE_UNION_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8275 gst_value_union_funcs->len);
8277 if (gst_value_intersect_funcs->len > GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE) {
8278 GST_ERROR ("Wrong initial gst_value_intersect_funcs table size. "
8279 "Please set GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8280 gst_value_intersect_funcs->len);
8282 if (gst_value_subtract_funcs->len > GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE) {
8283 GST_ERROR ("Wrong initial gst_value_subtract_funcs table size. "
8284 "Please set GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8285 gst_value_subtract_funcs->len);
8290 /* Implement these if needed */
8291 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
8292 gst_value_union_fraction_fraction_range);
8293 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
8294 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
8299 gst_flagset_class_init (gpointer g_class, gpointer class_data)
8301 GstFlagSetClass *f_class = (GstFlagSetClass *) (g_class);
8302 f_class->flags_type = (GType) GPOINTER_TO_SIZE (class_data);
8306 * gst_flagset_register:
8307 * @flags_type: a #GType of a #G_TYPE_FLAGS type.
8309 * Create a new sub-class of #GST_TYPE_FLAG_SET
8310 * which will pretty-print the human-readable flags
8311 * when serializing, for easier debugging.
8316 gst_flagset_register (GType flags_type)
8319 sizeof (GstFlagSetClass),
8321 (GClassInitFunc) gst_flagset_class_init,
8322 NULL, GSIZE_TO_POINTER (flags_type), 0, 0, NULL, NULL
8327 g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), 0);
8329 class_name = g_strdup_printf ("%sSet", g_type_name (flags_type));
8331 t = g_type_register_static (GST_TYPE_FLAG_SET,
8332 g_intern_string (class_name), &info, 0);
8333 g_free (class_name);