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, GstSerializeFlags flags)
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 gboolean nested_structs_brackets;
365 v = &vlist->fields[i];
366 nested_structs_brackets = !(flags & GST_SERIALIZE_FLAG_BACKWARD_COMPAT)
367 && (GST_VALUE_HOLDS_STRUCTURE (v) || GST_VALUE_HOLDS_CAPS (v));
368 if (!nested_structs_brackets) {
369 s_val = gst_value_serialize (v);
371 if (GST_VALUE_HOLDS_STRUCTURE (v))
372 s_val = gst_structure_serialize (gst_value_get_structure (v), flags);
373 else if (GST_VALUE_HOLDS_CAPS (v))
374 s_val = gst_caps_serialize (gst_value_get_caps (v), flags);
378 g_string_append_c (s, '(');
379 g_string_append (s, _priv_gst_value_gtype_to_abbr (G_VALUE_TYPE (v)));
380 g_string_append_c (s, ')');
383 if (nested_structs_brackets)
384 g_string_append_c (s, '[');
385 g_string_append (s, s_val);
386 if (nested_structs_brackets)
387 g_string_append_c (s, ']');
390 g_string_append_len (s, ", ", 2);
393 g_critical ("Could not serialize list/array value of type '%s'",
394 G_VALUE_TYPE_NAME (v));
397 g_string_append (s, end);
398 return g_string_free (s, FALSE);
402 gst_value_transform_any_list_string (const GValue * src_value,
403 GValue * dest_value, const gchar * begin, const gchar * end)
412 array = src_value->data[0].v_pointer;
415 /* estimate minimum string length to minimise re-allocs in GString */
416 s = g_string_sized_new (2 + (10 * alen) + 2);
417 g_string_append (s, begin);
418 for (i = 0; i < alen; i++) {
419 list_value = &array->fields[i];
422 g_string_append_len (s, ", ", 2);
424 list_s = g_strdup_value_contents (list_value);
425 g_string_append (s, list_s);
428 g_string_append (s, end);
430 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
434 _gst_value_serialize_g_value_array (const GValue * value, const gchar * begin,
438 GValueArray *array = value->data[0].v_pointer;
445 alen = array->n_values;
447 /* estimate minimum string length to minimise re-allocs in GString */
448 s = g_string_sized_new (2 + (6 * alen) + 2);
449 g_string_append (s, begin);
450 for (i = 0; i < alen; i++) {
451 v = g_value_array_get_nth (array, i);
452 s_val = gst_value_serialize (v);
454 g_string_append (s, s_val);
457 g_string_append_len (s, ", ", 2);
460 g_critical ("Could not serialize list/array value of type '%s'",
461 G_VALUE_TYPE_NAME (v));
464 g_string_append (s, end);
465 return g_string_free (s, FALSE);
469 _gst_value_transform_g_value_array_string (const GValue * src_value,
470 GValue * dest_value, const gchar * begin, const gchar * end)
479 array = src_value->data[0].v_pointer;
480 alen = array->n_values;
482 /* estimate minimum string length to minimise re-allocs in GString */
483 s = g_string_sized_new (2 + (10 * alen) + 2);
484 g_string_append (s, begin);
485 for (i = 0; i < alen; i++) {
486 list_value = g_value_array_get_nth (array, i);
489 g_string_append_len (s, ", ", 2);
491 list_s = g_strdup_value_contents (list_value);
492 g_string_append (s, list_s);
495 g_string_append (s, end);
497 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
501 * helper function to see if a type is fixed. Is used internally here and
502 * there. Do not export, since it doesn't work for types where the content
503 * decides the fixedness (e.g. GST_TYPE_ARRAY).
506 gst_type_is_fixed (GType type)
508 /* the basic int, string, double types */
509 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
512 /* our fundamental types that are certainly not fixed */
513 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
514 type == GST_TYPE_INT64_RANGE ||
515 type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE ||
516 type == GST_TYPE_STRUCTURE) {
519 /* other (boxed) types that are fixed */
520 if (type == GST_TYPE_BUFFER) {
524 if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
525 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
532 /* GValue functions usable for both regular lists and arrays */
534 gst_value_init_list_or_array (GValue * value)
536 value->data[0].v_pointer = _gst_value_list_new (0);
539 static GstValueList *
540 copy_gst_value_list (const GstValueList * src)
546 dest = _gst_value_list_new (len);
548 for (i = 0; i < len; i++) {
549 gst_value_init_and_copy (&dest->fields[i], &src->fields[i]);
556 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
558 dest_value->data[0].v_pointer =
559 copy_gst_value_list (VALUE_LIST_ARRAY (src_value));
563 gst_value_free_list_or_array (GValue * value)
566 GstValueList *src = VALUE_LIST_ARRAY (value);
569 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
570 for (i = 0; i < len; i++) {
571 g_value_unset (&src->fields[i]);
573 if (VALUE_LIST_IS_USING_DYNAMIC_ARRAY (src)) {
574 g_free (src->fields);
581 gst_value_list_or_array_peek_pointer (const GValue * value)
583 return value->data[0].v_pointer;
587 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
588 GTypeCValue * collect_values, guint collect_flags)
590 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
591 value->data[0].v_pointer = collect_values[0].v_pointer;
592 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
594 value->data[0].v_pointer =
595 copy_gst_value_list ((GstValueList *) collect_values[0].v_pointer);
601 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
602 GTypeCValue * collect_values, guint collect_flags)
604 GstValueList **dest = collect_values[0].v_pointer;
606 g_return_val_if_fail (dest != NULL,
607 g_strdup_printf ("value location for `%s' passed as NULL",
608 G_VALUE_TYPE_NAME (value)));
609 g_return_val_if_fail (value->data[0].v_pointer != NULL,
610 g_strdup_printf ("invalid value given for `%s'",
611 G_VALUE_TYPE_NAME (value)));
613 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
614 *dest = (GstValueList *) value->data[0].v_pointer;
616 *dest = copy_gst_value_list (VALUE_LIST_ARRAY (value));
622 gst_value_list_or_array_get_basic_type (const GValue * value, GType * type)
624 if (G_UNLIKELY (value == NULL))
627 if (GST_VALUE_HOLDS_LIST (value) || GST_VALUE_HOLDS_ARRAY (value)) {
628 if (VALUE_LIST_SIZE (value) == 0)
630 return gst_value_list_or_array_get_basic_type (VALUE_LIST_GET_VALUE (value,
634 *type = G_VALUE_TYPE (value);
639 #define IS_RANGE_COMPAT(type1,type2,t1,t2) \
640 (((t1) == (type1) && (t2) == (type2)) || ((t2) == (type1) && (t1) == (type2)))
643 gst_value_list_or_array_are_compatible (const GValue * value1,
644 const GValue * value2)
646 GType basic_type1, basic_type2;
648 /* empty or same type is OK */
649 if (!gst_value_list_or_array_get_basic_type (value1, &basic_type1) ||
650 !gst_value_list_or_array_get_basic_type (value2, &basic_type2) ||
651 basic_type1 == basic_type2)
654 /* ranges are distinct types for each bound type... */
655 if (IS_RANGE_COMPAT (G_TYPE_INT, GST_TYPE_INT_RANGE, basic_type1,
658 if (IS_RANGE_COMPAT (G_TYPE_INT64, GST_TYPE_INT64_RANGE, basic_type1,
661 if (IS_RANGE_COMPAT (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE, basic_type1,
664 if (IS_RANGE_COMPAT (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, basic_type1,
672 _gst_value_list_append_and_take_value (GValue * value, GValue * append_value)
674 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), append_value);
675 memset (append_value, 0, sizeof (GValue));
679 * gst_value_list_append_and_take_value:
680 * @value: a #GValue of type #GST_TYPE_LIST
681 * @append_value: (transfer full): the value to append
683 * Appends @append_value to the GstValueList in @value.
688 gst_value_list_append_and_take_value (GValue * value, GValue * append_value)
690 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
691 g_return_if_fail (G_IS_VALUE (append_value));
692 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
695 _gst_value_list_append_and_take_value (value, append_value);
699 * gst_value_list_append_value:
700 * @value: a #GValue of type #GST_TYPE_LIST
701 * @append_value: (transfer none): the value to append
703 * Appends @append_value to the GstValueList in @value.
706 gst_value_list_append_value (GValue * value, const GValue * append_value)
710 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
711 g_return_if_fail (G_IS_VALUE (append_value));
712 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
715 gst_value_init_and_copy (&val, append_value);
716 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), &val);
720 * gst_value_list_prepend_value:
721 * @value: a #GValue of type #GST_TYPE_LIST
722 * @prepend_value: the value to prepend
724 * Prepends @prepend_value to the GstValueList in @value.
727 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
731 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
732 g_return_if_fail (G_IS_VALUE (prepend_value));
733 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
736 gst_value_init_and_copy (&val, prepend_value);
737 _gst_value_list_prepend_val (VALUE_LIST_ARRAY (value), &val);
741 * gst_value_list_concat:
742 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
746 * Concatenates copies of @value1 and @value2 into a list. Values that are not
747 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
748 * @dest will be initialized to the type #GST_TYPE_LIST.
751 gst_value_list_concat (GValue * dest, const GValue * value1,
752 const GValue * value2)
754 guint i, value1_length, value2_length;
757 g_return_if_fail (dest != NULL);
758 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
759 g_return_if_fail (G_IS_VALUE (value1));
760 g_return_if_fail (G_IS_VALUE (value2));
761 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
764 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
766 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
768 _gst_value_list_init (dest, value1_length + value2_length);
769 vlist = VALUE_LIST_ARRAY (dest);
770 vlist->len = value1_length + value2_length;
772 if (GST_VALUE_HOLDS_LIST (value1)) {
773 for (i = 0; i < value1_length; i++) {
774 gst_value_init_and_copy (&vlist->fields[i],
775 VALUE_LIST_GET_VALUE (value1, i));
778 gst_value_init_and_copy (&vlist->fields[0], value1);
781 if (GST_VALUE_HOLDS_LIST (value2)) {
782 for (i = 0; i < value2_length; i++) {
783 gst_value_init_and_copy (&vlist->fields[i + value1_length],
784 VALUE_LIST_GET_VALUE (value2, i));
787 gst_value_init_and_copy (&vlist->fields[value1_length], value2);
791 /* same as gst_value_list_concat() but takes ownership of GValues */
793 gst_value_list_concat_and_take_values (GValue * dest, GValue * val1,
796 guint i, val1_length, val2_length;
797 gboolean val1_is_list;
798 gboolean val2_is_list;
801 g_assert (dest != NULL);
802 g_assert (G_VALUE_TYPE (dest) == 0);
803 g_assert (G_IS_VALUE (val1));
804 g_assert (G_IS_VALUE (val2));
805 g_assert (gst_value_list_or_array_are_compatible (val1, val2));
807 val1_is_list = GST_VALUE_HOLDS_LIST (val1);
808 val1_length = (val1_is_list ? VALUE_LIST_SIZE (val1) : 1);
810 val2_is_list = GST_VALUE_HOLDS_LIST (val2);
811 val2_length = (val2_is_list ? VALUE_LIST_SIZE (val2) : 1);
813 /* Overidding the default initialization to have a list of the target size */
814 _gst_value_list_init (dest, val1_length + val2_length);
815 vlist = VALUE_LIST_ARRAY (dest);
816 vlist->len = val1_length + val2_length;
819 for (i = 0; i < val1_length; i++) {
820 vlist->fields[i] = *VALUE_LIST_GET_VALUE (val1, i);
822 VALUE_LIST_ARRAY (val1)->len = 0;
823 g_value_unset (val1);
825 vlist->fields[0] = *val1;
826 G_VALUE_TYPE (val1) = G_TYPE_INVALID;
830 for (i = 0; i < val2_length; i++) {
831 const GValue *v2 = VALUE_LIST_GET_VALUE (val2, i);
832 vlist->fields[i + val1_length] = *v2;
835 VALUE_LIST_ARRAY (val2)->len = 0;
836 g_value_unset (val2);
838 vlist->fields[val1_length] = *val2;
839 G_VALUE_TYPE (val2) = G_TYPE_INVALID;
844 * gst_value_list_merge:
845 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
849 * Merges copies of @value1 and @value2. Values that are not
850 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
852 * The result will be put into @dest and will either be a list that will not
853 * contain any duplicates, or a non-list type (if @value1 and @value2
857 gst_value_list_merge (GValue * dest, const GValue * value1,
858 const GValue * value2)
860 guint i, j, k, value1_length, value2_length, skipped;
865 g_return_if_fail (dest != NULL);
866 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
867 g_return_if_fail (G_IS_VALUE (value1));
868 g_return_if_fail (G_IS_VALUE (value2));
869 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
872 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
874 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
876 _gst_value_list_init (dest, value1_length + value2_length);
877 vlist = VALUE_LIST_ARRAY (dest);
878 vlist->len = value1_length + value2_length;
880 if (GST_VALUE_HOLDS_LIST (value1)) {
881 for (i = 0; i < value1_length; i++) {
882 gst_value_init_and_copy (&vlist->fields[i], VALUE_LIST_GET_VALUE (value1,
886 gst_value_init_and_copy (&vlist->fields[0], value1);
891 if (GST_VALUE_HOLDS_LIST (value2)) {
892 for (i = 0; i < value2_length; i++) {
894 src = VALUE_LIST_GET_VALUE (value2, i);
895 for (k = 0; k < value1_length; k++) {
896 if (gst_value_compare (&vlist->fields[k], src) == GST_VALUE_EQUAL) {
903 gst_value_init_and_copy (&vlist->fields[j], src);
909 for (k = 0; k < value1_length; k++) {
910 if (gst_value_compare (&vlist->fields[k], value2) == GST_VALUE_EQUAL) {
917 gst_value_init_and_copy (&vlist->fields[j], value2);
921 guint new_size = value1_length + (value2_length - skipped);
925 vlist->len = new_size;
929 /* size is 1, take single value in list and make it new dest */
930 single_dest = vlist->fields[0];
932 /* clean up old value allocations: must set array size to 0, because
933 * allocated values are not inited meaning g_value_unset() will not
936 g_value_unset (dest);
938 /* the single value is our new result */
945 * gst_value_list_get_size:
946 * @value: a #GValue of type #GST_TYPE_LIST
948 * Gets the number of values contained in @value.
950 * Returns: the number of values
953 gst_value_list_get_size (const GValue * value)
955 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
957 return VALUE_LIST_SIZE (value);
961 * gst_value_list_get_value:
962 * @value: a #GValue of type #GST_TYPE_LIST
963 * @index: index of value to get from the list
965 * Gets the value that is a member of the list contained in @value and
966 * has the index @index.
968 * Returns: (transfer none): the value at the given index
971 gst_value_list_get_value (const GValue * value, guint index)
973 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
974 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
976 return VALUE_LIST_GET_VALUE (value, index);
980 * gst_value_array_append_value:
981 * @value: a #GValue of type #GST_TYPE_ARRAY
982 * @append_value: the value to append
984 * Appends @append_value to the GstValueArray in @value.
987 gst_value_array_append_value (GValue * value, const GValue * append_value)
991 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
992 g_return_if_fail (G_IS_VALUE (append_value));
993 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
996 gst_value_init_and_copy (&val, append_value);
997 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), &val);
1001 _gst_value_array_append_and_take_value (GValue * value, GValue * append_value)
1003 _gst_value_list_append_val (VALUE_LIST_ARRAY (value), append_value);
1004 memset (append_value, 0, sizeof (GValue));
1008 * gst_value_array_append_and_take_value:
1009 * @value: a #GValue of type #GST_TYPE_ARRAY
1010 * @append_value: (transfer full): the value to append
1012 * Appends @append_value to the GstValueArray in @value.
1017 gst_value_array_append_and_take_value (GValue * value, GValue * append_value)
1019 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
1020 g_return_if_fail (G_IS_VALUE (append_value));
1021 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
1024 _gst_value_array_append_and_take_value (value, append_value);
1028 * gst_value_array_prepend_value:
1029 * @value: a #GValue of type #GST_TYPE_ARRAY
1030 * @prepend_value: the value to prepend
1032 * Prepends @prepend_value to the GstValueArray in @value.
1035 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
1037 GValue val = { 0, };
1039 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
1040 g_return_if_fail (G_IS_VALUE (prepend_value));
1041 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
1044 gst_value_init_and_copy (&val, prepend_value);
1045 _gst_value_list_prepend_val (VALUE_LIST_ARRAY (value), &val);
1049 * gst_value_array_get_size:
1050 * @value: a #GValue of type #GST_TYPE_ARRAY
1052 * Gets the number of values contained in @value.
1054 * Returns: the number of values
1057 gst_value_array_get_size (const GValue * value)
1059 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
1061 return VALUE_LIST_SIZE (value);
1065 * gst_value_array_get_value:
1066 * @value: a #GValue of type #GST_TYPE_ARRAY
1067 * @index: index of value to get from the array
1069 * Gets the value that is a member of the array contained in @value and
1070 * has the index @index.
1072 * Returns: (transfer none): the value at the given index
1075 gst_value_array_get_value (const GValue * value, guint index)
1077 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
1078 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
1080 return VALUE_LIST_GET_VALUE (value, index);
1084 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
1086 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
1090 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
1092 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
1096 gst_value_transform_g_value_array_string (const GValue * src_value,
1097 GValue * dest_value)
1099 _gst_value_transform_g_value_array_string (src_value, dest_value, "< ", " >");
1103 gst_value_transform_g_value_array_any_list (const GValue * src_value,
1104 GValue * dest_value)
1106 const GValueArray *varray;
1107 GstValueList *vlist;
1110 varray = g_value_get_boxed (src_value);
1112 /* GLib will unset the value, memset to 0 the data instead of doing a proper
1113 * reset. That's why we need to allocate the array here */
1114 vlist = dest_value->data[0].v_pointer =
1115 _gst_value_list_new (varray->n_values);
1117 for (i = 0; i < varray->n_values; i++) {
1118 GValue val = G_VALUE_INIT;
1119 gst_value_init_and_copy (&val, &varray->values[i]);
1120 _gst_value_list_append_val (vlist, &val);
1125 gst_value_transform_any_list_g_value_array (const GValue * src_value,
1126 GValue * dest_value)
1128 GValueArray *varray;
1129 GstValueList *vlist;
1132 vlist = VALUE_LIST_ARRAY (src_value);
1133 varray = g_value_array_new (vlist->len);
1135 for (i = 0; i < vlist->len; i++)
1136 g_value_array_append (varray, &vlist->fields[i]);
1138 g_value_take_boxed (dest_value, varray);
1141 /* Do an unordered compare of the contents of a list */
1143 gst_value_compare_value_list (const GValue * value1, const GValue * value2)
1146 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
1147 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
1150 gint len, to_remove;
1152 GstValueCompareFunc compare;
1154 /* get length and do initial length check. */
1156 if (len != vlist2->len)
1157 return GST_VALUE_UNORDERED;
1159 /* Empty lists are equal */
1161 return GST_VALUE_EQUAL;
1163 /* We know lists are not empty. do sanity check on first values */
1164 if (G_VALUE_TYPE (&vlist1->fields[0]) != G_VALUE_TYPE (&vlist2->fields[0]))
1165 return GST_VALUE_UNORDERED;
1167 /* Get the compare function */
1168 if (!(compare = gst_value_get_compare_func (&vlist1->fields[0])))
1169 return GST_VALUE_UNORDERED;
1171 /* place to mark removed value indices of array2 */
1172 removed = g_newa (guint8, len);
1173 memset (removed, 0, len);
1176 /* loop over array1, all items should be in array2. When we find an
1177 * item in array2, remove it from array2 by marking it as removed */
1178 for (i = 0; i < len; i++) {
1179 v1 = &vlist1->fields[i];
1181 for (j = 0; j < len; j++) {
1182 /* item is removed, we can skip it */
1185 v2 = &vlist2->fields[j];
1186 /* Note: compare function can be called directly since we know the types
1188 if (compare (v1, v2) == GST_VALUE_EQUAL) {
1189 /* mark item as removed now that we found it in array2 and
1190 * decrement the number of remaining items in array2. */
1196 /* item in array1 and not in array2, UNORDERED */
1198 return GST_VALUE_UNORDERED;
1200 /* if not all items were removed, array2 contained something not in array1 */
1202 return GST_VALUE_UNORDERED;
1204 /* arrays are equal */
1205 return GST_VALUE_EQUAL;
1208 /* Perform an ordered comparison of the contents of an array */
1210 gst_value_compare_value_array (const GValue * value1, const GValue * value2)
1213 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
1214 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
1215 guint len = vlist1->len;
1219 if (len != vlist2->len)
1220 return GST_VALUE_UNORDERED;
1222 for (i = 0; i < len; i++) {
1223 v1 = &vlist1->fields[i];
1224 v2 = &vlist2->fields[i];
1225 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
1226 return GST_VALUE_UNORDERED;
1229 return GST_VALUE_EQUAL;
1233 gst_value_compare_g_value_array (const GValue * value1, const GValue * value2)
1236 GValueArray *array1 = value1->data[0].v_pointer;
1237 GValueArray *array2 = value2->data[0].v_pointer;
1238 guint len = array1 ? array1->n_values : 0;
1242 if (len != (array2 ? array2->n_values : 0))
1243 return GST_VALUE_UNORDERED;
1245 for (i = 0; i < len; i++) {
1246 v1 = g_value_array_get_nth (array1, i);
1247 v2 = g_value_array_get_nth (array2, i);
1248 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
1249 return GST_VALUE_UNORDERED;
1252 return GST_VALUE_EQUAL;
1256 gst_value_serialize_value_list (const GValue * value)
1258 return _priv_gst_value_serialize_any_list (value, "{ ", " }", TRUE,
1259 GST_SERIALIZE_FLAG_BACKWARD_COMPAT);
1263 gst_value_deserialize_value_list (GValue * dest, const gchar * s,
1266 gchar *s2 = (gchar *) s;
1267 return _priv_gst_value_parse_list (s2, &s2, dest, G_TYPE_INVALID, pspec);
1271 gst_value_serialize_value_array (const GValue * value)
1273 return _priv_gst_value_serialize_any_list (value, "< ", " >", TRUE,
1274 GST_SERIALIZE_FLAG_BACKWARD_COMPAT);
1278 gst_value_deserialize_value_array (GValue * dest, const gchar * s,
1281 gchar *s2 = (gchar *) s;
1282 return _priv_gst_value_parse_array (s2, &s2, dest, G_TYPE_INVALID, pspec);
1286 gst_value_serialize_g_value_array (const GValue * value)
1288 return _gst_value_serialize_g_value_array (value, "< ", " >");
1292 gst_value_deserialize_g_value_array (GValue * dest, const gchar * s)
1294 g_warning ("gst_value_deserialize_g_value_array: unimplemented");
1301 * Values in the range are defined as any value greater or equal
1302 * to min*step, AND lesser or equal to max*step.
1303 * For step == 1, this falls back to the traditional range semantics.
1305 * data[0] = (min << 32) | (max)
1310 #define INT_RANGE_MIN(v) ((gint) (((v)->data[0].v_uint64) >> 32))
1311 #define INT_RANGE_MAX(v) ((gint) (((v)->data[0].v_uint64) & 0xffffffff))
1312 #define INT_RANGE_STEP(v) ((v)->data[1].v_int)
1315 gst_value_init_int_range (GValue * value)
1317 G_STATIC_ASSERT (sizeof (gint) <= 2 * sizeof (guint64));
1319 value->data[0].v_uint64 = 0;
1320 value->data[1].v_int = 1;
1324 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
1326 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
1327 dest_value->data[1].v_int = src_value->data[1].v_int;
1331 gst_value_collect_int_range (GValue * value, guint n_collect_values,
1332 GTypeCValue * collect_values, guint collect_flags)
1334 g_return_val_if_fail (n_collect_values == 2,
1335 g_strdup_printf ("not enough value locations for `%s' passed",
1336 G_VALUE_TYPE_NAME (value)));
1337 g_return_val_if_fail (collect_values[0].v_int < collect_values[1].v_int,
1338 g_strdup_printf ("range start is not smaller than end for `%s'",
1339 G_VALUE_TYPE_NAME (value)));
1341 gst_value_set_int_range_step (value, collect_values[0].v_int,
1342 collect_values[1].v_int, 1);
1348 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
1349 GTypeCValue * collect_values, guint collect_flags)
1351 guint32 *int_range_start = collect_values[0].v_pointer;
1352 guint32 *int_range_end = collect_values[1].v_pointer;
1354 g_return_val_if_fail (int_range_start != NULL,
1355 g_strdup_printf ("start value location for `%s' passed as NULL",
1356 G_VALUE_TYPE_NAME (value)));
1357 g_return_val_if_fail (int_range_end != NULL,
1358 g_strdup_printf ("end value location for `%s' passed as NULL",
1359 G_VALUE_TYPE_NAME (value)));
1361 *int_range_start = INT_RANGE_MIN (value);
1362 *int_range_end = INT_RANGE_MAX (value);
1368 * gst_value_set_int_range_step:
1369 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1370 * @start: the start of the range
1371 * @end: the end of the range
1372 * @step: the step of the range
1374 * Sets @value to the range specified by @start, @end and @step.
1377 gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
1379 guint64 sstart, sstop;
1381 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
1382 g_return_if_fail (start < end);
1383 g_return_if_fail (step > 0);
1384 g_return_if_fail (start % step == 0);
1385 g_return_if_fail (end % step == 0);
1387 sstart = (guint) (start / step);
1388 sstop = (guint) (end / step);
1389 value->data[0].v_uint64 = (sstart << 32) | sstop;
1390 value->data[1].v_int = step;
1394 * gst_value_set_int_range:
1395 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1396 * @start: the start of the range
1397 * @end: the end of the range
1399 * Sets @value to the range specified by @start and @end.
1402 gst_value_set_int_range (GValue * value, gint start, gint end)
1404 gst_value_set_int_range_step (value, start, end, 1);
1408 * gst_value_get_int_range_min:
1409 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1411 * Gets the minimum of the range specified by @value.
1413 * Returns: the minimum of the range
1416 gst_value_get_int_range_min (const GValue * value)
1418 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1420 return INT_RANGE_MIN (value) * INT_RANGE_STEP (value);
1424 * gst_value_get_int_range_max:
1425 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1427 * Gets the maximum of the range specified by @value.
1429 * Returns: the maximum of the range
1432 gst_value_get_int_range_max (const GValue * value)
1434 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1436 return INT_RANGE_MAX (value) * INT_RANGE_STEP (value);
1440 * gst_value_get_int_range_step:
1441 * @value: a GValue initialized to GST_TYPE_INT_RANGE
1443 * Gets the step of the range specified by @value.
1445 * Returns: the step of the range
1448 gst_value_get_int_range_step (const GValue * value)
1450 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
1452 return INT_RANGE_STEP (value);
1456 gst_value_transform_int_range_string (const GValue * src_value,
1457 GValue * dest_value)
1459 if (INT_RANGE_STEP (src_value) == 1)
1460 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
1461 INT_RANGE_MIN (src_value), INT_RANGE_MAX (src_value));
1463 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d,%d]",
1464 INT_RANGE_MIN (src_value) * INT_RANGE_STEP (src_value),
1465 INT_RANGE_MAX (src_value) * INT_RANGE_STEP (src_value),
1466 INT_RANGE_STEP (src_value));
1470 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
1473 /* Compare the ranges. (Kept for clarity for the below comparision) */
1474 if (INT_RANGE_MIN (value1) != INT_RANGE_MIN (value2) ||
1475 INT_RANGE_MAX (value1) != INT_RANGE_MAX (value2))
1476 return GST_VALUE_UNORDERED;
1478 /* The MIN and MAX of the range are actually stored packed into one 64bit
1479 * value. We can therefore compare them directly */
1480 if (value1->data[0].v_uint64 != value2->data[0].v_uint64)
1481 return GST_VALUE_UNORDERED;
1484 /* The extents are equal */
1485 /* If there is only one value (min == max), we ignore the step for
1487 if (INT_RANGE_MIN (value1) == INT_RANGE_MAX (value1))
1488 return GST_VALUE_EQUAL;
1490 /* Else the ranges are only equal if their step is also equal */
1491 if (INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2))
1492 return GST_VALUE_EQUAL;
1493 return GST_VALUE_UNORDERED;
1497 gst_value_serialize_int_range (const GValue * value)
1499 if (INT_RANGE_STEP (value) == 1)
1500 return g_strdup_printf ("[ %d, %d ]", INT_RANGE_MIN (value),
1501 INT_RANGE_MAX (value));
1503 return g_strdup_printf ("[ %d, %d, %d ]",
1504 INT_RANGE_MIN (value) * INT_RANGE_STEP (value),
1505 INT_RANGE_MAX (value) * INT_RANGE_STEP (value), INT_RANGE_STEP (value));
1509 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1511 g_warning ("unimplemented");
1518 * Values in the range are defined as any value greater or equal
1519 * to min*step, AND lesser or equal to max*step.
1520 * For step == 1, this falls back to the traditional range semantics.
1523 #define INT64_RANGE_MIN(v) (((gint64 *)((v)->data[0].v_pointer))[0])
1524 #define INT64_RANGE_MAX(v) (((gint64 *)((v)->data[0].v_pointer))[1])
1525 #define INT64_RANGE_STEP(v) (((gint64 *)((v)->data[0].v_pointer))[2])
1528 gst_value_init_int64_range (GValue * value)
1530 gint64 *vals = g_slice_alloc0 (3 * sizeof (gint64));
1531 value->data[0].v_pointer = vals;
1532 INT64_RANGE_MIN (value) = 0;
1533 INT64_RANGE_MAX (value) = 0;
1534 INT64_RANGE_STEP (value) = 1;
1538 gst_value_free_int64_range (GValue * value)
1540 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1541 g_slice_free1 (3 * sizeof (gint64), value->data[0].v_pointer);
1542 value->data[0].v_pointer = NULL;
1546 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1548 gint64 *vals = (gint64 *) dest_value->data[0].v_pointer;
1549 gint64 *src_vals = (gint64 *) src_value->data[0].v_pointer;
1552 gst_value_init_int64_range (dest_value);
1555 if (src_vals != NULL) {
1556 INT64_RANGE_MIN (dest_value) = INT64_RANGE_MIN (src_value);
1557 INT64_RANGE_MAX (dest_value) = INT64_RANGE_MAX (src_value);
1558 INT64_RANGE_STEP (dest_value) = INT64_RANGE_STEP (src_value);
1563 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1564 GTypeCValue * collect_values, guint collect_flags)
1566 gint64 *vals = value->data[0].v_pointer;
1568 g_return_val_if_fail (n_collect_values == 2,
1569 g_strdup_printf ("not enough value locations for `%s' passed",
1570 G_VALUE_TYPE_NAME (value)));
1572 g_return_val_if_fail (collect_values[0].v_int64 < collect_values[1].v_int64,
1573 g_strdup_printf ("range start is not smaller than end for `%s'",
1574 G_VALUE_TYPE_NAME (value)));
1577 gst_value_init_int64_range (value);
1580 gst_value_set_int64_range_step (value, collect_values[0].v_int64,
1581 collect_values[1].v_int64, 1);
1587 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1588 GTypeCValue * collect_values, guint collect_flags)
1590 guint64 *int_range_start = collect_values[0].v_pointer;
1591 guint64 *int_range_end = collect_values[1].v_pointer;
1592 guint64 *int_range_step = collect_values[2].v_pointer;
1593 gint64 *vals = (gint64 *) value->data[0].v_pointer;
1595 g_return_val_if_fail (int_range_start != NULL,
1596 g_strdup_printf ("start value location for `%s' passed as NULL",
1597 G_VALUE_TYPE_NAME (value)));
1598 g_return_val_if_fail (int_range_end != NULL,
1599 g_strdup_printf ("end value location for `%s' passed as NULL",
1600 G_VALUE_TYPE_NAME (value)));
1601 g_return_val_if_fail (int_range_step != NULL,
1602 g_strdup_printf ("step value location for `%s' passed as NULL",
1603 G_VALUE_TYPE_NAME (value)));
1605 g_return_val_if_fail (vals != NULL,
1606 g_strdup_printf ("Uninitialised `%s' passed", G_VALUE_TYPE_NAME (value)));
1608 *int_range_start = INT64_RANGE_MIN (value);
1609 *int_range_end = INT64_RANGE_MAX (value);
1610 *int_range_step = INT64_RANGE_STEP (value);
1616 * gst_value_set_int64_range_step:
1617 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1618 * @start: the start of the range
1619 * @end: the end of the range
1620 * @step: the step of the range
1622 * Sets @value to the range specified by @start, @end and @step.
1625 gst_value_set_int64_range_step (GValue * value, gint64 start, gint64 end,
1628 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1629 g_return_if_fail (start < end);
1630 g_return_if_fail (step > 0);
1631 g_return_if_fail (start % step == 0);
1632 g_return_if_fail (end % step == 0);
1634 INT64_RANGE_MIN (value) = start / step;
1635 INT64_RANGE_MAX (value) = end / step;
1636 INT64_RANGE_STEP (value) = step;
1640 * gst_value_set_int64_range:
1641 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1642 * @start: the start of the range
1643 * @end: the end of the range
1645 * Sets @value to the range specified by @start and @end.
1648 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1650 gst_value_set_int64_range_step (value, start, end, 1);
1654 * gst_value_get_int64_range_min:
1655 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1657 * Gets the minimum of the range specified by @value.
1659 * Returns: the minimum of the range
1662 gst_value_get_int64_range_min (const GValue * value)
1664 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1666 return INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value);
1670 * gst_value_get_int64_range_max:
1671 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1673 * Gets the maximum of the range specified by @value.
1675 * Returns: the maximum of the range
1678 gst_value_get_int64_range_max (const GValue * value)
1680 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1682 return INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value);
1686 * gst_value_get_int64_range_step:
1687 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1689 * Gets the step of the range specified by @value.
1691 * Returns: the step of the range
1694 gst_value_get_int64_range_step (const GValue * value)
1696 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1698 return INT64_RANGE_STEP (value);
1702 gst_value_transform_int64_range_string (const GValue * src_value,
1703 GValue * dest_value)
1705 if (INT64_RANGE_STEP (src_value) == 1)
1706 dest_value->data[0].v_pointer =
1707 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1708 INT64_RANGE_MIN (src_value), INT64_RANGE_MAX (src_value));
1710 dest_value->data[0].v_pointer =
1711 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT
1712 ",%" G_GINT64_FORMAT "]",
1713 INT64_RANGE_MIN (src_value) * INT64_RANGE_STEP (src_value),
1714 INT64_RANGE_MAX (src_value) * INT64_RANGE_STEP (src_value),
1715 INT64_RANGE_STEP (src_value));
1719 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1721 /* Compare the ranges. */
1722 if (INT64_RANGE_MIN (value1) != INT64_RANGE_MIN (value2) ||
1723 INT64_RANGE_MAX (value1) != INT64_RANGE_MAX (value2))
1724 return GST_VALUE_UNORDERED;
1726 /* The extents are equal */
1727 /* If there is only one value (min == max), we ignore the step for
1729 if (INT64_RANGE_MIN (value1) == INT64_RANGE_MAX (value1))
1730 return GST_VALUE_EQUAL;
1732 /* Else the ranges are only equal if their step is also equal */
1733 if (INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2))
1734 return GST_VALUE_EQUAL;
1735 return GST_VALUE_UNORDERED;
1739 gst_value_serialize_int64_range (const GValue * value)
1741 if (INT64_RANGE_STEP (value) == 1)
1742 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1743 INT64_RANGE_MIN (value), INT64_RANGE_MAX (value));
1745 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT ", %"
1746 G_GINT64_FORMAT " ]",
1747 INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value),
1748 INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value),
1749 INT64_RANGE_STEP (value));
1753 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1755 g_warning ("unimplemented");
1764 gst_value_init_double_range (GValue * value)
1766 value->data[0].v_double = 0;
1767 value->data[1].v_double = 0;
1771 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1773 dest_value->data[0].v_double = src_value->data[0].v_double;
1774 dest_value->data[1].v_double = src_value->data[1].v_double;
1778 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1779 GTypeCValue * collect_values, guint collect_flags)
1781 g_return_val_if_fail (n_collect_values == 2,
1782 g_strdup_printf ("not enough value locations for `%s' passed",
1783 G_VALUE_TYPE_NAME (value)));
1784 g_return_val_if_fail (collect_values[0].v_double < collect_values[1].v_double,
1785 g_strdup_printf ("range start is not smaller than end for `%s'",
1786 G_VALUE_TYPE_NAME (value)));
1788 value->data[0].v_double = collect_values[0].v_double;
1789 value->data[1].v_double = collect_values[1].v_double;
1795 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1796 GTypeCValue * collect_values, guint collect_flags)
1798 gdouble *double_range_start = collect_values[0].v_pointer;
1799 gdouble *double_range_end = collect_values[1].v_pointer;
1801 g_return_val_if_fail (double_range_start != NULL,
1802 g_strdup_printf ("start value location for `%s' passed as NULL",
1803 G_VALUE_TYPE_NAME (value)));
1804 g_return_val_if_fail (double_range_end != NULL,
1805 g_strdup_printf ("end value location for `%s' passed as NULL",
1806 G_VALUE_TYPE_NAME (value)));
1808 *double_range_start = value->data[0].v_double;
1809 *double_range_end = value->data[1].v_double;
1815 * gst_value_set_double_range:
1816 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1817 * @start: the start of the range
1818 * @end: the end of the range
1820 * Sets @value to the range specified by @start and @end.
1823 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1825 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1826 g_return_if_fail (start < end);
1828 value->data[0].v_double = start;
1829 value->data[1].v_double = end;
1833 * gst_value_get_double_range_min:
1834 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1836 * Gets the minimum of the range specified by @value.
1838 * Returns: the minimum of the range
1841 gst_value_get_double_range_min (const GValue * value)
1843 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1845 return value->data[0].v_double;
1849 * gst_value_get_double_range_max:
1850 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1852 * Gets the maximum of the range specified by @value.
1854 * Returns: the maximum of the range
1857 gst_value_get_double_range_max (const GValue * value)
1859 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1861 return value->data[1].v_double;
1865 gst_value_transform_double_range_string (const GValue * src_value,
1866 GValue * dest_value)
1868 gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1870 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1871 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1872 src_value->data[0].v_double),
1873 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1874 src_value->data[1].v_double));
1878 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1880 if (value2->data[0].v_double == value1->data[0].v_double &&
1881 value2->data[1].v_double == value1->data[1].v_double)
1882 return GST_VALUE_EQUAL;
1883 return GST_VALUE_UNORDERED;
1887 gst_value_serialize_double_range (const GValue * value)
1889 gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1890 gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1892 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1893 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1894 return g_strdup_printf ("[ %s, %s ]", d1, d2);
1898 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1900 g_warning ("unimplemented");
1909 gst_value_init_fraction_range (GValue * value)
1914 ftype = GST_TYPE_FRACTION;
1916 value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1917 g_value_init (&vals[0], ftype);
1918 g_value_init (&vals[1], ftype);
1922 gst_value_free_fraction_range (GValue * value)
1924 GValue *vals = (GValue *) value->data[0].v_pointer;
1927 /* we know the two values contain fractions without internal allocs */
1928 /* g_value_unset (&vals[0]); */
1929 /* g_value_unset (&vals[1]); */
1930 g_slice_free1 (2 * sizeof (GValue), vals);
1931 value->data[0].v_pointer = NULL;
1936 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1938 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1939 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1942 gst_value_init_fraction_range (dest_value);
1943 vals = dest_value->data[0].v_pointer;
1945 if (src_vals != NULL) {
1946 g_value_copy (&src_vals[0], &vals[0]);
1947 g_value_copy (&src_vals[1], &vals[1]);
1952 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1953 GTypeCValue * collect_values, guint collect_flags)
1955 GValue *vals = (GValue *) value->data[0].v_pointer;
1957 g_return_val_if_fail (n_collect_values == 4,
1958 g_strdup_printf ("not enough value locations for `%s' passed",
1959 G_VALUE_TYPE_NAME (value)));
1960 g_return_val_if_fail (collect_values[1].v_int != 0,
1961 g_strdup_printf ("passed '0' as first denominator for `%s'",
1962 G_VALUE_TYPE_NAME (value)));
1963 g_return_val_if_fail (collect_values[3].v_int != 0,
1964 g_strdup_printf ("passed '0' as second denominator for `%s'",
1965 G_VALUE_TYPE_NAME (value)));
1966 g_return_val_if_fail (gst_util_fraction_compare (collect_values[0].v_int,
1967 collect_values[1].v_int, collect_values[2].v_int,
1968 collect_values[3].v_int) < 0,
1969 g_strdup_printf ("range start is not smaller than end for `%s'",
1970 G_VALUE_TYPE_NAME (value)));
1973 gst_value_init_fraction_range (value);
1974 vals = value->data[0].v_pointer;
1977 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1978 collect_values[1].v_int);
1979 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1980 collect_values[3].v_int);
1986 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1987 GTypeCValue * collect_values, guint collect_flags)
1990 gint *dest_values[4];
1991 GValue *vals = (GValue *) value->data[0].v_pointer;
1993 g_return_val_if_fail (n_collect_values == 4,
1994 g_strdup_printf ("not enough value locations for `%s' passed",
1995 G_VALUE_TYPE_NAME (value)));
1996 g_return_val_if_fail (vals != NULL,
1997 g_strdup_printf ("Uninitialised `%s' passed", G_VALUE_TYPE_NAME (value)));
1999 for (i = 0; i < 4; i++) {
2000 g_return_val_if_fail (collect_values[i].v_pointer != NULL,
2001 g_strdup_printf ("value location for `%s' passed as NULL",
2002 G_VALUE_TYPE_NAME (value)));
2004 dest_values[i] = collect_values[i].v_pointer;
2007 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
2008 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
2009 dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
2010 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
2015 * gst_value_set_fraction_range:
2016 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2017 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
2018 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
2020 * Sets @value to the range specified by @start and @end.
2023 gst_value_set_fraction_range (GValue * value, const GValue * start,
2028 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
2029 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
2030 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
2031 g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
2032 start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
2034 vals = (GValue *) value->data[0].v_pointer;
2036 gst_value_init_fraction_range (value);
2037 vals = value->data[0].v_pointer;
2039 g_value_copy (start, &vals[0]);
2040 g_value_copy (end, &vals[1]);
2044 * gst_value_set_fraction_range_full:
2045 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2046 * @numerator_start: the numerator start of the range
2047 * @denominator_start: the denominator start of the range
2048 * @numerator_end: the numerator end of the range
2049 * @denominator_end: the denominator end of the range
2051 * Sets @value to the range specified by @numerator_start/@denominator_start
2052 * and @numerator_end/@denominator_end.
2055 gst_value_set_fraction_range_full (GValue * value,
2056 gint numerator_start, gint denominator_start,
2057 gint numerator_end, gint denominator_end)
2059 GValue start = { 0 };
2062 g_return_if_fail (value != NULL);
2063 g_return_if_fail (denominator_start != 0);
2064 g_return_if_fail (denominator_end != 0);
2065 g_return_if_fail (gst_util_fraction_compare (numerator_start,
2066 denominator_start, numerator_end, denominator_end) < 0);
2068 g_value_init (&start, GST_TYPE_FRACTION);
2069 g_value_init (&end, GST_TYPE_FRACTION);
2071 gst_value_set_fraction (&start, numerator_start, denominator_start);
2072 gst_value_set_fraction (&end, numerator_end, denominator_end);
2073 gst_value_set_fraction_range (value, &start, &end);
2075 /* we know the two values contain fractions without internal allocs */
2076 /* g_value_unset (&start); */
2077 /* g_value_unset (&end); */
2080 /* FIXME 2.0: Don't leak the internal representation of fraction
2081 * ranges but instead return the numerator and denominator
2083 * This would allow to store fraction ranges as
2084 * data[0] = (min_n << 32) | (min_d)
2085 * data[1] = (max_n << 32) | (max_d)
2086 * without requiring an additional allocation for each value.
2090 * gst_value_get_fraction_range_min:
2091 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2093 * Gets the minimum of the range specified by @value.
2095 * Returns: (nullable): the minimum of the range
2098 gst_value_get_fraction_range_min (const GValue * value)
2102 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
2104 vals = (GValue *) value->data[0].v_pointer;
2113 * gst_value_get_fraction_range_max:
2114 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
2116 * Gets the maximum of the range specified by @value.
2118 * Returns: (nullable): the maximum of the range
2121 gst_value_get_fraction_range_max (const GValue * value)
2125 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
2127 vals = (GValue *) value->data[0].v_pointer;
2136 gst_value_serialize_fraction_range (const GValue * value)
2138 GValue *vals = (GValue *) value->data[0].v_pointer;
2142 retval = g_strdup ("[ 0/1, 0/1 ]");
2146 start = gst_value_serialize_fraction (&vals[0]);
2147 end = gst_value_serialize_fraction (&vals[1]);
2149 retval = g_strdup_printf ("[ %s, %s ]", start, end);
2158 gst_value_transform_fraction_range_string (const GValue * src_value,
2159 GValue * dest_value)
2161 dest_value->data[0].v_pointer =
2162 gst_value_serialize_fraction_range (src_value);
2166 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
2168 GValue *vals1, *vals2;
2170 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
2171 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
2173 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
2174 return GST_VALUE_UNORDERED;
2176 vals1 = (GValue *) value1->data[0].v_pointer;
2177 vals2 = (GValue *) value2->data[0].v_pointer;
2178 if (gst_value_compare_fraction (&vals1[0], &vals2[0]) == GST_VALUE_EQUAL &&
2179 gst_value_compare_fraction (&vals1[1], &vals2[1]) == GST_VALUE_EQUAL)
2180 return GST_VALUE_EQUAL;
2182 return GST_VALUE_UNORDERED;
2186 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
2188 g_warning ("unimplemented");
2197 * gst_value_set_caps:
2198 * @value: a GValue initialized to GST_TYPE_CAPS
2199 * @caps: (transfer none): the caps to set the value to
2201 * Sets the contents of @value to @caps. A reference to the
2202 * provided @caps will be taken by the @value.
2205 gst_value_set_caps (GValue * value, const GstCaps * caps)
2207 g_return_if_fail (G_IS_VALUE (value));
2208 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
2209 g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
2211 g_value_set_boxed (value, caps);
2215 * gst_value_get_caps:
2216 * @value: a GValue initialized to GST_TYPE_CAPS
2218 * Gets the contents of @value. The reference count of the returned
2219 * #GstCaps will not be modified, therefore the caller must take one
2220 * before getting rid of the @value.
2222 * Returns: (transfer none): the contents of @value
2225 gst_value_get_caps (const GValue * value)
2227 g_return_val_if_fail (G_IS_VALUE (value), NULL);
2228 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
2230 return (GstCaps *) g_value_get_boxed (value);
2234 gst_value_compare_caps (const GValue * value1, const GValue * value2)
2236 GstCaps *caps1 = GST_CAPS (gst_value_get_caps (value1));
2237 GstCaps *caps2 = GST_CAPS (gst_value_get_caps (value2));
2240 return GST_VALUE_EQUAL;
2242 if (!caps1 || !caps2)
2243 return GST_VALUE_UNORDERED;
2245 if (gst_caps_is_equal (caps1, caps2))
2246 return GST_VALUE_EQUAL;
2247 return GST_VALUE_UNORDERED;
2251 gst_value_serialize_caps (const GValue * value)
2253 GstCaps *caps = g_value_get_boxed (value);
2254 return priv_gst_string_take_and_wrap (gst_caps_to_string (caps));
2258 gst_value_deserialize_caps (GValue * dest, const gchar * s)
2263 /* this can happen if caps are ANY, EMPTY, or only contains a single
2264 * empty structure */
2265 caps = gst_caps_from_string (s);
2267 gchar *str = gst_string_unwrap (s);
2269 if (G_UNLIKELY (!str))
2272 caps = gst_caps_from_string (str);
2277 g_value_take_boxed (dest, caps);
2283 /********************************************
2284 * Serialization/deserialization of GValues *
2285 ********************************************/
2287 static GstValueAbbreviation *
2288 _priv_gst_value_get_abbrs (gint * n_abbrs)
2290 static GstValueAbbreviation *abbrs = NULL;
2291 static gsize num = 0;
2293 if (g_once_init_enter (&num)) {
2294 /* dynamically generate the array */
2296 GstValueAbbreviation dyn_abbrs[] = {
2301 {"uint", G_TYPE_UINT}
2305 {"float", G_TYPE_FLOAT}
2309 {"double", G_TYPE_DOUBLE}
2311 {"d", G_TYPE_DOUBLE}
2313 {"buffer", GST_TYPE_BUFFER}
2315 {"fraction", GST_TYPE_FRACTION}
2317 {"boolean", G_TYPE_BOOLEAN}
2319 {"bool", G_TYPE_BOOLEAN}
2321 {"b", G_TYPE_BOOLEAN}
2323 {"string", G_TYPE_STRING}
2325 {"str", G_TYPE_STRING}
2327 {"s", G_TYPE_STRING}
2329 {"structure", GST_TYPE_STRUCTURE}
2331 {"date", G_TYPE_DATE}
2333 {"datetime", GST_TYPE_DATE_TIME}
2335 {"bitmask", GST_TYPE_BITMASK}
2337 {"flagset", GST_TYPE_FLAG_SET}
2339 {"sample", GST_TYPE_SAMPLE}
2341 {"taglist", GST_TYPE_TAG_LIST}
2343 {"type", G_TYPE_GTYPE}
2345 {"array", GST_TYPE_ARRAY}
2347 {"list", GST_TYPE_LIST}
2349 _num = G_N_ELEMENTS (dyn_abbrs);
2350 /* permanently allocate and copy the array now */
2351 abbrs = g_new0 (GstValueAbbreviation, _num);
2352 memcpy (abbrs, dyn_abbrs, sizeof (GstValueAbbreviation) * _num);
2353 g_once_init_leave (&num, _num);
2360 /* given a type_name that could be a type abbreviation or a registered GType,
2361 * return a matching GType */
2363 _priv_gst_value_gtype_from_abbr (const char *type_name)
2366 GstValueAbbreviation *abbrs;
2370 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
2372 abbrs = _priv_gst_value_get_abbrs (&n_abbrs);
2374 for (i = 0; i < n_abbrs; i++) {
2375 if (strcmp (type_name, abbrs[i].type_name) == 0) {
2376 return abbrs[i].type;
2380 /* this is the fallback */
2381 ret = g_type_from_name (type_name);
2382 /* If not found, try it as a dynamic type */
2383 if (G_UNLIKELY (ret == 0))
2384 ret = gst_dynamic_type_factory_load (type_name);
2390 _priv_gst_value_gtype_to_abbr (GType type)
2393 GstValueAbbreviation *abbrs;
2396 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
2398 abbrs = _priv_gst_value_get_abbrs (&n_abbrs);
2400 for (i = 0; i < n_abbrs; i++) {
2401 if (type == abbrs[i].type) {
2402 return abbrs[i].type_name;
2406 return g_type_name (type);
2410 * _priv_gst_value_parse_string:
2411 * @s: string to parse
2412 * @end: out-pointer to char behind end of string
2413 * @next: out-pointer to start of unread data
2414 * @unescape: @TRUE if the substring is escaped.
2416 * Find the end of a sub-string. If end == next, the string will not be
2417 * null-terminated. In all other cases it will be.
2419 * Note: This function modifies the string in @s (if unescape == @TRUE).
2421 * Returns: @TRUE if a sub-string was found and @FALSE if the string is not
2425 _priv_gst_value_parse_string (gchar * s, gchar ** end, gchar ** next,
2434 int ret = _priv_gst_value_parse_simple_string (s, end);
2440 /* Find the closing quotes */
2445 if (G_UNLIKELY (*s == 0))
2447 if (G_UNLIKELY (*s == '\\')) {
2449 if (G_UNLIKELY (*s == 0))
2460 if (G_UNLIKELY (*s == 0))
2462 if (G_UNLIKELY (*s == '\\')) {
2464 if (G_UNLIKELY (*s == 0))
2480 _priv_gst_value_parse_range (gchar * s, gchar ** after, GValue * value,
2483 GValue value1 = { 0 };
2484 GValue value2 = { 0 };
2485 GValue value3 = { 0 };
2487 gboolean ret, have_step = FALSE;
2493 ret = _priv_gst_value_parse_value (s, &s, &value1, type, NULL);
2497 while (g_ascii_isspace (*s))
2504 while (g_ascii_isspace (*s))
2507 ret = _priv_gst_value_parse_value (s, &s, &value2, type, NULL);
2511 while (g_ascii_isspace (*s))
2514 /* optional step for int and int64 */
2515 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
2516 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2520 while (g_ascii_isspace (*s))
2523 ret = _priv_gst_value_parse_value (s, &s, &value3, type, NULL);
2527 while (g_ascii_isspace (*s))
2538 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2540 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2543 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2544 range_type = GST_TYPE_DOUBLE_RANGE;
2545 g_value_init (value, range_type);
2546 gst_value_set_double_range (value,
2547 gst_g_value_get_double_unchecked (&value1),
2548 gst_g_value_get_double_unchecked (&value2));
2549 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2550 range_type = GST_TYPE_INT_RANGE;
2551 g_value_init (value, range_type);
2553 gst_value_set_int_range_step (value,
2554 gst_g_value_get_int_unchecked (&value1),
2555 gst_g_value_get_int_unchecked (&value2),
2556 gst_g_value_get_int_unchecked (&value3));
2558 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2559 gst_g_value_get_int_unchecked (&value2));
2560 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2561 range_type = GST_TYPE_INT64_RANGE;
2562 g_value_init (value, range_type);
2564 gst_value_set_int64_range_step (value,
2565 gst_g_value_get_int64_unchecked (&value1),
2566 gst_g_value_get_int64_unchecked (&value2),
2567 gst_g_value_get_int64_unchecked (&value3));
2569 gst_value_set_int64_range (value,
2570 gst_g_value_get_int64_unchecked (&value1),
2571 gst_g_value_get_int64_unchecked (&value2));
2572 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2573 range_type = GST_TYPE_FRACTION_RANGE;
2574 g_value_init (value, range_type);
2575 gst_value_set_fraction_range (value, &value1, &value2);
2584 g_value_unset (value);
2585 g_value_unset (&value1);
2586 g_value_unset (&value2);
2587 g_value_unset (&value3);
2592 _priv_gst_value_parse_any_list (gchar * s, gchar ** after, GValue * value,
2593 GType type, char begin, char end, GParamSpec * pspec)
2595 GValue list_value = { 0 };
2597 GstValueList *vlist = VALUE_LIST_ARRAY (value);
2598 GParamSpec *element_spec = NULL;
2601 element_spec = GST_PARAM_SPEC_ARRAY_LIST (pspec)->element_spec;
2607 while (g_ascii_isspace (*s))
2613 while (g_ascii_isspace (*s))
2622 memset (&list_value, 0, sizeof (list_value));
2624 ret = _priv_gst_value_parse_value (s, &s, &list_value, type, element_spec);
2628 _gst_value_list_append_val (vlist, &list_value);
2630 while (g_ascii_isspace (*s))
2633 if (*s != ',' && *s != end)
2644 _priv_gst_value_parse_list (gchar * s, gchar ** after, GValue * value,
2645 GType type, GParamSpec * pspec)
2647 return _priv_gst_value_parse_any_list (s, after, value, type, '{', '}',
2652 _priv_gst_value_parse_array (gchar * s, gchar ** after, GValue * value,
2653 GType type, GParamSpec * pspec)
2655 return _priv_gst_value_parse_any_list (s, after, value, type, '<', '>',
2660 _priv_gst_value_parse_simple_string (gchar * str, gchar ** end)
2664 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2674 _priv_gst_value_parse_struct_or_caps (gchar * str, gchar ** after, GType type,
2678 gboolean ret = FALSE;
2679 gchar *s = str, t, *start, *end, *next;
2703 g_value_init (value, type);
2704 if (priv_gst_structure_parse_name (str, &start, &end, &next, TRUE))
2705 ret = gst_value_deserialize (value, str);
2706 if (G_UNLIKELY (!ret)) {
2708 g_value_unset (value);
2715 _priv_gst_value_parse_range_struct_caps (gchar * s, gchar ** after,
2716 GValue * value, GType type)
2720 gboolean ret = FALSE;
2721 GType try_types[] = {
2726 if (type == GST_TYPE_CAPS || type == GST_TYPE_STRUCTURE)
2727 ret = _priv_gst_value_parse_struct_or_caps (tmp, &tmp, type, value);
2733 ret = _priv_gst_value_parse_range (tmp, &tmp, value, type);
2737 if (type != G_TYPE_INVALID)
2740 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2742 ret = _priv_gst_value_parse_struct_or_caps (tmp, &tmp, try_types[i], value);
2755 _priv_gst_value_parse_value (gchar * str,
2756 gchar ** after, GValue * value, GType default_type, GParamSpec * pspec)
2765 GType type = default_type;
2768 while (g_ascii_isspace (*s))
2771 /* check if there's a (type_name) 'cast' */
2776 while (g_ascii_isspace (*s))
2779 if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &type_end)))
2782 while (g_ascii_isspace (*s))
2784 if (G_UNLIKELY (*s != ')'))
2787 while (g_ascii_isspace (*s))
2792 type = _priv_gst_value_gtype_from_abbr (type_name);
2793 GST_DEBUG ("trying type name '%s'", type_name);
2796 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2797 GST_WARNING ("invalid type");
2801 type = G_PARAM_SPEC_VALUE_TYPE (pspec);
2804 while (g_ascii_isspace (*s))
2807 ret = _priv_gst_value_parse_range_struct_caps (s, &s, value, type);
2808 } else if (*s == '{') {
2809 g_value_init (value, GST_TYPE_LIST);
2810 ret = _priv_gst_value_parse_list (s, &s, value, type, pspec);
2811 } else if (*s == '<') {
2812 g_value_init (value, GST_TYPE_ARRAY);
2813 ret = _priv_gst_value_parse_array (s, &s, value, type, pspec);
2817 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2819 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, GST_TYPE_FLAG_SET,
2820 G_TYPE_BOOLEAN, G_TYPE_STRING
2824 gboolean check_wrapped_non_string;
2826 if (G_UNLIKELY (!_priv_gst_value_parse_string (s, &value_end, &s, FALSE)))
2828 /* Set NULL terminator for deserialization */
2829 value_size = value_end - value_s;
2830 value_s = g_strndup (value_s, value_end - value_s);
2831 /* Keep old broken behavior where "2" could be interpretted as an int */
2832 check_wrapped_non_string = value_s[0] == '"' &&
2833 strlen (value_s) >= 2 && value_end[-1] == '"';
2835 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2836 g_value_init (value, try_types[i]);
2837 if (try_types[i] != G_TYPE_STRING && check_wrapped_non_string) {
2838 value_s[value_size - 1] = '\0';
2839 ret = gst_value_deserialize (value, value_s + 1);
2840 value_s[value_size - 1] = '"';
2842 const gchar *type_name = g_type_name (try_types[i]);
2844 g_warning ("Received a structure string that contains "
2845 "'=%s'. Reading as a %s value, rather than a string "
2846 "value. This is undesired behaviour, and with GStreamer 1.22 "
2847 " onward, this will be interpreted as a string value instead "
2848 "because it is wrapped in '\"' quotes. If you want to "
2849 "guarantee this value is read as a string, before this "
2850 "change, use '=(string)%s' instead. If you want to read "
2851 "in a %s value, leave its value unquoted.",
2852 value_s, type_name, value_s, type_name);
2856 ret = gst_value_deserialize (value, value_s);
2860 g_value_unset (value);
2863 g_value_init (value, type);
2865 if (G_UNLIKELY (!_priv_gst_value_parse_string (s, &value_end, &s, FALSE)))
2867 /* Set NULL terminator for deserialization */
2868 value_s = g_strndup (value_s, value_end - value_s);
2870 ret = gst_value_deserialize_with_pspec (value, value_s, pspec);
2871 if (G_UNLIKELY (!ret))
2872 g_value_unset (value);
2887 gst_value_serialize_segment_internal (const GValue * value, gboolean escape)
2889 GstSegment *seg = g_value_get_boxed (value);
2893 s = gst_structure_new_id (GST_QUARK (SEGMENT),
2894 GST_QUARK (FLAGS), GST_TYPE_SEGMENT_FLAGS, seg->flags,
2895 GST_QUARK (RATE), G_TYPE_DOUBLE, seg->rate,
2896 GST_QUARK (APPLIED_RATE), G_TYPE_DOUBLE, seg->applied_rate,
2897 GST_QUARK (FORMAT), GST_TYPE_FORMAT, seg->format,
2898 GST_QUARK (BASE), G_TYPE_UINT64, seg->base,
2899 GST_QUARK (OFFSET), G_TYPE_UINT64, seg->offset,
2900 GST_QUARK (START), G_TYPE_UINT64, seg->start,
2901 GST_QUARK (STOP), G_TYPE_UINT64, seg->stop,
2902 GST_QUARK (TIME), G_TYPE_UINT64, seg->time,
2903 GST_QUARK (POSITION), G_TYPE_UINT64, seg->position,
2904 GST_QUARK (DURATION), G_TYPE_UINT64, seg->duration, NULL);
2906 t = gst_structure_to_string (s);
2908 res = g_strdup_printf ("\"%s\"", t);
2913 gst_structure_free (s);
2919 gst_value_serialize_segment (const GValue * value)
2921 return gst_value_serialize_segment_internal (value, TRUE);
2925 gst_value_deserialize_segment_internal (GValue * dest, const gchar * s,
2936 if (G_UNLIKELY (*s != '"' || len < 2 || s[len - 1] != '"')) {
2937 /* "\"" is not an accepted string, so len must be at least 2 */
2938 GST_ERROR ("Failed deserializing segement: expected string to start and "
2942 t = g_strdup (s + 1);
2944 /* removed trailing '"' */
2945 str = gst_structure_from_string (t, NULL);
2948 str = gst_structure_from_string (s, NULL);
2950 if (G_UNLIKELY (str == NULL))
2953 res = gst_structure_id_get (str,
2954 GST_QUARK (FLAGS), GST_TYPE_SEGMENT_FLAGS, &seg.flags,
2955 GST_QUARK (RATE), G_TYPE_DOUBLE, &seg.rate,
2956 GST_QUARK (APPLIED_RATE), G_TYPE_DOUBLE, &seg.applied_rate,
2957 GST_QUARK (FORMAT), GST_TYPE_FORMAT, &seg.format,
2958 GST_QUARK (BASE), G_TYPE_UINT64, &seg.base,
2959 GST_QUARK (OFFSET), G_TYPE_UINT64, &seg.offset,
2960 GST_QUARK (START), G_TYPE_UINT64, &seg.start,
2961 GST_QUARK (STOP), G_TYPE_UINT64, &seg.stop,
2962 GST_QUARK (TIME), G_TYPE_UINT64, &seg.time,
2963 GST_QUARK (POSITION), G_TYPE_UINT64, &seg.position,
2964 GST_QUARK (DURATION), G_TYPE_UINT64, &seg.duration, NULL);
2965 gst_structure_free (str);
2968 g_value_set_boxed (dest, &seg);
2974 gst_value_deserialize_segment (GValue * dest, const gchar * s)
2976 return gst_value_deserialize_segment_internal (dest, s, TRUE);
2984 * gst_value_set_structure:
2985 * @value: a GValue initialized to GST_TYPE_STRUCTURE
2986 * @structure: the structure to set the value to
2988 * Sets the contents of @value to @structure.
2991 gst_value_set_structure (GValue * value, const GstStructure * structure)
2993 g_return_if_fail (G_IS_VALUE (value));
2994 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
2995 g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
2997 g_value_set_boxed (value, structure);
3001 * gst_value_get_structure:
3002 * @value: a GValue initialized to GST_TYPE_STRUCTURE
3004 * Gets the contents of @value.
3006 * Returns: (transfer none): the contents of @value
3008 const GstStructure *
3009 gst_value_get_structure (const GValue * value)
3011 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3012 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
3014 return (GstStructure *) g_value_get_boxed (value);
3018 gst_value_serialize_structure (const GValue * value)
3020 GstStructure *structure = g_value_get_boxed (value);
3022 return priv_gst_string_take_and_wrap (gst_structure_to_string (structure));
3023 /* string should always end up being wrapped, since a structure string
3024 * ends in a ';' character */
3028 gst_value_deserialize_structure (GValue * dest, const gchar * s)
3030 GstStructure *structure;
3033 /* the output of gst_value_serialize_structure would never produce
3034 * such a string, but a user may pass to gst_structure_from_string
3036 * name, sub=(GstStructure)sub-name, val=(int)5;
3037 * and expect sub to be read as an *empty* structure with the name
3038 * sub-name. Similar to
3039 * name, caps=(GstCaps)video/x-raw, val=(int)5;
3040 * which gst_structure_to_string can produce. */
3041 structure = gst_structure_from_string (s, NULL);
3043 gchar *str = gst_string_unwrap (s);
3045 if (G_UNLIKELY (!str))
3048 structure = gst_structure_from_string (str, NULL);
3052 if (G_LIKELY (structure)) {
3053 g_value_take_boxed (dest, structure);
3060 gst_value_compare_structure (const GValue * value1, const GValue * value2)
3062 GstStructure *structure1 = GST_STRUCTURE (g_value_get_boxed (value1));
3063 GstStructure *structure2 = GST_STRUCTURE (g_value_get_boxed (value2));
3065 if (structure1 == structure2)
3066 return GST_VALUE_EQUAL;
3068 if (!structure1 || !structure2)
3069 return GST_VALUE_UNORDERED;
3071 if (gst_structure_is_equal (structure1, structure2))
3072 return GST_VALUE_EQUAL;
3074 return GST_VALUE_UNORDERED;
3077 /*******************
3079 *******************/
3082 * gst_value_set_caps_features:
3083 * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
3084 * @features: the features to set the value to
3086 * Sets the contents of @value to @features.
3089 gst_value_set_caps_features (GValue * value, const GstCapsFeatures * features)
3091 g_return_if_fail (G_IS_VALUE (value));
3092 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES);
3093 g_return_if_fail (features == NULL || GST_IS_CAPS_FEATURES (features));
3095 g_value_set_boxed (value, features);
3099 * gst_value_get_caps_features:
3100 * @value: a GValue initialized to GST_TYPE_CAPS_FEATURES
3102 * Gets the contents of @value.
3104 * Returns: (transfer none): the contents of @value
3106 const GstCapsFeatures *
3107 gst_value_get_caps_features (const GValue * value)
3109 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3110 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS_FEATURES, NULL);
3112 return (GstCapsFeatures *) g_value_get_boxed (value);
3116 gst_value_serialize_caps_features (const GValue * value)
3118 GstCapsFeatures *features = g_value_get_boxed (value);
3120 return priv_gst_string_take_and_wrap (gst_caps_features_to_string (features));
3124 gst_value_deserialize_caps_features (GValue * dest, const gchar * s)
3126 GstCapsFeatures *features;
3129 /* This can happen if gst_caps_features_to_string only returns
3130 * ALL, NONE, or a single features name, which means it is not
3131 * actually wrapped by priv_gst_string_take_and_wrap */
3132 features = gst_caps_features_from_string (s);
3134 gchar *str = gst_string_unwrap (s);
3136 if (G_UNLIKELY (!str))
3139 features = gst_caps_features_from_string (str);
3143 if (G_LIKELY (features)) {
3144 g_value_take_boxed (dest, features);
3154 gst_value_compare_tag_list (const GValue * value1, const GValue * value2)
3156 GstTagList *taglist1 = GST_TAG_LIST (g_value_get_boxed (value1));
3157 GstTagList *taglist2 = GST_TAG_LIST (g_value_get_boxed (value2));
3159 if (gst_tag_list_is_equal (taglist1, taglist2))
3160 return GST_VALUE_EQUAL;
3161 return GST_VALUE_UNORDERED;
3165 gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
3167 GstTagList *taglist;
3170 /* the output of gst_value_serialize_tag_list would never produce
3171 * such a string, but a user may pass to gst_structure_from_string
3173 * name, list=(GstTagList)taglist, val=(int)5;
3174 * and expect list to be read as an *empty* tag list. Similar to
3175 * name, caps=(GstCaps)video/x-raw, val=(int)5;
3176 * which gst_structure_to_string can produce. */
3177 taglist = gst_tag_list_new_from_string (s);
3179 gchar *str = gst_string_unwrap (s);
3181 if (G_UNLIKELY (!str))
3184 taglist = gst_tag_list_new_from_string (str);
3188 if (G_LIKELY (taglist != NULL)) {
3189 g_value_take_boxed (dest, taglist);
3196 gst_value_serialize_tag_list (const GValue * value)
3198 GstTagList *taglist = g_value_get_boxed (value);
3200 return priv_gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
3201 /* string should always end up being wrapped, since a taglist (structure)
3202 * string ends in a ';' character */
3211 compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
3214 GstMapInfo info1, info2;
3218 return GST_VALUE_EQUAL;
3220 size1 = gst_buffer_get_size (buf1);
3221 size2 = gst_buffer_get_size (buf2);
3224 return GST_VALUE_UNORDERED;
3227 return GST_VALUE_EQUAL;
3229 if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
3230 return GST_VALUE_UNORDERED;
3232 if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
3233 gst_buffer_unmap (buf1, &info1);
3234 return GST_VALUE_UNORDERED;
3237 mret = memcmp (info1.data, info2.data, info1.size);
3239 result = GST_VALUE_EQUAL;
3241 result = GST_VALUE_LESS_THAN;
3243 result = GST_VALUE_GREATER_THAN;
3245 gst_buffer_unmap (buf1, &info1);
3246 gst_buffer_unmap (buf2, &info2);
3252 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
3254 GstBuffer *buf1 = gst_value_get_buffer (value1);
3255 GstBuffer *buf2 = gst_value_get_buffer (value2);
3257 return compare_buffer (buf1, buf2);
3261 gst_value_serialize_buffer (const GValue * value)
3269 buffer = gst_value_get_buffer (value);
3273 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
3278 string = g_malloc (info.size * 2 + 1);
3279 for (i = 0; i < info.size; i++) {
3280 sprintf (string + i * 2, "%02x", data[i]);
3282 string[info.size * 2] = 0;
3284 gst_buffer_unmap (buffer, &info);
3290 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
3303 buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
3304 if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
3308 for (i = 0; i < len / 2; i++) {
3309 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
3312 ts[0] = s[i * 2 + 0];
3313 ts[1] = s[i * 2 + 1];
3316 data[i] = (guint8) strtoul (ts, NULL, 16);
3318 gst_buffer_unmap (buffer, &info);
3320 gst_value_take_buffer (dest, buffer);
3335 gst_buffer_unref (buffer);
3336 gst_buffer_unmap (buffer, &info);
3345 /* This function is mostly used for comparing image/buffer tags in taglists */
3347 gst_value_compare_sample (const GValue * value1, const GValue * value2)
3349 GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
3350 GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
3352 /* FIXME: should we take into account anything else such as caps? */
3353 return compare_buffer (buf1, buf2);
3357 gst_value_serialize_sample (const GValue * value)
3359 const GstStructure *info_structure;
3360 GstSegment *segment;
3364 GValue val = { 0, };
3365 gchar *info_str, *caps_str, *tmp;
3366 gchar *buf_str, *seg_str, *s;
3368 sample = g_value_get_boxed (value);
3370 buffer = gst_sample_get_buffer (sample);
3372 g_value_init (&val, GST_TYPE_BUFFER);
3373 g_value_set_boxed (&val, buffer);
3374 buf_str = gst_value_serialize_buffer (&val);
3375 g_value_unset (&val);
3377 buf_str = g_strdup ("None");
3380 caps = gst_sample_get_caps (sample);
3382 tmp = gst_caps_to_string (caps);
3383 caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3384 g_strdelimit (caps_str, "=", '_');
3387 caps_str = g_strdup ("None");
3390 segment = gst_sample_get_segment (sample);
3392 g_value_init (&val, GST_TYPE_SEGMENT);
3393 g_value_set_boxed (&val, segment);
3394 tmp = gst_value_serialize_segment_internal (&val, FALSE);
3395 seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3396 g_strdelimit (seg_str, "=", '_');
3398 g_value_unset (&val);
3400 seg_str = g_strdup ("None");
3403 info_structure = gst_sample_get_info (sample);
3404 if (info_structure) {
3405 tmp = gst_structure_to_string (info_structure);
3406 info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
3407 g_strdelimit (info_str, "=", '_');
3410 info_str = g_strdup ("None");
3413 s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
3423 gst_value_deserialize_sample (GValue * dest, const gchar * s)
3425 GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
3428 GstCaps *caps = NULL;
3429 gboolean ret = FALSE;
3434 GST_TRACE ("deserialize '%s'", s);
3436 fields = g_strsplit (s, ":", -1);
3437 len = g_strv_length (fields);
3441 g_value_init (&bval, GST_TYPE_BUFFER);
3442 g_value_init (&sval, GST_TYPE_SEGMENT);
3444 if (!gst_value_deserialize_buffer (&bval, fields[0]))
3447 if (strcmp (fields[1], "None") != 0) {
3448 g_strdelimit (fields[1], "_", '=');
3449 g_base64_decode_inplace (fields[1], &outlen);
3450 GST_TRACE ("caps : %s", fields[1]);
3451 caps = gst_caps_from_string (fields[1]);
3456 if (strcmp (fields[2], "None") != 0) {
3457 g_strdelimit (fields[2], "_", '=');
3458 g_base64_decode_inplace (fields[2], &outlen);
3459 GST_TRACE ("segment : %s", fields[2]);
3460 if (!gst_value_deserialize_segment_internal (&sval, fields[2], FALSE))
3464 if (strcmp (fields[3], "None") != 0) {
3465 g_strdelimit (fields[3], "_", '=');
3466 g_base64_decode_inplace (fields[3], &outlen);
3467 GST_TRACE ("info : %s", fields[3]);
3468 info = gst_structure_from_string (fields[3], NULL);
3475 sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
3476 g_value_get_boxed (&sval), info);
3478 g_value_take_boxed (dest, sample);
3484 gst_caps_unref (caps);
3485 g_value_unset (&bval);
3486 g_value_unset (&sval);
3490 g_strfreev (fields);
3500 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
3502 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
3503 return GST_VALUE_EQUAL;
3504 return GST_VALUE_UNORDERED;
3508 gst_value_serialize_boolean (const GValue * value)
3510 if (value->data[0].v_int) {
3511 return g_strdup ("true");
3513 return g_strdup ("false");
3517 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
3519 gboolean ret = FALSE;
3521 if (g_ascii_strcasecmp (s, "true") == 0 ||
3522 g_ascii_strcasecmp (s, "yes") == 0 ||
3523 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
3524 g_value_set_boolean (dest, TRUE);
3526 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
3527 g_ascii_strcasecmp (s, "no") == 0 ||
3528 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
3529 g_value_set_boolean (dest, FALSE);
3536 #define CREATE_SERIALIZATION_START(_type,_macro) \
3538 gst_value_compare_ ## _type \
3539 (const GValue * value1, const GValue * value2) \
3541 g ## _type val1 = g_value_get_ ## _type (value1); \
3542 g ## _type val2 = g_value_get_ ## _type (value2); \
3544 return GST_VALUE_GREATER_THAN; \
3546 return GST_VALUE_LESS_THAN; \
3547 return GST_VALUE_EQUAL; \
3551 gst_value_serialize_ ## _type (const GValue * value) \
3553 GValue val = { 0, }; \
3554 g_value_init (&val, G_TYPE_STRING); \
3555 if (!g_value_transform (value, &val)) \
3556 g_assert_not_reached (); \
3557 /* NO_COPY_MADNESS!!! */ \
3558 return (char *) g_value_get_string (&val); \
3561 /* deserialize the given s into to as a gint64.
3562 * check if the result is actually storeable in the given size number of
3566 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
3567 gint64 min, gint64 max, gint size)
3569 gboolean ret = FALSE;
3574 *to = g_ascii_strtoull (s, &end, 0);
3575 /* a range error is a definitive no-no */
3576 if (errno == ERANGE) {
3583 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
3584 *to = G_LITTLE_ENDIAN;
3586 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
3589 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
3592 } else if (g_ascii_strcasecmp (s, "min") == 0) {
3595 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3601 /* by definition, a gint64 fits into a gint64; so ignore those */
3602 if (size != sizeof (mask)) {
3604 /* for positive numbers, we create a mask of 1's outside of the range
3605 * and 0's inside the range. An and will thus keep only 1 bits
3606 * outside of the range */
3607 mask <<= (size * 8);
3608 if ((mask & *to) != 0) {
3612 /* for negative numbers, we do a 2's complement version */
3613 mask <<= ((size * 8) - 1);
3614 if ((mask & *to) != mask) {
3623 #define CREATE_SERIALIZATION(_type,_macro) \
3624 CREATE_SERIALIZATION_START(_type,_macro) \
3627 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
3631 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
3632 G_MAX ## _macro, sizeof (g ## _type))) { \
3633 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
3640 #define CREATE_USERIALIZATION(_type,_macro) \
3641 CREATE_SERIALIZATION_START(_type,_macro) \
3644 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
3648 gboolean ret = FALSE; \
3651 x = g_ascii_strtoull (s, &end, 0); \
3652 /* a range error is a definitive no-no */ \
3653 if (errno == ERANGE) { \
3656 /* the cast ensures the range check later on makes sense */ \
3657 x = (g ## _type) x; \
3661 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
3662 x = G_LITTLE_ENDIAN; \
3664 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
3667 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
3670 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
3673 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
3674 x = G_MAX ## _macro; \
3679 if (x > G_MAX ## _macro) { \
3682 g_value_set_ ## _type (dest, x); \
3688 CREATE_SERIALIZATION (int, INT);
3689 CREATE_SERIALIZATION (int64, INT64);
3690 CREATE_SERIALIZATION (long, LONG);
3692 CREATE_USERIALIZATION (uint, UINT);
3693 CREATE_USERIALIZATION (uint64, UINT64);
3694 CREATE_USERIALIZATION (ulong, ULONG);
3696 /* FIXME 2.0: remove this again, plugins shouldn't have uchar properties */
3698 #define G_MAXUCHAR 255
3700 CREATE_USERIALIZATION (uchar, UCHAR);
3706 gst_value_compare_double (const GValue * value1, const GValue * value2)
3708 if (value1->data[0].v_double > value2->data[0].v_double)
3709 return GST_VALUE_GREATER_THAN;
3710 if (value1->data[0].v_double < value2->data[0].v_double)
3711 return GST_VALUE_LESS_THAN;
3712 if (value1->data[0].v_double == value2->data[0].v_double)
3713 return GST_VALUE_EQUAL;
3714 return GST_VALUE_UNORDERED;
3718 gst_value_serialize_double (const GValue * value)
3720 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
3722 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
3723 return g_strdup (d);
3727 gst_value_deserialize_double (GValue * dest, const gchar * s)
3730 gboolean ret = FALSE;
3733 x = g_ascii_strtod (s, &end);
3737 if (g_ascii_strcasecmp (s, "min") == 0) {
3740 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3746 g_value_set_double (dest, x);
3756 gst_value_compare_float (const GValue * value1, const GValue * value2)
3758 if (value1->data[0].v_float > value2->data[0].v_float)
3759 return GST_VALUE_GREATER_THAN;
3760 if (value1->data[0].v_float < value2->data[0].v_float)
3761 return GST_VALUE_LESS_THAN;
3762 if (value1->data[0].v_float == value2->data[0].v_float)
3763 return GST_VALUE_EQUAL;
3764 return GST_VALUE_UNORDERED;
3768 gst_value_serialize_float (const GValue * value)
3770 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
3772 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
3773 return g_strdup (d);
3777 gst_value_deserialize_float (GValue * dest, const gchar * s)
3780 gboolean ret = FALSE;
3783 x = g_ascii_strtod (s, &end);
3787 if (g_ascii_strcasecmp (s, "min") == 0) {
3790 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3795 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
3798 g_value_set_float (dest, (float) x);
3808 gst_value_compare_string (const GValue * value1, const GValue * value2)
3810 if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
3811 /* if only one is NULL, no match - otherwise both NULL == EQUAL */
3812 if (value1->data[0].v_pointer != value2->data[0].v_pointer)
3813 return GST_VALUE_UNORDERED;
3815 gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
3818 return GST_VALUE_LESS_THAN;
3820 return GST_VALUE_GREATER_THAN;
3823 return GST_VALUE_EQUAL;
3827 gst_string_measure_wrapping (const gchar * s)
3830 gboolean wrap = FALSE;
3832 if (G_UNLIKELY (s == NULL))
3835 /* Special case: the actual string NULL needs wrapping */
3836 if (G_UNLIKELY (strcmp (s, "NULL") == 0))
3841 if (GST_ASCII_IS_STRING (*s)) {
3843 } else if (*s < 0x20 || *s >= 0x7f) {
3853 /* Wrap the string if we found something that needs
3854 * wrapping, or the empty string (len == 0) */
3855 return (wrap || len == 0) ? len : -1;
3859 gst_string_wrap_inner (const gchar * s, gint len)
3863 e = d = g_malloc (len + 3);
3867 if (GST_ASCII_IS_STRING (*s)) {
3869 } else if (*s < 0x20 || *s >= 0x7f) {
3871 *e++ = '0' + ((*(guchar *) s) >> 6);
3872 *e++ = '0' + (((*s) >> 3) & 0x7);
3873 *e++ = '0' + ((*s++) & 0x7);
3882 g_assert (e - d <= len + 3);
3886 /* Do string wrapping/escaping */
3888 gst_string_wrap (const gchar * s)
3890 gint len = gst_string_measure_wrapping (s);
3892 if (G_LIKELY (len < 0))
3893 return g_strdup (s);
3895 return gst_string_wrap_inner (s, len);
3898 /* Same as above, but take ownership of the string */
3900 priv_gst_string_take_and_wrap (gchar * s)
3903 gint len = gst_string_measure_wrapping (s);
3905 if (G_LIKELY (len < 0))
3908 out = gst_string_wrap_inner (s, len);
3915 * This function takes a string delimited with double quotes (")
3916 * and unescapes any \xxx octal numbers.
3918 * If sequences of \y are found where y is not in the range of
3919 * 0->3, y is copied unescaped.
3921 * If \xyy is found where x is an octal number but y is not, an
3922 * error is encountered and %NULL is returned.
3924 * the input string must be \0 terminated.
3927 gst_string_unwrap (const gchar * s)
3930 gchar *read, *write;
3932 /* NULL string returns NULL */
3936 /* strings not starting with " are invalid */
3940 /* make copy of original string to hold the result. This
3941 * string will always be smaller than the original */
3946 /* need to move to the next position as we parsed the " */
3950 if (GST_ASCII_IS_STRING (*read)) {
3951 /* normal chars are just copied */
3953 } else if (*read == '"') {
3954 /* quote marks end of string */
3956 } else if (*read == '\\') {
3957 /* got an escape char, move to next position to read a tripplet
3958 * of octal numbers */
3960 /* is the next char a possible first octal number? */
3961 if (*read >= '0' && *read <= '3') {
3962 /* parse other 2 numbers, if one of them is not in the range of
3963 * an octal number, we error. We also catch the case where a zero
3964 * byte is found here. */
3965 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
3968 /* now convert the octal number to a byte again. */
3969 *write++ = ((read[0] - '0') << 6) +
3970 ((read[1] - '0') << 3) + (read[2] - '0');
3974 /* if we run into a \0 here, we definitely won't get a quote later */
3977 /* else copy \X sequence */
3980 } else if (*read == '\0') {
3986 /* if the string is not ending in " and zero terminated, we error */
3987 if (*read != '"' || read[1] != '\0')
3990 /* null terminate result string and return */
4000 gst_value_serialize_string (const GValue * value)
4002 return gst_string_wrap (value->data[0].v_pointer);
4006 gst_value_deserialize_string (GValue * dest, const gchar * s)
4008 if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
4009 g_value_set_string (dest, NULL);
4011 } else if (G_LIKELY (*s != '"' || s[strlen (s) - 1] != '"')) {
4012 if (!g_utf8_validate (s, -1, NULL))
4014 g_value_set_string (dest, s);
4017 /* strings delimited with double quotes should be unwrapped */
4018 gchar *str = gst_string_unwrap (s);
4019 if (G_UNLIKELY (!str))
4021 if (!g_utf8_validate (str, -1, NULL)) {
4025 g_value_take_string (dest, str);
4036 gst_value_compare_enum (const GValue * value1, const GValue * value2)
4038 GEnumValue *en1, *en2;
4039 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
4040 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
4042 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
4043 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
4044 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
4045 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
4046 g_type_class_unref (klass1);
4047 g_type_class_unref (klass2);
4048 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
4049 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
4050 if (en1->value < en2->value)
4051 return GST_VALUE_LESS_THAN;
4052 if (en1->value > en2->value)
4053 return GST_VALUE_GREATER_THAN;
4055 return GST_VALUE_EQUAL;
4059 gst_value_serialize_enum (const GValue * value)
4062 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
4064 g_return_val_if_fail (klass, NULL);
4065 en = g_enum_get_value (klass, g_value_get_enum (value));
4066 g_type_class_unref (klass);
4068 /* might be one of the custom formats registered later */
4069 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
4070 const GstFormatDefinition *format_def;
4072 format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
4073 g_return_val_if_fail (format_def != NULL, NULL);
4074 return g_strdup (format_def->description);
4077 g_return_val_if_fail (en, NULL);
4078 return g_strdup (en->value_nick);
4082 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
4085 const GstFormatDefinition *format_def =
4086 g_value_get_pointer (format_def_value);
4088 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
4091 return g_ascii_strcasecmp (s, format_def->description);
4095 gst_value_deserialize_enum (GValue * dest, const gchar * s)
4098 gchar *endptr = NULL;
4099 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
4101 g_return_val_if_fail (klass, FALSE);
4102 if (!(en = g_enum_get_value_by_name (klass, s))) {
4103 if (!(en = g_enum_get_value_by_nick (klass, s))) {
4104 gint i = strtol (s, &endptr, 0);
4106 if (endptr && *endptr == '\0') {
4107 en = g_enum_get_value (klass, i);
4111 g_type_class_unref (klass);
4113 /* might be one of the custom formats registered later */
4114 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
4115 GValue res = { 0, };
4116 const GstFormatDefinition *format_def;
4120 iter = gst_format_iterate_definitions ();
4122 found = gst_iterator_find_custom (iter,
4123 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
4126 format_def = g_value_get_pointer (&res);
4127 g_return_val_if_fail (format_def != NULL, FALSE);
4128 g_value_set_enum (dest, (gint) format_def->value);
4129 g_value_unset (&res);
4131 gst_iterator_free (iter);
4135 /* enum name/nick not found */
4139 g_value_set_enum (dest, en->value);
4147 /* we just compare the value here */
4149 gst_value_compare_gflags (const GValue * value1, const GValue * value2)
4152 GFlagsClass *klass1 =
4153 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
4154 GFlagsClass *klass2 =
4155 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
4157 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
4158 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
4159 fl1 = g_value_get_flags (value1);
4160 fl2 = g_value_get_flags (value2);
4161 g_type_class_unref (klass1);
4162 g_type_class_unref (klass2);
4164 return GST_VALUE_LESS_THAN;
4166 return GST_VALUE_GREATER_THAN;
4168 return GST_VALUE_EQUAL;
4171 /* the different flags are serialized separated with a + */
4173 gst_value_serialize_gflags (const GValue * value)
4177 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
4178 gchar *result, *tmp;
4179 gboolean first = TRUE;
4181 g_return_val_if_fail (klass, NULL);
4183 flags = g_value_get_flags (value);
4185 /* if no flags are set, try to serialize to the _NONE string */
4187 fl = g_flags_get_first_value (klass, flags);
4189 return g_strdup (fl->value_name);
4191 return g_strdup ("0");
4194 /* some flags are set, so serialize one by one */
4195 result = g_strdup ("");
4197 fl = g_flags_get_first_value (klass, flags);
4200 g_critical ("Could not serialize invalid flags 0x%x of type %s",
4201 flags, G_VALUE_TYPE_NAME (value));
4203 result = g_strdup ("0");
4208 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
4214 flags &= ~fl->value;
4216 g_type_class_unref (klass);
4222 gst_value_gflags_str_to_flags (GFlagsClass * klass, const gchar * s,
4223 guint * out_flags, guint * out_mask)
4227 const gchar *pos = NULL;
4229 gchar *cur_str, *endptr;
4234 g_return_val_if_fail (klass, FALSE);
4236 /* split into parts delimited with + or / and
4237 * compose the set of flags and mask. */
4241 goto done; /* Empty string, nothing to do */
4243 /* As a special case if the first char isn't a delimiter, assume
4244 * it's a '+' - for GFlags strings, which don't start with a
4245 * delimiter, while GFlagSet always will */
4246 if (*pos == '/' || *pos == '+') {
4254 /* Find the next delimiter */
4256 while (*next != '\0' && *next != '+' && *next != '/')
4258 cur_str = g_strndup (pos, next - pos);
4260 if ((fl = g_flags_get_value_by_name (klass, cur_str)))
4262 else if ((fl = g_flags_get_value_by_nick (klass, cur_str)))
4265 val = strtoul (cur_str, &endptr, 0);
4266 /* direct numeric value */
4267 if (endptr == NULL || *endptr != '\0') {
4269 return FALSE; /* Invalid numeric or string we can't convert */
4276 if (delimiter == '+')
4280 /* Advance to the next delimiter */
4284 } while (delimiter != '\0');
4297 gst_value_deserialize_gflags (GValue * dest, const gchar * s)
4299 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
4300 gboolean res = FALSE;
4303 if (gst_value_gflags_str_to_flags (klass, s, &flags, NULL)) {
4304 g_value_set_flags (dest, flags);
4308 g_type_class_unref (klass);
4318 gst_value_compare_gtype (const GValue * value1, const GValue * value2)
4320 if (value1->data[0].v_pointer == value2->data[0].v_pointer)
4321 return GST_VALUE_EQUAL;
4322 return GST_VALUE_UNORDERED;
4326 gst_value_serialize_gtype (const GValue * value)
4328 return g_strdup (g_type_name (g_value_get_gtype (value)));
4332 gst_value_deserialize_gtype (GValue * dest, const gchar * s)
4334 GType t = g_type_from_name (s);
4335 gboolean ret = TRUE;
4337 if (t == G_TYPE_INVALID)
4340 g_value_set_gtype (dest, t);
4350 gst_value_is_subset_int_range_int_range (const GValue * value1,
4351 const GValue * value2)
4355 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
4356 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
4358 if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
4359 INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
4361 if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
4362 INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
4365 if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
4366 if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
4367 INT_RANGE_STEP (value1))
4373 gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
4374 INT_RANGE_STEP (value2));
4375 if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
4382 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
4383 const GValue * value2)
4387 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
4388 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
4390 if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
4392 if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
4395 if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
4396 if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
4397 INT64_RANGE_STEP (value1))
4403 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
4404 INT64_RANGE_STEP (value2));
4405 if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
4411 /* A flag set is a subset of another if the superset allows the
4412 * flags of the subset */
4414 gst_value_is_subset_flagset_flagset (const GValue * value1,
4415 const GValue * value2)
4420 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value1), FALSE);
4421 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value2), FALSE);
4423 f1 = value1->data[0].v_uint;
4424 f2 = value2->data[0].v_uint;
4426 m1 = value1->data[1].v_uint;
4427 m2 = value2->data[1].v_uint;
4429 /* Not a subset if masked bits of superset disagree */
4430 if ((f1 & m1) != (f2 & (m1 & m2)))
4437 gst_value_is_subset_structure_structure (const GValue * value1,
4438 const GValue * value2)
4440 const GstStructure *s1, *s2;
4442 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (value1), FALSE);
4443 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (value2), FALSE);
4445 s1 = gst_value_get_structure (value1);
4446 s2 = gst_value_get_structure (value2);
4448 return gst_structure_is_subset (s1, s2);
4452 gst_value_is_subset_list_list (const GValue * value1, const GValue * value2)
4454 GstValueList *vlist1 = VALUE_LIST_ARRAY (value1);
4455 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
4456 gint it1, len1, it2, len2;
4461 /* A list can't be a subset of a smaller list */
4465 /* Check if all elements of the first list are within the 2nd list */
4466 for (it1 = 0; it1 < len1; it1++) {
4467 const GValue *child1 = &vlist1->fields[it1];
4468 gboolean seen = FALSE;
4470 for (it2 = 0; it2 < len2; it2++) {
4471 const GValue *child2 = &vlist2->fields[it2];
4472 if (gst_value_compare (child1, child2) == GST_VALUE_EQUAL) {
4485 gst_value_is_subset_list (const GValue * value1, const GValue * value2)
4487 GstValueList *vlist2 = VALUE_LIST_ARRAY (value2);
4492 /* Check whether value1 is within the list */
4493 for (it2 = 0; it2 < len2; it2++) {
4494 const GValue *child2 = &vlist2->fields[it2];
4495 if (gst_value_compare (value1, child2) == GST_VALUE_EQUAL) {
4504 * gst_value_is_subset:
4505 * @value1: a #GValue
4506 * @value2: a #GValue
4508 * Check that @value1 is a subset of @value2.
4510 * Return: %TRUE is @value1 is a subset of @value2
4513 gst_value_is_subset (const GValue * value1, const GValue * value2)
4515 GType type1 = G_VALUE_TYPE (value1);
4516 GType type2 = G_VALUE_TYPE (value2);
4518 /* special case for int/int64 ranges, since we cannot compute
4519 the difference for those when they have different steps,
4520 and it's actually a lot simpler to compute whether a range
4521 is a subset of another. */
4522 if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
4523 return gst_value_is_subset_int_range_int_range (value1, value2);
4524 } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
4525 && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
4526 return gst_value_is_subset_int64_range_int64_range (value1, value2);
4527 } else if (GST_VALUE_HOLDS_FLAG_SET (value1) &&
4528 GST_VALUE_HOLDS_FLAG_SET (value2)) {
4529 return gst_value_is_subset_flagset_flagset (value1, value2);
4530 } else if (GST_VALUE_HOLDS_STRUCTURE (value1)
4531 && GST_VALUE_HOLDS_STRUCTURE (value2)) {
4532 return gst_value_is_subset_structure_structure (value1, value2);
4533 } else if (type2 == GST_TYPE_LIST) {
4534 if (type1 == GST_TYPE_LIST)
4535 return gst_value_is_subset_list_list (value1, value2);
4536 return gst_value_is_subset_list (value1, value2);
4544 * -> 1 - [1,2] = empty
4548 * -> [1,2] - [1,3] = empty
4552 * -> {1,3} - {1,2} = 3
4555 * First caps subtraction needs to return a non-empty set, second
4556 * subtractions needs to give en empty set.
4557 * Both substractions are switched below, as it's faster that way.
4559 if (!gst_value_subtract (NULL, value1, value2)) {
4560 if (gst_value_subtract (NULL, value2, value1)) {
4572 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
4573 const GValue * src2)
4575 gint v = src1->data[0].v_int;
4577 /* check if it's already in the range */
4578 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
4579 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
4580 v % INT_RANGE_STEP (src2) == 0) {
4582 gst_value_init_and_copy (dest, src2);
4586 /* check if it extends the range */
4587 if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
4589 guint64 new_min = INT_RANGE_MIN (src2) - 1;
4590 guint64 new_max = INT_RANGE_MAX (src2);
4592 gst_value_init_and_copy (dest, src2);
4593 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4597 if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
4599 guint64 new_min = INT_RANGE_MIN (src2);
4600 guint64 new_max = INT_RANGE_MAX (src2) + 1;
4602 gst_value_init_and_copy (dest, src2);
4603 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4612 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
4613 const GValue * src2)
4615 /* We can union in several special cases:
4616 1 - one is a subset of another
4617 2 - same step and not disjoint
4618 3 - different step, at least one with one value which matches a 'next' or 'previous'
4623 if (gst_value_is_subset_int_range_int_range (src1, src2)) {
4625 gst_value_init_and_copy (dest, src2);
4628 if (gst_value_is_subset_int_range_int_range (src2, src1)) {
4630 gst_value_init_and_copy (dest, src1);
4634 /* 2 - same step and not disjoint */
4635 if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
4636 if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
4637 INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
4638 (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
4639 INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
4641 gint step = INT_RANGE_STEP (src1);
4642 gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
4643 gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
4644 g_value_init (dest, GST_TYPE_INT_RANGE);
4645 gst_value_set_int_range_step (dest, min, max, step);
4651 /* 3 - single value matches next or previous */
4652 if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
4653 gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
4654 gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
4655 if (n1 == 1 || n2 == 1) {
4656 const GValue *range_value = NULL;
4660 scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
4661 } else if (n2 == 1) {
4663 scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
4667 (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
4669 guint64 new_min = (guint)
4670 ((INT_RANGE_MIN (range_value) -
4671 1) * INT_RANGE_STEP (range_value));
4672 guint64 new_max = (guint)
4673 (INT_RANGE_MAX (range_value) * INT_RANGE_STEP (range_value));
4675 gst_value_init_and_copy (dest, range_value);
4676 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4679 } else if (scalar ==
4680 (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
4682 guint64 new_min = (guint)
4683 (INT_RANGE_MIN (range_value) * INT_RANGE_STEP (range_value));
4684 guint64 new_max = (guint)
4685 ((INT_RANGE_MAX (range_value) +
4686 1) * INT_RANGE_STEP (range_value));
4687 gst_value_init_and_copy (dest, range_value);
4688 dest->data[0].v_uint64 = (new_min << 32) | (new_max);
4695 /* If we get there, we did not find a way to make a union that can be
4696 represented with our simplistic model. */
4701 gst_value_union_flagset_flagset (GValue * dest, const GValue * src1,
4702 const GValue * src2)
4704 /* We can union 2 flag sets where they do not disagree on
4705 * required (masked) flag bits */
4709 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src1), FALSE);
4710 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src2), FALSE);
4712 f1 = src1->data[0].v_uint;
4713 f2 = src2->data[0].v_uint;
4715 m1 = src1->data[1].v_uint;
4716 m2 = src2->data[1].v_uint;
4718 /* Can't union if masked bits disagree */
4719 if ((f1 & (m1 & m2)) != (f2 & (m1 & m2)))
4723 g_value_init (dest, GST_TYPE_FLAG_SET);
4724 /* Copy masked bits from src2 to src1 */
4728 gst_value_set_flagset (dest, f1, m1);
4734 /* iterating over the result taking the union with the other structure's value */
4736 structure_field_union_into (GQuark field_id, GValue * val, gpointer user_data)
4738 GstStructure *other = user_data;
4739 const GValue *other_value;
4740 GValue res_value = G_VALUE_INIT;
4742 other_value = gst_structure_id_get_value (other, field_id);
4743 /* no value in the other struct, just keep this value */
4747 if (!gst_value_union (&res_value, val, other_value))
4750 g_value_unset (val);
4751 gst_value_move (val, &res_value);
4755 /* iterating over the other source structure adding missing values */
4757 structure_field_union_from (GQuark field_id, const GValue * other_val,
4760 GstStructure *result = user_data;
4761 const GValue *result_value;
4763 result_value = gst_structure_id_get_value (result, field_id);
4765 gst_structure_id_set_value (result, field_id, other_val);
4771 gst_value_union_structure_structure (GValue * dest, const GValue * src1,
4772 const GValue * src2)
4774 const GstStructure *s1, *s2;
4775 GstStructure *result;
4778 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (src1), FALSE);
4779 g_return_val_if_fail (GST_VALUE_HOLDS_STRUCTURE (src2), FALSE);
4781 s1 = gst_value_get_structure (src1);
4782 s2 = gst_value_get_structure (src2);
4784 /* Can't join two structures with different names into a single structure */
4785 if (!gst_structure_has_name (s1, gst_structure_get_name (s2))) {
4786 gst_value_list_concat (dest, src1, src2);
4790 result = gst_structure_copy (s1);
4792 gst_structure_map_in_place (result, structure_field_union_into,
4797 gst_structure_foreach (s2, structure_field_union_from, (gpointer) result);
4800 g_value_init (dest, GST_TYPE_STRUCTURE);
4801 gst_value_set_structure (dest, result);
4805 gst_structure_free (result);
4814 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
4815 const GValue * src2)
4817 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
4818 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
4819 src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
4821 gst_value_init_and_copy (dest, src1);
4829 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
4830 const GValue * src2)
4837 INT_RANGE_STEP (src1) /
4838 gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
4839 INT_RANGE_STEP (src2));
4840 if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
4842 step *= INT_RANGE_STEP (src2);
4845 MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
4846 INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
4847 min = (min + step - 1) / step * step;
4849 MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
4850 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
4851 max = max / step * step;
4855 g_value_init (dest, GST_TYPE_INT_RANGE);
4856 gst_value_set_int_range_step (dest, min, max, step);
4862 g_value_init (dest, G_TYPE_INT);
4863 g_value_set_int (dest, min);
4871 #define INT64_RANGE_MIN_VAL(v) (INT64_RANGE_MIN (v) * INT64_RANGE_STEP (v))
4872 #define INT64_RANGE_MAX_VAL(v) (INT64_RANGE_MAX (v) * INT64_RANGE_STEP (v))
4875 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
4876 const GValue * src2)
4878 if (INT64_RANGE_MIN_VAL (src2) <= src1->data[0].v_int64 &&
4879 INT64_RANGE_MAX_VAL (src2) >= src1->data[0].v_int64 &&
4880 src1->data[0].v_int64 % INT64_RANGE_STEP (src2) == 0) {
4882 gst_value_init_and_copy (dest, src1);
4890 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
4891 const GValue * src2)
4898 INT64_RANGE_STEP (src1) /
4899 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
4900 INT64_RANGE_STEP (src2));
4901 if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
4903 step *= INT64_RANGE_STEP (src2);
4906 MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
4907 INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
4908 min = (min + step - 1) / step * step;
4910 MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
4911 INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
4912 max = max / step * step;
4916 g_value_init (dest, GST_TYPE_INT64_RANGE);
4917 gst_value_set_int64_range_step (dest, min, max, step);
4923 g_value_init (dest, G_TYPE_INT64);
4924 g_value_set_int64 (dest, min);
4933 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
4934 const GValue * src2)
4936 if (src2->data[0].v_double <= src1->data[0].v_double &&
4937 src2->data[1].v_double >= src1->data[0].v_double) {
4939 gst_value_init_and_copy (dest, src1);
4947 gst_value_intersect_double_range_double_range (GValue * dest,
4948 const GValue * src1, const GValue * src2)
4953 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
4954 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
4958 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
4959 gst_value_set_double_range (dest, min, max);
4965 g_value_init (dest, G_TYPE_DOUBLE);
4966 g_value_set_int (dest, (int) min);
4975 gst_value_intersect_list_list (GValue * dest, const GValue * value1,
4976 const GValue * value2)
4978 guint8 tmpfield[16]; /* Check up to 128 values */
4980 gboolean alloc_bitfield = FALSE;
4981 gboolean res = FALSE;
4984 guint it1, len1, start2, it2, len2, itar;
4985 GstValueList *vlist = NULL;
4987 /* If they don't have the same basic type, all bets are off :) */
4988 if (!gst_value_list_or_array_get_basic_type (value1, &type1) ||
4989 !gst_value_list_or_array_get_basic_type (value2, &type2) ||
4993 len1 = VALUE_LIST_SIZE (value1);
4994 len2 = VALUE_LIST_SIZE (value2);
4996 /* Fast-path with no dest (i.e. only interested in knowing whether
4997 * both lists intersected without wanting the result) */
4999 for (it1 = 0; it1 < len1; it1++) {
5000 const GValue *item1 = VALUE_LIST_GET_VALUE (value1, it1);
5001 for (it2 = 0; it2 < len2; it2++) {
5002 const GValue *item2 = VALUE_LIST_GET_VALUE (value2, it2);
5003 if (gst_value_intersect (NULL, item1, item2)) {
5011 #define is_visited(idx) (bitfield[(idx) >> 3] & (1 << ((idx) & 0x7)))
5012 #define mark_visited(idx) (bitfield[(idx) >> 3] |= (1 << ((idx) & 0x7)))
5014 /* Bitfield to avoid double-visiting */
5015 if (G_UNLIKELY (len2 > 128)) {
5016 alloc_bitfield = TRUE;
5017 bitfield = g_malloc0 ((len2 / 8) + 1);
5018 GST_CAT_LOG (GST_CAT_PERFORMANCE,
5019 "Allocation for GstValueList with more than 128 members");
5021 bitfield = &tmpfield[0];
5022 memset (bitfield, 0, 16);
5026 /* When doing list<->list intersections, there is a greater
5027 * probability of ending up with a list than with a single value.
5028 * Furthermore, the biggest that list can be will the smallest list
5029 * (i.e. intersects fully).
5030 * Therefore we pre-allocate a GstValueList of that size. */
5031 vlist = _gst_value_list_new (MIN (len1, len2));
5034 tmp = &vlist->fields[0];
5037 for (it1 = 0; it1 < len1; it1++) {
5038 const GValue *item1 = VALUE_LIST_GET_VALUE (value1, it1);
5039 for (it2 = start2; it2 < len2; it2++) {
5040 const GValue *item2;
5041 if (is_visited (it2))
5043 item2 = VALUE_LIST_GET_VALUE (value2, it2);
5045 if (gst_value_intersect (tmp, item1, item2)) {
5048 /* Increment our inner-loop starting point */
5052 /* Move our collection value */
5054 tmp = &vlist->fields[itar];
5057 /* We can stop iterating the second part now that we've matched */
5067 /* If we end up with a single value in the list, just use that
5068 * value. Else use the list */
5069 if (vlist->len == 1) {
5070 gst_value_move (dest, &vlist->fields[0]);
5073 dest->g_type = GST_TYPE_LIST;
5074 dest->data[0].v_pointer = vlist;
5087 gst_value_intersect_list (GValue * dest, const GValue * value1,
5088 const GValue * value2)
5091 GValue intersection = { 0, };
5092 gboolean ret = FALSE;
5094 /* Use optimized list-list intersection */
5095 if (G_VALUE_TYPE (value2) == GST_TYPE_LIST) {
5096 return gst_value_intersect_list_list (dest, value1, value2);
5099 size = VALUE_LIST_SIZE (value1);
5100 for (i = 0; i < size; i++) {
5101 const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
5103 /* quicker version when we don't need the resulting set */
5105 if (gst_value_intersect (NULL, cur, value2)) {
5112 if (gst_value_intersect (&intersection, cur, value2)) {
5115 gst_value_move (dest, &intersection);
5117 } else if (GST_VALUE_HOLDS_LIST (dest)) {
5118 _gst_value_list_append_and_take_value (dest, &intersection);
5122 gst_value_move (&temp, dest);
5123 gst_value_list_merge (dest, &temp, &intersection);
5124 g_value_unset (&temp);
5125 g_value_unset (&intersection);
5134 gst_value_intersect_array (GValue * dest, const GValue * src1,
5135 const GValue * src2)
5141 /* only works on similar-sized arrays */
5142 size = gst_value_array_get_size (src1);
5143 if (size != gst_value_array_get_size (src2))
5146 /* quicker value when we don't need the resulting set */
5148 for (n = 0; n < size; n++) {
5149 if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
5150 gst_value_array_get_value (src2, n))) {
5157 g_value_init (dest, GST_TYPE_ARRAY);
5159 for (n = 0; n < size; n++) {
5160 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
5161 gst_value_array_get_value (src2, n))) {
5162 g_value_unset (dest);
5165 _gst_value_array_append_and_take_value (dest, &val);
5172 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
5173 const GValue * src2)
5178 vals = src2->data[0].v_pointer;
5183 res1 = gst_value_compare_fraction (&vals[0], src1);
5184 res2 = gst_value_compare_fraction (&vals[1], src1);
5186 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
5187 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
5189 gst_value_init_and_copy (dest, src1);
5196 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
5197 const GValue * src1, const GValue * src2)
5202 GValue *vals1, *vals2;
5204 vals1 = src1->data[0].v_pointer;
5205 vals2 = src2->data[0].v_pointer;
5206 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
5208 /* min = MAX (src1.start, src2.start) */
5209 res = gst_value_compare_fraction (&vals1[0], &vals2[0]);
5210 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5211 if (res == GST_VALUE_LESS_THAN)
5212 min = &vals2[0]; /* Take the max of the 2 */
5216 /* max = MIN (src1.end, src2.end) */
5217 res = gst_value_compare_fraction (&vals1[1], &vals2[1]);
5218 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5219 if (res == GST_VALUE_GREATER_THAN)
5220 max = &vals2[1]; /* Take the min of the 2 */
5224 res = gst_value_compare_fraction (min, max);
5225 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
5226 if (res == GST_VALUE_LESS_THAN) {
5228 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
5229 vals1 = dest->data[0].v_pointer;
5230 g_value_copy (min, &vals1[0]);
5231 g_value_copy (max, &vals1[1]);
5235 if (res == GST_VALUE_EQUAL) {
5237 gst_value_init_and_copy (dest, min);
5244 /* Two flagsets intersect if the masked bits in both
5245 * flagsets are exactly equal */
5247 gst_value_intersect_flagset_flagset (GValue * dest,
5248 const GValue * src1, const GValue * src2)
5252 GType type1, type2, flagset_type;
5254 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src1), FALSE);
5255 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (src2), FALSE);
5257 f1 = src1->data[0].v_uint;
5258 f2 = src2->data[0].v_uint;
5260 m1 = src1->data[1].v_uint;
5261 m2 = src2->data[1].v_uint;
5263 /* Don't intersect if masked bits disagree */
5264 if ((f1 & (m1 & m2)) != (f2 & (m1 & m2)))
5267 /* Allow intersection with the generic FlagSet type, on one
5268 * side, but not 2 different subtypes - that makes no sense */
5269 type1 = G_VALUE_TYPE (src1);
5270 type2 = G_VALUE_TYPE (src2);
5271 flagset_type = GST_TYPE_FLAG_SET;
5273 if (type1 != type2 && type1 != flagset_type && type2 != flagset_type)
5279 /* Prefer an output type that matches a sub-type,
5280 * rather than the generic type */
5281 if (type1 != flagset_type)
5286 g_value_init (dest, dest_type);
5288 /* The compatible set is all the bits from src1 that it
5289 * cares about and all the bits from src2 that it cares
5291 dest->data[0].v_uint = (f1 & m1) | (f2 & m2);
5292 dest->data[1].v_uint = m1 | m2;
5299 gst_value_intersect_structure_structure (GValue * dest,
5300 const GValue * src1, const GValue * src2)
5302 const GstStructure *s1, *s2;
5305 s1 = gst_value_get_structure (src1);
5306 s2 = gst_value_get_structure (src2);
5308 d1 = gst_structure_intersect (s1, s2);
5313 g_value_init (dest, GST_TYPE_STRUCTURE);
5314 gst_value_set_structure (dest, d1);
5317 gst_structure_free (d1);
5326 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
5327 const GValue * subtrahend)
5329 gint min = gst_value_get_int_range_min (subtrahend);
5330 gint max = gst_value_get_int_range_max (subtrahend);
5331 gint step = gst_value_get_int_range_step (subtrahend);
5332 gint val = g_value_get_int (minuend);
5337 /* subtracting a range from an int only works if the int is not in the
5339 if (val < min || val > max || val % step) {
5340 /* and the result is the int */
5342 gst_value_init_and_copy (dest, minuend);
5348 /* creates a new int range based on input values.
5351 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
5352 gint max2, gint step)
5356 GValue *pv1, *pv2; /* yeah, hungarian! */
5358 g_return_val_if_fail (step > 0, FALSE);
5359 g_return_val_if_fail (min1 % step == 0, FALSE);
5360 g_return_val_if_fail (max1 % step == 0, FALSE);
5361 g_return_val_if_fail (min2 % step == 0, FALSE);
5362 g_return_val_if_fail (max2 % step == 0, FALSE);
5364 if (min1 <= max1 && min2 <= max2) {
5367 } else if (min1 <= max1) {
5370 } else if (min2 <= max2) {
5381 g_value_init (pv1, GST_TYPE_INT_RANGE);
5382 gst_value_set_int_range_step (pv1, min1, max1, step);
5383 } else if (min1 == max1) {
5384 g_value_init (pv1, G_TYPE_INT);
5385 g_value_set_int (pv1, min1);
5388 g_value_init (pv2, GST_TYPE_INT_RANGE);
5389 gst_value_set_int_range_step (pv2, min2, max2, step);
5390 } else if (min2 == max2) {
5391 g_value_init (pv2, G_TYPE_INT);
5392 g_value_set_int (pv2, min2);
5395 if (min1 <= max1 && min2 <= max2) {
5396 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5402 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
5403 const GValue * subtrahend)
5405 gint min = gst_value_get_int_range_min (minuend);
5406 gint max = gst_value_get_int_range_max (minuend);
5407 gint step = gst_value_get_int_range_step (minuend);
5408 gint val = g_value_get_int (subtrahend);
5410 g_return_val_if_fail (min < max, FALSE);
5415 /* value is outside of the range, return range unchanged */
5416 if (val < min || val > max || val % step) {
5418 gst_value_init_and_copy (dest, minuend);
5421 /* max must be MAXINT too as val <= max */
5422 if (val >= G_MAXINT - step + 1) {
5426 /* min must be MININT too as val >= max */
5427 if (val <= G_MININT + step - 1) {
5432 gst_value_create_new_range (dest, min, val - step, val + step, max, step);
5438 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
5439 const GValue * subtrahend)
5441 gint min1 = gst_value_get_int_range_min (minuend);
5442 gint max1 = gst_value_get_int_range_max (minuend);
5443 gint step1 = gst_value_get_int_range_step (minuend);
5444 gint min2 = gst_value_get_int_range_min (subtrahend);
5445 gint max2 = gst_value_get_int_range_max (subtrahend);
5446 gint step2 = gst_value_get_int_range_step (subtrahend);
5449 if (step1 != step2) {
5459 if (max2 >= max1 && min2 <= min1) {
5461 } else if (max2 >= max1) {
5462 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
5464 } else if (min2 <= min1) {
5465 return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
5468 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
5469 MAX (max2 + step, min1), max1, step);
5474 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
5475 const GValue * subtrahend)
5477 gint64 min = gst_value_get_int64_range_min (subtrahend);
5478 gint64 max = gst_value_get_int64_range_max (subtrahend);
5479 gint64 step = gst_value_get_int64_range_step (subtrahend);
5480 gint64 val = g_value_get_int64 (minuend);
5484 /* subtracting a range from an int64 only works if the int64 is not in the
5486 if (val < min || val > max || val % step) {
5487 /* and the result is the int64 */
5489 gst_value_init_and_copy (dest, minuend);
5495 /* creates a new int64 range based on input values.
5498 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
5499 gint64 min2, gint64 max2, gint64 step)
5503 GValue *pv1, *pv2; /* yeah, hungarian! */
5505 g_return_val_if_fail (step > 0, FALSE);
5506 g_return_val_if_fail (min1 % step == 0, FALSE);
5507 g_return_val_if_fail (max1 % step == 0, FALSE);
5508 g_return_val_if_fail (min2 % step == 0, FALSE);
5509 g_return_val_if_fail (max2 % step == 0, FALSE);
5511 if (min1 <= max1 && min2 <= max2) {
5514 } else if (min1 <= max1) {
5517 } else if (min2 <= max2) {
5528 g_value_init (pv1, GST_TYPE_INT64_RANGE);
5529 gst_value_set_int64_range_step (pv1, min1, max1, step);
5530 } else if (min1 == max1) {
5531 g_value_init (pv1, G_TYPE_INT64);
5532 g_value_set_int64 (pv1, min1);
5535 g_value_init (pv2, GST_TYPE_INT64_RANGE);
5536 gst_value_set_int64_range_step (pv2, min2, max2, step);
5537 } else if (min2 == max2) {
5538 g_value_init (pv2, G_TYPE_INT64);
5539 g_value_set_int64 (pv2, min2);
5542 if (min1 <= max1 && min2 <= max2) {
5543 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5549 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
5550 const GValue * subtrahend)
5552 gint64 min = gst_value_get_int64_range_min (minuend);
5553 gint64 max = gst_value_get_int64_range_max (minuend);
5554 gint64 step = gst_value_get_int64_range_step (minuend);
5555 gint64 val = g_value_get_int64 (subtrahend);
5557 g_return_val_if_fail (min < max, FALSE);
5562 /* value is outside of the range, return range unchanged */
5563 if (val < min || val > max || val % step) {
5565 gst_value_init_and_copy (dest, minuend);
5568 /* max must be MAXINT64 too as val <= max */
5569 if (val >= G_MAXINT64 - step + 1) {
5573 /* min must be MININT64 too as val >= max */
5574 if (val <= G_MININT64 + step - 1) {
5579 gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
5586 gst_value_subtract_int64_range_int64_range (GValue * dest,
5587 const GValue * minuend, const GValue * subtrahend)
5589 gint64 min1 = gst_value_get_int64_range_min (minuend);
5590 gint64 max1 = gst_value_get_int64_range_max (minuend);
5591 gint64 step1 = gst_value_get_int64_range_step (minuend);
5592 gint64 min2 = gst_value_get_int64_range_min (subtrahend);
5593 gint64 max2 = gst_value_get_int64_range_max (subtrahend);
5594 gint64 step2 = gst_value_get_int64_range_step (subtrahend);
5597 if (step1 != step2) {
5608 if (max2 >= max1 && min2 <= min1) {
5610 } else if (max2 >= max1) {
5611 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
5612 max1), step, 0, step);
5613 } else if (min2 <= min1) {
5614 return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
5615 max1, step, 0, step);
5617 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
5618 max1), MAX (max2 + step, min1), max1, step);
5623 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
5624 const GValue * subtrahend)
5626 gdouble min = gst_value_get_double_range_min (subtrahend);
5627 gdouble max = gst_value_get_double_range_max (subtrahend);
5628 gdouble val = g_value_get_double (minuend);
5630 if (val < min || val > max) {
5632 gst_value_init_and_copy (dest, minuend);
5639 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
5640 const GValue * subtrahend)
5642 /* since we don't have open ranges, we cannot create a hole in
5643 * a double range. We return the original range */
5645 gst_value_init_and_copy (dest, minuend);
5650 gst_value_subtract_double_range_double_range (GValue * dest,
5651 const GValue * minuend, const GValue * subtrahend)
5653 /* since we don't have open ranges, we have to approximate */
5654 /* done like with ints */
5655 gdouble min1 = gst_value_get_double_range_min (minuend);
5656 gdouble max2 = gst_value_get_double_range_max (minuend);
5657 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
5658 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
5661 GValue *pv1, *pv2; /* yeah, hungarian! */
5663 if (min1 < max1 && min2 < max2) {
5666 } else if (min1 < max1) {
5669 } else if (min2 < max2) {
5680 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
5681 gst_value_set_double_range (pv1, min1, max1);
5684 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
5685 gst_value_set_double_range (pv2, min2, max2);
5688 if (min1 < max1 && min2 < max2) {
5689 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5695 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
5696 const GValue * subtrahend)
5699 GValue subtraction = { 0, };
5700 gboolean ret = FALSE;
5702 size = VALUE_LIST_SIZE (minuend);
5703 for (i = 0; i < size; i++) {
5704 const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
5706 /* quicker version when we can discard the result */
5708 if (gst_value_subtract (NULL, cur, subtrahend)) {
5715 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
5717 gst_value_move (dest, &subtraction);
5719 } else if (G_VALUE_TYPE (dest) == GST_TYPE_LIST
5720 && G_VALUE_TYPE (&subtraction) != GST_TYPE_LIST) {
5721 _gst_value_list_append_and_take_value (dest, &subtraction);
5725 gst_value_move (&temp, dest);
5726 gst_value_list_concat_and_take_values (dest, &temp, &subtraction);
5734 gst_value_subtract_list (GValue * dest, const GValue * minuend,
5735 const GValue * subtrahend)
5738 GValue data[2] = { {0,}, {0,} };
5739 GValue *subtraction = &data[0], *result = &data[1];
5741 gst_value_init_and_copy (result, minuend);
5742 size = VALUE_LIST_SIZE (subtrahend);
5743 for (i = 0; i < size; i++) {
5744 const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
5746 if (gst_value_subtract (subtraction, result, cur)) {
5747 GValue *temp = result;
5749 result = subtraction;
5751 g_value_unset (subtraction);
5753 g_value_unset (result);
5758 gst_value_move (dest, result);
5760 g_value_unset (result);
5766 gst_value_subtract_fraction_fraction_range (GValue * dest,
5767 const GValue * minuend, const GValue * subtrahend)
5769 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
5770 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
5772 /* subtracting a range from an fraction only works if the fraction
5773 * is not in the range */
5774 if (gst_value_compare_fraction (minuend, min) == GST_VALUE_LESS_THAN ||
5775 gst_value_compare_fraction (minuend, max) == GST_VALUE_GREATER_THAN) {
5776 /* and the result is the value */
5778 gst_value_init_and_copy (dest, minuend);
5786 gst_value_subtract_fraction_range_fraction (GValue * dest,
5787 const GValue * minuend, const GValue * subtrahend)
5789 /* since we don't have open ranges, we cannot create a hole in
5790 * a range. We return the original range */
5792 gst_value_init_and_copy (dest, minuend);
5797 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
5798 const GValue * minuend, const GValue * subtrahend)
5800 /* since we don't have open ranges, we have to approximate */
5801 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
5802 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
5803 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
5804 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
5805 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
5809 GValue *pv1, *pv2; /* yeah, hungarian! */
5811 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
5812 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
5814 cmp1 = gst_value_compare_fraction (max2, max1);
5815 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
5816 if (cmp1 == GST_VALUE_LESS_THAN)
5818 cmp1 = gst_value_compare_fraction (min1, min2);
5819 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
5820 if (cmp1 == GST_VALUE_GREATER_THAN)
5823 cmp1 = gst_value_compare_fraction (min1, max1);
5824 cmp2 = gst_value_compare_fraction (min2, max2);
5826 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
5829 } else if (cmp1 == GST_VALUE_LESS_THAN) {
5832 } else if (cmp2 == GST_VALUE_LESS_THAN) {
5842 if (cmp1 == GST_VALUE_LESS_THAN) {
5843 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
5844 gst_value_set_fraction_range (pv1, min1, max1);
5846 if (cmp2 == GST_VALUE_LESS_THAN) {
5847 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
5848 gst_value_set_fraction_range (pv2, min2, max2);
5851 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
5852 gst_value_list_concat_and_take_values (dest, pv1, pv2);
5862 * gst_value_get_compare_func:
5863 * @value1: a value to get the compare function for
5865 * Determines the compare function to be used with values of the same type as
5866 * @value1. The function can be given to gst_value_compare_with_func().
5868 * Returns: A #GstValueCompareFunc value
5870 static GstValueCompareFunc
5871 gst_value_get_compare_func (const GValue * value1)
5873 GstValueTable *table, *best = NULL;
5877 type1 = G_VALUE_TYPE (value1);
5879 /* this is a fast check */
5880 best = gst_value_hash_lookup_type (type1);
5883 if (G_UNLIKELY (!best || !best->compare)) {
5884 guint len = gst_value_table->len;
5887 for (i = 0; i < len; i++) {
5888 table = &g_array_index (gst_value_table, GstValueTable, i);
5889 if (table->compare && g_type_is_a (type1, table->type)) {
5890 if (!best || g_type_is_a (table->type, best->type))
5895 if (G_LIKELY (best))
5896 return best->compare;
5901 static inline gboolean
5902 gst_value_can_compare_unchecked (const GValue * value1, const GValue * value2)
5904 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
5907 return gst_value_get_compare_func (value1) != NULL;
5911 * gst_value_can_compare:
5912 * @value1: a value to compare
5913 * @value2: another value to compare
5915 * Determines if @value1 and @value2 can be compared.
5917 * Returns: %TRUE if the values can be compared
5920 gst_value_can_compare (const GValue * value1, const GValue * value2)
5922 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
5923 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
5925 return gst_value_can_compare_unchecked (value1, value2);
5929 gst_value_list_equals_range (const GValue * list, const GValue * value)
5931 const GValue *first;
5934 /* TODO: compare against an empty list ? No type though... */
5935 list_size = VALUE_LIST_SIZE (list);
5939 /* compare the basic types - they have to match */
5940 first = VALUE_LIST_GET_VALUE (list, 0);
5941 #define CHECK_TYPES(type,prefix) \
5942 ((first) && G_VALUE_TYPE(first) == prefix##_TYPE_##type && GST_VALUE_HOLDS_##type##_RANGE (value))
5943 if (CHECK_TYPES (INT, G)) {
5944 const gint rmin = gst_value_get_int_range_min (value);
5945 const gint rmax = gst_value_get_int_range_max (value);
5946 const gint rstep = gst_value_get_int_range_step (value);
5949 /* note: this will overflow for min 0 and max INT_MAX, but this
5950 would only be equal to a list of INT_MAX elements, which seems
5952 if (list_size != rmax / rstep - rmin / rstep + 1)
5954 for (n = 0; n < list_size; ++n) {
5955 gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
5956 if (v < rmin || v > rmax || v % rstep) {
5961 } else if (CHECK_TYPES (INT64, G)) {
5962 const gint64 rmin = gst_value_get_int64_range_min (value);
5963 const gint64 rmax = gst_value_get_int64_range_max (value);
5964 const gint64 rstep = gst_value_get_int64_range_step (value);
5965 GST_DEBUG ("List/range of int64s");
5968 if (list_size != rmax / rstep - rmin / rstep + 1)
5970 for (n = 0; n < list_size; ++n) {
5971 gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
5972 if (v < rmin || v > rmax || v % rstep)
5979 /* other combinations don't make sense for equality */
5983 /* "Pure" variant of gst_value_compare which is guaranteed to
5984 * not have list arguments and therefore does basic comparisons
5987 _gst_value_compare_nolist (const GValue * value1, const GValue * value2)
5989 GstValueCompareFunc compare;
5991 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
5992 return GST_VALUE_UNORDERED;
5994 compare = gst_value_get_compare_func (value1);
5996 return compare (value1, value2);
5999 g_critical ("unable to compare values of type %s\n",
6000 g_type_name (G_VALUE_TYPE (value1)));
6001 return GST_VALUE_UNORDERED;
6005 * gst_value_compare:
6006 * @value1: a value to compare
6007 * @value2: another value to compare
6009 * Compares @value1 and @value2. If @value1 and @value2 cannot be
6010 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
6011 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
6012 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
6013 * If the values are equal, GST_VALUE_EQUAL is returned.
6015 * Returns: comparison result
6018 gst_value_compare (const GValue * value1, const GValue * value2)
6020 gboolean value1_is_list;
6021 gboolean value2_is_list;
6023 g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
6024 g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
6026 value1_is_list = G_VALUE_TYPE (value1) == GST_TYPE_LIST;
6027 value2_is_list = G_VALUE_TYPE (value2) == GST_TYPE_LIST;
6029 /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
6030 as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
6031 if (value1_is_list && !value2_is_list) {
6034 if (gst_value_list_equals_range (value1, value2)) {
6035 return GST_VALUE_EQUAL;
6038 n = gst_value_list_get_size (value1);
6040 return GST_VALUE_UNORDERED;
6042 for (i = 0; i < n; i++) {
6045 elt = gst_value_list_get_value (value1, i);
6046 ret = gst_value_compare (elt, value2);
6047 if (ret != GST_VALUE_EQUAL && n == 1)
6049 else if (ret != GST_VALUE_EQUAL)
6050 return GST_VALUE_UNORDERED;
6053 return GST_VALUE_EQUAL;
6054 } else if (value2_is_list && !value1_is_list) {
6057 if (gst_value_list_equals_range (value2, value1)) {
6058 return GST_VALUE_EQUAL;
6061 n = gst_value_list_get_size (value2);
6063 return GST_VALUE_UNORDERED;
6065 for (i = 0; i < n; i++) {
6068 elt = gst_value_list_get_value (value2, i);
6069 ret = gst_value_compare (elt, value1);
6070 if (ret != GST_VALUE_EQUAL && n == 1)
6072 else if (ret != GST_VALUE_EQUAL)
6073 return GST_VALUE_UNORDERED;
6076 return GST_VALUE_EQUAL;
6079 /* And now handle the generic case */
6080 return _gst_value_compare_nolist (value1, value2);
6086 * gst_value_can_union:
6087 * @value1: a value to union
6088 * @value2: another value to union
6090 * Determines if @value1 and @value2 can be non-trivially unioned.
6091 * Any two values can be trivially unioned by adding both of them
6092 * to a GstValueList. However, certain types have the possibility
6093 * to be unioned in a simpler way. For example, an integer range
6094 * and an integer can be unioned if the integer is a subset of the
6095 * integer range. If there is the possibility that two values can
6096 * be unioned, this function returns %TRUE.
6098 * Returns: %TRUE if there is a function allowing the two values to
6102 gst_value_can_union (const GValue * value1, const GValue * value2)
6104 GstValueUnionInfo *union_info;
6107 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6108 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6110 len = gst_value_union_funcs->len;
6112 for (i = 0; i < len; i++) {
6113 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
6114 if (union_info->type1 == G_VALUE_TYPE (value1) &&
6115 union_info->type2 == G_VALUE_TYPE (value2))
6117 if (union_info->type1 == G_VALUE_TYPE (value2) &&
6118 union_info->type2 == G_VALUE_TYPE (value1))
6127 * @dest: (out caller-allocates): the destination value
6128 * @value1: a value to union
6129 * @value2: another value to union
6131 * Creates a GValue corresponding to the union of @value1 and @value2.
6133 * Returns: %TRUE if the union succeeded.
6136 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
6138 const GstValueUnionInfo *union_info;
6142 g_return_val_if_fail (dest != NULL, FALSE);
6143 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6144 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6145 g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
6148 len = gst_value_union_funcs->len;
6149 type1 = G_VALUE_TYPE (value1);
6150 type2 = G_VALUE_TYPE (value2);
6152 for (i = 0; i < len; i++) {
6153 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
6154 if (union_info->type1 == type1 && union_info->type2 == type2) {
6155 return union_info->func (dest, value1, value2);
6157 if (union_info->type1 == type2 && union_info->type2 == type1) {
6158 return union_info->func (dest, value2, value1);
6162 gst_value_list_concat (dest, value1, value2);
6166 /* gst_value_register_union_func: (skip)
6167 * @type1: a type to union
6168 * @type2: another type to union
6169 * @func: a function that implements creating a union between the two types
6171 * Registers a union function that can create a union between #GValue items
6172 * of the type @type1 and @type2.
6174 * Union functions should be registered at startup before any pipelines are
6175 * started, as gst_value_register_union_func() is not thread-safe and cannot
6176 * be used at the same time as gst_value_union() or gst_value_can_union().
6179 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
6181 GstValueUnionInfo union_info;
6183 union_info.type1 = type1;
6184 union_info.type2 = type2;
6185 union_info.func = func;
6187 g_array_append_val (gst_value_union_funcs, union_info);
6193 * gst_value_can_intersect:
6194 * @value1: a value to intersect
6195 * @value2: another value to intersect
6197 * Determines if intersecting two values will produce a valid result.
6198 * Two values will produce a valid intersection if they have the same
6201 * Returns: %TRUE if the values can intersect
6204 gst_value_can_intersect (const GValue * value1, const GValue * value2)
6206 GstValueIntersectInfo *intersect_info;
6210 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6211 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6213 type1 = G_VALUE_TYPE (value1);
6214 type2 = G_VALUE_TYPE (value2);
6216 /* practically all GstValue types have a compare function (_can_compare=TRUE)
6217 * GstStructure and GstCaps have not, but are intersectable */
6222 if (type1 == GST_TYPE_LIST || type2 == GST_TYPE_LIST)
6225 if (G_UNLIKELY (GST_VALUE_HOLDS_FLAG_SET (value1) &&
6226 GST_VALUE_HOLDS_FLAG_SET (value2))) {
6229 flagset_type = GST_TYPE_FLAG_SET;
6231 /* Allow intersection with the generic FlagSet type, on one
6232 * side, but not 2 different subtypes - that makes no sense */
6233 if (type1 == flagset_type || type2 == flagset_type)
6237 /* check registered intersect functions (only different gtype are checked at
6239 len = gst_value_intersect_funcs->len;
6240 for (i = 0; i < len; i++) {
6241 intersect_info = &g_array_index (gst_value_intersect_funcs,
6242 GstValueIntersectInfo, i);
6243 if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
6244 (intersect_info->type1 == type2 && intersect_info->type2 == type1))
6248 return gst_value_can_compare_unchecked (value1, value2);
6252 * gst_value_intersect:
6253 * @dest: (out caller-allocates) (transfer full) (allow-none):
6254 * a uninitialized #GValue that will hold the calculated
6255 * intersection value. May be %NULL if the resulting set if not
6257 * @value1: a value to intersect
6258 * @value2: another value to intersect
6260 * Calculates the intersection of two values. If the values have
6261 * a non-empty intersection, the value representing the intersection
6262 * is placed in @dest, unless %NULL. If the intersection is non-empty,
6263 * @dest is not modified.
6265 * Returns: %TRUE if the intersection is non-empty
6268 gst_value_intersect (GValue * dest, const GValue * value1,
6269 const GValue * value2)
6271 GstValueIntersectInfo *intersect_info;
6275 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
6276 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
6278 type1 = G_VALUE_TYPE (value1);
6279 type2 = G_VALUE_TYPE (value2);
6281 /* special cases first */
6282 if (type1 == GST_TYPE_LIST)
6283 return gst_value_intersect_list (dest, value1, value2);
6284 if (type2 == GST_TYPE_LIST)
6285 return gst_value_intersect_list (dest, value2, value1);
6287 if (_gst_value_compare_nolist (value1, value2) == GST_VALUE_EQUAL) {
6289 gst_value_init_and_copy (dest, value1);
6293 if (type1 == type2) {
6294 /* Equal type comparison */
6295 if (type1 == GST_TYPE_INT_RANGE)
6296 return gst_value_intersect_int_range_int_range (dest, value1, value2);
6297 if (type1 == GST_TYPE_INT64_RANGE)
6298 return gst_value_intersect_int64_range_int64_range (dest, value1, value2);
6299 if (type1 == GST_TYPE_DOUBLE_RANGE)
6300 return gst_value_intersect_double_range_double_range (dest, value1,
6302 if (type1 == GST_TYPE_ARRAY)
6303 return gst_value_intersect_array (dest, value1, value2);
6304 if (type1 == GST_TYPE_FRACTION_RANGE)
6305 return gst_value_intersect_fraction_range_fraction_range (dest, value1,
6307 if (type1 == GST_TYPE_FLAG_SET)
6308 return gst_value_intersect_flagset_flagset (dest, value1, value2);
6309 if (type1 == GST_TYPE_STRUCTURE)
6310 return gst_value_intersect_structure_structure (dest, value1, value2);
6312 /* Different type comparison */
6313 len = gst_value_intersect_funcs->len;
6314 for (i = 0; i < len; i++) {
6315 intersect_info = &g_array_index (gst_value_intersect_funcs,
6316 GstValueIntersectInfo, i);
6317 if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
6318 return intersect_info->func (dest, value1, value2);
6320 if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
6321 return intersect_info->func (dest, value2, value1);
6326 /* Failed to find a direct intersection, check if these are
6327 * GstFlagSet sub-types. */
6328 if (G_UNLIKELY (GST_VALUE_HOLDS_FLAG_SET (value1) &&
6329 GST_VALUE_HOLDS_FLAG_SET (value2))) {
6330 return gst_value_intersect_flagset_flagset (dest, value1, value2);
6338 /* gst_value_register_intersect_func: (skip)
6339 * @type1: the first type to intersect
6340 * @type2: the second type to intersect
6341 * @func: the intersection function
6343 * Registers a function that is called to calculate the intersection
6344 * of the values having the types @type1 and @type2.
6346 * Intersect functions should be registered at startup before any pipelines are
6347 * started, as gst_value_register_intersect_func() is not thread-safe and
6348 * cannot be used at the same time as gst_value_intersect() or
6349 * gst_value_can_intersect().
6352 gst_value_register_intersect_func (GType type1, GType type2,
6353 GstValueIntersectFunc func)
6355 GstValueIntersectInfo intersect_info;
6357 intersect_info.type1 = type1;
6358 intersect_info.type2 = type2;
6359 intersect_info.func = func;
6361 g_array_append_val (gst_value_intersect_funcs, intersect_info);
6368 * gst_value_subtract:
6369 * @dest: (out caller-allocates) (allow-none): the destination value
6370 * for the result if the subtraction is not empty. May be %NULL,
6371 * in which case the resulting set will not be computed, which can
6372 * give a fair speedup.
6373 * @minuend: the value to subtract from
6374 * @subtrahend: the value to subtract
6376 * Subtracts @subtrahend from @minuend and stores the result in @dest.
6377 * Note that this means subtraction as in sets, not as in mathematics.
6379 * Returns: %TRUE if the subtraction is not empty
6382 gst_value_subtract (GValue * dest, const GValue * minuend,
6383 const GValue * subtrahend)
6385 GstValueSubtractInfo *info;
6389 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
6390 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
6392 mtype = G_VALUE_TYPE (minuend);
6393 stype = G_VALUE_TYPE (subtrahend);
6395 /* special cases first */
6396 if (mtype == GST_TYPE_LIST)
6397 return gst_value_subtract_from_list (dest, minuend, subtrahend);
6398 if (stype == GST_TYPE_LIST)
6399 return gst_value_subtract_list (dest, minuend, subtrahend);
6401 len = gst_value_subtract_funcs->len;
6402 for (i = 0; i < len; i++) {
6403 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
6404 if (info->minuend == mtype && info->subtrahend == stype) {
6405 return info->func (dest, minuend, subtrahend);
6409 if (_gst_value_compare_nolist (minuend, subtrahend) != GST_VALUE_EQUAL) {
6411 gst_value_init_and_copy (dest, minuend);
6420 gst_value_subtract (GValue * dest, const GValue * minuend,
6421 const GValue * subtrahend)
6423 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
6425 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
6426 gst_value_serialize (subtrahend),
6427 ret ? gst_value_serialize (dest) : "---");
6433 * gst_value_can_subtract:
6434 * @minuend: the value to subtract from
6435 * @subtrahend: the value to subtract
6437 * Checks if it's possible to subtract @subtrahend from @minuend.
6439 * Returns: %TRUE if a subtraction is possible
6442 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
6444 GstValueSubtractInfo *info;
6448 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
6449 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
6451 mtype = G_VALUE_TYPE (minuend);
6452 stype = G_VALUE_TYPE (subtrahend);
6455 if (mtype == GST_TYPE_LIST || stype == GST_TYPE_LIST)
6457 if (mtype == GST_TYPE_STRUCTURE || stype == GST_TYPE_STRUCTURE)
6460 len = gst_value_subtract_funcs->len;
6461 for (i = 0; i < len; i++) {
6462 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
6463 if (info->minuend == mtype && info->subtrahend == stype)
6467 return gst_value_can_compare_unchecked (minuend, subtrahend);
6470 /* gst_value_register_subtract_func: (skip)
6471 * @minuend_type: type of the minuend
6472 * @subtrahend_type: type of the subtrahend
6473 * @func: function to use
6475 * Registers @func as a function capable of subtracting the values of
6476 * @subtrahend_type from values of @minuend_type.
6478 * Subtract functions should be registered at startup before any pipelines are
6479 * started, as gst_value_register_subtract_func() is not thread-safe and
6480 * cannot be used at the same time as gst_value_subtract().
6483 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
6484 GstValueSubtractFunc func)
6486 GstValueSubtractInfo info;
6488 g_return_if_fail (!gst_type_is_fixed (minuend_type)
6489 || !gst_type_is_fixed (subtrahend_type));
6491 info.minuend = minuend_type;
6492 info.subtrahend = subtrahend_type;
6495 g_array_append_val (gst_value_subtract_funcs, info);
6499 * gst_value_register:
6500 * @table: structure containing functions to register
6502 * Registers functions to perform calculations on #GValue items of a given
6503 * type. Each type can only be added once.
6506 gst_value_register (const GstValueTable * table)
6508 GstValueTable *found;
6510 g_return_if_fail (table != NULL);
6512 g_array_append_val (gst_value_table, *table);
6514 found = gst_value_hash_lookup_type (table->type);
6516 g_warning ("adding type %s multiple times", g_type_name (table->type));
6518 /* FIXME: we're not really doing the const justice, we assume the table is
6520 gst_value_hash_add_type (table->type, table);
6524 * gst_value_init_and_copy:
6525 * @dest: (out caller-allocates): the target value
6526 * @src: the source value
6528 * Initialises the target value to be of the same type as source and then copies
6529 * the contents from source to target.
6532 gst_value_init_and_copy (GValue * dest, const GValue * src)
6536 g_return_if_fail (G_IS_VALUE (src));
6537 g_return_if_fail (dest != NULL);
6539 type = G_VALUE_TYPE (src);
6540 /* We need to shortcut GstValueList/GstValueArray copying because:
6541 * * g_value_init would end up allocating something
6542 * * which g_value_copy would then free and re-alloc.
6544 * Instead directly call the copy */
6545 if (type == GST_TYPE_LIST || type == GST_TYPE_ARRAY) {
6546 dest->g_type = type;
6547 gst_value_copy_list_or_array (src, dest);
6551 g_value_init (dest, type);
6552 g_value_copy (src, dest);
6555 /* move src into dest and clear src */
6557 gst_value_move (GValue * dest, GValue * src)
6559 g_assert (G_IS_VALUE (src));
6560 g_assert (dest != NULL);
6563 memset (src, 0, sizeof (GValue));
6567 * gst_value_serialize:
6568 * @value: a #GValue to serialize
6570 * tries to transform the given @value into a string representation that allows
6571 * getting back this string later on using gst_value_deserialize().
6573 * Free-function: g_free
6575 * Returns: (transfer full) (nullable): the serialization for @value
6576 * or %NULL if none exists
6579 gst_value_serialize (const GValue * value)
6582 GValue s_val = { 0 };
6583 GstValueTable *table, *best;
6587 g_return_val_if_fail (G_IS_VALUE (value), NULL);
6589 type = G_VALUE_TYPE (value);
6591 best = gst_value_hash_lookup_type (type);
6593 if (G_UNLIKELY (!best || !best->serialize)) {
6594 len = gst_value_table->len;
6596 for (i = 0; i < len; i++) {
6597 table = &g_array_index (gst_value_table, GstValueTable, i);
6598 if (table->serialize && g_type_is_a (type, table->type)) {
6599 if (!best || g_type_is_a (table->type, best->type))
6604 if (G_LIKELY (best))
6605 return best->serialize (value);
6607 g_value_init (&s_val, G_TYPE_STRING);
6608 if (g_value_transform (value, &s_val)) {
6609 s = gst_string_wrap (g_value_get_string (&s_val));
6613 g_value_unset (&s_val);
6619 * gst_value_deserialize:
6620 * @dest: (out caller-allocates): #GValue to fill with contents of
6622 * @src: string to deserialize
6624 * Tries to deserialize a string into the type specified by the given GValue.
6625 * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
6627 * Returns: %TRUE on success
6630 gst_value_deserialize (GValue * dest, const gchar * src)
6632 GstValueTable *table, *best;
6636 g_return_val_if_fail (src != NULL, FALSE);
6637 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
6639 type = G_VALUE_TYPE (dest);
6641 best = gst_value_hash_lookup_type (type);
6642 if (G_UNLIKELY (!best || (!best->deserialize
6643 && !best->deserialize_with_pspec))) {
6644 len = gst_value_table->len;
6646 for (i = 0; i < len; i++) {
6647 table = &g_array_index (gst_value_table, GstValueTable, i);
6648 if ((table->deserialize || table->deserialize_with_pspec) &&
6649 g_type_is_a (type, table->type)) {
6650 if (!best || g_type_is_a (table->type, best->type))
6655 if (G_LIKELY (best)) {
6656 if (best->deserialize_with_pspec)
6657 return best->deserialize_with_pspec (dest, src, NULL);
6659 return best->deserialize (dest, src);
6666 * gst_value_deserialize_with_pspec:
6667 * @dest: (out caller-allocates): #GValue to fill with contents of
6669 * @src: string to deserialize
6670 * @pspec: (nullable): the #GParamSpec describing the expected value
6672 * Tries to deserialize a string into the type specified by the given GValue.
6673 * @pspec may be used to guide the deserializing of nested members.
6674 * If the operation succeeds, %TRUE is returned, %FALSE otherwise.
6676 * Returns: %TRUE on success
6680 gst_value_deserialize_with_pspec (GValue * dest, const gchar * src,
6683 GstValueTable *table, *best;
6687 g_return_val_if_fail (src != NULL, FALSE);
6688 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
6691 g_return_val_if_fail (G_VALUE_TYPE (dest) ==
6692 G_PARAM_SPEC_VALUE_TYPE (pspec), FALSE);
6694 type = G_VALUE_TYPE (dest);
6696 best = gst_value_hash_lookup_type (type);
6697 if (G_UNLIKELY (!best || (!best->deserialize
6698 && !best->deserialize_with_pspec))) {
6699 len = gst_value_table->len;
6701 for (i = 0; i < len; i++) {
6702 table = &g_array_index (gst_value_table, GstValueTable, i);
6703 if ((table->deserialize || table->deserialize_with_pspec) &&
6704 g_type_is_a (type, table->type)) {
6705 if (!best || g_type_is_a (table->type, best->type))
6710 if (G_LIKELY (best)) {
6711 if (best->deserialize_with_pspec)
6712 return best->deserialize_with_pspec (dest, src, pspec);
6714 return best->deserialize (dest, src);
6721 structure_field_is_fixed (GQuark field_id, const GValue * val,
6724 return gst_value_is_fixed (val);
6728 * gst_value_is_fixed:
6729 * @value: the #GValue to check
6731 * Tests if the given GValue, if available in a GstStructure (or any other
6732 * container) contains a "fixed" (which means: one value) or an "unfixed"
6733 * (which means: multiple possible values, such as data lists or data
6736 * Returns: true if the value is "fixed".
6740 gst_value_is_fixed (const GValue * value)
6744 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
6746 type = G_VALUE_TYPE (value);
6748 /* the most common types are just basic plain glib types */
6749 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
6753 if (type == GST_TYPE_ARRAY) {
6757 /* check recursively */
6758 size = gst_value_array_get_size (value);
6759 for (n = 0; n < size; n++) {
6760 kid = gst_value_array_get_value (value, n);
6761 if (!gst_value_is_fixed (kid))
6765 } else if (GST_VALUE_HOLDS_FLAG_SET (value)) {
6766 /* Flagsets are only fixed if there are no 'don't care' bits */
6767 return (gst_value_get_flagset_mask (value) == GST_FLAG_SET_MASK_EXACT);
6768 } else if (GST_VALUE_HOLDS_STRUCTURE (value)) {
6769 return gst_structure_foreach (gst_value_get_structure (value),
6770 structure_field_is_fixed, NULL);
6772 return gst_type_is_fixed (type);
6777 * @dest: the #GValue destination
6778 * @src: the #GValue to fixate
6780 * Fixate @src into a new value @dest.
6781 * For ranges, the first element is taken. For lists and arrays, the
6782 * first item is fixated and returned.
6783 * If @src is already fixed, this function returns %FALSE.
6785 * Returns: %TRUE if @dest contains a fixated version of @src.
6788 gst_value_fixate (GValue * dest, const GValue * src)
6790 g_return_val_if_fail (G_IS_VALUE (src), FALSE);
6791 g_return_val_if_fail (dest != NULL, FALSE);
6793 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
6794 g_value_init (dest, G_TYPE_INT);
6795 g_value_set_int (dest, gst_value_get_int_range_min (src));
6796 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
6797 g_value_init (dest, G_TYPE_DOUBLE);
6798 g_value_set_double (dest, gst_value_get_double_range_min (src));
6799 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
6800 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
6801 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
6802 GValue temp = { 0 };
6804 /* list could be empty */
6805 if (gst_value_list_get_size (src) <= 0)
6808 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
6810 if (!gst_value_fixate (dest, &temp)) {
6811 gst_value_move (dest, &temp);
6813 g_value_unset (&temp);
6815 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
6816 gboolean res = FALSE;
6819 len = gst_value_array_get_size (src);
6820 g_value_init (dest, GST_TYPE_ARRAY);
6821 for (n = 0; n < len; n++) {
6823 const GValue *orig_kid = gst_value_array_get_value (src, n);
6825 if (!gst_value_fixate (&kid, orig_kid))
6826 gst_value_init_and_copy (&kid, orig_kid);
6829 _gst_value_array_append_and_take_value (dest, &kid);
6833 g_value_unset (dest);
6836 } else if (GST_VALUE_HOLDS_FLAG_SET (src)) {
6839 if (gst_value_get_flagset_mask (src) == GST_FLAG_SET_MASK_EXACT)
6840 return FALSE; /* Already fixed */
6842 flags = gst_value_get_flagset_flags (src);
6843 g_value_init (dest, G_VALUE_TYPE (src));
6844 gst_value_set_flagset (dest, flags, GST_FLAG_SET_MASK_EXACT);
6846 } else if (GST_VALUE_HOLDS_STRUCTURE (src)) {
6847 const GstStructure *str = (GstStructure *) gst_value_get_structure (src);
6853 kid = gst_structure_copy (str);
6854 gst_structure_fixate (kid);
6855 g_value_init (dest, GST_TYPE_STRUCTURE);
6856 gst_value_set_structure (dest, kid);
6857 gst_structure_free (kid);
6870 /* helper functions */
6872 gst_value_init_fraction (GValue * value)
6874 value->data[0].v_int = 0;
6875 value->data[1].v_int = 1;
6879 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
6881 dest_value->data[0].v_int = src_value->data[0].v_int;
6882 dest_value->data[1].v_int = src_value->data[1].v_int;
6886 gst_value_collect_fraction (GValue * value, guint n_collect_values,
6887 GTypeCValue * collect_values, guint collect_flags)
6889 g_return_val_if_fail (n_collect_values == 2,
6890 g_strdup_printf ("not enough value locations for `%s' passed",
6891 G_VALUE_TYPE_NAME (value)));
6892 g_return_val_if_fail (collect_values[1].v_int != 0,
6893 g_strdup_printf ("passed '0' as denominator for `%s'",
6894 G_VALUE_TYPE_NAME (value)));
6895 g_return_val_if_fail (collect_values[0].v_int >= -G_MAXINT,
6897 ("passed value smaller than -G_MAXINT as numerator for `%s'",
6898 G_VALUE_TYPE_NAME (value)));
6899 g_return_val_if_fail (collect_values[1].v_int >= -G_MAXINT,
6901 ("passed value smaller than -G_MAXINT as denominator for `%s'",
6902 G_VALUE_TYPE_NAME (value)));
6904 gst_value_set_fraction (value,
6905 collect_values[0].v_int, collect_values[1].v_int);
6911 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
6912 GTypeCValue * collect_values, guint collect_flags)
6914 gint *numerator = collect_values[0].v_pointer;
6915 gint *denominator = collect_values[1].v_pointer;
6917 g_return_val_if_fail (numerator != NULL,
6918 g_strdup_printf ("numerator for `%s' passed as NULL",
6919 G_VALUE_TYPE_NAME (value)));
6920 g_return_val_if_fail (denominator != NULL,
6921 g_strdup_printf ("denominator for `%s' passed as NULL",
6922 G_VALUE_TYPE_NAME (value)));
6924 *numerator = value->data[0].v_int;
6925 *denominator = value->data[1].v_int;
6931 * gst_value_set_fraction:
6932 * @value: a GValue initialized to #GST_TYPE_FRACTION
6933 * @numerator: the numerator of the fraction
6934 * @denominator: the denominator of the fraction
6936 * Sets @value to the fraction specified by @numerator over @denominator.
6937 * The fraction gets reduced to the smallest numerator and denominator,
6938 * and if necessary the sign is moved to the numerator.
6941 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
6945 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
6946 g_return_if_fail (denominator != 0);
6947 g_return_if_fail (denominator >= -G_MAXINT);
6948 g_return_if_fail (numerator >= -G_MAXINT);
6950 /* normalize sign */
6951 if (denominator < 0) {
6952 numerator = -numerator;
6953 denominator = -denominator;
6956 /* check for reduction */
6957 gcd = gst_util_greatest_common_divisor (numerator, denominator);
6963 g_assert (denominator > 0);
6965 value->data[0].v_int = numerator;
6966 value->data[1].v_int = denominator;
6970 * gst_value_get_fraction_numerator:
6971 * @value: a GValue initialized to #GST_TYPE_FRACTION
6973 * Gets the numerator of the fraction specified by @value.
6975 * Returns: the numerator of the fraction.
6978 gst_value_get_fraction_numerator (const GValue * value)
6980 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
6982 return value->data[0].v_int;
6986 * gst_value_get_fraction_denominator:
6987 * @value: a GValue initialized to #GST_TYPE_FRACTION
6989 * Gets the denominator of the fraction specified by @value.
6991 * Returns: the denominator of the fraction.
6994 gst_value_get_fraction_denominator (const GValue * value)
6996 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
6998 return value->data[1].v_int;
7002 * gst_value_fraction_multiply:
7003 * @product: a GValue initialized to #GST_TYPE_FRACTION
7004 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
7005 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
7007 * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
7008 * @product to the product of the two fractions.
7010 * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
7013 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
7014 const GValue * factor2)
7016 gint n1, n2, d1, d2;
7019 g_return_val_if_fail (product != NULL, FALSE);
7020 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
7021 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
7023 n1 = factor1->data[0].v_int;
7024 n2 = factor2->data[0].v_int;
7025 d1 = factor1->data[1].v_int;
7026 d2 = factor2->data[1].v_int;
7028 if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
7031 gst_value_set_fraction (product, res_n, res_d);
7037 * gst_value_fraction_subtract:
7038 * @dest: a GValue initialized to #GST_TYPE_FRACTION
7039 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
7040 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
7042 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
7044 * Returns: %FALSE in case of an error (like integer overflow), %TRUE otherwise.
7047 gst_value_fraction_subtract (GValue * dest,
7048 const GValue * minuend, const GValue * subtrahend)
7050 gint n1, n2, d1, d2;
7053 g_return_val_if_fail (dest != NULL, FALSE);
7054 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
7055 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
7057 n1 = minuend->data[0].v_int;
7058 n2 = subtrahend->data[0].v_int;
7059 d1 = minuend->data[1].v_int;
7060 d2 = subtrahend->data[1].v_int;
7062 if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
7064 gst_value_set_fraction (dest, res_n, res_d);
7070 gst_value_serialize_fraction (const GValue * value)
7072 gint32 numerator = value->data[0].v_int;
7073 gint32 denominator = value->data[1].v_int;
7074 gboolean positive = TRUE;
7076 /* get the sign and make components absolute */
7077 if (numerator < 0) {
7078 numerator = -numerator;
7079 positive = !positive;
7081 if (denominator < 0) {
7082 denominator = -denominator;
7083 positive = !positive;
7086 return g_strdup_printf ("%s%d/%d",
7087 positive ? "" : "-", numerator, denominator);
7091 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
7096 if (G_UNLIKELY (s == NULL))
7099 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
7102 if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
7103 if (s[num_chars] != 0)
7108 gst_value_set_fraction (dest, num, den);
7110 } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
7111 gst_value_set_fraction (dest, 1, G_MAXINT);
7113 } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
7114 if (s[num_chars] != 0)
7116 gst_value_set_fraction (dest, num, 1);
7118 } else if (g_ascii_strcasecmp (s, "min") == 0) {
7119 gst_value_set_fraction (dest, -G_MAXINT, 1);
7121 } else if (g_ascii_strcasecmp (s, "max") == 0) {
7122 gst_value_set_fraction (dest, G_MAXINT, 1);
7130 gst_value_transform_fraction_string (const GValue * src_value,
7131 GValue * dest_value)
7133 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
7137 gst_value_transform_string_fraction (const GValue * src_value,
7138 GValue * dest_value)
7140 if (!gst_value_deserialize_fraction (dest_value,
7141 src_value->data[0].v_pointer))
7142 /* If the deserialize fails, ensure we leave the fraction in a
7143 * valid, if incorrect, state */
7144 gst_value_set_fraction (dest_value, 0, 1);
7148 gst_value_transform_double_fraction (const GValue * src_value,
7149 GValue * dest_value)
7151 gdouble src = g_value_get_double (src_value);
7154 gst_util_double_to_fraction (src, &n, &d);
7155 gst_value_set_fraction (dest_value, n, d);
7159 gst_value_transform_float_fraction (const GValue * src_value,
7160 GValue * dest_value)
7162 gfloat src = g_value_get_float (src_value);
7165 gst_util_double_to_fraction (src, &n, &d);
7166 gst_value_set_fraction (dest_value, n, d);
7170 gst_value_transform_fraction_double (const GValue * src_value,
7171 GValue * dest_value)
7173 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
7174 ((double) src_value->data[1].v_int);
7178 gst_value_transform_fraction_float (const GValue * src_value,
7179 GValue * dest_value)
7181 dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
7182 ((float) src_value->data[1].v_int);
7186 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
7192 n1 = value1->data[0].v_int;
7193 n2 = value2->data[0].v_int;
7194 d1 = value1->data[1].v_int;
7195 d2 = value2->data[1].v_int;
7197 /* fractions are reduced when set, so we can quickly see if they're equal */
7198 if (n1 == n2 && d1 == d2)
7199 return GST_VALUE_EQUAL;
7201 if (d1 == 0 && d2 == 0)
7202 return GST_VALUE_UNORDERED;
7204 return GST_VALUE_GREATER_THAN;
7206 return GST_VALUE_LESS_THAN;
7208 ret = gst_util_fraction_compare (n1, d1, n2, d2);
7210 return GST_VALUE_LESS_THAN;
7212 return GST_VALUE_GREATER_THAN;
7214 /* Equality can't happen here because we check for that
7216 g_return_val_if_reached (GST_VALUE_UNORDERED);
7224 gst_value_compare_date (const GValue * value1, const GValue * value2)
7226 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
7227 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
7231 return GST_VALUE_EQUAL;
7233 if ((date1 == NULL || !g_date_valid (date1))
7234 && (date2 != NULL && g_date_valid (date2))) {
7235 return GST_VALUE_LESS_THAN;
7238 if ((date2 == NULL || !g_date_valid (date2))
7239 && (date1 != NULL && g_date_valid (date1))) {
7240 return GST_VALUE_GREATER_THAN;
7243 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
7244 || !g_date_valid (date2)) {
7245 return GST_VALUE_UNORDERED;
7248 j1 = g_date_get_julian (date1);
7249 j2 = g_date_get_julian (date2);
7252 return GST_VALUE_EQUAL;
7254 return GST_VALUE_LESS_THAN;
7256 return GST_VALUE_GREATER_THAN;
7260 gst_value_serialize_date (const GValue * val)
7262 const GDate *date = (const GDate *) g_value_get_boxed (val);
7264 if (date == NULL || !g_date_valid (date))
7265 return g_strdup ("9999-99-99");
7267 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
7268 g_date_get_month (date), g_date_get_day (date));
7272 gst_value_deserialize_date (GValue * dest, const gchar * s)
7274 guint year, month, day;
7276 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
7279 if (!g_date_valid_dmy (day, month, year))
7282 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
7291 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
7293 const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
7294 const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
7297 return GST_VALUE_EQUAL;
7299 if ((date1 == NULL) && (date2 != NULL)) {
7300 return GST_VALUE_LESS_THAN;
7302 if ((date2 == NULL) && (date1 != NULL)) {
7303 return GST_VALUE_LESS_THAN;
7306 /* returns GST_VALUE_* */
7307 return __gst_date_time_compare (date1, date2);
7311 gst_value_serialize_date_time (const GValue * val)
7313 GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
7316 return g_strdup ("null");
7318 return __gst_date_time_serialize (date, TRUE);
7322 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
7324 GstDateTime *datetime;
7326 if (!s || strcmp (s, "null") == 0) {
7330 datetime = gst_date_time_new_from_iso8601_string (s);
7331 if (datetime != NULL) {
7332 g_value_take_boxed (dest, datetime);
7335 GST_WARNING ("Failed to deserialize date time string '%s'", s);
7340 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
7342 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
7346 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
7348 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
7357 gst_value_compare_bytes (const GValue * value1, const GValue * value2)
7359 GBytes *bytes1 = (GBytes *) g_value_get_boxed (value1);
7360 GBytes *bytes2 = (GBytes *) g_value_get_boxed (value2);
7362 if (G_UNLIKELY (!bytes1 || !bytes2)) {
7363 if (bytes1 == bytes2)
7364 return GST_VALUE_EQUAL;
7366 return GST_VALUE_UNORDERED;
7369 return g_bytes_compare (bytes1, bytes2);
7373 gst_value_serialize_bytes (const GValue * value)
7375 GBytes *bytes = (GBytes *) g_value_get_boxed (value);
7379 data = bytes ? g_bytes_get_data (bytes, &len) : NULL;
7381 return g_base64_encode (data, len);
7385 gst_value_deserialize_bytes (GValue * dest, const gchar * s)
7391 g_value_set_boxed (dest, g_bytes_new (NULL, 0));
7395 data = g_base64_decode (s, &len);
7396 g_value_set_boxed (dest, g_bytes_new_take (data, len));
7405 /* helper functions */
7407 gst_value_init_bitmask (GValue * value)
7409 value->data[0].v_uint64 = 0;
7413 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
7415 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7419 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
7420 GTypeCValue * collect_values, guint collect_flags)
7422 g_return_val_if_fail (n_collect_values == 1,
7423 g_strdup_printf ("not enough value locations for `%s' passed",
7424 G_VALUE_TYPE_NAME (value)));
7426 gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
7432 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
7433 GTypeCValue * collect_values, guint collect_flags)
7435 guint64 *bitmask = collect_values[0].v_pointer;
7437 g_return_val_if_fail (bitmask != NULL,
7438 g_strdup_printf ("value for `%s' passed as NULL",
7439 G_VALUE_TYPE_NAME (value)));
7441 *bitmask = value->data[0].v_uint64;
7447 * gst_value_set_bitmask:
7448 * @value: a GValue initialized to #GST_TYPE_BITMASK
7449 * @bitmask: the bitmask
7451 * Sets @value to the bitmask specified by @bitmask.
7454 gst_value_set_bitmask (GValue * value, guint64 bitmask)
7456 g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
7458 value->data[0].v_uint64 = bitmask;
7462 * gst_value_get_bitmask:
7463 * @value: a GValue initialized to #GST_TYPE_BITMASK
7465 * Gets the bitmask specified by @value.
7467 * Returns: the bitmask.
7470 gst_value_get_bitmask (const GValue * value)
7472 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
7474 return value->data[0].v_uint64;
7478 gst_value_serialize_bitmask (const GValue * value)
7480 guint64 bitmask = value->data[0].v_uint64;
7482 return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
7486 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
7488 gchar *endptr = NULL;
7491 if (G_UNLIKELY (s == NULL))
7494 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
7498 val = g_ascii_strtoull (s, &endptr, 16);
7499 if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
7501 if (val == 0 && endptr == s)
7504 gst_value_set_bitmask (dest, val);
7510 gst_value_transform_bitmask_string (const GValue * src_value,
7511 GValue * dest_value)
7513 dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
7517 gst_value_transform_string_bitmask (const GValue * src_value,
7518 GValue * dest_value)
7520 if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
7521 gst_value_set_bitmask (dest_value, 0);
7525 gst_value_transform_uint64_bitmask (const GValue * src_value,
7526 GValue * dest_value)
7528 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7532 gst_value_transform_bitmask_uint64 (const GValue * src_value,
7533 GValue * dest_value)
7535 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
7539 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
7543 v1 = value1->data[0].v_uint64;
7544 v2 = value2->data[0].v_uint64;
7547 return GST_VALUE_EQUAL;
7549 return GST_VALUE_UNORDERED;
7556 /* helper functions */
7558 gst_value_init_flagset (GValue * value)
7560 value->data[0].v_uint = 0;
7561 value->data[1].v_uint = 0;
7565 gst_value_copy_flagset (const GValue * src_value, GValue * dest_value)
7567 dest_value->data[0].v_uint = src_value->data[0].v_uint;
7568 dest_value->data[1].v_uint = src_value->data[1].v_uint;
7572 gst_value_collect_flagset (GValue * value, guint n_collect_values,
7573 GTypeCValue * collect_values, guint collect_flags)
7575 g_return_val_if_fail (n_collect_values == 2,
7576 g_strdup_printf ("not enough value locations for `%s' passed",
7577 G_VALUE_TYPE_NAME (value)));
7579 gst_value_set_flagset (value,
7580 (guint) collect_values[0].v_int, (guint) collect_values[1].v_int);
7586 gst_value_lcopy_flagset (const GValue * value, guint n_collect_values,
7587 GTypeCValue * collect_values, guint collect_flags)
7589 guint *flags = collect_values[0].v_pointer;
7590 guint *mask = collect_values[1].v_pointer;
7592 *flags = value->data[0].v_uint;
7593 *mask = value->data[1].v_uint;
7599 * gst_value_set_flagset:
7600 * @value: a GValue initialized to %GST_TYPE_FLAG_SET
7601 * @flags: The value of the flags set or unset
7602 * @mask: The mask indicate which flags bits must match for comparisons
7604 * Sets @value to the flags and mask values provided in @flags and @mask.
7605 * The @flags value indicates the values of flags, the @mask represents
7606 * which bits in the flag value have been set, and which are "don't care"
7611 gst_value_set_flagset (GValue * value, guint flags, guint mask)
7613 g_return_if_fail (GST_VALUE_HOLDS_FLAG_SET (value));
7615 /* Normalise and only keep flags mentioned in the mask */
7616 value->data[0].v_uint = flags & mask;
7617 value->data[1].v_uint = mask;
7621 * gst_value_get_flagset_flags:
7622 * @value: a GValue initialized to #GST_TYPE_FLAG_SET
7624 * Retrieve the flags field of a GstFlagSet @value.
7626 * Returns: the flags field of the flagset instance.
7631 gst_value_get_flagset_flags (const GValue * value)
7633 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value), 0);
7635 return value->data[0].v_uint;
7639 * gst_value_get_flagset_mask:
7640 * @value: a GValue initialized to #GST_TYPE_FLAG_SET
7642 * Retrieve the mask field of a GstFlagSet @value.
7644 * Returns: the mask field of the flagset instance.
7649 gst_value_get_flagset_mask (const GValue * value)
7651 g_return_val_if_fail (GST_VALUE_HOLDS_FLAG_SET (value), 1);
7653 return value->data[1].v_uint;
7657 gst_value_serialize_flagset (const GValue * value)
7659 guint flags = value->data[0].v_uint;
7660 guint mask = value->data[1].v_uint;
7661 GstFlagSetClass *set_klass =
7662 (GstFlagSetClass *) g_type_class_ref (G_VALUE_TYPE (value));
7665 result = g_strdup_printf ("%x:%x", flags, mask);
7667 /* If this flag set class has an associated GFlags GType, and some
7668 * bits in the mask, serialize the bits in human-readable form to
7670 if (mask && set_klass->flags_type) {
7671 GFlagsClass *flags_klass =
7672 (GFlagsClass *) (g_type_class_ref (set_klass->flags_type));
7675 gboolean first = TRUE;
7677 g_return_val_if_fail (flags_klass, NULL);
7679 /* some bits in the mask are set, so serialize one by one, according
7680 * to whether that bit is set or cleared in the flags value */
7682 fl = g_flags_get_first_value (flags_klass, mask);
7684 /* No more bits match in the flags mask - time to stop */
7689 tmp = g_strconcat (result,
7691 (flags & fl->value) ? "+" : "/", fl->value_nick, NULL);
7699 g_type_class_unref (flags_klass);
7702 g_type_class_unref (set_klass);
7708 is_valid_flags_string (const gchar * s)
7710 /* We're looking to match +this/that+other-thing/not-this-thing type strings */
7711 return g_regex_match_simple ("^([\\+\\/][\\w\\d-]+)+$", s, G_REGEX_CASELESS,
7716 gst_value_deserialize_flagset (GValue * dest, const gchar * s)
7718 gboolean res = FALSE;
7722 if (G_UNLIKELY (s == NULL))
7725 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FLAG_SET (dest)))
7728 /* Flagset strings look like %x:%x - hex flags : hex bitmask,
7729 * 32-bit each, or like a concatenated list of flag nicks,
7730 * with either '+' or '/' in front. The first form
7731 * may optionally be followed by ':' and a set of text flag descriptions
7732 * for easier debugging */
7734 /* Try and interpret as hex form first, as it's the most efficient */
7735 /* Read the flags first */
7736 flags = strtoul (s, &next, 16);
7737 if (G_UNLIKELY ((flags == 0 && errno == EINVAL) || s == next))
7738 goto try_as_flags_string;
7739 /* Next char should be a colon */
7745 mask = strtoul (cur, &next, 16);
7746 if (G_UNLIKELY ((mask == 0 && errno == EINVAL) || cur == next))
7747 goto try_as_flags_string;
7749 /* Next char should be NULL terminator, or a ':'. If ':', we need the flag string after */
7750 if (G_UNLIKELY (next[0] == 0)) {
7760 if (g_str_equal (g_type_name (G_VALUE_TYPE (dest)), "GstFlagSet")) {
7761 /* If we're parsing a generic flag set, that can mean we're guessing
7762 * at the type in deserialising a GstStructure so at least check that
7763 * we have a valid-looking string, so we don't cause deserialisation of
7764 * other types of strings like 00:01:00:00 - https://bugzilla.gnome.org/show_bug.cgi?id=779755 */
7765 if (is_valid_flags_string (s)) {
7772 /* Otherwise, we already got a hex string for a valid non-generic flagset type */
7776 try_as_flags_string:
7779 const gchar *set_class = g_type_name (G_VALUE_TYPE (dest));
7780 GFlagsClass *flags_klass = NULL;
7783 if (g_str_equal (set_class, "GstFlagSet")) {
7784 /* There's no hope to parse the fields of generic flag set if we didn't already
7785 * catch a hex-string above */
7789 /* Flags class is the FlagSet class with 'Set' removed from the end */
7790 end = g_strrstr (set_class, "Set");
7793 gchar *class_name = g_strndup (set_class, end - set_class);
7794 GType flags_type = g_type_from_name (class_name);
7795 if (flags_type == G_TYPE_INVALID) {
7796 GST_TRACE ("Looking for dynamic type %s", class_name);
7797 gst_dynamic_type_factory_load (class_name);
7800 if (flags_type != G_TYPE_INVALID) {
7801 flags_klass = g_type_class_ref (flags_type);
7802 GST_TRACE ("Going to parse %s as %s", s, class_name);
7804 g_free (class_name);
7808 res = gst_value_gflags_str_to_flags (flags_klass, s, &flags, &mask);
7809 g_type_class_unref (flags_klass);
7815 gst_value_set_flagset (dest, flags, mask);
7821 gst_value_transform_flagset_string (const GValue * src_value,
7822 GValue * dest_value)
7824 dest_value->data[0].v_pointer = gst_value_serialize_flagset (src_value);
7828 gst_value_transform_string_flagset (const GValue * src_value,
7829 GValue * dest_value)
7831 if (!gst_value_deserialize_flagset (dest_value, src_value->data[0].v_pointer)) {
7832 /* If the deserialize fails, ensure we leave the flags in a
7833 * valid, if incorrect, state */
7834 gst_value_set_flagset (dest_value, 0, 0);
7839 gst_value_compare_flagset (const GValue * value1, const GValue * value2)
7844 v1 = value1->data[0].v_uint;
7845 v2 = value2->data[0].v_uint;
7847 m1 = value1->data[1].v_uint;
7848 m2 = value2->data[1].v_uint;
7850 if (v1 == v2 && m1 == m2)
7851 return GST_VALUE_EQUAL;
7853 return GST_VALUE_UNORDERED;
7856 /***********************
7857 * GstAllocationParams *
7858 ***********************/
7860 gst_value_compare_allocation_params (const GValue * value1,
7861 const GValue * value2)
7863 GstAllocationParams *v1, *v2;
7865 v1 = value1->data[0].v_pointer;
7866 v2 = value2->data[0].v_pointer;
7868 if (v1 == NULL && v1 == v2)
7869 return GST_VALUE_EQUAL;
7871 if (v1 == NULL || v2 == NULL)
7872 return GST_VALUE_UNORDERED;
7874 if (v1->flags == v2->flags && v1->align == v2->align &&
7875 v1->prefix == v2->prefix && v1->padding == v2->padding)
7876 return GST_VALUE_EQUAL;
7878 return GST_VALUE_UNORDERED;
7887 gst_value_compare_object (const GValue * value1, const GValue * value2)
7891 v1 = value1->data[0].v_pointer;
7892 v2 = value2->data[0].v_pointer;
7895 return GST_VALUE_EQUAL;
7897 return GST_VALUE_UNORDERED;
7901 gst_value_transform_object_string (const GValue * src_value,
7902 GValue * dest_value)
7907 obj = g_value_get_object (src_value);
7910 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
7911 GST_OBJECT_NAME (obj));
7913 str = g_strdup ("NULL");
7916 dest_value->data[0].v_pointer = str;
7919 static GTypeInfo _info = {
7920 0, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, NULL,
7923 static GTypeFundamentalInfo _finfo = {
7927 #define FUNC_VALUE_GET_TYPE_CLASSED(type, name, csize, flags) \
7928 GType _gst_ ## type ## _type = 0; \
7930 GType gst_ ## type ## _get_type (void) \
7932 static GType gst_ ## type ## _type = 0; \
7934 if (g_once_init_enter (&gst_ ## type ## _type)) { \
7936 _info.class_size = csize; \
7937 _finfo.type_flags = flags; \
7938 _info.value_table = & _gst_ ## type ## _value_table; \
7939 _type = g_type_register_fundamental ( \
7940 g_type_fundamental_next (), \
7941 name, &_info, &_finfo, 0); \
7942 _gst_ ## type ## _type = _type; \
7943 g_once_init_leave(&gst_ ## type ## _type, _type); \
7946 return gst_ ## type ## _type; \
7949 #define FUNC_VALUE_GET_TYPE(type, name) \
7950 FUNC_VALUE_GET_TYPE_CLASSED(type, name, 0, 0)
7952 static const GTypeValueTable _gst_int_range_value_table = {
7953 gst_value_init_int_range,
7955 gst_value_copy_int_range,
7958 gst_value_collect_int_range, (char *) "pp", gst_value_lcopy_int_range
7961 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
7963 static const GTypeValueTable _gst_int64_range_value_table = {
7964 gst_value_init_int64_range,
7965 gst_value_free_int64_range,
7966 gst_value_copy_int64_range,
7969 gst_value_collect_int64_range,
7970 (char *) "pp", gst_value_lcopy_int64_range
7973 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
7975 static const GTypeValueTable _gst_double_range_value_table = {
7976 gst_value_init_double_range,
7978 gst_value_copy_double_range,
7981 gst_value_collect_double_range,
7982 (char *) "pp", gst_value_lcopy_double_range
7985 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
7987 static const GTypeValueTable _gst_fraction_range_value_table = {
7988 gst_value_init_fraction_range,
7989 gst_value_free_fraction_range,
7990 gst_value_copy_fraction_range,
7993 gst_value_collect_fraction_range,
7994 (char *) "pppp", gst_value_lcopy_fraction_range
7997 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
7999 static const GTypeValueTable _gst_value_list_value_table = {
8000 gst_value_init_list_or_array,
8001 gst_value_free_list_or_array,
8002 gst_value_copy_list_or_array,
8003 gst_value_list_or_array_peek_pointer,
8005 gst_value_collect_list_or_array,
8006 (char *) "p", gst_value_lcopy_list_or_array
8009 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
8011 static const GTypeValueTable _gst_value_array_value_table = {
8012 gst_value_init_list_or_array,
8013 gst_value_free_list_or_array,
8014 gst_value_copy_list_or_array,
8015 gst_value_list_or_array_peek_pointer,
8017 gst_value_collect_list_or_array,
8018 (char *) "p", gst_value_lcopy_list_or_array
8021 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
8023 static const GTypeValueTable _gst_fraction_value_table = {
8024 gst_value_init_fraction,
8026 gst_value_copy_fraction,
8029 gst_value_collect_fraction, (char *) "pp", gst_value_lcopy_fraction
8032 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
8034 static const GTypeValueTable _gst_bitmask_value_table = {
8035 gst_value_init_bitmask,
8037 gst_value_copy_bitmask,
8040 gst_value_collect_bitmask, (char *) "p", gst_value_lcopy_bitmask
8043 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
8045 static const GTypeValueTable _gst_flagset_value_table = {
8046 gst_value_init_flagset,
8048 gst_value_copy_flagset,
8051 gst_value_collect_flagset, (char *) "pp", gst_value_lcopy_flagset
8054 FUNC_VALUE_GET_TYPE_CLASSED (flagset, "GstFlagSet",
8055 sizeof (GstFlagSetClass), G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE);
8058 gst_g_thread_get_type (void)
8060 return G_TYPE_THREAD;
8063 #define SERIAL_VTABLE(t,c,s,d) { t, c, s, d, NULL }
8064 #define SERIAL_VTABLE_PSPEC(t,c,s,d) { t, c, s, NULL, d }
8066 #define REGISTER_SERIALIZATION_CONST(_gtype, _type) \
8068 static const GstValueTable gst_value = \
8069 SERIAL_VTABLE (_gtype, gst_value_compare_ ## _type, \
8070 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8071 gst_value_register (&gst_value); \
8074 #define REGISTER_SERIALIZATION(_gtype, _type) \
8076 static GstValueTable gst_value = \
8077 SERIAL_VTABLE (0, gst_value_compare_ ## _type, \
8078 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8079 gst_value.type = _gtype; \
8080 gst_value_register (&gst_value); \
8083 #define REGISTER_SERIALIZATION_WITH_PSPEC(_gtype, _type) \
8085 static GstValueTable gst_value = \
8086 SERIAL_VTABLE_PSPEC (0, gst_value_compare_ ## _type, \
8087 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8088 gst_value.type = _gtype; \
8089 gst_value_register (&gst_value); \
8092 #define REGISTER_SERIALIZATION_NO_COMPARE(_gtype, _type) \
8094 static GstValueTable gst_value = \
8095 SERIAL_VTABLE (0, NULL, \
8096 gst_value_serialize_ ## _type, gst_value_deserialize_ ## _type); \
8097 gst_value.type = _gtype; \
8098 gst_value_register (&gst_value); \
8101 #define REGISTER_SERIALIZATION_COMPARE_ONLY(_gtype, _type) \
8103 static GstValueTable gst_value = \
8104 SERIAL_VTABLE (0, gst_value_compare_ ## _type, \
8106 gst_value.type = _gtype; \
8107 gst_value_register (&gst_value); \
8110 /* These initial sizes are used for the tables
8111 * below, and save a couple of reallocs at startup */
8113 static const gint GST_VALUE_TABLE_DEFAULT_SIZE = 40;
8114 static const gint GST_VALUE_UNION_TABLE_DEFAULT_SIZE = 8;
8115 static const gint GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE = 4;
8116 static const gint GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE = 16;
8119 _priv_gst_value_initialize (void)
8122 g_array_sized_new (FALSE, FALSE, sizeof (GstValueTable),
8123 GST_VALUE_TABLE_DEFAULT_SIZE);
8124 gst_value_hash = g_hash_table_new (NULL, NULL);
8125 gst_value_union_funcs = g_array_sized_new (FALSE, FALSE,
8126 sizeof (GstValueUnionInfo), GST_VALUE_UNION_TABLE_DEFAULT_SIZE);
8127 gst_value_intersect_funcs = g_array_sized_new (FALSE, FALSE,
8128 sizeof (GstValueIntersectInfo), GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE);
8129 gst_value_subtract_funcs = g_array_sized_new (FALSE, FALSE,
8130 sizeof (GstValueSubtractInfo), GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE);
8132 REGISTER_SERIALIZATION (gst_int_range_get_type (), int_range);
8133 REGISTER_SERIALIZATION (gst_int64_range_get_type (), int64_range);
8134 REGISTER_SERIALIZATION (gst_double_range_get_type (), double_range);
8135 REGISTER_SERIALIZATION (gst_fraction_range_get_type (), fraction_range);
8136 REGISTER_SERIALIZATION (g_value_array_get_type (), g_value_array);
8137 REGISTER_SERIALIZATION (gst_buffer_get_type (), buffer);
8138 REGISTER_SERIALIZATION (gst_sample_get_type (), sample);
8139 REGISTER_SERIALIZATION (gst_fraction_get_type (), fraction);
8140 REGISTER_SERIALIZATION (gst_caps_get_type (), caps);
8141 REGISTER_SERIALIZATION (gst_tag_list_get_type (), tag_list);
8142 REGISTER_SERIALIZATION (G_TYPE_DATE, date);
8143 REGISTER_SERIALIZATION (G_TYPE_BYTES, bytes);
8144 REGISTER_SERIALIZATION (gst_date_time_get_type (), date_time);
8145 REGISTER_SERIALIZATION (gst_bitmask_get_type (), bitmask);
8146 REGISTER_SERIALIZATION (gst_structure_get_type (), structure);
8147 REGISTER_SERIALIZATION (gst_flagset_get_type (), flagset);
8149 REGISTER_SERIALIZATION_NO_COMPARE (gst_segment_get_type (), segment);
8150 REGISTER_SERIALIZATION_NO_COMPARE (gst_caps_features_get_type (),
8153 REGISTER_SERIALIZATION_COMPARE_ONLY (gst_allocation_params_get_type (),
8155 REGISTER_SERIALIZATION_COMPARE_ONLY (G_TYPE_OBJECT, object);
8157 REGISTER_SERIALIZATION_CONST (G_TYPE_DOUBLE, double);
8158 REGISTER_SERIALIZATION_CONST (G_TYPE_FLOAT, float);
8160 REGISTER_SERIALIZATION_CONST (G_TYPE_STRING, string);
8161 REGISTER_SERIALIZATION_CONST (G_TYPE_BOOLEAN, boolean);
8162 REGISTER_SERIALIZATION_CONST (G_TYPE_ENUM, enum);
8164 REGISTER_SERIALIZATION_CONST (G_TYPE_FLAGS, gflags);
8166 REGISTER_SERIALIZATION_CONST (G_TYPE_INT, int);
8168 REGISTER_SERIALIZATION_CONST (G_TYPE_INT64, int64);
8169 REGISTER_SERIALIZATION_CONST (G_TYPE_LONG, long);
8171 REGISTER_SERIALIZATION_CONST (G_TYPE_UINT, uint);
8172 REGISTER_SERIALIZATION_CONST (G_TYPE_UINT64, uint64);
8173 REGISTER_SERIALIZATION_CONST (G_TYPE_ULONG, ulong);
8175 REGISTER_SERIALIZATION_CONST (G_TYPE_UCHAR, uchar);
8177 REGISTER_SERIALIZATION (G_TYPE_GTYPE, gtype);
8179 REGISTER_SERIALIZATION_WITH_PSPEC (gst_value_list_get_type (), value_list);
8180 REGISTER_SERIALIZATION_WITH_PSPEC (gst_value_array_get_type (), value_array);
8182 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
8183 gst_value_transform_int_range_string);
8184 g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
8185 gst_value_transform_int64_range_string);
8186 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
8187 gst_value_transform_double_range_string);
8188 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
8189 gst_value_transform_fraction_range_string);
8190 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
8191 gst_value_transform_list_string);
8192 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_VALUE_ARRAY,
8193 gst_value_transform_any_list_g_value_array);
8194 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
8195 gst_value_transform_array_string);
8196 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_VALUE_ARRAY,
8197 gst_value_transform_any_list_g_value_array);
8198 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, G_TYPE_STRING,
8199 gst_value_transform_g_value_array_string);
8200 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, GST_TYPE_ARRAY,
8201 gst_value_transform_g_value_array_any_list);
8202 g_value_register_transform_func (G_TYPE_VALUE_ARRAY, GST_TYPE_LIST,
8203 gst_value_transform_g_value_array_any_list);
8204 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
8205 gst_value_transform_fraction_string);
8206 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
8207 gst_value_transform_string_fraction);
8208 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
8209 gst_value_transform_fraction_double);
8210 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
8211 gst_value_transform_fraction_float);
8212 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
8213 gst_value_transform_double_fraction);
8214 g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
8215 gst_value_transform_float_fraction);
8216 g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
8217 gst_value_transform_date_string);
8218 g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
8219 gst_value_transform_string_date);
8220 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
8221 gst_value_transform_object_string);
8222 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
8223 gst_value_transform_bitmask_uint64);
8224 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
8225 gst_value_transform_bitmask_string);
8226 g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
8227 gst_value_transform_uint64_bitmask);
8228 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
8229 gst_value_transform_string_bitmask);
8231 g_value_register_transform_func (GST_TYPE_FLAG_SET, G_TYPE_STRING,
8232 gst_value_transform_flagset_string);
8233 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FLAG_SET,
8234 gst_value_transform_string_flagset);
8236 /* Only register intersection functions for *different* types.
8237 * Identical type intersection should be specified directly in
8238 * gst_value_intersect() */
8239 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8240 gst_value_intersect_int_int_range);
8241 gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
8242 gst_value_intersect_int64_int64_range);
8243 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
8244 gst_value_intersect_double_double_range);
8245 gst_value_register_intersect_func (GST_TYPE_FRACTION,
8246 GST_TYPE_FRACTION_RANGE, gst_value_intersect_fraction_fraction_range);
8248 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8249 gst_value_subtract_int_int_range);
8250 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
8251 gst_value_subtract_int_range_int);
8252 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
8253 gst_value_subtract_int_range_int_range);
8254 gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
8255 gst_value_subtract_int64_int64_range);
8256 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
8257 gst_value_subtract_int64_range_int64);
8258 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE,
8259 GST_TYPE_INT64_RANGE, gst_value_subtract_int64_range_int64_range);
8260 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
8261 gst_value_subtract_double_double_range);
8262 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
8263 gst_value_subtract_double_range_double);
8264 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
8265 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
8266 gst_value_register_subtract_func (GST_TYPE_FRACTION,
8267 GST_TYPE_FRACTION_RANGE, gst_value_subtract_fraction_fraction_range);
8268 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
8269 GST_TYPE_FRACTION, gst_value_subtract_fraction_range_fraction);
8270 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
8271 GST_TYPE_FRACTION_RANGE,
8272 gst_value_subtract_fraction_range_fraction_range);
8275 GType date_type = G_TYPE_DATE;
8277 g_type_name (date_type);
8280 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
8281 gst_value_union_int_int_range);
8282 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
8283 gst_value_union_int_range_int_range);
8284 gst_value_register_union_func (GST_TYPE_FLAG_SET, GST_TYPE_FLAG_SET,
8285 gst_value_union_flagset_flagset);
8286 gst_value_register_union_func (GST_TYPE_STRUCTURE, GST_TYPE_STRUCTURE,
8287 gst_value_union_structure_structure);
8289 #if GST_VERSION_NANO == 1
8290 /* If building from git master, check starting array sizes matched actual size
8291 * so we can keep the defines in sync and save a few reallocs on startup */
8292 if (gst_value_table->len > GST_VALUE_TABLE_DEFAULT_SIZE) {
8293 GST_ERROR ("Wrong initial gst_value_table size. "
8294 "Please set GST_VALUE_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8295 gst_value_table->len);
8297 if (gst_value_union_funcs->len > GST_VALUE_UNION_TABLE_DEFAULT_SIZE) {
8298 GST_ERROR ("Wrong initial gst_value_union_funcs table size. "
8299 "Please set GST_VALUE_UNION_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8300 gst_value_union_funcs->len);
8302 if (gst_value_intersect_funcs->len > GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE) {
8303 GST_ERROR ("Wrong initial gst_value_intersect_funcs table size. "
8304 "Please set GST_VALUE_INTERSECT_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8305 gst_value_intersect_funcs->len);
8307 if (gst_value_subtract_funcs->len > GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE) {
8308 GST_ERROR ("Wrong initial gst_value_subtract_funcs table size. "
8309 "Please set GST_VALUE_SUBTRACT_TABLE_DEFAULT_SIZE to %u in gstvalue.c",
8310 gst_value_subtract_funcs->len);
8315 /* Implement these if needed */
8316 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
8317 gst_value_union_fraction_fraction_range);
8318 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
8319 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
8324 gst_flagset_class_init (gpointer g_class, gpointer class_data)
8326 GstFlagSetClass *f_class = (GstFlagSetClass *) (g_class);
8327 f_class->flags_type = (GType) GPOINTER_TO_SIZE (class_data);
8331 * gst_flagset_register:
8332 * @flags_type: a #GType of a #G_TYPE_FLAGS type.
8334 * Create a new sub-class of #GST_TYPE_FLAG_SET
8335 * which will pretty-print the human-readable flags
8336 * when serializing, for easier debugging.
8341 gst_flagset_register (GType flags_type)
8344 sizeof (GstFlagSetClass),
8346 (GClassInitFunc) gst_flagset_class_init,
8347 NULL, GSIZE_TO_POINTER (flags_type), 0, 0, NULL, NULL
8352 g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), 0);
8354 class_name = g_strdup_printf ("%sSet", g_type_name (flags_type));
8356 t = g_type_register_static (GST_TYPE_FLAG_SET,
8357 g_intern_string (class_name), &info, 0);
8358 g_free (class_name);