2 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 * @short_description: GValue implementations specific
25 * GValue implementations specific to GStreamer.
27 * Note that operations on the same GstValue (or GValue) from multiple
28 * threads may lead to undefined behaviour.
30 * Last reviewed on 2008-03-11 (0.10.18)
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 /* estimate minimum string length to minimise re-allocs in GString */
101 s = g_string_sized_new (2 + (6 * array->len) + 2);
102 g_string_append (s, begin);
103 for (i = 0; i < array->len; i++) {
104 v = &g_array_index (array, GValue, i);
105 s_val = gst_value_serialize (v);
106 g_string_append (s, s_val);
108 if (i < array->len - 1) {
109 g_string_append_len (s, ", ", 2);
112 g_string_append (s, end);
113 return g_string_free (s, FALSE);
117 gst_value_transform_any_list_string (const GValue * src_value,
118 GValue * dest_value, const gchar * begin, const gchar * end)
126 array = src_value->data[0].v_pointer;
128 /* estimate minimum string length to minimise re-allocs in GString */
129 s = g_string_sized_new (2 + (10 * array->len) + 2);
130 g_string_append (s, begin);
131 for (i = 0; i < array->len; i++) {
132 list_value = &g_array_index (array, GValue, i);
135 g_string_append_len (s, ", ", 2);
137 list_s = g_strdup_value_contents (list_value);
138 g_string_append (s, list_s);
141 g_string_append (s, end);
143 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
147 * helper function to see if a type is fixed. Is used internally here and
148 * there. Do not export, since it doesn't work for types where the content
149 * decides the fixedness (e.g. GST_TYPE_ARRAY).
152 gst_type_is_fixed (GType type)
154 /* the basic int, string, double types */
155 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
158 /* our fundamental types that are certainly not fixed */
159 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
160 type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
163 /* other (boxed) types that are fixed */
164 if (type == GST_TYPE_BUFFER) {
168 if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
169 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
176 /* GValue functions usable for both regular lists and arrays */
178 gst_value_init_list_or_array (GValue * value)
180 value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
184 copy_garray_of_gstvalue (const GArray * src)
189 dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
190 g_array_set_size (dest, src->len);
191 for (i = 0; i < src->len; i++) {
192 gst_value_init_and_copy (&g_array_index (dest, GValue, i),
193 &g_array_index (src, GValue, i));
200 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
202 dest_value->data[0].v_pointer =
203 copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
207 gst_value_free_list_or_array (GValue * value)
210 GArray *src = (GArray *) value->data[0].v_pointer;
212 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
213 for (i = 0; i < src->len; i++) {
214 g_value_unset (&g_array_index (src, GValue, i));
216 g_array_free (src, TRUE);
221 gst_value_list_or_array_peek_pointer (const GValue * value)
223 return value->data[0].v_pointer;
227 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
228 GTypeCValue * collect_values, guint collect_flags)
230 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
231 value->data[0].v_pointer = collect_values[0].v_pointer;
232 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
234 value->data[0].v_pointer =
235 copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
241 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
242 GTypeCValue * collect_values, guint collect_flags)
244 GArray **dest = collect_values[0].v_pointer;
247 return g_strdup_printf ("value location for `%s' passed as NULL",
248 G_VALUE_TYPE_NAME (value));
249 if (!value->data[0].v_pointer)
250 return g_strdup_printf ("invalid value given for `%s'",
251 G_VALUE_TYPE_NAME (value));
252 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
253 *dest = (GArray *) value->data[0].v_pointer;
255 *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
261 * gst_value_list_append_value:
262 * @value: a #GValue of type #GST_TYPE_LIST
263 * @append_value: the value to append
265 * Appends @append_value to the GstValueList in @value.
268 gst_value_list_append_value (GValue * value, const GValue * append_value)
272 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
274 gst_value_init_and_copy (&val, append_value);
275 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
279 * gst_value_list_prepend_value:
280 * @value: a #GValue of type #GST_TYPE_LIST
281 * @prepend_value: the value to prepend
283 * Prepends @prepend_value to the GstValueList in @value.
286 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
290 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
292 gst_value_init_and_copy (&val, prepend_value);
293 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
297 * gst_value_list_concat:
298 * @dest: an uninitialized #GValue to take the result
302 * Concatenates copies of @value1 and @value2 into a list. Values that are not
303 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
304 * @dest will be initialized to the type #GST_TYPE_LIST.
307 gst_value_list_concat (GValue * dest, const GValue * value1,
308 const GValue * value2)
310 guint i, value1_length, value2_length;
313 g_return_if_fail (dest != NULL);
314 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
315 g_return_if_fail (G_IS_VALUE (value1));
316 g_return_if_fail (G_IS_VALUE (value2));
319 (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
321 (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
322 g_value_init (dest, GST_TYPE_LIST);
323 array = (GArray *) dest->data[0].v_pointer;
324 g_array_set_size (array, value1_length + value2_length);
326 if (GST_VALUE_HOLDS_LIST (value1)) {
327 for (i = 0; i < value1_length; i++) {
328 gst_value_init_and_copy (&g_array_index (array, GValue, i),
329 gst_value_list_get_value (value1, i));
332 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
335 if (GST_VALUE_HOLDS_LIST (value2)) {
336 for (i = 0; i < value2_length; i++) {
337 gst_value_init_and_copy (&g_array_index (array, GValue,
338 i + value1_length), gst_value_list_get_value (value2, i));
341 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
347 * gst_value_list_get_size:
348 * @value: a #GValue of type #GST_TYPE_LIST
350 * Gets the number of values contained in @value.
352 * Returns: the number of values
355 gst_value_list_get_size (const GValue * value)
357 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
359 return ((GArray *) value->data[0].v_pointer)->len;
363 * gst_value_list_get_value:
364 * @value: a #GValue of type #GST_TYPE_LIST
365 * @index: index of value to get from the list
367 * Gets the value that is a member of the list contained in @value and
368 * has the index @index.
370 * Returns: the value at the given index
373 gst_value_list_get_value (const GValue * value, guint index)
375 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
376 g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
378 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
383 * gst_value_array_append_value:
384 * @value: a #GValue of type #GST_TYPE_ARRAY
385 * @append_value: the value to append
387 * Appends @append_value to the GstValueArray in @value.
390 gst_value_array_append_value (GValue * value, const GValue * append_value)
394 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
396 gst_value_init_and_copy (&val, append_value);
397 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
401 * gst_value_array_prepend_value:
402 * @value: a #GValue of type #GST_TYPE_ARRAY
403 * @prepend_value: the value to prepend
405 * Prepends @prepend_value to the GstValueArray in @value.
408 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
412 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
414 gst_value_init_and_copy (&val, prepend_value);
415 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
419 * gst_value_array_get_size:
420 * @value: a #GValue of type #GST_TYPE_ARRAY
422 * Gets the number of values contained in @value.
424 * Returns: the number of values
427 gst_value_array_get_size (const GValue * value)
429 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
431 return ((GArray *) value->data[0].v_pointer)->len;
435 * gst_value_array_get_value:
436 * @value: a #GValue of type #GST_TYPE_ARRAY
437 * @index: index of value to get from the array
439 * Gets the value that is a member of the array contained in @value and
440 * has the index @index.
442 * Returns: the value at the given index
445 gst_value_array_get_value (const GValue * value, guint index)
447 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
448 g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
450 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
455 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
457 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
461 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
463 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
466 /* Do an unordered compare of the contents of a list */
468 gst_value_compare_list (const GValue * value1, const GValue * value2)
471 GArray *array1 = value1->data[0].v_pointer;
472 GArray *array2 = value2->data[0].v_pointer;
477 GstValueCompareFunc compare;
479 /* get length and do initial length check. */
481 if (len != array2->len)
482 return GST_VALUE_UNORDERED;
484 /* place to mark removed value indices of array2 */
485 removed = g_newa (guint8, len);
486 memset (removed, 0, len);
489 /* loop over array1, all items should be in array2. When we find an
490 * item in array2, remove it from array2 by marking it as removed */
491 for (i = 0; i < len; i++) {
492 v1 = &g_array_index (array1, GValue, i);
493 if ((compare = gst_value_get_compare_func (v1))) {
494 for (j = 0; j < len; j++) {
495 /* item is removed, we can skip it */
498 v2 = &g_array_index (array2, GValue, j);
499 if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
500 /* mark item as removed now that we found it in array2 and
501 * decrement the number of remaining items in array2. */
507 /* item in array1 and not in array2, UNORDERED */
509 return GST_VALUE_UNORDERED;
511 return GST_VALUE_UNORDERED;
513 /* if not all items were removed, array2 contained something not in array1 */
515 return GST_VALUE_UNORDERED;
517 /* arrays are equal */
518 return GST_VALUE_EQUAL;
521 /* Perform an ordered comparison of the contents of an array */
523 gst_value_compare_array (const GValue * value1, const GValue * value2)
526 GArray *array1 = value1->data[0].v_pointer;
527 GArray *array2 = value2->data[0].v_pointer;
531 if (array1->len != array2->len)
532 return GST_VALUE_UNORDERED;
534 for (i = 0; i < array1->len; i++) {
535 v1 = &g_array_index (array1, GValue, i);
536 v2 = &g_array_index (array2, GValue, i);
537 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
538 return GST_VALUE_UNORDERED;
541 return GST_VALUE_EQUAL;
545 gst_value_serialize_list (const GValue * value)
547 return gst_value_serialize_any_list (value, "{ ", " }");
551 gst_value_deserialize_list (GValue * dest, const gchar * s)
553 g_warning ("gst_value_deserialize_list: unimplemented");
558 gst_value_serialize_array (const GValue * value)
560 return gst_value_serialize_any_list (value, "< ", " >");
564 gst_value_deserialize_array (GValue * dest, const gchar * s)
566 g_warning ("gst_value_deserialize_array: unimplemented");
575 gst_value_init_fourcc (GValue * value)
577 value->data[0].v_int = 0;
581 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
583 dest_value->data[0].v_int = src_value->data[0].v_int;
587 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
588 GTypeCValue * collect_values, guint collect_flags)
590 value->data[0].v_int = collect_values[0].v_int;
596 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
597 GTypeCValue * collect_values, guint collect_flags)
599 guint32 *fourcc_p = collect_values[0].v_pointer;
602 return g_strdup_printf ("value location for `%s' passed as NULL",
603 G_VALUE_TYPE_NAME (value));
605 *fourcc_p = value->data[0].v_int;
611 * gst_value_set_fourcc:
612 * @value: a GValue initialized to #GST_TYPE_FOURCC
613 * @fourcc: the #guint32 fourcc to set
615 * Sets @value to @fourcc.
618 gst_value_set_fourcc (GValue * value, guint32 fourcc)
620 g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
622 value->data[0].v_int = fourcc;
626 * gst_value_get_fourcc:
627 * @value: a GValue initialized to #GST_TYPE_FOURCC
629 * Gets the #guint32 fourcc contained in @value.
631 * Returns: the #guint32 fourcc contained in @value.
634 gst_value_get_fourcc (const GValue * value)
636 g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
638 return value->data[0].v_int;
642 gst_value_transform_fourcc_string (const GValue * src_value,
645 guint32 fourcc = src_value->data[0].v_int;
647 if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
648 g_ascii_isprint ((fourcc >> 8) & 0xff) &&
649 g_ascii_isprint ((fourcc >> 16) & 0xff) &&
650 g_ascii_isprint ((fourcc >> 24) & 0xff)) {
651 dest_value->data[0].v_pointer =
652 g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
654 dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
659 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
661 if (value2->data[0].v_int == value1->data[0].v_int)
662 return GST_VALUE_EQUAL;
663 return GST_VALUE_UNORDERED;
667 gst_value_serialize_fourcc (const GValue * value)
669 guint32 fourcc = value->data[0].v_int;
671 if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
672 g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
673 g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
674 g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
675 return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
677 return g_strdup_printf ("0x%08x", fourcc);
682 gst_value_deserialize_fourcc (GValue * dest, const char *s)
684 gboolean ret = FALSE;
688 if (strlen (s) == 4) {
689 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
691 } else if (g_ascii_isdigit (*s)) {
692 fourcc = strtoul (s, &end, 0);
697 gst_value_set_fourcc (dest, fourcc);
707 gst_value_init_int_range (GValue * value)
709 value->data[0].v_int = 0;
710 value->data[1].v_int = 0;
714 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
716 dest_value->data[0].v_int = src_value->data[0].v_int;
717 dest_value->data[1].v_int = src_value->data[1].v_int;
721 gst_value_collect_int_range (GValue * value, guint n_collect_values,
722 GTypeCValue * collect_values, guint collect_flags)
724 value->data[0].v_int = collect_values[0].v_int;
725 value->data[1].v_int = collect_values[1].v_int;
731 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
732 GTypeCValue * collect_values, guint collect_flags)
734 guint32 *int_range_start = collect_values[0].v_pointer;
735 guint32 *int_range_end = collect_values[1].v_pointer;
737 if (!int_range_start)
738 return g_strdup_printf ("start value location for `%s' passed as NULL",
739 G_VALUE_TYPE_NAME (value));
741 return g_strdup_printf ("end value location for `%s' passed as NULL",
742 G_VALUE_TYPE_NAME (value));
744 *int_range_start = value->data[0].v_int;
745 *int_range_end = value->data[1].v_int;
751 * gst_value_set_int_range:
752 * @value: a GValue initialized to GST_TYPE_INT_RANGE
753 * @start: the start of the range
754 * @end: the end of the range
756 * Sets @value to the range specified by @start and @end.
759 gst_value_set_int_range (GValue * value, gint start, gint end)
761 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
762 g_return_if_fail (start < end);
764 value->data[0].v_int = start;
765 value->data[1].v_int = end;
769 * gst_value_get_int_range_min:
770 * @value: a GValue initialized to GST_TYPE_INT_RANGE
772 * Gets the minimum of the range specified by @value.
774 * Returns: the minimum of the range
777 gst_value_get_int_range_min (const GValue * value)
779 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
781 return value->data[0].v_int;
785 * gst_value_get_int_range_max:
786 * @value: a GValue initialized to GST_TYPE_INT_RANGE
788 * Gets the maximum of the range specified by @value.
790 * Returns: the maxumum of the range
793 gst_value_get_int_range_max (const GValue * value)
795 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
797 return value->data[1].v_int;
801 gst_value_transform_int_range_string (const GValue * src_value,
804 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
805 (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
809 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
811 if (value2->data[0].v_int == value1->data[0].v_int &&
812 value2->data[1].v_int == value1->data[1].v_int)
813 return GST_VALUE_EQUAL;
814 return GST_VALUE_UNORDERED;
818 gst_value_serialize_int_range (const GValue * value)
820 return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
821 value->data[1].v_int);
825 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
827 g_warning ("unimplemented");
836 gst_value_init_double_range (GValue * value)
838 value->data[0].v_double = 0;
839 value->data[1].v_double = 0;
843 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
845 dest_value->data[0].v_double = src_value->data[0].v_double;
846 dest_value->data[1].v_double = src_value->data[1].v_double;
850 gst_value_collect_double_range (GValue * value, guint n_collect_values,
851 GTypeCValue * collect_values, guint collect_flags)
853 value->data[0].v_double = collect_values[0].v_double;
854 value->data[1].v_double = collect_values[1].v_double;
860 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
861 GTypeCValue * collect_values, guint collect_flags)
863 gdouble *double_range_start = collect_values[0].v_pointer;
864 gdouble *double_range_end = collect_values[1].v_pointer;
866 if (!double_range_start)
867 return g_strdup_printf ("start value location for `%s' passed as NULL",
868 G_VALUE_TYPE_NAME (value));
869 if (!double_range_end)
870 return g_strdup_printf ("end value location for `%s' passed as NULL",
871 G_VALUE_TYPE_NAME (value));
873 *double_range_start = value->data[0].v_double;
874 *double_range_end = value->data[1].v_double;
880 * gst_value_set_double_range:
881 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
882 * @start: the start of the range
883 * @end: the end of the range
885 * Sets @value to the range specified by @start and @end.
888 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
890 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
892 value->data[0].v_double = start;
893 value->data[1].v_double = end;
897 * gst_value_get_double_range_min:
898 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
900 * Gets the minimum of the range specified by @value.
902 * Returns: the minimum of the range
905 gst_value_get_double_range_min (const GValue * value)
907 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
909 return value->data[0].v_double;
913 * gst_value_get_double_range_max:
914 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
916 * Gets the maximum of the range specified by @value.
918 * Returns: the maxumum of the range
921 gst_value_get_double_range_max (const GValue * value)
923 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
925 return value->data[1].v_double;
929 gst_value_transform_double_range_string (const GValue * src_value,
932 char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
934 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
935 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
936 src_value->data[0].v_double),
937 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
938 src_value->data[1].v_double));
942 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
944 if (value2->data[0].v_double == value1->data[0].v_double &&
945 value2->data[0].v_double == value1->data[0].v_double)
946 return GST_VALUE_EQUAL;
947 return GST_VALUE_UNORDERED;
951 gst_value_serialize_double_range (const GValue * value)
953 char d1[G_ASCII_DTOSTR_BUF_SIZE];
954 char d2[G_ASCII_DTOSTR_BUF_SIZE];
956 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
957 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
958 return g_strdup_printf ("[ %s, %s ]", d1, d2);
962 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
964 g_warning ("unimplemented");
973 gst_value_init_fraction_range (GValue * value)
977 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
978 g_value_init (&vals[0], GST_TYPE_FRACTION);
979 g_value_init (&vals[1], GST_TYPE_FRACTION);
983 gst_value_free_fraction_range (GValue * value)
985 GValue *vals = (GValue *) value->data[0].v_pointer;
988 g_value_unset (&vals[0]);
989 g_value_unset (&vals[1]);
991 value->data[0].v_pointer = NULL;
996 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
998 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
999 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1002 dest_value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1003 g_return_if_fail (vals != NULL);
1004 g_value_init (&vals[0], GST_TYPE_FRACTION);
1005 g_value_init (&vals[1], GST_TYPE_FRACTION);
1008 if (src_vals != NULL) {
1009 g_value_copy (&src_vals[0], &vals[0]);
1010 g_value_copy (&src_vals[1], &vals[1]);
1015 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1016 GTypeCValue * collect_values, guint collect_flags)
1018 GValue *vals = (GValue *) value->data[0].v_pointer;
1020 if (n_collect_values != 4)
1021 return g_strdup_printf ("not enough value locations for `%s' passed",
1022 G_VALUE_TYPE_NAME (value));
1024 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1026 return g_strdup_printf ("Could not initialise`%s' during collect",
1027 G_VALUE_TYPE_NAME (value));
1028 g_value_init (&vals[0], GST_TYPE_FRACTION);
1029 g_value_init (&vals[1], GST_TYPE_FRACTION);
1032 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1033 collect_values[1].v_int);
1034 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1035 collect_values[3].v_int);
1041 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1042 GTypeCValue * collect_values, guint collect_flags)
1045 int *dest_values[4];
1046 GValue *vals = (GValue *) value->data[0].v_pointer;
1048 if (n_collect_values != 4)
1049 return g_strdup_printf ("not enough value locations for `%s' passed",
1050 G_VALUE_TYPE_NAME (value));
1052 for (i = 0; i < 4; i++) {
1053 if (collect_values[i].v_pointer == NULL) {
1054 return g_strdup_printf ("value location for `%s' passed as NULL",
1055 G_VALUE_TYPE_NAME (value));
1057 dest_values[i] = collect_values[i].v_pointer;
1061 return g_strdup_printf ("Uninitialised `%s' passed",
1062 G_VALUE_TYPE_NAME (value));
1065 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1066 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1067 dest_values[2][0] = gst_value_get_fraction_denominator (&vals[1]);
1068 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1073 * gst_value_set_fraction_range:
1074 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1075 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1076 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1078 * Sets @value to the range specified by @start and @end.
1081 gst_value_set_fraction_range (GValue * value, const GValue * start,
1086 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1088 vals = (GValue *) value->data[0].v_pointer;
1090 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1091 g_value_init (&vals[0], GST_TYPE_FRACTION);
1092 g_value_init (&vals[1], GST_TYPE_FRACTION);
1095 g_value_copy (start, &vals[0]);
1096 g_value_copy (end, &vals[1]);
1100 * gst_value_set_fraction_range_full:
1101 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1102 * @numerator_start: the numerator start of the range
1103 * @denominator_start: the denominator start of the range
1104 * @numerator_end: the numerator end of the range
1105 * @denominator_end: the denominator end of the range
1107 * Sets @value to the range specified by @numerator_start/@denominator_start
1108 * and @numerator_end/@denominator_end.
1111 gst_value_set_fraction_range_full (GValue * value,
1112 gint numerator_start, gint denominator_start,
1113 gint numerator_end, gint denominator_end)
1115 GValue start = { 0 };
1118 g_value_init (&start, GST_TYPE_FRACTION);
1119 g_value_init (&end, GST_TYPE_FRACTION);
1121 gst_value_set_fraction (&start, numerator_start, denominator_start);
1122 gst_value_set_fraction (&end, numerator_end, denominator_end);
1123 gst_value_set_fraction_range (value, &start, &end);
1125 g_value_unset (&start);
1126 g_value_unset (&end);
1130 * gst_value_get_fraction_range_min:
1131 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1133 * Gets the minimum of the range specified by @value.
1135 * Returns: the minimum of the range
1138 gst_value_get_fraction_range_min (const GValue * value)
1142 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1144 vals = (GValue *) value->data[0].v_pointer;
1153 * gst_value_get_fraction_range_max:
1154 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1156 * Gets the maximum of the range specified by @value.
1158 * Returns: the maximum of the range
1161 gst_value_get_fraction_range_max (const GValue * value)
1165 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1167 vals = (GValue *) value->data[0].v_pointer;
1176 gst_value_serialize_fraction_range (const GValue * value)
1178 GValue *vals = (GValue *) value->data[0].v_pointer;
1182 retval = g_strdup ("[ 0/1, 0/1 ]");
1186 start = gst_value_serialize_fraction (&vals[0]);
1187 end = gst_value_serialize_fraction (&vals[1]);
1189 retval = g_strdup_printf ("[ %s, %s ]", start, end);
1198 gst_value_transform_fraction_range_string (const GValue * src_value,
1199 GValue * dest_value)
1201 dest_value->data[0].v_pointer =
1202 gst_value_serialize_fraction_range (src_value);
1206 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1208 GValue *vals1, *vals2;
1209 GstValueCompareFunc compare;
1211 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1212 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
1214 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1215 return GST_VALUE_UNORDERED;
1217 vals1 = (GValue *) value1->data[0].v_pointer;
1218 vals2 = (GValue *) value2->data[0].v_pointer;
1219 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1220 if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1222 gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1224 return GST_VALUE_EQUAL;
1226 return GST_VALUE_UNORDERED;
1230 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1232 g_warning ("unimplemented");
1241 * gst_value_set_caps:
1242 * @value: a GValue initialized to GST_TYPE_CAPS
1243 * @caps: the caps to set the value to
1245 * Sets the contents of @value to @caps. The actual
1246 * #GstCaps structure is copied before it is used.
1249 gst_value_set_caps (GValue * value, const GstCaps * caps)
1251 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1253 g_value_set_boxed (value, caps);
1257 * gst_value_get_caps:
1258 * @value: a GValue initialized to GST_TYPE_CAPS
1260 * Gets the contents of @value.
1262 * Returns: the contents of @value
1265 gst_value_get_caps (const GValue * value)
1267 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1269 return (GstCaps *) g_value_get_boxed (value);
1273 gst_value_serialize_caps (const GValue * value)
1275 GstCaps *caps = g_value_get_boxed (value);
1277 return gst_caps_to_string (caps);
1281 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1285 caps = gst_caps_from_string (s);
1288 g_value_take_boxed (dest, caps);
1299 * gst_value_set_structure:
1300 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1301 * @structure: the structure to set the value to
1303 * Sets the contents of @value to @structure. The actual
1308 gst_value_set_structure (GValue * value, const GstStructure * structure)
1310 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1312 g_value_set_boxed (value, structure);
1316 * gst_value_get_structure:
1317 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1319 * Gets the contents of @value.
1321 * Returns: the contents of @value
1325 const GstStructure *
1326 gst_value_get_structure (const GValue * value)
1328 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1330 return (GstStructure *) g_value_get_boxed (value);
1334 gst_value_serialize_structure (const GValue * value)
1336 GstStructure *structure = g_value_get_boxed (value);
1338 return gst_structure_to_string (structure);
1342 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1344 GstStructure *structure;
1346 structure = gst_structure_from_string (s, NULL);
1349 g_value_set_boxed (dest, structure);
1360 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1362 GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
1363 GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
1365 if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
1366 return GST_VALUE_UNORDERED;
1367 if (GST_BUFFER_SIZE (buf1) == 0)
1368 return GST_VALUE_EQUAL;
1369 g_assert (GST_BUFFER_DATA (buf1));
1370 g_assert (GST_BUFFER_DATA (buf2));
1371 if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
1372 GST_BUFFER_SIZE (buf1)) == 0)
1373 return GST_VALUE_EQUAL;
1375 return GST_VALUE_UNORDERED;
1379 gst_value_serialize_buffer (const GValue * value)
1387 buffer = gst_value_get_buffer (value);
1391 data = GST_BUFFER_DATA (buffer);
1392 size = GST_BUFFER_SIZE (buffer);
1394 string = g_malloc (size * 2 + 1);
1395 for (i = 0; i < size; i++) {
1396 sprintf (string + i * 2, "%02x", data[i]);
1398 string[size * 2] = 0;
1404 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1416 buffer = gst_buffer_new_and_alloc (len / 2);
1417 data = GST_BUFFER_DATA (buffer);
1418 for (i = 0; i < len / 2; i++) {
1419 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1422 ts[0] = s[i * 2 + 0];
1423 ts[1] = s[i * 2 + 1];
1426 data[i] = (guint8) strtoul (ts, NULL, 16);
1429 gst_value_take_buffer (dest, buffer);
1440 gst_buffer_unref (buffer);
1451 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1453 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1454 return GST_VALUE_EQUAL;
1455 return GST_VALUE_UNORDERED;
1459 gst_value_serialize_boolean (const GValue * value)
1461 if (value->data[0].v_int) {
1462 return g_strdup ("true");
1464 return g_strdup ("false");
1468 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1470 gboolean ret = FALSE;
1472 if (g_ascii_strcasecmp (s, "true") == 0 ||
1473 g_ascii_strcasecmp (s, "yes") == 0 ||
1474 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1475 g_value_set_boolean (dest, TRUE);
1477 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1478 g_ascii_strcasecmp (s, "no") == 0 ||
1479 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1480 g_value_set_boolean (dest, FALSE);
1487 #define CREATE_SERIALIZATION_START(_type,_macro) \
1489 gst_value_compare_ ## _type \
1490 (const GValue * value1, const GValue * value2) \
1492 g ## _type val1 = g_value_get_ ## _type (value1); \
1493 g ## _type val2 = g_value_get_ ## _type (value2); \
1495 return GST_VALUE_GREATER_THAN; \
1497 return GST_VALUE_LESS_THAN; \
1498 return GST_VALUE_EQUAL; \
1502 gst_value_serialize_ ## _type (const GValue * value) \
1504 GValue val = { 0, }; \
1505 g_value_init (&val, G_TYPE_STRING); \
1506 if (!g_value_transform (value, &val)) \
1507 g_assert_not_reached (); \
1508 /* NO_COPY_MADNESS!!! */ \
1509 return (char *) g_value_get_string (&val); \
1512 /* deserialize the given s into to as a gint64.
1513 * check if the result is actually storeable in the given size number of
1517 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1518 gint64 min, gint64 max, gint size)
1520 gboolean ret = FALSE;
1525 *to = g_ascii_strtoull (s, &end, 0);
1526 /* a range error is a definitive no-no */
1527 if (errno == ERANGE) {
1534 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1535 *to = G_LITTLE_ENDIAN;
1537 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1540 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1543 } else if (g_ascii_strcasecmp (s, "min") == 0) {
1546 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1552 /* by definition, a gint64 fits into a gint64; so ignore those */
1553 if (size != sizeof (mask)) {
1555 /* for positive numbers, we create a mask of 1's outside of the range
1556 * and 0's inside the range. An and will thus keep only 1 bits
1557 * outside of the range */
1558 mask <<= (size * 8);
1559 if ((mask & *to) != 0) {
1563 /* for negative numbers, we do a 2's complement version */
1564 mask <<= ((size * 8) - 1);
1565 if ((mask & *to) != mask) {
1574 #define CREATE_SERIALIZATION(_type,_macro) \
1575 CREATE_SERIALIZATION_START(_type,_macro) \
1578 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1582 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
1583 G_MAX ## _macro, sizeof (g ## _type))) { \
1584 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
1591 #define CREATE_USERIALIZATION(_type,_macro) \
1592 CREATE_SERIALIZATION_START(_type,_macro) \
1595 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1599 gboolean ret = FALSE; \
1602 x = g_ascii_strtoull (s, &end, 0); \
1603 /* a range error is a definitive no-no */ \
1604 if (errno == ERANGE) { \
1607 /* the cast ensures the range check later on makes sense */ \
1608 x = (g ## _type) x; \
1612 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
1613 x = G_LITTLE_ENDIAN; \
1615 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
1618 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
1621 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
1624 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
1625 x = G_MAX ## _macro; \
1630 if (x > G_MAX ## _macro) { \
1633 g_value_set_ ## _type (dest, x); \
1639 #define REGISTER_SERIALIZATION(_gtype, _type) \
1641 static const GstValueTable gst_value = { \
1643 gst_value_compare_ ## _type, \
1644 gst_value_serialize_ ## _type, \
1645 gst_value_deserialize_ ## _type, \
1648 gst_value_register (&gst_value); \
1651 CREATE_SERIALIZATION (int, INT);
1652 CREATE_SERIALIZATION (int64, INT64);
1653 CREATE_SERIALIZATION (long, LONG);
1655 CREATE_USERIALIZATION (uint, UINT);
1656 CREATE_USERIALIZATION (uint64, UINT64);
1657 CREATE_USERIALIZATION (ulong, ULONG);
1663 gst_value_compare_double (const GValue * value1, const GValue * value2)
1665 if (value1->data[0].v_double > value2->data[0].v_double)
1666 return GST_VALUE_GREATER_THAN;
1667 if (value1->data[0].v_double < value2->data[0].v_double)
1668 return GST_VALUE_LESS_THAN;
1669 if (value1->data[0].v_double == value2->data[0].v_double)
1670 return GST_VALUE_EQUAL;
1671 return GST_VALUE_UNORDERED;
1675 gst_value_serialize_double (const GValue * value)
1677 char d[G_ASCII_DTOSTR_BUF_SIZE];
1679 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1680 return g_strdup (d);
1684 gst_value_deserialize_double (GValue * dest, const gchar * s)
1687 gboolean ret = FALSE;
1690 x = g_ascii_strtod (s, &end);
1694 if (g_ascii_strcasecmp (s, "min") == 0) {
1697 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1703 g_value_set_double (dest, x);
1713 gst_value_compare_float (const GValue * value1, const GValue * value2)
1715 if (value1->data[0].v_float > value2->data[0].v_float)
1716 return GST_VALUE_GREATER_THAN;
1717 if (value1->data[0].v_float < value2->data[0].v_float)
1718 return GST_VALUE_LESS_THAN;
1719 if (value1->data[0].v_float == value2->data[0].v_float)
1720 return GST_VALUE_EQUAL;
1721 return GST_VALUE_UNORDERED;
1725 gst_value_serialize_float (const GValue * value)
1727 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
1729 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1730 return g_strdup (d);
1734 gst_value_deserialize_float (GValue * dest, const gchar * s)
1737 gboolean ret = FALSE;
1740 x = g_ascii_strtod (s, &end);
1744 if (g_ascii_strcasecmp (s, "min") == 0) {
1747 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1752 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1755 g_value_set_float (dest, (float) x);
1765 gst_value_compare_string (const GValue * value1, const GValue * value2)
1767 if (!value1->data[0].v_pointer || !value2->data[0].v_pointer) {
1768 return GST_VALUE_UNORDERED;
1770 int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1773 return GST_VALUE_LESS_THAN;
1775 return GST_VALUE_GREATER_THAN;
1776 return GST_VALUE_EQUAL;
1780 /* keep in sync with gststructure.c */
1781 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1782 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1786 gst_string_wrap (const gchar * s)
1791 gboolean wrap = FALSE;
1798 if (GST_ASCII_IS_STRING (*t)) {
1800 } else if (*t < 0x20 || *t >= 0x7f) {
1811 return g_strdup (s);
1813 e = d = g_malloc (len + 3);
1818 if (GST_ASCII_IS_STRING (*t)) {
1820 } else if (*t < 0x20 || *t >= 0x7f) {
1822 *e++ = '0' + ((*(guchar *) t) >> 6);
1823 *e++ = '0' + (((*t) >> 3) & 0x7);
1824 *e++ = '0' + ((*t++) & 0x7);
1837 * This function takes a string delimited with double quotes (")
1838 * and unescapes any \xxx octal numbers.
1840 * If sequences of \y are found where y is not in the range of
1841 * 0->3, y is copied unescaped.
1843 * If \xyy is found where x is an octal number but y is not, an
1844 * error is encountered and NULL is returned.
1846 * the input string must be \0 terminated.
1849 gst_string_unwrap (const gchar * s)
1852 gchar *read, *write;
1854 /* NULL string returns NULL */
1858 /* strings not starting with " are invalid */
1862 /* make copy of original string to hold the result. This
1863 * string will always be smaller than the original */
1868 /* need to move to the next position as we parsed the " */
1872 if (GST_ASCII_IS_STRING (*read)) {
1873 /* normal chars are just copied */
1875 } else if (*read == '"') {
1876 /* quote marks end of string */
1878 } else if (*read == '\\') {
1879 /* got an escape char, move to next position to read a tripplet
1880 * of octal numbers */
1882 /* is the next char a possible first octal number? */
1883 if (*read >= '0' && *read <= '3') {
1884 /* parse other 2 numbers, if one of them is not in the range of
1885 * an octal number, we error. We also catch the case where a zero
1886 * byte is found here. */
1887 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1890 /* now convert the octal number to a byte again. */
1891 *write++ = ((read[0] - '0') << 6) +
1892 ((read[1] - '0') << 3) + (read[2] - '0');
1896 /* if we run into a \0 here, we definately won't get a quote later */
1900 /* else copy \X sequence */
1904 /* weird character, error */
1908 /* if the string is not ending in " and zero terminated, we error */
1909 if (*read != '"' || read[1] != '\0')
1912 /* null terminate result string and return */
1922 gst_value_serialize_string (const GValue * value)
1924 return gst_string_wrap (value->data[0].v_pointer);
1928 gst_value_deserialize_string (GValue * dest, const gchar * s)
1931 if (!g_utf8_validate (s, -1, NULL))
1933 g_value_set_string (dest, s);
1936 gchar *str = gst_string_unwrap (s);
1940 g_value_take_string (dest, str);
1951 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1953 GEnumValue *en1, *en2;
1954 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1955 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1957 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1958 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1959 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1960 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
1961 g_type_class_unref (klass1);
1962 g_type_class_unref (klass2);
1963 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
1964 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
1965 if (en1->value < en2->value)
1966 return GST_VALUE_LESS_THAN;
1967 if (en1->value > en2->value)
1968 return GST_VALUE_GREATER_THAN;
1970 return GST_VALUE_EQUAL;
1974 gst_value_serialize_enum (const GValue * value)
1977 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
1979 g_return_val_if_fail (klass, NULL);
1980 en = g_enum_get_value (klass, g_value_get_enum (value));
1981 g_type_class_unref (klass);
1983 /* might be one of the custom formats registered later */
1984 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
1985 const GstFormatDefinition *format_def;
1987 format_def = gst_format_get_details (g_value_get_enum (value));
1988 g_return_val_if_fail (format_def != NULL, NULL);
1989 return g_strdup (format_def->description);
1992 g_return_val_if_fail (en, NULL);
1993 return g_strdup (en->value_name);
1997 gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
2000 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2003 return g_ascii_strcasecmp (s, format_def->description);
2007 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2010 gchar *endptr = NULL;
2011 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2013 g_return_val_if_fail (klass, FALSE);
2014 if (!(en = g_enum_get_value_by_name (klass, s))) {
2015 if (!(en = g_enum_get_value_by_nick (klass, s))) {
2016 gint i = strtol (s, &endptr, 0);
2018 if (endptr && *endptr == '\0') {
2019 en = g_enum_get_value (klass, i);
2023 g_type_class_unref (klass);
2025 /* might be one of the custom formats registered later */
2026 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2027 const GstFormatDefinition *format_def;
2030 iter = gst_format_iterate_definitions ();
2032 format_def = gst_iterator_find_custom (iter,
2033 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
2035 g_return_val_if_fail (format_def != NULL, FALSE);
2036 g_value_set_enum (dest, (gint) format_def->value);
2037 gst_iterator_free (iter);
2041 g_return_val_if_fail (en, FALSE);
2042 g_value_set_enum (dest, en->value);
2050 /* we just compare the value here */
2052 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2055 GFlagsClass *klass1 =
2056 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2057 GFlagsClass *klass2 =
2058 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2060 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2061 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2062 fl1 = g_value_get_flags (value1);
2063 fl2 = g_value_get_flags (value2);
2064 g_type_class_unref (klass1);
2065 g_type_class_unref (klass2);
2067 return GST_VALUE_LESS_THAN;
2069 return GST_VALUE_GREATER_THAN;
2071 return GST_VALUE_EQUAL;
2074 /* the different flags are serialized separated with a + */
2076 gst_value_serialize_flags (const GValue * value)
2080 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2081 gchar *result, *tmp;
2082 gboolean first = TRUE;
2084 g_return_val_if_fail (klass, NULL);
2086 flags = g_value_get_flags (value);
2088 /* if no flags are set, try to serialize to the _NONE string */
2090 fl = g_flags_get_first_value (klass, flags);
2091 return g_strdup (fl->value_name);
2094 /* some flags are set, so serialize one by one */
2095 result = g_strdup ("");
2097 fl = g_flags_get_first_value (klass, flags);
2099 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2105 flags &= ~fl->value;
2108 g_type_class_unref (klass);
2114 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2117 gchar *endptr = NULL;
2118 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2123 g_return_val_if_fail (klass, FALSE);
2125 /* split into parts delimited with + */
2126 split = g_strsplit (s, "+", 0);
2130 /* loop over each part */
2132 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2133 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2134 gint val = strtol (split[i], &endptr, 0);
2136 /* just or numeric value */
2137 if (endptr && *endptr == '\0') {
2148 g_type_class_unref (klass);
2149 g_value_set_flags (dest, flags);
2159 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2160 const GValue * src2)
2162 if (src2->data[0].v_int <= src1->data[0].v_int &&
2163 src2->data[1].v_int >= src1->data[0].v_int) {
2164 gst_value_init_and_copy (dest, src2);
2171 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2172 const GValue * src2)
2177 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2178 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2181 g_value_init (dest, GST_TYPE_INT_RANGE);
2182 gst_value_set_int_range (dest,
2183 MIN (src1->data[0].v_int, src2->data[0].v_int),
2184 MAX (src1->data[1].v_int, src2->data[1].v_int));
2196 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2197 const GValue * src2)
2199 if (src2->data[0].v_int <= src1->data[0].v_int &&
2200 src2->data[1].v_int >= src1->data[0].v_int) {
2201 gst_value_init_and_copy (dest, src1);
2209 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2210 const GValue * src2)
2215 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2216 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2219 g_value_init (dest, GST_TYPE_INT_RANGE);
2220 gst_value_set_int_range (dest, min, max);
2224 g_value_init (dest, G_TYPE_INT);
2225 g_value_set_int (dest, min);
2233 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2234 const GValue * src2)
2236 if (src2->data[0].v_double <= src1->data[0].v_double &&
2237 src2->data[1].v_double >= src1->data[0].v_double) {
2238 gst_value_init_and_copy (dest, src1);
2246 gst_value_intersect_double_range_double_range (GValue * dest,
2247 const GValue * src1, const GValue * src2)
2252 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2253 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2256 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2257 gst_value_set_double_range (dest, min, max);
2261 g_value_init (dest, G_TYPE_DOUBLE);
2262 g_value_set_int (dest, (int) min);
2270 gst_value_intersect_list (GValue * dest, const GValue * value1,
2271 const GValue * value2)
2274 GValue intersection = { 0, };
2275 gboolean ret = FALSE;
2277 size = gst_value_list_get_size (value1);
2278 for (i = 0; i < size; i++) {
2279 const GValue *cur = gst_value_list_get_value (value1, i);
2281 if (gst_value_intersect (&intersection, cur, value2)) {
2284 gst_value_init_and_copy (dest, &intersection);
2286 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2287 gst_value_list_append_value (dest, &intersection);
2289 GValue temp = { 0, };
2291 gst_value_init_and_copy (&temp, dest);
2292 g_value_unset (dest);
2293 gst_value_list_concat (dest, &temp, &intersection);
2294 g_value_unset (&temp);
2296 g_value_unset (&intersection);
2304 gst_value_intersect_array (GValue * dest, const GValue * src1,
2305 const GValue * src2)
2311 /* only works on similar-sized arrays */
2312 size = gst_value_array_get_size (src1);
2313 if (size != gst_value_array_get_size (src2))
2315 g_value_init (dest, GST_TYPE_ARRAY);
2317 for (n = 0; n < size; n++) {
2318 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2319 gst_value_array_get_value (src2, n))) {
2320 g_value_unset (dest);
2323 gst_value_array_append_value (dest, &val);
2324 g_value_unset (&val);
2331 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2332 const GValue * src2)
2336 GstValueCompareFunc compare;
2338 vals = src2->data[0].v_pointer;
2343 if ((compare = gst_value_get_compare_func (src1))) {
2344 res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2345 res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2347 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2348 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2349 gst_value_init_and_copy (dest, src1);
2358 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
2359 const GValue * src1, const GValue * src2)
2364 GValue *vals1, *vals2;
2365 GstValueCompareFunc compare;
2367 vals1 = src1->data[0].v_pointer;
2368 vals2 = src2->data[0].v_pointer;
2369 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2371 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2372 /* min = MAX (src1.start, src2.start) */
2373 res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2374 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2375 if (res == GST_VALUE_LESS_THAN)
2376 min = &vals2[0]; /* Take the max of the 2 */
2380 /* max = MIN (src1.end, src2.end) */
2381 res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2382 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2383 if (res == GST_VALUE_GREATER_THAN)
2384 max = &vals2[1]; /* Take the min of the 2 */
2388 res = gst_value_compare_with_func (min, max, compare);
2389 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2390 if (res == GST_VALUE_LESS_THAN) {
2391 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2392 vals1 = dest->data[0].v_pointer;
2393 g_value_copy (min, &vals1[0]);
2394 g_value_copy (max, &vals1[1]);
2397 if (res == GST_VALUE_EQUAL) {
2398 gst_value_init_and_copy (dest, min);
2411 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2412 const GValue * subtrahend)
2414 int min = gst_value_get_int_range_min (subtrahend);
2415 int max = gst_value_get_int_range_max (subtrahend);
2416 int val = g_value_get_int (minuend);
2418 /* subtracting a range from an int only works if the int is not in the
2420 if (val < min || val > max) {
2421 /* and the result is the int */
2422 gst_value_init_and_copy (dest, minuend);
2428 /* creates a new int range based on input values.
2431 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2436 GValue *pv1, *pv2; /* yeah, hungarian! */
2438 if (min1 <= max1 && min2 <= max2) {
2441 } else if (min1 <= max1) {
2444 } else if (min2 <= max2) {
2452 g_value_init (pv1, GST_TYPE_INT_RANGE);
2453 gst_value_set_int_range (pv1, min1, max1);
2454 } else if (min1 == max1) {
2455 g_value_init (pv1, G_TYPE_INT);
2456 g_value_set_int (pv1, min1);
2459 g_value_init (pv2, GST_TYPE_INT_RANGE);
2460 gst_value_set_int_range (pv2, min2, max2);
2461 } else if (min2 == max2) {
2462 g_value_init (pv2, G_TYPE_INT);
2463 g_value_set_int (pv2, min2);
2466 if (min1 <= max1 && min2 <= max2) {
2467 gst_value_list_concat (dest, pv1, pv2);
2468 g_value_unset (pv1);
2469 g_value_unset (pv2);
2475 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2476 const GValue * subtrahend)
2478 gint min = gst_value_get_int_range_min (minuend);
2479 gint max = gst_value_get_int_range_max (minuend);
2480 gint val = g_value_get_int (subtrahend);
2482 g_return_val_if_fail (min < max, FALSE);
2484 /* value is outside of the range, return range unchanged */
2485 if (val < min || val > max) {
2486 gst_value_init_and_copy (dest, minuend);
2489 /* max must be MAXINT too as val <= max */
2490 if (val == G_MAXINT) {
2494 /* min must be MININT too as val >= max */
2495 if (val == G_MININT) {
2499 gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2505 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2506 const GValue * subtrahend)
2508 gint min1 = gst_value_get_int_range_min (minuend);
2509 gint max1 = gst_value_get_int_range_max (minuend);
2510 gint min2 = gst_value_get_int_range_min (subtrahend);
2511 gint max2 = gst_value_get_int_range_max (subtrahend);
2513 if (max2 == G_MAXINT && min2 == G_MININT) {
2515 } else if (max2 == G_MAXINT) {
2516 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
2517 } else if (min2 == G_MININT) {
2518 return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
2520 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
2521 MAX (max2 + 1, min1), max1);
2526 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
2527 const GValue * subtrahend)
2529 gdouble min = gst_value_get_double_range_min (subtrahend);
2530 gdouble max = gst_value_get_double_range_max (subtrahend);
2531 gdouble val = g_value_get_double (minuend);
2533 if (val < min || val > max) {
2534 gst_value_init_and_copy (dest, minuend);
2541 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
2542 const GValue * subtrahend)
2544 /* since we don't have open ranges, we cannot create a hole in
2545 * a double range. We return the original range */
2546 gst_value_init_and_copy (dest, minuend);
2551 gst_value_subtract_double_range_double_range (GValue * dest,
2552 const GValue * minuend, const GValue * subtrahend)
2554 /* since we don't have open ranges, we have to approximate */
2555 /* done like with ints */
2556 gdouble min1 = gst_value_get_double_range_min (minuend);
2557 gdouble max2 = gst_value_get_double_range_max (minuend);
2558 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
2559 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
2562 GValue *pv1, *pv2; /* yeah, hungarian! */
2564 if (min1 < max1 && min2 < max2) {
2567 } else if (min1 < max1) {
2570 } else if (min2 < max2) {
2578 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
2579 gst_value_set_double_range (pv1, min1, max1);
2582 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
2583 gst_value_set_double_range (pv2, min2, max2);
2586 if (min1 < max1 && min2 < max2) {
2587 gst_value_list_concat (dest, pv1, pv2);
2588 g_value_unset (pv1);
2589 g_value_unset (pv2);
2595 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
2596 const GValue * subtrahend)
2599 GValue subtraction = { 0, };
2600 gboolean ret = FALSE;
2602 size = gst_value_list_get_size (minuend);
2603 for (i = 0; i < size; i++) {
2604 const GValue *cur = gst_value_list_get_value (minuend, i);
2606 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2608 gst_value_init_and_copy (dest, &subtraction);
2610 } else if (GST_VALUE_HOLDS_LIST (dest)
2611 && GST_VALUE_HOLDS_LIST (&subtraction)) {
2613 GValue unroll = { 0, };
2615 gst_value_init_and_copy (&unroll, dest);
2616 g_value_unset (dest);
2617 gst_value_list_concat (dest, &unroll, &subtraction);
2618 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2619 gst_value_list_append_value (dest, &subtraction);
2621 GValue temp = { 0, };
2623 gst_value_init_and_copy (&temp, dest);
2624 g_value_unset (dest);
2625 gst_value_list_concat (dest, &temp, &subtraction);
2626 g_value_unset (&temp);
2628 g_value_unset (&subtraction);
2635 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2636 const GValue * subtrahend)
2639 GValue data[2] = { {0,}, {0,} };
2640 GValue *subtraction = &data[0], *result = &data[1];
2642 gst_value_init_and_copy (result, minuend);
2643 size = gst_value_list_get_size (subtrahend);
2644 for (i = 0; i < size; i++) {
2645 const GValue *cur = gst_value_list_get_value (subtrahend, i);
2647 if (gst_value_subtract (subtraction, result, cur)) {
2648 GValue *temp = result;
2650 result = subtraction;
2652 g_value_unset (subtraction);
2654 g_value_unset (result);
2658 gst_value_init_and_copy (dest, result);
2659 g_value_unset (result);
2664 gst_value_subtract_fraction_fraction_range (GValue * dest,
2665 const GValue * minuend, const GValue * subtrahend)
2667 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
2668 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
2669 GstValueCompareFunc compare;
2671 if ((compare = gst_value_get_compare_func (minuend))) {
2672 /* subtracting a range from an fraction only works if the fraction
2673 * is not in the range */
2674 if (gst_value_compare_with_func (minuend, min, compare) ==
2675 GST_VALUE_LESS_THAN ||
2676 gst_value_compare_with_func (minuend, max, compare) ==
2677 GST_VALUE_GREATER_THAN) {
2678 /* and the result is the value */
2679 gst_value_init_and_copy (dest, minuend);
2687 gst_value_subtract_fraction_range_fraction (GValue * dest,
2688 const GValue * minuend, const GValue * subtrahend)
2690 /* since we don't have open ranges, we cannot create a hole in
2691 * a range. We return the original range */
2692 gst_value_init_and_copy (dest, minuend);
2697 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
2698 const GValue * minuend, const GValue * subtrahend)
2700 /* since we don't have open ranges, we have to approximate */
2701 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
2702 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
2703 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
2704 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
2705 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
2709 GValue *pv1, *pv2; /* yeah, hungarian! */
2710 GstValueCompareFunc compare;
2712 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
2713 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
2715 compare = gst_value_get_compare_func (min1);
2716 g_return_val_if_fail (compare, FALSE);
2718 cmp1 = gst_value_compare_with_func (max2, max1, compare);
2719 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2720 if (cmp1 == GST_VALUE_LESS_THAN)
2722 cmp1 = gst_value_compare_with_func (min1, min2, compare);
2723 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2724 if (cmp1 == GST_VALUE_GREATER_THAN)
2727 cmp1 = gst_value_compare_with_func (min1, max1, compare);
2728 cmp2 = gst_value_compare_with_func (min2, max2, compare);
2730 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2733 } else if (cmp1 == GST_VALUE_LESS_THAN) {
2736 } else if (cmp2 == GST_VALUE_LESS_THAN) {
2743 if (cmp1 == GST_VALUE_LESS_THAN) {
2744 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
2745 gst_value_set_fraction_range (pv1, min1, max1);
2747 if (cmp2 == GST_VALUE_LESS_THAN) {
2748 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
2749 gst_value_set_fraction_range (pv2, min2, max2);
2752 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2753 gst_value_list_concat (dest, pv1, pv2);
2754 g_value_unset (pv1);
2755 g_value_unset (pv2);
2766 * gst_value_can_compare:
2767 * @value1: a value to compare
2768 * @value2: another value to compare
2770 * Determines if @value1 and @value2 can be compared.
2772 * Returns: TRUE if the values can be compared
2775 gst_value_can_compare (const GValue * value1, const GValue * value2)
2777 GstValueTable *table;
2780 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2783 for (i = 0; i < gst_value_table->len; i++) {
2784 table = &g_array_index (gst_value_table, GstValueTable, i);
2785 if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2793 * gst_value_get_compare_func:
2794 * @value1: a value to get the compare function for
2796 * Determines the compare function to be used with values of the same type as
2797 * @value1. The function can be given to gst_value_compare_with_func().
2799 * Returns: A #GstValueCompareFunc value
2801 static GstValueCompareFunc
2802 gst_value_get_compare_func (const GValue * value1)
2804 GstValueTable *table, *best = NULL;
2807 /* this is a fast check */
2808 for (i = 0; i < gst_value_table->len; i++) {
2809 table = &g_array_index (gst_value_table, GstValueTable, i);
2810 if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2817 for (i = 0; i < gst_value_table->len; i++) {
2818 table = &g_array_index (gst_value_table, GstValueTable, i);
2819 if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2820 if (!best || g_type_is_a (table->type, best->type))
2826 return best->compare;
2832 * gst_value_compare:
2833 * @value1: a value to compare
2834 * @value2: another value to compare
2836 * Compares @value1 and @value2. If @value1 and @value2 cannot be
2837 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
2838 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
2839 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
2840 * If the values are equal, GST_VALUE_EQUAL is returned.
2842 * Returns: A #GstValueCompareType value
2845 gst_value_compare (const GValue * value1, const GValue * value2)
2847 GstValueCompareFunc compare;
2849 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2850 return GST_VALUE_UNORDERED;
2852 compare = gst_value_get_compare_func (value1);
2854 return compare (value1, value2);
2857 g_critical ("unable to compare values of type %s\n",
2858 g_type_name (G_VALUE_TYPE (value1)));
2859 return GST_VALUE_UNORDERED;
2863 * gst_value_compare_with_func:
2864 * @value1: a value to compare
2865 * @value2: another value to compare
2866 * @compare: compare function
2868 * Compares @value1 and @value2 using the @compare function. Works like
2869 * gst_value_compare() but allows to save time determining the compare function
2872 * Returns: A #GstValueCompareType value
2875 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
2876 GstValueCompareFunc compare)
2880 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2881 return GST_VALUE_UNORDERED;
2883 return compare (value1, value2);
2889 * gst_value_can_union:
2890 * @value1: a value to union
2891 * @value2: another value to union
2893 * Determines if @value1 and @value2 can be non-trivially unioned.
2894 * Any two values can be trivially unioned by adding both of them
2895 * to a GstValueList. However, certain types have the possibility
2896 * to be unioned in a simpler way. For example, an integer range
2897 * and an integer can be unioned if the integer is a subset of the
2898 * integer range. If there is the possibility that two values can
2899 * be unioned, this function returns TRUE.
2901 * Returns: TRUE if there is a function allowing the two values to
2905 gst_value_can_union (const GValue * value1, const GValue * value2)
2907 GstValueUnionInfo *union_info;
2910 for (i = 0; i < gst_value_union_funcs->len; i++) {
2911 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2912 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2913 union_info->type2 == G_VALUE_TYPE (value2))
2915 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2916 union_info->type2 == G_VALUE_TYPE (value1))
2925 * @dest: the destination value
2926 * @value1: a value to union
2927 * @value2: another value to union
2929 * Creates a GValue cooresponding to the union of @value1 and @value2.
2931 * Returns: always returns %TRUE
2933 /* FIXME: change return type to 'void'? */
2935 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2937 GstValueUnionInfo *union_info;
2940 for (i = 0; i < gst_value_union_funcs->len; i++) {
2941 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2942 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2943 union_info->type2 == G_VALUE_TYPE (value2)) {
2944 if (union_info->func (dest, value1, value2)) {
2948 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2949 union_info->type2 == G_VALUE_TYPE (value1)) {
2950 if (union_info->func (dest, value2, value1)) {
2956 gst_value_list_concat (dest, value1, value2);
2961 * gst_value_register_union_func:
2962 * @type1: a type to union
2963 * @type2: another type to union
2964 * @func: a function that implments creating a union between the two types
2966 * Registers a union function that can create a union between GValues
2967 * of the type @type1 and @type2.
2969 * Union functions should be registered at startup before any pipelines are
2970 * started, as gst_value_register_union_func() is not thread-safe and cannot
2971 * be used at the same time as gst_value_union() or gst_value_can_union().
2974 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
2976 GstValueUnionInfo union_info;
2978 union_info.type1 = type1;
2979 union_info.type2 = type2;
2980 union_info.func = func;
2982 g_array_append_val (gst_value_union_funcs, union_info);
2988 * gst_value_can_intersect:
2989 * @value1: a value to intersect
2990 * @value2: another value to intersect
2992 * Determines if intersecting two values will produce a valid result.
2993 * Two values will produce a valid intersection if they have the same
2994 * type, or if there is a method (registered by
2995 * gst_value_register_intersection_func()) to calculate the intersection.
2997 * Returns: TRUE if the values can intersect
3000 gst_value_can_intersect (const GValue * value1, const GValue * value2)
3002 GstValueIntersectInfo *intersect_info;
3006 if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
3009 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
3010 intersect_info = &g_array_index (gst_value_intersect_funcs,
3011 GstValueIntersectInfo, i);
3012 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
3013 intersect_info->type2 == G_VALUE_TYPE (value2))
3014 if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
3015 intersect_info->type1 == G_VALUE_TYPE (value2))
3019 return gst_value_can_compare (value1, value2);
3023 * gst_value_intersect:
3024 * @dest: a uninitialized #GValue that will hold the calculated
3025 * intersection value
3026 * @value1: a value to intersect
3027 * @value2: another value to intersect
3029 * Calculates the intersection of two values. If the values have
3030 * a non-empty intersection, the value representing the intersection
3031 * is placed in @dest. If the intersection is non-empty, @dest is
3034 * Returns: TRUE if the intersection is non-empty
3037 gst_value_intersect (GValue * dest, const GValue * value1,
3038 const GValue * value2)
3040 GstValueIntersectInfo *intersect_info;
3042 gboolean ret = FALSE;
3044 /* special cases first */
3045 if (GST_VALUE_HOLDS_LIST (value1))
3046 return gst_value_intersect_list (dest, value1, value2);
3047 if (GST_VALUE_HOLDS_LIST (value2))
3048 return gst_value_intersect_list (dest, value2, value1);
3050 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
3051 intersect_info = &g_array_index (gst_value_intersect_funcs,
3052 GstValueIntersectInfo, i);
3053 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
3054 intersect_info->type2 == G_VALUE_TYPE (value2)) {
3055 ret = intersect_info->func (dest, value1, value2);
3058 if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
3059 intersect_info->type2 == G_VALUE_TYPE (value1)) {
3060 ret = intersect_info->func (dest, value2, value1);
3065 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
3066 gst_value_init_and_copy (dest, value1);
3074 * gst_value_register_intersect_func:
3075 * @type1: the first type to intersect
3076 * @type2: the second type to intersect
3077 * @func: the intersection function
3079 * Registers a function that is called to calculate the intersection
3080 * of the values having the types @type1 and @type2.
3082 * Intersect functions should be registered at startup before any pipelines are
3083 * started, as gst_value_register_intersect_func() is not thread-safe and
3084 * cannot be used at the same time as gst_value_intersect() or
3085 * gst_value_can_intersect().
3088 gst_value_register_intersect_func (GType type1, GType type2,
3089 GstValueIntersectFunc func)
3091 GstValueIntersectInfo intersect_info;
3093 intersect_info.type1 = type1;
3094 intersect_info.type2 = type2;
3095 intersect_info.func = func;
3097 g_array_append_val (gst_value_intersect_funcs, intersect_info);
3104 * gst_value_subtract:
3105 * @dest: the destination value for the result if the subtraction is not empty
3106 * @minuend: the value to subtract from
3107 * @subtrahend: the value to subtract
3109 * Subtracts @subtrahend from @minuend and stores the result in @dest.
3110 * Note that this means subtraction as in sets, not as in mathematics.
3112 * Returns: %TRUE if the subtraction is not empty
3115 gst_value_subtract (GValue * dest, const GValue * minuend,
3116 const GValue * subtrahend)
3118 GstValueSubtractInfo *info;
3121 /* special cases first */
3122 if (GST_VALUE_HOLDS_LIST (minuend))
3123 return gst_value_subtract_from_list (dest, minuend, subtrahend);
3124 if (GST_VALUE_HOLDS_LIST (subtrahend))
3125 return gst_value_subtract_list (dest, minuend, subtrahend);
3127 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3128 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3129 if (info->minuend == G_VALUE_TYPE (minuend) &&
3130 info->subtrahend == G_VALUE_TYPE (subtrahend)) {
3131 return info->func (dest, minuend, subtrahend);
3135 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3136 gst_value_init_and_copy (dest, minuend);
3145 gst_value_subtract (GValue * dest, const GValue * minuend,
3146 const GValue * subtrahend)
3148 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3150 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
3151 gst_value_serialize (subtrahend),
3152 ret ? gst_value_serialize (dest) : "---");
3158 * gst_value_can_subtract:
3159 * @minuend: the value to subtract from
3160 * @subtrahend: the value to subtract
3162 * Checks if it's possible to subtract @subtrahend from @minuend.
3164 * Returns: TRUE if a subtraction is possible
3167 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3169 GstValueSubtractInfo *info;
3173 if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
3176 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3177 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3178 if (info->minuend == G_VALUE_TYPE (minuend) &&
3179 info->subtrahend == G_VALUE_TYPE (subtrahend))
3183 return gst_value_can_compare (minuend, subtrahend);
3187 * gst_value_register_subtract_func:
3188 * @minuend_type: type of the minuend
3189 * @subtrahend_type: type of the subtrahend
3190 * @func: function to use
3192 * Registers @func as a function capable of subtracting the values of
3193 * @subtrahend_type from values of @minuend_type.
3195 * Subtract functions should be registered at startup before any pipelines are
3196 * started, as gst_value_register_subtract_func() is not thread-safe and
3197 * cannot be used at the same time as gst_value_subtract().
3200 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3201 GstValueSubtractFunc func)
3203 GstValueSubtractInfo info;
3205 /* one type must be unfixed, other subtractions can be done as comparisons */
3206 g_return_if_fail (!gst_type_is_fixed (minuend_type)
3207 || !gst_type_is_fixed (subtrahend_type));
3209 info.minuend = minuend_type;
3210 info.subtrahend = subtrahend_type;
3213 g_array_append_val (gst_value_subtract_funcs, info);
3217 * gst_value_register:
3218 * @table: structure containing functions to register
3220 * Registers functions to perform calculations on #GValues of a given
3224 gst_value_register (const GstValueTable * table)
3226 g_array_append_val (gst_value_table, *table);
3230 * gst_value_init_and_copy:
3231 * @dest: the target value
3232 * @src: the source value
3234 * Initialises the target value to be of the same type as source and then copies
3235 * the contents from source to target.
3238 gst_value_init_and_copy (GValue * dest, const GValue * src)
3240 g_value_init (dest, G_VALUE_TYPE (src));
3241 g_value_copy (src, dest);
3245 * gst_value_serialize:
3246 * @value: a #GValue to serialize
3248 * tries to transform the given @value into a string representation that allows
3249 * getting back this string later on using gst_value_deserialize().
3251 * Returns: the serialization for @value or NULL if none exists
3254 gst_value_serialize (const GValue * value)
3257 GValue s_val = { 0 };
3258 GstValueTable *table, *best = NULL;
3261 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3263 for (i = 0; i < gst_value_table->len; i++) {
3264 table = &g_array_index (gst_value_table, GstValueTable, i);
3265 if (table->serialize == NULL)
3267 if (table->type == G_VALUE_TYPE (value)) {
3271 if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
3272 if (!best || g_type_is_a (table->type, best->type))
3277 return best->serialize (value);
3279 g_value_init (&s_val, G_TYPE_STRING);
3280 if (g_value_transform (value, &s_val)) {
3281 s = gst_string_wrap (g_value_get_string (&s_val));
3285 g_value_unset (&s_val);
3291 * gst_value_deserialize:
3292 * @dest: #GValue to fill with contents of deserialization
3293 * @src: string to deserialize
3295 * Tries to deserialize a string into the type specified by the given GValue.
3296 * If the operation succeeds, TRUE is returned, FALSE otherwise.
3298 * Returns: TRUE on success
3301 gst_value_deserialize (GValue * dest, const gchar * src)
3303 GstValueTable *table, *best = NULL;
3306 g_return_val_if_fail (src != NULL, FALSE);
3307 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
3309 for (i = 0; i < gst_value_table->len; i++) {
3310 table = &g_array_index (gst_value_table, GstValueTable, i);
3311 if (table->serialize == NULL)
3314 if (table->type == G_VALUE_TYPE (dest)) {
3319 if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
3320 if (!best || g_type_is_a (table->type, best->type))
3325 return best->deserialize (dest, src);
3332 * gst_value_is_fixed:
3333 * @value: the #GValue to check
3335 * Tests if the given GValue, if available in a GstStructure (or any other
3336 * container) contains a "fixed" (which means: one value) or an "unfixed"
3337 * (which means: multiple possible values, such as data lists or data
3340 * Returns: true if the value is "fixed".
3344 gst_value_is_fixed (const GValue * value)
3346 GType type = G_VALUE_TYPE (value);
3348 /* the most common types are just basic plain glib types */
3349 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
3353 if (type == GST_TYPE_ARRAY) {
3357 /* check recursively */
3358 size = gst_value_array_get_size (value);
3359 for (n = 0; n < size; n++) {
3360 kid = gst_value_array_get_value (value, n);
3361 if (!gst_value_is_fixed (kid))
3366 return gst_type_is_fixed (type);
3373 /* helper functions */
3375 /* Finds the greatest common divisor.
3376 * Returns 1 if none other found.
3377 * This is Euclid's algorithm. */
3379 gst_greatest_common_divisor (gint a, gint b)
3392 gst_value_init_fraction (GValue * value)
3394 value->data[0].v_int = 0;
3395 value->data[1].v_int = 1;
3399 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
3401 dest_value->data[0].v_int = src_value->data[0].v_int;
3402 dest_value->data[1].v_int = src_value->data[1].v_int;
3406 gst_value_collect_fraction (GValue * value, guint n_collect_values,
3407 GTypeCValue * collect_values, guint collect_flags)
3409 gst_value_set_fraction (value,
3410 collect_values[0].v_int, collect_values[1].v_int);
3416 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
3417 GTypeCValue * collect_values, guint collect_flags)
3419 gint *numerator = collect_values[0].v_pointer;
3420 gint *denominator = collect_values[1].v_pointer;
3423 return g_strdup_printf ("numerator for `%s' passed as NULL",
3424 G_VALUE_TYPE_NAME (value));
3426 return g_strdup_printf ("denominator for `%s' passed as NULL",
3427 G_VALUE_TYPE_NAME (value));
3429 *numerator = value->data[0].v_int;
3430 *denominator = value->data[1].v_int;
3436 * gst_value_set_fraction:
3437 * @value: a GValue initialized to #GST_TYPE_FRACTION
3438 * @numerator: the numerator of the fraction
3439 * @denominator: the denominator of the fraction
3441 * Sets @value to the fraction specified by @numerator over @denominator.
3442 * The fraction gets reduced to the smallest numerator and denominator,
3443 * and if necessary the sign is moved to the numerator.
3446 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
3450 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
3451 g_return_if_fail (denominator != 0);
3452 g_return_if_fail (denominator >= -G_MAXINT);
3453 g_return_if_fail (numerator >= -G_MAXINT);
3455 /* normalize sign */
3456 if (denominator < 0) {
3457 numerator = -numerator;
3458 denominator = -denominator;
3461 /* check for reduction */
3462 gcd = gst_greatest_common_divisor (numerator, denominator);
3468 g_assert (denominator > 0);
3470 value->data[0].v_int = numerator;
3471 value->data[1].v_int = denominator;
3475 * gst_value_get_fraction_numerator:
3476 * @value: a GValue initialized to #GST_TYPE_FRACTION
3478 * Gets the numerator of the fraction specified by @value.
3480 * Returns: the numerator of the fraction.
3483 gst_value_get_fraction_numerator (const GValue * value)
3485 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
3487 return value->data[0].v_int;
3491 * gst_value_get_fraction_denominator:
3492 * @value: a GValue initialized to #GST_TYPE_FRACTION
3494 * Gets the denominator of the fraction specified by @value.
3496 * Returns: the denominator of the fraction.
3499 gst_value_get_fraction_denominator (const GValue * value)
3501 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
3503 return value->data[1].v_int;
3507 * gst_value_fraction_multiply:
3508 * @product: a GValue initialized to #GST_TYPE_FRACTION
3509 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
3510 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
3512 * Multiplies the two GValues containing a GstFraction and sets @product
3513 * to the product of the two fractions.
3515 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3518 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
3519 const GValue * factor2)
3521 gint gcd, n1, n2, d1, d2;
3523 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
3524 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
3526 n1 = factor1->data[0].v_int;
3527 n2 = factor2->data[0].v_int;
3528 d1 = factor1->data[1].v_int;
3529 d2 = factor2->data[1].v_int;
3531 gcd = gst_greatest_common_divisor (n1, d2);
3534 gcd = gst_greatest_common_divisor (n2, d1);
3538 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
3539 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3541 gst_value_set_fraction (product, n1 * n2, d1 * d2);
3547 * gst_value_fraction_subtract:
3548 * @dest: a GValue initialized to #GST_TYPE_FRACTION
3549 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
3550 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
3552 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
3554 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3557 gst_value_fraction_subtract (GValue * dest,
3558 const GValue * minuend, const GValue * subtrahend)
3560 gint n1, n2, d1, d2;
3562 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
3563 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
3565 n1 = minuend->data[0].v_int;
3566 n2 = subtrahend->data[0].v_int;
3567 d1 = minuend->data[1].v_int;
3568 d2 = subtrahend->data[1].v_int;
3571 gst_value_set_fraction (dest, -n2, d2);
3575 gst_value_set_fraction (dest, n1, d1);
3579 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (d2), FALSE);
3580 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (n2), FALSE);
3581 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3583 gst_value_set_fraction (dest, (n1 * d2) - (n2 * d1), d1 * d2);
3589 gst_value_serialize_fraction (const GValue * value)
3591 gint32 numerator = value->data[0].v_int;
3592 gint32 denominator = value->data[1].v_int;
3593 gboolean positive = TRUE;
3595 /* get the sign and make components absolute */
3596 if (numerator < 0) {
3597 numerator = -numerator;
3598 positive = !positive;
3600 if (denominator < 0) {
3601 denominator = -denominator;
3602 positive = !positive;
3605 return g_strdup_printf ("%s%d/%d",
3606 positive ? "" : "-", numerator, denominator);
3610 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
3614 if (G_UNLIKELY (s == NULL))
3617 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
3620 if (sscanf (s, "%d/%d", &num, &den) == 2) {
3621 gst_value_set_fraction (dest, num, den);
3624 if (sscanf (s, "%d", &num) == 1) {
3625 gst_value_set_fraction (dest, num, 1);
3628 if (g_ascii_strcasecmp (s, "min") == 0) {
3629 gst_value_set_fraction (dest, -G_MAXINT, 1);
3631 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3632 gst_value_set_fraction (dest, G_MAXINT, 1);
3640 gst_value_transform_fraction_string (const GValue * src_value,
3641 GValue * dest_value)
3643 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
3647 gst_value_transform_string_fraction (const GValue * src_value,
3648 GValue * dest_value)
3650 if (!gst_value_deserialize_fraction (dest_value,
3651 src_value->data[0].v_pointer))
3652 /* If the deserialize fails, ensure we leave the fraction in a
3653 * valid, if incorrect, state */
3654 gst_value_set_fraction (dest_value, 0, 1);
3657 #define MAX_TERMS 30
3658 #define MIN_DIVISOR 1.0e-10
3659 #define MAX_ERROR 1.0e-20
3661 /* use continued fractions to transform a double into a fraction,
3662 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3663 * This algorithm takes care of overflows.
3666 gst_value_transform_double_fraction (const GValue * src_value,
3667 GValue * dest_value)
3669 gdouble V, F; /* double being converted */
3670 gint N, D; /* will contain the result */
3671 gint A; /* current term in continued fraction */
3672 gint64 N1, D1; /* numerator, denominator of last approx */
3673 gint64 N2, D2; /* numerator, denominator of previous approx */
3675 gboolean negative = FALSE;
3677 /* initialize fraction being converted */
3678 F = src_value->data[0].v_double;
3685 /* initialize fractions with 1/0, 0/1 */
3693 for (i = 0; i < MAX_TERMS; i++) {
3695 A = (gint) F; /* no floor() needed, F is always >= 0 */
3696 /* get new divisor */
3699 /* calculate new fraction in temp */
3703 /* guard against overflow */
3704 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3711 /* save last two fractions */
3717 /* quit if dividing by zero or close enough to target */
3718 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3722 /* Take reciprocal */
3725 /* fix for overflow */
3730 /* fix for negative */
3734 /* will also simplify */
3735 gst_value_set_fraction (dest_value, N, D);
3739 gst_value_transform_fraction_double (const GValue * src_value,
3740 GValue * dest_value)
3742 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
3743 ((double) src_value->data[1].v_int);
3747 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
3755 n1 = value1->data[0].v_int;
3756 n2 = value2->data[0].v_int;
3757 d1 = value1->data[1].v_int;
3758 d2 = value2->data[1].v_int;
3760 /* fractions are reduced when set, so we can quickly see if they're equal */
3761 if (n1 == n2 && d1 == d2)
3762 return GST_VALUE_EQUAL;
3764 /* extend to 64 bits */
3765 new_num_1 = ((gint64) n1) * d2;
3766 new_num_2 = ((gint64) n2) * d1;
3767 if (new_num_1 < new_num_2)
3768 return GST_VALUE_LESS_THAN;
3769 if (new_num_1 > new_num_2)
3770 return GST_VALUE_GREATER_THAN;
3772 /* new_num_1 == new_num_2 implies that both denominators must have
3773 * been 0, beause otherwise simplification would have caught the
3775 return GST_VALUE_UNORDERED;
3783 * gst_value_set_date:
3784 * @value: a GValue initialized to GST_TYPE_DATE
3785 * @date: the date to set the value to
3787 * Sets the contents of @value to coorespond to @date. The actual
3788 * #GDate structure is copied before it is used.
3791 gst_value_set_date (GValue * value, const GDate * date)
3793 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
3794 g_return_if_fail (g_date_valid (date));
3796 g_value_set_boxed (value, date);
3800 * gst_value_get_date:
3801 * @value: a GValue initialized to GST_TYPE_DATE
3803 * Gets the contents of @value.
3805 * Returns: the contents of @value
3808 gst_value_get_date (const GValue * value)
3810 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3812 return (const GDate *) g_value_get_boxed (value);
3816 gst_date_copy (gpointer boxed)
3818 const GDate *date = (const GDate *) boxed;
3820 if (!g_date_valid (date)) {
3821 GST_WARNING ("invalid GDate");
3825 return g_date_new_julian (g_date_get_julian (date));
3829 gst_value_compare_date (const GValue * value1, const GValue * value2)
3831 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3832 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3836 return GST_VALUE_EQUAL;
3838 if ((date1 == NULL || !g_date_valid (date1))
3839 && (date2 != NULL && g_date_valid (date2))) {
3840 return GST_VALUE_LESS_THAN;
3843 if ((date2 == NULL || !g_date_valid (date2))
3844 && (date1 != NULL && g_date_valid (date1))) {
3845 return GST_VALUE_GREATER_THAN;
3848 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3849 || !g_date_valid (date2)) {
3850 return GST_VALUE_UNORDERED;
3853 j1 = g_date_get_julian (date1);
3854 j2 = g_date_get_julian (date2);
3857 return GST_VALUE_EQUAL;
3859 return GST_VALUE_LESS_THAN;
3861 return GST_VALUE_GREATER_THAN;
3865 gst_value_serialize_date (const GValue * val)
3867 const GDate *date = (const GDate *) g_value_get_boxed (val);
3869 if (date == NULL || !g_date_valid (date))
3870 return g_strdup ("9999-99-99");
3872 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3873 g_date_get_month (date), g_date_get_day (date));
3877 gst_value_deserialize_date (GValue * dest, const char *s)
3879 guint year, month, day;
3881 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3884 if (!g_date_valid_dmy (day, month, year))
3887 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3892 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3894 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3898 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3900 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3904 gst_value_transform_object_string (const GValue * src_value,
3905 GValue * dest_value)
3910 obj = g_value_get_object (src_value);
3913 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
3914 GST_OBJECT_NAME (obj));
3916 str = g_strdup ("NULL");
3919 dest_value->data[0].v_pointer = str;
3922 static GTypeInfo _info = {
3935 static GTypeFundamentalInfo _finfo = {
3939 #define FUNC_VALUE_GET_TYPE(type, name) \
3940 GType gst_ ## type ## _get_type (void) \
3942 static GType gst_ ## type ## _type = 0; \
3944 if (G_UNLIKELY (gst_ ## type ## _type == 0)) { \
3945 _info.value_table = & _gst_ ## type ## _value_table; \
3946 gst_ ## type ## _type = g_type_register_fundamental ( \
3947 g_type_fundamental_next (), \
3948 name, &_info, &_finfo, 0); \
3951 return gst_ ## type ## _type; \
3954 static const GTypeValueTable _gst_fourcc_value_table = {
3955 gst_value_init_fourcc,
3957 gst_value_copy_fourcc,
3960 gst_value_collect_fourcc,
3962 gst_value_lcopy_fourcc
3965 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
3967 static const GTypeValueTable _gst_int_range_value_table = {
3968 gst_value_init_int_range,
3970 gst_value_copy_int_range,
3973 gst_value_collect_int_range,
3975 gst_value_lcopy_int_range
3978 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
3980 static const GTypeValueTable _gst_double_range_value_table = {
3981 gst_value_init_double_range,
3983 gst_value_copy_double_range,
3986 gst_value_collect_double_range,
3988 gst_value_lcopy_double_range
3991 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
3993 static const GTypeValueTable _gst_fraction_range_value_table = {
3994 gst_value_init_fraction_range,
3995 gst_value_free_fraction_range,
3996 gst_value_copy_fraction_range,
3999 gst_value_collect_fraction_range,
4001 gst_value_lcopy_fraction_range
4004 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
4006 static const GTypeValueTable _gst_value_list_value_table = {
4007 gst_value_init_list_or_array,
4008 gst_value_free_list_or_array,
4009 gst_value_copy_list_or_array,
4010 gst_value_list_or_array_peek_pointer,
4012 gst_value_collect_list_or_array,
4014 gst_value_lcopy_list_or_array
4017 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
4019 static const GTypeValueTable _gst_value_array_value_table = {
4020 gst_value_init_list_or_array,
4021 gst_value_free_list_or_array,
4022 gst_value_copy_list_or_array,
4023 gst_value_list_or_array_peek_pointer,
4025 gst_value_collect_list_or_array,
4027 gst_value_lcopy_list_or_array
4030 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
4032 static const GTypeValueTable _gst_fraction_value_table = {
4033 gst_value_init_fraction,
4035 gst_value_copy_fraction,
4038 gst_value_collect_fraction,
4040 gst_value_lcopy_fraction
4043 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
4047 gst_date_get_type (void)
4049 static GType gst_date_type = 0;
4051 if (G_UNLIKELY (gst_date_type == 0)) {
4052 /* FIXME 0.11: we require GLib 2.8 already
4053 * Not using G_TYPE_DATE here on purpose, even if we could
4054 * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
4055 * serialised strings to have different type strings depending
4056 * on what version is used, so FIXME when we require GLib-2.8 */
4057 gst_date_type = g_boxed_type_register_static ("GstDate",
4058 (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
4061 return gst_date_type;
4065 _gst_value_initialize (void)
4067 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
4068 gst_value_union_funcs = g_array_new (FALSE, FALSE,
4069 sizeof (GstValueUnionInfo));
4070 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
4071 sizeof (GstValueIntersectInfo));
4072 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4073 sizeof (GstValueSubtractInfo));
4076 static GstValueTable gst_value = {
4078 gst_value_compare_fourcc,
4079 gst_value_serialize_fourcc,
4080 gst_value_deserialize_fourcc,
4083 gst_value.type = gst_fourcc_get_type ();
4084 gst_value_register (&gst_value);
4088 static GstValueTable gst_value = {
4090 gst_value_compare_int_range,
4091 gst_value_serialize_int_range,
4092 gst_value_deserialize_int_range,
4095 gst_value.type = gst_int_range_get_type ();
4096 gst_value_register (&gst_value);
4100 static GstValueTable gst_value = {
4102 gst_value_compare_double_range,
4103 gst_value_serialize_double_range,
4104 gst_value_deserialize_double_range,
4107 gst_value.type = gst_double_range_get_type ();
4108 gst_value_register (&gst_value);
4112 static GstValueTable gst_value = {
4114 gst_value_compare_fraction_range,
4115 gst_value_serialize_fraction_range,
4116 gst_value_deserialize_fraction_range,
4119 gst_value.type = gst_fraction_range_get_type ();
4120 gst_value_register (&gst_value);
4124 static GstValueTable gst_value = {
4126 gst_value_compare_list,
4127 gst_value_serialize_list,
4128 gst_value_deserialize_list,
4131 gst_value.type = gst_value_list_get_type ();
4132 gst_value_register (&gst_value);
4136 static GstValueTable gst_value = {
4138 gst_value_compare_array,
4139 gst_value_serialize_array,
4140 gst_value_deserialize_array,
4143 gst_value.type = gst_value_array_get_type ();;
4144 gst_value_register (&gst_value);
4149 static const GTypeValueTable value_table = {
4150 gst_value_init_buffer,
4152 gst_value_copy_buffer,
4155 NULL, /*gst_value_collect_buffer, */
4157 NULL /*gst_value_lcopy_buffer */
4160 static GstValueTable gst_value = {
4162 gst_value_compare_buffer,
4163 gst_value_serialize_buffer,
4164 gst_value_deserialize_buffer,
4167 gst_value.type = GST_TYPE_BUFFER;
4168 gst_value_register (&gst_value);
4171 static GstValueTable gst_value = {
4173 gst_value_compare_fraction,
4174 gst_value_serialize_fraction,
4175 gst_value_deserialize_fraction,
4178 gst_value.type = gst_fraction_get_type ();
4179 gst_value_register (&gst_value);
4182 static GstValueTable gst_value = {
4185 gst_value_serialize_caps,
4186 gst_value_deserialize_caps,
4189 gst_value.type = GST_TYPE_CAPS;
4190 gst_value_register (&gst_value);
4193 static GstValueTable gst_value = {
4196 gst_value_serialize_structure,
4197 gst_value_deserialize_structure,
4200 gst_value.type = GST_TYPE_STRUCTURE;
4201 gst_value_register (&gst_value);
4204 static GstValueTable gst_value = {
4206 gst_value_compare_date,
4207 gst_value_serialize_date,
4208 gst_value_deserialize_date,
4211 gst_value.type = gst_date_get_type ();
4212 gst_value_register (&gst_value);
4215 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
4216 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
4218 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
4219 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
4220 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
4222 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
4224 REGISTER_SERIALIZATION (G_TYPE_INT, int);
4226 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
4227 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
4229 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
4230 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
4231 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
4233 g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
4234 gst_value_transform_fourcc_string);
4235 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
4236 gst_value_transform_int_range_string);
4237 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
4238 gst_value_transform_double_range_string);
4239 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
4240 gst_value_transform_fraction_range_string);
4241 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
4242 gst_value_transform_list_string);
4243 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
4244 gst_value_transform_array_string);
4245 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
4246 gst_value_transform_fraction_string);
4247 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
4248 gst_value_transform_string_fraction);
4249 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
4250 gst_value_transform_fraction_double);
4251 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
4252 gst_value_transform_double_fraction);
4253 g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
4254 gst_value_transform_date_string);
4255 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
4256 gst_value_transform_string_date);
4257 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
4258 gst_value_transform_object_string);
4260 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4261 gst_value_intersect_int_int_range);
4262 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4263 gst_value_intersect_int_range_int_range);
4264 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4265 gst_value_intersect_double_double_range);
4266 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
4267 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
4268 gst_value_register_intersect_func (GST_TYPE_ARRAY,
4269 GST_TYPE_ARRAY, gst_value_intersect_array);
4270 gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4271 gst_value_intersect_fraction_fraction_range);
4272 gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
4273 GST_TYPE_FRACTION_RANGE,
4274 gst_value_intersect_fraction_range_fraction_range);
4276 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4277 gst_value_subtract_int_int_range);
4278 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
4279 gst_value_subtract_int_range_int);
4280 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4281 gst_value_subtract_int_range_int_range);
4282 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4283 gst_value_subtract_double_double_range);
4284 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
4285 gst_value_subtract_double_range_double);
4286 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
4287 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
4289 gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4290 gst_value_subtract_fraction_fraction_range);
4291 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
4292 gst_value_subtract_fraction_range_fraction);
4293 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
4294 GST_TYPE_FRACTION_RANGE,
4295 gst_value_subtract_fraction_range_fraction_range);
4297 /* see bug #317246, #64994, #65041 */
4299 volatile GType date_type = G_TYPE_DATE;
4301 g_type_name (date_type);
4304 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4305 gst_value_union_int_int_range);
4306 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4307 gst_value_union_int_range_int_range);
4310 /* Implement these if needed */
4311 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4312 gst_value_union_fraction_fraction_range);
4313 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
4314 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);