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 (GST_VALUE_HOLDS_LIST (value)) {
303 if (VALUE_LIST_SIZE (value) == 0)
305 return gst_value_list_or_array_get_basic_type (VALUE_LIST_GET_VALUE (value,
308 if (GST_VALUE_HOLDS_ARRAY (value)) {
309 const GArray *array = (const GArray *) value->data[0].v_pointer;
312 return gst_value_list_or_array_get_basic_type (&g_array_index (array,
315 *type = G_VALUE_TYPE (value);
319 #define IS_RANGE_COMPAT(type1,type2,t1,t2) \
320 (((t1) == (type1) && (t2) == (type2)) || ((t2) == (type1) && (t1) == (type2)))
323 gst_value_list_or_array_are_compatible (const GValue * value1,
324 const GValue * value2)
326 GType basic_type1, basic_type2;
328 /* empty or same type is OK */
329 if (!gst_value_list_or_array_get_basic_type (value1, &basic_type1) ||
330 !gst_value_list_or_array_get_basic_type (value2, &basic_type2) ||
331 basic_type1 == basic_type2)
334 /* ranges are distinct types for each bound type... */
335 if (IS_RANGE_COMPAT (G_TYPE_INT, GST_TYPE_INT_RANGE, basic_type1,
338 if (IS_RANGE_COMPAT (G_TYPE_INT64, GST_TYPE_INT64_RANGE, basic_type1,
341 if (IS_RANGE_COMPAT (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE, basic_type1,
344 if (IS_RANGE_COMPAT (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, basic_type1,
352 * gst_value_list_append_value:
353 * @value: a #GValue of type #GST_TYPE_LIST
354 * @append_value: the value to append
356 * Appends @append_value to the GstValueList in @value.
359 gst_value_list_append_value (GValue * value, const GValue * append_value)
363 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
364 g_return_if_fail (G_IS_VALUE (append_value));
365 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
368 gst_value_init_and_copy (&val, append_value);
369 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
373 * gst_value_list_prepend_value:
374 * @value: a #GValue of type #GST_TYPE_LIST
375 * @prepend_value: the value to prepend
377 * Prepends @prepend_value to the GstValueList in @value.
380 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
384 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
385 g_return_if_fail (G_IS_VALUE (prepend_value));
386 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
389 gst_value_init_and_copy (&val, prepend_value);
390 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
394 * gst_value_list_concat:
395 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
399 * Concatenates copies of @value1 and @value2 into a list. Values that are not
400 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
401 * @dest will be initialized to the type #GST_TYPE_LIST.
404 gst_value_list_concat (GValue * dest, const GValue * value1,
405 const GValue * value2)
407 guint i, value1_length, value2_length;
410 g_return_if_fail (dest != NULL);
411 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
412 g_return_if_fail (G_IS_VALUE (value1));
413 g_return_if_fail (G_IS_VALUE (value2));
414 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
417 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
419 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
420 g_value_init (dest, GST_TYPE_LIST);
421 array = (GArray *) dest->data[0].v_pointer;
422 g_array_set_size (array, value1_length + value2_length);
424 if (GST_VALUE_HOLDS_LIST (value1)) {
425 for (i = 0; i < value1_length; i++) {
426 gst_value_init_and_copy (&g_array_index (array, GValue, i),
427 VALUE_LIST_GET_VALUE (value1, i));
430 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
433 if (GST_VALUE_HOLDS_LIST (value2)) {
434 for (i = 0; i < value2_length; i++) {
435 gst_value_init_and_copy (&g_array_index (array, GValue,
436 i + value1_length), VALUE_LIST_GET_VALUE (value2, i));
439 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
445 * gst_value_list_merge:
446 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
450 * Merges copies of @value1 and @value2. Values that are not
451 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
453 * The result will be put into @dest and will either be a list that will not
454 * contain any duplicates, or a non-list type (if @value1 and @value2
460 gst_value_list_merge (GValue * dest, const GValue * value1,
461 const GValue * value2)
463 guint i, j, k, value1_length, value2_length, skipped;
468 g_return_if_fail (dest != NULL);
469 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
470 g_return_if_fail (G_IS_VALUE (value1));
471 g_return_if_fail (G_IS_VALUE (value2));
472 g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
475 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
477 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
478 g_value_init (dest, GST_TYPE_LIST);
479 array = (GArray *) dest->data[0].v_pointer;
480 g_array_set_size (array, value1_length + value2_length);
482 if (GST_VALUE_HOLDS_LIST (value1)) {
483 for (i = 0; i < value1_length; i++) {
484 gst_value_init_and_copy (&g_array_index (array, GValue, i),
485 VALUE_LIST_GET_VALUE (value1, i));
488 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
493 if (GST_VALUE_HOLDS_LIST (value2)) {
494 for (i = 0; i < value2_length; i++) {
496 src = VALUE_LIST_GET_VALUE (value2, i);
497 for (k = 0; k < value1_length; k++) {
498 if (gst_value_compare (&g_array_index (array, GValue, k),
499 src) == GST_VALUE_EQUAL) {
506 gst_value_init_and_copy (&g_array_index (array, GValue, j), src);
512 for (k = 0; k < value1_length; k++) {
513 if (gst_value_compare (&g_array_index (array, GValue, k),
514 value2) == GST_VALUE_EQUAL) {
521 gst_value_init_and_copy (&g_array_index (array, GValue, j), value2);
525 guint new_size = value1_length + (value2_length - skipped);
529 g_array_set_size (array, new_size);
533 /* size is 1, take single value in list and make it new dest */
534 single_dest = g_array_index (array, GValue, 0);
536 /* clean up old value allocations: must set array size to 0, because
537 * allocated values are not inited meaning g_value_unset() will not
539 g_array_set_size (array, 0);
540 g_value_unset (dest);
542 /* the single value is our new result */
549 * gst_value_list_get_size:
550 * @value: a #GValue of type #GST_TYPE_LIST
552 * Gets the number of values contained in @value.
554 * Returns: the number of values
557 gst_value_list_get_size (const GValue * value)
559 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
561 return ((GArray *) value->data[0].v_pointer)->len;
565 * gst_value_list_get_value:
566 * @value: a #GValue of type #GST_TYPE_LIST
567 * @index: index of value to get from the list
569 * Gets the value that is a member of the list contained in @value and
570 * has the index @index.
572 * Returns: (transfer none): the value at the given index
575 gst_value_list_get_value (const GValue * value, guint index)
577 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
578 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
580 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
585 * gst_value_array_append_value:
586 * @value: a #GValue of type #GST_TYPE_ARRAY
587 * @append_value: the value to append
589 * Appends @append_value to the GstValueArray in @value.
592 gst_value_array_append_value (GValue * value, const GValue * append_value)
596 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
597 g_return_if_fail (G_IS_VALUE (append_value));
598 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
601 gst_value_init_and_copy (&val, append_value);
602 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
606 * gst_value_array_prepend_value:
607 * @value: a #GValue of type #GST_TYPE_ARRAY
608 * @prepend_value: the value to prepend
610 * Prepends @prepend_value to the GstValueArray in @value.
613 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
617 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
618 g_return_if_fail (G_IS_VALUE (prepend_value));
619 g_return_if_fail (gst_value_list_or_array_are_compatible (value,
622 gst_value_init_and_copy (&val, prepend_value);
623 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
627 * gst_value_array_get_size:
628 * @value: a #GValue of type #GST_TYPE_ARRAY
630 * Gets the number of values contained in @value.
632 * Returns: the number of values
635 gst_value_array_get_size (const GValue * value)
637 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
639 return ((GArray *) value->data[0].v_pointer)->len;
643 * gst_value_array_get_value:
644 * @value: a #GValue of type #GST_TYPE_ARRAY
645 * @index: index of value to get from the array
647 * Gets the value that is a member of the array contained in @value and
648 * has the index @index.
650 * Returns: (transfer none): the value at the given index
653 gst_value_array_get_value (const GValue * value, guint index)
655 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
656 g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
658 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
663 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
665 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
669 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
671 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
674 /* Do an unordered compare of the contents of a list */
676 gst_value_compare_list (const GValue * value1, const GValue * value2)
679 GArray *array1 = value1->data[0].v_pointer;
680 GArray *array2 = value2->data[0].v_pointer;
685 GstValueCompareFunc compare;
687 /* get length and do initial length check. */
689 if (len != array2->len)
690 return GST_VALUE_UNORDERED;
692 /* place to mark removed value indices of array2 */
693 removed = g_newa (guint8, len);
694 memset (removed, 0, len);
697 /* loop over array1, all items should be in array2. When we find an
698 * item in array2, remove it from array2 by marking it as removed */
699 for (i = 0; i < len; i++) {
700 v1 = &g_array_index (array1, GValue, i);
701 if ((compare = gst_value_get_compare_func (v1))) {
702 for (j = 0; j < len; j++) {
703 /* item is removed, we can skip it */
706 v2 = &g_array_index (array2, GValue, j);
707 if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
708 /* mark item as removed now that we found it in array2 and
709 * decrement the number of remaining items in array2. */
715 /* item in array1 and not in array2, UNORDERED */
717 return GST_VALUE_UNORDERED;
719 return GST_VALUE_UNORDERED;
721 /* if not all items were removed, array2 contained something not in array1 */
723 return GST_VALUE_UNORDERED;
725 /* arrays are equal */
726 return GST_VALUE_EQUAL;
729 /* Perform an ordered comparison of the contents of an array */
731 gst_value_compare_array (const GValue * value1, const GValue * value2)
734 GArray *array1 = value1->data[0].v_pointer;
735 GArray *array2 = value2->data[0].v_pointer;
736 guint len = array1->len;
740 if (len != array2->len)
741 return GST_VALUE_UNORDERED;
743 for (i = 0; i < len; i++) {
744 v1 = &g_array_index (array1, GValue, i);
745 v2 = &g_array_index (array2, GValue, i);
746 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
747 return GST_VALUE_UNORDERED;
750 return GST_VALUE_EQUAL;
754 gst_value_serialize_list (const GValue * value)
756 return gst_value_serialize_any_list (value, "{ ", " }");
760 gst_value_deserialize_list (GValue * dest, const gchar * s)
762 g_warning ("gst_value_deserialize_list: unimplemented");
767 gst_value_serialize_array (const GValue * value)
769 return gst_value_serialize_any_list (value, "< ", " >");
773 gst_value_deserialize_array (GValue * dest, const gchar * s)
775 g_warning ("gst_value_deserialize_array: unimplemented");
782 * Values in the range are defined as any value greater or equal
783 * to min*step, AND lesser or equal to max*step.
784 * For step == 1, this falls back to the traditional range semantics.
787 #define INT_RANGE_MIN(v) (((gint *)((v)->data[0].v_pointer))[0])
788 #define INT_RANGE_MAX(v) (((gint *)((v)->data[0].v_pointer))[1])
789 #define INT_RANGE_STEP(v) (((gint *)((v)->data[0].v_pointer))[2])
792 gst_value_init_int_range (GValue * value)
794 gint *vals = g_slice_alloc0 (3 * sizeof (gint));
795 value->data[0].v_pointer = vals;
796 INT_RANGE_MIN (value) = 0;
797 INT_RANGE_MAX (value) = 0;
798 INT_RANGE_STEP (value) = 1;
802 gst_value_free_int_range (GValue * value)
804 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
805 g_slice_free1 (3 * sizeof (gint), value->data[0].v_pointer);
806 value->data[0].v_pointer = NULL;
810 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
812 gint *vals = (gint *) dest_value->data[0].v_pointer;
813 gint *src_vals = (gint *) src_value->data[0].v_pointer;
816 gst_value_init_int_range (dest_value);
818 if (src_vals != NULL) {
819 INT_RANGE_MIN (dest_value) = INT_RANGE_MIN (src_value);
820 INT_RANGE_MAX (dest_value) = INT_RANGE_MAX (src_value);
821 INT_RANGE_STEP (dest_value) = INT_RANGE_STEP (src_value);
826 gst_value_collect_int_range (GValue * value, guint n_collect_values,
827 GTypeCValue * collect_values, guint collect_flags)
829 gint *vals = value->data[0].v_pointer;
831 if (n_collect_values != 2)
832 return g_strdup_printf ("not enough value locations for `%s' passed",
833 G_VALUE_TYPE_NAME (value));
834 if (collect_values[0].v_int >= collect_values[1].v_int)
835 return g_strdup_printf ("range start is not smaller than end for `%s'",
836 G_VALUE_TYPE_NAME (value));
839 gst_value_init_int_range (value);
842 gst_value_set_int_range_step (value, collect_values[0].v_int,
843 collect_values[1].v_int, 1);
849 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
850 GTypeCValue * collect_values, guint collect_flags)
852 guint32 *int_range_start = collect_values[0].v_pointer;
853 guint32 *int_range_end = collect_values[1].v_pointer;
854 guint32 *int_range_step = collect_values[2].v_pointer;
855 gint *vals = (gint *) value->data[0].v_pointer;
857 if (!int_range_start)
858 return g_strdup_printf ("start value location for `%s' passed as NULL",
859 G_VALUE_TYPE_NAME (value));
861 return g_strdup_printf ("end value location for `%s' passed as NULL",
862 G_VALUE_TYPE_NAME (value));
864 return g_strdup_printf ("step value location for `%s' passed as NULL",
865 G_VALUE_TYPE_NAME (value));
867 if (G_UNLIKELY (vals == NULL)) {
868 return g_strdup_printf ("Uninitialised `%s' passed",
869 G_VALUE_TYPE_NAME (value));
872 *int_range_start = INT_RANGE_MIN (value);
873 *int_range_end = INT_RANGE_MAX (value);
874 *int_range_step = INT_RANGE_STEP (value);
880 * gst_value_set_int_range_step:
881 * @value: a GValue initialized to GST_TYPE_INT_RANGE
882 * @start: the start of the range
883 * @end: the end of the range
884 * @step: the step of the range
886 * Sets @value to the range specified by @start, @end and @step.
889 gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
891 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
892 g_return_if_fail (start < end);
893 g_return_if_fail (step > 0);
894 g_return_if_fail (start % step == 0);
895 g_return_if_fail (end % step == 0);
897 INT_RANGE_MIN (value) = start / step;
898 INT_RANGE_MAX (value) = end / step;
899 INT_RANGE_STEP (value) = step;
903 * gst_value_set_int_range:
904 * @value: a GValue initialized to GST_TYPE_INT_RANGE
905 * @start: the start of the range
906 * @end: the end of the range
908 * Sets @value to the range specified by @start and @end.
911 gst_value_set_int_range (GValue * value, gint start, gint end)
913 gst_value_set_int_range_step (value, start, end, 1);
917 * gst_value_get_int_range_min:
918 * @value: a GValue initialized to GST_TYPE_INT_RANGE
920 * Gets the minimum of the range specified by @value.
922 * Returns: the minimum of the range
925 gst_value_get_int_range_min (const GValue * value)
927 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
929 return INT_RANGE_MIN (value) * INT_RANGE_STEP (value);
933 * gst_value_get_int_range_max:
934 * @value: a GValue initialized to GST_TYPE_INT_RANGE
936 * Gets the maximum of the range specified by @value.
938 * Returns: the maxumum of the range
941 gst_value_get_int_range_max (const GValue * value)
943 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
945 return INT_RANGE_MAX (value) * INT_RANGE_STEP (value);
949 * gst_value_get_int_range_step:
950 * @value: a GValue initialized to GST_TYPE_INT_RANGE
952 * Gets the step of the range specified by @value.
954 * Returns: the step of the range
957 gst_value_get_int_range_step (const GValue * value)
959 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
961 return INT_RANGE_STEP (value);
965 gst_value_transform_int_range_string (const GValue * src_value,
968 if (INT_RANGE_STEP (src_value) == 1)
969 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
970 INT_RANGE_MIN (src_value), INT_RANGE_MAX (src_value));
972 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d,%d]",
973 INT_RANGE_MIN (src_value) * INT_RANGE_STEP (src_value),
974 INT_RANGE_MAX (src_value) * INT_RANGE_STEP (src_value),
975 INT_RANGE_STEP (src_value));
979 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
981 /* calculate the number of values in each range */
982 gint n1 = INT_RANGE_MAX (value1) - INT_RANGE_MIN (value1) + 1;
983 gint n2 = INT_RANGE_MAX (value2) - INT_RANGE_MIN (value2) + 1;
985 /* they must be equal */
987 return GST_VALUE_UNORDERED;
989 /* if empty, equal */
991 return GST_VALUE_EQUAL;
993 /* if more than one value, then it is only equal if the step is equal
994 and bounds lie on the same value */
996 if (INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2) &&
997 INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2) &&
998 INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2)) {
999 return GST_VALUE_EQUAL;
1001 return GST_VALUE_UNORDERED;
1003 /* if just one, only if the value is equal */
1004 if (INT_RANGE_MIN (value1) == INT_RANGE_MIN (value2))
1005 return GST_VALUE_EQUAL;
1006 return GST_VALUE_UNORDERED;
1011 gst_value_serialize_int_range (const GValue * value)
1013 if (INT_RANGE_STEP (value) == 1)
1014 return g_strdup_printf ("[ %d, %d ]", INT_RANGE_MIN (value),
1015 INT_RANGE_MAX (value));
1017 return g_strdup_printf ("[ %d, %d, %d ]",
1018 INT_RANGE_MIN (value) * INT_RANGE_STEP (value),
1019 INT_RANGE_MAX (value) * INT_RANGE_STEP (value), INT_RANGE_STEP (value));
1023 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1025 g_warning ("unimplemented");
1032 * Values in the range are defined as any value greater or equal
1033 * to min*step, AND lesser or equal to max*step.
1034 * For step == 1, this falls back to the traditional range semantics.
1037 #define INT64_RANGE_MIN(v) (((gint64 *)((v)->data[0].v_pointer))[0])
1038 #define INT64_RANGE_MAX(v) (((gint64 *)((v)->data[0].v_pointer))[1])
1039 #define INT64_RANGE_STEP(v) (((gint64 *)((v)->data[0].v_pointer))[2])
1042 gst_value_init_int64_range (GValue * value)
1044 gint64 *vals = g_slice_alloc0 (3 * sizeof (gint64));
1045 value->data[0].v_pointer = vals;
1046 INT64_RANGE_MIN (value) = 0;
1047 INT64_RANGE_MAX (value) = 0;
1048 INT64_RANGE_STEP (value) = 1;
1052 gst_value_free_int64_range (GValue * value)
1054 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1055 g_slice_free1 (3 * sizeof (gint64), value->data[0].v_pointer);
1056 value->data[0].v_pointer = NULL;
1060 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1062 gint64 *vals = (gint64 *) dest_value->data[0].v_pointer;
1063 gint64 *src_vals = (gint64 *) src_value->data[0].v_pointer;
1066 gst_value_init_int64_range (dest_value);
1067 vals = (gint64 *) dest_value->data[0].v_pointer;
1069 if (src_vals != NULL) {
1070 INT64_RANGE_MIN (dest_value) = INT64_RANGE_MIN (src_value);
1071 INT64_RANGE_MAX (dest_value) = INT64_RANGE_MAX (src_value);
1072 INT64_RANGE_STEP (dest_value) = INT64_RANGE_STEP (src_value);
1077 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1078 GTypeCValue * collect_values, guint collect_flags)
1080 gint64 *vals = value->data[0].v_pointer;
1082 if (n_collect_values != 2)
1083 return g_strdup_printf ("not enough value locations for `%s' passed",
1084 G_VALUE_TYPE_NAME (value));
1085 if (collect_values[0].v_int64 >= collect_values[1].v_int64)
1086 return g_strdup_printf ("range start is not smaller than end for `%s'",
1087 G_VALUE_TYPE_NAME (value));
1090 gst_value_init_int64_range (value);
1093 gst_value_set_int64_range_step (value, collect_values[0].v_int64,
1094 collect_values[1].v_int64, 1);
1100 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1101 GTypeCValue * collect_values, guint collect_flags)
1103 guint64 *int_range_start = collect_values[0].v_pointer;
1104 guint64 *int_range_end = collect_values[1].v_pointer;
1105 guint64 *int_range_step = collect_values[2].v_pointer;
1106 gint64 *vals = (gint64 *) value->data[0].v_pointer;
1108 if (!int_range_start)
1109 return g_strdup_printf ("start value location for `%s' passed as NULL",
1110 G_VALUE_TYPE_NAME (value));
1112 return g_strdup_printf ("end value location for `%s' passed as NULL",
1113 G_VALUE_TYPE_NAME (value));
1114 if (!int_range_step)
1115 return g_strdup_printf ("step value location for `%s' passed as NULL",
1116 G_VALUE_TYPE_NAME (value));
1118 if (G_UNLIKELY (vals == NULL)) {
1119 return g_strdup_printf ("Uninitialised `%s' passed",
1120 G_VALUE_TYPE_NAME (value));
1123 *int_range_start = INT64_RANGE_MIN (value);
1124 *int_range_end = INT64_RANGE_MAX (value);
1125 *int_range_step = INT64_RANGE_STEP (value);
1131 * gst_value_set_int64_range_step:
1132 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1133 * @start: the start of the range
1134 * @end: the end of the range
1135 * @step: the step of the range
1137 * Sets @value to the range specified by @start, @end and @step.
1142 gst_value_set_int64_range_step (GValue * value, gint64 start, gint64 end,
1145 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1146 g_return_if_fail (start < end);
1147 g_return_if_fail (step > 0);
1148 g_return_if_fail (start % step == 0);
1149 g_return_if_fail (end % step == 0);
1151 INT64_RANGE_MIN (value) = start / step;
1152 INT64_RANGE_MAX (value) = end / step;
1153 INT64_RANGE_STEP (value) = step;
1157 * gst_value_set_int64_range:
1158 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1159 * @start: the start of the range
1160 * @end: the end of the range
1162 * Sets @value to the range specified by @start and @end.
1167 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1169 gst_value_set_int64_range_step (value, start, end, 1);
1173 * gst_value_get_int64_range_min:
1174 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1176 * Gets the minimum of the range specified by @value.
1178 * Returns: the minimum of the range
1183 gst_value_get_int64_range_min (const GValue * value)
1185 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1187 return INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value);
1191 * gst_value_get_int64_range_max:
1192 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1194 * Gets the maximum of the range specified by @value.
1196 * Returns: the maxumum of the range
1201 gst_value_get_int64_range_max (const GValue * value)
1203 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1205 return INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value);
1209 * gst_value_get_int64_range_step:
1210 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1212 * Gets the step of the range specified by @value.
1214 * Returns: the step of the range
1219 gst_value_get_int64_range_step (const GValue * value)
1221 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1223 return INT64_RANGE_STEP (value);
1227 gst_value_transform_int64_range_string (const GValue * src_value,
1228 GValue * dest_value)
1230 if (INT64_RANGE_STEP (src_value) == 1)
1231 dest_value->data[0].v_pointer =
1232 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1233 INT64_RANGE_MIN (src_value), INT64_RANGE_MAX (src_value));
1235 dest_value->data[0].v_pointer =
1236 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT
1237 ",%" G_GINT64_FORMAT "]",
1238 INT64_RANGE_MIN (src_value) * INT64_RANGE_STEP (src_value),
1239 INT64_RANGE_MAX (src_value) * INT64_RANGE_STEP (src_value),
1240 INT64_RANGE_STEP (src_value));
1244 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1246 /* calculate the number of values in each range */
1247 gint64 n1 = INT64_RANGE_MAX (value1) - INT64_RANGE_MIN (value1) + 1;
1248 gint64 n2 = INT64_RANGE_MAX (value2) - INT64_RANGE_MIN (value2) + 1;
1250 /* they must be equal */
1252 return GST_VALUE_UNORDERED;
1254 /* if empty, equal */
1256 return GST_VALUE_EQUAL;
1258 /* if more than one value, then it is only equal if the step is equal
1259 and bounds lie on the same value */
1261 if (INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2) &&
1262 INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2) &&
1263 INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2)) {
1264 return GST_VALUE_EQUAL;
1266 return GST_VALUE_UNORDERED;
1268 /* if just one, only if the value is equal */
1269 if (INT64_RANGE_MIN (value1) == INT64_RANGE_MIN (value2))
1270 return GST_VALUE_EQUAL;
1271 return GST_VALUE_UNORDERED;
1276 gst_value_serialize_int64_range (const GValue * value)
1278 if (INT64_RANGE_STEP (value) == 1)
1279 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1280 INT64_RANGE_MIN (value), INT64_RANGE_MAX (value));
1282 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT ", %"
1283 G_GINT64_FORMAT " ]",
1284 INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value),
1285 INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value),
1286 INT64_RANGE_STEP (value));
1290 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1292 g_warning ("unimplemented");
1301 gst_value_init_double_range (GValue * value)
1303 value->data[0].v_double = 0;
1304 value->data[1].v_double = 0;
1308 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1310 dest_value->data[0].v_double = src_value->data[0].v_double;
1311 dest_value->data[1].v_double = src_value->data[1].v_double;
1315 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1316 GTypeCValue * collect_values, guint collect_flags)
1318 if (n_collect_values != 2)
1319 return g_strdup_printf ("not enough value locations for `%s' passed",
1320 G_VALUE_TYPE_NAME (value));
1321 if (collect_values[0].v_double >= collect_values[1].v_double)
1322 return g_strdup_printf ("range start is not smaller than end for `%s'",
1323 G_VALUE_TYPE_NAME (value));
1325 value->data[0].v_double = collect_values[0].v_double;
1326 value->data[1].v_double = collect_values[1].v_double;
1332 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1333 GTypeCValue * collect_values, guint collect_flags)
1335 gdouble *double_range_start = collect_values[0].v_pointer;
1336 gdouble *double_range_end = collect_values[1].v_pointer;
1338 if (!double_range_start)
1339 return g_strdup_printf ("start value location for `%s' passed as NULL",
1340 G_VALUE_TYPE_NAME (value));
1341 if (!double_range_end)
1342 return g_strdup_printf ("end value location for `%s' passed as NULL",
1343 G_VALUE_TYPE_NAME (value));
1345 *double_range_start = value->data[0].v_double;
1346 *double_range_end = value->data[1].v_double;
1352 * gst_value_set_double_range:
1353 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1354 * @start: the start of the range
1355 * @end: the end of the range
1357 * Sets @value to the range specified by @start and @end.
1360 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1362 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1363 g_return_if_fail (start < end);
1365 value->data[0].v_double = start;
1366 value->data[1].v_double = end;
1370 * gst_value_get_double_range_min:
1371 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1373 * Gets the minimum of the range specified by @value.
1375 * Returns: the minimum of the range
1378 gst_value_get_double_range_min (const GValue * value)
1380 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1382 return value->data[0].v_double;
1386 * gst_value_get_double_range_max:
1387 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1389 * Gets the maximum of the range specified by @value.
1391 * Returns: the maxumum of the range
1394 gst_value_get_double_range_max (const GValue * value)
1396 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1398 return value->data[1].v_double;
1402 gst_value_transform_double_range_string (const GValue * src_value,
1403 GValue * dest_value)
1405 gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1407 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1408 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1409 src_value->data[0].v_double),
1410 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1411 src_value->data[1].v_double));
1415 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1417 if (value2->data[0].v_double == value1->data[0].v_double &&
1418 value2->data[0].v_double == value1->data[0].v_double)
1419 return GST_VALUE_EQUAL;
1420 return GST_VALUE_UNORDERED;
1424 gst_value_serialize_double_range (const GValue * value)
1426 gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1427 gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1429 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1430 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1431 return g_strdup_printf ("[ %s, %s ]", d1, d2);
1435 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1437 g_warning ("unimplemented");
1446 gst_value_init_fraction_range (GValue * value)
1451 ftype = GST_TYPE_FRACTION;
1453 value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1454 g_value_init (&vals[0], ftype);
1455 g_value_init (&vals[1], ftype);
1459 gst_value_free_fraction_range (GValue * value)
1461 GValue *vals = (GValue *) value->data[0].v_pointer;
1464 /* we know the two values contain fractions without internal allocs */
1465 /* g_value_unset (&vals[0]); */
1466 /* g_value_unset (&vals[1]); */
1467 g_slice_free1 (2 * sizeof (GValue), vals);
1468 value->data[0].v_pointer = NULL;
1473 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1475 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1476 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1479 gst_value_init_fraction_range (dest_value);
1480 vals = dest_value->data[0].v_pointer;
1482 if (src_vals != NULL) {
1483 g_value_copy (&src_vals[0], &vals[0]);
1484 g_value_copy (&src_vals[1], &vals[1]);
1489 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1490 GTypeCValue * collect_values, guint collect_flags)
1492 GValue *vals = (GValue *) value->data[0].v_pointer;
1494 if (n_collect_values != 4)
1495 return g_strdup_printf ("not enough value locations for `%s' passed",
1496 G_VALUE_TYPE_NAME (value));
1497 if (collect_values[1].v_int == 0)
1498 return g_strdup_printf ("passed '0' as first denominator for `%s'",
1499 G_VALUE_TYPE_NAME (value));
1500 if (collect_values[3].v_int == 0)
1501 return g_strdup_printf ("passed '0' as second denominator for `%s'",
1502 G_VALUE_TYPE_NAME (value));
1503 if (gst_util_fraction_compare (collect_values[0].v_int,
1504 collect_values[1].v_int, collect_values[2].v_int,
1505 collect_values[3].v_int) >= 0)
1506 return g_strdup_printf ("range start is not smaller than end for `%s'",
1507 G_VALUE_TYPE_NAME (value));
1510 gst_value_init_fraction_range (value);
1511 vals = value->data[0].v_pointer;
1514 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1515 collect_values[1].v_int);
1516 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1517 collect_values[3].v_int);
1523 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1524 GTypeCValue * collect_values, guint collect_flags)
1527 gint *dest_values[4];
1528 GValue *vals = (GValue *) value->data[0].v_pointer;
1530 if (G_UNLIKELY (n_collect_values != 4))
1531 return g_strdup_printf ("not enough value locations for `%s' passed",
1532 G_VALUE_TYPE_NAME (value));
1534 for (i = 0; i < 4; i++) {
1535 if (G_UNLIKELY (collect_values[i].v_pointer == NULL)) {
1536 return g_strdup_printf ("value location for `%s' passed as NULL",
1537 G_VALUE_TYPE_NAME (value));
1539 dest_values[i] = collect_values[i].v_pointer;
1542 if (G_UNLIKELY (vals == NULL)) {
1543 return g_strdup_printf ("Uninitialised `%s' passed",
1544 G_VALUE_TYPE_NAME (value));
1547 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1548 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1549 dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1550 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1555 * gst_value_set_fraction_range:
1556 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1557 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1558 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1560 * Sets @value to the range specified by @start and @end.
1563 gst_value_set_fraction_range (GValue * value, const GValue * start,
1568 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1569 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
1570 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
1571 g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
1572 start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
1574 vals = (GValue *) value->data[0].v_pointer;
1576 gst_value_init_fraction_range (value);
1577 vals = value->data[0].v_pointer;
1579 g_value_copy (start, &vals[0]);
1580 g_value_copy (end, &vals[1]);
1584 * gst_value_set_fraction_range_full:
1585 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1586 * @numerator_start: the numerator start of the range
1587 * @denominator_start: the denominator start of the range
1588 * @numerator_end: the numerator end of the range
1589 * @denominator_end: the denominator end of the range
1591 * Sets @value to the range specified by @numerator_start/@denominator_start
1592 * and @numerator_end/@denominator_end.
1595 gst_value_set_fraction_range_full (GValue * value,
1596 gint numerator_start, gint denominator_start,
1597 gint numerator_end, gint denominator_end)
1599 GValue start = { 0 };
1602 g_return_if_fail (value != NULL);
1603 g_return_if_fail (denominator_start != 0);
1604 g_return_if_fail (denominator_end != 0);
1605 g_return_if_fail (gst_util_fraction_compare (numerator_start,
1606 denominator_start, numerator_end, denominator_end) < 0);
1608 g_value_init (&start, GST_TYPE_FRACTION);
1609 g_value_init (&end, GST_TYPE_FRACTION);
1611 gst_value_set_fraction (&start, numerator_start, denominator_start);
1612 gst_value_set_fraction (&end, numerator_end, denominator_end);
1613 gst_value_set_fraction_range (value, &start, &end);
1615 /* we know the two values contain fractions without internal allocs */
1616 /* g_value_unset (&start); */
1617 /* g_value_unset (&end); */
1621 * gst_value_get_fraction_range_min:
1622 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1624 * Gets the minimum of the range specified by @value.
1626 * Returns: the minimum of the range
1629 gst_value_get_fraction_range_min (const GValue * value)
1633 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1635 vals = (GValue *) value->data[0].v_pointer;
1644 * gst_value_get_fraction_range_max:
1645 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1647 * Gets the maximum of the range specified by @value.
1649 * Returns: the maximum of the range
1652 gst_value_get_fraction_range_max (const GValue * value)
1656 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1658 vals = (GValue *) value->data[0].v_pointer;
1667 gst_value_serialize_fraction_range (const GValue * value)
1669 GValue *vals = (GValue *) value->data[0].v_pointer;
1673 retval = g_strdup ("[ 0/1, 0/1 ]");
1677 start = gst_value_serialize_fraction (&vals[0]);
1678 end = gst_value_serialize_fraction (&vals[1]);
1680 retval = g_strdup_printf ("[ %s, %s ]", start, end);
1689 gst_value_transform_fraction_range_string (const GValue * src_value,
1690 GValue * dest_value)
1692 dest_value->data[0].v_pointer =
1693 gst_value_serialize_fraction_range (src_value);
1697 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1699 GValue *vals1, *vals2;
1700 GstValueCompareFunc compare;
1702 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1703 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
1705 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1706 return GST_VALUE_UNORDERED;
1708 vals1 = (GValue *) value1->data[0].v_pointer;
1709 vals2 = (GValue *) value2->data[0].v_pointer;
1710 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1711 if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1713 gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1715 return GST_VALUE_EQUAL;
1717 return GST_VALUE_UNORDERED;
1721 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1723 g_warning ("unimplemented");
1732 * gst_value_set_caps:
1733 * @value: a GValue initialized to GST_TYPE_CAPS
1734 * @caps: (transfer none): the caps to set the value to
1736 * Sets the contents of @value to @caps. A reference to the
1737 * provided @caps will be taken by the @value.
1740 gst_value_set_caps (GValue * value, const GstCaps * caps)
1742 g_return_if_fail (G_IS_VALUE (value));
1743 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1744 g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
1746 g_value_set_boxed (value, caps);
1750 * gst_value_get_caps:
1751 * @value: a GValue initialized to GST_TYPE_CAPS
1753 * Gets the contents of @value. The reference count of the returned
1754 * #GstCaps will not be modified, therefore the caller must take one
1755 * before getting rid of the @value.
1757 * Returns: (transfer none): the contents of @value
1760 gst_value_get_caps (const GValue * value)
1762 g_return_val_if_fail (G_IS_VALUE (value), NULL);
1763 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1765 return (GstCaps *) g_value_get_boxed (value);
1769 gst_value_serialize_caps (const GValue * value)
1771 GstCaps *caps = g_value_get_boxed (value);
1773 return gst_caps_to_string (caps);
1777 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1781 caps = gst_caps_from_string (s);
1784 g_value_take_boxed (dest, caps);
1795 * gst_value_set_structure:
1796 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1797 * @structure: the structure to set the value to
1799 * Sets the contents of @value to @structure. The actual
1804 gst_value_set_structure (GValue * value, const GstStructure * structure)
1806 g_return_if_fail (G_IS_VALUE (value));
1807 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1808 g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
1810 g_value_set_boxed (value, structure);
1814 * gst_value_get_structure:
1815 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1817 * Gets the contents of @value.
1819 * Returns: (transfer none): the contents of @value
1823 const GstStructure *
1824 gst_value_get_structure (const GValue * value)
1826 g_return_val_if_fail (G_IS_VALUE (value), NULL);
1827 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1829 return (GstStructure *) g_value_get_boxed (value);
1833 gst_value_serialize_structure (const GValue * value)
1835 GstStructure *structure = g_value_get_boxed (value);
1837 return gst_string_take_and_wrap (gst_structure_to_string (structure));
1841 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1843 GstStructure *structure;
1846 structure = gst_structure_from_string (s, NULL);
1848 gchar *str = gst_string_unwrap (s);
1850 if (G_UNLIKELY (!str))
1853 structure = gst_structure_from_string (str, NULL);
1857 if (G_LIKELY (structure)) {
1858 g_value_take_boxed (dest, structure);
1869 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1871 GstBuffer *buf1 = gst_value_get_buffer (value1);
1872 GstBuffer *buf2 = gst_value_get_buffer (value2);
1874 GstMapInfo info1, info2;
1875 gint result = GST_VALUE_UNORDERED;
1877 size1 = gst_buffer_get_size (buf1);
1878 size2 = gst_buffer_get_size (buf2);
1881 return GST_VALUE_UNORDERED;
1884 return GST_VALUE_EQUAL;
1886 if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
1887 return GST_VALUE_UNORDERED;
1889 if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
1890 gst_buffer_unmap (buf1, &info2);
1891 return GST_VALUE_UNORDERED;
1894 if (memcmp (info1.data, info2.data, info1.size) == 0)
1895 result = GST_VALUE_EQUAL;
1897 gst_buffer_unmap (buf2, &info1);
1898 gst_buffer_unmap (buf1, &info2);
1904 gst_value_serialize_buffer (const GValue * value)
1912 buffer = gst_value_get_buffer (value);
1916 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
1921 string = g_malloc (info.size * 2 + 1);
1922 for (i = 0; i < info.size; i++) {
1923 sprintf (string + i * 2, "%02x", data[i]);
1925 string[info.size * 2] = 0;
1927 gst_buffer_unmap (buffer, &info);
1933 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1946 buffer = gst_buffer_new_allocate (NULL, len / 2, 0);
1947 if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
1951 for (i = 0; i < len / 2; i++) {
1952 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1955 ts[0] = s[i * 2 + 0];
1956 ts[1] = s[i * 2 + 1];
1959 data[i] = (guint8) strtoul (ts, NULL, 16);
1961 gst_buffer_unmap (buffer, &info);
1963 gst_value_take_buffer (dest, buffer);
1978 gst_buffer_unref (buffer);
1979 gst_buffer_unmap (buffer, &info);
1990 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1992 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1993 return GST_VALUE_EQUAL;
1994 return GST_VALUE_UNORDERED;
1998 gst_value_serialize_boolean (const GValue * value)
2000 if (value->data[0].v_int) {
2001 return g_strdup ("true");
2003 return g_strdup ("false");
2007 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
2009 gboolean ret = FALSE;
2011 if (g_ascii_strcasecmp (s, "true") == 0 ||
2012 g_ascii_strcasecmp (s, "yes") == 0 ||
2013 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
2014 g_value_set_boolean (dest, TRUE);
2016 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
2017 g_ascii_strcasecmp (s, "no") == 0 ||
2018 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
2019 g_value_set_boolean (dest, FALSE);
2026 #define CREATE_SERIALIZATION_START(_type,_macro) \
2028 gst_value_compare_ ## _type \
2029 (const GValue * value1, const GValue * value2) \
2031 g ## _type val1 = g_value_get_ ## _type (value1); \
2032 g ## _type val2 = g_value_get_ ## _type (value2); \
2034 return GST_VALUE_GREATER_THAN; \
2036 return GST_VALUE_LESS_THAN; \
2037 return GST_VALUE_EQUAL; \
2041 gst_value_serialize_ ## _type (const GValue * value) \
2043 GValue val = { 0, }; \
2044 g_value_init (&val, G_TYPE_STRING); \
2045 if (!g_value_transform (value, &val)) \
2046 g_assert_not_reached (); \
2047 /* NO_COPY_MADNESS!!! */ \
2048 return (char *) g_value_get_string (&val); \
2051 /* deserialize the given s into to as a gint64.
2052 * check if the result is actually storeable in the given size number of
2056 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
2057 gint64 min, gint64 max, gint size)
2059 gboolean ret = FALSE;
2064 *to = g_ascii_strtoull (s, &end, 0);
2065 /* a range error is a definitive no-no */
2066 if (errno == ERANGE) {
2073 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
2074 *to = G_LITTLE_ENDIAN;
2076 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
2079 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
2082 } else if (g_ascii_strcasecmp (s, "min") == 0) {
2085 } else if (g_ascii_strcasecmp (s, "max") == 0) {
2091 /* by definition, a gint64 fits into a gint64; so ignore those */
2092 if (size != sizeof (mask)) {
2094 /* for positive numbers, we create a mask of 1's outside of the range
2095 * and 0's inside the range. An and will thus keep only 1 bits
2096 * outside of the range */
2097 mask <<= (size * 8);
2098 if ((mask & *to) != 0) {
2102 /* for negative numbers, we do a 2's complement version */
2103 mask <<= ((size * 8) - 1);
2104 if ((mask & *to) != mask) {
2113 #define CREATE_SERIALIZATION(_type,_macro) \
2114 CREATE_SERIALIZATION_START(_type,_macro) \
2117 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
2121 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
2122 G_MAX ## _macro, sizeof (g ## _type))) { \
2123 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
2130 #define CREATE_USERIALIZATION(_type,_macro) \
2131 CREATE_SERIALIZATION_START(_type,_macro) \
2134 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
2138 gboolean ret = FALSE; \
2141 x = g_ascii_strtoull (s, &end, 0); \
2142 /* a range error is a definitive no-no */ \
2143 if (errno == ERANGE) { \
2146 /* the cast ensures the range check later on makes sense */ \
2147 x = (g ## _type) x; \
2151 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
2152 x = G_LITTLE_ENDIAN; \
2154 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
2157 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
2160 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
2163 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
2164 x = G_MAX ## _macro; \
2169 if (x > G_MAX ## _macro) { \
2172 g_value_set_ ## _type (dest, x); \
2178 #define REGISTER_SERIALIZATION(_gtype, _type) \
2180 static const GstValueTable gst_value = { \
2182 gst_value_compare_ ## _type, \
2183 gst_value_serialize_ ## _type, \
2184 gst_value_deserialize_ ## _type, \
2187 gst_value_register (&gst_value); \
2190 CREATE_SERIALIZATION (int, INT);
2191 CREATE_SERIALIZATION (int64, INT64);
2192 CREATE_SERIALIZATION (long, LONG);
2194 CREATE_USERIALIZATION (uint, UINT);
2195 CREATE_USERIALIZATION (uint64, UINT64);
2196 CREATE_USERIALIZATION (ulong, ULONG);
2198 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2200 #define G_MAXUCHAR 255
2202 CREATE_USERIALIZATION (uchar, UCHAR);
2208 gst_value_compare_double (const GValue * value1, const GValue * value2)
2210 if (value1->data[0].v_double > value2->data[0].v_double)
2211 return GST_VALUE_GREATER_THAN;
2212 if (value1->data[0].v_double < value2->data[0].v_double)
2213 return GST_VALUE_LESS_THAN;
2214 if (value1->data[0].v_double == value2->data[0].v_double)
2215 return GST_VALUE_EQUAL;
2216 return GST_VALUE_UNORDERED;
2220 gst_value_serialize_double (const GValue * value)
2222 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2224 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2225 return g_strdup (d);
2229 gst_value_deserialize_double (GValue * dest, const gchar * s)
2232 gboolean ret = FALSE;
2235 x = g_ascii_strtod (s, &end);
2239 if (g_ascii_strcasecmp (s, "min") == 0) {
2242 } else if (g_ascii_strcasecmp (s, "max") == 0) {
2248 g_value_set_double (dest, x);
2258 gst_value_compare_float (const GValue * value1, const GValue * value2)
2260 if (value1->data[0].v_float > value2->data[0].v_float)
2261 return GST_VALUE_GREATER_THAN;
2262 if (value1->data[0].v_float < value2->data[0].v_float)
2263 return GST_VALUE_LESS_THAN;
2264 if (value1->data[0].v_float == value2->data[0].v_float)
2265 return GST_VALUE_EQUAL;
2266 return GST_VALUE_UNORDERED;
2270 gst_value_serialize_float (const GValue * value)
2272 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2274 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2275 return g_strdup (d);
2279 gst_value_deserialize_float (GValue * dest, const gchar * s)
2282 gboolean ret = FALSE;
2285 x = g_ascii_strtod (s, &end);
2289 if (g_ascii_strcasecmp (s, "min") == 0) {
2292 } else if (g_ascii_strcasecmp (s, "max") == 0) {
2297 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2300 g_value_set_float (dest, (float) x);
2310 gst_value_compare_string (const GValue * value1, const GValue * value2)
2312 if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2313 /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2314 if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2315 return GST_VALUE_UNORDERED;
2317 gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2320 return GST_VALUE_LESS_THAN;
2322 return GST_VALUE_GREATER_THAN;
2325 return GST_VALUE_EQUAL;
2329 gst_string_measure_wrapping (const gchar * s)
2332 gboolean wrap = FALSE;
2334 if (G_UNLIKELY (s == NULL))
2337 /* Special case: the actual string NULL needs wrapping */
2338 if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2343 if (GST_ASCII_IS_STRING (*s)) {
2345 } else if (*s < 0x20 || *s >= 0x7f) {
2355 /* Wrap the string if we found something that needs
2356 * wrapping, or the empty string (len == 0) */
2357 return (wrap || len == 0) ? len : -1;
2361 gst_string_wrap_inner (const gchar * s, gint len)
2365 e = d = g_malloc (len + 3);
2369 if (GST_ASCII_IS_STRING (*s)) {
2371 } else if (*s < 0x20 || *s >= 0x7f) {
2373 *e++ = '0' + ((*(guchar *) s) >> 6);
2374 *e++ = '0' + (((*s) >> 3) & 0x7);
2375 *e++ = '0' + ((*s++) & 0x7);
2384 g_assert (e - d <= len + 3);
2388 /* Do string wrapping/escaping */
2390 gst_string_wrap (const gchar * s)
2392 gint len = gst_string_measure_wrapping (s);
2394 if (G_LIKELY (len < 0))
2395 return g_strdup (s);
2397 return gst_string_wrap_inner (s, len);
2400 /* Same as above, but take ownership of the string */
2402 gst_string_take_and_wrap (gchar * s)
2405 gint len = gst_string_measure_wrapping (s);
2407 if (G_LIKELY (len < 0))
2410 out = gst_string_wrap_inner (s, len);
2417 * This function takes a string delimited with double quotes (")
2418 * and unescapes any \xxx octal numbers.
2420 * If sequences of \y are found where y is not in the range of
2421 * 0->3, y is copied unescaped.
2423 * If \xyy is found where x is an octal number but y is not, an
2424 * error is encountered and NULL is returned.
2426 * the input string must be \0 terminated.
2429 gst_string_unwrap (const gchar * s)
2432 gchar *read, *write;
2434 /* NULL string returns NULL */
2438 /* strings not starting with " are invalid */
2442 /* make copy of original string to hold the result. This
2443 * string will always be smaller than the original */
2448 /* need to move to the next position as we parsed the " */
2452 if (GST_ASCII_IS_STRING (*read)) {
2453 /* normal chars are just copied */
2455 } else if (*read == '"') {
2456 /* quote marks end of string */
2458 } else if (*read == '\\') {
2459 /* got an escape char, move to next position to read a tripplet
2460 * of octal numbers */
2462 /* is the next char a possible first octal number? */
2463 if (*read >= '0' && *read <= '3') {
2464 /* parse other 2 numbers, if one of them is not in the range of
2465 * an octal number, we error. We also catch the case where a zero
2466 * byte is found here. */
2467 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2470 /* now convert the octal number to a byte again. */
2471 *write++ = ((read[0] - '0') << 6) +
2472 ((read[1] - '0') << 3) + (read[2] - '0');
2476 /* if we run into a \0 here, we definitely won't get a quote later */
2480 /* else copy \X sequence */
2484 /* weird character, error */
2488 /* if the string is not ending in " and zero terminated, we error */
2489 if (*read != '"' || read[1] != '\0')
2492 /* null terminate result string and return */
2502 gst_value_serialize_string (const GValue * value)
2504 return gst_string_wrap (value->data[0].v_pointer);
2508 gst_value_deserialize_string (GValue * dest, const gchar * s)
2510 if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2511 g_value_set_string (dest, NULL);
2513 } else if (G_LIKELY (*s != '"')) {
2514 if (!g_utf8_validate (s, -1, NULL))
2516 g_value_set_string (dest, s);
2519 gchar *str = gst_string_unwrap (s);
2520 if (G_UNLIKELY (!str))
2522 g_value_take_string (dest, str);
2533 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2535 GEnumValue *en1, *en2;
2536 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2537 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2539 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2540 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2541 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2542 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2543 g_type_class_unref (klass1);
2544 g_type_class_unref (klass2);
2545 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2546 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2547 if (en1->value < en2->value)
2548 return GST_VALUE_LESS_THAN;
2549 if (en1->value > en2->value)
2550 return GST_VALUE_GREATER_THAN;
2552 return GST_VALUE_EQUAL;
2556 gst_value_serialize_enum (const GValue * value)
2559 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
2561 g_return_val_if_fail (klass, NULL);
2562 en = g_enum_get_value (klass, g_value_get_enum (value));
2563 g_type_class_unref (klass);
2565 /* might be one of the custom formats registered later */
2566 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
2567 const GstFormatDefinition *format_def;
2569 format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
2570 g_return_val_if_fail (format_def != NULL, NULL);
2571 return g_strdup (format_def->description);
2574 g_return_val_if_fail (en, NULL);
2575 return g_strdup (en->value_name);
2579 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
2582 const GstFormatDefinition *format_def =
2583 g_value_get_pointer (format_def_value);
2585 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2588 return g_ascii_strcasecmp (s, format_def->description);
2592 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2595 gchar *endptr = NULL;
2596 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2598 g_return_val_if_fail (klass, FALSE);
2599 if (!(en = g_enum_get_value_by_name (klass, s))) {
2600 if (!(en = g_enum_get_value_by_nick (klass, s))) {
2601 gint i = strtol (s, &endptr, 0);
2603 if (endptr && *endptr == '\0') {
2604 en = g_enum_get_value (klass, i);
2608 g_type_class_unref (klass);
2610 /* might be one of the custom formats registered later */
2611 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2612 GValue res = { 0, };
2613 const GstFormatDefinition *format_def;
2617 iter = gst_format_iterate_definitions ();
2619 found = gst_iterator_find_custom (iter,
2620 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
2622 g_return_val_if_fail (found, FALSE);
2623 format_def = g_value_get_pointer (&res);
2624 g_return_val_if_fail (format_def != NULL, FALSE);
2625 g_value_set_enum (dest, (gint) format_def->value);
2626 g_value_unset (&res);
2627 gst_iterator_free (iter);
2631 g_return_val_if_fail (en, FALSE);
2632 g_value_set_enum (dest, en->value);
2640 /* we just compare the value here */
2642 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2645 GFlagsClass *klass1 =
2646 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2647 GFlagsClass *klass2 =
2648 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2650 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2651 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2652 fl1 = g_value_get_flags (value1);
2653 fl2 = g_value_get_flags (value2);
2654 g_type_class_unref (klass1);
2655 g_type_class_unref (klass2);
2657 return GST_VALUE_LESS_THAN;
2659 return GST_VALUE_GREATER_THAN;
2661 return GST_VALUE_EQUAL;
2664 /* the different flags are serialized separated with a + */
2666 gst_value_serialize_flags (const GValue * value)
2670 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2671 gchar *result, *tmp;
2672 gboolean first = TRUE;
2674 g_return_val_if_fail (klass, NULL);
2676 flags = g_value_get_flags (value);
2678 /* if no flags are set, try to serialize to the _NONE string */
2680 fl = g_flags_get_first_value (klass, flags);
2681 return g_strdup (fl->value_name);
2684 /* some flags are set, so serialize one by one */
2685 result = g_strdup ("");
2687 fl = g_flags_get_first_value (klass, flags);
2689 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2695 flags &= ~fl->value;
2698 g_type_class_unref (klass);
2704 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2707 gchar *endptr = NULL;
2708 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2713 g_return_val_if_fail (klass, FALSE);
2715 /* split into parts delimited with + */
2716 split = g_strsplit (s, "+", 0);
2720 /* loop over each part */
2722 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2723 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2724 gint val = strtol (split[i], &endptr, 0);
2726 /* just or numeric value */
2727 if (endptr && *endptr == '\0') {
2738 g_type_class_unref (klass);
2739 g_value_set_flags (dest, flags);
2749 gst_value_is_subset_int_range_int_range (const GValue * value1,
2750 const GValue * value2)
2754 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
2755 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
2757 if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
2758 INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
2760 if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
2761 INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
2764 if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
2765 if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
2766 INT_RANGE_STEP (value1))
2772 gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
2773 INT_RANGE_STEP (value2));
2774 if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
2781 gst_value_is_subset_int64_range_int64_range (const GValue * value1,
2782 const GValue * value2)
2786 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
2787 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
2789 if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
2791 if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
2794 if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
2795 if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
2796 INT64_RANGE_STEP (value1))
2802 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
2803 INT64_RANGE_STEP (value2));
2804 if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
2811 gst_value_is_subset (const GValue * value1, const GValue * value2)
2813 /* special case for int/int64 ranges, since we cannot compute
2814 the difference for those when they have different steps,
2815 and it's actually a lot simpler to compute whether a range
2816 is a subset of another. */
2817 if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
2818 return gst_value_is_subset_int_range_int_range (value1, value2);
2819 } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
2820 && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
2821 return gst_value_is_subset_int64_range_int64_range (value1, value2);
2829 * -> 1 - [1,2] = empty
2833 * -> [1,2] - [1,3] = empty
2837 * -> {1,3} - {1,2} = 3
2840 * First caps subtraction needs to return a non-empty set, second
2841 * subtractions needs to give en empty set.
2842 * Both substractions are switched below, as it's faster that way.
2844 if (!gst_value_subtract (NULL, value1, value2)) {
2845 if (gst_value_subtract (NULL, value2, value1)) {
2857 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2858 const GValue * src2)
2860 gint v = src1->data[0].v_int;
2862 /* check if it's already in the range */
2863 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
2864 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
2865 v % INT_RANGE_STEP (src2) == 0) {
2867 gst_value_init_and_copy (dest, src2);
2871 /* check if it extends the range */
2872 if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
2874 gst_value_init_and_copy (dest, src2);
2875 --INT_RANGE_MIN (src2);
2879 if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
2881 gst_value_init_and_copy (dest, src2);
2882 ++INT_RANGE_MAX (src2);
2891 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2892 const GValue * src2)
2894 /* We can union in several special cases:
2895 1 - one is a subset of another
2896 2 - same step and not disjoint
2897 3 - different step, at least one with one value which matches a 'next' or 'previous'
2902 if (gst_value_is_subset_int_range_int_range (src1, src2)) {
2904 gst_value_init_and_copy (dest, src2);
2907 if (gst_value_is_subset_int_range_int_range (src2, src1)) {
2909 gst_value_init_and_copy (dest, src1);
2913 /* 2 - same step and not disjoint */
2914 if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
2915 if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
2916 INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
2917 (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
2918 INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
2920 gint step = INT_RANGE_STEP (src1);
2921 gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
2922 gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
2923 g_value_init (dest, GST_TYPE_INT_RANGE);
2924 gst_value_set_int_range_step (dest, min, max, step);
2930 /* 3 - single value matches next or previous */
2931 if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
2932 gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
2933 gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
2934 if (n1 == 1 || n2 == 1) {
2935 const GValue *range_value = NULL;
2939 scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
2940 } else if (n2 == 1) {
2942 scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
2946 (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
2948 gst_value_init_and_copy (dest, range_value);
2949 --INT_RANGE_MIN (range_value);
2952 } else if (scalar ==
2953 (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
2955 gst_value_init_and_copy (dest, range_value);
2956 ++INT_RANGE_MIN (range_value);
2963 /* If we get there, we did not find a way to make a union that can be
2964 represented with our simplistic model. */
2973 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2974 const GValue * src2)
2976 if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
2977 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
2978 src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
2980 gst_value_init_and_copy (dest, src1);
2988 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2989 const GValue * src2)
2996 INT_RANGE_STEP (src1) /
2997 gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
2998 INT_RANGE_STEP (src2));
2999 if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
3001 step *= INT_RANGE_STEP (src2);
3004 MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
3005 INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
3006 min = (min + step - 1) / step * step;
3008 MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
3009 INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
3010 max = max / step * step;
3014 g_value_init (dest, GST_TYPE_INT_RANGE);
3015 gst_value_set_int_range_step (dest, min, max, step);
3021 g_value_init (dest, G_TYPE_INT);
3022 g_value_set_int (dest, min);
3031 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
3032 const GValue * src2)
3034 if (INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2) <= src1->data[0].v_int &&
3035 INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2) >= src1->data[0].v_int &&
3036 src1->data[0].v_int % INT64_RANGE_STEP (src2) == 0) {
3038 gst_value_init_and_copy (dest, src1);
3046 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
3047 const GValue * src2)
3054 INT64_RANGE_STEP (src1) /
3055 gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
3056 INT64_RANGE_STEP (src2));
3057 if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
3059 step *= INT64_RANGE_STEP (src2);
3062 MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
3063 INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
3064 min = (min + step - 1) / step * step;
3066 MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
3067 INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
3068 max = max / step * step;
3072 g_value_init (dest, GST_TYPE_INT64_RANGE);
3073 gst_value_set_int64_range_step (dest, min, max, step);
3079 g_value_init (dest, G_TYPE_INT64);
3080 g_value_set_int64 (dest, min);
3089 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
3090 const GValue * src2)
3092 if (src2->data[0].v_double <= src1->data[0].v_double &&
3093 src2->data[1].v_double >= src1->data[0].v_double) {
3095 gst_value_init_and_copy (dest, src1);
3103 gst_value_intersect_double_range_double_range (GValue * dest,
3104 const GValue * src1, const GValue * src2)
3109 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
3110 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
3114 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
3115 gst_value_set_double_range (dest, min, max);
3121 g_value_init (dest, G_TYPE_DOUBLE);
3122 g_value_set_int (dest, (int) min);
3131 gst_value_intersect_list (GValue * dest, const GValue * value1,
3132 const GValue * value2)
3135 GValue intersection = { 0, };
3136 gboolean ret = FALSE;
3138 size = VALUE_LIST_SIZE (value1);
3139 for (i = 0; i < size; i++) {
3140 const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
3142 /* quicker version when we don't need the resulting set */
3144 if (gst_value_intersect (NULL, cur, value2)) {
3151 if (gst_value_intersect (&intersection, cur, value2)) {
3154 gst_value_init_and_copy (dest, &intersection);
3156 } else if (GST_VALUE_HOLDS_LIST (dest)) {
3157 gst_value_list_append_value (dest, &intersection);
3159 GValue temp = { 0, };
3161 gst_value_init_and_copy (&temp, dest);
3162 g_value_unset (dest);
3163 gst_value_list_concat (dest, &temp, &intersection);
3164 g_value_unset (&temp);
3166 g_value_unset (&intersection);
3174 gst_value_intersect_array (GValue * dest, const GValue * src1,
3175 const GValue * src2)
3181 /* only works on similar-sized arrays */
3182 size = gst_value_array_get_size (src1);
3183 if (size != gst_value_array_get_size (src2))
3186 /* quicker value when we don't need the resulting set */
3188 for (n = 0; n < size; n++) {
3189 if (!gst_value_intersect (NULL, gst_value_array_get_value (src1, n),
3190 gst_value_array_get_value (src2, n))) {
3197 g_value_init (dest, GST_TYPE_ARRAY);
3199 for (n = 0; n < size; n++) {
3200 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
3201 gst_value_array_get_value (src2, n))) {
3202 g_value_unset (dest);
3205 gst_value_array_append_value (dest, &val);
3206 g_value_unset (&val);
3213 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
3214 const GValue * src2)
3218 GstValueCompareFunc compare;
3220 vals = src2->data[0].v_pointer;
3225 if ((compare = gst_value_get_compare_func (src1))) {
3226 res1 = gst_value_compare_with_func (&vals[0], src1, compare);
3227 res2 = gst_value_compare_with_func (&vals[1], src1, compare);
3229 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
3230 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
3232 gst_value_init_and_copy (dest, src1);
3241 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
3242 const GValue * src1, const GValue * src2)
3247 GValue *vals1, *vals2;
3248 GstValueCompareFunc compare;
3250 vals1 = src1->data[0].v_pointer;
3251 vals2 = src2->data[0].v_pointer;
3252 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
3254 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
3255 /* min = MAX (src1.start, src2.start) */
3256 res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
3257 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3258 if (res == GST_VALUE_LESS_THAN)
3259 min = &vals2[0]; /* Take the max of the 2 */
3263 /* max = MIN (src1.end, src2.end) */
3264 res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
3265 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3266 if (res == GST_VALUE_GREATER_THAN)
3267 max = &vals2[1]; /* Take the min of the 2 */
3271 res = gst_value_compare_with_func (min, max, compare);
3272 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
3273 if (res == GST_VALUE_LESS_THAN) {
3275 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
3276 vals1 = dest->data[0].v_pointer;
3277 g_value_copy (min, &vals1[0]);
3278 g_value_copy (max, &vals1[1]);
3282 if (res == GST_VALUE_EQUAL) {
3284 gst_value_init_and_copy (dest, min);
3297 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
3298 const GValue * subtrahend)
3300 gint min = gst_value_get_int_range_min (subtrahend);
3301 gint max = gst_value_get_int_range_max (subtrahend);
3302 gint step = gst_value_get_int_range_step (subtrahend);
3303 gint val = g_value_get_int (minuend);
3305 /* subtracting a range from an int only works if the int is not in the
3307 if (val < min || val > max || val % step) {
3308 /* and the result is the int */
3310 gst_value_init_and_copy (dest, minuend);
3316 /* creates a new int range based on input values.
3319 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
3320 gint max2, gint step)
3324 GValue *pv1, *pv2; /* yeah, hungarian! */
3326 g_return_val_if_fail (step > 0, FALSE);
3327 g_return_val_if_fail (min1 % step == 0, FALSE);
3328 g_return_val_if_fail (max1 % step == 0, FALSE);
3329 g_return_val_if_fail (min2 % step == 0, FALSE);
3330 g_return_val_if_fail (max2 % step == 0, FALSE);
3332 if (min1 <= max1 && min2 <= max2) {
3335 } else if (min1 <= max1) {
3338 } else if (min2 <= max2) {
3349 g_value_init (pv1, GST_TYPE_INT_RANGE);
3350 gst_value_set_int_range_step (pv1, min1, max1, step);
3351 } else if (min1 == max1) {
3352 g_value_init (pv1, G_TYPE_INT);
3353 g_value_set_int (pv1, min1);
3356 g_value_init (pv2, GST_TYPE_INT_RANGE);
3357 gst_value_set_int_range_step (pv2, min2, max2, step);
3358 } else if (min2 == max2) {
3359 g_value_init (pv2, G_TYPE_INT);
3360 g_value_set_int (pv2, min2);
3363 if (min1 <= max1 && min2 <= max2) {
3364 gst_value_list_concat (dest, pv1, pv2);
3365 g_value_unset (pv1);
3366 g_value_unset (pv2);
3372 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
3373 const GValue * subtrahend)
3375 gint min = gst_value_get_int_range_min (minuend);
3376 gint max = gst_value_get_int_range_max (minuend);
3377 gint step = gst_value_get_int_range_step (minuend);
3378 gint val = g_value_get_int (subtrahend);
3380 g_return_val_if_fail (min < max, FALSE);
3382 /* value is outside of the range, return range unchanged */
3383 if (val < min || val > max || val % step) {
3385 gst_value_init_and_copy (dest, minuend);
3388 /* max must be MAXINT too as val <= max */
3389 if (val >= G_MAXINT - step + 1) {
3393 /* min must be MININT too as val >= max */
3394 if (val <= G_MININT + step - 1) {
3399 gst_value_create_new_range (dest, min, val - step, val + step, max, step);
3405 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
3406 const GValue * subtrahend)
3408 gint min1 = gst_value_get_int_range_min (minuend);
3409 gint max1 = gst_value_get_int_range_max (minuend);
3410 gint step1 = gst_value_get_int_range_step (minuend);
3411 gint min2 = gst_value_get_int_range_min (subtrahend);
3412 gint max2 = gst_value_get_int_range_max (subtrahend);
3413 gint step2 = gst_value_get_int_range_step (subtrahend);
3416 if (step1 != step2) {
3423 if (max2 >= max1 && min2 <= min1) {
3425 } else if (max2 >= max1) {
3426 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3428 } else if (min2 <= min1) {
3429 return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
3432 return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
3433 MAX (max2 + step, min1), max1, step);
3438 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
3439 const GValue * subtrahend)
3441 gint64 min = gst_value_get_int64_range_min (subtrahend);
3442 gint64 max = gst_value_get_int64_range_max (subtrahend);
3443 gint64 step = gst_value_get_int64_range_step (subtrahend);
3444 gint64 val = g_value_get_int64 (minuend);
3446 /* subtracting a range from an int64 only works if the int64 is not in the
3448 if (val < min || val > max || val % step) {
3449 /* and the result is the int64 */
3451 gst_value_init_and_copy (dest, minuend);
3457 /* creates a new int64 range based on input values.
3460 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
3461 gint64 min2, gint64 max2, gint64 step)
3465 GValue *pv1, *pv2; /* yeah, hungarian! */
3467 g_return_val_if_fail (step > 0, FALSE);
3468 g_return_val_if_fail (min1 % step == 0, FALSE);
3469 g_return_val_if_fail (max1 % step == 0, FALSE);
3470 g_return_val_if_fail (min2 % step == 0, FALSE);
3471 g_return_val_if_fail (max2 % step == 0, FALSE);
3473 if (min1 <= max1 && min2 <= max2) {
3476 } else if (min1 <= max1) {
3479 } else if (min2 <= max2) {
3490 g_value_init (pv1, GST_TYPE_INT64_RANGE);
3491 gst_value_set_int64_range_step (pv1, min1, max1, step);
3492 } else if (min1 == max1) {
3493 g_value_init (pv1, G_TYPE_INT64);
3494 g_value_set_int64 (pv1, min1);
3497 g_value_init (pv2, GST_TYPE_INT64_RANGE);
3498 gst_value_set_int64_range_step (pv2, min2, max2, step);
3499 } else if (min2 == max2) {
3500 g_value_init (pv2, G_TYPE_INT64);
3501 g_value_set_int64 (pv2, min2);
3504 if (min1 <= max1 && min2 <= max2) {
3505 gst_value_list_concat (dest, pv1, pv2);
3506 g_value_unset (pv1);
3507 g_value_unset (pv2);
3513 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
3514 const GValue * subtrahend)
3516 gint64 min = gst_value_get_int64_range_min (minuend);
3517 gint64 max = gst_value_get_int64_range_max (minuend);
3518 gint64 step = gst_value_get_int64_range_step (minuend);
3519 gint64 val = g_value_get_int64 (subtrahend);
3521 g_return_val_if_fail (min < max, FALSE);
3523 /* value is outside of the range, return range unchanged */
3524 if (val < min || val > max || val % step) {
3526 gst_value_init_and_copy (dest, minuend);
3529 /* max must be MAXINT64 too as val <= max */
3530 if (val >= G_MAXINT64 - step + 1) {
3534 /* min must be MININT64 too as val >= max */
3535 if (val <= G_MININT64 + step - 1) {
3540 gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
3547 gst_value_subtract_int64_range_int64_range (GValue * dest,
3548 const GValue * minuend, const GValue * subtrahend)
3550 gint64 min1 = gst_value_get_int64_range_min (minuend);
3551 gint64 max1 = gst_value_get_int64_range_max (minuend);
3552 gint64 step1 = gst_value_get_int64_range_step (minuend);
3553 gint64 min2 = gst_value_get_int64_range_min (subtrahend);
3554 gint64 max2 = gst_value_get_int64_range_max (subtrahend);
3555 gint64 step2 = gst_value_get_int64_range_step (subtrahend);
3558 if (step1 != step2) {
3565 if (max2 >= max1 && min2 <= min1) {
3567 } else if (max2 >= max1) {
3568 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
3569 max1), step, 0, step);
3570 } else if (min2 <= min1) {
3571 return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
3572 max1, step, 0, step);
3574 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
3575 max1), MAX (max2 + step, min1), max1, step);
3580 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
3581 const GValue * subtrahend)
3583 gdouble min = gst_value_get_double_range_min (subtrahend);
3584 gdouble max = gst_value_get_double_range_max (subtrahend);
3585 gdouble val = g_value_get_double (minuend);
3587 if (val < min || val > max) {
3589 gst_value_init_and_copy (dest, minuend);
3596 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
3597 const GValue * subtrahend)
3599 /* since we don't have open ranges, we cannot create a hole in
3600 * a double range. We return the original range */
3602 gst_value_init_and_copy (dest, minuend);
3607 gst_value_subtract_double_range_double_range (GValue * dest,
3608 const GValue * minuend, const GValue * subtrahend)
3610 /* since we don't have open ranges, we have to approximate */
3611 /* done like with ints */
3612 gdouble min1 = gst_value_get_double_range_min (minuend);
3613 gdouble max2 = gst_value_get_double_range_max (minuend);
3614 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
3615 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
3618 GValue *pv1, *pv2; /* yeah, hungarian! */
3620 if (min1 < max1 && min2 < max2) {
3623 } else if (min1 < max1) {
3626 } else if (min2 < max2) {
3637 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
3638 gst_value_set_double_range (pv1, min1, max1);
3641 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
3642 gst_value_set_double_range (pv2, min2, max2);
3645 if (min1 < max1 && min2 < max2) {
3646 gst_value_list_concat (dest, pv1, pv2);
3647 g_value_unset (pv1);
3648 g_value_unset (pv2);
3654 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
3655 const GValue * subtrahend)
3658 GValue subtraction = { 0, };
3659 gboolean ret = FALSE;
3662 ltype = gst_value_list_get_type ();
3664 size = VALUE_LIST_SIZE (minuend);
3665 for (i = 0; i < size; i++) {
3666 const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
3668 /* quicker version when we can discard the result */
3670 if (gst_value_subtract (NULL, cur, subtrahend)) {
3677 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
3679 gst_value_init_and_copy (dest, &subtraction);
3681 } else if (G_VALUE_HOLDS (dest, ltype)
3682 && !G_VALUE_HOLDS (&subtraction, ltype)) {
3683 gst_value_list_append_value (dest, &subtraction);
3685 GValue temp = { 0, };
3687 gst_value_init_and_copy (&temp, dest);
3688 g_value_unset (dest);
3689 gst_value_list_concat (dest, &temp, &subtraction);
3690 g_value_unset (&temp);
3692 g_value_unset (&subtraction);
3699 gst_value_subtract_list (GValue * dest, const GValue * minuend,
3700 const GValue * subtrahend)
3703 GValue data[2] = { {0,}, {0,} };
3704 GValue *subtraction = &data[0], *result = &data[1];
3706 gst_value_init_and_copy (result, minuend);
3707 size = VALUE_LIST_SIZE (subtrahend);
3708 for (i = 0; i < size; i++) {
3709 const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
3711 if (gst_value_subtract (subtraction, result, cur)) {
3712 GValue *temp = result;
3714 result = subtraction;
3716 g_value_unset (subtraction);
3718 g_value_unset (result);
3723 gst_value_init_and_copy (dest, result);
3724 g_value_unset (result);
3729 gst_value_subtract_fraction_fraction_range (GValue * dest,
3730 const GValue * minuend, const GValue * subtrahend)
3732 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
3733 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
3734 GstValueCompareFunc compare;
3736 if ((compare = gst_value_get_compare_func (minuend))) {
3737 /* subtracting a range from an fraction only works if the fraction
3738 * is not in the range */
3739 if (gst_value_compare_with_func (minuend, min, compare) ==
3740 GST_VALUE_LESS_THAN ||
3741 gst_value_compare_with_func (minuend, max, compare) ==
3742 GST_VALUE_GREATER_THAN) {
3743 /* and the result is the value */
3745 gst_value_init_and_copy (dest, minuend);
3753 gst_value_subtract_fraction_range_fraction (GValue * dest,
3754 const GValue * minuend, const GValue * subtrahend)
3756 /* since we don't have open ranges, we cannot create a hole in
3757 * a range. We return the original range */
3759 gst_value_init_and_copy (dest, minuend);
3764 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
3765 const GValue * minuend, const GValue * subtrahend)
3767 /* since we don't have open ranges, we have to approximate */
3768 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
3769 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
3770 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
3771 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
3772 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
3776 GValue *pv1, *pv2; /* yeah, hungarian! */
3777 GstValueCompareFunc compare;
3779 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
3780 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
3782 compare = gst_value_get_compare_func (min1);
3783 g_return_val_if_fail (compare, FALSE);
3785 cmp1 = gst_value_compare_with_func (max2, max1, compare);
3786 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3787 if (cmp1 == GST_VALUE_LESS_THAN)
3789 cmp1 = gst_value_compare_with_func (min1, min2, compare);
3790 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3791 if (cmp1 == GST_VALUE_GREATER_THAN)
3794 cmp1 = gst_value_compare_with_func (min1, max1, compare);
3795 cmp2 = gst_value_compare_with_func (min2, max2, compare);
3797 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3800 } else if (cmp1 == GST_VALUE_LESS_THAN) {
3803 } else if (cmp2 == GST_VALUE_LESS_THAN) {
3813 if (cmp1 == GST_VALUE_LESS_THAN) {
3814 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
3815 gst_value_set_fraction_range (pv1, min1, max1);
3817 if (cmp2 == GST_VALUE_LESS_THAN) {
3818 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
3819 gst_value_set_fraction_range (pv2, min2, max2);
3822 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3823 gst_value_list_concat (dest, pv1, pv2);
3824 g_value_unset (pv1);
3825 g_value_unset (pv2);
3836 * gst_value_get_compare_func:
3837 * @value1: a value to get the compare function for
3839 * Determines the compare function to be used with values of the same type as
3840 * @value1. The function can be given to gst_value_compare_with_func().
3842 * Returns: A #GstValueCompareFunc value
3844 static GstValueCompareFunc
3845 gst_value_get_compare_func (const GValue * value1)
3847 GstValueTable *table, *best = NULL;
3851 type1 = G_VALUE_TYPE (value1);
3853 /* this is a fast check */
3854 best = gst_value_hash_lookup_type (type1);
3857 if (G_UNLIKELY (!best || !best->compare)) {
3858 guint len = gst_value_table->len;
3861 for (i = 0; i < len; i++) {
3862 table = &g_array_index (gst_value_table, GstValueTable, i);
3863 if (table->compare && g_type_is_a (type1, table->type)) {
3864 if (!best || g_type_is_a (table->type, best->type))
3869 if (G_LIKELY (best))
3870 return best->compare;
3876 * gst_value_can_compare:
3877 * @value1: a value to compare
3878 * @value2: another value to compare
3880 * Determines if @value1 and @value2 can be compared.
3882 * Returns: TRUE if the values can be compared
3885 gst_value_can_compare (const GValue * value1, const GValue * value2)
3887 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3888 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3890 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3893 return gst_value_get_compare_func (value1) != NULL;
3897 gst_value_list_equals_range (const GValue * list, const GValue * value)
3899 const GValue *first;
3902 g_return_val_if_fail (G_IS_VALUE (list), FALSE);
3903 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
3904 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (list), FALSE);
3906 /* TODO: compare against an empty list ? No type though... */
3907 list_size = VALUE_LIST_SIZE (list);
3911 /* compare the basic types - they have to match */
3912 first = VALUE_LIST_GET_VALUE (list, 0);
3913 #define CHECK_TYPES(type,prefix) \
3914 (prefix##_VALUE_HOLDS_##type(first) && GST_VALUE_HOLDS_##type##_RANGE (value))
3915 if (CHECK_TYPES (INT, G)) {
3916 const gint rmin = gst_value_get_int_range_min (value);
3917 const gint rmax = gst_value_get_int_range_max (value);
3918 const gint rstep = gst_value_get_int_range_step (value);
3919 /* note: this will overflow for min 0 and max INT_MAX, but this
3920 would only be equal to a list of INT_MAX elements, which seems
3922 if (list_size != rmax / rstep - rmin / rstep + 1)
3924 for (n = 0; n < list_size; ++n) {
3925 gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
3926 if (v < rmin || v > rmax || v % rstep) {
3931 } else if (CHECK_TYPES (INT64, G)) {
3932 const gint64 rmin = gst_value_get_int64_range_min (value);
3933 const gint64 rmax = gst_value_get_int64_range_max (value);
3934 const gint64 rstep = gst_value_get_int64_range_step (value);
3935 GST_DEBUG ("List/range of int64s");
3936 if (list_size != rmax / rstep - rmin / rstep + 1)
3938 for (n = 0; n < list_size; ++n) {
3939 gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
3940 if (v < rmin || v > rmax || v % rstep)
3947 /* other combinations don't make sense for equality */
3952 * gst_value_compare:
3953 * @value1: a value to compare
3954 * @value2: another value to compare
3956 * Compares @value1 and @value2. If @value1 and @value2 cannot be
3957 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
3958 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
3959 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
3960 * If the values are equal, GST_VALUE_EQUAL is returned.
3962 * Returns: comparison result
3965 gst_value_compare (const GValue * value1, const GValue * value2)
3967 GstValueCompareFunc compare;
3970 g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
3971 g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
3973 /* Special cases: lists and scalar values ("{ 1 }" and "1" are equal),
3974 as well as lists and ranges ("{ 1, 2 }" and "[ 1, 2 ]" are equal) */
3975 ltype = gst_value_list_get_type ();
3976 if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)) {
3978 if (gst_value_list_equals_range (value1, value2)) {
3979 return GST_VALUE_EQUAL;
3980 } else if (gst_value_list_get_size (value1) == 1) {
3983 elt = gst_value_list_get_value (value1, 0);
3984 return gst_value_compare (elt, value2);
3986 } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)) {
3987 if (gst_value_list_equals_range (value2, value1)) {
3988 return GST_VALUE_EQUAL;
3989 } else if (gst_value_list_get_size (value2) == 1) {
3992 elt = gst_value_list_get_value (value2, 0);
3993 return gst_value_compare (elt, value1);
3997 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3998 return GST_VALUE_UNORDERED;
4000 compare = gst_value_get_compare_func (value1);
4002 return compare (value1, value2);
4005 g_critical ("unable to compare values of type %s\n",
4006 g_type_name (G_VALUE_TYPE (value1)));
4007 return GST_VALUE_UNORDERED;
4011 * gst_value_compare_with_func:
4012 * @value1: a value to compare
4013 * @value2: another value to compare
4014 * @compare: compare function
4016 * Compares @value1 and @value2 using the @compare function. Works like
4017 * gst_value_compare() but allows to save time determining the compare function
4020 * Returns: comparison result
4023 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
4024 GstValueCompareFunc compare)
4028 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
4029 return GST_VALUE_UNORDERED;
4031 return compare (value1, value2);
4037 * gst_value_can_union:
4038 * @value1: a value to union
4039 * @value2: another value to union
4041 * Determines if @value1 and @value2 can be non-trivially unioned.
4042 * Any two values can be trivially unioned by adding both of them
4043 * to a GstValueList. However, certain types have the possibility
4044 * to be unioned in a simpler way. For example, an integer range
4045 * and an integer can be unioned if the integer is a subset of the
4046 * integer range. If there is the possibility that two values can
4047 * be unioned, this function returns TRUE.
4049 * Returns: TRUE if there is a function allowing the two values to
4053 gst_value_can_union (const GValue * value1, const GValue * value2)
4055 GstValueUnionInfo *union_info;
4058 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4059 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4061 len = gst_value_union_funcs->len;
4063 for (i = 0; i < len; i++) {
4064 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4065 if (union_info->type1 == G_VALUE_TYPE (value1) &&
4066 union_info->type2 == G_VALUE_TYPE (value2))
4068 if (union_info->type1 == G_VALUE_TYPE (value2) &&
4069 union_info->type2 == G_VALUE_TYPE (value1))
4078 * @dest: (out caller-allocates): the destination value
4079 * @value1: a value to union
4080 * @value2: another value to union
4082 * Creates a GValue corresponding to the union of @value1 and @value2.
4084 * Returns: TRUE if the union suceeded.
4087 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
4089 const GstValueUnionInfo *union_info;
4093 g_return_val_if_fail (dest != NULL, FALSE);
4094 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4095 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4096 g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
4099 len = gst_value_union_funcs->len;
4100 type1 = G_VALUE_TYPE (value1);
4101 type2 = G_VALUE_TYPE (value2);
4103 for (i = 0; i < len; i++) {
4104 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
4105 if (union_info->type1 == type1 && union_info->type2 == type2) {
4106 return union_info->func (dest, value1, value2);
4108 if (union_info->type1 == type2 && union_info->type2 == type1) {
4109 return union_info->func (dest, value2, value1);
4113 gst_value_list_concat (dest, value1, value2);
4118 * gst_value_register_union_func: (skip)
4119 * @type1: a type to union
4120 * @type2: another type to union
4121 * @func: a function that implements creating a union between the two types
4123 * Registers a union function that can create a union between #GValue items
4124 * of the type @type1 and @type2.
4126 * Union functions should be registered at startup before any pipelines are
4127 * started, as gst_value_register_union_func() is not thread-safe and cannot
4128 * be used at the same time as gst_value_union() or gst_value_can_union().
4131 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
4133 GstValueUnionInfo union_info;
4135 union_info.type1 = type1;
4136 union_info.type2 = type2;
4137 union_info.func = func;
4139 g_array_append_val (gst_value_union_funcs, union_info);
4145 * gst_value_can_intersect:
4146 * @value1: a value to intersect
4147 * @value2: another value to intersect
4149 * Determines if intersecting two values will produce a valid result.
4150 * Two values will produce a valid intersection if they have the same
4151 * type, or if there is a method (registered by
4152 * gst_value_register_intersect_func()) to calculate the intersection.
4154 * Returns: TRUE if the values can intersect
4157 gst_value_can_intersect (const GValue * value1, const GValue * value2)
4159 GstValueIntersectInfo *intersect_info;
4161 GType ltype, type1, type2;
4163 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4164 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4166 ltype = gst_value_list_get_type ();
4169 if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
4172 type1 = G_VALUE_TYPE (value1);
4173 type2 = G_VALUE_TYPE (value2);
4175 /* practically all GstValue types have a compare function (_can_compare=TRUE)
4176 * GstStructure and GstCaps have npot, but are intersectable */
4180 /* check registered intersect functions */
4181 len = gst_value_intersect_funcs->len;
4182 for (i = 0; i < len; i++) {
4183 intersect_info = &g_array_index (gst_value_intersect_funcs,
4184 GstValueIntersectInfo, i);
4185 if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
4186 (intersect_info->type1 == type2 && intersect_info->type2 == type1))
4190 return gst_value_can_compare (value1, value2);
4194 * gst_value_intersect:
4195 * @dest: (out caller-allocates) (transfer full): a uninitialized #GValue that will hold the calculated
4196 * intersection value. May be NULL if the resulting set if not needed.
4197 * @value1: a value to intersect
4198 * @value2: another value to intersect
4200 * Calculates the intersection of two values. If the values have
4201 * a non-empty intersection, the value representing the intersection
4202 * is placed in @dest, unless NULL. If the intersection is non-empty,
4203 * @dest is not modified.
4205 * Returns: TRUE if the intersection is non-empty
4208 gst_value_intersect (GValue * dest, const GValue * value1,
4209 const GValue * value2)
4211 GstValueIntersectInfo *intersect_info;
4213 GType ltype, type1, type2;
4215 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
4216 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
4218 ltype = gst_value_list_get_type ();
4220 /* special cases first */
4221 if (G_VALUE_HOLDS (value1, ltype))
4222 return gst_value_intersect_list (dest, value1, value2);
4223 if (G_VALUE_HOLDS (value2, ltype))
4224 return gst_value_intersect_list (dest, value2, value1);
4226 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
4228 gst_value_init_and_copy (dest, value1);
4232 type1 = G_VALUE_TYPE (value1);
4233 type2 = G_VALUE_TYPE (value2);
4235 len = gst_value_intersect_funcs->len;
4236 for (i = 0; i < len; i++) {
4237 intersect_info = &g_array_index (gst_value_intersect_funcs,
4238 GstValueIntersectInfo, i);
4239 if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
4240 return intersect_info->func (dest, value1, value2);
4242 if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
4243 return intersect_info->func (dest, value2, value1);
4252 * gst_value_register_intersect_func: (skip)
4253 * @type1: the first type to intersect
4254 * @type2: the second type to intersect
4255 * @func: the intersection function
4257 * Registers a function that is called to calculate the intersection
4258 * of the values having the types @type1 and @type2.
4260 * Intersect functions should be registered at startup before any pipelines are
4261 * started, as gst_value_register_intersect_func() is not thread-safe and
4262 * cannot be used at the same time as gst_value_intersect() or
4263 * gst_value_can_intersect().
4266 gst_value_register_intersect_func (GType type1, GType type2,
4267 GstValueIntersectFunc func)
4269 GstValueIntersectInfo intersect_info;
4271 intersect_info.type1 = type1;
4272 intersect_info.type2 = type2;
4273 intersect_info.func = func;
4275 g_array_append_val (gst_value_intersect_funcs, intersect_info);
4282 * gst_value_subtract:
4283 * @dest: (out caller-allocates): the destination value for the result if the
4284 * subtraction is not empty. May be NULL, in which case the resulting set
4285 * will not be computed, which can give a fair speedup.
4286 * @minuend: the value to subtract from
4287 * @subtrahend: the value to subtract
4289 * Subtracts @subtrahend from @minuend and stores the result in @dest.
4290 * Note that this means subtraction as in sets, not as in mathematics.
4292 * Returns: %TRUE if the subtraction is not empty
4295 gst_value_subtract (GValue * dest, const GValue * minuend,
4296 const GValue * subtrahend)
4298 GstValueSubtractInfo *info;
4300 GType ltype, mtype, stype;
4302 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4303 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4305 ltype = gst_value_list_get_type ();
4307 /* special cases first */
4308 if (G_VALUE_HOLDS (minuend, ltype))
4309 return gst_value_subtract_from_list (dest, minuend, subtrahend);
4310 if (G_VALUE_HOLDS (subtrahend, ltype))
4311 return gst_value_subtract_list (dest, minuend, subtrahend);
4313 mtype = G_VALUE_TYPE (minuend);
4314 stype = G_VALUE_TYPE (subtrahend);
4316 len = gst_value_subtract_funcs->len;
4317 for (i = 0; i < len; i++) {
4318 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4319 if (info->minuend == mtype && info->subtrahend == stype) {
4320 return info->func (dest, minuend, subtrahend);
4324 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
4326 gst_value_init_and_copy (dest, minuend);
4335 gst_value_subtract (GValue * dest, const GValue * minuend,
4336 const GValue * subtrahend)
4338 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
4340 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
4341 gst_value_serialize (subtrahend),
4342 ret ? gst_value_serialize (dest) : "---");
4348 * gst_value_can_subtract:
4349 * @minuend: the value to subtract from
4350 * @subtrahend: the value to subtract
4352 * Checks if it's possible to subtract @subtrahend from @minuend.
4354 * Returns: TRUE if a subtraction is possible
4357 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
4359 GstValueSubtractInfo *info;
4361 GType ltype, mtype, stype;
4363 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
4364 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
4366 ltype = gst_value_list_get_type ();
4369 if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
4372 mtype = G_VALUE_TYPE (minuend);
4373 stype = G_VALUE_TYPE (subtrahend);
4375 len = gst_value_subtract_funcs->len;
4376 for (i = 0; i < len; i++) {
4377 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
4378 if (info->minuend == mtype && info->subtrahend == stype)
4382 return gst_value_can_compare (minuend, subtrahend);
4386 * gst_value_register_subtract_func: (skip)
4387 * @minuend_type: type of the minuend
4388 * @subtrahend_type: type of the subtrahend
4389 * @func: function to use
4391 * Registers @func as a function capable of subtracting the values of
4392 * @subtrahend_type from values of @minuend_type.
4394 * Subtract functions should be registered at startup before any pipelines are
4395 * started, as gst_value_register_subtract_func() is not thread-safe and
4396 * cannot be used at the same time as gst_value_subtract().
4399 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
4400 GstValueSubtractFunc func)
4402 GstValueSubtractInfo info;
4404 /* one type must be unfixed, other subtractions can be done as comparisons,
4405 * special case: bitmasks */
4406 if (minuend_type != GST_TYPE_BITMASK)
4407 g_return_if_fail (!gst_type_is_fixed (minuend_type)
4408 || !gst_type_is_fixed (subtrahend_type));
4410 info.minuend = minuend_type;
4411 info.subtrahend = subtrahend_type;
4414 g_array_append_val (gst_value_subtract_funcs, info);
4418 * gst_value_register:
4419 * @table: structure containing functions to register
4421 * Registers functions to perform calculations on #GValue items of a given
4422 * type. Each type can only be added once.
4425 gst_value_register (const GstValueTable * table)
4427 GstValueTable *found;
4429 g_return_if_fail (table != NULL);
4431 g_array_append_val (gst_value_table, *table);
4433 found = gst_value_hash_lookup_type (table->type);
4435 g_warning ("adding type %s multiple times", g_type_name (table->type));
4437 /* FIXME: we're not really doing the const justice, we assume the table is
4439 gst_value_hash_add_type (table->type, table);
4443 * gst_value_init_and_copy:
4444 * @dest: (out caller-allocates): the target value
4445 * @src: the source value
4447 * Initialises the target value to be of the same type as source and then copies
4448 * the contents from source to target.
4451 gst_value_init_and_copy (GValue * dest, const GValue * src)
4453 g_return_if_fail (G_IS_VALUE (src));
4454 g_return_if_fail (dest != NULL);
4456 g_value_init (dest, G_VALUE_TYPE (src));
4457 g_value_copy (src, dest);
4461 * gst_value_serialize:
4462 * @value: a #GValue to serialize
4464 * tries to transform the given @value into a string representation that allows
4465 * getting back this string later on using gst_value_deserialize().
4467 * Free-function: g_free
4469 * Returns: (transfer full): the serialization for @value or NULL if none exists
4472 gst_value_serialize (const GValue * value)
4475 GValue s_val = { 0 };
4476 GstValueTable *table, *best;
4480 g_return_val_if_fail (G_IS_VALUE (value), NULL);
4482 type = G_VALUE_TYPE (value);
4484 best = gst_value_hash_lookup_type (type);
4486 if (G_UNLIKELY (!best || !best->serialize)) {
4487 len = gst_value_table->len;
4489 for (i = 0; i < len; i++) {
4490 table = &g_array_index (gst_value_table, GstValueTable, i);
4491 if (table->serialize && g_type_is_a (type, table->type)) {
4492 if (!best || g_type_is_a (table->type, best->type))
4497 if (G_LIKELY (best))
4498 return best->serialize (value);
4500 g_value_init (&s_val, G_TYPE_STRING);
4501 if (g_value_transform (value, &s_val)) {
4502 s = gst_string_wrap (g_value_get_string (&s_val));
4506 g_value_unset (&s_val);
4512 * gst_value_deserialize:
4513 * @dest: (out caller-allocates): #GValue to fill with contents of
4515 * @src: string to deserialize
4517 * Tries to deserialize a string into the type specified by the given GValue.
4518 * If the operation succeeds, TRUE is returned, FALSE otherwise.
4520 * Returns: TRUE on success
4523 gst_value_deserialize (GValue * dest, const gchar * src)
4525 GstValueTable *table, *best;
4529 g_return_val_if_fail (src != NULL, FALSE);
4530 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
4532 type = G_VALUE_TYPE (dest);
4534 best = gst_value_hash_lookup_type (type);
4535 if (G_UNLIKELY (!best || !best->deserialize)) {
4536 len = gst_value_table->len;
4538 for (i = 0; i < len; i++) {
4539 table = &g_array_index (gst_value_table, GstValueTable, i);
4540 if (table->deserialize && g_type_is_a (type, table->type)) {
4541 if (!best || g_type_is_a (table->type, best->type))
4546 if (G_LIKELY (best))
4547 return best->deserialize (dest, src);
4553 * gst_value_is_fixed:
4554 * @value: the #GValue to check
4556 * Tests if the given GValue, if available in a GstStructure (or any other
4557 * container) contains a "fixed" (which means: one value) or an "unfixed"
4558 * (which means: multiple possible values, such as data lists or data
4561 * Returns: true if the value is "fixed".
4565 gst_value_is_fixed (const GValue * value)
4569 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
4571 type = G_VALUE_TYPE (value);
4573 /* the most common types are just basic plain glib types */
4574 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
4578 if (type == GST_TYPE_ARRAY) {
4582 /* check recursively */
4583 size = gst_value_array_get_size (value);
4584 for (n = 0; n < size; n++) {
4585 kid = gst_value_array_get_value (value, n);
4586 if (!gst_value_is_fixed (kid))
4591 return gst_type_is_fixed (type);
4596 * @dest: the #GValue destination
4597 * @src: the #GValue to fixate
4599 * Fixate @src into a new value @dest.
4600 * For ranges, the first element is taken. For lists and arrays, the
4601 * first item is fixated and returned.
4602 * If @src is already fixed, this function returns FALSE.
4604 * Returns: true if @dest contains a fixated version of @src.
4607 gst_value_fixate (GValue * dest, const GValue * src)
4609 g_return_val_if_fail (G_IS_VALUE (src), FALSE);
4610 g_return_val_if_fail (dest != NULL, FALSE);
4612 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
4613 g_value_init (dest, G_TYPE_INT);
4614 g_value_set_int (dest, gst_value_get_int_range_min (src));
4615 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
4616 g_value_init (dest, G_TYPE_DOUBLE);
4617 g_value_set_double (dest, gst_value_get_double_range_min (src));
4618 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
4619 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
4620 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
4621 GValue temp = { 0 };
4623 /* list could be empty */
4624 if (gst_value_list_get_size (src) <= 0)
4627 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
4629 if (!gst_value_fixate (dest, &temp))
4630 gst_value_init_and_copy (dest, &temp);
4631 g_value_unset (&temp);
4632 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
4633 gboolean res = FALSE;
4636 len = gst_value_array_get_size (src);
4637 g_value_init (dest, GST_TYPE_ARRAY);
4638 for (n = 0; n < len; n++) {
4640 const GValue *orig_kid = gst_value_array_get_value (src, n);
4642 if (!gst_value_fixate (&kid, orig_kid))
4643 gst_value_init_and_copy (&kid, orig_kid);
4646 gst_value_array_append_value (dest, &kid);
4647 g_value_unset (&kid);
4651 g_value_unset (dest);
4665 /* helper functions */
4667 gst_value_init_fraction (GValue * value)
4669 value->data[0].v_int = 0;
4670 value->data[1].v_int = 1;
4674 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
4676 dest_value->data[0].v_int = src_value->data[0].v_int;
4677 dest_value->data[1].v_int = src_value->data[1].v_int;
4681 gst_value_collect_fraction (GValue * value, guint n_collect_values,
4682 GTypeCValue * collect_values, guint collect_flags)
4684 if (n_collect_values != 2)
4685 return g_strdup_printf ("not enough value locations for `%s' passed",
4686 G_VALUE_TYPE_NAME (value));
4687 if (collect_values[1].v_int == 0)
4688 return g_strdup_printf ("passed '0' as denominator for `%s'",
4689 G_VALUE_TYPE_NAME (value));
4690 if (collect_values[0].v_int < -G_MAXINT)
4693 ("passed value smaller than -G_MAXINT as numerator for `%s'",
4694 G_VALUE_TYPE_NAME (value));
4695 if (collect_values[1].v_int < -G_MAXINT)
4698 ("passed value smaller than -G_MAXINT as denominator for `%s'",
4699 G_VALUE_TYPE_NAME (value));
4701 gst_value_set_fraction (value,
4702 collect_values[0].v_int, collect_values[1].v_int);
4708 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
4709 GTypeCValue * collect_values, guint collect_flags)
4711 gint *numerator = collect_values[0].v_pointer;
4712 gint *denominator = collect_values[1].v_pointer;
4715 return g_strdup_printf ("numerator for `%s' passed as NULL",
4716 G_VALUE_TYPE_NAME (value));
4718 return g_strdup_printf ("denominator for `%s' passed as NULL",
4719 G_VALUE_TYPE_NAME (value));
4721 *numerator = value->data[0].v_int;
4722 *denominator = value->data[1].v_int;
4728 * gst_value_set_fraction:
4729 * @value: a GValue initialized to #GST_TYPE_FRACTION
4730 * @numerator: the numerator of the fraction
4731 * @denominator: the denominator of the fraction
4733 * Sets @value to the fraction specified by @numerator over @denominator.
4734 * The fraction gets reduced to the smallest numerator and denominator,
4735 * and if necessary the sign is moved to the numerator.
4738 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
4742 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
4743 g_return_if_fail (denominator != 0);
4744 g_return_if_fail (denominator >= -G_MAXINT);
4745 g_return_if_fail (numerator >= -G_MAXINT);
4747 /* normalize sign */
4748 if (denominator < 0) {
4749 numerator = -numerator;
4750 denominator = -denominator;
4753 /* check for reduction */
4754 gcd = gst_util_greatest_common_divisor (numerator, denominator);
4760 g_assert (denominator > 0);
4762 value->data[0].v_int = numerator;
4763 value->data[1].v_int = denominator;
4767 * gst_value_get_fraction_numerator:
4768 * @value: a GValue initialized to #GST_TYPE_FRACTION
4770 * Gets the numerator of the fraction specified by @value.
4772 * Returns: the numerator of the fraction.
4775 gst_value_get_fraction_numerator (const GValue * value)
4777 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
4779 return value->data[0].v_int;
4783 * gst_value_get_fraction_denominator:
4784 * @value: a GValue initialized to #GST_TYPE_FRACTION
4786 * Gets the denominator of the fraction specified by @value.
4788 * Returns: the denominator of the fraction.
4791 gst_value_get_fraction_denominator (const GValue * value)
4793 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
4795 return value->data[1].v_int;
4799 * gst_value_fraction_multiply:
4800 * @product: a GValue initialized to #GST_TYPE_FRACTION
4801 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
4802 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
4804 * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
4805 * @product to the product of the two fractions.
4807 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4810 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
4811 const GValue * factor2)
4813 gint n1, n2, d1, d2;
4816 g_return_val_if_fail (product != NULL, FALSE);
4817 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
4818 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
4820 n1 = factor1->data[0].v_int;
4821 n2 = factor2->data[0].v_int;
4822 d1 = factor1->data[1].v_int;
4823 d2 = factor2->data[1].v_int;
4825 if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
4828 gst_value_set_fraction (product, res_n, res_d);
4834 * gst_value_fraction_subtract:
4835 * @dest: a GValue initialized to #GST_TYPE_FRACTION
4836 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
4837 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
4839 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
4841 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4844 gst_value_fraction_subtract (GValue * dest,
4845 const GValue * minuend, const GValue * subtrahend)
4847 gint n1, n2, d1, d2;
4850 g_return_val_if_fail (dest != NULL, FALSE);
4851 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
4852 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
4854 n1 = minuend->data[0].v_int;
4855 n2 = subtrahend->data[0].v_int;
4856 d1 = minuend->data[1].v_int;
4857 d2 = subtrahend->data[1].v_int;
4859 if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
4861 gst_value_set_fraction (dest, res_n, res_d);
4867 gst_value_serialize_fraction (const GValue * value)
4869 gint32 numerator = value->data[0].v_int;
4870 gint32 denominator = value->data[1].v_int;
4871 gboolean positive = TRUE;
4873 /* get the sign and make components absolute */
4874 if (numerator < 0) {
4875 numerator = -numerator;
4876 positive = !positive;
4878 if (denominator < 0) {
4879 denominator = -denominator;
4880 positive = !positive;
4883 return g_strdup_printf ("%s%d/%d",
4884 positive ? "" : "-", numerator, denominator);
4888 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
4893 if (G_UNLIKELY (s == NULL))
4896 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
4899 if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
4900 if (s[num_chars] != 0)
4905 gst_value_set_fraction (dest, num, den);
4907 } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
4908 gst_value_set_fraction (dest, 1, G_MAXINT);
4910 } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
4911 if (s[num_chars] != 0)
4913 gst_value_set_fraction (dest, num, 1);
4915 } else if (g_ascii_strcasecmp (s, "min") == 0) {
4916 gst_value_set_fraction (dest, -G_MAXINT, 1);
4918 } else if (g_ascii_strcasecmp (s, "max") == 0) {
4919 gst_value_set_fraction (dest, G_MAXINT, 1);
4927 gst_value_transform_fraction_string (const GValue * src_value,
4928 GValue * dest_value)
4930 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
4934 gst_value_transform_string_fraction (const GValue * src_value,
4935 GValue * dest_value)
4937 if (!gst_value_deserialize_fraction (dest_value,
4938 src_value->data[0].v_pointer))
4939 /* If the deserialize fails, ensure we leave the fraction in a
4940 * valid, if incorrect, state */
4941 gst_value_set_fraction (dest_value, 0, 1);
4945 gst_value_transform_double_fraction (const GValue * src_value,
4946 GValue * dest_value)
4948 gdouble src = g_value_get_double (src_value);
4951 gst_util_double_to_fraction (src, &n, &d);
4952 gst_value_set_fraction (dest_value, n, d);
4956 gst_value_transform_float_fraction (const GValue * src_value,
4957 GValue * dest_value)
4959 gfloat src = g_value_get_float (src_value);
4962 gst_util_double_to_fraction (src, &n, &d);
4963 gst_value_set_fraction (dest_value, n, d);
4967 gst_value_transform_fraction_double (const GValue * src_value,
4968 GValue * dest_value)
4970 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
4971 ((double) src_value->data[1].v_int);
4975 gst_value_transform_fraction_float (const GValue * src_value,
4976 GValue * dest_value)
4978 dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
4979 ((float) src_value->data[1].v_int);
4983 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
4989 n1 = value1->data[0].v_int;
4990 n2 = value2->data[0].v_int;
4991 d1 = value1->data[1].v_int;
4992 d2 = value2->data[1].v_int;
4994 /* fractions are reduced when set, so we can quickly see if they're equal */
4995 if (n1 == n2 && d1 == d2)
4996 return GST_VALUE_EQUAL;
4998 if (d1 == 0 && d2 == 0)
4999 return GST_VALUE_UNORDERED;
5001 return GST_VALUE_GREATER_THAN;
5003 return GST_VALUE_LESS_THAN;
5005 ret = gst_util_fraction_compare (n1, d1, n2, d2);
5007 return GST_VALUE_LESS_THAN;
5009 return GST_VALUE_GREATER_THAN;
5011 /* Equality can't happen here because we check for that
5013 g_return_val_if_reached (GST_VALUE_UNORDERED);
5021 gst_value_compare_date (const GValue * value1, const GValue * value2)
5023 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
5024 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
5028 return GST_VALUE_EQUAL;
5030 if ((date1 == NULL || !g_date_valid (date1))
5031 && (date2 != NULL && g_date_valid (date2))) {
5032 return GST_VALUE_LESS_THAN;
5035 if ((date2 == NULL || !g_date_valid (date2))
5036 && (date1 != NULL && g_date_valid (date1))) {
5037 return GST_VALUE_GREATER_THAN;
5040 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
5041 || !g_date_valid (date2)) {
5042 return GST_VALUE_UNORDERED;
5045 j1 = g_date_get_julian (date1);
5046 j2 = g_date_get_julian (date2);
5049 return GST_VALUE_EQUAL;
5051 return GST_VALUE_LESS_THAN;
5053 return GST_VALUE_GREATER_THAN;
5057 gst_value_serialize_date (const GValue * val)
5059 const GDate *date = (const GDate *) g_value_get_boxed (val);
5061 if (date == NULL || !g_date_valid (date))
5062 return g_strdup ("9999-99-99");
5064 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
5065 g_date_get_month (date), g_date_get_day (date));
5069 gst_value_deserialize_date (GValue * dest, const gchar * s)
5071 guint year, month, day;
5073 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
5076 if (!g_date_valid_dmy (day, month, year))
5079 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
5088 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
5090 const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
5091 const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
5095 return GST_VALUE_EQUAL;
5097 if ((date1 == NULL) && (date2 != NULL)) {
5098 return GST_VALUE_LESS_THAN;
5100 if ((date2 == NULL) && (date1 != NULL)) {
5101 return GST_VALUE_LESS_THAN;
5104 ret = priv_gst_date_time_compare (date1, date2);
5107 return GST_VALUE_EQUAL;
5109 return GST_VALUE_LESS_THAN;
5111 return GST_VALUE_GREATER_THAN;
5115 gst_value_serialize_date_time (const GValue * val)
5117 GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
5119 gint tzhour, tzminute;
5122 return g_strdup ("null");
5124 offset = gst_date_time_get_time_zone_offset (date);
5126 tzhour = (gint) ABS (offset);
5127 tzminute = (gint) ((ABS (offset) - tzhour) * 60);
5129 return g_strdup_printf ("\"%04d-%02d-%02dT%02d:%02d:%02d.%06d"
5130 "%c%02d%02d\"", gst_date_time_get_year (date),
5131 gst_date_time_get_month (date), gst_date_time_get_day (date),
5132 gst_date_time_get_hour (date), gst_date_time_get_minute (date),
5133 gst_date_time_get_second (date), gst_date_time_get_microsecond (date),
5134 offset >= 0 ? '+' : '-', tzhour, tzminute);
5138 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
5140 gint year, month, day, hour, minute, second, usecond;
5143 gfloat tzoffset = 0;
5146 if (!s || strcmp (s, "null") == 0) {
5150 ret = sscanf (s, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%04d",
5151 &year, &month, &day, &hour, &minute, &second, &usecond, &signal, &offset);
5153 tzoffset = (offset / 100) + ((offset % 100) / 60.0);
5155 tzoffset = -tzoffset;
5159 g_value_take_boxed (dest, gst_date_time_new (tzoffset, year, month, day, hour,
5160 minute, second + (usecond / 1000000.0)));
5165 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
5167 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
5171 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
5173 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
5181 /* helper functions */
5183 gst_value_init_bitmask (GValue * value)
5185 value->data[0].v_uint64 = 0;
5189 gst_value_copy_bitmask (const GValue * src_value, GValue * dest_value)
5191 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5195 gst_value_collect_bitmask (GValue * value, guint n_collect_values,
5196 GTypeCValue * collect_values, guint collect_flags)
5198 if (n_collect_values != 1)
5199 return g_strdup_printf ("not enough value locations for `%s' passed",
5200 G_VALUE_TYPE_NAME (value));
5202 gst_value_set_bitmask (value, (guint64) collect_values[0].v_int64);
5208 gst_value_lcopy_bitmask (const GValue * value, guint n_collect_values,
5209 GTypeCValue * collect_values, guint collect_flags)
5211 guint64 *bitmask = collect_values[0].v_pointer;
5214 return g_strdup_printf ("value for `%s' passed as NULL",
5215 G_VALUE_TYPE_NAME (value));
5217 *bitmask = value->data[0].v_uint64;
5223 * gst_value_set_bitmask:
5224 * @value: a GValue initialized to #GST_TYPE_FRACTION
5225 * @bitmask: the bitmask
5227 * Sets @value to the bitmask specified by @bitmask.
5230 gst_value_set_bitmask (GValue * value, guint64 bitmask)
5232 g_return_if_fail (GST_VALUE_HOLDS_BITMASK (value));
5234 value->data[0].v_uint64 = bitmask;
5238 * gst_value_get_bitmask:
5239 * @value: a GValue initialized to #GST_TYPE_FRACTION
5241 * Gets the bitmask specified by @value.
5243 * Returns: the bitmask.
5246 gst_value_get_bitmask (const GValue * value)
5248 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (value), 0);
5250 return value->data[0].v_uint64;
5254 gst_value_serialize_bitmask (const GValue * value)
5256 guint64 bitmask = value->data[0].v_uint64;
5258 return g_strdup_printf ("0x%016" G_GINT64_MODIFIER "x", bitmask);
5262 gst_value_deserialize_bitmask (GValue * dest, const gchar * s)
5264 gchar *endptr = NULL;
5267 if (G_UNLIKELY (s == NULL))
5270 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_BITMASK (dest)))
5273 val = g_ascii_strtoull (s, &endptr, 16);
5274 if (val == G_MAXUINT64 && (errno == ERANGE || errno == EINVAL))
5276 if (val == 0 && endptr == s)
5279 gst_value_set_bitmask (dest, val);
5285 gst_value_transform_bitmask_string (const GValue * src_value,
5286 GValue * dest_value)
5288 dest_value->data[0].v_pointer = gst_value_serialize_bitmask (src_value);
5292 gst_value_transform_string_bitmask (const GValue * src_value,
5293 GValue * dest_value)
5295 if (!gst_value_deserialize_bitmask (dest_value, src_value->data[0].v_pointer))
5296 gst_value_set_bitmask (dest_value, 0);
5300 gst_value_transform_uint64_bitmask (const GValue * src_value,
5301 GValue * dest_value)
5303 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5307 gst_value_transform_bitmask_uint64 (const GValue * src_value,
5308 GValue * dest_value)
5310 dest_value->data[0].v_uint64 = src_value->data[0].v_uint64;
5314 gst_value_intersect_bitmask_bitmask (GValue * dest, const GValue * src1,
5315 const GValue * src2)
5319 s1 = gst_value_get_bitmask (src1);
5320 s2 = gst_value_get_bitmask (src2);
5323 g_value_init (dest, GST_TYPE_BITMASK);
5324 gst_value_set_bitmask (dest, s1 & s2);
5331 gst_value_union_bitmask_bitmask (GValue * dest, const GValue * src1,
5332 const GValue * src2)
5336 s1 = gst_value_get_bitmask (src1);
5337 s2 = gst_value_get_bitmask (src2);
5339 g_value_init (dest, GST_TYPE_BITMASK);
5340 gst_value_set_bitmask (dest, s1 | s2);
5346 gst_value_subtract_bitmask_bitmask (GValue * dest,
5347 const GValue * minuend, const GValue * subtrahend)
5351 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (minuend), FALSE);
5352 g_return_val_if_fail (GST_VALUE_HOLDS_BITMASK (subtrahend), FALSE);
5354 m = minuend->data[0].v_uint64;
5355 s = subtrahend->data[0].v_uint64;
5359 g_value_init (dest, GST_TYPE_BITMASK);
5360 gst_value_set_bitmask (dest, r);
5366 gst_value_compare_bitmask (const GValue * value1, const GValue * value2)
5370 v1 = value1->data[0].v_uint64;
5371 v2 = value2->data[0].v_uint64;
5374 return GST_VALUE_EQUAL;
5376 return GST_VALUE_UNORDERED;
5380 gst_value_transform_object_string (const GValue * src_value,
5381 GValue * dest_value)
5386 obj = g_value_get_object (src_value);
5389 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
5390 GST_OBJECT_NAME (obj));
5392 str = g_strdup ("NULL");
5395 dest_value->data[0].v_pointer = str;
5398 static GTypeInfo _info = {
5411 static GTypeFundamentalInfo _finfo = {
5415 #define FUNC_VALUE_GET_TYPE(type, name) \
5416 GType gst_ ## type ## _get_type (void) \
5418 static volatile GType gst_ ## type ## _type = 0; \
5420 if (g_once_init_enter (&gst_ ## type ## _type)) { \
5422 _info.value_table = & _gst_ ## type ## _value_table; \
5423 _type = g_type_register_fundamental ( \
5424 g_type_fundamental_next (), \
5425 name, &_info, &_finfo, 0); \
5426 g_once_init_leave(&gst_ ## type ## _type, _type); \
5429 return gst_ ## type ## _type; \
5432 static const GTypeValueTable _gst_int_range_value_table = {
5433 gst_value_init_int_range,
5434 gst_value_free_int_range,
5435 gst_value_copy_int_range,
5438 gst_value_collect_int_range,
5440 gst_value_lcopy_int_range
5443 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
5445 static const GTypeValueTable _gst_int64_range_value_table = {
5446 gst_value_init_int64_range,
5447 gst_value_free_int64_range,
5448 gst_value_copy_int64_range,
5451 gst_value_collect_int64_range,
5453 gst_value_lcopy_int64_range
5456 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
5458 static const GTypeValueTable _gst_double_range_value_table = {
5459 gst_value_init_double_range,
5461 gst_value_copy_double_range,
5464 gst_value_collect_double_range,
5466 gst_value_lcopy_double_range
5469 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
5471 static const GTypeValueTable _gst_fraction_range_value_table = {
5472 gst_value_init_fraction_range,
5473 gst_value_free_fraction_range,
5474 gst_value_copy_fraction_range,
5477 gst_value_collect_fraction_range,
5479 gst_value_lcopy_fraction_range
5482 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
5484 static const GTypeValueTable _gst_value_list_value_table = {
5485 gst_value_init_list_or_array,
5486 gst_value_free_list_or_array,
5487 gst_value_copy_list_or_array,
5488 gst_value_list_or_array_peek_pointer,
5490 gst_value_collect_list_or_array,
5492 gst_value_lcopy_list_or_array
5495 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
5497 static const GTypeValueTable _gst_value_array_value_table = {
5498 gst_value_init_list_or_array,
5499 gst_value_free_list_or_array,
5500 gst_value_copy_list_or_array,
5501 gst_value_list_or_array_peek_pointer,
5503 gst_value_collect_list_or_array,
5505 gst_value_lcopy_list_or_array
5508 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
5510 static const GTypeValueTable _gst_fraction_value_table = {
5511 gst_value_init_fraction,
5513 gst_value_copy_fraction,
5516 gst_value_collect_fraction,
5518 gst_value_lcopy_fraction
5521 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
5523 G_DEFINE_BOXED_TYPE (GstDateTime, gst_date_time,
5524 (GBoxedCopyFunc) gst_date_time_ref, (GBoxedFreeFunc) gst_date_time_unref);
5526 static const GTypeValueTable _gst_bitmask_value_table = {
5527 gst_value_init_bitmask,
5529 gst_value_copy_bitmask,
5532 gst_value_collect_bitmask,
5534 gst_value_lcopy_bitmask
5537 FUNC_VALUE_GET_TYPE (bitmask, "GstBitmask");
5541 _priv_gst_value_initialize (void)
5543 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
5544 gst_value_hash = g_hash_table_new (NULL, NULL);
5545 gst_value_union_funcs = g_array_new (FALSE, FALSE,
5546 sizeof (GstValueUnionInfo));
5547 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
5548 sizeof (GstValueIntersectInfo));
5549 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
5550 sizeof (GstValueSubtractInfo));
5553 static GstValueTable gst_value = {
5555 gst_value_compare_int_range,
5556 gst_value_serialize_int_range,
5557 gst_value_deserialize_int_range,
5560 gst_value.type = gst_int_range_get_type ();
5561 gst_value_register (&gst_value);
5565 static GstValueTable gst_value = {
5567 gst_value_compare_int64_range,
5568 gst_value_serialize_int64_range,
5569 gst_value_deserialize_int64_range,
5572 gst_value.type = gst_int64_range_get_type ();
5573 gst_value_register (&gst_value);
5577 static GstValueTable gst_value = {
5579 gst_value_compare_double_range,
5580 gst_value_serialize_double_range,
5581 gst_value_deserialize_double_range,
5584 gst_value.type = gst_double_range_get_type ();
5585 gst_value_register (&gst_value);
5589 static GstValueTable gst_value = {
5591 gst_value_compare_fraction_range,
5592 gst_value_serialize_fraction_range,
5593 gst_value_deserialize_fraction_range,
5596 gst_value.type = gst_fraction_range_get_type ();
5597 gst_value_register (&gst_value);
5601 static GstValueTable gst_value = {
5603 gst_value_compare_list,
5604 gst_value_serialize_list,
5605 gst_value_deserialize_list,
5608 gst_value.type = gst_value_list_get_type ();
5609 gst_value_register (&gst_value);
5613 static GstValueTable gst_value = {
5615 gst_value_compare_array,
5616 gst_value_serialize_array,
5617 gst_value_deserialize_array,
5620 gst_value.type = gst_value_array_get_type ();
5621 gst_value_register (&gst_value);
5626 static const GTypeValueTable value_table = {
5627 gst_value_init_buffer,
5629 gst_value_copy_buffer,
5632 NULL, /*gst_value_collect_buffer, */
5634 NULL /*gst_value_lcopy_buffer */
5637 static GstValueTable gst_value = {
5639 gst_value_compare_buffer,
5640 gst_value_serialize_buffer,
5641 gst_value_deserialize_buffer,
5644 gst_value.type = GST_TYPE_BUFFER;
5645 gst_value_register (&gst_value);
5648 static GstValueTable gst_value = {
5650 gst_value_compare_fraction,
5651 gst_value_serialize_fraction,
5652 gst_value_deserialize_fraction,
5655 gst_value.type = gst_fraction_get_type ();
5656 gst_value_register (&gst_value);
5659 static GstValueTable gst_value = {
5662 gst_value_serialize_caps,
5663 gst_value_deserialize_caps,
5666 gst_value.type = GST_TYPE_CAPS;
5667 gst_value_register (&gst_value);
5670 static GstValueTable gst_value = {
5673 gst_value_serialize_structure,
5674 gst_value_deserialize_structure,
5677 gst_value.type = GST_TYPE_STRUCTURE;
5678 gst_value_register (&gst_value);
5681 static GstValueTable gst_value = {
5683 gst_value_compare_date,
5684 gst_value_serialize_date,
5685 gst_value_deserialize_date,
5688 gst_value.type = G_TYPE_DATE;
5689 gst_value_register (&gst_value);
5692 static GstValueTable gst_value = {
5694 gst_value_compare_date_time,
5695 gst_value_serialize_date_time,
5696 gst_value_deserialize_date_time,
5699 gst_value.type = gst_date_time_get_type ();
5700 gst_value_register (&gst_value);
5704 static GstValueTable gst_value = {
5706 gst_value_compare_bitmask,
5707 gst_value_serialize_bitmask,
5708 gst_value_deserialize_bitmask,
5711 gst_value.type = gst_bitmask_get_type ();
5712 gst_value_register (&gst_value);
5715 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
5716 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
5718 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
5719 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
5720 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
5722 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
5724 REGISTER_SERIALIZATION (G_TYPE_INT, int);
5726 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
5727 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
5729 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
5730 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
5731 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
5733 REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
5735 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
5736 gst_value_transform_int_range_string);
5737 g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
5738 gst_value_transform_int64_range_string);
5739 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
5740 gst_value_transform_double_range_string);
5741 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
5742 gst_value_transform_fraction_range_string);
5743 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
5744 gst_value_transform_list_string);
5745 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
5746 gst_value_transform_array_string);
5747 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
5748 gst_value_transform_fraction_string);
5749 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
5750 gst_value_transform_string_fraction);
5751 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
5752 gst_value_transform_fraction_double);
5753 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
5754 gst_value_transform_fraction_float);
5755 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
5756 gst_value_transform_double_fraction);
5757 g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
5758 gst_value_transform_float_fraction);
5759 g_value_register_transform_func (G_TYPE_DATE, G_TYPE_STRING,
5760 gst_value_transform_date_string);
5761 g_value_register_transform_func (G_TYPE_STRING, G_TYPE_DATE,
5762 gst_value_transform_string_date);
5763 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
5764 gst_value_transform_object_string);
5765 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_UINT64,
5766 gst_value_transform_bitmask_uint64);
5767 g_value_register_transform_func (GST_TYPE_BITMASK, G_TYPE_STRING,
5768 gst_value_transform_bitmask_string);
5769 g_value_register_transform_func (G_TYPE_UINT64, GST_TYPE_BITMASK,
5770 gst_value_transform_uint64_bitmask);
5771 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_BITMASK,
5772 gst_value_transform_string_bitmask);
5774 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5775 gst_value_intersect_int_int_range);
5776 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5777 gst_value_intersect_int_range_int_range);
5778 gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5779 gst_value_intersect_int64_int64_range);
5780 gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5781 gst_value_intersect_int64_range_int64_range);
5782 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5783 gst_value_intersect_double_double_range);
5784 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
5785 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
5786 gst_value_register_intersect_func (GST_TYPE_ARRAY,
5787 GST_TYPE_ARRAY, gst_value_intersect_array);
5788 gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5789 gst_value_intersect_fraction_fraction_range);
5790 gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
5791 GST_TYPE_FRACTION_RANGE,
5792 gst_value_intersect_fraction_range_fraction_range);
5793 gst_value_register_intersect_func (GST_TYPE_BITMASK,
5794 GST_TYPE_BITMASK, gst_value_intersect_bitmask_bitmask);
5796 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5797 gst_value_subtract_int_int_range);
5798 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
5799 gst_value_subtract_int_range_int);
5800 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5801 gst_value_subtract_int_range_int_range);
5802 gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5803 gst_value_subtract_int64_int64_range);
5804 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
5805 gst_value_subtract_int64_range_int64);
5806 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5807 gst_value_subtract_int64_range_int64_range);
5808 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5809 gst_value_subtract_double_double_range);
5810 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
5811 gst_value_subtract_double_range_double);
5812 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
5813 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
5814 gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5815 gst_value_subtract_fraction_fraction_range);
5816 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
5817 gst_value_subtract_fraction_range_fraction);
5818 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
5819 GST_TYPE_FRACTION_RANGE,
5820 gst_value_subtract_fraction_range_fraction_range);
5821 gst_value_register_subtract_func (GST_TYPE_BITMASK,
5822 GST_TYPE_BITMASK, gst_value_subtract_bitmask_bitmask);
5824 /* see bug #317246, #64994, #65041 */
5826 volatile GType date_type = G_TYPE_DATE;
5828 g_type_name (date_type);
5831 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5832 gst_value_union_int_int_range);
5833 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5834 gst_value_union_int_range_int_range);
5835 gst_value_register_union_func (GST_TYPE_BITMASK,
5836 GST_TYPE_BITMASK, gst_value_union_bitmask_bitmask);
5839 /* Implement these if needed */
5840 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5841 gst_value_union_fraction_fraction_range);
5842 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
5843 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);