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 and GParamSpec implementations specific
25 * GValue and GParamSpec implementations specific to GStreamer.
27 * Note that operations on the same GstValue (or GValue) from multiple
28 * threads may lead to undefined behaviour.
30 * Last reviewed on 2006-03-07 (0.10.4)
41 #include "gst_private.h"
42 #include "glib-compat-private.h"
44 #include <gobject/gvaluecollector.h>
46 typedef struct _GstValueUnionInfo GstValueUnionInfo;
47 struct _GstValueUnionInfo
51 GstValueUnionFunc func;
54 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
55 struct _GstValueIntersectInfo
59 GstValueIntersectFunc func;
62 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
63 struct _GstValueSubtractInfo
67 GstValueSubtractFunc func;
70 static GArray *gst_value_table;
71 static GArray *gst_value_union_funcs;
72 static GArray *gst_value_intersect_funcs;
73 static GArray *gst_value_subtract_funcs;
75 /* Forward declarations */
76 static gint gst_greatest_common_divisor (gint a, gint b);
77 static gchar *gst_value_serialize_fraction (const GValue * value);
79 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
80 static gint gst_value_compare_with_func (const GValue * value1,
81 const GValue * value2, GstValueCompareFunc compare);
87 /* two helper functions to serialize/stringify any type of list
88 * regular lists are done with { }, arrays with < >
91 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
95 GArray *array = value->data[0].v_pointer;
100 s = g_string_new (begin);
101 for (i = 0; i < array->len; i++) {
102 v = &g_array_index (array, GValue, i);
103 s_val = gst_value_serialize (v);
104 g_string_append (s, s_val);
106 if (i < array->len - 1) {
107 g_string_append (s, ", ");
110 g_string_append (s, end);
111 return g_string_free (s, FALSE);
115 gst_value_transform_any_list_string (const GValue * src_value,
116 GValue * dest_value, const gchar * begin, const gchar * end)
124 array = src_value->data[0].v_pointer;
126 s = g_string_new (begin);
127 for (i = 0; i < array->len; i++) {
128 list_value = &g_array_index (array, GValue, i);
131 g_string_append (s, ", ");
133 list_s = g_strdup_value_contents (list_value);
134 g_string_append (s, list_s);
137 g_string_append (s, end);
139 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
143 * helper function to see if a type is fixed. Is used internally here and
144 * there. Do not export, since it doesn't work for types where the content
145 * decides the fixedness (e.g. GST_TYPE_ARRAY).
149 gst_type_is_fixed (GType type)
151 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
152 type == GST_TYPE_LIST) {
155 if (G_TYPE_FUNDAMENTAL (type) <=
156 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
159 if (type == GST_TYPE_BUFFER || type == GST_TYPE_FOURCC
160 || type == GST_TYPE_ARRAY || type == GST_TYPE_FRACTION) {
167 /* GValue functions usable for both regular lists and arrays */
169 gst_value_init_list_or_array (GValue * value)
171 value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
175 copy_garray_of_gstvalue (const GArray * src)
180 dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
181 g_array_set_size (dest, src->len);
182 for (i = 0; i < src->len; i++) {
183 gst_value_init_and_copy (&g_array_index (dest, GValue, i),
184 &g_array_index (src, GValue, i));
191 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
193 dest_value->data[0].v_pointer =
194 copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
198 gst_value_free_list_or_array (GValue * value)
201 GArray *src = (GArray *) value->data[0].v_pointer;
203 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
204 for (i = 0; i < src->len; i++) {
205 g_value_unset (&g_array_index (src, GValue, i));
207 g_array_free (src, TRUE);
212 gst_value_list_or_array_peek_pointer (const GValue * value)
214 return value->data[0].v_pointer;
218 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
219 GTypeCValue * collect_values, guint collect_flags)
221 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
222 value->data[0].v_pointer = collect_values[0].v_pointer;
223 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
225 value->data[0].v_pointer =
226 copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
232 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
233 GTypeCValue * collect_values, guint collect_flags)
235 GArray **dest = collect_values[0].v_pointer;
238 return g_strdup_printf ("value location for `%s' passed as NULL",
239 G_VALUE_TYPE_NAME (value));
240 if (!value->data[0].v_pointer)
241 return g_strdup_printf ("invalid value given for `%s'",
242 G_VALUE_TYPE_NAME (value));
243 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
244 *dest = (GArray *) value->data[0].v_pointer;
246 *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
252 * gst_value_list_append_value:
253 * @value: a #GValue of type #GST_TYPE_LIST
254 * @append_value: the value to append
256 * Appends @append_value to the GstValueList in @value.
259 gst_value_list_append_value (GValue * value, const GValue * append_value)
263 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
265 gst_value_init_and_copy (&val, append_value);
266 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
270 * gst_value_list_prepend_value:
271 * @value: a #GValue of type #GST_TYPE_LIST
272 * @prepend_value: the value to prepend
274 * Prepends @prepend_value to the GstValueList in @value.
277 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
281 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
283 gst_value_init_and_copy (&val, prepend_value);
284 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
288 * gst_value_list_concat:
289 * @dest: an uninitialized #GValue to take the result
293 * Concatenates copies of @value1 and @value2 into a list. Values that are not
294 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
295 * @dest will be initialized to the type #GST_TYPE_LIST.
298 gst_value_list_concat (GValue * dest, const GValue * value1,
299 const GValue * value2)
301 guint i, value1_length, value2_length;
304 g_return_if_fail (dest != NULL);
305 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
306 g_return_if_fail (G_IS_VALUE (value1));
307 g_return_if_fail (G_IS_VALUE (value2));
310 (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
312 (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
313 g_value_init (dest, GST_TYPE_LIST);
314 array = (GArray *) dest->data[0].v_pointer;
315 g_array_set_size (array, value1_length + value2_length);
317 if (GST_VALUE_HOLDS_LIST (value1)) {
318 for (i = 0; i < value1_length; i++) {
319 gst_value_init_and_copy (&g_array_index (array, GValue, i),
320 gst_value_list_get_value (value1, i));
323 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
326 if (GST_VALUE_HOLDS_LIST (value2)) {
327 for (i = 0; i < value2_length; i++) {
328 gst_value_init_and_copy (&g_array_index (array, GValue,
329 i + value1_length), gst_value_list_get_value (value2, i));
332 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
338 * gst_value_list_get_size:
339 * @value: a #GValue of type #GST_TYPE_LIST
341 * Gets the number of values contained in @value.
343 * Returns: the number of values
346 gst_value_list_get_size (const GValue * value)
348 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
350 return ((GArray *) value->data[0].v_pointer)->len;
354 * gst_value_list_get_value:
355 * @value: a #GValue of type #GST_TYPE_LIST
356 * @index: index of value to get from the list
358 * Gets the value that is a member of the list contained in @value and
359 * has the index @index.
361 * Returns: the value at the given index
364 gst_value_list_get_value (const GValue * value, guint index)
366 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
367 g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
369 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
374 * gst_value_array_append_value:
375 * @value: a #GValue of type #GST_TYPE_ARRAY
376 * @append_value: the value to append
378 * Appends @append_value to the GstValueArray in @value.
381 gst_value_array_append_value (GValue * value, const GValue * append_value)
385 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
387 gst_value_init_and_copy (&val, append_value);
388 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
392 * gst_value_array_prepend_value:
393 * @value: a #GValue of type #GST_TYPE_ARRAY
394 * @prepend_value: the value to prepend
396 * Prepends @prepend_value to the GstValueArray in @value.
399 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
403 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
405 gst_value_init_and_copy (&val, prepend_value);
406 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
410 * gst_value_array_get_size:
411 * @value: a #GValue of type #GST_TYPE_ARRAY
413 * Gets the number of values contained in @value.
415 * Returns: the number of values
418 gst_value_array_get_size (const GValue * value)
420 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
422 return ((GArray *) value->data[0].v_pointer)->len;
426 * gst_value_array_get_value:
427 * @value: a #GValue of type #GST_TYPE_ARRAY
428 * @index: index of value to get from the array
430 * Gets the value that is a member of the array contained in @value and
431 * has the index @index.
433 * Returns: the value at the given index
436 gst_value_array_get_value (const GValue * value, guint index)
438 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
439 g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
441 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
446 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
448 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
452 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
454 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
457 /* Do an unordered compare of the contents of a list */
459 gst_value_compare_list (const GValue * value1, const GValue * value2)
462 GArray *array1 = value1->data[0].v_pointer;
463 GArray *array2 = value2->data[0].v_pointer;
468 GstValueCompareFunc compare;
470 /* get length and do initial length check. */
472 if (len != array2->len)
473 return GST_VALUE_UNORDERED;
475 /* place to mark removed value indices of array2 */
476 removed = g_newa (guint8, len);
477 memset (removed, 0, len);
480 /* loop over array1, all items should be in array2. When we find an
481 * item in array2, remove it from array2 by marking it as removed */
482 for (i = 0; i < len; i++) {
483 v1 = &g_array_index (array1, GValue, i);
484 if ((compare = gst_value_get_compare_func (v1))) {
485 for (j = 0; j < len; j++) {
486 /* item is removed, we can skip it */
489 v2 = &g_array_index (array2, GValue, j);
490 if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
491 /* mark item as removed now that we found it in array2 and
492 * decrement the number of remaining items in array2. */
498 /* item in array1 and not in array2, UNORDERED */
500 return GST_VALUE_UNORDERED;
502 return GST_VALUE_UNORDERED;
504 /* if not all items were removed, array2 contained something not in array1 */
506 return GST_VALUE_UNORDERED;
508 /* arrays are equal */
509 return GST_VALUE_EQUAL;
512 /* Perform an ordered comparison of the contents of an array */
514 gst_value_compare_array (const GValue * value1, const GValue * value2)
517 GArray *array1 = value1->data[0].v_pointer;
518 GArray *array2 = value2->data[0].v_pointer;
522 if (array1->len != array2->len)
523 return GST_VALUE_UNORDERED;
525 for (i = 0; i < array1->len; i++) {
526 v1 = &g_array_index (array1, GValue, i);
527 v2 = &g_array_index (array2, GValue, i);
528 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
529 return GST_VALUE_UNORDERED;
532 return GST_VALUE_EQUAL;
536 gst_value_serialize_list (const GValue * value)
538 return gst_value_serialize_any_list (value, "{ ", " }");
542 gst_value_deserialize_list (GValue * dest, const gchar * s)
544 g_warning ("gst_value_deserialize_list: unimplemented");
549 gst_value_serialize_array (const GValue * value)
551 return gst_value_serialize_any_list (value, "< ", " >");
555 gst_value_deserialize_array (GValue * dest, const gchar * s)
557 g_warning ("gst_value_deserialize_array: unimplemented");
566 gst_value_init_fourcc (GValue * value)
568 value->data[0].v_int = 0;
572 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
574 dest_value->data[0].v_int = src_value->data[0].v_int;
578 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
579 GTypeCValue * collect_values, guint collect_flags)
581 value->data[0].v_int = collect_values[0].v_int;
587 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
588 GTypeCValue * collect_values, guint collect_flags)
590 guint32 *fourcc_p = collect_values[0].v_pointer;
593 return g_strdup_printf ("value location for `%s' passed as NULL",
594 G_VALUE_TYPE_NAME (value));
596 *fourcc_p = value->data[0].v_int;
602 * gst_value_set_fourcc:
603 * @value: a GValue initialized to #GST_TYPE_FOURCC
604 * @fourcc: the #guint32 fourcc to set
606 * Sets @value to @fourcc.
609 gst_value_set_fourcc (GValue * value, guint32 fourcc)
611 g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
613 value->data[0].v_int = fourcc;
617 * gst_value_get_fourcc:
618 * @value: a GValue initialized to #GST_TYPE_FOURCC
620 * Gets the #guint32 fourcc contained in @value.
622 * Returns: the #guint32 fourcc contained in @value.
625 gst_value_get_fourcc (const GValue * value)
627 g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
629 return value->data[0].v_int;
633 gst_value_transform_fourcc_string (const GValue * src_value,
636 guint32 fourcc = src_value->data[0].v_int;
638 if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
639 g_ascii_isprint ((fourcc >> 8) & 0xff) &&
640 g_ascii_isprint ((fourcc >> 16) & 0xff) &&
641 g_ascii_isprint ((fourcc >> 24) & 0xff)) {
642 dest_value->data[0].v_pointer =
643 g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
645 dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
650 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
652 if (value2->data[0].v_int == value1->data[0].v_int)
653 return GST_VALUE_EQUAL;
654 return GST_VALUE_UNORDERED;
658 gst_value_serialize_fourcc (const GValue * value)
660 guint32 fourcc = value->data[0].v_int;
662 if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
663 g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
664 g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
665 g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
666 return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
668 return g_strdup_printf ("0x%08x", fourcc);
673 gst_value_deserialize_fourcc (GValue * dest, const char *s)
675 gboolean ret = FALSE;
679 if (strlen (s) == 4) {
680 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
682 } else if (g_ascii_isdigit (*s)) {
683 fourcc = strtoul (s, &end, 0);
688 gst_value_set_fourcc (dest, fourcc);
698 gst_value_init_int_range (GValue * value)
700 value->data[0].v_int = 0;
701 value->data[1].v_int = 0;
705 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
707 dest_value->data[0].v_int = src_value->data[0].v_int;
708 dest_value->data[1].v_int = src_value->data[1].v_int;
712 gst_value_collect_int_range (GValue * value, guint n_collect_values,
713 GTypeCValue * collect_values, guint collect_flags)
715 value->data[0].v_int = collect_values[0].v_int;
716 value->data[1].v_int = collect_values[1].v_int;
722 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
723 GTypeCValue * collect_values, guint collect_flags)
725 guint32 *int_range_start = collect_values[0].v_pointer;
726 guint32 *int_range_end = collect_values[1].v_pointer;
728 if (!int_range_start)
729 return g_strdup_printf ("start value location for `%s' passed as NULL",
730 G_VALUE_TYPE_NAME (value));
732 return g_strdup_printf ("end value location for `%s' passed as NULL",
733 G_VALUE_TYPE_NAME (value));
735 *int_range_start = value->data[0].v_int;
736 *int_range_end = value->data[1].v_int;
742 * gst_value_set_int_range:
743 * @value: a GValue initialized to GST_TYPE_INT_RANGE
744 * @start: the start of the range
745 * @end: the end of the range
747 * Sets @value to the range specified by @start and @end.
750 gst_value_set_int_range (GValue * value, gint start, gint end)
752 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
753 g_return_if_fail (start < end);
755 value->data[0].v_int = start;
756 value->data[1].v_int = end;
760 * gst_value_get_int_range_min:
761 * @value: a GValue initialized to GST_TYPE_INT_RANGE
763 * Gets the minimum of the range specified by @value.
765 * Returns: the minimum of the range
768 gst_value_get_int_range_min (const GValue * value)
770 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
772 return value->data[0].v_int;
776 * gst_value_get_int_range_max:
777 * @value: a GValue initialized to GST_TYPE_INT_RANGE
779 * Gets the maximum of the range specified by @value.
781 * Returns: the maxumum of the range
784 gst_value_get_int_range_max (const GValue * value)
786 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
788 return value->data[1].v_int;
792 gst_value_transform_int_range_string (const GValue * src_value,
795 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
796 (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
800 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
802 if (value2->data[0].v_int == value1->data[0].v_int &&
803 value2->data[1].v_int == value1->data[1].v_int)
804 return GST_VALUE_EQUAL;
805 return GST_VALUE_UNORDERED;
809 gst_value_serialize_int_range (const GValue * value)
811 return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
812 value->data[1].v_int);
816 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
818 g_warning ("unimplemented");
827 gst_value_init_double_range (GValue * value)
829 value->data[0].v_double = 0;
830 value->data[1].v_double = 0;
834 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
836 dest_value->data[0].v_double = src_value->data[0].v_double;
837 dest_value->data[1].v_double = src_value->data[1].v_double;
841 gst_value_collect_double_range (GValue * value, guint n_collect_values,
842 GTypeCValue * collect_values, guint collect_flags)
844 value->data[0].v_double = collect_values[0].v_double;
845 value->data[1].v_double = collect_values[1].v_double;
851 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
852 GTypeCValue * collect_values, guint collect_flags)
854 gdouble *double_range_start = collect_values[0].v_pointer;
855 gdouble *double_range_end = collect_values[1].v_pointer;
857 if (!double_range_start)
858 return g_strdup_printf ("start value location for `%s' passed as NULL",
859 G_VALUE_TYPE_NAME (value));
860 if (!double_range_end)
861 return g_strdup_printf ("end value location for `%s' passed as NULL",
862 G_VALUE_TYPE_NAME (value));
864 *double_range_start = value->data[0].v_double;
865 *double_range_end = value->data[1].v_double;
871 * gst_value_set_double_range:
872 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
873 * @start: the start of the range
874 * @end: the end of the range
876 * Sets @value to the range specified by @start and @end.
879 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
881 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
883 value->data[0].v_double = start;
884 value->data[1].v_double = end;
888 * gst_value_get_double_range_min:
889 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
891 * Gets the minimum of the range specified by @value.
893 * Returns: the minumum of the range
896 gst_value_get_double_range_min (const GValue * value)
898 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
900 return value->data[0].v_double;
904 * gst_value_get_double_range_max:
905 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
907 * Gets the maximum of the range specified by @value.
909 * Returns: the maxumum of the range
912 gst_value_get_double_range_max (const GValue * value)
914 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
916 return value->data[1].v_double;
920 gst_value_transform_double_range_string (const GValue * src_value,
923 char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
925 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
926 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
927 src_value->data[0].v_double),
928 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
929 src_value->data[1].v_double));
933 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
935 if (value2->data[0].v_double == value1->data[0].v_double &&
936 value2->data[0].v_double == value1->data[0].v_double)
937 return GST_VALUE_EQUAL;
938 return GST_VALUE_UNORDERED;
942 gst_value_serialize_double_range (const GValue * value)
944 char d1[G_ASCII_DTOSTR_BUF_SIZE];
945 char d2[G_ASCII_DTOSTR_BUF_SIZE];
947 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
948 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
949 return g_strdup_printf ("[ %s, %s ]", d1, d2);
953 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
955 g_warning ("unimplemented");
964 gst_value_init_fraction_range (GValue * value)
968 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
969 g_value_init (&vals[0], GST_TYPE_FRACTION);
970 g_value_init (&vals[1], GST_TYPE_FRACTION);
974 gst_value_free_fraction_range (GValue * value)
976 GValue *vals = (GValue *) value->data[0].v_pointer;
979 g_value_unset (&vals[0]);
980 g_value_unset (&vals[1]);
982 value->data[0].v_pointer = NULL;
987 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
989 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
990 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
993 dest_value->data[0].v_pointer = vals = g_new0 (GValue, 2);
994 g_return_if_fail (vals != NULL);
995 g_value_init (&vals[0], GST_TYPE_FRACTION);
996 g_value_init (&vals[1], GST_TYPE_FRACTION);
999 if (src_vals != NULL) {
1000 g_value_copy (&src_vals[0], &vals[0]);
1001 g_value_copy (&src_vals[1], &vals[1]);
1006 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1007 GTypeCValue * collect_values, guint collect_flags)
1009 GValue *vals = (GValue *) value->data[0].v_pointer;
1011 if (n_collect_values != 4)
1012 return g_strdup_printf ("not enough value locations for `%s' passed",
1013 G_VALUE_TYPE_NAME (value));
1015 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1017 return g_strdup_printf ("Could not initialise`%s' during collect",
1018 G_VALUE_TYPE_NAME (value));
1019 g_value_init (&vals[0], GST_TYPE_FRACTION);
1020 g_value_init (&vals[1], GST_TYPE_FRACTION);
1023 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1024 collect_values[1].v_int);
1025 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1026 collect_values[3].v_int);
1032 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1033 GTypeCValue * collect_values, guint collect_flags)
1036 int *dest_values[4];
1037 GValue *vals = (GValue *) value->data[0].v_pointer;
1039 if (n_collect_values != 4)
1040 return g_strdup_printf ("not enough value locations for `%s' passed",
1041 G_VALUE_TYPE_NAME (value));
1043 for (i = 0; i < 4; i++) {
1044 if (collect_values[i].v_pointer == NULL) {
1045 return g_strdup_printf ("value location for `%s' passed as NULL",
1046 G_VALUE_TYPE_NAME (value));
1048 dest_values[i] = collect_values[i].v_pointer;
1052 return g_strdup_printf ("Uninitialised `%s' passed",
1053 G_VALUE_TYPE_NAME (value));
1056 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1057 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1058 dest_values[2][0] = gst_value_get_fraction_denominator (&vals[1]);
1059 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1064 * gst_value_set_fraction_range:
1065 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1066 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1067 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1069 * Sets @value to the range specified by @start and @end.
1072 gst_value_set_fraction_range (GValue * value, const GValue * start,
1077 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1079 vals = (GValue *) value->data[0].v_pointer;
1081 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1082 g_value_init (&vals[0], GST_TYPE_FRACTION);
1083 g_value_init (&vals[1], GST_TYPE_FRACTION);
1086 g_value_copy (start, &vals[0]);
1087 g_value_copy (end, &vals[1]);
1091 * gst_value_set_fraction_range_full:
1092 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1093 * @numerator_start: the numerator start of the range
1094 * @denominator_start: the denominator start of the range
1095 * @numerator_end: the numerator end of the range
1096 * @denominator_end: the denominator end of the range
1098 * Sets @value to the range specified by @numerator_start/@denominator_start
1099 * and @numerator_end/@denominator_end.
1102 gst_value_set_fraction_range_full (GValue * value,
1103 gint numerator_start, gint denominator_start,
1104 gint numerator_end, gint denominator_end)
1106 GValue start = { 0 };
1109 g_value_init (&start, GST_TYPE_FRACTION);
1110 g_value_init (&end, GST_TYPE_FRACTION);
1112 gst_value_set_fraction (&start, numerator_start, denominator_start);
1113 gst_value_set_fraction (&end, numerator_end, denominator_end);
1114 gst_value_set_fraction_range (value, &start, &end);
1116 g_value_unset (&start);
1117 g_value_unset (&end);
1121 * gst_value_get_fraction_range_min:
1122 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1124 * Gets the minimum of the range specified by @value.
1126 * Returns: the minumum of the range
1129 gst_value_get_fraction_range_min (const GValue * value)
1133 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1135 vals = (GValue *) value->data[0].v_pointer;
1144 * gst_value_get_fraction_range_max:
1145 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1147 * Gets the maximum of the range specified by @value.
1149 * Returns: the maximum of the range
1152 gst_value_get_fraction_range_max (const GValue * value)
1156 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1158 vals = (GValue *) value->data[0].v_pointer;
1167 gst_value_serialize_fraction_range (const GValue * value)
1169 GValue *vals = (GValue *) value->data[0].v_pointer;
1173 retval = g_strdup ("[ 0/1, 0/1 ]");
1177 start = gst_value_serialize_fraction (&vals[0]);
1178 end = gst_value_serialize_fraction (&vals[1]);
1180 retval = g_strdup_printf ("[ %s, %s ]", start, end);
1189 gst_value_transform_fraction_range_string (const GValue * src_value,
1190 GValue * dest_value)
1192 dest_value->data[0].v_pointer =
1193 gst_value_serialize_fraction_range (src_value);
1197 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1199 GValue *vals1, *vals2;
1200 GstValueCompareFunc compare;
1202 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1203 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
1205 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1206 return GST_VALUE_UNORDERED;
1208 vals1 = (GValue *) value1->data[0].v_pointer;
1209 vals2 = (GValue *) value2->data[0].v_pointer;
1210 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1211 if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1213 gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1215 return GST_VALUE_EQUAL;
1217 return GST_VALUE_UNORDERED;
1221 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1223 g_warning ("unimplemented");
1232 * gst_value_set_caps:
1233 * @value: a GValue initialized to GST_TYPE_CAPS
1234 * @caps: the caps to set the value to
1236 * Sets the contents of @value to @caps. The actual
1237 * #GstCaps structure is copied before it is used.
1240 gst_value_set_caps (GValue * value, const GstCaps * caps)
1242 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1244 g_value_set_boxed (value, caps);
1248 * gst_value_get_caps:
1249 * @value: a GValue initialized to GST_TYPE_CAPS
1251 * Gets the contents of @value.
1253 * Returns: the contents of @value
1256 gst_value_get_caps (const GValue * value)
1258 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1260 return (GstCaps *) g_value_get_boxed (value);
1264 gst_value_serialize_caps (const GValue * value)
1266 GstCaps *caps = g_value_get_boxed (value);
1268 return gst_caps_to_string (caps);
1272 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1276 caps = gst_caps_from_string (s);
1279 g_value_set_boxed (dest, caps);
1290 * gst_value_set_structure:
1291 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1292 * @structure: the structure to set the value to
1294 * Sets the contents of @value to @structure. The actual
1297 gst_value_set_structure (GValue * value, const GstStructure * structure)
1299 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1301 g_value_set_boxed (value, structure);
1305 * gst_value_get_structure:
1306 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1308 * Gets the contents of @value.
1310 * Returns: the contents of @value
1312 const GstStructure *
1313 gst_value_get_structure (const GValue * value)
1315 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1317 return (GstStructure *) g_value_get_boxed (value);
1321 gst_value_serialize_structure (const GValue * value)
1323 GstStructure *structure = g_value_get_boxed (value);
1325 return gst_structure_to_string (structure);
1329 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1331 GstStructure *structure;
1333 structure = gst_structure_from_string (s, NULL);
1336 g_value_set_boxed (dest, structure);
1347 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1349 GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
1350 GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
1352 if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
1353 return GST_VALUE_UNORDERED;
1354 if (GST_BUFFER_SIZE (buf1) == 0)
1355 return GST_VALUE_EQUAL;
1356 g_assert (GST_BUFFER_DATA (buf1));
1357 g_assert (GST_BUFFER_DATA (buf2));
1358 if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
1359 GST_BUFFER_SIZE (buf1)) == 0)
1360 return GST_VALUE_EQUAL;
1362 return GST_VALUE_UNORDERED;
1366 gst_value_serialize_buffer (const GValue * value)
1374 buffer = gst_value_get_buffer (value);
1378 data = GST_BUFFER_DATA (buffer);
1379 size = GST_BUFFER_SIZE (buffer);
1381 string = g_malloc (size * 2 + 1);
1382 for (i = 0; i < size; i++) {
1383 sprintf (string + i * 2, "%02x", data[i]);
1385 string[size * 2] = 0;
1391 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1403 buffer = gst_buffer_new_and_alloc (len / 2);
1404 data = GST_BUFFER_DATA (buffer);
1405 for (i = 0; i < len / 2; i++) {
1406 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1409 ts[0] = s[i * 2 + 0];
1410 ts[1] = s[i * 2 + 1];
1413 data[i] = (guint8) strtoul (ts, NULL, 16);
1416 gst_value_take_buffer (dest, buffer);
1427 gst_buffer_unref (buffer);
1438 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1440 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1441 return GST_VALUE_EQUAL;
1442 return GST_VALUE_UNORDERED;
1446 gst_value_serialize_boolean (const GValue * value)
1448 if (value->data[0].v_int) {
1449 return g_strdup ("true");
1451 return g_strdup ("false");
1455 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1457 gboolean ret = FALSE;
1459 if (g_ascii_strcasecmp (s, "true") == 0 ||
1460 g_ascii_strcasecmp (s, "yes") == 0 ||
1461 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1462 g_value_set_boolean (dest, TRUE);
1464 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1465 g_ascii_strcasecmp (s, "no") == 0 ||
1466 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1467 g_value_set_boolean (dest, FALSE);
1474 #define CREATE_SERIALIZATION_START(_type,_macro) \
1476 gst_value_compare_ ## _type \
1477 (const GValue * value1, const GValue * value2) \
1479 g ## _type val1 = g_value_get_ ## _type (value1); \
1480 g ## _type val2 = g_value_get_ ## _type (value2); \
1482 return GST_VALUE_GREATER_THAN; \
1484 return GST_VALUE_LESS_THAN; \
1485 return GST_VALUE_EQUAL; \
1489 gst_value_serialize_ ## _type (const GValue * value) \
1491 GValue val = { 0, }; \
1492 g_value_init (&val, G_TYPE_STRING); \
1493 if (!g_value_transform (value, &val)) \
1494 g_assert_not_reached (); \
1495 /* NO_COPY_MADNESS!!! */ \
1496 return (char *) g_value_get_string (&val); \
1499 /* deserialize the given s into to as a gint64.
1500 * check if the result is actually storeable in the given size number of
1504 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1505 gint64 min, gint64 max, gint size)
1507 gboolean ret = FALSE;
1512 *to = g_ascii_strtoull (s, &end, 0);
1513 /* a range error is a definitive no-no */
1514 if (errno == ERANGE) {
1521 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1522 *to = G_LITTLE_ENDIAN;
1524 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1527 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1530 } else if (g_ascii_strcasecmp (s, "min") == 0) {
1533 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1539 /* by definition, a gint64 fits into a gint64; so ignore those */
1540 if (size != sizeof (mask)) {
1542 /* for positive numbers, we create a mask of 1's outside of the range
1543 * and 0's inside the range. An and will thus keep only 1 bits
1544 * outside of the range */
1545 mask <<= (size * 8);
1546 if ((mask & *to) != 0) {
1550 /* for negative numbers, we do a 2's complement version */
1551 mask <<= ((size * 8) - 1);
1552 if ((mask & *to) != mask) {
1561 #define CREATE_SERIALIZATION(_type,_macro) \
1562 CREATE_SERIALIZATION_START(_type,_macro) \
1565 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1569 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
1570 G_MAX ## _macro, sizeof (g ## _type))) { \
1571 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
1578 #define CREATE_USERIALIZATION(_type,_macro) \
1579 CREATE_SERIALIZATION_START(_type,_macro) \
1582 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1586 gboolean ret = FALSE; \
1589 x = g_ascii_strtoull (s, &end, 0); \
1590 /* a range error is a definitive no-no */ \
1591 if (errno == ERANGE) { \
1594 /* the cast ensures the range check later on makes sense */ \
1595 x = (g ## _type) x; \
1599 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
1600 x = G_LITTLE_ENDIAN; \
1602 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
1605 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
1608 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
1611 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
1612 x = G_MAX ## _macro; \
1617 if (x > G_MAX ## _macro) { \
1620 g_value_set_ ## _type (dest, x); \
1626 #define REGISTER_SERIALIZATION(_gtype, _type) \
1628 static const GstValueTable gst_value = { \
1630 gst_value_compare_ ## _type, \
1631 gst_value_serialize_ ## _type, \
1632 gst_value_deserialize_ ## _type, \
1635 gst_value_register (&gst_value); \
1638 CREATE_SERIALIZATION (int, INT);
1639 CREATE_SERIALIZATION (int64, INT64);
1640 CREATE_SERIALIZATION (long, LONG);
1642 CREATE_USERIALIZATION (uint, UINT);
1643 CREATE_USERIALIZATION (uint64, UINT64);
1644 CREATE_USERIALIZATION (ulong, ULONG);
1650 gst_value_compare_double (const GValue * value1, const GValue * value2)
1652 if (value1->data[0].v_double > value2->data[0].v_double)
1653 return GST_VALUE_GREATER_THAN;
1654 if (value1->data[0].v_double < value2->data[0].v_double)
1655 return GST_VALUE_LESS_THAN;
1656 if (value1->data[0].v_double == value2->data[0].v_double)
1657 return GST_VALUE_EQUAL;
1658 return GST_VALUE_UNORDERED;
1662 gst_value_serialize_double (const GValue * value)
1664 char d[G_ASCII_DTOSTR_BUF_SIZE];
1666 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1667 return g_strdup (d);
1671 gst_value_deserialize_double (GValue * dest, const gchar * s)
1674 gboolean ret = FALSE;
1677 x = g_ascii_strtod (s, &end);
1681 if (g_ascii_strcasecmp (s, "min") == 0) {
1684 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1690 g_value_set_double (dest, x);
1700 gst_value_compare_float (const GValue * value1, const GValue * value2)
1702 if (value1->data[0].v_float > value2->data[0].v_float)
1703 return GST_VALUE_GREATER_THAN;
1704 if (value1->data[0].v_float < value2->data[0].v_float)
1705 return GST_VALUE_LESS_THAN;
1706 if (value1->data[0].v_float == value2->data[0].v_float)
1707 return GST_VALUE_EQUAL;
1708 return GST_VALUE_UNORDERED;
1712 gst_value_serialize_float (const GValue * value)
1714 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
1716 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1717 return g_strdup (d);
1721 gst_value_deserialize_float (GValue * dest, const gchar * s)
1724 gboolean ret = FALSE;
1727 x = g_ascii_strtod (s, &end);
1731 if (g_ascii_strcasecmp (s, "min") == 0) {
1734 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1739 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1742 g_value_set_float (dest, (float) x);
1752 gst_value_compare_string (const GValue * value1, const GValue * value2)
1754 int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1757 return GST_VALUE_LESS_THAN;
1759 return GST_VALUE_GREATER_THAN;
1760 return GST_VALUE_EQUAL;
1763 /* keep in sync with gststructure.c */
1764 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1765 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1769 gst_string_wrap (const gchar * s)
1774 gboolean wrap = FALSE;
1781 if (GST_ASCII_IS_STRING (*t)) {
1783 } else if (*t < 0x20 || *t >= 0x7f) {
1794 return g_strdup (s);
1796 e = d = g_malloc (len + 3);
1801 if (GST_ASCII_IS_STRING (*t)) {
1803 } else if (*t < 0x20 || *t >= 0x7f) {
1805 *e++ = '0' + ((*(guchar *) t) >> 6);
1806 *e++ = '0' + (((*t) >> 3) & 0x7);
1807 *e++ = '0' + ((*t++) & 0x7);
1820 * This function takes a string delimited with double quotes (")
1821 * and unescapes any \xxx octal numbers.
1823 * If sequences of \y are found where y is not in the range of
1824 * 0->3, y is copied unescaped.
1826 * If \xyy is found where x is an octal number but y is not, an
1827 * error is encountered and NULL is returned.
1829 * the input string must be \0 terminated.
1832 gst_string_unwrap (const gchar * s)
1835 gchar *read, *write;
1837 /* NULL string returns NULL */
1841 /* strings not starting with " are invalid */
1845 /* make copy of original string to hold the result. This
1846 * string will always be smaller than the original */
1851 /* need to move to the next position as we parsed the " */
1855 if (GST_ASCII_IS_STRING (*read)) {
1856 /* normal chars are just copied */
1858 } else if (*read == '"') {
1859 /* quote marks end of string */
1861 } else if (*read == '\\') {
1862 /* got an escape char, move to next position to read a tripplet
1863 * of octal numbers */
1865 /* is the next char a possible first octal number? */
1866 if (*read >= '0' && *read <= '3') {
1867 /* parse other 2 numbers, if one of them is not in the range of
1868 * an octal number, we error. We also catch the case where a zero
1869 * byte is found here. */
1870 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1873 /* now convert the octal number to a byte again. */
1874 *write++ = ((read[0] - '0') << 6) +
1875 ((read[1] - '0') << 3) + (read[2] - '0');
1879 /* if we run into a \0 here, we definately won't get a quote later */
1883 /* else copy \X sequence */
1887 /* weird character, error */
1891 /* if the string is not ending in " and zero terminated, we error */
1892 if (*read != '"' || read[1] != '\0')
1895 /* null terminate result string and return */
1905 gst_value_serialize_string (const GValue * value)
1907 return gst_string_wrap (value->data[0].v_pointer);
1911 gst_value_deserialize_string (GValue * dest, const gchar * s)
1914 if (!g_utf8_validate (s, -1, NULL))
1916 g_value_set_string (dest, s);
1919 gchar *str = gst_string_unwrap (s);
1923 g_value_take_string (dest, str);
1934 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1936 GEnumValue *en1, *en2;
1937 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1938 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1940 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1941 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1942 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1943 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
1944 g_type_class_unref (klass1);
1945 g_type_class_unref (klass2);
1946 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
1947 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
1948 if (en1->value < en2->value)
1949 return GST_VALUE_LESS_THAN;
1950 if (en1->value > en2->value)
1951 return GST_VALUE_GREATER_THAN;
1953 return GST_VALUE_EQUAL;
1957 gst_value_serialize_enum (const GValue * value)
1960 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
1962 g_return_val_if_fail (klass, NULL);
1963 en = g_enum_get_value (klass, g_value_get_enum (value));
1964 g_type_class_unref (klass);
1966 /* might be one of the custom formats registered later */
1967 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
1968 const GstFormatDefinition *format_def;
1970 format_def = gst_format_get_details (g_value_get_enum (value));
1971 g_return_val_if_fail (format_def != NULL, NULL);
1972 return g_strdup (format_def->description);
1975 g_return_val_if_fail (en, NULL);
1976 return g_strdup (en->value_name);
1980 gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
1983 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
1986 return g_ascii_strcasecmp (s, format_def->description);
1990 gst_value_deserialize_enum (GValue * dest, const gchar * s)
1993 gchar *endptr = NULL;
1994 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1996 g_return_val_if_fail (klass, FALSE);
1997 if (!(en = g_enum_get_value_by_name (klass, s))) {
1998 if (!(en = g_enum_get_value_by_nick (klass, s))) {
1999 gint i = strtol (s, &endptr, 0);
2001 if (endptr && *endptr == '\0') {
2002 en = g_enum_get_value (klass, i);
2006 g_type_class_unref (klass);
2008 /* might be one of the custom formats registered later */
2009 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2010 const GstFormatDefinition *format_def;
2013 iter = gst_format_iterate_definitions ();
2015 format_def = gst_iterator_find_custom (iter,
2016 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
2018 g_return_val_if_fail (format_def != NULL, FALSE);
2019 g_value_set_enum (dest, (gint) format_def->value);
2020 gst_iterator_free (iter);
2024 g_return_val_if_fail (en, FALSE);
2025 g_value_set_enum (dest, en->value);
2033 /* we just compare the value here */
2035 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2038 GFlagsClass *klass1 =
2039 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2040 GFlagsClass *klass2 =
2041 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2043 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2044 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2045 fl1 = g_value_get_flags (value1);
2046 fl2 = g_value_get_flags (value2);
2047 g_type_class_unref (klass1);
2048 g_type_class_unref (klass2);
2050 return GST_VALUE_LESS_THAN;
2052 return GST_VALUE_GREATER_THAN;
2054 return GST_VALUE_EQUAL;
2057 /* the different flags are serialized separated with a + */
2059 gst_value_serialize_flags (const GValue * value)
2063 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2064 gchar *result, *tmp;
2065 gboolean first = TRUE;
2067 g_return_val_if_fail (klass, NULL);
2069 flags = g_value_get_flags (value);
2071 /* if no flags are set, try to serialize to the _NONE string */
2073 fl = g_flags_get_first_value (klass, flags);
2074 return g_strdup (fl->value_name);
2077 /* some flags are set, so serialize one by one */
2078 result = g_strdup ("");
2080 fl = g_flags_get_first_value (klass, flags);
2082 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2088 flags &= ~fl->value;
2091 g_type_class_unref (klass);
2097 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2100 gchar *endptr = NULL;
2101 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2106 g_return_val_if_fail (klass, FALSE);
2108 /* split into parts delimited with + */
2109 split = g_strsplit (s, "+", 0);
2113 /* loop over each part */
2115 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2116 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2117 gint val = strtol (split[i], &endptr, 0);
2119 /* just or numeric value */
2120 if (endptr && *endptr == '\0') {
2131 g_type_class_unref (klass);
2132 g_value_set_flags (dest, flags);
2142 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2143 const GValue * src2)
2145 if (src2->data[0].v_int <= src1->data[0].v_int &&
2146 src2->data[1].v_int >= src1->data[0].v_int) {
2147 gst_value_init_and_copy (dest, src2);
2154 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2155 const GValue * src2)
2160 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2161 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2164 g_value_init (dest, GST_TYPE_INT_RANGE);
2165 gst_value_set_int_range (dest,
2166 MIN (src1->data[0].v_int, src2->data[0].v_int),
2167 MAX (src1->data[1].v_int, src2->data[1].v_int));
2179 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2180 const GValue * src2)
2182 if (src2->data[0].v_int <= src1->data[0].v_int &&
2183 src2->data[1].v_int >= src1->data[0].v_int) {
2184 gst_value_init_and_copy (dest, src1);
2192 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2193 const GValue * src2)
2198 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2199 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2202 g_value_init (dest, GST_TYPE_INT_RANGE);
2203 gst_value_set_int_range (dest, min, max);
2207 g_value_init (dest, G_TYPE_INT);
2208 g_value_set_int (dest, min);
2216 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2217 const GValue * src2)
2219 if (src2->data[0].v_double <= src1->data[0].v_double &&
2220 src2->data[1].v_double >= src1->data[0].v_double) {
2221 gst_value_init_and_copy (dest, src1);
2229 gst_value_intersect_double_range_double_range (GValue * dest,
2230 const GValue * src1, const GValue * src2)
2235 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2236 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2239 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2240 gst_value_set_double_range (dest, min, max);
2244 g_value_init (dest, G_TYPE_DOUBLE);
2245 g_value_set_int (dest, (int) min);
2253 gst_value_intersect_list (GValue * dest, const GValue * value1,
2254 const GValue * value2)
2257 GValue intersection = { 0, };
2258 gboolean ret = FALSE;
2260 size = gst_value_list_get_size (value1);
2261 for (i = 0; i < size; i++) {
2262 const GValue *cur = gst_value_list_get_value (value1, i);
2264 if (gst_value_intersect (&intersection, cur, value2)) {
2267 gst_value_init_and_copy (dest, &intersection);
2269 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2270 gst_value_list_append_value (dest, &intersection);
2272 GValue temp = { 0, };
2274 gst_value_init_and_copy (&temp, dest);
2275 g_value_unset (dest);
2276 gst_value_list_concat (dest, &temp, &intersection);
2277 g_value_unset (&temp);
2279 g_value_unset (&intersection);
2287 gst_value_intersect_array (GValue * dest, const GValue * src1,
2288 const GValue * src2)
2294 /* only works on similar-sized arrays */
2295 size = gst_value_array_get_size (src1);
2296 if (size != gst_value_array_get_size (src2))
2298 g_value_init (dest, GST_TYPE_ARRAY);
2300 for (n = 0; n < size; n++) {
2301 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2302 gst_value_array_get_value (src2, n))) {
2303 g_value_unset (dest);
2306 gst_value_array_append_value (dest, &val);
2307 g_value_unset (&val);
2314 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2315 const GValue * src2)
2319 GstValueCompareFunc compare;
2321 vals = src2->data[0].v_pointer;
2326 if ((compare = gst_value_get_compare_func (src1))) {
2327 res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2328 res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2330 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2331 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2332 gst_value_init_and_copy (dest, src1);
2341 gst_value_intersect_fraction_range_fraction_range
2342 (GValue * dest, const GValue * src1, const GValue * src2)
2347 GValue *vals1, *vals2;
2348 GstValueCompareFunc compare;
2350 vals1 = src1->data[0].v_pointer;
2351 vals2 = src2->data[0].v_pointer;
2352 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2354 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2355 /* min = MAX (src1.start, src2.start) */
2356 res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2357 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2358 if (res == GST_VALUE_LESS_THAN)
2359 min = &vals2[0]; /* Take the max of the 2 */
2363 /* max = MIN (src1.end, src2.end) */
2364 res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2365 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2366 if (res == GST_VALUE_GREATER_THAN)
2367 max = &vals2[1]; /* Take the min of the 2 */
2371 res = gst_value_compare_with_func (min, max, compare);
2372 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2373 if (res == GST_VALUE_LESS_THAN) {
2374 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2375 vals1 = dest->data[0].v_pointer;
2376 g_value_copy (min, &vals1[0]);
2377 g_value_copy (max, &vals1[1]);
2380 if (res == GST_VALUE_EQUAL) {
2381 gst_value_init_and_copy (dest, min);
2394 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2395 const GValue * subtrahend)
2397 int min = gst_value_get_int_range_min (subtrahend);
2398 int max = gst_value_get_int_range_max (subtrahend);
2399 int val = g_value_get_int (minuend);
2401 /* subtracting a range from an int only works if the int is not in the
2403 if (val < min || val > max) {
2404 /* and the result is the int */
2405 gst_value_init_and_copy (dest, minuend);
2411 /* creates a new int range based on input values.
2414 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2419 GValue *pv1, *pv2; /* yeah, hungarian! */
2421 if (min1 <= max1 && min2 <= max2) {
2424 } else if (min1 <= max1) {
2427 } else if (min2 <= max2) {
2435 g_value_init (pv1, GST_TYPE_INT_RANGE);
2436 gst_value_set_int_range (pv1, min1, max1);
2437 } else if (min1 == max1) {
2438 g_value_init (pv1, G_TYPE_INT);
2439 g_value_set_int (pv1, min1);
2442 g_value_init (pv2, GST_TYPE_INT_RANGE);
2443 gst_value_set_int_range (pv2, min2, max2);
2444 } else if (min2 == max2) {
2445 g_value_init (pv2, G_TYPE_INT);
2446 g_value_set_int (pv2, min2);
2449 if (min1 <= max1 && min2 <= max2) {
2450 gst_value_list_concat (dest, pv1, pv2);
2451 g_value_unset (pv1);
2452 g_value_unset (pv2);
2458 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2459 const GValue * subtrahend)
2461 gint min = gst_value_get_int_range_min (minuend);
2462 gint max = gst_value_get_int_range_max (minuend);
2463 gint val = g_value_get_int (subtrahend);
2465 g_return_val_if_fail (min < max, FALSE);
2467 /* value is outside of the range, return range unchanged */
2468 if (val < min || val > max) {
2469 gst_value_init_and_copy (dest, minuend);
2472 /* max must be MAXINT too as val <= max */
2473 if (val == G_MAXINT) {
2477 /* min must be MININT too as val >= max */
2478 if (val == G_MININT) {
2482 gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2488 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2489 const GValue * subtrahend)
2491 gint min1 = gst_value_get_int_range_min (minuend);
2492 gint max1 = gst_value_get_int_range_max (minuend);
2493 gint min2 = gst_value_get_int_range_min (subtrahend);
2494 gint max2 = gst_value_get_int_range_max (subtrahend);
2496 if (max2 == G_MAXINT && min2 == G_MININT) {
2498 } else if (max2 == G_MAXINT) {
2499 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
2500 } else if (min2 == G_MININT) {
2501 return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
2503 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
2504 MAX (max2 + 1, min1), max1);
2509 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
2510 const GValue * subtrahend)
2512 gdouble min = gst_value_get_double_range_min (subtrahend);
2513 gdouble max = gst_value_get_double_range_max (subtrahend);
2514 gdouble val = g_value_get_double (minuend);
2516 if (val < min || val > max) {
2517 gst_value_init_and_copy (dest, minuend);
2524 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
2525 const GValue * subtrahend)
2527 /* since we don't have open ranges, we cannot create a hole in
2528 * a double range. We return the original range */
2529 gst_value_init_and_copy (dest, minuend);
2534 gst_value_subtract_double_range_double_range (GValue * dest,
2535 const GValue * minuend, const GValue * subtrahend)
2537 /* since we don't have open ranges, we have to approximate */
2538 /* done like with ints */
2539 gdouble min1 = gst_value_get_double_range_min (minuend);
2540 gdouble max2 = gst_value_get_double_range_max (minuend);
2541 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
2542 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
2545 GValue *pv1, *pv2; /* yeah, hungarian! */
2547 if (min1 < max1 && min2 < max2) {
2550 } else if (min1 < max1) {
2553 } else if (min2 < max2) {
2561 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
2562 gst_value_set_double_range (pv1, min1, max1);
2565 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
2566 gst_value_set_double_range (pv2, min2, max2);
2569 if (min1 < max1 && min2 < max2) {
2570 gst_value_list_concat (dest, pv1, pv2);
2571 g_value_unset (pv1);
2572 g_value_unset (pv2);
2578 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
2579 const GValue * subtrahend)
2582 GValue subtraction = { 0, };
2583 gboolean ret = FALSE;
2585 size = gst_value_list_get_size (minuend);
2586 for (i = 0; i < size; i++) {
2587 const GValue *cur = gst_value_list_get_value (minuend, i);
2589 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2591 gst_value_init_and_copy (dest, &subtraction);
2593 } else if (GST_VALUE_HOLDS_LIST (dest)
2594 && GST_VALUE_HOLDS_LIST (&subtraction)) {
2596 GValue unroll = { 0, };
2598 gst_value_init_and_copy (&unroll, dest);
2599 g_value_unset (dest);
2600 gst_value_list_concat (dest, &unroll, &subtraction);
2601 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2602 gst_value_list_append_value (dest, &subtraction);
2604 GValue temp = { 0, };
2606 gst_value_init_and_copy (&temp, dest);
2607 g_value_unset (dest);
2608 gst_value_list_concat (dest, &temp, &subtraction);
2609 g_value_unset (&temp);
2611 g_value_unset (&subtraction);
2618 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2619 const GValue * subtrahend)
2622 GValue data[2] = { {0,}, {0,} };
2623 GValue *subtraction = &data[0], *result = &data[1];
2625 gst_value_init_and_copy (result, minuend);
2626 size = gst_value_list_get_size (subtrahend);
2627 for (i = 0; i < size; i++) {
2628 const GValue *cur = gst_value_list_get_value (subtrahend, i);
2630 if (gst_value_subtract (subtraction, result, cur)) {
2631 GValue *temp = result;
2633 result = subtraction;
2635 g_value_unset (subtraction);
2637 g_value_unset (result);
2641 gst_value_init_and_copy (dest, result);
2642 g_value_unset (result);
2647 gst_value_subtract_fraction_fraction_range (GValue * dest,
2648 const GValue * minuend, const GValue * subtrahend)
2650 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
2651 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
2652 GstValueCompareFunc compare;
2654 if ((compare = gst_value_get_compare_func (minuend))) {
2655 /* subtracting a range from an fraction only works if the fraction
2656 * is not in the range */
2657 if (gst_value_compare_with_func (minuend, min, compare) ==
2658 GST_VALUE_LESS_THAN ||
2659 gst_value_compare_with_func (minuend, max, compare) ==
2660 GST_VALUE_GREATER_THAN) {
2661 /* and the result is the value */
2662 gst_value_init_and_copy (dest, minuend);
2670 gst_value_subtract_fraction_range_fraction (GValue * dest,
2671 const GValue * minuend, const GValue * subtrahend)
2673 /* since we don't have open ranges, we cannot create a hole in
2674 * a range. We return the original range */
2675 gst_value_init_and_copy (dest, minuend);
2680 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
2681 const GValue * minuend, const GValue * subtrahend)
2683 /* since we don't have open ranges, we have to approximate */
2684 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
2685 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
2686 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
2687 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
2688 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
2692 GValue *pv1, *pv2; /* yeah, hungarian! */
2693 GstValueCompareFunc compare;
2695 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
2696 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
2698 compare = gst_value_get_compare_func (min1);
2699 g_return_val_if_fail (compare, FALSE);
2701 cmp1 = gst_value_compare_with_func (max2, max1, compare);
2702 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2703 if (cmp1 == GST_VALUE_LESS_THAN)
2705 cmp1 = gst_value_compare_with_func (min1, min2, compare);
2706 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2707 if (cmp1 == GST_VALUE_GREATER_THAN)
2710 cmp1 = gst_value_compare_with_func (min1, max1, compare);
2711 cmp2 = gst_value_compare_with_func (min2, max2, compare);
2713 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2716 } else if (cmp1 == GST_VALUE_LESS_THAN) {
2719 } else if (cmp2 == GST_VALUE_LESS_THAN) {
2726 if (cmp1 == GST_VALUE_LESS_THAN) {
2727 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
2728 gst_value_set_fraction_range (pv1, min1, max1);
2730 if (cmp2 == GST_VALUE_LESS_THAN) {
2731 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
2732 gst_value_set_fraction_range (pv2, min2, max2);
2735 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2736 gst_value_list_concat (dest, pv1, pv2);
2737 g_value_unset (pv1);
2738 g_value_unset (pv2);
2749 * gst_value_can_compare:
2750 * @value1: a value to compare
2751 * @value2: another value to compare
2753 * Determines if @value1 and @value2 can be compared.
2755 * Returns: TRUE if the values can be compared
2758 gst_value_can_compare (const GValue * value1, const GValue * value2)
2760 GstValueTable *table;
2763 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2766 for (i = 0; i < gst_value_table->len; i++) {
2767 table = &g_array_index (gst_value_table, GstValueTable, i);
2768 if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2776 * gst_value_get_compare_func:
2777 * @value1: a value to get the compare function for
2779 * Determines the compare function to be used with values of the same type as
2780 * @value1. The function can be given to gst_value_compare_with_func().
2782 * Returns: A #GstValueCompareFunc value
2784 static GstValueCompareFunc
2785 gst_value_get_compare_func (const GValue * value1)
2787 GstValueTable *table, *best = NULL;
2790 for (i = 0; i < gst_value_table->len; i++) {
2791 table = &g_array_index (gst_value_table, GstValueTable, i);
2792 if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2796 if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2797 if (!best || g_type_is_a (table->type, best->type))
2802 return best->compare;
2808 * gst_value_compare:
2809 * @value1: a value to compare
2810 * @value2: another value to compare
2812 * Compares @value1 and @value2. If @value1 and @value2 cannot be
2813 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
2814 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
2815 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
2816 * If the values are equal, GST_VALUE_EQUAL is returned.
2818 * Returns: A #GstValueCompareType value
2821 gst_value_compare (const GValue * value1, const GValue * value2)
2823 GstValueCompareFunc compare;
2825 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2826 return GST_VALUE_UNORDERED;
2828 compare = gst_value_get_compare_func (value1);
2830 return compare (value1, value2);
2833 g_critical ("unable to compare values of type %s\n",
2834 g_type_name (G_VALUE_TYPE (value1)));
2835 return GST_VALUE_UNORDERED;
2839 * gst_value_compare_with_func:
2840 * @value1: a value to compare
2841 * @value2: another value to compare
2842 * @compare: compare function
2844 * Compares @value1 and @value2 using the @compare function. Works like
2845 * gst_value_compare() but allows to save time determining the compare function
2848 * Returns: A #GstValueCompareType value
2851 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
2852 GstValueCompareFunc compare)
2856 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2857 return GST_VALUE_UNORDERED;
2859 return compare (value1, value2);
2865 * gst_value_can_union:
2866 * @value1: a value to union
2867 * @value2: another value to union
2869 * Determines if @value1 and @value2 can be non-trivially unioned.
2870 * Any two values can be trivially unioned by adding both of them
2871 * to a GstValueList. However, certain types have the possibility
2872 * to be unioned in a simpler way. For example, an integer range
2873 * and an integer can be unioned if the integer is a subset of the
2874 * integer range. If there is the possibility that two values can
2875 * be unioned, this function returns TRUE.
2877 * Returns: TRUE if there is a function allowing the two values to
2881 gst_value_can_union (const GValue * value1, const GValue * value2)
2883 GstValueUnionInfo *union_info;
2886 for (i = 0; i < gst_value_union_funcs->len; i++) {
2887 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2888 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2889 union_info->type2 == G_VALUE_TYPE (value2))
2891 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2892 union_info->type2 == G_VALUE_TYPE (value1))
2901 * @dest: the destination value
2902 * @value1: a value to union
2903 * @value2: another value to union
2905 * Creates a GValue cooresponding to the union of @value1 and @value2.
2907 * Returns: always returns %TRUE
2909 /* FIXME: change return type to 'void'? */
2911 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2913 GstValueUnionInfo *union_info;
2916 for (i = 0; i < gst_value_union_funcs->len; i++) {
2917 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2918 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2919 union_info->type2 == G_VALUE_TYPE (value2)) {
2920 if (union_info->func (dest, value1, value2)) {
2924 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2925 union_info->type2 == G_VALUE_TYPE (value1)) {
2926 if (union_info->func (dest, value2, value1)) {
2932 gst_value_list_concat (dest, value1, value2);
2937 * gst_value_register_union_func:
2938 * @type1: a type to union
2939 * @type2: another type to union
2940 * @func: a function that implments creating a union between the two types
2942 * Registers a union function that can create a union between GValues
2943 * of the type @type1 and @type2.
2945 * Union functions should be registered at startup before any pipelines are
2946 * started, as gst_value_register_union_func() is not thread-safe and cannot
2947 * be used at the same time as gst_value_union() or gst_value_can_union().
2950 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
2952 GstValueUnionInfo union_info;
2954 union_info.type1 = type1;
2955 union_info.type2 = type2;
2956 union_info.func = func;
2958 g_array_append_val (gst_value_union_funcs, union_info);
2964 * gst_value_can_intersect:
2965 * @value1: a value to intersect
2966 * @value2: another value to intersect
2968 * Determines if intersecting two values will produce a valid result.
2969 * Two values will produce a valid intersection if they have the same
2970 * type, or if there is a method (registered by
2971 * gst_value_register_intersection_func()) to calculate the intersection.
2973 * Returns: TRUE if the values can intersect
2976 gst_value_can_intersect (const GValue * value1, const GValue * value2)
2978 GstValueIntersectInfo *intersect_info;
2982 if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
2985 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2986 intersect_info = &g_array_index (gst_value_intersect_funcs,
2987 GstValueIntersectInfo, i);
2988 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2989 intersect_info->type2 == G_VALUE_TYPE (value2))
2990 if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
2991 intersect_info->type1 == G_VALUE_TYPE (value2))
2995 return gst_value_can_compare (value1, value2);
2999 * gst_value_intersect:
3000 * @dest: a uninitialized #GValue that will hold the calculated
3001 * intersection value
3002 * @value1: a value to intersect
3003 * @value2: another value to intersect
3005 * Calculates the intersection of two values. If the values have
3006 * a non-empty intersection, the value representing the intersection
3007 * is placed in @dest. If the intersection is non-empty, @dest is
3010 * Returns: TRUE if the intersection is non-empty
3013 gst_value_intersect (GValue * dest, const GValue * value1,
3014 const GValue * value2)
3016 GstValueIntersectInfo *intersect_info;
3018 gboolean ret = FALSE;
3020 /* special cases first */
3021 if (GST_VALUE_HOLDS_LIST (value1))
3022 return gst_value_intersect_list (dest, value1, value2);
3023 if (GST_VALUE_HOLDS_LIST (value2))
3024 return gst_value_intersect_list (dest, value2, value1);
3026 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
3027 intersect_info = &g_array_index (gst_value_intersect_funcs,
3028 GstValueIntersectInfo, i);
3029 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
3030 intersect_info->type2 == G_VALUE_TYPE (value2)) {
3031 ret = intersect_info->func (dest, value1, value2);
3034 if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
3035 intersect_info->type2 == G_VALUE_TYPE (value1)) {
3036 ret = intersect_info->func (dest, value2, value1);
3041 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
3042 gst_value_init_and_copy (dest, value1);
3050 * gst_value_register_intersect_func:
3051 * @type1: the first type to intersect
3052 * @type2: the second type to intersect
3053 * @func: the intersection function
3055 * Registers a function that is called to calculate the intersection
3056 * of the values having the types @type1 and @type2.
3058 * Intersect functions should be registered at startup before any pipelines are
3059 * started, as gst_value_register_intersect_func() is not thread-safe and
3060 * cannot be used at the same time as gst_value_intersect() or
3061 * gst_value_can_intersect().
3064 gst_value_register_intersect_func (GType type1, GType type2,
3065 GstValueIntersectFunc func)
3067 GstValueIntersectInfo intersect_info;
3069 intersect_info.type1 = type1;
3070 intersect_info.type2 = type2;
3071 intersect_info.func = func;
3073 g_array_append_val (gst_value_intersect_funcs, intersect_info);
3080 * gst_value_subtract:
3081 * @dest: the destination value for the result if the subtraction is not empty
3082 * @minuend: the value to subtract from
3083 * @subtrahend: the value to subtract
3085 * Subtracts @subtrahend from @minuend and stores the result in @dest.
3086 * Note that this means subtraction as in sets, not as in mathematics.
3088 * Returns: %TRUE if the subtraction is not empty
3091 gst_value_subtract (GValue * dest, const GValue * minuend,
3092 const GValue * subtrahend)
3094 GstValueSubtractInfo *info;
3097 /* special cases first */
3098 if (GST_VALUE_HOLDS_LIST (minuend))
3099 return gst_value_subtract_from_list (dest, minuend, subtrahend);
3100 if (GST_VALUE_HOLDS_LIST (subtrahend))
3101 return gst_value_subtract_list (dest, minuend, subtrahend);
3103 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3104 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3105 if (info->minuend == G_VALUE_TYPE (minuend) &&
3106 info->subtrahend == G_VALUE_TYPE (subtrahend)) {
3107 return info->func (dest, minuend, subtrahend);
3111 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3112 gst_value_init_and_copy (dest, minuend);
3121 gst_value_subtract (GValue * dest, const GValue * minuend,
3122 const GValue * subtrahend)
3124 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3126 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
3127 gst_value_serialize (subtrahend),
3128 ret ? gst_value_serialize (dest) : "---");
3134 * gst_value_can_subtract:
3135 * @minuend: the value to subtract from
3136 * @subtrahend: the value to subtract
3138 * Checks if it's possible to subtract @subtrahend from @minuend.
3140 * Returns: TRUE if a subtraction is possible
3143 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3145 GstValueSubtractInfo *info;
3149 if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
3152 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3153 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3154 if (info->minuend == G_VALUE_TYPE (minuend) &&
3155 info->subtrahend == G_VALUE_TYPE (subtrahend))
3159 return gst_value_can_compare (minuend, subtrahend);
3163 * gst_value_register_subtract_func:
3164 * @minuend_type: type of the minuend
3165 * @subtrahend_type: type of the subtrahend
3166 * @func: function to use
3168 * Registers @func as a function capable of subtracting the values of
3169 * @subtrahend_type from values of @minuend_type.
3171 * Subtract functions should be registered at startup before any pipelines are
3172 * started, as gst_value_register_subtract_func() is not thread-safe and
3173 * cannot be used at the same time as gst_value_subtract().
3176 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3177 GstValueSubtractFunc func)
3179 GstValueSubtractInfo info;
3181 /* one type must be unfixed, other subtractions can be done as comparisons */
3182 g_return_if_fail (!gst_type_is_fixed (minuend_type)
3183 || !gst_type_is_fixed (subtrahend_type));
3185 info.minuend = minuend_type;
3186 info.subtrahend = subtrahend_type;
3189 g_array_append_val (gst_value_subtract_funcs, info);
3193 * gst_value_register:
3194 * @table: structure containing functions to register
3196 * Registers functions to perform calculations on #GValues of a given
3200 gst_value_register (const GstValueTable * table)
3202 g_array_append_val (gst_value_table, *table);
3206 * gst_value_init_and_copy:
3207 * @dest: the target value
3208 * @src: the source value
3210 * Initialises the target value to be of the same type as source and then copies
3211 * the contents from source to target.
3214 gst_value_init_and_copy (GValue * dest, const GValue * src)
3216 g_value_init (dest, G_VALUE_TYPE (src));
3217 g_value_copy (src, dest);
3221 * gst_value_serialize:
3222 * @value: a #GValue to serialize
3224 * tries to transform the given @value into a string representation that allows
3225 * getting back this string later on using gst_value_deserialize().
3227 * Returns: the serialization for @value or NULL if none exists
3230 gst_value_serialize (const GValue * value)
3233 GValue s_val = { 0 };
3234 GstValueTable *table, *best = NULL;
3237 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3239 for (i = 0; i < gst_value_table->len; i++) {
3240 table = &g_array_index (gst_value_table, GstValueTable, i);
3241 if (table->serialize == NULL)
3243 if (table->type == G_VALUE_TYPE (value)) {
3247 if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
3248 if (!best || g_type_is_a (table->type, best->type))
3253 return best->serialize (value);
3255 g_value_init (&s_val, G_TYPE_STRING);
3256 if (g_value_transform (value, &s_val)) {
3257 s = gst_string_wrap (g_value_get_string (&s_val));
3261 g_value_unset (&s_val);
3267 * gst_value_deserialize:
3268 * @dest: #GValue to fill with contents of deserialization
3269 * @src: string to deserialize
3271 * Tries to deserialize a string into the type specified by the given GValue.
3272 * If the operation succeeds, TRUE is returned, FALSE otherwise.
3274 * Returns: TRUE on success
3277 gst_value_deserialize (GValue * dest, const gchar * src)
3279 GstValueTable *table, *best = NULL;
3282 g_return_val_if_fail (src != NULL, FALSE);
3283 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
3285 for (i = 0; i < gst_value_table->len; i++) {
3286 table = &g_array_index (gst_value_table, GstValueTable, i);
3287 if (table->serialize == NULL)
3290 if (table->type == G_VALUE_TYPE (dest)) {
3295 if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
3296 if (!best || g_type_is_a (table->type, best->type))
3301 return best->deserialize (dest, src);
3308 * gst_value_is_fixed:
3309 * @value: the #GValue to check
3311 * Tests if the given GValue, if available in a GstStructure (or any other
3312 * container) contains a "fixed" (which means: one value) or an "unfixed"
3313 * (which means: multiple possible values, such as data lists or data
3316 * Returns: true if the value is "fixed".
3320 gst_value_is_fixed (const GValue * value)
3322 GType type = G_VALUE_TYPE (value);
3324 if (type == GST_TYPE_ARRAY) {
3325 gboolean fixed = TRUE;
3329 /* check recursively */
3330 size = gst_value_array_get_size (value);
3331 for (n = 0; n < size; n++) {
3332 kid = gst_value_array_get_value (value, n);
3333 fixed &= gst_value_is_fixed (kid);
3339 return gst_type_is_fixed (type);
3346 /* helper functions */
3348 /* Finds the greatest common divisor.
3349 * Returns 1 if none other found.
3350 * This is Euclid's algorithm. */
3352 gst_greatest_common_divisor (gint a, gint b)
3365 gst_value_init_fraction (GValue * value)
3367 value->data[0].v_int = 0;
3368 value->data[1].v_int = 1;
3372 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
3374 dest_value->data[0].v_int = src_value->data[0].v_int;
3375 dest_value->data[1].v_int = src_value->data[1].v_int;
3379 gst_value_collect_fraction (GValue * value, guint n_collect_values,
3380 GTypeCValue * collect_values, guint collect_flags)
3382 gst_value_set_fraction (value,
3383 collect_values[0].v_int, collect_values[1].v_int);
3389 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
3390 GTypeCValue * collect_values, guint collect_flags)
3392 gint *numerator = collect_values[0].v_pointer;
3393 gint *denominator = collect_values[1].v_pointer;
3396 return g_strdup_printf ("numerator for `%s' passed as NULL",
3397 G_VALUE_TYPE_NAME (value));
3399 return g_strdup_printf ("denominator for `%s' passed as NULL",
3400 G_VALUE_TYPE_NAME (value));
3402 *numerator = value->data[0].v_int;
3403 *denominator = value->data[1].v_int;
3409 * gst_value_set_fraction:
3410 * @value: a GValue initialized to #GST_TYPE_FRACTION
3411 * @numerator: the numerator of the fraction
3412 * @denominator: the denominator of the fraction
3414 * Sets @value to the fraction specified by @numerator over @denominator.
3415 * The fraction gets reduced to the smallest numerator and denominator,
3416 * and if necessary the sign is moved to the numerator.
3419 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
3423 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
3424 g_return_if_fail (denominator != 0);
3425 g_return_if_fail (denominator >= -G_MAXINT);
3426 g_return_if_fail (numerator >= -G_MAXINT);
3428 /* normalize sign */
3429 if (denominator < 0) {
3430 numerator = -numerator;
3431 denominator = -denominator;
3434 /* check for reduction */
3435 gcd = gst_greatest_common_divisor (numerator, denominator);
3441 g_assert (denominator > 0);
3443 value->data[0].v_int = numerator;
3444 value->data[1].v_int = denominator;
3448 * gst_value_get_fraction_numerator:
3449 * @value: a GValue initialized to #GST_TYPE_FRACTION
3451 * Gets the numerator of the fraction specified by @value.
3453 * Returns: the numerator of the fraction.
3456 gst_value_get_fraction_numerator (const GValue * value)
3458 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
3460 return value->data[0].v_int;
3464 * gst_value_get_fraction_denominator:
3465 * @value: a GValue initialized to #GST_TYPE_FRACTION
3467 * Gets the denominator of the fraction specified by @value.
3469 * Returns: the denominator of the fraction.
3472 gst_value_get_fraction_denominator (const GValue * value)
3474 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
3476 return value->data[1].v_int;
3480 * gst_value_fraction_multiply:
3481 * @product: a GValue initialized to #GST_TYPE_FRACTION
3482 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
3483 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
3485 * Multiplies the two GValues containing a GstFraction and sets @product
3486 * to the product of the two fractions.
3488 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3491 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
3492 const GValue * factor2)
3494 gint gcd, n1, n2, d1, d2;
3496 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
3497 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
3499 n1 = factor1->data[0].v_int;
3500 n2 = factor2->data[0].v_int;
3501 d1 = factor1->data[1].v_int;
3502 d2 = factor2->data[1].v_int;
3504 gcd = gst_greatest_common_divisor (n1, d2);
3507 gcd = gst_greatest_common_divisor (n2, d1);
3511 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
3512 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3514 gst_value_set_fraction (product, n1 * n2, d1 * d2);
3520 * gst_value_fraction_subtract:
3521 * @dest: a GValue initialized to #GST_TYPE_FRACTION
3522 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
3523 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
3525 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
3527 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3530 gst_value_fraction_subtract (GValue * dest,
3531 const GValue * minuend, const GValue * subtrahend)
3533 gint n1, n2, d1, d2;
3535 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
3536 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
3538 n1 = minuend->data[0].v_int;
3539 n2 = subtrahend->data[0].v_int;
3540 d1 = minuend->data[1].v_int;
3541 d2 = subtrahend->data[1].v_int;
3544 gst_value_set_fraction (dest, -n2, d2);
3548 gst_value_set_fraction (dest, n1, d1);
3552 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (d2), FALSE);
3553 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (n2), FALSE);
3554 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3556 gst_value_set_fraction (dest, (n1 * d2) - (n2 * d1), d1 * d2);
3562 gst_value_serialize_fraction (const GValue * value)
3564 gint32 numerator = value->data[0].v_int;
3565 gint32 denominator = value->data[1].v_int;
3566 gboolean positive = TRUE;
3568 /* get the sign and make components absolute */
3569 if (numerator < 0) {
3570 numerator = -numerator;
3571 positive = !positive;
3573 if (denominator < 0) {
3574 denominator = -denominator;
3575 positive = !positive;
3578 return g_strdup_printf ("%s%d/%d",
3579 positive ? "" : "-", numerator, denominator);
3583 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
3587 if (G_UNLIKELY (s == NULL))
3590 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
3593 if (sscanf (s, "%d/%d", &num, &den) == 2) {
3594 gst_value_set_fraction (dest, num, den);
3597 if (sscanf (s, "%d", &num) == 1) {
3598 gst_value_set_fraction (dest, num, 1);
3601 if (g_ascii_strcasecmp (s, "min") == 0) {
3602 gst_value_set_fraction (dest, -G_MAXINT, 1);
3604 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3605 gst_value_set_fraction (dest, G_MAXINT, 1);
3613 gst_value_transform_fraction_string (const GValue * src_value,
3614 GValue * dest_value)
3616 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
3620 gst_value_transform_string_fraction (const GValue * src_value,
3621 GValue * dest_value)
3623 if (!gst_value_deserialize_fraction (dest_value,
3624 src_value->data[0].v_pointer))
3625 /* If the deserialize fails, ensure we leave the fraction in a
3626 * valid, if incorrect, state */
3627 gst_value_set_fraction (dest_value, 0, 1);
3630 #define MAX_TERMS 30
3631 #define MIN_DIVISOR 1.0e-10
3632 #define MAX_ERROR 1.0e-20
3634 /* use continued fractions to transform a double into a fraction,
3635 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3636 * This algorithm takes care of overflows.
3639 gst_value_transform_double_fraction (const GValue * src_value,
3640 GValue * dest_value)
3642 gdouble V, F; /* double being converted */
3643 gint N, D; /* will contain the result */
3644 gint A; /* current term in continued fraction */
3645 gint64 N1, D1; /* numerator, denominator of last approx */
3646 gint64 N2, D2; /* numerator, denominator of previous approx */
3648 gboolean negative = FALSE;
3650 /* initialize fraction being converted */
3651 F = src_value->data[0].v_double;
3658 /* initialize fractions with 1/0, 0/1 */
3666 for (i = 0; i < MAX_TERMS; i++) {
3668 A = (gint) F; /* no floor() needed, F is always >= 0 */
3669 /* get new divisor */
3672 /* calculate new fraction in temp */
3676 /* guard against overflow */
3677 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3684 /* save last two fractions */
3690 /* quit if dividing by zero or close enough to target */
3691 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3695 /* Take reciprocal */
3698 /* fix for overflow */
3703 /* fix for negative */
3707 /* will also simplify */
3708 gst_value_set_fraction (dest_value, N, D);
3712 gst_value_transform_fraction_double (const GValue * src_value,
3713 GValue * dest_value)
3715 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
3716 ((double) src_value->data[1].v_int);
3720 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
3728 n1 = value1->data[0].v_int;
3729 n2 = value2->data[0].v_int;
3730 d1 = value1->data[1].v_int;
3731 d2 = value2->data[1].v_int;
3733 /* fractions are reduced when set, so we can quickly see if they're equal */
3734 if (n1 == n2 && d1 == d2)
3735 return GST_VALUE_EQUAL;
3737 /* extend to 64 bits */
3738 new_num_1 = ((gint64) n1) * d2;
3739 new_num_2 = ((gint64) n2) * d1;
3740 if (new_num_1 < new_num_2)
3741 return GST_VALUE_LESS_THAN;
3742 if (new_num_1 > new_num_2)
3743 return GST_VALUE_GREATER_THAN;
3745 /* new_num_1 == new_num_2 implies that both denominators must have
3746 * been 0, beause otherwise simplification would have caught the
3748 return GST_VALUE_UNORDERED;
3756 * gst_value_set_date:
3757 * @value: a GValue initialized to GST_TYPE_DATE
3758 * @date: the date to set the value to
3760 * Sets the contents of @value to coorespond to @date. The actual
3761 * #GDate structure is copied before it is used.
3764 gst_value_set_date (GValue * value, const GDate * date)
3766 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
3767 g_return_if_fail (g_date_valid (date));
3769 g_value_set_boxed (value, date);
3773 * gst_value_get_date:
3774 * @value: a GValue initialized to GST_TYPE_DATE
3776 * Gets the contents of @value.
3778 * Returns: the contents of @value
3781 gst_value_get_date (const GValue * value)
3783 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3785 return (const GDate *) g_value_get_boxed (value);
3789 gst_date_copy (gpointer boxed)
3791 const GDate *date = (const GDate *) boxed;
3793 if (!g_date_valid (date)) {
3794 GST_WARNING ("invalid GDate");
3798 return g_date_new_julian (g_date_get_julian (date));
3802 gst_value_compare_date (const GValue * value1, const GValue * value2)
3804 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3805 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3809 return GST_VALUE_EQUAL;
3811 if ((date1 == NULL || !g_date_valid (date1))
3812 && (date2 != NULL && g_date_valid (date2))) {
3813 return GST_VALUE_LESS_THAN;
3816 if ((date2 == NULL || !g_date_valid (date2))
3817 && (date1 != NULL && g_date_valid (date1))) {
3818 return GST_VALUE_GREATER_THAN;
3821 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3822 || !g_date_valid (date2)) {
3823 return GST_VALUE_UNORDERED;
3826 j1 = g_date_get_julian (date1);
3827 j2 = g_date_get_julian (date2);
3830 return GST_VALUE_EQUAL;
3832 return GST_VALUE_LESS_THAN;
3834 return GST_VALUE_GREATER_THAN;
3838 gst_value_serialize_date (const GValue * val)
3840 const GDate *date = (const GDate *) g_value_get_boxed (val);
3842 if (date == NULL || !g_date_valid (date))
3843 return g_strdup ("9999-99-99");
3845 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3846 g_date_get_month (date), g_date_get_day (date));
3850 gst_value_deserialize_date (GValue * dest, const char *s)
3852 guint year, month, day;
3854 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3857 if (!g_date_valid_dmy (day, month, year))
3860 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3865 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3867 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3871 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3873 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3877 gst_value_transform_object_string (const GValue * src_value,
3878 GValue * dest_value)
3883 obj = g_value_get_object (src_value);
3884 str = obj ? GST_OBJECT_NAME (obj) : "NULL";
3886 dest_value->data[0].v_pointer = g_strdup (str);
3889 static GTypeInfo _info = {
3902 static GTypeFundamentalInfo _finfo = {
3906 #define FUNC_VALUE_GET_TYPE(type, name) \
3907 GType gst_ ## type ## _get_type (void) \
3909 static GType gst_ ## type ## _type = 0; \
3911 if (G_UNLIKELY (gst_ ## type ## _type == 0)) { \
3912 _info.value_table = & _gst_ ## type ## _value_table; \
3913 gst_ ## type ## _type = g_type_register_fundamental ( \
3914 g_type_fundamental_next (), \
3915 name, &_info, &_finfo, 0); \
3918 return gst_ ## type ## _type; \
3921 static const GTypeValueTable _gst_fourcc_value_table = {
3922 gst_value_init_fourcc,
3924 gst_value_copy_fourcc,
3927 gst_value_collect_fourcc,
3929 gst_value_lcopy_fourcc
3932 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
3934 static const GTypeValueTable _gst_int_range_value_table = {
3935 gst_value_init_int_range,
3937 gst_value_copy_int_range,
3940 gst_value_collect_int_range,
3942 gst_value_lcopy_int_range
3945 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
3947 static const GTypeValueTable _gst_double_range_value_table = {
3948 gst_value_init_double_range,
3950 gst_value_copy_double_range,
3953 gst_value_collect_double_range,
3955 gst_value_lcopy_double_range
3958 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
3960 static const GTypeValueTable _gst_fraction_range_value_table = {
3961 gst_value_init_fraction_range,
3962 gst_value_free_fraction_range,
3963 gst_value_copy_fraction_range,
3966 gst_value_collect_fraction_range,
3968 gst_value_lcopy_fraction_range
3971 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
3973 static const GTypeValueTable _gst_value_list_value_table = {
3974 gst_value_init_list_or_array,
3975 gst_value_free_list_or_array,
3976 gst_value_copy_list_or_array,
3977 gst_value_list_or_array_peek_pointer,
3979 gst_value_collect_list_or_array,
3981 gst_value_lcopy_list_or_array
3984 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
3986 static const GTypeValueTable _gst_value_array_value_table = {
3987 gst_value_init_list_or_array,
3988 gst_value_free_list_or_array,
3989 gst_value_copy_list_or_array,
3990 gst_value_list_or_array_peek_pointer,
3992 gst_value_collect_list_or_array,
3994 gst_value_lcopy_list_or_array
3997 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
3999 static const GTypeValueTable _gst_fraction_value_table = {
4000 gst_value_init_fraction,
4002 gst_value_copy_fraction,
4005 gst_value_collect_fraction,
4007 gst_value_lcopy_fraction
4010 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
4014 gst_date_get_type (void)
4016 static GType gst_date_type = 0;
4018 if (G_UNLIKELY (gst_date_type == 0)) {
4019 /* FIXME 0.11: we require GLib 2.8 already
4020 * Not using G_TYPE_DATE here on purpose, even if we could
4021 * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
4022 * serialised strings to have different type strings depending
4023 * on what version is used, so FIXME when we require GLib-2.8 */
4024 gst_date_type = g_boxed_type_register_static ("GstDate",
4025 (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
4028 return gst_date_type;
4032 _gst_value_initialize (void)
4034 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
4035 gst_value_union_funcs = g_array_new (FALSE, FALSE,
4036 sizeof (GstValueUnionInfo));
4037 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
4038 sizeof (GstValueIntersectInfo));
4039 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4040 sizeof (GstValueSubtractInfo));
4043 static GstValueTable gst_value = {
4045 gst_value_compare_fourcc,
4046 gst_value_serialize_fourcc,
4047 gst_value_deserialize_fourcc,
4050 gst_value.type = gst_fourcc_get_type ();
4051 gst_value_register (&gst_value);
4055 static GstValueTable gst_value = {
4057 gst_value_compare_int_range,
4058 gst_value_serialize_int_range,
4059 gst_value_deserialize_int_range,
4062 gst_value.type = gst_int_range_get_type ();
4063 gst_value_register (&gst_value);
4067 static GstValueTable gst_value = {
4069 gst_value_compare_double_range,
4070 gst_value_serialize_double_range,
4071 gst_value_deserialize_double_range,
4074 gst_value.type = gst_double_range_get_type ();
4075 gst_value_register (&gst_value);
4079 static GstValueTable gst_value = {
4081 gst_value_compare_fraction_range,
4082 gst_value_serialize_fraction_range,
4083 gst_value_deserialize_fraction_range,
4086 gst_value.type = gst_fraction_range_get_type ();
4087 gst_value_register (&gst_value);
4091 static GstValueTable gst_value = {
4093 gst_value_compare_list,
4094 gst_value_serialize_list,
4095 gst_value_deserialize_list,
4098 gst_value.type = gst_value_list_get_type ();
4099 gst_value_register (&gst_value);
4103 static GstValueTable gst_value = {
4105 gst_value_compare_array,
4106 gst_value_serialize_array,
4107 gst_value_deserialize_array,
4110 gst_value.type = gst_value_array_get_type ();;
4111 gst_value_register (&gst_value);
4116 static const GTypeValueTable value_table = {
4117 gst_value_init_buffer,
4119 gst_value_copy_buffer,
4122 NULL, /*gst_value_collect_buffer, */
4124 NULL /*gst_value_lcopy_buffer */
4127 static GstValueTable gst_value = {
4129 gst_value_compare_buffer,
4130 gst_value_serialize_buffer,
4131 gst_value_deserialize_buffer,
4134 gst_value.type = GST_TYPE_BUFFER;
4135 gst_value_register (&gst_value);
4138 static GstValueTable gst_value = {
4140 gst_value_compare_fraction,
4141 gst_value_serialize_fraction,
4142 gst_value_deserialize_fraction,
4145 gst_value.type = gst_fraction_get_type ();
4146 gst_value_register (&gst_value);
4149 static GstValueTable gst_value = {
4152 gst_value_serialize_caps,
4153 gst_value_deserialize_caps,
4156 gst_value.type = GST_TYPE_CAPS;
4157 gst_value_register (&gst_value);
4160 static GstValueTable gst_value = {
4163 gst_value_serialize_structure,
4164 gst_value_deserialize_structure,
4167 gst_value.type = GST_TYPE_STRUCTURE;
4168 gst_value_register (&gst_value);
4171 static GstValueTable gst_value = {
4173 gst_value_compare_date,
4174 gst_value_serialize_date,
4175 gst_value_deserialize_date,
4178 gst_value.type = gst_date_get_type ();
4179 gst_value_register (&gst_value);
4182 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
4183 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
4185 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
4186 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
4187 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
4189 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
4191 REGISTER_SERIALIZATION (G_TYPE_INT, int);
4193 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
4194 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
4196 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
4197 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
4198 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
4200 g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
4201 gst_value_transform_fourcc_string);
4202 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
4203 gst_value_transform_int_range_string);
4204 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
4205 gst_value_transform_double_range_string);
4206 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
4207 gst_value_transform_fraction_range_string);
4208 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
4209 gst_value_transform_list_string);
4210 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
4211 gst_value_transform_array_string);
4212 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
4213 gst_value_transform_fraction_string);
4214 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
4215 gst_value_transform_string_fraction);
4216 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
4217 gst_value_transform_fraction_double);
4218 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
4219 gst_value_transform_double_fraction);
4220 g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
4221 gst_value_transform_date_string);
4222 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
4223 gst_value_transform_string_date);
4224 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
4225 gst_value_transform_object_string);
4227 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4228 gst_value_intersect_int_int_range);
4229 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4230 gst_value_intersect_int_range_int_range);
4231 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4232 gst_value_intersect_double_double_range);
4233 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
4234 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
4235 gst_value_register_intersect_func (GST_TYPE_ARRAY,
4236 GST_TYPE_ARRAY, gst_value_intersect_array);
4237 gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4238 gst_value_intersect_fraction_fraction_range);
4239 gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
4240 GST_TYPE_FRACTION_RANGE,
4241 gst_value_intersect_fraction_range_fraction_range);
4243 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4244 gst_value_subtract_int_int_range);
4245 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
4246 gst_value_subtract_int_range_int);
4247 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4248 gst_value_subtract_int_range_int_range);
4249 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4250 gst_value_subtract_double_double_range);
4251 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
4252 gst_value_subtract_double_range_double);
4253 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
4254 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
4256 gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4257 gst_value_subtract_fraction_fraction_range);
4258 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
4259 gst_value_subtract_fraction_range_fraction);
4260 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
4261 GST_TYPE_FRACTION_RANGE,
4262 gst_value_subtract_fraction_range_fraction_range);
4264 /* see bug #317246, #64994, #65041 */
4266 volatile GType date_type = G_TYPE_DATE;
4268 g_type_name (date_type);
4271 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4272 gst_value_union_int_int_range);
4273 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4274 gst_value_union_int_range_int_range);
4277 /* Implement these if needed */
4278 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4279 gst_value_union_fraction_fraction_range);
4280 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
4281 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);