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);
83 static gchar *gst_string_wrap (const gchar * s);
84 static gchar *gst_string_unwrap (const gchar * s);
90 /* two helper functions to serialize/stringify any type of list
91 * regular lists are done with { }, arrays with < >
94 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
98 GArray *array = value->data[0].v_pointer;
103 /* estimate minimum string length to minimise re-allocs in GString */
104 s = g_string_sized_new (2 + (6 * array->len) + 2);
105 g_string_append (s, begin);
106 for (i = 0; i < array->len; i++) {
107 v = &g_array_index (array, GValue, i);
108 s_val = gst_value_serialize (v);
109 g_string_append (s, s_val);
111 if (i < array->len - 1) {
112 g_string_append_len (s, ", ", 2);
115 g_string_append (s, end);
116 return g_string_free (s, FALSE);
120 gst_value_transform_any_list_string (const GValue * src_value,
121 GValue * dest_value, const gchar * begin, const gchar * end)
129 array = src_value->data[0].v_pointer;
131 /* estimate minimum string length to minimise re-allocs in GString */
132 s = g_string_sized_new (2 + (10 * array->len) + 2);
133 g_string_append (s, begin);
134 for (i = 0; i < array->len; i++) {
135 list_value = &g_array_index (array, GValue, i);
138 g_string_append_len (s, ", ", 2);
140 list_s = g_strdup_value_contents (list_value);
141 g_string_append (s, list_s);
144 g_string_append (s, end);
146 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
150 * helper function to see if a type is fixed. Is used internally here and
151 * there. Do not export, since it doesn't work for types where the content
152 * decides the fixedness (e.g. GST_TYPE_ARRAY).
155 gst_type_is_fixed (GType type)
157 /* the basic int, string, double types */
158 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
161 /* our fundamental types that are certainly not fixed */
162 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
163 type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
166 /* other (boxed) types that are fixed */
167 if (type == GST_TYPE_BUFFER) {
171 if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
172 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
179 /* GValue functions usable for both regular lists and arrays */
181 gst_value_init_list_or_array (GValue * value)
183 value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
187 copy_garray_of_gstvalue (const GArray * src)
192 dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
193 g_array_set_size (dest, src->len);
194 for (i = 0; i < src->len; i++) {
195 gst_value_init_and_copy (&g_array_index (dest, GValue, i),
196 &g_array_index (src, GValue, i));
203 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
205 dest_value->data[0].v_pointer =
206 copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
210 gst_value_free_list_or_array (GValue * value)
213 GArray *src = (GArray *) value->data[0].v_pointer;
215 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
216 for (i = 0; i < src->len; i++) {
217 g_value_unset (&g_array_index (src, GValue, i));
219 g_array_free (src, TRUE);
224 gst_value_list_or_array_peek_pointer (const GValue * value)
226 return value->data[0].v_pointer;
230 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
231 GTypeCValue * collect_values, guint collect_flags)
233 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
234 value->data[0].v_pointer = collect_values[0].v_pointer;
235 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
237 value->data[0].v_pointer =
238 copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
244 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
245 GTypeCValue * collect_values, guint collect_flags)
247 GArray **dest = collect_values[0].v_pointer;
250 return g_strdup_printf ("value location for `%s' passed as NULL",
251 G_VALUE_TYPE_NAME (value));
252 if (!value->data[0].v_pointer)
253 return g_strdup_printf ("invalid value given for `%s'",
254 G_VALUE_TYPE_NAME (value));
255 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
256 *dest = (GArray *) value->data[0].v_pointer;
258 *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
264 * gst_value_list_append_value:
265 * @value: a #GValue of type #GST_TYPE_LIST
266 * @append_value: the value to append
268 * Appends @append_value to the GstValueList in @value.
271 gst_value_list_append_value (GValue * value, const GValue * append_value)
275 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
277 gst_value_init_and_copy (&val, append_value);
278 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
282 * gst_value_list_prepend_value:
283 * @value: a #GValue of type #GST_TYPE_LIST
284 * @prepend_value: the value to prepend
286 * Prepends @prepend_value to the GstValueList in @value.
289 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
293 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
295 gst_value_init_and_copy (&val, prepend_value);
296 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
300 * gst_value_list_concat:
301 * @dest: an uninitialized #GValue to take the result
305 * Concatenates copies of @value1 and @value2 into a list. Values that are not
306 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
307 * @dest will be initialized to the type #GST_TYPE_LIST.
310 gst_value_list_concat (GValue * dest, const GValue * value1,
311 const GValue * value2)
313 guint i, value1_length, value2_length;
316 g_return_if_fail (dest != NULL);
317 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
318 g_return_if_fail (G_IS_VALUE (value1));
319 g_return_if_fail (G_IS_VALUE (value2));
322 (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
324 (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
325 g_value_init (dest, GST_TYPE_LIST);
326 array = (GArray *) dest->data[0].v_pointer;
327 g_array_set_size (array, value1_length + value2_length);
329 if (GST_VALUE_HOLDS_LIST (value1)) {
330 for (i = 0; i < value1_length; i++) {
331 gst_value_init_and_copy (&g_array_index (array, GValue, i),
332 gst_value_list_get_value (value1, i));
335 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
338 if (GST_VALUE_HOLDS_LIST (value2)) {
339 for (i = 0; i < value2_length; i++) {
340 gst_value_init_and_copy (&g_array_index (array, GValue,
341 i + value1_length), gst_value_list_get_value (value2, i));
344 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
350 * gst_value_list_get_size:
351 * @value: a #GValue of type #GST_TYPE_LIST
353 * Gets the number of values contained in @value.
355 * Returns: the number of values
358 gst_value_list_get_size (const GValue * value)
360 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
362 return ((GArray *) value->data[0].v_pointer)->len;
366 * gst_value_list_get_value:
367 * @value: a #GValue of type #GST_TYPE_LIST
368 * @index: index of value to get from the list
370 * Gets the value that is a member of the list contained in @value and
371 * has the index @index.
373 * Returns: the value at the given index
376 gst_value_list_get_value (const GValue * value, guint index)
378 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
379 g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
381 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
386 * gst_value_array_append_value:
387 * @value: a #GValue of type #GST_TYPE_ARRAY
388 * @append_value: the value to append
390 * Appends @append_value to the GstValueArray in @value.
393 gst_value_array_append_value (GValue * value, const GValue * append_value)
397 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
399 gst_value_init_and_copy (&val, append_value);
400 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
404 * gst_value_array_prepend_value:
405 * @value: a #GValue of type #GST_TYPE_ARRAY
406 * @prepend_value: the value to prepend
408 * Prepends @prepend_value to the GstValueArray in @value.
411 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
415 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
417 gst_value_init_and_copy (&val, prepend_value);
418 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
422 * gst_value_array_get_size:
423 * @value: a #GValue of type #GST_TYPE_ARRAY
425 * Gets the number of values contained in @value.
427 * Returns: the number of values
430 gst_value_array_get_size (const GValue * value)
432 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
434 return ((GArray *) value->data[0].v_pointer)->len;
438 * gst_value_array_get_value:
439 * @value: a #GValue of type #GST_TYPE_ARRAY
440 * @index: index of value to get from the array
442 * Gets the value that is a member of the array contained in @value and
443 * has the index @index.
445 * Returns: the value at the given index
448 gst_value_array_get_value (const GValue * value, guint index)
450 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
451 g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
453 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
458 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
460 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
464 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
466 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
469 /* Do an unordered compare of the contents of a list */
471 gst_value_compare_list (const GValue * value1, const GValue * value2)
474 GArray *array1 = value1->data[0].v_pointer;
475 GArray *array2 = value2->data[0].v_pointer;
480 GstValueCompareFunc compare;
482 /* get length and do initial length check. */
484 if (len != array2->len)
485 return GST_VALUE_UNORDERED;
487 /* place to mark removed value indices of array2 */
488 removed = g_newa (guint8, len);
489 memset (removed, 0, len);
492 /* loop over array1, all items should be in array2. When we find an
493 * item in array2, remove it from array2 by marking it as removed */
494 for (i = 0; i < len; i++) {
495 v1 = &g_array_index (array1, GValue, i);
496 if ((compare = gst_value_get_compare_func (v1))) {
497 for (j = 0; j < len; j++) {
498 /* item is removed, we can skip it */
501 v2 = &g_array_index (array2, GValue, j);
502 if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
503 /* mark item as removed now that we found it in array2 and
504 * decrement the number of remaining items in array2. */
510 /* item in array1 and not in array2, UNORDERED */
512 return GST_VALUE_UNORDERED;
514 return GST_VALUE_UNORDERED;
516 /* if not all items were removed, array2 contained something not in array1 */
518 return GST_VALUE_UNORDERED;
520 /* arrays are equal */
521 return GST_VALUE_EQUAL;
524 /* Perform an ordered comparison of the contents of an array */
526 gst_value_compare_array (const GValue * value1, const GValue * value2)
529 GArray *array1 = value1->data[0].v_pointer;
530 GArray *array2 = value2->data[0].v_pointer;
534 if (array1->len != array2->len)
535 return GST_VALUE_UNORDERED;
537 for (i = 0; i < array1->len; i++) {
538 v1 = &g_array_index (array1, GValue, i);
539 v2 = &g_array_index (array2, GValue, i);
540 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
541 return GST_VALUE_UNORDERED;
544 return GST_VALUE_EQUAL;
548 gst_value_serialize_list (const GValue * value)
550 return gst_value_serialize_any_list (value, "{ ", " }");
554 gst_value_deserialize_list (GValue * dest, const gchar * s)
556 g_warning ("gst_value_deserialize_list: unimplemented");
561 gst_value_serialize_array (const GValue * value)
563 return gst_value_serialize_any_list (value, "< ", " >");
567 gst_value_deserialize_array (GValue * dest, const gchar * s)
569 g_warning ("gst_value_deserialize_array: unimplemented");
578 gst_value_init_fourcc (GValue * value)
580 value->data[0].v_int = 0;
584 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
586 dest_value->data[0].v_int = src_value->data[0].v_int;
590 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
591 GTypeCValue * collect_values, guint collect_flags)
593 value->data[0].v_int = collect_values[0].v_int;
599 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
600 GTypeCValue * collect_values, guint collect_flags)
602 guint32 *fourcc_p = collect_values[0].v_pointer;
605 return g_strdup_printf ("value location for `%s' passed as NULL",
606 G_VALUE_TYPE_NAME (value));
608 *fourcc_p = value->data[0].v_int;
614 * gst_value_set_fourcc:
615 * @value: a GValue initialized to #GST_TYPE_FOURCC
616 * @fourcc: the #guint32 fourcc to set
618 * Sets @value to @fourcc.
621 gst_value_set_fourcc (GValue * value, guint32 fourcc)
623 g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
625 value->data[0].v_int = fourcc;
629 * gst_value_get_fourcc:
630 * @value: a GValue initialized to #GST_TYPE_FOURCC
632 * Gets the #guint32 fourcc contained in @value.
634 * Returns: the #guint32 fourcc contained in @value.
637 gst_value_get_fourcc (const GValue * value)
639 g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
641 return value->data[0].v_int;
645 gst_value_transform_fourcc_string (const GValue * src_value,
648 guint32 fourcc = src_value->data[0].v_int;
650 if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
651 g_ascii_isprint ((fourcc >> 8) & 0xff) &&
652 g_ascii_isprint ((fourcc >> 16) & 0xff) &&
653 g_ascii_isprint ((fourcc >> 24) & 0xff)) {
654 dest_value->data[0].v_pointer =
655 g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
657 dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
662 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
664 if (value2->data[0].v_int == value1->data[0].v_int)
665 return GST_VALUE_EQUAL;
666 return GST_VALUE_UNORDERED;
670 gst_value_serialize_fourcc (const GValue * value)
672 guint32 fourcc = value->data[0].v_int;
674 if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
675 g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
676 g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
677 g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
678 return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
680 return g_strdup_printf ("0x%08x", fourcc);
685 gst_value_deserialize_fourcc (GValue * dest, const char *s)
687 gboolean ret = FALSE;
691 if (strlen (s) == 4) {
692 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
694 } else if (g_ascii_isdigit (*s)) {
695 fourcc = strtoul (s, &end, 0);
700 gst_value_set_fourcc (dest, fourcc);
710 gst_value_init_int_range (GValue * value)
712 value->data[0].v_int = 0;
713 value->data[1].v_int = 0;
717 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
719 dest_value->data[0].v_int = src_value->data[0].v_int;
720 dest_value->data[1].v_int = src_value->data[1].v_int;
724 gst_value_collect_int_range (GValue * value, guint n_collect_values,
725 GTypeCValue * collect_values, guint collect_flags)
727 value->data[0].v_int = collect_values[0].v_int;
728 value->data[1].v_int = collect_values[1].v_int;
734 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
735 GTypeCValue * collect_values, guint collect_flags)
737 guint32 *int_range_start = collect_values[0].v_pointer;
738 guint32 *int_range_end = collect_values[1].v_pointer;
740 if (!int_range_start)
741 return g_strdup_printf ("start value location for `%s' passed as NULL",
742 G_VALUE_TYPE_NAME (value));
744 return g_strdup_printf ("end value location for `%s' passed as NULL",
745 G_VALUE_TYPE_NAME (value));
747 *int_range_start = value->data[0].v_int;
748 *int_range_end = value->data[1].v_int;
754 * gst_value_set_int_range:
755 * @value: a GValue initialized to GST_TYPE_INT_RANGE
756 * @start: the start of the range
757 * @end: the end of the range
759 * Sets @value to the range specified by @start and @end.
762 gst_value_set_int_range (GValue * value, gint start, gint end)
764 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
765 g_return_if_fail (start < end);
767 value->data[0].v_int = start;
768 value->data[1].v_int = end;
772 * gst_value_get_int_range_min:
773 * @value: a GValue initialized to GST_TYPE_INT_RANGE
775 * Gets the minimum of the range specified by @value.
777 * Returns: the minimum of the range
780 gst_value_get_int_range_min (const GValue * value)
782 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
784 return value->data[0].v_int;
788 * gst_value_get_int_range_max:
789 * @value: a GValue initialized to GST_TYPE_INT_RANGE
791 * Gets the maximum of the range specified by @value.
793 * Returns: the maxumum of the range
796 gst_value_get_int_range_max (const GValue * value)
798 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
800 return value->data[1].v_int;
804 gst_value_transform_int_range_string (const GValue * src_value,
807 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
808 (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
812 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
814 if (value2->data[0].v_int == value1->data[0].v_int &&
815 value2->data[1].v_int == value1->data[1].v_int)
816 return GST_VALUE_EQUAL;
817 return GST_VALUE_UNORDERED;
821 gst_value_serialize_int_range (const GValue * value)
823 return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
824 value->data[1].v_int);
828 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
830 g_warning ("unimplemented");
839 gst_value_init_double_range (GValue * value)
841 value->data[0].v_double = 0;
842 value->data[1].v_double = 0;
846 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
848 dest_value->data[0].v_double = src_value->data[0].v_double;
849 dest_value->data[1].v_double = src_value->data[1].v_double;
853 gst_value_collect_double_range (GValue * value, guint n_collect_values,
854 GTypeCValue * collect_values, guint collect_flags)
856 value->data[0].v_double = collect_values[0].v_double;
857 value->data[1].v_double = collect_values[1].v_double;
863 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
864 GTypeCValue * collect_values, guint collect_flags)
866 gdouble *double_range_start = collect_values[0].v_pointer;
867 gdouble *double_range_end = collect_values[1].v_pointer;
869 if (!double_range_start)
870 return g_strdup_printf ("start value location for `%s' passed as NULL",
871 G_VALUE_TYPE_NAME (value));
872 if (!double_range_end)
873 return g_strdup_printf ("end value location for `%s' passed as NULL",
874 G_VALUE_TYPE_NAME (value));
876 *double_range_start = value->data[0].v_double;
877 *double_range_end = value->data[1].v_double;
883 * gst_value_set_double_range:
884 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
885 * @start: the start of the range
886 * @end: the end of the range
888 * Sets @value to the range specified by @start and @end.
891 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
893 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
895 value->data[0].v_double = start;
896 value->data[1].v_double = end;
900 * gst_value_get_double_range_min:
901 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
903 * Gets the minimum of the range specified by @value.
905 * Returns: the minimum of the range
908 gst_value_get_double_range_min (const GValue * value)
910 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
912 return value->data[0].v_double;
916 * gst_value_get_double_range_max:
917 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
919 * Gets the maximum of the range specified by @value.
921 * Returns: the maxumum of the range
924 gst_value_get_double_range_max (const GValue * value)
926 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
928 return value->data[1].v_double;
932 gst_value_transform_double_range_string (const GValue * src_value,
935 char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
937 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
938 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
939 src_value->data[0].v_double),
940 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
941 src_value->data[1].v_double));
945 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
947 if (value2->data[0].v_double == value1->data[0].v_double &&
948 value2->data[0].v_double == value1->data[0].v_double)
949 return GST_VALUE_EQUAL;
950 return GST_VALUE_UNORDERED;
954 gst_value_serialize_double_range (const GValue * value)
956 char d1[G_ASCII_DTOSTR_BUF_SIZE];
957 char d2[G_ASCII_DTOSTR_BUF_SIZE];
959 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
960 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
961 return g_strdup_printf ("[ %s, %s ]", d1, d2);
965 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
967 g_warning ("unimplemented");
976 gst_value_init_fraction_range (GValue * value)
980 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
981 g_value_init (&vals[0], GST_TYPE_FRACTION);
982 g_value_init (&vals[1], GST_TYPE_FRACTION);
986 gst_value_free_fraction_range (GValue * value)
988 GValue *vals = (GValue *) value->data[0].v_pointer;
991 g_value_unset (&vals[0]);
992 g_value_unset (&vals[1]);
994 value->data[0].v_pointer = NULL;
999 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1001 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1002 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1005 dest_value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1006 g_return_if_fail (vals != NULL);
1007 g_value_init (&vals[0], GST_TYPE_FRACTION);
1008 g_value_init (&vals[1], GST_TYPE_FRACTION);
1011 if (src_vals != NULL) {
1012 g_value_copy (&src_vals[0], &vals[0]);
1013 g_value_copy (&src_vals[1], &vals[1]);
1018 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1019 GTypeCValue * collect_values, guint collect_flags)
1021 GValue *vals = (GValue *) value->data[0].v_pointer;
1023 if (n_collect_values != 4)
1024 return g_strdup_printf ("not enough value locations for `%s' passed",
1025 G_VALUE_TYPE_NAME (value));
1027 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1029 return g_strdup_printf ("Could not initialise`%s' during collect",
1030 G_VALUE_TYPE_NAME (value));
1031 g_value_init (&vals[0], GST_TYPE_FRACTION);
1032 g_value_init (&vals[1], GST_TYPE_FRACTION);
1035 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1036 collect_values[1].v_int);
1037 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1038 collect_values[3].v_int);
1044 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1045 GTypeCValue * collect_values, guint collect_flags)
1048 int *dest_values[4];
1049 GValue *vals = (GValue *) value->data[0].v_pointer;
1051 if (n_collect_values != 4)
1052 return g_strdup_printf ("not enough value locations for `%s' passed",
1053 G_VALUE_TYPE_NAME (value));
1055 for (i = 0; i < 4; i++) {
1056 if (collect_values[i].v_pointer == NULL) {
1057 return g_strdup_printf ("value location for `%s' passed as NULL",
1058 G_VALUE_TYPE_NAME (value));
1060 dest_values[i] = collect_values[i].v_pointer;
1064 return g_strdup_printf ("Uninitialised `%s' passed",
1065 G_VALUE_TYPE_NAME (value));
1068 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1069 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1070 dest_values[2][0] = gst_value_get_fraction_denominator (&vals[1]);
1071 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1076 * gst_value_set_fraction_range:
1077 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1078 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1079 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1081 * Sets @value to the range specified by @start and @end.
1084 gst_value_set_fraction_range (GValue * value, const GValue * start,
1089 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1091 vals = (GValue *) value->data[0].v_pointer;
1093 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1094 g_value_init (&vals[0], GST_TYPE_FRACTION);
1095 g_value_init (&vals[1], GST_TYPE_FRACTION);
1098 g_value_copy (start, &vals[0]);
1099 g_value_copy (end, &vals[1]);
1103 * gst_value_set_fraction_range_full:
1104 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1105 * @numerator_start: the numerator start of the range
1106 * @denominator_start: the denominator start of the range
1107 * @numerator_end: the numerator end of the range
1108 * @denominator_end: the denominator end of the range
1110 * Sets @value to the range specified by @numerator_start/@denominator_start
1111 * and @numerator_end/@denominator_end.
1114 gst_value_set_fraction_range_full (GValue * value,
1115 gint numerator_start, gint denominator_start,
1116 gint numerator_end, gint denominator_end)
1118 GValue start = { 0 };
1121 g_value_init (&start, GST_TYPE_FRACTION);
1122 g_value_init (&end, GST_TYPE_FRACTION);
1124 gst_value_set_fraction (&start, numerator_start, denominator_start);
1125 gst_value_set_fraction (&end, numerator_end, denominator_end);
1126 gst_value_set_fraction_range (value, &start, &end);
1128 g_value_unset (&start);
1129 g_value_unset (&end);
1133 * gst_value_get_fraction_range_min:
1134 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1136 * Gets the minimum of the range specified by @value.
1138 * Returns: the minimum of the range
1141 gst_value_get_fraction_range_min (const GValue * value)
1145 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1147 vals = (GValue *) value->data[0].v_pointer;
1156 * gst_value_get_fraction_range_max:
1157 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1159 * Gets the maximum of the range specified by @value.
1161 * Returns: the maximum of the range
1164 gst_value_get_fraction_range_max (const GValue * value)
1168 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1170 vals = (GValue *) value->data[0].v_pointer;
1179 gst_value_serialize_fraction_range (const GValue * value)
1181 GValue *vals = (GValue *) value->data[0].v_pointer;
1185 retval = g_strdup ("[ 0/1, 0/1 ]");
1189 start = gst_value_serialize_fraction (&vals[0]);
1190 end = gst_value_serialize_fraction (&vals[1]);
1192 retval = g_strdup_printf ("[ %s, %s ]", start, end);
1201 gst_value_transform_fraction_range_string (const GValue * src_value,
1202 GValue * dest_value)
1204 dest_value->data[0].v_pointer =
1205 gst_value_serialize_fraction_range (src_value);
1209 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1211 GValue *vals1, *vals2;
1212 GstValueCompareFunc compare;
1214 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1215 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
1217 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1218 return GST_VALUE_UNORDERED;
1220 vals1 = (GValue *) value1->data[0].v_pointer;
1221 vals2 = (GValue *) value2->data[0].v_pointer;
1222 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1223 if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1225 gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1227 return GST_VALUE_EQUAL;
1229 return GST_VALUE_UNORDERED;
1233 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1235 g_warning ("unimplemented");
1244 * gst_value_set_caps:
1245 * @value: a GValue initialized to GST_TYPE_CAPS
1246 * @caps: the caps to set the value to
1248 * Sets the contents of @value to @caps. The actual
1249 * #GstCaps structure is copied before it is used.
1252 gst_value_set_caps (GValue * value, const GstCaps * caps)
1254 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1256 g_value_set_boxed (value, caps);
1260 * gst_value_get_caps:
1261 * @value: a GValue initialized to GST_TYPE_CAPS
1263 * Gets the contents of @value.
1265 * Returns: the contents of @value
1268 gst_value_get_caps (const GValue * value)
1270 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1272 return (GstCaps *) g_value_get_boxed (value);
1276 gst_value_serialize_caps (const GValue * value)
1278 GstCaps *caps = g_value_get_boxed (value);
1280 return gst_caps_to_string (caps);
1284 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1288 caps = gst_caps_from_string (s);
1291 g_value_take_boxed (dest, caps);
1302 * gst_value_set_structure:
1303 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1304 * @structure: the structure to set the value to
1306 * Sets the contents of @value to @structure. The actual
1311 gst_value_set_structure (GValue * value, const GstStructure * structure)
1313 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1315 g_value_set_boxed (value, structure);
1319 * gst_value_get_structure:
1320 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1322 * Gets the contents of @value.
1324 * Returns: the contents of @value
1328 const GstStructure *
1329 gst_value_get_structure (const GValue * value)
1331 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1333 return (GstStructure *) g_value_get_boxed (value);
1337 gst_value_serialize_structure (const GValue * value)
1339 GstStructure *structure = g_value_get_boxed (value);
1341 return gst_string_wrap (gst_structure_to_string (structure));
1345 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1347 GstStructure *structure;
1350 structure = gst_structure_from_string (s, NULL);
1352 gchar *str = gst_string_unwrap (s);
1357 structure = gst_structure_from_string (str, NULL);
1361 g_value_set_boxed (dest, structure);
1372 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1374 GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
1375 GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
1377 if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
1378 return GST_VALUE_UNORDERED;
1379 if (GST_BUFFER_SIZE (buf1) == 0)
1380 return GST_VALUE_EQUAL;
1381 g_assert (GST_BUFFER_DATA (buf1));
1382 g_assert (GST_BUFFER_DATA (buf2));
1383 if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
1384 GST_BUFFER_SIZE (buf1)) == 0)
1385 return GST_VALUE_EQUAL;
1387 return GST_VALUE_UNORDERED;
1391 gst_value_serialize_buffer (const GValue * value)
1399 buffer = gst_value_get_buffer (value);
1403 data = GST_BUFFER_DATA (buffer);
1404 size = GST_BUFFER_SIZE (buffer);
1406 string = g_malloc (size * 2 + 1);
1407 for (i = 0; i < size; i++) {
1408 sprintf (string + i * 2, "%02x", data[i]);
1410 string[size * 2] = 0;
1416 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1428 buffer = gst_buffer_new_and_alloc (len / 2);
1429 data = GST_BUFFER_DATA (buffer);
1430 for (i = 0; i < len / 2; i++) {
1431 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1434 ts[0] = s[i * 2 + 0];
1435 ts[1] = s[i * 2 + 1];
1438 data[i] = (guint8) strtoul (ts, NULL, 16);
1441 gst_value_take_buffer (dest, buffer);
1452 gst_buffer_unref (buffer);
1463 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1465 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1466 return GST_VALUE_EQUAL;
1467 return GST_VALUE_UNORDERED;
1471 gst_value_serialize_boolean (const GValue * value)
1473 if (value->data[0].v_int) {
1474 return g_strdup ("true");
1476 return g_strdup ("false");
1480 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1482 gboolean ret = FALSE;
1484 if (g_ascii_strcasecmp (s, "true") == 0 ||
1485 g_ascii_strcasecmp (s, "yes") == 0 ||
1486 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1487 g_value_set_boolean (dest, TRUE);
1489 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1490 g_ascii_strcasecmp (s, "no") == 0 ||
1491 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1492 g_value_set_boolean (dest, FALSE);
1499 #define CREATE_SERIALIZATION_START(_type,_macro) \
1501 gst_value_compare_ ## _type \
1502 (const GValue * value1, const GValue * value2) \
1504 g ## _type val1 = g_value_get_ ## _type (value1); \
1505 g ## _type val2 = g_value_get_ ## _type (value2); \
1507 return GST_VALUE_GREATER_THAN; \
1509 return GST_VALUE_LESS_THAN; \
1510 return GST_VALUE_EQUAL; \
1514 gst_value_serialize_ ## _type (const GValue * value) \
1516 GValue val = { 0, }; \
1517 g_value_init (&val, G_TYPE_STRING); \
1518 if (!g_value_transform (value, &val)) \
1519 g_assert_not_reached (); \
1520 /* NO_COPY_MADNESS!!! */ \
1521 return (char *) g_value_get_string (&val); \
1524 /* deserialize the given s into to as a gint64.
1525 * check if the result is actually storeable in the given size number of
1529 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1530 gint64 min, gint64 max, gint size)
1532 gboolean ret = FALSE;
1537 *to = g_ascii_strtoull (s, &end, 0);
1538 /* a range error is a definitive no-no */
1539 if (errno == ERANGE) {
1546 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1547 *to = G_LITTLE_ENDIAN;
1549 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1552 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1555 } else if (g_ascii_strcasecmp (s, "min") == 0) {
1558 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1564 /* by definition, a gint64 fits into a gint64; so ignore those */
1565 if (size != sizeof (mask)) {
1567 /* for positive numbers, we create a mask of 1's outside of the range
1568 * and 0's inside the range. An and will thus keep only 1 bits
1569 * outside of the range */
1570 mask <<= (size * 8);
1571 if ((mask & *to) != 0) {
1575 /* for negative numbers, we do a 2's complement version */
1576 mask <<= ((size * 8) - 1);
1577 if ((mask & *to) != mask) {
1586 #define CREATE_SERIALIZATION(_type,_macro) \
1587 CREATE_SERIALIZATION_START(_type,_macro) \
1590 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1594 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
1595 G_MAX ## _macro, sizeof (g ## _type))) { \
1596 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
1603 #define CREATE_USERIALIZATION(_type,_macro) \
1604 CREATE_SERIALIZATION_START(_type,_macro) \
1607 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1611 gboolean ret = FALSE; \
1614 x = g_ascii_strtoull (s, &end, 0); \
1615 /* a range error is a definitive no-no */ \
1616 if (errno == ERANGE) { \
1619 /* the cast ensures the range check later on makes sense */ \
1620 x = (g ## _type) x; \
1624 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
1625 x = G_LITTLE_ENDIAN; \
1627 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
1630 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
1633 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
1636 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
1637 x = G_MAX ## _macro; \
1642 if (x > G_MAX ## _macro) { \
1645 g_value_set_ ## _type (dest, x); \
1651 #define REGISTER_SERIALIZATION(_gtype, _type) \
1653 static const GstValueTable gst_value = { \
1655 gst_value_compare_ ## _type, \
1656 gst_value_serialize_ ## _type, \
1657 gst_value_deserialize_ ## _type, \
1660 gst_value_register (&gst_value); \
1663 CREATE_SERIALIZATION (int, INT);
1664 CREATE_SERIALIZATION (int64, INT64);
1665 CREATE_SERIALIZATION (long, LONG);
1667 CREATE_USERIALIZATION (uint, UINT);
1668 CREATE_USERIALIZATION (uint64, UINT64);
1669 CREATE_USERIALIZATION (ulong, ULONG);
1675 gst_value_compare_double (const GValue * value1, const GValue * value2)
1677 if (value1->data[0].v_double > value2->data[0].v_double)
1678 return GST_VALUE_GREATER_THAN;
1679 if (value1->data[0].v_double < value2->data[0].v_double)
1680 return GST_VALUE_LESS_THAN;
1681 if (value1->data[0].v_double == value2->data[0].v_double)
1682 return GST_VALUE_EQUAL;
1683 return GST_VALUE_UNORDERED;
1687 gst_value_serialize_double (const GValue * value)
1689 char d[G_ASCII_DTOSTR_BUF_SIZE];
1691 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1692 return g_strdup (d);
1696 gst_value_deserialize_double (GValue * dest, const gchar * s)
1699 gboolean ret = FALSE;
1702 x = g_ascii_strtod (s, &end);
1706 if (g_ascii_strcasecmp (s, "min") == 0) {
1709 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1715 g_value_set_double (dest, x);
1725 gst_value_compare_float (const GValue * value1, const GValue * value2)
1727 if (value1->data[0].v_float > value2->data[0].v_float)
1728 return GST_VALUE_GREATER_THAN;
1729 if (value1->data[0].v_float < value2->data[0].v_float)
1730 return GST_VALUE_LESS_THAN;
1731 if (value1->data[0].v_float == value2->data[0].v_float)
1732 return GST_VALUE_EQUAL;
1733 return GST_VALUE_UNORDERED;
1737 gst_value_serialize_float (const GValue * value)
1739 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
1741 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1742 return g_strdup (d);
1746 gst_value_deserialize_float (GValue * dest, const gchar * s)
1749 gboolean ret = FALSE;
1752 x = g_ascii_strtod (s, &end);
1756 if (g_ascii_strcasecmp (s, "min") == 0) {
1759 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1764 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1767 g_value_set_float (dest, (float) x);
1777 gst_value_compare_string (const GValue * value1, const GValue * value2)
1779 if (!value1->data[0].v_pointer || !value2->data[0].v_pointer) {
1780 return GST_VALUE_UNORDERED;
1782 int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1785 return GST_VALUE_LESS_THAN;
1787 return GST_VALUE_GREATER_THAN;
1788 return GST_VALUE_EQUAL;
1793 gst_string_wrap (const gchar * s)
1798 gboolean wrap = FALSE;
1805 if (GST_ASCII_IS_STRING (*t)) {
1807 } else if (*t < 0x20 || *t >= 0x7f) {
1818 return g_strdup (s);
1820 e = d = g_malloc (len + 3);
1825 if (GST_ASCII_IS_STRING (*t)) {
1827 } else if (*t < 0x20 || *t >= 0x7f) {
1829 *e++ = '0' + ((*(guchar *) t) >> 6);
1830 *e++ = '0' + (((*t) >> 3) & 0x7);
1831 *e++ = '0' + ((*t++) & 0x7);
1844 * This function takes a string delimited with double quotes (")
1845 * and unescapes any \xxx octal numbers.
1847 * If sequences of \y are found where y is not in the range of
1848 * 0->3, y is copied unescaped.
1850 * If \xyy is found where x is an octal number but y is not, an
1851 * error is encountered and NULL is returned.
1853 * the input string must be \0 terminated.
1856 gst_string_unwrap (const gchar * s)
1859 gchar *read, *write;
1861 /* NULL string returns NULL */
1865 /* strings not starting with " are invalid */
1869 /* make copy of original string to hold the result. This
1870 * string will always be smaller than the original */
1875 /* need to move to the next position as we parsed the " */
1879 if (GST_ASCII_IS_STRING (*read)) {
1880 /* normal chars are just copied */
1882 } else if (*read == '"') {
1883 /* quote marks end of string */
1885 } else if (*read == '\\') {
1886 /* got an escape char, move to next position to read a tripplet
1887 * of octal numbers */
1889 /* is the next char a possible first octal number? */
1890 if (*read >= '0' && *read <= '3') {
1891 /* parse other 2 numbers, if one of them is not in the range of
1892 * an octal number, we error. We also catch the case where a zero
1893 * byte is found here. */
1894 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1897 /* now convert the octal number to a byte again. */
1898 *write++ = ((read[0] - '0') << 6) +
1899 ((read[1] - '0') << 3) + (read[2] - '0');
1903 /* if we run into a \0 here, we definately won't get a quote later */
1907 /* else copy \X sequence */
1911 /* weird character, error */
1915 /* if the string is not ending in " and zero terminated, we error */
1916 if (*read != '"' || read[1] != '\0')
1919 /* null terminate result string and return */
1929 gst_value_serialize_string (const GValue * value)
1931 return gst_string_wrap (value->data[0].v_pointer);
1935 gst_value_deserialize_string (GValue * dest, const gchar * s)
1938 if (!g_utf8_validate (s, -1, NULL))
1940 g_value_set_string (dest, s);
1943 gchar *str = gst_string_unwrap (s);
1947 g_value_take_string (dest, str);
1958 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1960 GEnumValue *en1, *en2;
1961 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1962 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1964 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1965 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1966 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1967 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
1968 g_type_class_unref (klass1);
1969 g_type_class_unref (klass2);
1970 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
1971 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
1972 if (en1->value < en2->value)
1973 return GST_VALUE_LESS_THAN;
1974 if (en1->value > en2->value)
1975 return GST_VALUE_GREATER_THAN;
1977 return GST_VALUE_EQUAL;
1981 gst_value_serialize_enum (const GValue * value)
1984 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
1986 g_return_val_if_fail (klass, NULL);
1987 en = g_enum_get_value (klass, g_value_get_enum (value));
1988 g_type_class_unref (klass);
1990 /* might be one of the custom formats registered later */
1991 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
1992 const GstFormatDefinition *format_def;
1994 format_def = gst_format_get_details (g_value_get_enum (value));
1995 g_return_val_if_fail (format_def != NULL, NULL);
1996 return g_strdup (format_def->description);
1999 g_return_val_if_fail (en, NULL);
2000 return g_strdup (en->value_name);
2004 gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
2007 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2010 return g_ascii_strcasecmp (s, format_def->description);
2014 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2017 gchar *endptr = NULL;
2018 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2020 g_return_val_if_fail (klass, FALSE);
2021 if (!(en = g_enum_get_value_by_name (klass, s))) {
2022 if (!(en = g_enum_get_value_by_nick (klass, s))) {
2023 gint i = strtol (s, &endptr, 0);
2025 if (endptr && *endptr == '\0') {
2026 en = g_enum_get_value (klass, i);
2030 g_type_class_unref (klass);
2032 /* might be one of the custom formats registered later */
2033 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2034 const GstFormatDefinition *format_def;
2037 iter = gst_format_iterate_definitions ();
2039 format_def = gst_iterator_find_custom (iter,
2040 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
2042 g_return_val_if_fail (format_def != NULL, FALSE);
2043 g_value_set_enum (dest, (gint) format_def->value);
2044 gst_iterator_free (iter);
2048 g_return_val_if_fail (en, FALSE);
2049 g_value_set_enum (dest, en->value);
2057 /* we just compare the value here */
2059 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2062 GFlagsClass *klass1 =
2063 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2064 GFlagsClass *klass2 =
2065 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2067 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2068 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2069 fl1 = g_value_get_flags (value1);
2070 fl2 = g_value_get_flags (value2);
2071 g_type_class_unref (klass1);
2072 g_type_class_unref (klass2);
2074 return GST_VALUE_LESS_THAN;
2076 return GST_VALUE_GREATER_THAN;
2078 return GST_VALUE_EQUAL;
2081 /* the different flags are serialized separated with a + */
2083 gst_value_serialize_flags (const GValue * value)
2087 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2088 gchar *result, *tmp;
2089 gboolean first = TRUE;
2091 g_return_val_if_fail (klass, NULL);
2093 flags = g_value_get_flags (value);
2095 /* if no flags are set, try to serialize to the _NONE string */
2097 fl = g_flags_get_first_value (klass, flags);
2098 return g_strdup (fl->value_name);
2101 /* some flags are set, so serialize one by one */
2102 result = g_strdup ("");
2104 fl = g_flags_get_first_value (klass, flags);
2106 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2112 flags &= ~fl->value;
2115 g_type_class_unref (klass);
2121 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2124 gchar *endptr = NULL;
2125 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2130 g_return_val_if_fail (klass, FALSE);
2132 /* split into parts delimited with + */
2133 split = g_strsplit (s, "+", 0);
2137 /* loop over each part */
2139 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2140 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2141 gint val = strtol (split[i], &endptr, 0);
2143 /* just or numeric value */
2144 if (endptr && *endptr == '\0') {
2155 g_type_class_unref (klass);
2156 g_value_set_flags (dest, flags);
2166 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2167 const GValue * src2)
2169 if (src2->data[0].v_int <= src1->data[0].v_int &&
2170 src2->data[1].v_int >= src1->data[0].v_int) {
2171 gst_value_init_and_copy (dest, src2);
2178 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2179 const GValue * src2)
2184 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2185 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2188 g_value_init (dest, GST_TYPE_INT_RANGE);
2189 gst_value_set_int_range (dest,
2190 MIN (src1->data[0].v_int, src2->data[0].v_int),
2191 MAX (src1->data[1].v_int, src2->data[1].v_int));
2203 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2204 const GValue * src2)
2206 if (src2->data[0].v_int <= src1->data[0].v_int &&
2207 src2->data[1].v_int >= src1->data[0].v_int) {
2208 gst_value_init_and_copy (dest, src1);
2216 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2217 const GValue * src2)
2222 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2223 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2226 g_value_init (dest, GST_TYPE_INT_RANGE);
2227 gst_value_set_int_range (dest, min, max);
2231 g_value_init (dest, G_TYPE_INT);
2232 g_value_set_int (dest, min);
2240 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2241 const GValue * src2)
2243 if (src2->data[0].v_double <= src1->data[0].v_double &&
2244 src2->data[1].v_double >= src1->data[0].v_double) {
2245 gst_value_init_and_copy (dest, src1);
2253 gst_value_intersect_double_range_double_range (GValue * dest,
2254 const GValue * src1, const GValue * src2)
2259 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2260 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2263 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2264 gst_value_set_double_range (dest, min, max);
2268 g_value_init (dest, G_TYPE_DOUBLE);
2269 g_value_set_int (dest, (int) min);
2277 gst_value_intersect_list (GValue * dest, const GValue * value1,
2278 const GValue * value2)
2281 GValue intersection = { 0, };
2282 gboolean ret = FALSE;
2284 size = gst_value_list_get_size (value1);
2285 for (i = 0; i < size; i++) {
2286 const GValue *cur = gst_value_list_get_value (value1, i);
2288 if (gst_value_intersect (&intersection, cur, value2)) {
2291 gst_value_init_and_copy (dest, &intersection);
2293 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2294 gst_value_list_append_value (dest, &intersection);
2296 GValue temp = { 0, };
2298 gst_value_init_and_copy (&temp, dest);
2299 g_value_unset (dest);
2300 gst_value_list_concat (dest, &temp, &intersection);
2301 g_value_unset (&temp);
2303 g_value_unset (&intersection);
2311 gst_value_intersect_array (GValue * dest, const GValue * src1,
2312 const GValue * src2)
2318 /* only works on similar-sized arrays */
2319 size = gst_value_array_get_size (src1);
2320 if (size != gst_value_array_get_size (src2))
2322 g_value_init (dest, GST_TYPE_ARRAY);
2324 for (n = 0; n < size; n++) {
2325 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2326 gst_value_array_get_value (src2, n))) {
2327 g_value_unset (dest);
2330 gst_value_array_append_value (dest, &val);
2331 g_value_unset (&val);
2338 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2339 const GValue * src2)
2343 GstValueCompareFunc compare;
2345 vals = src2->data[0].v_pointer;
2350 if ((compare = gst_value_get_compare_func (src1))) {
2351 res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2352 res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2354 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2355 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2356 gst_value_init_and_copy (dest, src1);
2365 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
2366 const GValue * src1, const GValue * src2)
2371 GValue *vals1, *vals2;
2372 GstValueCompareFunc compare;
2374 vals1 = src1->data[0].v_pointer;
2375 vals2 = src2->data[0].v_pointer;
2376 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2378 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2379 /* min = MAX (src1.start, src2.start) */
2380 res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2381 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2382 if (res == GST_VALUE_LESS_THAN)
2383 min = &vals2[0]; /* Take the max of the 2 */
2387 /* max = MIN (src1.end, src2.end) */
2388 res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2389 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2390 if (res == GST_VALUE_GREATER_THAN)
2391 max = &vals2[1]; /* Take the min of the 2 */
2395 res = gst_value_compare_with_func (min, max, compare);
2396 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2397 if (res == GST_VALUE_LESS_THAN) {
2398 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2399 vals1 = dest->data[0].v_pointer;
2400 g_value_copy (min, &vals1[0]);
2401 g_value_copy (max, &vals1[1]);
2404 if (res == GST_VALUE_EQUAL) {
2405 gst_value_init_and_copy (dest, min);
2418 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2419 const GValue * subtrahend)
2421 int min = gst_value_get_int_range_min (subtrahend);
2422 int max = gst_value_get_int_range_max (subtrahend);
2423 int val = g_value_get_int (minuend);
2425 /* subtracting a range from an int only works if the int is not in the
2427 if (val < min || val > max) {
2428 /* and the result is the int */
2429 gst_value_init_and_copy (dest, minuend);
2435 /* creates a new int range based on input values.
2438 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2443 GValue *pv1, *pv2; /* yeah, hungarian! */
2445 if (min1 <= max1 && min2 <= max2) {
2448 } else if (min1 <= max1) {
2451 } else if (min2 <= max2) {
2459 g_value_init (pv1, GST_TYPE_INT_RANGE);
2460 gst_value_set_int_range (pv1, min1, max1);
2461 } else if (min1 == max1) {
2462 g_value_init (pv1, G_TYPE_INT);
2463 g_value_set_int (pv1, min1);
2466 g_value_init (pv2, GST_TYPE_INT_RANGE);
2467 gst_value_set_int_range (pv2, min2, max2);
2468 } else if (min2 == max2) {
2469 g_value_init (pv2, G_TYPE_INT);
2470 g_value_set_int (pv2, min2);
2473 if (min1 <= max1 && min2 <= max2) {
2474 gst_value_list_concat (dest, pv1, pv2);
2475 g_value_unset (pv1);
2476 g_value_unset (pv2);
2482 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2483 const GValue * subtrahend)
2485 gint min = gst_value_get_int_range_min (minuend);
2486 gint max = gst_value_get_int_range_max (minuend);
2487 gint val = g_value_get_int (subtrahend);
2489 g_return_val_if_fail (min < max, FALSE);
2491 /* value is outside of the range, return range unchanged */
2492 if (val < min || val > max) {
2493 gst_value_init_and_copy (dest, minuend);
2496 /* max must be MAXINT too as val <= max */
2497 if (val == G_MAXINT) {
2501 /* min must be MININT too as val >= max */
2502 if (val == G_MININT) {
2506 gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2512 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2513 const GValue * subtrahend)
2515 gint min1 = gst_value_get_int_range_min (minuend);
2516 gint max1 = gst_value_get_int_range_max (minuend);
2517 gint min2 = gst_value_get_int_range_min (subtrahend);
2518 gint max2 = gst_value_get_int_range_max (subtrahend);
2520 if (max2 == G_MAXINT && min2 == G_MININT) {
2522 } else if (max2 == G_MAXINT) {
2523 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
2524 } else if (min2 == G_MININT) {
2525 return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
2527 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
2528 MAX (max2 + 1, min1), max1);
2533 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
2534 const GValue * subtrahend)
2536 gdouble min = gst_value_get_double_range_min (subtrahend);
2537 gdouble max = gst_value_get_double_range_max (subtrahend);
2538 gdouble val = g_value_get_double (minuend);
2540 if (val < min || val > max) {
2541 gst_value_init_and_copy (dest, minuend);
2548 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
2549 const GValue * subtrahend)
2551 /* since we don't have open ranges, we cannot create a hole in
2552 * a double range. We return the original range */
2553 gst_value_init_and_copy (dest, minuend);
2558 gst_value_subtract_double_range_double_range (GValue * dest,
2559 const GValue * minuend, const GValue * subtrahend)
2561 /* since we don't have open ranges, we have to approximate */
2562 /* done like with ints */
2563 gdouble min1 = gst_value_get_double_range_min (minuend);
2564 gdouble max2 = gst_value_get_double_range_max (minuend);
2565 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
2566 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
2569 GValue *pv1, *pv2; /* yeah, hungarian! */
2571 if (min1 < max1 && min2 < max2) {
2574 } else if (min1 < max1) {
2577 } else if (min2 < max2) {
2585 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
2586 gst_value_set_double_range (pv1, min1, max1);
2589 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
2590 gst_value_set_double_range (pv2, min2, max2);
2593 if (min1 < max1 && min2 < max2) {
2594 gst_value_list_concat (dest, pv1, pv2);
2595 g_value_unset (pv1);
2596 g_value_unset (pv2);
2602 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
2603 const GValue * subtrahend)
2606 GValue subtraction = { 0, };
2607 gboolean ret = FALSE;
2609 size = gst_value_list_get_size (minuend);
2610 for (i = 0; i < size; i++) {
2611 const GValue *cur = gst_value_list_get_value (minuend, i);
2613 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2615 gst_value_init_and_copy (dest, &subtraction);
2617 } else if (GST_VALUE_HOLDS_LIST (dest)
2618 && GST_VALUE_HOLDS_LIST (&subtraction)) {
2620 GValue unroll = { 0, };
2622 gst_value_init_and_copy (&unroll, dest);
2623 g_value_unset (dest);
2624 gst_value_list_concat (dest, &unroll, &subtraction);
2625 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2626 gst_value_list_append_value (dest, &subtraction);
2628 GValue temp = { 0, };
2630 gst_value_init_and_copy (&temp, dest);
2631 g_value_unset (dest);
2632 gst_value_list_concat (dest, &temp, &subtraction);
2633 g_value_unset (&temp);
2635 g_value_unset (&subtraction);
2642 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2643 const GValue * subtrahend)
2646 GValue data[2] = { {0,}, {0,} };
2647 GValue *subtraction = &data[0], *result = &data[1];
2649 gst_value_init_and_copy (result, minuend);
2650 size = gst_value_list_get_size (subtrahend);
2651 for (i = 0; i < size; i++) {
2652 const GValue *cur = gst_value_list_get_value (subtrahend, i);
2654 if (gst_value_subtract (subtraction, result, cur)) {
2655 GValue *temp = result;
2657 result = subtraction;
2659 g_value_unset (subtraction);
2661 g_value_unset (result);
2665 gst_value_init_and_copy (dest, result);
2666 g_value_unset (result);
2671 gst_value_subtract_fraction_fraction_range (GValue * dest,
2672 const GValue * minuend, const GValue * subtrahend)
2674 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
2675 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
2676 GstValueCompareFunc compare;
2678 if ((compare = gst_value_get_compare_func (minuend))) {
2679 /* subtracting a range from an fraction only works if the fraction
2680 * is not in the range */
2681 if (gst_value_compare_with_func (minuend, min, compare) ==
2682 GST_VALUE_LESS_THAN ||
2683 gst_value_compare_with_func (minuend, max, compare) ==
2684 GST_VALUE_GREATER_THAN) {
2685 /* and the result is the value */
2686 gst_value_init_and_copy (dest, minuend);
2694 gst_value_subtract_fraction_range_fraction (GValue * dest,
2695 const GValue * minuend, const GValue * subtrahend)
2697 /* since we don't have open ranges, we cannot create a hole in
2698 * a range. We return the original range */
2699 gst_value_init_and_copy (dest, minuend);
2704 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
2705 const GValue * minuend, const GValue * subtrahend)
2707 /* since we don't have open ranges, we have to approximate */
2708 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
2709 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
2710 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
2711 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
2712 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
2716 GValue *pv1, *pv2; /* yeah, hungarian! */
2717 GstValueCompareFunc compare;
2719 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
2720 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
2722 compare = gst_value_get_compare_func (min1);
2723 g_return_val_if_fail (compare, FALSE);
2725 cmp1 = gst_value_compare_with_func (max2, max1, compare);
2726 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2727 if (cmp1 == GST_VALUE_LESS_THAN)
2729 cmp1 = gst_value_compare_with_func (min1, min2, compare);
2730 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2731 if (cmp1 == GST_VALUE_GREATER_THAN)
2734 cmp1 = gst_value_compare_with_func (min1, max1, compare);
2735 cmp2 = gst_value_compare_with_func (min2, max2, compare);
2737 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2740 } else if (cmp1 == GST_VALUE_LESS_THAN) {
2743 } else if (cmp2 == GST_VALUE_LESS_THAN) {
2750 if (cmp1 == GST_VALUE_LESS_THAN) {
2751 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
2752 gst_value_set_fraction_range (pv1, min1, max1);
2754 if (cmp2 == GST_VALUE_LESS_THAN) {
2755 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
2756 gst_value_set_fraction_range (pv2, min2, max2);
2759 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2760 gst_value_list_concat (dest, pv1, pv2);
2761 g_value_unset (pv1);
2762 g_value_unset (pv2);
2773 * gst_value_can_compare:
2774 * @value1: a value to compare
2775 * @value2: another value to compare
2777 * Determines if @value1 and @value2 can be compared.
2779 * Returns: TRUE if the values can be compared
2782 gst_value_can_compare (const GValue * value1, const GValue * value2)
2784 GstValueTable *table;
2787 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2790 for (i = 0; i < gst_value_table->len; i++) {
2791 table = &g_array_index (gst_value_table, GstValueTable, i);
2792 if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2800 * gst_value_get_compare_func:
2801 * @value1: a value to get the compare function for
2803 * Determines the compare function to be used with values of the same type as
2804 * @value1. The function can be given to gst_value_compare_with_func().
2806 * Returns: A #GstValueCompareFunc value
2808 static GstValueCompareFunc
2809 gst_value_get_compare_func (const GValue * value1)
2811 GstValueTable *table, *best = NULL;
2814 /* this is a fast check */
2815 for (i = 0; i < gst_value_table->len; i++) {
2816 table = &g_array_index (gst_value_table, GstValueTable, i);
2817 if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2824 for (i = 0; i < gst_value_table->len; i++) {
2825 table = &g_array_index (gst_value_table, GstValueTable, i);
2826 if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2827 if (!best || g_type_is_a (table->type, best->type))
2833 return best->compare;
2839 * gst_value_compare:
2840 * @value1: a value to compare
2841 * @value2: another value to compare
2843 * Compares @value1 and @value2. If @value1 and @value2 cannot be
2844 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
2845 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
2846 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
2847 * If the values are equal, GST_VALUE_EQUAL is returned.
2849 * Returns: A #GstValueCompareType value
2852 gst_value_compare (const GValue * value1, const GValue * value2)
2854 GstValueCompareFunc compare;
2856 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2857 return GST_VALUE_UNORDERED;
2859 compare = gst_value_get_compare_func (value1);
2861 return compare (value1, value2);
2864 g_critical ("unable to compare values of type %s\n",
2865 g_type_name (G_VALUE_TYPE (value1)));
2866 return GST_VALUE_UNORDERED;
2870 * gst_value_compare_with_func:
2871 * @value1: a value to compare
2872 * @value2: another value to compare
2873 * @compare: compare function
2875 * Compares @value1 and @value2 using the @compare function. Works like
2876 * gst_value_compare() but allows to save time determining the compare function
2879 * Returns: A #GstValueCompareType value
2882 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
2883 GstValueCompareFunc compare)
2887 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2888 return GST_VALUE_UNORDERED;
2890 return compare (value1, value2);
2896 * gst_value_can_union:
2897 * @value1: a value to union
2898 * @value2: another value to union
2900 * Determines if @value1 and @value2 can be non-trivially unioned.
2901 * Any two values can be trivially unioned by adding both of them
2902 * to a GstValueList. However, certain types have the possibility
2903 * to be unioned in a simpler way. For example, an integer range
2904 * and an integer can be unioned if the integer is a subset of the
2905 * integer range. If there is the possibility that two values can
2906 * be unioned, this function returns TRUE.
2908 * Returns: TRUE if there is a function allowing the two values to
2912 gst_value_can_union (const GValue * value1, const GValue * value2)
2914 GstValueUnionInfo *union_info;
2917 for (i = 0; i < gst_value_union_funcs->len; i++) {
2918 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2919 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2920 union_info->type2 == G_VALUE_TYPE (value2))
2922 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2923 union_info->type2 == G_VALUE_TYPE (value1))
2932 * @dest: the destination value
2933 * @value1: a value to union
2934 * @value2: another value to union
2936 * Creates a GValue cooresponding to the union of @value1 and @value2.
2938 * Returns: always returns %TRUE
2940 /* FIXME: change return type to 'void'? */
2942 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2944 GstValueUnionInfo *union_info;
2947 for (i = 0; i < gst_value_union_funcs->len; i++) {
2948 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2949 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2950 union_info->type2 == G_VALUE_TYPE (value2)) {
2951 if (union_info->func (dest, value1, value2)) {
2955 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2956 union_info->type2 == G_VALUE_TYPE (value1)) {
2957 if (union_info->func (dest, value2, value1)) {
2963 gst_value_list_concat (dest, value1, value2);
2968 * gst_value_register_union_func:
2969 * @type1: a type to union
2970 * @type2: another type to union
2971 * @func: a function that implments creating a union between the two types
2973 * Registers a union function that can create a union between GValues
2974 * of the type @type1 and @type2.
2976 * Union functions should be registered at startup before any pipelines are
2977 * started, as gst_value_register_union_func() is not thread-safe and cannot
2978 * be used at the same time as gst_value_union() or gst_value_can_union().
2981 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
2983 GstValueUnionInfo union_info;
2985 union_info.type1 = type1;
2986 union_info.type2 = type2;
2987 union_info.func = func;
2989 g_array_append_val (gst_value_union_funcs, union_info);
2995 * gst_value_can_intersect:
2996 * @value1: a value to intersect
2997 * @value2: another value to intersect
2999 * Determines if intersecting two values will produce a valid result.
3000 * Two values will produce a valid intersection if they have the same
3001 * type, or if there is a method (registered by
3002 * gst_value_register_intersection_func()) to calculate the intersection.
3004 * Returns: TRUE if the values can intersect
3007 gst_value_can_intersect (const GValue * value1, const GValue * value2)
3009 GstValueIntersectInfo *intersect_info;
3013 if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
3016 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
3017 intersect_info = &g_array_index (gst_value_intersect_funcs,
3018 GstValueIntersectInfo, i);
3019 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
3020 intersect_info->type2 == G_VALUE_TYPE (value2))
3021 if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
3022 intersect_info->type1 == G_VALUE_TYPE (value2))
3026 return gst_value_can_compare (value1, value2);
3030 * gst_value_intersect:
3031 * @dest: a uninitialized #GValue that will hold the calculated
3032 * intersection value
3033 * @value1: a value to intersect
3034 * @value2: another value to intersect
3036 * Calculates the intersection of two values. If the values have
3037 * a non-empty intersection, the value representing the intersection
3038 * is placed in @dest. If the intersection is non-empty, @dest is
3041 * Returns: TRUE if the intersection is non-empty
3044 gst_value_intersect (GValue * dest, const GValue * value1,
3045 const GValue * value2)
3047 GstValueIntersectInfo *intersect_info;
3049 gboolean ret = FALSE;
3051 /* special cases first */
3052 if (GST_VALUE_HOLDS_LIST (value1))
3053 return gst_value_intersect_list (dest, value1, value2);
3054 if (GST_VALUE_HOLDS_LIST (value2))
3055 return gst_value_intersect_list (dest, value2, value1);
3057 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
3058 intersect_info = &g_array_index (gst_value_intersect_funcs,
3059 GstValueIntersectInfo, i);
3060 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
3061 intersect_info->type2 == G_VALUE_TYPE (value2)) {
3062 ret = intersect_info->func (dest, value1, value2);
3065 if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
3066 intersect_info->type2 == G_VALUE_TYPE (value1)) {
3067 ret = intersect_info->func (dest, value2, value1);
3072 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
3073 gst_value_init_and_copy (dest, value1);
3081 * gst_value_register_intersect_func:
3082 * @type1: the first type to intersect
3083 * @type2: the second type to intersect
3084 * @func: the intersection function
3086 * Registers a function that is called to calculate the intersection
3087 * of the values having the types @type1 and @type2.
3089 * Intersect functions should be registered at startup before any pipelines are
3090 * started, as gst_value_register_intersect_func() is not thread-safe and
3091 * cannot be used at the same time as gst_value_intersect() or
3092 * gst_value_can_intersect().
3095 gst_value_register_intersect_func (GType type1, GType type2,
3096 GstValueIntersectFunc func)
3098 GstValueIntersectInfo intersect_info;
3100 intersect_info.type1 = type1;
3101 intersect_info.type2 = type2;
3102 intersect_info.func = func;
3104 g_array_append_val (gst_value_intersect_funcs, intersect_info);
3111 * gst_value_subtract:
3112 * @dest: the destination value for the result if the subtraction is not empty
3113 * @minuend: the value to subtract from
3114 * @subtrahend: the value to subtract
3116 * Subtracts @subtrahend from @minuend and stores the result in @dest.
3117 * Note that this means subtraction as in sets, not as in mathematics.
3119 * Returns: %TRUE if the subtraction is not empty
3122 gst_value_subtract (GValue * dest, const GValue * minuend,
3123 const GValue * subtrahend)
3125 GstValueSubtractInfo *info;
3128 /* special cases first */
3129 if (GST_VALUE_HOLDS_LIST (minuend))
3130 return gst_value_subtract_from_list (dest, minuend, subtrahend);
3131 if (GST_VALUE_HOLDS_LIST (subtrahend))
3132 return gst_value_subtract_list (dest, minuend, subtrahend);
3134 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3135 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3136 if (info->minuend == G_VALUE_TYPE (minuend) &&
3137 info->subtrahend == G_VALUE_TYPE (subtrahend)) {
3138 return info->func (dest, minuend, subtrahend);
3142 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3143 gst_value_init_and_copy (dest, minuend);
3152 gst_value_subtract (GValue * dest, const GValue * minuend,
3153 const GValue * subtrahend)
3155 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3157 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
3158 gst_value_serialize (subtrahend),
3159 ret ? gst_value_serialize (dest) : "---");
3165 * gst_value_can_subtract:
3166 * @minuend: the value to subtract from
3167 * @subtrahend: the value to subtract
3169 * Checks if it's possible to subtract @subtrahend from @minuend.
3171 * Returns: TRUE if a subtraction is possible
3174 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3176 GstValueSubtractInfo *info;
3180 if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
3183 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3184 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3185 if (info->minuend == G_VALUE_TYPE (minuend) &&
3186 info->subtrahend == G_VALUE_TYPE (subtrahend))
3190 return gst_value_can_compare (minuend, subtrahend);
3194 * gst_value_register_subtract_func:
3195 * @minuend_type: type of the minuend
3196 * @subtrahend_type: type of the subtrahend
3197 * @func: function to use
3199 * Registers @func as a function capable of subtracting the values of
3200 * @subtrahend_type from values of @minuend_type.
3202 * Subtract functions should be registered at startup before any pipelines are
3203 * started, as gst_value_register_subtract_func() is not thread-safe and
3204 * cannot be used at the same time as gst_value_subtract().
3207 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3208 GstValueSubtractFunc func)
3210 GstValueSubtractInfo info;
3212 /* one type must be unfixed, other subtractions can be done as comparisons */
3213 g_return_if_fail (!gst_type_is_fixed (minuend_type)
3214 || !gst_type_is_fixed (subtrahend_type));
3216 info.minuend = minuend_type;
3217 info.subtrahend = subtrahend_type;
3220 g_array_append_val (gst_value_subtract_funcs, info);
3224 * gst_value_register:
3225 * @table: structure containing functions to register
3227 * Registers functions to perform calculations on #GValues of a given
3231 gst_value_register (const GstValueTable * table)
3233 g_array_append_val (gst_value_table, *table);
3237 * gst_value_init_and_copy:
3238 * @dest: the target value
3239 * @src: the source value
3241 * Initialises the target value to be of the same type as source and then copies
3242 * the contents from source to target.
3245 gst_value_init_and_copy (GValue * dest, const GValue * src)
3247 g_value_init (dest, G_VALUE_TYPE (src));
3248 g_value_copy (src, dest);
3252 * gst_value_serialize:
3253 * @value: a #GValue to serialize
3255 * tries to transform the given @value into a string representation that allows
3256 * getting back this string later on using gst_value_deserialize().
3258 * Returns: the serialization for @value or NULL if none exists
3261 gst_value_serialize (const GValue * value)
3264 GValue s_val = { 0 };
3265 GstValueTable *table, *best = NULL;
3268 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3270 for (i = 0; i < gst_value_table->len; i++) {
3271 table = &g_array_index (gst_value_table, GstValueTable, i);
3272 if (table->serialize == NULL)
3274 if (table->type == G_VALUE_TYPE (value)) {
3278 if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
3279 if (!best || g_type_is_a (table->type, best->type))
3284 return best->serialize (value);
3286 g_value_init (&s_val, G_TYPE_STRING);
3287 if (g_value_transform (value, &s_val)) {
3288 s = gst_string_wrap (g_value_get_string (&s_val));
3292 g_value_unset (&s_val);
3298 * gst_value_deserialize:
3299 * @dest: #GValue to fill with contents of deserialization
3300 * @src: string to deserialize
3302 * Tries to deserialize a string into the type specified by the given GValue.
3303 * If the operation succeeds, TRUE is returned, FALSE otherwise.
3305 * Returns: TRUE on success
3308 gst_value_deserialize (GValue * dest, const gchar * src)
3310 GstValueTable *table, *best = NULL;
3313 g_return_val_if_fail (src != NULL, FALSE);
3314 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
3316 for (i = 0; i < gst_value_table->len; i++) {
3317 table = &g_array_index (gst_value_table, GstValueTable, i);
3318 if (table->serialize == NULL)
3321 if (table->type == G_VALUE_TYPE (dest)) {
3326 if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
3327 if (!best || g_type_is_a (table->type, best->type))
3332 return best->deserialize (dest, src);
3339 * gst_value_is_fixed:
3340 * @value: the #GValue to check
3342 * Tests if the given GValue, if available in a GstStructure (or any other
3343 * container) contains a "fixed" (which means: one value) or an "unfixed"
3344 * (which means: multiple possible values, such as data lists or data
3347 * Returns: true if the value is "fixed".
3351 gst_value_is_fixed (const GValue * value)
3353 GType type = G_VALUE_TYPE (value);
3355 /* the most common types are just basic plain glib types */
3356 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
3360 if (type == GST_TYPE_ARRAY) {
3364 /* check recursively */
3365 size = gst_value_array_get_size (value);
3366 for (n = 0; n < size; n++) {
3367 kid = gst_value_array_get_value (value, n);
3368 if (!gst_value_is_fixed (kid))
3373 return gst_type_is_fixed (type);
3380 /* helper functions */
3382 /* Finds the greatest common divisor.
3383 * Returns 1 if none other found.
3384 * This is Euclid's algorithm. */
3386 gst_greatest_common_divisor (gint a, gint b)
3399 gst_value_init_fraction (GValue * value)
3401 value->data[0].v_int = 0;
3402 value->data[1].v_int = 1;
3406 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
3408 dest_value->data[0].v_int = src_value->data[0].v_int;
3409 dest_value->data[1].v_int = src_value->data[1].v_int;
3413 gst_value_collect_fraction (GValue * value, guint n_collect_values,
3414 GTypeCValue * collect_values, guint collect_flags)
3416 gst_value_set_fraction (value,
3417 collect_values[0].v_int, collect_values[1].v_int);
3423 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
3424 GTypeCValue * collect_values, guint collect_flags)
3426 gint *numerator = collect_values[0].v_pointer;
3427 gint *denominator = collect_values[1].v_pointer;
3430 return g_strdup_printf ("numerator for `%s' passed as NULL",
3431 G_VALUE_TYPE_NAME (value));
3433 return g_strdup_printf ("denominator for `%s' passed as NULL",
3434 G_VALUE_TYPE_NAME (value));
3436 *numerator = value->data[0].v_int;
3437 *denominator = value->data[1].v_int;
3443 * gst_value_set_fraction:
3444 * @value: a GValue initialized to #GST_TYPE_FRACTION
3445 * @numerator: the numerator of the fraction
3446 * @denominator: the denominator of the fraction
3448 * Sets @value to the fraction specified by @numerator over @denominator.
3449 * The fraction gets reduced to the smallest numerator and denominator,
3450 * and if necessary the sign is moved to the numerator.
3453 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
3457 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
3458 g_return_if_fail (denominator != 0);
3459 g_return_if_fail (denominator >= -G_MAXINT);
3460 g_return_if_fail (numerator >= -G_MAXINT);
3462 /* normalize sign */
3463 if (denominator < 0) {
3464 numerator = -numerator;
3465 denominator = -denominator;
3468 /* check for reduction */
3469 gcd = gst_greatest_common_divisor (numerator, denominator);
3475 g_assert (denominator > 0);
3477 value->data[0].v_int = numerator;
3478 value->data[1].v_int = denominator;
3482 * gst_value_get_fraction_numerator:
3483 * @value: a GValue initialized to #GST_TYPE_FRACTION
3485 * Gets the numerator of the fraction specified by @value.
3487 * Returns: the numerator of the fraction.
3490 gst_value_get_fraction_numerator (const GValue * value)
3492 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
3494 return value->data[0].v_int;
3498 * gst_value_get_fraction_denominator:
3499 * @value: a GValue initialized to #GST_TYPE_FRACTION
3501 * Gets the denominator of the fraction specified by @value.
3503 * Returns: the denominator of the fraction.
3506 gst_value_get_fraction_denominator (const GValue * value)
3508 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
3510 return value->data[1].v_int;
3514 * gst_value_fraction_multiply:
3515 * @product: a GValue initialized to #GST_TYPE_FRACTION
3516 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
3517 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
3519 * Multiplies the two GValues containing a GstFraction and sets @product
3520 * to the product of the two fractions.
3522 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3525 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
3526 const GValue * factor2)
3528 gint gcd, n1, n2, d1, d2;
3530 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
3531 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
3533 n1 = factor1->data[0].v_int;
3534 n2 = factor2->data[0].v_int;
3535 d1 = factor1->data[1].v_int;
3536 d2 = factor2->data[1].v_int;
3538 gcd = gst_greatest_common_divisor (n1, d2);
3541 gcd = gst_greatest_common_divisor (n2, d1);
3545 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
3546 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3548 gst_value_set_fraction (product, n1 * n2, d1 * d2);
3554 * gst_value_fraction_subtract:
3555 * @dest: a GValue initialized to #GST_TYPE_FRACTION
3556 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
3557 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
3559 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
3561 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3564 gst_value_fraction_subtract (GValue * dest,
3565 const GValue * minuend, const GValue * subtrahend)
3567 gint n1, n2, d1, d2;
3569 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
3570 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
3572 n1 = minuend->data[0].v_int;
3573 n2 = subtrahend->data[0].v_int;
3574 d1 = minuend->data[1].v_int;
3575 d2 = subtrahend->data[1].v_int;
3578 gst_value_set_fraction (dest, -n2, d2);
3582 gst_value_set_fraction (dest, n1, d1);
3586 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (d2), FALSE);
3587 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (n2), FALSE);
3588 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3590 gst_value_set_fraction (dest, (n1 * d2) - (n2 * d1), d1 * d2);
3596 gst_value_serialize_fraction (const GValue * value)
3598 gint32 numerator = value->data[0].v_int;
3599 gint32 denominator = value->data[1].v_int;
3600 gboolean positive = TRUE;
3602 /* get the sign and make components absolute */
3603 if (numerator < 0) {
3604 numerator = -numerator;
3605 positive = !positive;
3607 if (denominator < 0) {
3608 denominator = -denominator;
3609 positive = !positive;
3612 return g_strdup_printf ("%s%d/%d",
3613 positive ? "" : "-", numerator, denominator);
3617 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
3621 if (G_UNLIKELY (s == NULL))
3624 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
3627 if (sscanf (s, "%d/%d", &num, &den) == 2) {
3628 gst_value_set_fraction (dest, num, den);
3631 if (sscanf (s, "%d", &num) == 1) {
3632 gst_value_set_fraction (dest, num, 1);
3635 if (g_ascii_strcasecmp (s, "min") == 0) {
3636 gst_value_set_fraction (dest, -G_MAXINT, 1);
3638 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3639 gst_value_set_fraction (dest, G_MAXINT, 1);
3647 gst_value_transform_fraction_string (const GValue * src_value,
3648 GValue * dest_value)
3650 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
3654 gst_value_transform_string_fraction (const GValue * src_value,
3655 GValue * dest_value)
3657 if (!gst_value_deserialize_fraction (dest_value,
3658 src_value->data[0].v_pointer))
3659 /* If the deserialize fails, ensure we leave the fraction in a
3660 * valid, if incorrect, state */
3661 gst_value_set_fraction (dest_value, 0, 1);
3664 #define MAX_TERMS 30
3665 #define MIN_DIVISOR 1.0e-10
3666 #define MAX_ERROR 1.0e-20
3668 /* use continued fractions to transform a double into a fraction,
3669 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3670 * This algorithm takes care of overflows.
3673 gst_value_transform_double_fraction (const GValue * src_value,
3674 GValue * dest_value)
3676 gdouble V, F; /* double being converted */
3677 gint N, D; /* will contain the result */
3678 gint A; /* current term in continued fraction */
3679 gint64 N1, D1; /* numerator, denominator of last approx */
3680 gint64 N2, D2; /* numerator, denominator of previous approx */
3682 gboolean negative = FALSE;
3684 /* initialize fraction being converted */
3685 F = src_value->data[0].v_double;
3692 /* initialize fractions with 1/0, 0/1 */
3700 for (i = 0; i < MAX_TERMS; i++) {
3702 A = (gint) F; /* no floor() needed, F is always >= 0 */
3703 /* get new divisor */
3706 /* calculate new fraction in temp */
3710 /* guard against overflow */
3711 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3718 /* save last two fractions */
3724 /* quit if dividing by zero or close enough to target */
3725 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3729 /* Take reciprocal */
3732 /* fix for overflow */
3737 /* fix for negative */
3741 /* will also simplify */
3742 gst_value_set_fraction (dest_value, N, D);
3746 gst_value_transform_fraction_double (const GValue * src_value,
3747 GValue * dest_value)
3749 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
3750 ((double) src_value->data[1].v_int);
3754 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
3762 n1 = value1->data[0].v_int;
3763 n2 = value2->data[0].v_int;
3764 d1 = value1->data[1].v_int;
3765 d2 = value2->data[1].v_int;
3767 /* fractions are reduced when set, so we can quickly see if they're equal */
3768 if (n1 == n2 && d1 == d2)
3769 return GST_VALUE_EQUAL;
3771 /* extend to 64 bits */
3772 new_num_1 = ((gint64) n1) * d2;
3773 new_num_2 = ((gint64) n2) * d1;
3774 if (new_num_1 < new_num_2)
3775 return GST_VALUE_LESS_THAN;
3776 if (new_num_1 > new_num_2)
3777 return GST_VALUE_GREATER_THAN;
3779 /* new_num_1 == new_num_2 implies that both denominators must have
3780 * been 0, beause otherwise simplification would have caught the
3782 return GST_VALUE_UNORDERED;
3790 * gst_value_set_date:
3791 * @value: a GValue initialized to GST_TYPE_DATE
3792 * @date: the date to set the value to
3794 * Sets the contents of @value to coorespond to @date. The actual
3795 * #GDate structure is copied before it is used.
3798 gst_value_set_date (GValue * value, const GDate * date)
3800 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
3801 g_return_if_fail (g_date_valid (date));
3803 g_value_set_boxed (value, date);
3807 * gst_value_get_date:
3808 * @value: a GValue initialized to GST_TYPE_DATE
3810 * Gets the contents of @value.
3812 * Returns: the contents of @value
3815 gst_value_get_date (const GValue * value)
3817 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3819 return (const GDate *) g_value_get_boxed (value);
3823 gst_date_copy (gpointer boxed)
3825 const GDate *date = (const GDate *) boxed;
3827 if (!g_date_valid (date)) {
3828 GST_WARNING ("invalid GDate");
3832 return g_date_new_julian (g_date_get_julian (date));
3836 gst_value_compare_date (const GValue * value1, const GValue * value2)
3838 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3839 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3843 return GST_VALUE_EQUAL;
3845 if ((date1 == NULL || !g_date_valid (date1))
3846 && (date2 != NULL && g_date_valid (date2))) {
3847 return GST_VALUE_LESS_THAN;
3850 if ((date2 == NULL || !g_date_valid (date2))
3851 && (date1 != NULL && g_date_valid (date1))) {
3852 return GST_VALUE_GREATER_THAN;
3855 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3856 || !g_date_valid (date2)) {
3857 return GST_VALUE_UNORDERED;
3860 j1 = g_date_get_julian (date1);
3861 j2 = g_date_get_julian (date2);
3864 return GST_VALUE_EQUAL;
3866 return GST_VALUE_LESS_THAN;
3868 return GST_VALUE_GREATER_THAN;
3872 gst_value_serialize_date (const GValue * val)
3874 const GDate *date = (const GDate *) g_value_get_boxed (val);
3876 if (date == NULL || !g_date_valid (date))
3877 return g_strdup ("9999-99-99");
3879 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3880 g_date_get_month (date), g_date_get_day (date));
3884 gst_value_deserialize_date (GValue * dest, const char *s)
3886 guint year, month, day;
3888 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3891 if (!g_date_valid_dmy (day, month, year))
3894 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3899 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3901 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3905 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3907 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3911 gst_value_transform_object_string (const GValue * src_value,
3912 GValue * dest_value)
3917 obj = g_value_get_object (src_value);
3920 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
3921 GST_OBJECT_NAME (obj));
3923 str = g_strdup ("NULL");
3926 dest_value->data[0].v_pointer = str;
3929 static GTypeInfo _info = {
3942 static GTypeFundamentalInfo _finfo = {
3946 #define FUNC_VALUE_GET_TYPE(type, name) \
3947 GType gst_ ## type ## _get_type (void) \
3949 static GType gst_ ## type ## _type = 0; \
3951 if (G_UNLIKELY (gst_ ## type ## _type == 0)) { \
3952 _info.value_table = & _gst_ ## type ## _value_table; \
3953 gst_ ## type ## _type = g_type_register_fundamental ( \
3954 g_type_fundamental_next (), \
3955 name, &_info, &_finfo, 0); \
3958 return gst_ ## type ## _type; \
3961 static const GTypeValueTable _gst_fourcc_value_table = {
3962 gst_value_init_fourcc,
3964 gst_value_copy_fourcc,
3967 gst_value_collect_fourcc,
3969 gst_value_lcopy_fourcc
3972 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
3974 static const GTypeValueTable _gst_int_range_value_table = {
3975 gst_value_init_int_range,
3977 gst_value_copy_int_range,
3980 gst_value_collect_int_range,
3982 gst_value_lcopy_int_range
3985 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
3987 static const GTypeValueTable _gst_double_range_value_table = {
3988 gst_value_init_double_range,
3990 gst_value_copy_double_range,
3993 gst_value_collect_double_range,
3995 gst_value_lcopy_double_range
3998 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
4000 static const GTypeValueTable _gst_fraction_range_value_table = {
4001 gst_value_init_fraction_range,
4002 gst_value_free_fraction_range,
4003 gst_value_copy_fraction_range,
4006 gst_value_collect_fraction_range,
4008 gst_value_lcopy_fraction_range
4011 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
4013 static const GTypeValueTable _gst_value_list_value_table = {
4014 gst_value_init_list_or_array,
4015 gst_value_free_list_or_array,
4016 gst_value_copy_list_or_array,
4017 gst_value_list_or_array_peek_pointer,
4019 gst_value_collect_list_or_array,
4021 gst_value_lcopy_list_or_array
4024 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
4026 static const GTypeValueTable _gst_value_array_value_table = {
4027 gst_value_init_list_or_array,
4028 gst_value_free_list_or_array,
4029 gst_value_copy_list_or_array,
4030 gst_value_list_or_array_peek_pointer,
4032 gst_value_collect_list_or_array,
4034 gst_value_lcopy_list_or_array
4037 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
4039 static const GTypeValueTable _gst_fraction_value_table = {
4040 gst_value_init_fraction,
4042 gst_value_copy_fraction,
4045 gst_value_collect_fraction,
4047 gst_value_lcopy_fraction
4050 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
4054 gst_date_get_type (void)
4056 static GType gst_date_type = 0;
4058 if (G_UNLIKELY (gst_date_type == 0)) {
4059 /* FIXME 0.11: we require GLib 2.8 already
4060 * Not using G_TYPE_DATE here on purpose, even if we could
4061 * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
4062 * serialised strings to have different type strings depending
4063 * on what version is used, so FIXME when we require GLib-2.8 */
4064 gst_date_type = g_boxed_type_register_static ("GstDate",
4065 (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
4068 return gst_date_type;
4072 _gst_value_initialize (void)
4074 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
4075 gst_value_union_funcs = g_array_new (FALSE, FALSE,
4076 sizeof (GstValueUnionInfo));
4077 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
4078 sizeof (GstValueIntersectInfo));
4079 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4080 sizeof (GstValueSubtractInfo));
4083 static GstValueTable gst_value = {
4085 gst_value_compare_fourcc,
4086 gst_value_serialize_fourcc,
4087 gst_value_deserialize_fourcc,
4090 gst_value.type = gst_fourcc_get_type ();
4091 gst_value_register (&gst_value);
4095 static GstValueTable gst_value = {
4097 gst_value_compare_int_range,
4098 gst_value_serialize_int_range,
4099 gst_value_deserialize_int_range,
4102 gst_value.type = gst_int_range_get_type ();
4103 gst_value_register (&gst_value);
4107 static GstValueTable gst_value = {
4109 gst_value_compare_double_range,
4110 gst_value_serialize_double_range,
4111 gst_value_deserialize_double_range,
4114 gst_value.type = gst_double_range_get_type ();
4115 gst_value_register (&gst_value);
4119 static GstValueTable gst_value = {
4121 gst_value_compare_fraction_range,
4122 gst_value_serialize_fraction_range,
4123 gst_value_deserialize_fraction_range,
4126 gst_value.type = gst_fraction_range_get_type ();
4127 gst_value_register (&gst_value);
4131 static GstValueTable gst_value = {
4133 gst_value_compare_list,
4134 gst_value_serialize_list,
4135 gst_value_deserialize_list,
4138 gst_value.type = gst_value_list_get_type ();
4139 gst_value_register (&gst_value);
4143 static GstValueTable gst_value = {
4145 gst_value_compare_array,
4146 gst_value_serialize_array,
4147 gst_value_deserialize_array,
4150 gst_value.type = gst_value_array_get_type ();;
4151 gst_value_register (&gst_value);
4156 static const GTypeValueTable value_table = {
4157 gst_value_init_buffer,
4159 gst_value_copy_buffer,
4162 NULL, /*gst_value_collect_buffer, */
4164 NULL /*gst_value_lcopy_buffer */
4167 static GstValueTable gst_value = {
4169 gst_value_compare_buffer,
4170 gst_value_serialize_buffer,
4171 gst_value_deserialize_buffer,
4174 gst_value.type = GST_TYPE_BUFFER;
4175 gst_value_register (&gst_value);
4178 static GstValueTable gst_value = {
4180 gst_value_compare_fraction,
4181 gst_value_serialize_fraction,
4182 gst_value_deserialize_fraction,
4185 gst_value.type = gst_fraction_get_type ();
4186 gst_value_register (&gst_value);
4189 static GstValueTable gst_value = {
4192 gst_value_serialize_caps,
4193 gst_value_deserialize_caps,
4196 gst_value.type = GST_TYPE_CAPS;
4197 gst_value_register (&gst_value);
4200 static GstValueTable gst_value = {
4203 gst_value_serialize_structure,
4204 gst_value_deserialize_structure,
4207 gst_value.type = GST_TYPE_STRUCTURE;
4208 gst_value_register (&gst_value);
4211 static GstValueTable gst_value = {
4213 gst_value_compare_date,
4214 gst_value_serialize_date,
4215 gst_value_deserialize_date,
4218 gst_value.type = gst_date_get_type ();
4219 gst_value_register (&gst_value);
4222 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
4223 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
4225 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
4226 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
4227 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
4229 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
4231 REGISTER_SERIALIZATION (G_TYPE_INT, int);
4233 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
4234 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
4236 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
4237 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
4238 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
4240 g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
4241 gst_value_transform_fourcc_string);
4242 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
4243 gst_value_transform_int_range_string);
4244 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
4245 gst_value_transform_double_range_string);
4246 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
4247 gst_value_transform_fraction_range_string);
4248 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
4249 gst_value_transform_list_string);
4250 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
4251 gst_value_transform_array_string);
4252 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
4253 gst_value_transform_fraction_string);
4254 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
4255 gst_value_transform_string_fraction);
4256 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
4257 gst_value_transform_fraction_double);
4258 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
4259 gst_value_transform_double_fraction);
4260 g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
4261 gst_value_transform_date_string);
4262 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
4263 gst_value_transform_string_date);
4264 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
4265 gst_value_transform_object_string);
4267 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4268 gst_value_intersect_int_int_range);
4269 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4270 gst_value_intersect_int_range_int_range);
4271 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4272 gst_value_intersect_double_double_range);
4273 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
4274 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
4275 gst_value_register_intersect_func (GST_TYPE_ARRAY,
4276 GST_TYPE_ARRAY, gst_value_intersect_array);
4277 gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4278 gst_value_intersect_fraction_fraction_range);
4279 gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
4280 GST_TYPE_FRACTION_RANGE,
4281 gst_value_intersect_fraction_range_fraction_range);
4283 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4284 gst_value_subtract_int_int_range);
4285 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
4286 gst_value_subtract_int_range_int);
4287 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4288 gst_value_subtract_int_range_int_range);
4289 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4290 gst_value_subtract_double_double_range);
4291 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
4292 gst_value_subtract_double_range_double);
4293 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
4294 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
4296 gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4297 gst_value_subtract_fraction_fraction_range);
4298 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
4299 gst_value_subtract_fraction_range_fraction);
4300 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
4301 GST_TYPE_FRACTION_RANGE,
4302 gst_value_subtract_fraction_range_fraction_range);
4304 /* see bug #317246, #64994, #65041 */
4306 volatile GType date_type = G_TYPE_DATE;
4308 g_type_name (date_type);
4311 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4312 gst_value_union_int_int_range);
4313 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4314 gst_value_union_int_range_int_range);
4317 /* Implement these if needed */
4318 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4319 gst_value_union_fraction_fraction_range);
4320 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
4321 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);