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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 * @short_description: GValue implementations specific
25 * GValue implementations specific to GStreamer.
27 * Note that operations on the same #GValue from multiple threads may lead to
28 * undefined behaviour.
30 * Last reviewed on 2008-03-11 (0.10.18)
42 #include "gst_private.h"
43 #include "glib-compat-private.h"
45 #include <gobject/gvaluecollector.h>
48 typedef struct _GstValueUnionInfo GstValueUnionInfo;
49 struct _GstValueUnionInfo
53 GstValueUnionFunc func;
56 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
57 struct _GstValueIntersectInfo
61 GstValueIntersectFunc func;
64 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
65 struct _GstValueSubtractInfo
69 GstValueSubtractFunc func;
72 #define FUNDAMENTAL_TYPE_ID_MAX \
73 (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
74 #define FUNDAMENTAL_TYPE_ID(type) \
75 ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
77 #define VALUE_LIST_SIZE(v) (((GArray *) (v)->data[0].v_pointer)->len)
78 #define VALUE_LIST_GET_VALUE(v, index) ((const GValue *) &g_array_index ((GArray *) (v)->data[0].v_pointer, GValue, (index)))
80 static GArray *gst_value_table;
81 static GHashTable *gst_value_hash;
82 static GstValueTable *gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID_MAX + 1];
83 static GArray *gst_value_union_funcs;
84 static GArray *gst_value_intersect_funcs;
85 static GArray *gst_value_subtract_funcs;
87 /* Forward declarations */
88 static gchar *gst_value_serialize_fraction (const GValue * value);
90 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
91 static gint gst_value_compare_with_func (const GValue * value1,
92 const GValue * value2, GstValueCompareFunc compare);
94 static gchar *gst_string_wrap (const gchar * s);
95 static gchar *gst_string_take_and_wrap (gchar * s);
96 static gchar *gst_string_unwrap (const gchar * s);
98 static inline GstValueTable *
99 gst_value_hash_lookup_type (GType type)
101 if (G_LIKELY (G_TYPE_IS_FUNDAMENTAL (type)))
102 return gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)];
104 return g_hash_table_lookup (gst_value_hash, (gpointer) type);
108 gst_value_hash_add_type (GType type, const GstValueTable * table)
110 if (G_TYPE_IS_FUNDAMENTAL (type))
111 gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)] = (gpointer) table;
113 g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table);
120 /* two helper functions to serialize/stringify any type of list
121 * regular lists are done with { }, arrays with < >
124 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
128 GArray *array = value->data[0].v_pointer;
132 guint alen = array->len;
134 /* estimate minimum string length to minimise re-allocs in GString */
135 s = g_string_sized_new (2 + (6 * alen) + 2);
136 g_string_append (s, begin);
137 for (i = 0; i < alen; i++) {
138 v = &g_array_index (array, GValue, i);
139 s_val = gst_value_serialize (v);
140 g_string_append (s, s_val);
143 g_string_append_len (s, ", ", 2);
146 g_string_append (s, end);
147 return g_string_free (s, FALSE);
151 gst_value_transform_any_list_string (const GValue * src_value,
152 GValue * dest_value, const gchar * begin, const gchar * end)
161 array = src_value->data[0].v_pointer;
164 /* estimate minimum string length to minimise re-allocs in GString */
165 s = g_string_sized_new (2 + (10 * alen) + 2);
166 g_string_append (s, begin);
167 for (i = 0; i < alen; i++) {
168 list_value = &g_array_index (array, GValue, i);
171 g_string_append_len (s, ", ", 2);
173 list_s = g_strdup_value_contents (list_value);
174 g_string_append (s, list_s);
177 g_string_append (s, end);
179 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
183 * helper function to see if a type is fixed. Is used internally here and
184 * there. Do not export, since it doesn't work for types where the content
185 * decides the fixedness (e.g. GST_TYPE_ARRAY).
188 gst_type_is_fixed (GType type)
190 /* the basic int, string, double types */
191 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
194 /* our fundamental types that are certainly not fixed */
195 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
196 type == GST_TYPE_INT64_RANGE ||
197 type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
200 /* other (boxed) types that are fixed */
201 if (type == GST_TYPE_BUFFER) {
205 if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
206 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
213 /* GValue functions usable for both regular lists and arrays */
215 gst_value_init_list_or_array (GValue * value)
217 value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
221 copy_garray_of_gstvalue (const GArray * src)
227 dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), len);
228 g_array_set_size (dest, len);
229 for (i = 0; i < len; i++) {
230 gst_value_init_and_copy (&g_array_index (dest, GValue, i),
231 &g_array_index (src, GValue, i));
238 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
240 dest_value->data[0].v_pointer =
241 copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
245 gst_value_free_list_or_array (GValue * value)
248 GArray *src = (GArray *) value->data[0].v_pointer;
251 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
252 for (i = 0; i < len; i++) {
253 g_value_unset (&g_array_index (src, GValue, i));
255 g_array_free (src, TRUE);
260 gst_value_list_or_array_peek_pointer (const GValue * value)
262 return value->data[0].v_pointer;
266 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
267 GTypeCValue * collect_values, guint collect_flags)
269 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
270 value->data[0].v_pointer = collect_values[0].v_pointer;
271 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
273 value->data[0].v_pointer =
274 copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
280 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
281 GTypeCValue * collect_values, guint collect_flags)
283 GArray **dest = collect_values[0].v_pointer;
286 return g_strdup_printf ("value location for `%s' passed as NULL",
287 G_VALUE_TYPE_NAME (value));
288 if (!value->data[0].v_pointer)
289 return g_strdup_printf ("invalid value given for `%s'",
290 G_VALUE_TYPE_NAME (value));
291 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
292 *dest = (GArray *) value->data[0].v_pointer;
294 *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
300 gst_value_list_or_array_get_basic_type (const GValue * value, GType * type)
302 if (G_UNLIKELY (value == NULL))
305 if (GST_VALUE_HOLDS_LIST (value)) {
306 if (VALUE_LIST_SIZE (value) == 0)
308 return gst_value_list_or_array_get_basic_type (VALUE_LIST_GET_VALUE (value,
311 if (GST_VALUE_HOLDS_ARRAY (value)) {
312 const GArray *array = (const GArray *) value->data[0].v_pointer;
315 return gst_value_list_or_array_get_basic_type (&g_array_index (array,
319 *type = G_VALUE_TYPE (value);
324 #define IS_RANGE_COMPAT(type1,type2,t1,t2) \
325 (((t1) == (type1) && (t2) == (type2)) || ((t2) == (type1) && (t1) == (type2)))
328 gst_value_list_or_array_are_compatible (const GValue * value1,
329 const GValue * value2)
331 GType basic_type1, basic_type2;
333 /* empty or same type is OK */
334 if (!gst_value_list_or_array_get_basic_type (value1, &basic_type1) ||
335 !gst_value_list_or_array_get_basic_type (value2, &basic_type2) ||
336 basic_type1 == basic_type2)
339 /* ranges are distinct types for each bound type... */
340 if (IS_RANGE_COMPAT (G_TYPE_INT, GST_TYPE_INT_RANGE, basic_type1,
343 if (IS_RANGE_COMPAT (G_TYPE_INT64, GST_TYPE_INT64_RANGE, basic_type1,
346 if (IS_RANGE_COMPAT (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE, basic_type1,
349 if (IS_RANGE_COMPAT (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, basic_type1,
357 * gst_value_list_append_value:
358 * @value: a #GValue of type #GST_TYPE_LIST
359 * @append_value: the value to append
361 * Appends @append_value to the GstValueList in @value.
364 gst_value_list_append_value (GValue * value, const GValue * append_value)
368 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
369 g_return_if_fail (G_IS_VALUE (append_value));
370 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
373 gst_value_init_and_copy (&val, append_value);
374 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
378 * gst_value_list_prepend_value:
379 * @value: a #GValue of type #GST_TYPE_LIST
380 * @prepend_value: the value to prepend
382 * Prepends @prepend_value to the GstValueList in @value.
385 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
389 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
390 g_return_if_fail (G_IS_VALUE (prepend_value));
391 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
394 gst_value_init_and_copy (&val, prepend_value);
395 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
399 * gst_value_list_concat:
400 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
404 * Concatenates copies of @value1 and @value2 into a list. Values that are not
405 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
406 * @dest will be initialized to the type #GST_TYPE_LIST.
409 gst_value_list_concat (GValue * dest, const GValue * value1,
410 const GValue * value2)
412 guint i, value1_length, value2_length;
415 g_return_if_fail (dest != NULL);
416 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
417 g_return_if_fail (G_IS_VALUE (value1));
418 g_return_if_fail (G_IS_VALUE (value2));
419 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
422 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
424 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
425 g_value_init (dest, GST_TYPE_LIST);
426 array = (GArray *) dest->data[0].v_pointer;
427 g_array_set_size (array, value1_length + value2_length);
429 if (GST_VALUE_HOLDS_LIST (value1)) {
430 for (i = 0; i < value1_length; i++) {
431 gst_value_init_and_copy (&g_array_index (array, GValue, i),
432 VALUE_LIST_GET_VALUE (value1, i));
435 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
438 if (GST_VALUE_HOLDS_LIST (value2)) {
439 for (i = 0; i < value2_length; i++) {
440 gst_value_init_and_copy (&g_array_index (array, GValue,
441 i + value1_length), VALUE_LIST_GET_VALUE (value2, i));
444 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
450 * gst_value_list_merge:
451 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
455 * Merges copies of @value1 and @value2. Values that are not
456 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
458 * The result will be put into @dest and will either be a list that will not
459 * contain any duplicates, or a non-list type (if @value1 and @value2
465 gst_value_list_merge (GValue * dest, const GValue * value1,
466 const GValue * value2)
468 guint i, j, k, value1_length, value2_length, skipped;
473 g_return_if_fail (dest != NULL);
474 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
475 g_return_if_fail (G_IS_VALUE (value1));
476 g_return_if_fail (G_IS_VALUE (value2));
477 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
480 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
482 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
483 g_value_init (dest, GST_TYPE_LIST);
484 array = (GArray *) dest->data[0].v_pointer;
485 g_array_set_size (array, value1_length + value2_length);
487 if (GST_VALUE_HOLDS_LIST (value1)) {
488 for (i = 0; i < value1_length; i++) {
489 gst_value_init_and_copy (&g_array_index (array, GValue, i),
490 VALUE_LIST_GET_VALUE (value1, i));
493 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
498 if (GST_VALUE_HOLDS_LIST (value2)) {
499 for (i = 0; i < value2_length; i++) {
501 src = VALUE_LIST_GET_VALUE (value2, i);
502 for (k = 0; k < value1_length; k++) {
503 if (gst_value_compare (&g_array_index (array, GValue, k),
504 src) == GST_VALUE_EQUAL) {
511 gst_value_init_and_copy (&g_array_index (array, GValue, j), src);
517 for (k = 0; k < value1_length; k++) {
518 if (gst_value_compare (&g_array_index (array, GValue, k),
519 value2) == GST_VALUE_EQUAL) {
526 gst_value_init_and_copy (&g_array_index (array, GValue, j), value2);
530 guint new_size = value1_length + (value2_length - skipped);
534 g_array_set_size (array, new_size);
538 /* size is 1, take single value in list and make it new dest */
539 single_dest = g_array_index (array, GValue, 0);
541 /* clean up old value allocations: must set array size to 0, because
542 * allocated values are not inited meaning g_value_unset() will not
544 g_array_set_size (array, 0);
545 g_value_unset (dest);
547 /* the single value is our new result */
554 * gst_value_list_get_size:
555 * @value: a #GValue of type #GST_TYPE_LIST
557 * Gets the number of values contained in @value.
559 * Returns: the number of values
562 gst_value_list_get_size (const GValue * value)
564 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
566 return ((GArray *) value->data[0].v_pointer)->len;
570 * gst_value_list_get_value:
571 * @value: a #GValue of type #GST_TYPE_LIST
572 * @index: index of value to get from the list
574 * Gets the value that is a member of the list contained in @value and
575 * has the index @index.
577 * Returns: (transfer none): the value at the given index
580 gst_value_list_get_value (const GValue * value, guint index)
582 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
583 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
585 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
590 * gst_value_array_append_value:
591 * @value: a #GValue of type #GST_TYPE_ARRAY
592 * @append_value: the value to append
594 * Appends @append_value to the GstValueArray in @value.
597 gst_value_array_append_value (GValue * value, const GValue * append_value)
601 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
602 g_return_if_fail (G_IS_VALUE (append_value));
603 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
606 gst_value_init_and_copy (&val, append_value);
607 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
611 * gst_value_array_prepend_value:
612 * @value: a #GValue of type #GST_TYPE_ARRAY
613 * @prepend_value: the value to prepend
615 * Prepends @prepend_value to the GstValueArray in @value.
618 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
622 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
623 g_return_if_fail (G_IS_VALUE (prepend_value));
624 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
627 gst_value_init_and_copy (&val, prepend_value);
628 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
632 * gst_value_array_get_size:
633 * @value: a #GValue of type #GST_TYPE_ARRAY
635 * Gets the number of values contained in @value.
637 * Returns: the number of values
640 gst_value_array_get_size (const GValue * value)
642 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
644 return ((GArray *) value->data[0].v_pointer)->len;
648 * gst_value_array_get_value:
649 * @value: a #GValue of type #GST_TYPE_ARRAY
650 * @index: index of value to get from the array
652 * Gets the value that is a member of the array contained in @value and
653 * has the index @index.
655 * Returns: (transfer none): the value at the given index
658 gst_value_array_get_value (const GValue * value, guint index)
660 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
661 g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
663 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
668 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
670 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
674 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
676 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
679 /* Do an unordered compare of the contents of a list */
681 gst_value_compare_list (const GValue * value1, const GValue * value2)
684 GArray *array1 = value1->data[0].v_pointer;
685 GArray *array2 = value2->data[0].v_pointer;
690 GstValueCompareFunc compare;
692 /* get length and do initial length check. */
694 if (len != array2->len)
695 return GST_VALUE_UNORDERED;
697 /* place to mark removed value indices of array2 */
698 removed = g_newa (guint8, len);
699 memset (removed, 0, len);
702 /* loop over array1, all items should be in array2. When we find an
703 * item in array2, remove it from array2 by marking it as removed */
704 for (i = 0; i < len; i++) {
705 v1 = &g_array_index (array1, GValue, i);
706 if ((compare = gst_value_get_compare_func (v1))) {
707 for (j = 0; j < len; j++) {
708 /* item is removed, we can skip it */
711 v2 = &g_array_index (array2, GValue, j);
712 if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
713 /* mark item as removed now that we found it in array2 and
714 * decrement the number of remaining items in array2. */
720 /* item in array1 and not in array2, UNORDERED */
722 return GST_VALUE_UNORDERED;
724 return GST_VALUE_UNORDERED;
726 /* if not all items were removed, array2 contained something not in array1 */
728 return GST_VALUE_UNORDERED;
730 /* arrays are equal */
731 return GST_VALUE_EQUAL;
734 /* Perform an ordered comparison of the contents of an array */
736 gst_value_compare_array (const GValue * value1, const GValue * value2)
739 GArray *array1 = value1->data[0].v_pointer;
740 GArray *array2 = value2->data[0].v_pointer;
741 guint len = array1->len;
745 if (len != array2->len)
746 return GST_VALUE_UNORDERED;
748 for (i = 0; i < len; i++) {
749 v1 = &g_array_index (array1, GValue, i);
750 v2 = &g_array_index (array2, GValue, i);
751 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
752 return GST_VALUE_UNORDERED;
755 return GST_VALUE_EQUAL;
759 gst_value_serialize_list (const GValue * value)
761 return gst_value_serialize_any_list (value, "{ ", " }");
765 gst_value_deserialize_list (GValue * dest, const gchar * s)
767 g_warning ("gst_value_deserialize_list: unimplemented");
772 gst_value_serialize_array (const GValue * value)
774 return gst_value_serialize_any_list (value, "< ", " >");
778 gst_value_deserialize_array (GValue * dest, const gchar * s)
780 g_warning ("gst_value_deserialize_array: unimplemented");
787 * Values in the range are defined as any value greater or equal
788 * to min*step, AND lesser or equal to max*step.
789 * For step == 1, this falls back to the traditional range semantics.
792 #define INT_RANGE_MIN(v) (((gint *)((v)->data[0].v_pointer))[0])
793 #define INT_RANGE_MAX(v) (((gint *)((v)->data[0].v_pointer))[1])
794 #define INT_RANGE_STEP(v) (((gint *)((v)->data[0].v_pointer))[2])
797 gst_value_init_int_range (GValue * value)
799 gint *vals = g_slice_alloc0 (3 * sizeof (gint));
800 value->data[0].v_pointer = vals;
801 INT_RANGE_MIN (value) = 0;
802 INT_RANGE_MAX (value) = 0;
803 INT_RANGE_STEP (value) = 1;
807 gst_value_free_int_range (GValue * value)
809 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
810 g_slice_free1 (3 * sizeof (gint), value->data[0].v_pointer);
811 value->data[0].v_pointer = NULL;
815 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
817 gint *vals = (gint *) dest_value->data[0].v_pointer;
818 gint *src_vals = (gint *) src_value->data[0].v_pointer;
821 gst_value_init_int_range (dest_value);
823 if (src_vals != NULL) {
824 INT_RANGE_MIN (dest_value) = INT_RANGE_MIN (src_value);
825 INT_RANGE_MAX (dest_value) = INT_RANGE_MAX (src_value);
826 INT_RANGE_STEP (dest_value) = INT_RANGE_STEP (src_value);
831 gst_value_collect_int_range (GValue * value, guint n_collect_values,
832 GTypeCValue * collect_values, guint collect_flags)
834 gint *vals = value->data[0].v_pointer;
836 if (n_collect_values != 2)
837 return g_strdup_printf ("not enough value locations for `%s' passed",
838 G_VALUE_TYPE_NAME (value));
839 if (collect_values[0].v_int >= collect_values[1].v_int)
840 return g_strdup_printf ("range start is not smaller than end for `%s'",
841 G_VALUE_TYPE_NAME (value));
844 gst_value_init_int_range (value);
847 gst_value_set_int_range_step (value, collect_values[0].v_int,
848 collect_values[1].v_int, 1);
854 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
855 GTypeCValue * collect_values, guint collect_flags)
857 guint32 *int_range_start = collect_values[0].v_pointer;
858 guint32 *int_range_end = collect_values[1].v_pointer;
859 guint32 *int_range_step = collect_values[2].v_pointer;
860 gint *vals = (gint *) value->data[0].v_pointer;
862 if (!int_range_start)
863 return g_strdup_printf ("start value location for `%s' passed as NULL",
864 G_VALUE_TYPE_NAME (value));
866 return g_strdup_printf ("end value location for `%s' passed as NULL",
867 G_VALUE_TYPE_NAME (value));
869 return g_strdup_printf ("step value location for `%s' passed as NULL",
870 G_VALUE_TYPE_NAME (value));
872 if (G_UNLIKELY (vals == NULL)) {
873 return g_strdup_printf ("Uninitialised `%s' passed",
874 G_VALUE_TYPE_NAME (value));
877 *int_range_start = INT_RANGE_MIN (value);
878 *int_range_end = INT_RANGE_MAX (value);
879 *int_range_step = INT_RANGE_STEP (value);
885 * gst_value_set_int_range_step:
886 * @value: a GValue initialized to GST_TYPE_INT_RANGE
887 * @start: the start of the range
888 * @end: the end of the range
889 * @step: the step of the range
891 * Sets @value to the range specified by @start, @end and @step.
894 gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
896 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
897 g_return_if_fail (start < end);
898 g_return_if_fail (step > 0);
899 g_return_if_fail (start % step == 0);
900 g_return_if_fail (end % step == 0);
902 INT_RANGE_MIN (value) = start / step;
903 INT_RANGE_MAX (value) = end / step;
904 INT_RANGE_STEP (value) = step;
908 * gst_value_set_int_range:
909 * @value: a GValue initialized to GST_TYPE_INT_RANGE
910 * @start: the start of the range
911 * @end: the end of the range
913 * Sets @value to the range specified by @start and @end.
916 gst_value_set_int_range (GValue * value, gint start, gint end)
918 gst_value_set_int_range_step (value, start, end, 1);
922 * gst_value_get_int_range_min:
923 * @value: a GValue initialized to GST_TYPE_INT_RANGE
925 * Gets the minimum of the range specified by @value.
927 * Returns: the minimum of the range
930 gst_value_get_int_range_min (const GValue * value)
932 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
934 return INT_RANGE_MIN (value) * INT_RANGE_STEP (value);
938 * gst_value_get_int_range_max:
939 * @value: a GValue initialized to GST_TYPE_INT_RANGE
941 * Gets the maximum of the range specified by @value.
943 * Returns: the maxumum of the range
946 gst_value_get_int_range_max (const GValue * value)
948 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
950 return INT_RANGE_MAX (value) * INT_RANGE_STEP (value);
954 * gst_value_get_int_range_step:
955 * @value: a GValue initialized to GST_TYPE_INT_RANGE
957 * Gets the step of the range specified by @value.
959 * Returns: the step of the range
962 gst_value_get_int_range_step (const GValue * value)
964 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
966 return INT_RANGE_STEP (value);
970 gst_value_transform_int_range_string (const GValue * src_value,
973 if (INT_RANGE_STEP (src_value) == 1)
974 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
975 INT_RANGE_MIN (src_value), INT_RANGE_MAX (src_value));
977 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d,%d]",
978 INT_RANGE_MIN (src_value) * INT_RANGE_STEP (src_value),
979 INT_RANGE_MAX (src_value) * INT_RANGE_STEP (src_value),
980 INT_RANGE_STEP (src_value));
984 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
986 /* calculate the number of values in each range */
987 gint n1 = INT_RANGE_MAX (value1) - INT_RANGE_MIN (value1) + 1;
988 gint n2 = INT_RANGE_MAX (value2) - INT_RANGE_MIN (value2) + 1;
990 /* they must be equal */
992 return GST_VALUE_UNORDERED;
994 /* if empty, equal */
996 return GST_VALUE_EQUAL;
998 /* if more than one value, then it is only equal if the step is equal
999 and bounds lie on the same value */
1001 if (INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2) &&
1002 INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2) &&
1003 INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2)) {
1004 return GST_VALUE_EQUAL;
1006 return GST_VALUE_UNORDERED;
1008 /* if just one, only if the value is equal */
1009 if (INT_RANGE_MIN (value1) == INT_RANGE_MIN (value2))
1010 return GST_VALUE_EQUAL;
1011 return GST_VALUE_UNORDERED;
1016 gst_value_serialize_int_range (const GValue * value)
1018 if (INT_RANGE_STEP (value) == 1)
1019 return g_strdup_printf ("[ %d, %d ]", INT_RANGE_MIN (value),
1020 INT_RANGE_MAX (value));
1022 return g_strdup_printf ("[ %d, %d, %d ]",
1023 INT_RANGE_MIN (value) * INT_RANGE_STEP (value),
1024 INT_RANGE_MAX (value) * INT_RANGE_STEP (value), INT_RANGE_STEP (value));
1028 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1030 g_warning ("unimplemented");
1037 * Values in the range are defined as any value greater or equal
1038 * to min*step, AND lesser or equal to max*step.
1039 * For step == 1, this falls back to the traditional range semantics.
1042 #define INT64_RANGE_MIN(v) (((gint64 *)((v)->data[0].v_pointer))[0])
1043 #define INT64_RANGE_MAX(v) (((gint64 *)((v)->data[0].v_pointer))[1])
1044 #define INT64_RANGE_STEP(v) (((gint64 *)((v)->data[0].v_pointer))[2])
1047 gst_value_init_int64_range (GValue * value)
1049 gint64 *vals = g_slice_alloc0 (3 * sizeof (gint64));
1050 value->data[0].v_pointer = vals;
1051 INT64_RANGE_MIN (value) = 0;
1052 INT64_RANGE_MAX (value) = 0;
1053 INT64_RANGE_STEP (value) = 1;
1057 gst_value_free_int64_range (GValue * value)
1059 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1060 g_slice_free1 (3 * sizeof (gint64), value->data[0].v_pointer);
1061 value->data[0].v_pointer = NULL;
1065 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1067 gint64 *vals = (gint64 *) dest_value->data[0].v_pointer;
1068 gint64 *src_vals = (gint64 *) src_value->data[0].v_pointer;
1071 gst_value_init_int64_range (dest_value);
1074 if (src_vals != NULL) {
1075 INT64_RANGE_MIN (dest_value) = INT64_RANGE_MIN (src_value);
1076 INT64_RANGE_MAX (dest_value) = INT64_RANGE_MAX (src_value);
1077 INT64_RANGE_STEP (dest_value) = INT64_RANGE_STEP (src_value);
1082 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1083 GTypeCValue * collect_values, guint collect_flags)
1085 gint64 *vals = value->data[0].v_pointer;
1087 if (n_collect_values != 2)
1088 return g_strdup_printf ("not enough value locations for `%s' passed",
1089 G_VALUE_TYPE_NAME (value));
1090 if (collect_values[0].v_int64 >= collect_values[1].v_int64)
1091 return g_strdup_printf ("range start is not smaller than end for `%s'",
1092 G_VALUE_TYPE_NAME (value));
1095 gst_value_init_int64_range (value);
1098 gst_value_set_int64_range_step (value, collect_values[0].v_int64,
1099 collect_values[1].v_int64, 1);
1105 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1106 GTypeCValue * collect_values, guint collect_flags)
1108 guint64 *int_range_start = collect_values[0].v_pointer;
1109 guint64 *int_range_end = collect_values[1].v_pointer;
1110 guint64 *int_range_step = collect_values[2].v_pointer;
1111 gint64 *vals = (gint64 *) value->data[0].v_pointer;
1113 if (!int_range_start)
1114 return g_strdup_printf ("start value location for `%s' passed as NULL",
1115 G_VALUE_TYPE_NAME (value));
1117 return g_strdup_printf ("end value location for `%s' passed as NULL",
1118 G_VALUE_TYPE_NAME (value));
1119 if (!int_range_step)
1120 return g_strdup_printf ("step value location for `%s' passed as NULL",
1121 G_VALUE_TYPE_NAME (value));
1123 if (G_UNLIKELY (vals == NULL)) {
1124 return g_strdup_printf ("Uninitialised `%s' passed",
1125 G_VALUE_TYPE_NAME (value));
1128 *int_range_start = INT64_RANGE_MIN (value);
1129 *int_range_end = INT64_RANGE_MAX (value);
1130 *int_range_step = INT64_RANGE_STEP (value);
1136 * gst_value_set_int64_range_step:
1137 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1138 * @start: the start of the range
1139 * @end: the end of the range
1140 * @step: the step of the range
1142 * Sets @value to the range specified by @start, @end and @step.
1147 gst_value_set_int64_range_step (GValue * value, gint64 start, gint64 end,
1150 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1151 g_return_if_fail (start < end);
1152 g_return_if_fail (step > 0);
1153 g_return_if_fail (start % step == 0);
1154 g_return_if_fail (end % step == 0);
1156 INT64_RANGE_MIN (value) = start / step;
1157 INT64_RANGE_MAX (value) = end / step;
1158 INT64_RANGE_STEP (value) = step;
1162 * gst_value_set_int64_range:
1163 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1164 * @start: the start of the range
1165 * @end: the end of the range
1167 * Sets @value to the range specified by @start and @end.
1172 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1174 gst_value_set_int64_range_step (value, start, end, 1);
1178 * gst_value_get_int64_range_min:
1179 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1181 * Gets the minimum of the range specified by @value.
1183 * Returns: the minimum of the range
1188 gst_value_get_int64_range_min (const GValue * value)
1190 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1192 return INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value);
1196 * gst_value_get_int64_range_max:
1197 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1199 * Gets the maximum of the range specified by @value.
1201 * Returns: the maxumum of the range
1206 gst_value_get_int64_range_max (const GValue * value)
1208 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1210 return INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value);
1214 * gst_value_get_int64_range_step:
1215 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1217 * Gets the step of the range specified by @value.
1219 * Returns: the step of the range
1224 gst_value_get_int64_range_step (const GValue * value)
1226 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1228 return INT64_RANGE_STEP (value);
1232 gst_value_transform_int64_range_string (const GValue * src_value,
1233 GValue * dest_value)
1235 if (INT64_RANGE_STEP (src_value) == 1)
1236 dest_value->data[0].v_pointer =
1237 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1238 INT64_RANGE_MIN (src_value), INT64_RANGE_MAX (src_value));
1240 dest_value->data[0].v_pointer =
1241 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT
1242 ",%" G_GINT64_FORMAT "]",
1243 INT64_RANGE_MIN (src_value) * INT64_RANGE_STEP (src_value),
1244 INT64_RANGE_MAX (src_value) * INT64_RANGE_STEP (src_value),
1245 INT64_RANGE_STEP (src_value));
1249 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1251 /* calculate the number of values in each range */
1252 gint64 n1 = INT64_RANGE_MAX (value1) - INT64_RANGE_MIN (value1) + 1;
1253 gint64 n2 = INT64_RANGE_MAX (value2) - INT64_RANGE_MIN (value2) + 1;
1255 /* they must be equal */
1257 return GST_VALUE_UNORDERED;
1259 /* if empty, equal */
1261 return GST_VALUE_EQUAL;
1263 /* if more than one value, then it is only equal if the step is equal
1264 and bounds lie on the same value */
1266 if (INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2) &&
1267 INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2) &&
1268 INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2)) {
1269 return GST_VALUE_EQUAL;
1271 return GST_VALUE_UNORDERED;
1273 /* if just one, only if the value is equal */
1274 if (INT64_RANGE_MIN (value1) == INT64_RANGE_MIN (value2))
1275 return GST_VALUE_EQUAL;
1276 return GST_VALUE_UNORDERED;
1281 gst_value_serialize_int64_range (const GValue * value)
1283 if (INT64_RANGE_STEP (value) == 1)
1284 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1285 INT64_RANGE_MIN (value), INT64_RANGE_MAX (value));
1287 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT ", %"
1288 G_GINT64_FORMAT " ]",
1289 INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value),
1290 INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value),
1291 INT64_RANGE_STEP (value));
1295 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1297 g_warning ("unimplemented");
1306 gst_value_init_double_range (GValue * value)
1308 value->data[0].v_double = 0;
1309 value->data[1].v_double = 0;
1313 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1315 dest_value->data[0].v_double = src_value->data[0].v_double;
1316 dest_value->data[1].v_double = src_value->data[1].v_double;
1320 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1321 GTypeCValue * collect_values, guint collect_flags)
1323 if (n_collect_values != 2)
1324 return g_strdup_printf ("not enough value locations for `%s' passed",
1325 G_VALUE_TYPE_NAME (value));
1326 if (collect_values[0].v_double >= collect_values[1].v_double)
1327 return g_strdup_printf ("range start is not smaller than end for `%s'",
1328 G_VALUE_TYPE_NAME (value));
1330 value->data[0].v_double = collect_values[0].v_double;
1331 value->data[1].v_double = collect_values[1].v_double;
1337 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1338 GTypeCValue * collect_values, guint collect_flags)
1340 gdouble *double_range_start = collect_values[0].v_pointer;
1341 gdouble *double_range_end = collect_values[1].v_pointer;
1343 if (!double_range_start)
1344 return g_strdup_printf ("start value location for `%s' passed as NULL",
1345 G_VALUE_TYPE_NAME (value));
1346 if (!double_range_end)
1347 return g_strdup_printf ("end value location for `%s' passed as NULL",
1348 G_VALUE_TYPE_NAME (value));
1350 *double_range_start = value->data[0].v_double;
1351 *double_range_end = value->data[1].v_double;
1357 * gst_value_set_double_range:
1358 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1359 * @start: the start of the range
1360 * @end: the end of the range
1362 * Sets @value to the range specified by @start and @end.
1365 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1367 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1368 g_return_if_fail (start < end);
1370 value->data[0].v_double = start;
1371 value->data[1].v_double = end;
1375 * gst_value_get_double_range_min:
1376 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1378 * Gets the minimum of the range specified by @value.
1380 * Returns: the minimum of the range
1383 gst_value_get_double_range_min (const GValue * value)
1385 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1387 return value->data[0].v_double;
1391 * gst_value_get_double_range_max:
1392 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1394 * Gets the maximum of the range specified by @value.
1396 * Returns: the maxumum of the range
1399 gst_value_get_double_range_max (const GValue * value)
1401 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1403 return value->data[1].v_double;
1407 gst_value_transform_double_range_string (const GValue * src_value,
1408 GValue * dest_value)
1410 gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1412 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1413 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1414 src_value->data[0].v_double),
1415 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1416 src_value->data[1].v_double));
1420 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1422 if (value2->data[0].v_double == value1->data[0].v_double &&
1423 value2->data[0].v_double == value1->data[0].v_double)
1424 return GST_VALUE_EQUAL;
1425 return GST_VALUE_UNORDERED;
1429 gst_value_serialize_double_range (const GValue * value)
1431 gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1432 gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1434 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1435 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1436 return g_strdup_printf ("[ %s, %s ]", d1, d2);
1440 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1442 g_warning ("unimplemented");
1451 gst_value_init_fraction_range (GValue * value)
1456 ftype = GST_TYPE_FRACTION;
1458 value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1459 g_value_init (&vals[0], ftype);
1460 g_value_init (&vals[1], ftype);
1464 gst_value_free_fraction_range (GValue * value)
1466 GValue *vals = (GValue *) value->data[0].v_pointer;
1469 /* we know the two values contain fractions without internal allocs */
1470 /* g_value_unset (&vals[0]); */
1471 /* g_value_unset (&vals[1]); */
1472 g_slice_free1 (2 * sizeof (GValue), vals);
1473 value->data[0].v_pointer = NULL;
1478 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1480 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1481 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1484 gst_value_init_fraction_range (dest_value);
1485 vals = dest_value->data[0].v_pointer;
1487 if (src_vals != NULL) {
1488 g_value_copy (&src_vals[0], &vals[0]);
1489 g_value_copy (&src_vals[1], &vals[1]);
1494 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1495 GTypeCValue * collect_values, guint collect_flags)
1497 GValue *vals = (GValue *) value->data[0].v_pointer;
1499 if (n_collect_values != 4)
1500 return g_strdup_printf ("not enough value locations for `%s' passed",
1501 G_VALUE_TYPE_NAME (value));
1502 if (collect_values[1].v_int == 0)
1503 return g_strdup_printf ("passed '0' as first denominator for `%s'",
1504 G_VALUE_TYPE_NAME (value));
1505 if (collect_values[3].v_int == 0)
1506 return g_strdup_printf ("passed '0' as second denominator for `%s'",
1507 G_VALUE_TYPE_NAME (value));
1508 if (gst_util_fraction_compare (collect_values[0].v_int,
1509 collect_values[1].v_int, collect_values[2].v_int,
1510 collect_values[3].v_int) >= 0)
1511 return g_strdup_printf ("range start is not smaller than end for `%s'",
1512 G_VALUE_TYPE_NAME (value));
1515 gst_value_init_fraction_range (value);
1516 vals = value->data[0].v_pointer;
1519 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1520 collect_values[1].v_int);
1521 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1522 collect_values[3].v_int);
1528 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1529 GTypeCValue * collect_values, guint collect_flags)
1532 gint *dest_values[4];
1533 GValue *vals = (GValue *) value->data[0].v_pointer;
1535 if (G_UNLIKELY (n_collect_values != 4))
1536 return g_strdup_printf ("not enough value locations for `%s' passed",
1537 G_VALUE_TYPE_NAME (value));
1539 for (i = 0; i < 4; i++) {
1540 if (G_UNLIKELY (collect_values[i].v_pointer == NULL)) {
1541 return g_strdup_printf ("value location for `%s' passed as NULL",
1542 G_VALUE_TYPE_NAME (value));
1544 dest_values[i] = collect_values[i].v_pointer;
1547 if (G_UNLIKELY (vals == NULL)) {
1548 return g_strdup_printf ("Uninitialised `%s' passed",
1549 G_VALUE_TYPE_NAME (value));
1552 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1553 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1554 dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1555 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1560 * gst_value_set_fraction_range:
1561 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1562 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1563 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1565 * Sets @value to the range specified by @start and @end.
1568 gst_value_set_fraction_range (GValue * value, const GValue * start,
1573 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1574 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
1575 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
1576 g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
1577 start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
1579 vals = (GValue *) value->data[0].v_pointer;
1581 gst_value_init_fraction_range (value);
1582 vals = value->data[0].v_pointer;
1584 g_value_copy (start, &vals[0]);
1585 g_value_copy (end, &vals[1]);
1589 * gst_value_set_fraction_range_full:
1590 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1591 * @numerator_start: the numerator start of the range
1592 * @denominator_start: the denominator start of the range
1593 * @numerator_end: the numerator end of the range
1594 * @denominator_end: the denominator end of the range
1596 * Sets @value to the range specified by @numerator_start/@denominator_start
1597 * and @numerator_end/@denominator_end.
1600 gst_value_set_fraction_range_full (GValue * value,
1601 gint numerator_start, gint denominator_start,
1602 gint numerator_end, gint denominator_end)
1604 GValue start = { 0 };
1607 g_return_if_fail (value != NULL);
1608 g_return_if_fail (denominator_start != 0);
1609 g_return_if_fail (denominator_end != 0);
1610 g_return_if_fail (gst_util_fraction_compare (numerator_start,
1611 denominator_start, numerator_end, denominator_end) < 0);
1613 g_value_init (&start, GST_TYPE_FRACTION);
1614 g_value_init (&end, GST_TYPE_FRACTION);
1616 gst_value_set_fraction (&start, numerator_start, denominator_start);
1617 gst_value_set_fraction (&end, numerator_end, denominator_end);
1618 gst_value_set_fraction_range (value, &start, &end);
1620 /* we know the two values contain fractions without internal allocs */
1621 /* g_value_unset (&start); */
1622 /* g_value_unset (&end); */
1626 * gst_value_get_fraction_range_min:
1627 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1629 * Gets the minimum of the range specified by @value.
1631 * Returns: the minimum of the range
1634 gst_value_get_fraction_range_min (const GValue * value)
1638 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1640 vals = (GValue *) value->data[0].v_pointer;
1649 * gst_value_get_fraction_range_max:
1650 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1652 * Gets the maximum of the range specified by @value.
1654 * Returns: the maximum of the range
1657 gst_value_get_fraction_range_max (const GValue * value)
1661 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1663 vals = (GValue *) value->data[0].v_pointer;
1672 gst_value_serialize_fraction_range (const GValue * value)
1674 GValue *vals = (GValue *) value->data[0].v_pointer;
1678 retval = g_strdup ("[ 0/1, 0/1 ]");
1682 start = gst_value_serialize_fraction (&vals[0]);
1683 end = gst_value_serialize_fraction (&vals[1]);
1685 retval = g_strdup_printf ("[ %s, %s ]", start, end);
1694 gst_value_transform_fraction_range_string (const GValue * src_value,
1695 GValue * dest_value)
1697 dest_value->data[0].v_pointer =
1698 gst_value_serialize_fraction_range (src_value);
1702 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1704 GValue *vals1, *vals2;
1705 GstValueCompareFunc compare;
1707 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1708 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
1710 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1711 return GST_VALUE_UNORDERED;
1713 vals1 = (GValue *) value1->data[0].v_pointer;
1714 vals2 = (GValue *) value2->data[0].v_pointer;
1715 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1716 if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1718 gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1720 return GST_VALUE_EQUAL;
1722 return GST_VALUE_UNORDERED;
1726 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1728 g_warning ("unimplemented");
1737 * gst_value_set_caps:
1738 * @value: a GValue initialized to GST_TYPE_CAPS
1739 * @caps: (transfer none): the caps to set the value to
1741 * Sets the contents of @value to @caps. A reference to the
1742 * provided @caps will be taken by the @value.
1745 gst_value_set_caps (GValue * value, const GstCaps * caps)
1747 g_return_if_fail (G_IS_VALUE (value));
1748 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1749 g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
1751 g_value_set_boxed (value, caps);
1755 * gst_value_get_caps:
1756 * @value: a GValue initialized to GST_TYPE_CAPS
1758 * Gets the contents of @value. The reference count of the returned
1759 * #GstCaps will not be modified, therefore the caller must take one
1760 * before getting rid of the @value.
1762 * Returns: (transfer none): the contents of @value
1765 gst_value_get_caps (const GValue * value)
1767 g_return_val_if_fail (G_IS_VALUE (value), NULL);
1768 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1770 return (GstCaps *) g_value_get_boxed (value);
1774 gst_value_serialize_caps (const GValue * value)
1776 GstCaps *caps = g_value_get_boxed (value);
1778 return gst_caps_to_string (caps);
1782 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1786 caps = gst_caps_from_string (s);
1789 g_value_take_boxed (dest, caps);
1800 * gst_value_set_structure:
1801 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1802 * @structure: the structure to set the value to
1804 * Sets the contents of @value to @structure. The actual
1809 gst_value_set_structure (GValue * value, const GstStructure * structure)
1811 g_return_if_fail (G_IS_VALUE (value));
1812 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1813 g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
1815 g_value_set_boxed (value, structure);
1819 * gst_value_get_structure:
1820 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1822 * Gets the contents of @value.
1824 * Returns: (transfer none): the contents of @value
1828 const GstStructure *
1829 gst_value_get_structure (const GValue * value)
1831 g_return_val_if_fail (G_IS_VALUE (value), NULL);
1832 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1834 return (GstStructure *) g_value_get_boxed (value);
1838 gst_value_serialize_structure (const GValue * value)
1840 GstStructure *structure = g_value_get_boxed (value);
1842 return gst_string_take_and_wrap (gst_structure_to_string (structure));
1846 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1848 GstStructure *structure;
1851 structure = gst_structure_from_string (s, NULL);
1853 gchar *str = gst_string_unwrap (s);
1855 if (G_UNLIKELY (!str))
1858 structure = gst_structure_from_string (str, NULL);
1862 if (G_LIKELY (structure)) {
1863 g_value_take_boxed (dest, structure);
1874 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1876 GstBuffer *buf1 = gst_value_get_buffer (value1);
1877 GstBuffer *buf2 = gst_value_get_buffer (value2);
1879 GstMapInfo info1, info2;
1880 gint result = GST_VALUE_UNORDERED;
1882 size1 = gst_buffer_get_size (buf1);
1883 size2 = gst_buffer_get_size (buf2);
1886 return GST_VALUE_UNORDERED;
1889 return GST_VALUE_EQUAL;
1891 if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
1892 return GST_VALUE_UNORDERED;
1894 if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
1895 gst_buffer_unmap (buf1, &info2);
1896 return GST_VALUE_UNORDERED;
1899 if (memcmp (info1.data, info2.data, info1.size) == 0)
1900 result = GST_VALUE_EQUAL;
1902 gst_buffer_unmap (buf2, &info1);
1903 gst_buffer_unmap (buf1, &info2);
1909 gst_value_serialize_buffer (const GValue * value)
1917 buffer = gst_value_get_buffer (value);
1921 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
1926 string = g_malloc (info.size * 2 + 1);
1927 for (i = 0; i < info.size; i++) {
1928 sprintf (string + i * 2, "%02x", data[i]);
1930 string[info.size * 2] = 0;
1932 gst_buffer_unmap (buffer, &info);
1938 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1951 buffer = gst_buffer_new_allocate (NULL, len / 2, 0);
1952 if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
1956 for (i = 0; i < len / 2; i++) {
1957 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1960 ts[0] = s[i * 2 + 0];
1961 ts[1] = s[i * 2 + 1];
1964 data[i] = (guint8) strtoul (ts, NULL, 16);
1966 gst_buffer_unmap (buffer, &info);
1968 gst_value_take_buffer (dest, buffer);
1983 gst_buffer_unref (buffer);
1984 gst_buffer_unmap (buffer, &info);
1995 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1997 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1998 return GST_VALUE_EQUAL;
1999 return GST_VALUE_UNORDERED;
2003 gst_value_serialize_boolean (const GValue * value)
2005 if (value->data[0].v_int) {
2006 return g_strdup ("true");
2008 return g_strdup ("false");
2012 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
2014 gboolean ret = FALSE;
2016 if (g_ascii_strcasecmp (s, "true") == 0 ||
2017 g_ascii_strcasecmp (s, "yes") == 0 ||
2018 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
2019 g_value_set_boolean (dest, TRUE);
2021 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
2022 g_ascii_strcasecmp (s, "no") == 0 ||
2023 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
2024 g_value_set_boolean (dest, FALSE);
2031 #define CREATE_SERIALIZATION_START(_type,_macro) \
2033 gst_value_compare_ ## _type \
2034 (const GValue * value1, const GValue * value2) \
2036 g ## _type val1 = g_value_get_ ## _type (value1); \
2037 g ## _type val2 = g_value_get_ ## _type (value2); \
2039 return GST_VALUE_GREATER_THAN; \
2041 return GST_VALUE_LESS_THAN; \
2042 return GST_VALUE_EQUAL; \
2046 gst_value_serialize_ ## _type (const GValue * value) \
2048 GValue val = { 0, }; \
2049 g_value_init (&val, G_TYPE_STRING); \
2050 if (!g_value_transform (value, &val)) \
2051 g_assert_not_reached (); \
2052 /* NO_COPY_MADNESS!!! */ \
2053 return (char *) g_value_get_string (&val); \
2056 /* deserialize the given s into to as a gint64.
2057 * check if the result is actually storeable in the given size number of
2061 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
2062 gint64 min, gint64 max, gint size)
2064 gboolean ret = FALSE;
2069 *to = g_ascii_strtoull (s, &end, 0);
2070 /* a range error is a definitive no-no */
2071 if (errno == ERANGE) {
2078 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
2079 *to = G_LITTLE_ENDIAN;
2081 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
2084 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
2087 } else if (g_ascii_strcasecmp (s, "min") == 0) {
2090 } else if (g_ascii_strcasecmp (s, "max") == 0) {
2096 /* by definition, a gint64 fits into a gint64; so ignore those */
2097 if (size != sizeof (mask)) {
2099 /* for positive numbers, we create a mask of 1's outside of the range
2100 * and 0's inside the range. An and will thus keep only 1 bits
2101 * outside of the range */
2102 mask <<= (size * 8);
2103 if ((mask & *to) != 0) {
2107 /* for negative numbers, we do a 2's complement version */
2108 mask <<= ((size * 8) - 1);
2109 if ((mask & *to) != mask) {
2118 #define CREATE_SERIALIZATION(_type,_macro) \
2119 CREATE_SERIALIZATION_START(_type,_macro) \
2122 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
2126 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
2127 G_MAX ## _macro, sizeof (g ## _type))) { \
2128 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
2135 #define CREATE_USERIALIZATION(_type,_macro) \
2136 CREATE_SERIALIZATION_START(_type,_macro) \
2139 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
2143 gboolean ret = FALSE; \
2146 x = g_ascii_strtoull (s, &end, 0); \
2147 /* a range error is a definitive no-no */ \
2148 if (errno == ERANGE) { \
2151 /* the cast ensures the range check later on makes sense */ \
2152 x = (g ## _type) x; \
2156 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
2157 x = G_LITTLE_ENDIAN; \
2159 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
2162 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
2165 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
2168 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
2169 x = G_MAX ## _macro; \
2174 if (x > G_MAX ## _macro) { \
2177 g_value_set_ ## _type (dest, x); \
2183 #define REGISTER_SERIALIZATION(_gtype, _type) \
2185 static const GstValueTable gst_value = { \
2187 gst_value_compare_ ## _type, \
2188 gst_value_serialize_ ## _type, \
2189 gst_value_deserialize_ ## _type, \
2192 gst_value_register (&gst_value); \
2195 CREATE_SERIALIZATION (int, INT);
2196 CREATE_SERIALIZATION (int64, INT64);
2197 CREATE_SERIALIZATION (long, LONG);
2199 CREATE_USERIALIZATION (uint, UINT);
2200 CREATE_USERIALIZATION (uint64, UINT64);
2201 CREATE_USERIALIZATION (ulong, ULONG);
2203 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2205 #define G_MAXUCHAR 255
2207 CREATE_USERIALIZATION (uchar, UCHAR);
2213 gst_value_compare_double (const GValue * value1, const GValue * value2)
2215 if (value1->data[0].v_double > value2->data[0].v_double)
2216 return GST_VALUE_GREATER_THAN;
2217 if (value1->data[0].v_double < value2->data[0].v_double)
2218 return GST_VALUE_LESS_THAN;
2219 if (value1->data[0].v_double == value2->data[0].v_double)
2220 return GST_VALUE_EQUAL;
2221 return GST_VALUE_UNORDERED;
2225 gst_value_serialize_double (const GValue * value)
2227 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2229 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2230 return g_strdup (d);
2234 gst_value_deserialize_double (GValue * dest, const gchar * s)
2237 gboolean ret = FALSE;
2240 x = g_ascii_strtod (s, &end);
2244 if (g_ascii_strcasecmp (s, "min") == 0) {
2247 } else if (g_ascii_strcasecmp (s, "max") == 0) {
2253 g_value_set_double (dest, x);
2263 gst_value_compare_float (const GValue * value1, const GValue * value2)
2265 if (value1->data[0].v_float > value2->data[0].v_float)
2266 return GST_VALUE_GREATER_THAN;
2267 if (value1->data[0].v_float < value2->data[0].v_float)
2268 return GST_VALUE_LESS_THAN;
2269 if (value1->data[0].v_float == value2->data[0].v_float)
2270 return GST_VALUE_EQUAL;
2271 return GST_VALUE_UNORDERED;
2275 gst_value_serialize_float (const GValue * value)
2277 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2279 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2280 return g_strdup (d);
2284 gst_value_deserialize_float (GValue * dest, const gchar * s)
2287 gboolean ret = FALSE;
2290 x = g_ascii_strtod (s, &end);
2294 if (g_ascii_strcasecmp (s, "min") == 0) {
2297 } else if (g_ascii_strcasecmp (s, "max") == 0) {
2302 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2305 g_value_set_float (dest, (float) x);
2315 gst_value_compare_string (const GValue * value1, const GValue * value2)
2317 if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2318 /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2319 if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2320 return GST_VALUE_UNORDERED;
2322 gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2325 return GST_VALUE_LESS_THAN;
2327 return GST_VALUE_GREATER_THAN;
2330 return GST_VALUE_EQUAL;
2334 gst_string_measure_wrapping (const gchar * s)
2337 gboolean wrap = FALSE;
2339 if (G_UNLIKELY (s == NULL))
2342 /* Special case: the actual string NULL needs wrapping */
2343 if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2348 if (GST_ASCII_IS_STRING (*s)) {
2350 } else if (*s < 0x20 || *s >= 0x7f) {
2360 /* Wrap the string if we found something that needs
2361 * wrapping, or the empty string (len == 0) */
2362 return (wrap || len == 0) ? len : -1;
2366 gst_string_wrap_inner (const gchar * s, gint len)
2370 e = d = g_malloc (len + 3);
2374 if (GST_ASCII_IS_STRING (*s)) {
2376 } else if (*s < 0x20 || *s >= 0x7f) {
2378 *e++ = '0' + ((*(guchar *) s) >> 6);
2379 *e++ = '0' + (((*s) >> 3) & 0x7);
2380 *e++ = '0' + ((*s++) & 0x7);
2389 g_assert (e - d <= len + 3);
2393 /* Do string wrapping/escaping */
2395 gst_string_wrap (const gchar * s)
2397 gint len = gst_string_measure_wrapping (s);
2399 if (G_LIKELY (len < 0))
2400 return g_strdup (s);
2402 return gst_string_wrap_inner (s, len);
2405 /* Same as above, but take ownership of the string */
2407 gst_string_take_and_wrap (gchar * s)
2410 gint len = gst_string_measure_wrapping (s);
2412 if (G_LIKELY (len < 0))
2415 out = gst_string_wrap_inner (s, len);
2422 * This function takes a string delimited with double quotes (")
2423 * and unescapes any \xxx octal numbers.
2425 * If sequences of \y are found where y is not in the range of
2426 * 0->3, y is copied unescaped.
2428 * If \xyy is found where x is an octal number but y is not, an
2429 * error is encountered and NULL is returned.
2431 * the input string must be \0 terminated.
2434 gst_string_unwrap (const gchar * s)
2437 gchar *read, *write;
2439 /* NULL string returns NULL */
2443 /* strings not starting with " are invalid */
2447 /* make copy of original string to hold the result. This
2448 * string will always be smaller than the original */
2453 /* need to move to the next position as we parsed the " */
2457 if (GST_ASCII_IS_STRING (*read)) {
2458 /* normal chars are just copied */
2460 } else if (*read == '"') {
2461 /* quote marks end of string */
2463 } else if (*read == '\\') {
2464 /* got an escape char, move to next position to read a tripplet
2465 * of octal numbers */
2467 /* is the next char a possible first octal number? */
2468 if (*read >= '0' && *read <= '3') {
2469 /* parse other 2 numbers, if one of them is not in the range of
2470 * an octal number, we error. We also catch the case where a zero
2471 * byte is found here. */
2472 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2475 /* now convert the octal number to a byte again. */
2476 *write++ = ((read[0] - '0') << 6) +
2477 ((read[1] - '0') << 3) + (read[2] - '0');
2481 /* if we run into a \0 here, we definitely won't get a quote later */
2485 /* else copy \X sequence */
2489 /* weird character, error */
2493 /* if the string is not ending in " and zero terminated, we error */
2494 if (*read != '"' || read[1] != '\0')
2497 /* null terminate result string and return */
2507 gst_value_serialize_string (const GValue * value)
2509 return gst_string_wrap (value->data[0].v_pointer);
2513 gst_value_deserialize_string (GValue * dest, const gchar * s)
2515 if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2516 g_value_set_string (dest, NULL);
2518 } else if (G_LIKELY (*s != '"')) {
2519 if (!g_utf8_validate (s, -1, NULL))
2521 g_value_set_string (dest, s);
2524 gchar *str = gst_string_unwrap (s);
2525 if (G_UNLIKELY (!str))
2527 g_value_take_string (dest, str);
2538 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2540 GEnumValue *en1, *en2;
2541 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2542 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2544 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2545 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2546 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2547 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2548 g_type_class_unref (klass1);
2549 g_type_class_unref (klass2);
2550 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2551 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2552 if (en1->value < en2->value)
2553 return GST_VALUE_LESS_THAN;
2554 if (en1->value > en2->value)
2555 return GST_VALUE_GREATER_THAN;
2557 return GST_VALUE_EQUAL;
2561 gst_value_serialize_enum (const GValue * value)
2564 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
2566 g_return_val_if_fail (klass, NULL);
2567 en = g_enum_get_value (klass, g_value_get_enum (value));
2568 g_type_class_unref (klass);
2570 /* might be one of the custom formats registered later */
2571 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
2572 const GstFormatDefinition *format_def;
2574 format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
2575 g_return_val_if_fail (format_def != NULL, NULL);
2576 return g_strdup (format_def->description);
2579 g_return_val_if_fail (en, NULL);
2580 return g_strdup (en->value_name);
2584 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
2587 const GstFormatDefinition *format_def =
2588 g_value_get_pointer (format_def_value);
2590 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2593 return g_ascii_strcasecmp (s, format_def->description);
2597 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2600 gchar *endptr = NULL;
2601 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2603 g_return_val_if_fail (klass, FALSE);
2604 if (!(en = g_enum_get_value_by_name (klass, s))) {
2605 if (!(en = g_enum_get_value_by_nick (klass, s))) {
2606 gint i = strtol (s, &endptr, 0);
2608 if (endptr && *endptr == '\0') {
2609 en = g_enum_get_value (klass, i);
2613 g_type_class_unref (klass);
2615 /* might be one of the custom formats registered later */
2616 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2617 GValue res = { 0, };
2618 const GstFormatDefinition *format_def;
2622 iter = gst_format_iterate_definitions ();
2624 found = gst_iterator_find_custom (iter,
2625 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
2627 g_return_val_if_fail (found, FALSE);
2628 format_def = g_value_get_pointer (&res);
2629 g_return_val_if_fail (format_def != NULL, FALSE);
2630 g_value_set_enum (dest, (gint) format_def->value);
2631 g_value_unset (&res);
2632 gst_iterator_free (iter);
2636 g_return_val_if_fail (en, FALSE);
2637 g_value_set_enum (dest, en->value);
2645 /* we just compare the value here */
2647 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2650 GFlagsClass *klass1 =
2651 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2652 GFlagsClass *klass2 =
2653 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2655 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2656 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2657 fl1 = g_value_get_flags (value1);
2658 fl2 = g_value_get_flags (value2);
2659 g_type_class_unref (klass1);
2660 g_type_class_unref (klass2);
2662 return GST_VALUE_LESS_THAN;
2664 return GST_VALUE_GREATER_THAN;
2666 return GST_VALUE_EQUAL;
2669 /* the different flags are serialized separated with a + */
2671 gst_value_serialize_flags (const GValue * value)
2675 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2676 gchar *result, *tmp;
2677 gboolean first = TRUE;
2679 g_return_val_if_fail (klass, NULL);
2681 flags = g_value_get_flags (value);
2683 /* if no flags are set, try to serialize to the _NONE string */
2685 fl = g_flags_get_first_value (klass, flags);
2686 return g_strdup (fl->value_name);
2689 /* some flags are set, so serialize one by one */
2690 result = g_strdup ("");
2692 fl = g_flags_get_first_value (klass, flags);
2694 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2700 flags &= ~fl->value;
2703 g_type_class_unref (klass);
2709 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2712 gchar *endptr = NULL;
2713 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2718 g_return_val_if_fail (klass, FALSE);
2720 /* split into parts delimited with + */
2721 split = g_strsplit (s, "+", 0);
2725 /* loop over each part */
2727 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2728 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2729 gint val = strtol (split[i], &endptr, 0);
2731 /* just or numeric value */
2732 if (endptr && *endptr == '\0') {
2743 g_type_class_unref (klass);
2744 g_value_set_flags (dest, flags);
2754 gst_value_is_subset_int_range_int_range (const GValue * value1,
2755 const GValue * value2)
2759 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
2760 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
2762 if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
2763 INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
2765 if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
2766 INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
2769 if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
2770 if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
2771 INT_RANGE_STEP (value1))
2777 gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
2778 INT_RANGE_STEP (value2));
2779 if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
2786 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
2787 const GValue * value2)
2791 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
2792 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
2794 if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
2796 if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
2799 if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
2800 if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
2801 INT64_RANGE_STEP (value1))
2807 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
2808 INT64_RANGE_STEP (value2));
2809 if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
2816 gst_value_is_subset (const GValue * value1, const GValue * value2)
2818 /* special case for int/int64 ranges, since we cannot compute
2819 the difference for those when they have different steps,
2820 and it's actually a lot simpler to compute whether a range
2821 is a subset of another. */
2822 if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
2823 return gst_value_is_subset_int_range_int_range (value1, value2);
2824 } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
2825 && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
2826 return gst_value_is_subset_int64_range_int64_range (value1, value2);
2834 * -> 1 - [1,2] = empty
2838 * -> [1,2] - [1,3] = empty
2842 * -> {1,3} - {1,2} = 3
2845 * First caps subtraction needs to return a non-empty set, second
2846 * subtractions needs to give en empty set.
2847 * Both substractions are switched below, as it's faster that way.
2849 if (!gst_value_subtract (NULL, value1, value2)) {
2850 if (gst_value_subtract (NULL, value2, value1)) {
2862 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2863 const GValue * src2)
2865 gint v = src1->data[0].v_int;
2867 /* check if it's already in the range */
2868 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
2869 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
2870 v % INT_RANGE_STEP (src2) == 0) {
2872 gst_value_init_and_copy (dest, src2);
2876 /* check if it extends the range */
2877 if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
2879 gst_value_init_and_copy (dest, src2);
2880 --INT_RANGE_MIN (src2);
2884 if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
2886 gst_value_init_and_copy (dest, src2);
2887 ++INT_RANGE_MAX (src2);
2896 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2897 const GValue * src2)
2899 /* We can union in several special cases:
2900 1 - one is a subset of another
2901 2 - same step and not disjoint
2902 3 - different step, at least one with one value which matches a 'next' or 'previous'
2907 if (gst_value_is_subset_int_range_int_range (src1, src2)) {
2909 gst_value_init_and_copy (dest, src2);
2912 if (gst_value_is_subset_int_range_int_range (src2, src1)) {
2914 gst_value_init_and_copy (dest, src1);
2918 /* 2 - same step and not disjoint */
2919 if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
2920 if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
2921 INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
2922 (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
2923 INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
2925 gint step = INT_RANGE_STEP (src1);
2926 gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
2927 gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
2928 g_value_init (dest, GST_TYPE_INT_RANGE);
2929 gst_value_set_int_range_step (dest, min, max, step);
2935 /* 3 - single value matches next or previous */
2936 if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
2937 gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
2938 gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
2939 if (n1 == 1 || n2 == 1) {
2940 const GValue *range_value = NULL;
2944 scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
2945 } else if (n2 == 1) {
2947 scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
2951 (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
2953 gst_value_init_and_copy (dest, range_value);
2954 --INT_RANGE_MIN (range_value);
2957 } else if (scalar ==
2958 (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
2960 gst_value_init_and_copy (dest, range_value);
2961 ++INT_RANGE_MIN (range_value);
2968 /* If we get there, we did not find a way to make a union that can be
2969 represented with our simplistic model. */
2978 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2979 const GValue * src2)
2981 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
2982 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
2983 src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
2985 gst_value_init_and_copy (dest, src1);
2993 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2994 const GValue * src2)
3001 INT_RANGE_STEP (src1) /
3002 gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
3003 INT_RANGE_STEP (src2));
3004 if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
3006 step *= INT_RANGE_STEP (src2);
3009 MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
3010 INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3011 min = (min + step - 1) / step * step;
3013 MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
3014 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3015 max = max / step * step;
3019 g_value_init (dest, GST_TYPE_INT_RANGE);
3020 gst_value_set_int_range_step (dest, min, max, step);
3026 g_value_init (dest, G_TYPE_INT);
3027 g_value_set_int (dest, min);
3036 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
3037 const GValue * src2)
3039 if (INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2) <= src1->data[0].v_int &&
3040 INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2) >= src1->data[0].v_int &&
3041 src1->data[0].v_int % INT64_RANGE_STEP (src2) == 0) {
3043 gst_value_init_and_copy (dest, src1);
3051 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
3052 const GValue * src2)
3059 INT64_RANGE_STEP (src1) /
3060 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
3061 INT64_RANGE_STEP (src2));
3062 if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
3064 step *= INT64_RANGE_STEP (src2);
3067 MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
3068 INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
3069 min = (min + step - 1) / step * step;
3071 MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
3072 INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
3073 max = max / step * step;
3077 g_value_init (dest, GST_TYPE_INT64_RANGE);
3078 gst_value_set_int64_range_step (dest, min, max, step);
3084 g_value_init (dest, G_TYPE_INT64);
3085 g_value_set_int64 (dest, min);
3094 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
3095 const GValue * src2)
3097 if (src2->data[0].v_double <= src1->data[0].v_double &&
3098 src2->data[1].v_double >= src1->data[0].v_double) {
3100 gst_value_init_and_copy (dest, src1);
3108 gst_value_intersect_double_range_double_range (GValue * dest,
3109 const GValue * src1, const GValue * src2)
3114 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
3115 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
3119 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
3120 gst_value_set_double_range (dest, min, max);
3126 g_value_init (dest, G_TYPE_DOUBLE);
3127 g_value_set_int (dest, (int) min);
3136 gst_value_intersect_list (GValue * dest, const GValue * value1,
3137 const GValue * value2)
3140 GValue intersection = { 0, };
3141 gboolean ret = FALSE;
3143 size = VALUE_LIST_SIZE (value1);
3144 for (i = 0; i < size; i++) {
3145 const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
3147 /* quicker version when we don't need the resulting set */
3149 if (gst_value_intersect (NULL, cur, value2)) {
3156 if (gst_value_intersect (&intersection, cur, value2)) {
3159 gst_value_init_and_copy (dest, &intersection);
3161 } else if (GST_VALUE_HOLDS_LIST (dest)) {
3162 gst_value_list_append_value (dest, &intersection);
3164 GValue temp = { 0, };
3166 gst_value_init_and_copy (&temp, dest);
3167 g_value_unset (dest);
3168 gst_value_list_concat (dest, &temp, &intersection);
3169 g_value_unset (&temp);
3171 g_value_unset (&intersection);
3179 gst_value_intersect_array (GValue * dest, const GValue * src1,
3180 const GValue * src2)
3186 /* only works on similar-sized arrays */
3187 size = gst_value_array_get_size (src1);
3188 if (size != gst_value_array_get_size (src2))
3191 /* quicker value when we don't need the resulting set */
3193 for (n = 0; n < size; n++) {
3194 if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
3195 gst_value_array_get_value (src2, n))) {
3202 g_value_init (dest, GST_TYPE_ARRAY);
3204 for (n = 0; n < size; n++) {
3205 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
3206 gst_value_array_get_value (src2, n))) {
3207 g_value_unset (dest);
3210 gst_value_array_append_value (dest, &val);
3211 g_value_unset (&val);
3218 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
3219 const GValue * src2)
3223 GstValueCompareFunc compare;
3225 vals = src2->data[0].v_pointer;
3230 if ((compare = gst_value_get_compare_func (src1))) {
3231 res1 = gst_value_compare_with_func (&vals[0], src1, compare);
3232 res2 = gst_value_compare_with_func (&vals[1], src1, compare);
3234 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
3235 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
3237 gst_value_init_and_copy (dest, src1);
3246 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
3247 const GValue * src1, const GValue * src2)
3252 GValue *vals1, *vals2;
3253 GstValueCompareFunc compare;
3255 vals1 = src1->data[0].v_pointer;
3256 vals2 = src2->data[0].v_pointer;
3257 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
3259 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
3260 /* min = MAX (src1.start, src2.start) */
3261 res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
3262 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3263 if (res == GST_VALUE_LESS_THAN)
3264 min = &vals2[0]; /* Take the max of the 2 */
3268 /* max = MIN (src1.end, src2.end) */
3269 res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
3270 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3271 if (res == GST_VALUE_GREATER_THAN)
3272 max = &vals2[1]; /* Take the min of the 2 */
3276 res = gst_value_compare_with_func (min, max, compare);
3277 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3278 if (res == GST_VALUE_LESS_THAN) {
3280 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
3281 vals1 = dest->data[0].v_pointer;
3282 g_value_copy (min, &vals1[0]);
3283 g_value_copy (max, &vals1[1]);
3287 if (res == GST_VALUE_EQUAL) {
3289 gst_value_init_and_copy (dest, min);
3302 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
3303 const GValue * subtrahend)
3305 gint min = gst_value_get_int_range_min (subtrahend);
3306 gint max = gst_value_get_int_range_max (subtrahend);
3307 gint step = gst_value_get_int_range_step (subtrahend);
3308 gint val = g_value_get_int (minuend);
3310 /* subtracting a range from an int only works if the int is not in the
3312 if (val < min || val > max || val % step) {
3313 /* and the result is the int */
3315 gst_value_init_and_copy (dest, minuend);
3321 /* creates a new int range based on input values.
3324 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
3325 gint max2, gint step)
3329 GValue *pv1, *pv2; /* yeah, hungarian! */
3331 g_return_val_if_fail (step > 0, FALSE);
3332 g_return_val_if_fail (min1 % step == 0, FALSE);
3333 g_return_val_if_fail (max1 % step == 0, FALSE);
3334 g_return_val_if_fail (min2 % step == 0, FALSE);
3335 g_return_val_if_fail (max2 % step == 0, FALSE);
3337 if (min1 <= max1 && min2 <= max2) {
3340 } else if (min1 <= max1) {
3343 } else if (min2 <= max2) {
3354 g_value_init (pv1, GST_TYPE_INT_RANGE);
3355 gst_value_set_int_range_step (pv1, min1, max1, step);
3356 } else if (min1 == max1) {
3357 g_value_init (pv1, G_TYPE_INT);
3358 g_value_set_int (pv1, min1);
3361 g_value_init (pv2, GST_TYPE_INT_RANGE);
3362 gst_value_set_int_range_step (pv2, min2, max2, step);
3363 } else if (min2 == max2) {
3364 g_value_init (pv2, G_TYPE_INT);
3365 g_value_set_int (pv2, min2);
3368 if (min1 <= max1 && min2 <= max2) {
3369 gst_value_list_concat (dest, pv1, pv2);
3370 g_value_unset (pv1);
3371 g_value_unset (pv2);
3377 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
3378 const GValue * subtrahend)
3380 gint min = gst_value_get_int_range_min (minuend);
3381 gint max = gst_value_get_int_range_max (minuend);
3382 gint step = gst_value_get_int_range_step (minuend);
3383 gint val = g_value_get_int (subtrahend);
3385 g_return_val_if_fail (min < max, FALSE);
3387 /* value is outside of the range, return range unchanged */
3388 if (val < min || val > max || val % step) {
3390 gst_value_init_and_copy (dest, minuend);
3393 /* max must be MAXINT too as val <= max */
3394 if (val >= G_MAXINT - step + 1) {
3398 /* min must be MININT too as val >= max */
3399 if (val <= G_MININT + step - 1) {
3404 gst_value_create_new_range (dest, min, val - step, val + step, max, step);
3410 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
3411 const GValue * subtrahend)
3413 gint min1 = gst_value_get_int_range_min (minuend);
3414 gint max1 = gst_value_get_int_range_max (minuend);
3415 gint step1 = gst_value_get_int_range_step (minuend);
3416 gint min2 = gst_value_get_int_range_min (subtrahend);
3417 gint max2 = gst_value_get_int_range_max (subtrahend);
3418 gint step2 = gst_value_get_int_range_step (subtrahend);
3421 if (step1 != step2) {
3428 if (max2 >= max1 && min2 <= min1) {
3430 } else if (max2 >= max1) {
3431 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3433 } else if (min2 <= min1) {
3434 return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
3437 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3438 MAX (max2 + step, min1), max1, step);
3443 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
3444 const GValue * subtrahend)
3446 gint64 min = gst_value_get_int64_range_min (subtrahend);
3447 gint64 max = gst_value_get_int64_range_max (subtrahend);
3448 gint64 step = gst_value_get_int64_range_step (subtrahend);
3449 gint64 val = g_value_get_int64 (minuend);
3451 /* subtracting a range from an int64 only works if the int64 is not in the
3453 if (val < min || val > max || val % step) {
3454 /* and the result is the int64 */
3456 gst_value_init_and_copy (dest, minuend);
3462 /* creates a new int64 range based on input values.
3465 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
3466 gint64 min2, gint64 max2, gint64 step)
3470 GValue *pv1, *pv2; /* yeah, hungarian! */
3472 g_return_val_if_fail (step > 0, FALSE);
3473 g_return_val_if_fail (min1 % step == 0, FALSE);
3474 g_return_val_if_fail (max1 % step == 0, FALSE);
3475 g_return_val_if_fail (min2 % step == 0, FALSE);
3476 g_return_val_if_fail (max2 % step == 0, FALSE);
3478 if (min1 <= max1 && min2 <= max2) {
3481 } else if (min1 <= max1) {
3484 } else if (min2 <= max2) {
3495 g_value_init (pv1, GST_TYPE_INT64_RANGE);
3496 gst_value_set_int64_range_step (pv1, min1, max1, step);
3497 } else if (min1 == max1) {
3498 g_value_init (pv1, G_TYPE_INT64);
3499 g_value_set_int64 (pv1, min1);
3502 g_value_init (pv2, GST_TYPE_INT64_RANGE);
3503 gst_value_set_int64_range_step (pv2, min2, max2, step);
3504 } else if (min2 == max2) {
3505 g_value_init (pv2, G_TYPE_INT64);
3506 g_value_set_int64 (pv2, min2);
3509 if (min1 <= max1 && min2 <= max2) {
3510 gst_value_list_concat (dest, pv1, pv2);
3511 g_value_unset (pv1);
3512 g_value_unset (pv2);
3518 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
3519 const GValue * subtrahend)
3521 gint64 min = gst_value_get_int64_range_min (minuend);
3522 gint64 max = gst_value_get_int64_range_max (minuend);
3523 gint64 step = gst_value_get_int64_range_step (minuend);
3524 gint64 val = g_value_get_int64 (subtrahend);
3526 g_return_val_if_fail (min < max, FALSE);
3528 /* value is outside of the range, return range unchanged */
3529 if (val < min || val > max || val % step) {
3531 gst_value_init_and_copy (dest, minuend);
3534 /* max must be MAXINT64 too as val <= max */
3535 if (val >= G_MAXINT64 - step + 1) {
3539 /* min must be MININT64 too as val >= max */
3540 if (val <= G_MININT64 + step - 1) {
3545 gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
3552 gst_value_subtract_int64_range_int64_range (GValue * dest,
3553 const GValue * minuend, const GValue * subtrahend)
3555 gint64 min1 = gst_value_get_int64_range_min (minuend);
3556 gint64 max1 = gst_value_get_int64_range_max (minuend);
3557 gint64 step1 = gst_value_get_int64_range_step (minuend);
3558 gint64 min2 = gst_value_get_int64_range_min (subtrahend);
3559 gint64 max2 = gst_value_get_int64_range_max (subtrahend);
3560 gint64 step2 = gst_value_get_int64_range_step (subtrahend);
3563 if (step1 != step2) {
3570 if (max2 >= max1 && min2 <= min1) {
3572 } else if (max2 >= max1) {
3573 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
3574 max1), step, 0, step);
3575 } else if (min2 <= min1) {
3576 return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
3577 max1, step, 0, step);
3579 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
3580 max1), MAX (max2 + step, min1), max1, step);
3585 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
3586 const GValue * subtrahend)
3588 gdouble min = gst_value_get_double_range_min (subtrahend);
3589 gdouble max = gst_value_get_double_range_max (subtrahend);
3590 gdouble val = g_value_get_double (minuend);
3592 if (val < min || val > max) {
3594 gst_value_init_and_copy (dest, minuend);
3601 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
3602 const GValue * subtrahend)
3604 /* since we don't have open ranges, we cannot create a hole in
3605 * a double range. We return the original range */
3607 gst_value_init_and_copy (dest, minuend);
3612 gst_value_subtract_double_range_double_range (GValue * dest,
3613 const GValue * minuend, const GValue * subtrahend)
3615 /* since we don't have open ranges, we have to approximate */
3616 /* done like with ints */
3617 gdouble min1 = gst_value_get_double_range_min (minuend);
3618 gdouble max2 = gst_value_get_double_range_max (minuend);
3619 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
3620 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
3623 GValue *pv1, *pv2; /* yeah, hungarian! */
3625 if (min1 < max1 && min2 < max2) {
3628 } else if (min1 < max1) {
3631 } else if (min2 < max2) {
3642 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
3643 gst_value_set_double_range (pv1, min1, max1);
3646 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
3647 gst_value_set_double_range (pv2, min2, max2);
3650 if (min1 < max1 && min2 < max2) {
3651 gst_value_list_concat (dest, pv1, pv2);
3652 g_value_unset (pv1);
3653 g_value_unset (pv2);
3659 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
3660 const GValue * subtrahend)
3663 GValue subtraction = { 0, };
3664 gboolean ret = FALSE;
3667 ltype = gst_value_list_get_type ();
3669 size = VALUE_LIST_SIZE (minuend);
3670 for (i = 0; i < size; i++) {
3671 const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
3673 /* quicker version when we can discard the result */
3675 if (gst_value_subtract (NULL, cur, subtrahend)) {
3682 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
3684 gst_value_init_and_copy (dest, &subtraction);
3686 } else if (G_VALUE_HOLDS (dest, ltype)
3687 && !G_VALUE_HOLDS (&subtraction, ltype)) {
3688 gst_value_list_append_value (dest, &subtraction);
3690 GValue temp = { 0, };
3692 gst_value_init_and_copy (&temp, dest);
3693 g_value_unset (dest);
3694 gst_value_list_concat (dest, &temp, &subtraction);
3695 g_value_unset (&temp);
3697 g_value_unset (&subtraction);
3704 gst_value_subtract_list (GValue * dest, const GValue * minuend,
3705 const GValue * subtrahend)
3708 GValue data[2] = { {0,}, {0,} };
3709 GValue *subtraction = &data[0], *result = &data[1];
3711 gst_value_init_and_copy (result, minuend);
3712 size = VALUE_LIST_SIZE (subtrahend);
3713 for (i = 0; i < size; i++) {
3714 const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
3716 if (gst_value_subtract (subtraction, result, cur)) {
3717 GValue *temp = result;
3719 result = subtraction;
3721 g_value_unset (subtraction);
3723 g_value_unset (result);
3728 gst_value_init_and_copy (dest, result);
3729 g_value_unset (result);
3734 gst_value_subtract_fraction_fraction_range (GValue * dest,
3735 const GValue * minuend, const GValue * subtrahend)
3737 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
3738 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
3739 GstValueCompareFunc compare;
3741 if ((compare = gst_value_get_compare_func (minuend))) {
3742 /* subtracting a range from an fraction only works if the fraction
3743 * is not in the range */
3744 if (gst_value_compare_with_func (minuend, min, compare) ==
3745 GST_VALUE_LESS_THAN ||
3746 gst_value_compare_with_func (minuend, max, compare) ==
3747 GST_VALUE_GREATER_THAN) {
3748 /* and the result is the value */
3750 gst_value_init_and_copy (dest, minuend);
3758 gst_value_subtract_fraction_range_fraction (GValue * dest,
3759 const GValue * minuend, const GValue * subtrahend)
3761 /* since we don't have open ranges, we cannot create a hole in
3762 * a range. We return the original range */
3764 gst_value_init_and_copy (dest, minuend);
3769 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
3770 const GValue * minuend, const GValue * subtrahend)
3772 /* since we don't have open ranges, we have to approximate */
3773 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
3774 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
3775 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
3776 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
3777 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
3781 GValue *pv1, *pv2; /* yeah, hungarian! */
3782 GstValueCompareFunc compare;
3784 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
3785 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
3787 compare = gst_value_get_compare_func (min1);
3788 g_return_val_if_fail (compare, FALSE);
3790 cmp1 = gst_value_compare_with_func (max2, max1, compare);
3791 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3792 if (cmp1 == GST_VALUE_LESS_THAN)
3794 cmp1 = gst_value_compare_with_func (min1, min2, compare);
3795 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3796 if (cmp1 == GST_VALUE_GREATER_THAN)
3799 cmp1 = gst_value_compare_with_func (min1, max1, compare);
3800 cmp2 = gst_value_compare_with_func (min2, max2, compare);
3802 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3805 } else if (cmp1 == GST_VALUE_LESS_THAN) {
3808 } else if (cmp2 == GST_VALUE_LESS_THAN) {
3818 if (cmp1 == GST_VALUE_LESS_THAN) {
3819 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
3820 gst_value_set_fraction_range (pv1, min1, max1);
3822 if (cmp2 == GST_VALUE_LESS_THAN) {
3823 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
3824 gst_value_set_fraction_range (pv2, min2, max2);
3827 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3828 gst_value_list_concat (dest, pv1, pv2);
3829 g_value_unset (pv1);
3830 g_value_unset (pv2);
3841 * gst_value_get_compare_func:
3842 * @value1: a value to get the compare function for
3844 * Determines the compare function to be used with values of the same type as
3845 * @value1. The function can be given to gst_value_compare_with_func().
3847 * Returns: A #GstValueCompareFunc value
3849 static GstValueCompareFunc
3850 gst_value_get_compare_func (const GValue * value1)
3852 GstValueTable *table, *best = NULL;
3856 type1 = G_VALUE_TYPE (value1);
3858 /* this is a fast check */
3859 best = gst_value_hash_lookup_type (type1);
3862 if (G_UNLIKELY (!best || !best->compare)) {
3863 guint len = gst_value_table->len;
3866 for (i = 0; i < len; i++) {
3867 table = &g_array_index (gst_value_table, GstValueTable, i);
3868 if (table->compare && g_type_is_a (type1, table->type)) {
3869 if (!best || g_type_is_a (table->type, best->type))
3874 if (G_LIKELY (best))
3875 return best->compare;
3881 * gst_value_can_compare:
3882 * @value1: a value to compare
3883 * @value2: another value to compare
3885 * Determines if @value1 and @value2 can be compared.
3887 * Returns: TRUE if the values can be compared
3890 gst_value_can_compare (const GValue * value1, const GValue * value2)
3892 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3893 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3895 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3898 return gst_value_get_compare_func (value1) != NULL;
3902 gst_value_list_equals_range (const GValue * list, const GValue * value)
3904 const GValue *first;
3907 g_return_val_if_fail (G_IS_VALUE (list), FALSE);
3908 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
3909 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (list), FALSE);
3911 /* TODO: compare against an empty list ? No type though... */
3912 list_size = VALUE_LIST_SIZE (list);
3916 /* compare the basic types - they have to match */
3917 first = VALUE_LIST_GET_VALUE (list, 0);
3918 #define CHECK_TYPES(type,prefix) \
3919 (prefix##_VALUE_HOLDS_##type(first) && GST_VALUE_HOLDS_##type##_RANGE (value))
3920 if (CHECK_TYPES (INT, G)) {
3921 const gint rmin = gst_value_get_int_range_min (value);
3922 const gint rmax = gst_value_get_int_range_max (value);
3923 const gint rstep = gst_value_get_int_range_step (value);
3924 /* note: this will overflow for min 0 and max INT_MAX, but this
3925 would only be equal to a list of INT_MAX elements, which seems
3927 if (list_size != rmax / rstep - rmin / rstep + 1)
3929 for (n = 0; n < list_size; ++n) {
3930 gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
3931 if (v < rmin || v > rmax || v % rstep) {
3936 } else if (CHECK_TYPES (INT64, G)) {
3937 const gint64 rmin = gst_value_get_int64_range_min (value);
3938 const gint64 rmax = gst_value_get_int64_range_max (value);
3939 const gint64 rstep = gst_value_get_int64_range_step (value);
3940 GST_DEBUG ("List/range of int64s");
3941 if (list_size != rmax / rstep - rmin / rstep + 1)
3943 for (n = 0; n < list_size; ++n) {
3944 gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
3945 if (v < rmin || v > rmax || v % rstep)
3952 /* other combinations don't make sense for equality */
3957 * gst_value_compare:
3958 * @value1: a value to compare
3959 * @value2: another value to compare
3961 * Compares @value1 and @value2. If @value1 and @value2 cannot be
3962 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
3963 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
3964 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
3965 * If the values are equal, GST_VALUE_EQUAL is returned.
3967 * Returns: comparison result
3970 gst_value_compare (const GValue * value1, const GValue * value2)
3972 GstValueCompareFunc compare;
3975 g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
3976 g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
3978 /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
3979 as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
3980 ltype = gst_value_list_get_type ();
3981 if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)) {
3983 if (gst_value_list_equals_range (value1, value2)) {
3984 return GST_VALUE_EQUAL;
3985 } else if (gst_value_list_get_size (value1) == 1) {
3988 elt = gst_value_list_get_value (value1, 0);
3989 return gst_value_compare (elt, value2);
3991 } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)) {
3992 if (gst_value_list_equals_range (value2, value1)) {
3993 return GST_VALUE_EQUAL;
3994 } else if (gst_value_list_get_size (value2) == 1) {
3997 elt = gst_value_list_get_value (value2, 0);
3998 return gst_value_compare (elt, value1);
4002 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4003 return GST_VALUE_UNORDERED;
4005 compare = gst_value_get_compare_func (value1);
4007 return compare (value1, value2);
4010 g_critical ("unable to compare values of type %s\n",
4011 g_type_name (G_VALUE_TYPE (value1)));
4012 return GST_VALUE_UNORDERED;
4016 * gst_value_compare_with_func:
4017 * @value1: a value to compare
4018 * @value2: another value to compare
4019 * @compare: compare function
4021 * Compares @value1 and @value2 using the @compare function. Works like
4022 * gst_value_compare() but allows to save time determining the compare function
4025 * Returns: comparison result
4028 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
4029 GstValueCompareFunc compare)
4033 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4034 return GST_VALUE_UNORDERED;
4036 return compare (value1, value2);
4042 * gst_value_can_union:
4043 * @value1: a value to union
4044 * @value2: another value to union
4046 * Determines if @value1 and @value2 can be non-trivially unioned.
4047 * Any two values can be trivially unioned by adding both of them
4048 * to a GstValueList. However, certain types have the possibility
4049 * to be unioned in a simpler way. For example, an integer range
4050 * and an integer can be unioned if the integer is a subset of the
4051 * integer range. If there is the possibility that two values can
4052 * be unioned, this function returns TRUE.
4054 * Returns: TRUE if there is a function allowing the two values to
4058 gst_value_can_union (const GValue * value1, const GValue * value2)
4060 GstValueUnionInfo *union_info;
4063 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4064 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4066 len = gst_value_union_funcs->len;
4068 for (i = 0; i < len; i++) {
4069 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4070 if (union_info->type1 == G_VALUE_TYPE (value1) &&
4071 union_info->type2 == G_VALUE_TYPE (value2))
4073 if (union_info->type1 == G_VALUE_TYPE (value2) &&
4074 union_info->type2 == G_VALUE_TYPE (value1))
4083 * @dest: (out caller-allocates): the destination value
4084 * @value1: a value to union
4085 * @value2: another value to union
4087 * Creates a GValue corresponding to the union of @value1 and @value2.
4089 * Returns: TRUE if the union suceeded.
4092 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
4094 const GstValueUnionInfo *union_info;
4098 g_return_val_if_fail (dest != NULL, FALSE);
4099 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4100 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4101 g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
4104 len = gst_value_union_funcs->len;
4105 type1 = G_VALUE_TYPE (value1);
4106 type2 = G_VALUE_TYPE (value2);
4108 for (i = 0; i < len; i++) {
4109 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4110 if (union_info->type1 == type1 && union_info->type2 == type2) {
4111 return union_info->func (dest, value1, value2);
4113 if (union_info->type1 == type2 && union_info->type2 == type1) {
4114 return union_info->func (dest, value2, value1);
4118 gst_value_list_concat (dest, value1, value2);
4123 * gst_value_register_union_func: (skip)
4124 * @type1: a type to union
4125 * @type2: another type to union
4126 * @func: a function that implements creating a union between the two types
4128 * Registers a union function that can create a union between #GValue items
4129 * of the type @type1 and @type2.
4131 * Union functions should be registered at startup before any pipelines are
4132 * started, as gst_value_register_union_func() is not thread-safe and cannot
4133 * be used at the same time as gst_value_union() or gst_value_can_union().
4136 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
4138 GstValueUnionInfo union_info;
4140 union_info.type1 = type1;
4141 union_info.type2 = type2;
4142 union_info.func = func;
4144 g_array_append_val (gst_value_union_funcs, union_info);
4150 * gst_value_can_intersect:
4151 * @value1: a value to intersect
4152 * @value2: another value to intersect
4154 * Determines if intersecting two values will produce a valid result.
4155 * Two values will produce a valid intersection if they have the same
4156 * type, or if there is a method (registered by
4157 * gst_value_register_intersect_func()) to calculate the intersection.
4159 * Returns: TRUE if the values can intersect
4162 gst_value_can_intersect (const GValue * value1, const GValue * value2)
4164 GstValueIntersectInfo *intersect_info;
4166 GType ltype, type1, type2;
4168 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4169 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4171 ltype = gst_value_list_get_type ();
4174 if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
4177 type1 = G_VALUE_TYPE (value1);
4178 type2 = G_VALUE_TYPE (value2);
4180 /* practically all GstValue types have a compare function (_can_compare=TRUE)
4181 * GstStructure and GstCaps have npot, but are intersectable */
4185 /* check registered intersect functions */
4186 len = gst_value_intersect_funcs->len;
4187 for (i = 0; i < len; i++) {
4188 intersect_info = &g_array_index (gst_value_intersect_funcs,
4189 GstValueIntersectInfo, i);
4190 if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
4191 (intersect_info->type1 == type2 && intersect_info->type2 == type1))
4195 return gst_value_can_compare (value1, value2);
4199 * gst_value_intersect:
4200 * @dest: (out caller-allocates) (transfer full): a uninitialized #GValue that will hold the calculated
4201 * intersection value. May be NULL if the resulting set if not needed.
4202 * @value1: a value to intersect
4203 * @value2: another value to intersect
4205 * Calculates the intersection of two values. If the values have
4206 * a non-empty intersection, the value representing the intersection
4207 * is placed in @dest, unless NULL. If the intersection is non-empty,
4208 * @dest is not modified.
4210 * Returns: TRUE if the intersection is non-empty
4213 gst_value_intersect (GValue * dest, const GValue * value1,
4214 const GValue * value2)
4216 GstValueIntersectInfo *intersect_info;
4218 GType ltype, type1, type2;
4220 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4221 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4223 ltype = gst_value_list_get_type ();
4225 /* special cases first */
4226 if (G_VALUE_HOLDS (value1, ltype))
4227 return gst_value_intersect_list (dest, value1, value2);
4228 if (G_VALUE_HOLDS (value2, ltype))
4229 return gst_value_intersect_list (dest, value2, value1);
4231 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
4233 gst_value_init_and_copy (dest, value1);
4237 type1 = G_VALUE_TYPE (value1);
4238 type2 = G_VALUE_TYPE (value2);
4240 len = gst_value_intersect_funcs->len;
4241 for (i = 0; i < len; i++) {
4242 intersect_info = &g_array_index (gst_value_intersect_funcs,
4243 GstValueIntersectInfo, i);
4244 if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
4245 return intersect_info->func (dest, value1, value2);
4247 if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
4248 return intersect_info->func (dest, value2, value1);
4257 * gst_value_register_intersect_func: (skip)
4258 * @type1: the first type to intersect
4259 * @type2: the second type to intersect
4260 * @func: the intersection function
4262 * Registers a function that is called to calculate the intersection
4263 * of the values having the types @type1 and @type2.
4265 * Intersect functions should be registered at startup before any pipelines are
4266 * started, as gst_value_register_intersect_func() is not thread-safe and
4267 * cannot be used at the same time as gst_value_intersect() or
4268 * gst_value_can_intersect().
4271 gst_value_register_intersect_func (GType type1, GType type2,
4272 GstValueIntersectFunc func)
4274 GstValueIntersectInfo intersect_info;
4276 intersect_info.type1 = type1;
4277 intersect_info.type2 = type2;
4278 intersect_info.func = func;
4280 g_array_append_val (gst_value_intersect_funcs, intersect_info);
4287 * gst_value_subtract:
4288 * @dest: (out caller-allocates): the destination value for the result if the
4289 * subtraction is not empty. May be NULL, in which case the resulting set
4290 * will not be computed, which can give a fair speedup.
4291 * @minuend: the value to subtract from
4292 * @subtrahend: the value to subtract
4294 * Subtracts @subtrahend from @minuend and stores the result in @dest.
4295 * Note that this means subtraction as in sets, not as in mathematics.
4297 * Returns: %TRUE if the subtraction is not empty
4300 gst_value_subtract (GValue * dest, const GValue * minuend,
4301 const GValue * subtrahend)
4303 GstValueSubtractInfo *info;
4305 GType ltype, mtype, stype;
4307 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4308 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4310 ltype = gst_value_list_get_type ();
4312 /* special cases first */
4313 if (G_VALUE_HOLDS (minuend, ltype))
4314 return gst_value_subtract_from_list (dest, minuend, subtrahend);
4315 if (G_VALUE_HOLDS (subtrahend, ltype))
4316 return gst_value_subtract_list (dest, minuend, subtrahend);
4318 mtype = G_VALUE_TYPE (minuend);
4319 stype = G_VALUE_TYPE (subtrahend);
4321 len = gst_value_subtract_funcs->len;
4322 for (i = 0; i < len; i++) {
4323 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4324 if (info->minuend == mtype && info->subtrahend == stype) {
4325 return info->func (dest, minuend, subtrahend);
4329 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
4331 gst_value_init_and_copy (dest, minuend);
4340 gst_value_subtract (GValue * dest, const GValue * minuend,
4341 const GValue * subtrahend)
4343 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
4345 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
4346 gst_value_serialize (subtrahend),
4347 ret ? gst_value_serialize (dest) : "---");
4353 * gst_value_can_subtract:
4354 * @minuend: the value to subtract from
4355 * @subtrahend: the value to subtract
4357 * Checks if it's possible to subtract @subtrahend from @minuend.
4359 * Returns: TRUE if a subtraction is possible
4362 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
4364 GstValueSubtractInfo *info;
4366 GType ltype, mtype, stype;
4368 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4369 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4371 ltype = gst_value_list_get_type ();
4374 if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
4377 mtype = G_VALUE_TYPE (minuend);
4378 stype = G_VALUE_TYPE (subtrahend);
4380 len = gst_value_subtract_funcs->len;
4381 for (i = 0; i < len; i++) {
4382 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4383 if (info->minuend == mtype && info->subtrahend == stype)
4387 return gst_value_can_compare (minuend, subtrahend);
4391 * gst_value_register_subtract_func: (skip)
4392 * @minuend_type: type of the minuend
4393 * @subtrahend_type: type of the subtrahend
4394 * @func: function to use
4396 * Registers @func as a function capable of subtracting the values of
4397 * @subtrahend_type from values of @minuend_type.
4399 * Subtract functions should be registered at startup before any pipelines are
4400 * started, as gst_value_register_subtract_func() is not thread-safe and
4401 * cannot be used at the same time as gst_value_subtract().
4404 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
4405 GstValueSubtractFunc func)
4407 GstValueSubtractInfo info;
4409 /* one type must be unfixed, other subtractions can be done as comparisons,
4410 * special case: bitmasks */
4411 if (minuend_type != GST_TYPE_BITMASK)
4412 g_return_if_fail (!gst_type_is_fixed (minuend_type)
4413 || !gst_type_is_fixed (subtrahend_type));
4415 info.minuend = minuend_type;
4416 info.subtrahend = subtrahend_type;
4419 g_array_append_val (gst_value_subtract_funcs, info);
4423 * gst_value_register:
4424 * @table: structure containing functions to register
4426 * Registers functions to perform calculations on #GValue items of a given
4427 * type. Each type can only be added once.
4430 gst_value_register (const GstValueTable * table)
4432 GstValueTable *found;
4434 g_return_if_fail (table != NULL);
4436 g_array_append_val (gst_value_table, *table);
4438 found = gst_value_hash_lookup_type (table->type);
4440 g_warning ("adding type %s multiple times", g_type_name (table->type));
4442 /* FIXME: we're not really doing the const justice, we assume the table is
4444 gst_value_hash_add_type (table->type, table);
4448 * gst_value_init_and_copy:
4449 * @dest: (out caller-allocates): the target value
4450 * @src: the source value
4452 * Initialises the target value to be of the same type as source and then copies
4453 * the contents from source to target.
4456 gst_value_init_and_copy (GValue * dest, const GValue * src)
4458 g_return_if_fail (G_IS_VALUE (src));
4459 g_return_if_fail (dest != NULL);
4461 g_value_init (dest, G_VALUE_TYPE (src));
4462 g_value_copy (src, dest);
4466 * gst_value_serialize:
4467 * @value: a #GValue to serialize
4469 * tries to transform the given @value into a string representation that allows
4470 * getting back this string later on using gst_value_deserialize().
4472 * Free-function: g_free
4474 * Returns: (transfer full): the serialization for @value or NULL if none exists
4477 gst_value_serialize (const GValue * value)
4480 GValue s_val = { 0 };
4481 GstValueTable *table, *best;
4485 g_return_val_if_fail (G_IS_VALUE (value), NULL);
4487 type = G_VALUE_TYPE (value);
4489 best = gst_value_hash_lookup_type (type);
4491 if (G_UNLIKELY (!best || !best->serialize)) {
4492 len = gst_value_table->len;
4494 for (i = 0; i < len; i++) {
4495 table = &g_array_index (gst_value_table, GstValueTable, i);
4496 if (table->serialize && g_type_is_a (type, table->type)) {
4497 if (!best || g_type_is_a (table->type, best->type))
4502 if (G_LIKELY (best))
4503 return best->serialize (value);
4505 g_value_init (&s_val, G_TYPE_STRING);
4506 if (g_value_transform (value, &s_val)) {
4507 s = gst_string_wrap (g_value_get_string (&s_val));
4511 g_value_unset (&s_val);
4517 * gst_value_deserialize:
4518 * @dest: (out caller-allocates): #GValue to fill with contents of
4520 * @src: string to deserialize
4522 * Tries to deserialize a string into the type specified by the given GValue.
4523 * If the operation succeeds, TRUE is returned, FALSE otherwise.
4525 * Returns: TRUE on success
4528 gst_value_deserialize (GValue * dest, const gchar * src)
4530 GstValueTable *table, *best;
4534 g_return_val_if_fail (src != NULL, FALSE);
4535 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
4537 type = G_VALUE_TYPE (dest);
4539 best = gst_value_hash_lookup_type (type);
4540 if (G_UNLIKELY (!best || !best->deserialize)) {
4541 len = gst_value_table->len;
4543 for (i = 0; i < len; i++) {
4544 table = &g_array_index (gst_value_table, GstValueTable, i);
4545 if (table->deserialize && g_type_is_a (type, table->type)) {
4546 if (!best || g_type_is_a (table->type, best->type))
4551 if (G_LIKELY (best))
4552 return best->deserialize (dest, src);
4558 * gst_value_is_fixed:
4559 * @value: the #GValue to check
4561 * Tests if the given GValue, if available in a GstStructure (or any other
4562 * container) contains a "fixed" (which means: one value) or an "unfixed"
4563 * (which means: multiple possible values, such as data lists or data
4566 * Returns: true if the value is "fixed".
4570 gst_value_is_fixed (const GValue * value)
4574 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
4576 type = G_VALUE_TYPE (value);
4578 /* the most common types are just basic plain glib types */
4579 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
4583 if (type == GST_TYPE_ARRAY) {
4587 /* check recursively */
4588 size = gst_value_array_get_size (value);
4589 for (n = 0; n < size; n++) {
4590 kid = gst_value_array_get_value (value, n);
4591 if (!gst_value_is_fixed (kid))
4596 return gst_type_is_fixed (type);
4601 * @dest: the #GValue destination
4602 * @src: the #GValue to fixate
4604 * Fixate @src into a new value @dest.
4605 * For ranges, the first element is taken. For lists and arrays, the
4606 * first item is fixated and returned.
4607 * If @src is already fixed, this function returns FALSE.
4609 * Returns: true if @dest contains a fixated version of @src.
4612 gst_value_fixate (GValue * dest, const GValue * src)
4614 g_return_val_if_fail (G_IS_VALUE (src), FALSE);
4615 g_return_val_if_fail (dest != NULL, FALSE);
4617 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
4618 g_value_init (dest, G_TYPE_INT);
4619 g_value_set_int (dest, gst_value_get_int_range_min (src));
4620 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
4621 g_value_init (dest, G_TYPE_DOUBLE);
4622 g_value_set_double (dest, gst_value_get_double_range_min (src));
4623 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
4624 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
4625 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
4626 GValue temp = { 0 };
4628 /* list could be empty */
4629 if (gst_value_list_get_size (src) <= 0)
4632 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
4634 if (!gst_value_fixate (dest, &temp))
4635 gst_value_init_and_copy (dest, &temp);
4636 g_value_unset (&temp);
4637 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
4638 gboolean res = FALSE;
4641 len = gst_value_array_get_size (src);
4642 g_value_init (dest, GST_TYPE_ARRAY);
4643 for (n = 0; n < len; n++) {
4645 const GValue *orig_kid = gst_value_array_get_value (src, n);
4647 if (!gst_value_fixate (&kid, orig_kid))
4648 gst_value_init_and_copy (&kid, orig_kid);
4651 gst_value_array_append_value (dest, &kid);
4652 g_value_unset (&kid);
4656 g_value_unset (dest);
4670 /* helper functions */
4672 gst_value_init_fraction (GValue * value)
4674 value->data[0].v_int = 0;
4675 value->data[1].v_int = 1;
4679 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
4681 dest_value->data[0].v_int = src_value->data[0].v_int;
4682 dest_value->data[1].v_int = src_value->data[1].v_int;
4686 gst_value_collect_fraction (GValue * value, guint n_collect_values,
4687 GTypeCValue * collect_values, guint collect_flags)
4689 if (n_collect_values != 2)
4690 return g_strdup_printf ("not enough value locations for `%s' passed",
4691 G_VALUE_TYPE_NAME (value));
4692 if (collect_values[1].v_int == 0)
4693 return g_strdup_printf ("passed '0' as denominator for `%s'",
4694 G_VALUE_TYPE_NAME (value));
4695 if (collect_values[0].v_int < -G_MAXINT)
4698 ("passed value smaller than -G_MAXINT as numerator for `%s'",
4699 G_VALUE_TYPE_NAME (value));
4700 if (collect_values[1].v_int < -G_MAXINT)
4703 ("passed value smaller than -G_MAXINT as denominator for `%s'",
4704 G_VALUE_TYPE_NAME (value));
4706 gst_value_set_fraction (value,
4707 collect_values[0].v_int, collect_values[1].v_int);
4713 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
4714 GTypeCValue * collect_values, guint collect_flags)
4716 gint *numerator = collect_values[0].v_pointer;
4717 gint *denominator = collect_values[1].v_pointer;
4720 return g_strdup_printf ("numerator for `%s' passed as NULL",
4721 G_VALUE_TYPE_NAME (value));
4723 return g_strdup_printf ("denominator for `%s' passed as NULL",
4724 G_VALUE_TYPE_NAME (value));
4726 *numerator = value->data[0].v_int;
4727 *denominator = value->data[1].v_int;
4733 * gst_value_set_fraction:
4734 * @value: a GValue initialized to #GST_TYPE_FRACTION
4735 * @numerator: the numerator of the fraction
4736 * @denominator: the denominator of the fraction
4738 * Sets @value to the fraction specified by @numerator over @denominator.
4739 * The fraction gets reduced to the smallest numerator and denominator,
4740 * and if necessary the sign is moved to the numerator.
4743 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
4747 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
4748 g_return_if_fail (denominator != 0);
4749 g_return_if_fail (denominator >= -G_MAXINT);
4750 g_return_if_fail (numerator >= -G_MAXINT);
4752 /* normalize sign */
4753 if (denominator < 0) {
4754 numerator = -numerator;
4755 denominator = -denominator;
4758 /* check for reduction */
4759 gcd = gst_util_greatest_common_divisor (numerator, denominator);
4765 g_assert (denominator > 0);
4767 value->data[0].v_int = numerator;
4768 value->data[1].v_int = denominator;
4772 * gst_value_get_fraction_numerator:
4773 * @value: a GValue initialized to #GST_TYPE_FRACTION
4775 * Gets the numerator of the fraction specified by @value.
4777 * Returns: the numerator of the fraction.
4780 gst_value_get_fraction_numerator (const GValue * value)
4782 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
4784 return value->data[0].v_int;
4788 * gst_value_get_fraction_denominator:
4789 * @value: a GValue initialized to #GST_TYPE_FRACTION
4791 * Gets the denominator of the fraction specified by @value.
4793 * Returns: the denominator of the fraction.
4796 gst_value_get_fraction_denominator (const GValue * value)
4798 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
4800 return value->data[1].v_int;
4804 * gst_value_fraction_multiply:
4805 * @product: a GValue initialized to #GST_TYPE_FRACTION
4806 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
4807 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
4809 * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
4810 * @product to the product of the two fractions.
4812 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4815 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
4816 const GValue * factor2)
4818 gint n1, n2, d1, d2;
4821 g_return_val_if_fail (product != NULL, FALSE);
4822 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
4823 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
4825 n1 = factor1->data[0].v_int;
4826 n2 = factor2->data[0].v_int;
4827 d1 = factor1->data[1].v_int;
4828 d2 = factor2->data[1].v_int;
4830 if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
4833 gst_value_set_fraction (product, res_n, res_d);
4839 * gst_value_fraction_subtract:
4840 * @dest: a GValue initialized to #GST_TYPE_FRACTION
4841 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
4842 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
4844 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
4846 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4849 gst_value_fraction_subtract (GValue * dest,
4850 const GValue * minuend, const GValue * subtrahend)
4852 gint n1, n2, d1, d2;
4855 g_return_val_if_fail (dest != NULL, FALSE);
4856 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
4857 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
4859 n1 = minuend->data[0].v_int;
4860 n2 = subtrahend->data[0].v_int;
4861 d1 = minuend->data[1].v_int;
4862 d2 = subtrahend->data[1].v_int;
4864 if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
4866 gst_value_set_fraction (dest, res_n, res_d);
4872 gst_value_serialize_fraction (const GValue * value)
4874 gint32 numerator = value->data[0].v_int;
4875 gint32 denominator = value->data[1].v_int;
4876 gboolean positive = TRUE;
4878 /* get the sign and make components absolute */
4879 if (numerator < 0) {
4880 numerator = -numerator;
4881 positive = !positive;
4883 if (denominator < 0) {
4884 denominator = -denominator;
4885 positive = !positive;
4888 return g_strdup_printf ("%s%d/%d",
4889 positive ? "" : "-", numerator, denominator);
4893 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
4898 if (G_UNLIKELY (s == NULL))
4901 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
4904 if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
4905 if (s[num_chars] != 0)
4910 gst_value_set_fraction (dest, num, den);
4912 } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
4913 gst_value_set_fraction (dest, 1, G_MAXINT);
4915 } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
4916 if (s[num_chars] != 0)
4918 gst_value_set_fraction (dest, num, 1);
4920 } else if (g_ascii_strcasecmp (s, "min") == 0) {
4921 gst_value_set_fraction (dest, -G_MAXINT, 1);
4923 } else if (g_ascii_strcasecmp (s, "max") == 0) {
4924 gst_value_set_fraction (dest, G_MAXINT, 1);
4932 gst_value_transform_fraction_string (const GValue * src_value,
4933 GValue * dest_value)
4935 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
4939 gst_value_transform_string_fraction (const GValue * src_value,
4940 GValue * dest_value)
4942 if (!gst_value_deserialize_fraction (dest_value,
4943 src_value->data[0].v_pointer))
4944 /* If the deserialize fails, ensure we leave the fraction in a
4945 * valid, if incorrect, state */
4946 gst_value_set_fraction (dest_value, 0, 1);
4950 gst_value_transform_double_fraction (const GValue * src_value,
4951 GValue * dest_value)
4953 gdouble src = g_value_get_double (src_value);
4956 gst_util_double_to_fraction (src, &n, &d);
4957 gst_value_set_fraction (dest_value, n, d);
4961 gst_value_transform_float_fraction (const GValue * src_value,
4962 GValue * dest_value)
4964 gfloat src = g_value_get_float (src_value);
4967 gst_util_double_to_fraction (src, &n, &d);
4968 gst_value_set_fraction (dest_value, n, d);
4972 gst_value_transform_fraction_double (const GValue * src_value,
4973 GValue * dest_value)
4975 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
4976 ((double) src_value->data[1].v_int);
4980 gst_value_transform_fraction_float (const GValue * src_value,
4981 GValue * dest_value)
4983 dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
4984 ((float) src_value->data[1].v_int);
4988 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
4994 n1 = value1->data[0].v_int;
4995 n2 = value2->data[0].v_int;
4996 d1 = value1->data[1].v_int;
4997 d2 = value2->data[1].v_int;
4999 /* fractions are reduced when set, so we can quickly see if they're equal */
5000 if (n1 == n2 && d1 == d2)
5001 return GST_VALUE_EQUAL;
5003 if (d1 == 0 && d2 == 0)
5004 return GST_VALUE_UNORDERED;
5006 return GST_VALUE_GREATER_THAN;
5008 return GST_VALUE_LESS_THAN;
5010 ret = gst_util_fraction_compare (n1, d1, n2, d2);
5012 return GST_VALUE_LESS_THAN;
5014 return GST_VALUE_GREATER_THAN;
5016 /* Equality can't happen here because we check for that
5018 g_return_val_if_reached (GST_VALUE_UNORDERED);
5026 gst_value_compare_date (const GValue * value1, const GValue * value2)
5028 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
5029 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
5033 return GST_VALUE_EQUAL;
5035 if ((date1 == NULL || !g_date_valid (date1))
5036 && (date2 != NULL && g_date_valid (date2))) {
5037 return GST_VALUE_LESS_THAN;
5040 if ((date2 == NULL || !g_date_valid (date2))
5041 && (date1 != NULL && g_date_valid (date1))) {
5042 return GST_VALUE_GREATER_THAN;
5045 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
5046 || !g_date_valid (date2)) {
5047 return GST_VALUE_UNORDERED;
5050 j1 = g_date_get_julian (date1);
5051 j2 = g_date_get_julian (date2);
5054 return GST_VALUE_EQUAL;
5056 return GST_VALUE_LESS_THAN;
5058 return GST_VALUE_GREATER_THAN;
5062 gst_value_serialize_date (const GValue * val)
5064 const GDate *date = (const GDate *) g_value_get_boxed (val);
5066 if (date == NULL || !g_date_valid (date))
5067 return g_strdup ("9999-99-99");
5069 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
5070 g_date_get_month (date), g_date_get_day (date));
5074 gst_value_deserialize_date (GValue * dest, const gchar * s)
5076 guint year, month, day;
5078 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
5081 if (!g_date_valid_dmy (day, month, year))
5084 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
5093 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
5095 const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
5096 const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
5100 return GST_VALUE_EQUAL;
5102 if ((date1 == NULL) && (date2 != NULL)) {
5103 return GST_VALUE_LESS_THAN;
5105 if ((date2 == NULL) && (date1 != NULL)) {
5106 return GST_VALUE_LESS_THAN;
5109 ret = priv_gst_date_time_compare (date1, date2);
5112 return GST_VALUE_EQUAL;
5114 return GST_VALUE_LESS_THAN;
5116 return GST_VALUE_GREATER_THAN;
5120 gst_value_serialize_date_time (const GValue * val)
5122 GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
5124 gint tzhour, tzminute;
5127 return g_strdup ("null");
5129 offset = gst_date_time_get_time_zone_offset (date);
5131 tzhour = (gint) ABS (offset);
5132 tzminute = (gint) ((ABS (offset) - tzhour) * 60);
5134 return g_strdup_printf ("\"%04d-%02d-%02dT%02d:%02d:%02d.%06d"
5135 "%c%02d%02d\"", gst_date_time_get_year (date),
5136 gst_date_time_get_month (date), gst_date_time_get_day (date),
5137 gst_date_time_get_hour (date), gst_date_time_get_minute (date),
5138 gst_date_time_get_second (date), gst_date_time_get_microsecond (date),
5139 offset >= 0 ? '+' : '-', tzhour, tzminute);
5143 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
5145 gint year, month, day, hour, minute, second, usecond;
5148 gfloat tzoffset = 0;
5151 if (!s || strcmp (s, "null") == 0) {
5155 ret = sscanf (s, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%04d",
5156 &year, &month, &day, &hour, &minute, &second, &usecond, &signal, &offset);
5158 tzoffset = (offset / 100) + ((offset % 100) / 60.0);
5160 tzoffset = -tzoffset;
5164 g_value_take_boxed (dest, gst_date_time_new (tzoffset, year, month, day, hour,
5165 minute, second + (usecond / 1000000.0)));
5170 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
5172 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
5176 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
5178 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
5186 /* helper functions */
5188 gst_value_init_bitmask (GValue * value)
5190 value->data[0].v_uint64 = 0;
5194 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
5196 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5200 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
5201 GTypeCValue * collect_values, guint collect_flags)
5203 if (n_collect_values != 1)
5204 return g_strdup_printf ("not enough value locations for `%s' passed",
5205 G_VALUE_TYPE_NAME (value));
5207 gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
5213 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
5214 GTypeCValue * collect_values, guint collect_flags)
5216 guint64 *bitmask = collect_values[0].v_pointer;
5219 return g_strdup_printf ("value for `%s' passed as NULL",
5220 G_VALUE_TYPE_NAME (value));
5222 *bitmask = value->data[0].v_uint64;
5228 * gst_value_set_bitmask:
5229 * @value: a GValue initialized to #GST_TYPE_FRACTION
5230 * @bitmask: the bitmask
5232 * Sets @value to the bitmask specified by @bitmask.
5235 gst_value_set_bitmask (GValue * value, guint64 bitmask)
5237 g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
5239 value->data[0].v_uint64 = bitmask;
5243 * gst_value_get_bitmask:
5244 * @value: a GValue initialized to #GST_TYPE_FRACTION
5246 * Gets the bitmask specified by @value.
5248 * Returns: the bitmask.
5251 gst_value_get_bitmask (const GValue * value)
5253 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
5255 return value->data[0].v_uint64;
5259 gst_value_serialize_bitmask (const GValue * value)
5261 guint64 bitmask = value->data[0].v_uint64;
5263 return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
5267 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
5269 gchar *endptr = NULL;
5272 if (G_UNLIKELY (s == NULL))
5275 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
5278 val = g_ascii_strtoull (s, &endptr, 16);
5279 if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
5281 if (val == 0 && endptr == s)
5284 gst_value_set_bitmask (dest, val);
5290 gst_value_transform_bitmask_string (const GValue * src_value,
5291 GValue * dest_value)
5293 dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
5297 gst_value_transform_string_bitmask (const GValue * src_value,
5298 GValue * dest_value)
5300 if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
5301 gst_value_set_bitmask (dest_value, 0);
5305 gst_value_transform_uint64_bitmask (const GValue * src_value,
5306 GValue * dest_value)
5308 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5312 gst_value_transform_bitmask_uint64 (const GValue * src_value,
5313 GValue * dest_value)
5315 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5319 gst_value_intersect_bitmask_bitmask (GValue * dest, const GValue * src1,
5320 const GValue * src2)
5324 s1 = gst_value_get_bitmask (src1);
5325 s2 = gst_value_get_bitmask (src2);
5328 g_value_init (dest, GST_TYPE_BITMASK);
5329 gst_value_set_bitmask (dest, s1 & s2);
5336 gst_value_union_bitmask_bitmask (GValue * dest, const GValue * src1,
5337 const GValue * src2)
5341 s1 = gst_value_get_bitmask (src1);
5342 s2 = gst_value_get_bitmask (src2);
5344 g_value_init (dest, GST_TYPE_BITMASK);
5345 gst_value_set_bitmask (dest, s1 | s2);
5351 gst_value_subtract_bitmask_bitmask (GValue * dest,
5352 const GValue * minuend, const GValue * subtrahend)
5356 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (minuend), FALSE);
5357 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (subtrahend), FALSE);
5359 m = minuend->data[0].v_uint64;
5360 s = subtrahend->data[0].v_uint64;
5364 g_value_init (dest, GST_TYPE_BITMASK);
5365 gst_value_set_bitmask (dest, r);
5371 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
5375 v1 = value1->data[0].v_uint64;
5376 v2 = value2->data[0].v_uint64;
5379 return GST_VALUE_EQUAL;
5381 return GST_VALUE_UNORDERED;
5385 gst_value_transform_object_string (const GValue * src_value,
5386 GValue * dest_value)
5391 obj = g_value_get_object (src_value);
5394 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
5395 GST_OBJECT_NAME (obj));
5397 str = g_strdup ("NULL");
5400 dest_value->data[0].v_pointer = str;
5403 static GTypeInfo _info = {
5416 static GTypeFundamentalInfo _finfo = {
5420 #define FUNC_VALUE_GET_TYPE(type, name) \
5421 GType gst_ ## type ## _get_type (void) \
5423 static volatile GType gst_ ## type ## _type = 0; \
5425 if (g_once_init_enter (&gst_ ## type ## _type)) { \
5427 _info.value_table = & _gst_ ## type ## _value_table; \
5428 _type = g_type_register_fundamental ( \
5429 g_type_fundamental_next (), \
5430 name, &_info, &_finfo, 0); \
5431 g_once_init_leave(&gst_ ## type ## _type, _type); \
5434 return gst_ ## type ## _type; \
5437 static const GTypeValueTable _gst_int_range_value_table = {
5438 gst_value_init_int_range,
5439 gst_value_free_int_range,
5440 gst_value_copy_int_range,
5443 gst_value_collect_int_range,
5445 gst_value_lcopy_int_range
5448 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
5450 static const GTypeValueTable _gst_int64_range_value_table = {
5451 gst_value_init_int64_range,
5452 gst_value_free_int64_range,
5453 gst_value_copy_int64_range,
5456 gst_value_collect_int64_range,
5458 gst_value_lcopy_int64_range
5461 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
5463 static const GTypeValueTable _gst_double_range_value_table = {
5464 gst_value_init_double_range,
5466 gst_value_copy_double_range,
5469 gst_value_collect_double_range,
5471 gst_value_lcopy_double_range
5474 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
5476 static const GTypeValueTable _gst_fraction_range_value_table = {
5477 gst_value_init_fraction_range,
5478 gst_value_free_fraction_range,
5479 gst_value_copy_fraction_range,
5482 gst_value_collect_fraction_range,
5484 gst_value_lcopy_fraction_range
5487 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
5489 static const GTypeValueTable _gst_value_list_value_table = {
5490 gst_value_init_list_or_array,
5491 gst_value_free_list_or_array,
5492 gst_value_copy_list_or_array,
5493 gst_value_list_or_array_peek_pointer,
5495 gst_value_collect_list_or_array,
5497 gst_value_lcopy_list_or_array
5500 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
5502 static const GTypeValueTable _gst_value_array_value_table = {
5503 gst_value_init_list_or_array,
5504 gst_value_free_list_or_array,
5505 gst_value_copy_list_or_array,
5506 gst_value_list_or_array_peek_pointer,
5508 gst_value_collect_list_or_array,
5510 gst_value_lcopy_list_or_array
5513 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
5515 static const GTypeValueTable _gst_fraction_value_table = {
5516 gst_value_init_fraction,
5518 gst_value_copy_fraction,
5521 gst_value_collect_fraction,
5523 gst_value_lcopy_fraction
5526 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
5528 G_DEFINE_BOXED_TYPE (GstDateTime, gst_date_time,
5529 (GBoxedCopyFunc) gst_date_time_ref, (GBoxedFreeFunc) gst_date_time_unref);
5531 static const GTypeValueTable _gst_bitmask_value_table = {
5532 gst_value_init_bitmask,
5534 gst_value_copy_bitmask,
5537 gst_value_collect_bitmask,
5539 gst_value_lcopy_bitmask
5542 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
5546 _priv_gst_value_initialize (void)
5548 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
5549 gst_value_hash = g_hash_table_new (NULL, NULL);
5550 gst_value_union_funcs = g_array_new (FALSE, FALSE,
5551 sizeof (GstValueUnionInfo));
5552 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
5553 sizeof (GstValueIntersectInfo));
5554 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
5555 sizeof (GstValueSubtractInfo));
5558 static GstValueTable gst_value = {
5560 gst_value_compare_int_range,
5561 gst_value_serialize_int_range,
5562 gst_value_deserialize_int_range,
5565 gst_value.type = gst_int_range_get_type ();
5566 gst_value_register (&gst_value);
5570 static GstValueTable gst_value = {
5572 gst_value_compare_int64_range,
5573 gst_value_serialize_int64_range,
5574 gst_value_deserialize_int64_range,
5577 gst_value.type = gst_int64_range_get_type ();
5578 gst_value_register (&gst_value);
5582 static GstValueTable gst_value = {
5584 gst_value_compare_double_range,
5585 gst_value_serialize_double_range,
5586 gst_value_deserialize_double_range,
5589 gst_value.type = gst_double_range_get_type ();
5590 gst_value_register (&gst_value);
5594 static GstValueTable gst_value = {
5596 gst_value_compare_fraction_range,
5597 gst_value_serialize_fraction_range,
5598 gst_value_deserialize_fraction_range,
5601 gst_value.type = gst_fraction_range_get_type ();
5602 gst_value_register (&gst_value);
5606 static GstValueTable gst_value = {
5608 gst_value_compare_list,
5609 gst_value_serialize_list,
5610 gst_value_deserialize_list,
5613 gst_value.type = gst_value_list_get_type ();
5614 gst_value_register (&gst_value);
5618 static GstValueTable gst_value = {
5620 gst_value_compare_array,
5621 gst_value_serialize_array,
5622 gst_value_deserialize_array,
5625 gst_value.type = gst_value_array_get_type ();
5626 gst_value_register (&gst_value);
5631 static const GTypeValueTable value_table = {
5632 gst_value_init_buffer,
5634 gst_value_copy_buffer,
5637 NULL, /*gst_value_collect_buffer, */
5639 NULL /*gst_value_lcopy_buffer */
5642 static GstValueTable gst_value = {
5644 gst_value_compare_buffer,
5645 gst_value_serialize_buffer,
5646 gst_value_deserialize_buffer,
5649 gst_value.type = GST_TYPE_BUFFER;
5650 gst_value_register (&gst_value);
5653 static GstValueTable gst_value = {
5655 gst_value_compare_fraction,
5656 gst_value_serialize_fraction,
5657 gst_value_deserialize_fraction,
5660 gst_value.type = gst_fraction_get_type ();
5661 gst_value_register (&gst_value);
5664 static GstValueTable gst_value = {
5667 gst_value_serialize_caps,
5668 gst_value_deserialize_caps,
5671 gst_value.type = GST_TYPE_CAPS;
5672 gst_value_register (&gst_value);
5675 static GstValueTable gst_value = {
5678 gst_value_serialize_structure,
5679 gst_value_deserialize_structure,
5682 gst_value.type = GST_TYPE_STRUCTURE;
5683 gst_value_register (&gst_value);
5686 static GstValueTable gst_value = {
5688 gst_value_compare_date,
5689 gst_value_serialize_date,
5690 gst_value_deserialize_date,
5693 gst_value.type = G_TYPE_DATE;
5694 gst_value_register (&gst_value);
5697 static GstValueTable gst_value = {
5699 gst_value_compare_date_time,
5700 gst_value_serialize_date_time,
5701 gst_value_deserialize_date_time,
5704 gst_value.type = gst_date_time_get_type ();
5705 gst_value_register (&gst_value);
5709 static GstValueTable gst_value = {
5711 gst_value_compare_bitmask,
5712 gst_value_serialize_bitmask,
5713 gst_value_deserialize_bitmask,
5716 gst_value.type = gst_bitmask_get_type ();
5717 gst_value_register (&gst_value);
5720 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
5721 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
5723 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
5724 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
5725 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
5727 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
5729 REGISTER_SERIALIZATION (G_TYPE_INT, int);
5731 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
5732 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
5734 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
5735 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
5736 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
5738 REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
5740 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
5741 gst_value_transform_int_range_string);
5742 g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
5743 gst_value_transform_int64_range_string);
5744 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
5745 gst_value_transform_double_range_string);
5746 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
5747 gst_value_transform_fraction_range_string);
5748 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
5749 gst_value_transform_list_string);
5750 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
5751 gst_value_transform_array_string);
5752 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
5753 gst_value_transform_fraction_string);
5754 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
5755 gst_value_transform_string_fraction);
5756 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
5757 gst_value_transform_fraction_double);
5758 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
5759 gst_value_transform_fraction_float);
5760 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
5761 gst_value_transform_double_fraction);
5762 g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
5763 gst_value_transform_float_fraction);
5764 g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
5765 gst_value_transform_date_string);
5766 g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
5767 gst_value_transform_string_date);
5768 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
5769 gst_value_transform_object_string);
5770 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
5771 gst_value_transform_bitmask_uint64);
5772 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
5773 gst_value_transform_bitmask_string);
5774 g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
5775 gst_value_transform_uint64_bitmask);
5776 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
5777 gst_value_transform_string_bitmask);
5779 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5780 gst_value_intersect_int_int_range);
5781 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5782 gst_value_intersect_int_range_int_range);
5783 gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5784 gst_value_intersect_int64_int64_range);
5785 gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5786 gst_value_intersect_int64_range_int64_range);
5787 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5788 gst_value_intersect_double_double_range);
5789 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
5790 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
5791 gst_value_register_intersect_func (GST_TYPE_ARRAY,
5792 GST_TYPE_ARRAY, gst_value_intersect_array);
5793 gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5794 gst_value_intersect_fraction_fraction_range);
5795 gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
5796 GST_TYPE_FRACTION_RANGE,
5797 gst_value_intersect_fraction_range_fraction_range);
5798 gst_value_register_intersect_func (GST_TYPE_BITMASK,
5799 GST_TYPE_BITMASK, gst_value_intersect_bitmask_bitmask);
5801 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5802 gst_value_subtract_int_int_range);
5803 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
5804 gst_value_subtract_int_range_int);
5805 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5806 gst_value_subtract_int_range_int_range);
5807 gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5808 gst_value_subtract_int64_int64_range);
5809 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
5810 gst_value_subtract_int64_range_int64);
5811 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5812 gst_value_subtract_int64_range_int64_range);
5813 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5814 gst_value_subtract_double_double_range);
5815 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
5816 gst_value_subtract_double_range_double);
5817 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
5818 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
5819 gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5820 gst_value_subtract_fraction_fraction_range);
5821 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
5822 gst_value_subtract_fraction_range_fraction);
5823 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
5824 GST_TYPE_FRACTION_RANGE,
5825 gst_value_subtract_fraction_range_fraction_range);
5826 gst_value_register_subtract_func (GST_TYPE_BITMASK,
5827 GST_TYPE_BITMASK, gst_value_subtract_bitmask_bitmask);
5829 /* see bug #317246, #64994, #65041 */
5831 volatile GType date_type = G_TYPE_DATE;
5833 g_type_name (date_type);
5836 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5837 gst_value_union_int_int_range);
5838 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5839 gst_value_union_int_range_int_range);
5840 gst_value_register_union_func (GST_TYPE_BITMASK,
5841 GST_TYPE_BITMASK, gst_value_union_bitmask_bitmask);
5844 /* Implement these if needed */
5845 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5846 gst_value_union_fraction_fraction_range);
5847 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
5848 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);