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_take_and_wrap (gchar * s);
85 static gchar *gst_string_unwrap (const gchar * s);
91 /* two helper functions to serialize/stringify any type of list
92 * regular lists are done with { }, arrays with < >
95 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
99 GArray *array = value->data[0].v_pointer;
104 /* estimate minimum string length to minimise re-allocs in GString */
105 s = g_string_sized_new (2 + (6 * array->len) + 2);
106 g_string_append (s, begin);
107 for (i = 0; i < array->len; i++) {
108 v = &g_array_index (array, GValue, i);
109 s_val = gst_value_serialize (v);
110 g_string_append (s, s_val);
112 if (i < array->len - 1) {
113 g_string_append_len (s, ", ", 2);
116 g_string_append (s, end);
117 return g_string_free (s, FALSE);
121 gst_value_transform_any_list_string (const GValue * src_value,
122 GValue * dest_value, const gchar * begin, const gchar * end)
130 array = src_value->data[0].v_pointer;
132 /* estimate minimum string length to minimise re-allocs in GString */
133 s = g_string_sized_new (2 + (10 * array->len) + 2);
134 g_string_append (s, begin);
135 for (i = 0; i < array->len; i++) {
136 list_value = &g_array_index (array, GValue, i);
139 g_string_append_len (s, ", ", 2);
141 list_s = g_strdup_value_contents (list_value);
142 g_string_append (s, list_s);
145 g_string_append (s, end);
147 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
151 * helper function to see if a type is fixed. Is used internally here and
152 * there. Do not export, since it doesn't work for types where the content
153 * decides the fixedness (e.g. GST_TYPE_ARRAY).
156 gst_type_is_fixed (GType type)
158 /* the basic int, string, double types */
159 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
162 /* our fundamental types that are certainly not fixed */
163 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
164 type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
167 /* other (boxed) types that are fixed */
168 if (type == GST_TYPE_BUFFER) {
172 if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
173 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
180 /* GValue functions usable for both regular lists and arrays */
182 gst_value_init_list_or_array (GValue * value)
184 value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
188 copy_garray_of_gstvalue (const GArray * src)
193 dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
194 g_array_set_size (dest, src->len);
195 for (i = 0; i < src->len; i++) {
196 gst_value_init_and_copy (&g_array_index (dest, GValue, i),
197 &g_array_index (src, GValue, i));
204 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
206 dest_value->data[0].v_pointer =
207 copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
211 gst_value_free_list_or_array (GValue * value)
214 GArray *src = (GArray *) value->data[0].v_pointer;
216 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
217 for (i = 0; i < src->len; i++) {
218 g_value_unset (&g_array_index (src, GValue, i));
220 g_array_free (src, TRUE);
225 gst_value_list_or_array_peek_pointer (const GValue * value)
227 return value->data[0].v_pointer;
231 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
232 GTypeCValue * collect_values, guint collect_flags)
234 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
235 value->data[0].v_pointer = collect_values[0].v_pointer;
236 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
238 value->data[0].v_pointer =
239 copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
245 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
246 GTypeCValue * collect_values, guint collect_flags)
248 GArray **dest = collect_values[0].v_pointer;
251 return g_strdup_printf ("value location for `%s' passed as NULL",
252 G_VALUE_TYPE_NAME (value));
253 if (!value->data[0].v_pointer)
254 return g_strdup_printf ("invalid value given for `%s'",
255 G_VALUE_TYPE_NAME (value));
256 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
257 *dest = (GArray *) value->data[0].v_pointer;
259 *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
265 * gst_value_list_append_value:
266 * @value: a #GValue of type #GST_TYPE_LIST
267 * @append_value: the value to append
269 * Appends @append_value to the GstValueList in @value.
272 gst_value_list_append_value (GValue * value, const GValue * append_value)
276 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
278 gst_value_init_and_copy (&val, append_value);
279 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
283 * gst_value_list_prepend_value:
284 * @value: a #GValue of type #GST_TYPE_LIST
285 * @prepend_value: the value to prepend
287 * Prepends @prepend_value to the GstValueList in @value.
290 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
294 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
296 gst_value_init_and_copy (&val, prepend_value);
297 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
301 * gst_value_list_concat:
302 * @dest: an uninitialized #GValue to take the result
306 * Concatenates copies of @value1 and @value2 into a list. Values that are not
307 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
308 * @dest will be initialized to the type #GST_TYPE_LIST.
311 gst_value_list_concat (GValue * dest, const GValue * value1,
312 const GValue * value2)
314 guint i, value1_length, value2_length;
317 g_return_if_fail (dest != NULL);
318 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
319 g_return_if_fail (G_IS_VALUE (value1));
320 g_return_if_fail (G_IS_VALUE (value2));
323 (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
325 (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
326 g_value_init (dest, GST_TYPE_LIST);
327 array = (GArray *) dest->data[0].v_pointer;
328 g_array_set_size (array, value1_length + value2_length);
330 if (GST_VALUE_HOLDS_LIST (value1)) {
331 for (i = 0; i < value1_length; i++) {
332 gst_value_init_and_copy (&g_array_index (array, GValue, i),
333 gst_value_list_get_value (value1, i));
336 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
339 if (GST_VALUE_HOLDS_LIST (value2)) {
340 for (i = 0; i < value2_length; i++) {
341 gst_value_init_and_copy (&g_array_index (array, GValue,
342 i + value1_length), gst_value_list_get_value (value2, i));
345 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
351 * gst_value_list_get_size:
352 * @value: a #GValue of type #GST_TYPE_LIST
354 * Gets the number of values contained in @value.
356 * Returns: the number of values
359 gst_value_list_get_size (const GValue * value)
361 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
363 return ((GArray *) value->data[0].v_pointer)->len;
367 * gst_value_list_get_value:
368 * @value: a #GValue of type #GST_TYPE_LIST
369 * @index: index of value to get from the list
371 * Gets the value that is a member of the list contained in @value and
372 * has the index @index.
374 * Returns: the value at the given index
377 gst_value_list_get_value (const GValue * value, guint index)
379 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
380 g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
382 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
387 * gst_value_array_append_value:
388 * @value: a #GValue of type #GST_TYPE_ARRAY
389 * @append_value: the value to append
391 * Appends @append_value to the GstValueArray in @value.
394 gst_value_array_append_value (GValue * value, const GValue * append_value)
398 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
400 gst_value_init_and_copy (&val, append_value);
401 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
405 * gst_value_array_prepend_value:
406 * @value: a #GValue of type #GST_TYPE_ARRAY
407 * @prepend_value: the value to prepend
409 * Prepends @prepend_value to the GstValueArray in @value.
412 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
416 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
418 gst_value_init_and_copy (&val, prepend_value);
419 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
423 * gst_value_array_get_size:
424 * @value: a #GValue of type #GST_TYPE_ARRAY
426 * Gets the number of values contained in @value.
428 * Returns: the number of values
431 gst_value_array_get_size (const GValue * value)
433 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
435 return ((GArray *) value->data[0].v_pointer)->len;
439 * gst_value_array_get_value:
440 * @value: a #GValue of type #GST_TYPE_ARRAY
441 * @index: index of value to get from the array
443 * Gets the value that is a member of the array contained in @value and
444 * has the index @index.
446 * Returns: the value at the given index
449 gst_value_array_get_value (const GValue * value, guint index)
451 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
452 g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
454 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
459 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
461 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
465 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
467 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
470 /* Do an unordered compare of the contents of a list */
472 gst_value_compare_list (const GValue * value1, const GValue * value2)
475 GArray *array1 = value1->data[0].v_pointer;
476 GArray *array2 = value2->data[0].v_pointer;
481 GstValueCompareFunc compare;
483 /* get length and do initial length check. */
485 if (len != array2->len)
486 return GST_VALUE_UNORDERED;
488 /* place to mark removed value indices of array2 */
489 removed = g_newa (guint8, len);
490 memset (removed, 0, len);
493 /* loop over array1, all items should be in array2. When we find an
494 * item in array2, remove it from array2 by marking it as removed */
495 for (i = 0; i < len; i++) {
496 v1 = &g_array_index (array1, GValue, i);
497 if ((compare = gst_value_get_compare_func (v1))) {
498 for (j = 0; j < len; j++) {
499 /* item is removed, we can skip it */
502 v2 = &g_array_index (array2, GValue, j);
503 if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
504 /* mark item as removed now that we found it in array2 and
505 * decrement the number of remaining items in array2. */
511 /* item in array1 and not in array2, UNORDERED */
513 return GST_VALUE_UNORDERED;
515 return GST_VALUE_UNORDERED;
517 /* if not all items were removed, array2 contained something not in array1 */
519 return GST_VALUE_UNORDERED;
521 /* arrays are equal */
522 return GST_VALUE_EQUAL;
525 /* Perform an ordered comparison of the contents of an array */
527 gst_value_compare_array (const GValue * value1, const GValue * value2)
530 GArray *array1 = value1->data[0].v_pointer;
531 GArray *array2 = value2->data[0].v_pointer;
535 if (array1->len != array2->len)
536 return GST_VALUE_UNORDERED;
538 for (i = 0; i < array1->len; i++) {
539 v1 = &g_array_index (array1, GValue, i);
540 v2 = &g_array_index (array2, GValue, i);
541 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
542 return GST_VALUE_UNORDERED;
545 return GST_VALUE_EQUAL;
549 gst_value_serialize_list (const GValue * value)
551 return gst_value_serialize_any_list (value, "{ ", " }");
555 gst_value_deserialize_list (GValue * dest, const gchar * s)
557 g_warning ("gst_value_deserialize_list: unimplemented");
562 gst_value_serialize_array (const GValue * value)
564 return gst_value_serialize_any_list (value, "< ", " >");
568 gst_value_deserialize_array (GValue * dest, const gchar * s)
570 g_warning ("gst_value_deserialize_array: unimplemented");
579 gst_value_init_fourcc (GValue * value)
581 value->data[0].v_int = 0;
585 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
587 dest_value->data[0].v_int = src_value->data[0].v_int;
591 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
592 GTypeCValue * collect_values, guint collect_flags)
594 value->data[0].v_int = collect_values[0].v_int;
600 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
601 GTypeCValue * collect_values, guint collect_flags)
603 guint32 *fourcc_p = collect_values[0].v_pointer;
606 return g_strdup_printf ("value location for `%s' passed as NULL",
607 G_VALUE_TYPE_NAME (value));
609 *fourcc_p = value->data[0].v_int;
615 * gst_value_set_fourcc:
616 * @value: a GValue initialized to #GST_TYPE_FOURCC
617 * @fourcc: the #guint32 fourcc to set
619 * Sets @value to @fourcc.
622 gst_value_set_fourcc (GValue * value, guint32 fourcc)
624 g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
626 value->data[0].v_int = fourcc;
630 * gst_value_get_fourcc:
631 * @value: a GValue initialized to #GST_TYPE_FOURCC
633 * Gets the #guint32 fourcc contained in @value.
635 * Returns: the #guint32 fourcc contained in @value.
638 gst_value_get_fourcc (const GValue * value)
640 g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
642 return value->data[0].v_int;
646 gst_value_transform_fourcc_string (const GValue * src_value,
649 guint32 fourcc = src_value->data[0].v_int;
651 if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
652 g_ascii_isprint ((fourcc >> 8) & 0xff) &&
653 g_ascii_isprint ((fourcc >> 16) & 0xff) &&
654 g_ascii_isprint ((fourcc >> 24) & 0xff)) {
655 dest_value->data[0].v_pointer =
656 g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
658 dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
663 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
665 if (value2->data[0].v_int == value1->data[0].v_int)
666 return GST_VALUE_EQUAL;
667 return GST_VALUE_UNORDERED;
671 gst_value_serialize_fourcc (const GValue * value)
673 guint32 fourcc = value->data[0].v_int;
675 if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
676 g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
677 g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
678 g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
679 return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
681 return g_strdup_printf ("0x%08x", fourcc);
686 gst_value_deserialize_fourcc (GValue * dest, const char *s)
688 gboolean ret = FALSE;
692 if (strlen (s) == 4) {
693 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
695 } else if (g_ascii_isdigit (*s)) {
696 fourcc = strtoul (s, &end, 0);
701 gst_value_set_fourcc (dest, fourcc);
711 gst_value_init_int_range (GValue * value)
713 value->data[0].v_int = 0;
714 value->data[1].v_int = 0;
718 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
720 dest_value->data[0].v_int = src_value->data[0].v_int;
721 dest_value->data[1].v_int = src_value->data[1].v_int;
725 gst_value_collect_int_range (GValue * value, guint n_collect_values,
726 GTypeCValue * collect_values, guint collect_flags)
728 value->data[0].v_int = collect_values[0].v_int;
729 value->data[1].v_int = collect_values[1].v_int;
735 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
736 GTypeCValue * collect_values, guint collect_flags)
738 guint32 *int_range_start = collect_values[0].v_pointer;
739 guint32 *int_range_end = collect_values[1].v_pointer;
741 if (!int_range_start)
742 return g_strdup_printf ("start value location for `%s' passed as NULL",
743 G_VALUE_TYPE_NAME (value));
745 return g_strdup_printf ("end value location for `%s' passed as NULL",
746 G_VALUE_TYPE_NAME (value));
748 *int_range_start = value->data[0].v_int;
749 *int_range_end = value->data[1].v_int;
755 * gst_value_set_int_range:
756 * @value: a GValue initialized to GST_TYPE_INT_RANGE
757 * @start: the start of the range
758 * @end: the end of the range
760 * Sets @value to the range specified by @start and @end.
763 gst_value_set_int_range (GValue * value, gint start, gint end)
765 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
766 g_return_if_fail (start < end);
768 value->data[0].v_int = start;
769 value->data[1].v_int = end;
773 * gst_value_get_int_range_min:
774 * @value: a GValue initialized to GST_TYPE_INT_RANGE
776 * Gets the minimum of the range specified by @value.
778 * Returns: the minimum of the range
781 gst_value_get_int_range_min (const GValue * value)
783 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
785 return value->data[0].v_int;
789 * gst_value_get_int_range_max:
790 * @value: a GValue initialized to GST_TYPE_INT_RANGE
792 * Gets the maximum of the range specified by @value.
794 * Returns: the maxumum of the range
797 gst_value_get_int_range_max (const GValue * value)
799 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
801 return value->data[1].v_int;
805 gst_value_transform_int_range_string (const GValue * src_value,
808 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
809 (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
813 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
815 if (value2->data[0].v_int == value1->data[0].v_int &&
816 value2->data[1].v_int == value1->data[1].v_int)
817 return GST_VALUE_EQUAL;
818 return GST_VALUE_UNORDERED;
822 gst_value_serialize_int_range (const GValue * value)
824 return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
825 value->data[1].v_int);
829 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
831 g_warning ("unimplemented");
840 gst_value_init_double_range (GValue * value)
842 value->data[0].v_double = 0;
843 value->data[1].v_double = 0;
847 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
849 dest_value->data[0].v_double = src_value->data[0].v_double;
850 dest_value->data[1].v_double = src_value->data[1].v_double;
854 gst_value_collect_double_range (GValue * value, guint n_collect_values,
855 GTypeCValue * collect_values, guint collect_flags)
857 value->data[0].v_double = collect_values[0].v_double;
858 value->data[1].v_double = collect_values[1].v_double;
864 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
865 GTypeCValue * collect_values, guint collect_flags)
867 gdouble *double_range_start = collect_values[0].v_pointer;
868 gdouble *double_range_end = collect_values[1].v_pointer;
870 if (!double_range_start)
871 return g_strdup_printf ("start value location for `%s' passed as NULL",
872 G_VALUE_TYPE_NAME (value));
873 if (!double_range_end)
874 return g_strdup_printf ("end value location for `%s' passed as NULL",
875 G_VALUE_TYPE_NAME (value));
877 *double_range_start = value->data[0].v_double;
878 *double_range_end = value->data[1].v_double;
884 * gst_value_set_double_range:
885 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
886 * @start: the start of the range
887 * @end: the end of the range
889 * Sets @value to the range specified by @start and @end.
892 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
894 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
896 value->data[0].v_double = start;
897 value->data[1].v_double = end;
901 * gst_value_get_double_range_min:
902 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
904 * Gets the minimum of the range specified by @value.
906 * Returns: the minimum of the range
909 gst_value_get_double_range_min (const GValue * value)
911 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
913 return value->data[0].v_double;
917 * gst_value_get_double_range_max:
918 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
920 * Gets the maximum of the range specified by @value.
922 * Returns: the maxumum of the range
925 gst_value_get_double_range_max (const GValue * value)
927 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
929 return value->data[1].v_double;
933 gst_value_transform_double_range_string (const GValue * src_value,
936 char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
938 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
939 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
940 src_value->data[0].v_double),
941 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
942 src_value->data[1].v_double));
946 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
948 if (value2->data[0].v_double == value1->data[0].v_double &&
949 value2->data[0].v_double == value1->data[0].v_double)
950 return GST_VALUE_EQUAL;
951 return GST_VALUE_UNORDERED;
955 gst_value_serialize_double_range (const GValue * value)
957 char d1[G_ASCII_DTOSTR_BUF_SIZE];
958 char d2[G_ASCII_DTOSTR_BUF_SIZE];
960 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
961 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
962 return g_strdup_printf ("[ %s, %s ]", d1, d2);
966 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
968 g_warning ("unimplemented");
977 gst_value_init_fraction_range (GValue * value)
981 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
982 g_value_init (&vals[0], GST_TYPE_FRACTION);
983 g_value_init (&vals[1], GST_TYPE_FRACTION);
987 gst_value_free_fraction_range (GValue * value)
989 GValue *vals = (GValue *) value->data[0].v_pointer;
992 g_value_unset (&vals[0]);
993 g_value_unset (&vals[1]);
995 value->data[0].v_pointer = NULL;
1000 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1002 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1003 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1006 dest_value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1007 g_return_if_fail (vals != NULL);
1008 g_value_init (&vals[0], GST_TYPE_FRACTION);
1009 g_value_init (&vals[1], GST_TYPE_FRACTION);
1012 if (src_vals != NULL) {
1013 g_value_copy (&src_vals[0], &vals[0]);
1014 g_value_copy (&src_vals[1], &vals[1]);
1019 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1020 GTypeCValue * collect_values, guint collect_flags)
1022 GValue *vals = (GValue *) value->data[0].v_pointer;
1024 if (n_collect_values != 4)
1025 return g_strdup_printf ("not enough value locations for `%s' passed",
1026 G_VALUE_TYPE_NAME (value));
1028 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1030 return g_strdup_printf ("Could not initialise`%s' during collect",
1031 G_VALUE_TYPE_NAME (value));
1032 g_value_init (&vals[0], GST_TYPE_FRACTION);
1033 g_value_init (&vals[1], GST_TYPE_FRACTION);
1036 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1037 collect_values[1].v_int);
1038 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1039 collect_values[3].v_int);
1045 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1046 GTypeCValue * collect_values, guint collect_flags)
1049 int *dest_values[4];
1050 GValue *vals = (GValue *) value->data[0].v_pointer;
1052 if (n_collect_values != 4)
1053 return g_strdup_printf ("not enough value locations for `%s' passed",
1054 G_VALUE_TYPE_NAME (value));
1056 for (i = 0; i < 4; i++) {
1057 if (collect_values[i].v_pointer == NULL) {
1058 return g_strdup_printf ("value location for `%s' passed as NULL",
1059 G_VALUE_TYPE_NAME (value));
1061 dest_values[i] = collect_values[i].v_pointer;
1065 return g_strdup_printf ("Uninitialised `%s' passed",
1066 G_VALUE_TYPE_NAME (value));
1069 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1070 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1071 dest_values[2][0] = gst_value_get_fraction_denominator (&vals[1]);
1072 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1077 * gst_value_set_fraction_range:
1078 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1079 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1080 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1082 * Sets @value to the range specified by @start and @end.
1085 gst_value_set_fraction_range (GValue * value, const GValue * start,
1090 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1092 vals = (GValue *) value->data[0].v_pointer;
1094 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1095 g_value_init (&vals[0], GST_TYPE_FRACTION);
1096 g_value_init (&vals[1], GST_TYPE_FRACTION);
1099 g_value_copy (start, &vals[0]);
1100 g_value_copy (end, &vals[1]);
1104 * gst_value_set_fraction_range_full:
1105 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1106 * @numerator_start: the numerator start of the range
1107 * @denominator_start: the denominator start of the range
1108 * @numerator_end: the numerator end of the range
1109 * @denominator_end: the denominator end of the range
1111 * Sets @value to the range specified by @numerator_start/@denominator_start
1112 * and @numerator_end/@denominator_end.
1115 gst_value_set_fraction_range_full (GValue * value,
1116 gint numerator_start, gint denominator_start,
1117 gint numerator_end, gint denominator_end)
1119 GValue start = { 0 };
1122 g_value_init (&start, GST_TYPE_FRACTION);
1123 g_value_init (&end, GST_TYPE_FRACTION);
1125 gst_value_set_fraction (&start, numerator_start, denominator_start);
1126 gst_value_set_fraction (&end, numerator_end, denominator_end);
1127 gst_value_set_fraction_range (value, &start, &end);
1129 g_value_unset (&start);
1130 g_value_unset (&end);
1134 * gst_value_get_fraction_range_min:
1135 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1137 * Gets the minimum of the range specified by @value.
1139 * Returns: the minimum of the range
1142 gst_value_get_fraction_range_min (const GValue * value)
1146 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1148 vals = (GValue *) value->data[0].v_pointer;
1157 * gst_value_get_fraction_range_max:
1158 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1160 * Gets the maximum of the range specified by @value.
1162 * Returns: the maximum of the range
1165 gst_value_get_fraction_range_max (const GValue * value)
1169 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1171 vals = (GValue *) value->data[0].v_pointer;
1180 gst_value_serialize_fraction_range (const GValue * value)
1182 GValue *vals = (GValue *) value->data[0].v_pointer;
1186 retval = g_strdup ("[ 0/1, 0/1 ]");
1190 start = gst_value_serialize_fraction (&vals[0]);
1191 end = gst_value_serialize_fraction (&vals[1]);
1193 retval = g_strdup_printf ("[ %s, %s ]", start, end);
1202 gst_value_transform_fraction_range_string (const GValue * src_value,
1203 GValue * dest_value)
1205 dest_value->data[0].v_pointer =
1206 gst_value_serialize_fraction_range (src_value);
1210 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1212 GValue *vals1, *vals2;
1213 GstValueCompareFunc compare;
1215 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1216 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
1218 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1219 return GST_VALUE_UNORDERED;
1221 vals1 = (GValue *) value1->data[0].v_pointer;
1222 vals2 = (GValue *) value2->data[0].v_pointer;
1223 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1224 if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1226 gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1228 return GST_VALUE_EQUAL;
1230 return GST_VALUE_UNORDERED;
1234 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1236 g_warning ("unimplemented");
1245 * gst_value_set_caps:
1246 * @value: a GValue initialized to GST_TYPE_CAPS
1247 * @caps: the caps to set the value to
1249 * Sets the contents of @value to @caps. The actual
1250 * #GstCaps structure is copied before it is used.
1253 gst_value_set_caps (GValue * value, const GstCaps * caps)
1255 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1257 g_value_set_boxed (value, caps);
1261 * gst_value_get_caps:
1262 * @value: a GValue initialized to GST_TYPE_CAPS
1264 * Gets the contents of @value.
1266 * Returns: the contents of @value
1269 gst_value_get_caps (const GValue * value)
1271 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1273 return (GstCaps *) g_value_get_boxed (value);
1277 gst_value_serialize_caps (const GValue * value)
1279 GstCaps *caps = g_value_get_boxed (value);
1281 return gst_caps_to_string (caps);
1285 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1289 caps = gst_caps_from_string (s);
1292 g_value_take_boxed (dest, caps);
1303 * gst_value_set_structure:
1304 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1305 * @structure: the structure to set the value to
1307 * Sets the contents of @value to @structure. The actual
1312 gst_value_set_structure (GValue * value, const GstStructure * structure)
1314 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1316 g_value_set_boxed (value, structure);
1320 * gst_value_get_structure:
1321 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1323 * Gets the contents of @value.
1325 * Returns: the contents of @value
1329 const GstStructure *
1330 gst_value_get_structure (const GValue * value)
1332 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1334 return (GstStructure *) g_value_get_boxed (value);
1338 gst_value_serialize_structure (const GValue * value)
1340 GstStructure *structure = g_value_get_boxed (value);
1342 return gst_string_take_and_wrap (gst_structure_to_string (structure));
1346 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1348 GstStructure *structure;
1351 structure = gst_structure_from_string (s, NULL);
1353 gchar *str = gst_string_unwrap (s);
1355 if (G_UNLIKELY (!str))
1358 structure = gst_structure_from_string (str, NULL);
1362 if (G_LIKELY (structure)) {
1363 g_value_take_boxed (dest, structure);
1374 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1376 GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
1377 GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
1379 if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
1380 return GST_VALUE_UNORDERED;
1381 if (GST_BUFFER_SIZE (buf1) == 0)
1382 return GST_VALUE_EQUAL;
1383 g_assert (GST_BUFFER_DATA (buf1));
1384 g_assert (GST_BUFFER_DATA (buf2));
1385 if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
1386 GST_BUFFER_SIZE (buf1)) == 0)
1387 return GST_VALUE_EQUAL;
1389 return GST_VALUE_UNORDERED;
1393 gst_value_serialize_buffer (const GValue * value)
1401 buffer = gst_value_get_buffer (value);
1405 data = GST_BUFFER_DATA (buffer);
1406 size = GST_BUFFER_SIZE (buffer);
1408 string = g_malloc (size * 2 + 1);
1409 for (i = 0; i < size; i++) {
1410 sprintf (string + i * 2, "%02x", data[i]);
1412 string[size * 2] = 0;
1418 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1430 buffer = gst_buffer_new_and_alloc (len / 2);
1431 data = GST_BUFFER_DATA (buffer);
1432 for (i = 0; i < len / 2; i++) {
1433 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1436 ts[0] = s[i * 2 + 0];
1437 ts[1] = s[i * 2 + 1];
1440 data[i] = (guint8) strtoul (ts, NULL, 16);
1443 gst_value_take_buffer (dest, buffer);
1454 gst_buffer_unref (buffer);
1465 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1467 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1468 return GST_VALUE_EQUAL;
1469 return GST_VALUE_UNORDERED;
1473 gst_value_serialize_boolean (const GValue * value)
1475 if (value->data[0].v_int) {
1476 return g_strdup ("true");
1478 return g_strdup ("false");
1482 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1484 gboolean ret = FALSE;
1486 if (g_ascii_strcasecmp (s, "true") == 0 ||
1487 g_ascii_strcasecmp (s, "yes") == 0 ||
1488 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1489 g_value_set_boolean (dest, TRUE);
1491 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1492 g_ascii_strcasecmp (s, "no") == 0 ||
1493 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1494 g_value_set_boolean (dest, FALSE);
1501 #define CREATE_SERIALIZATION_START(_type,_macro) \
1503 gst_value_compare_ ## _type \
1504 (const GValue * value1, const GValue * value2) \
1506 g ## _type val1 = g_value_get_ ## _type (value1); \
1507 g ## _type val2 = g_value_get_ ## _type (value2); \
1509 return GST_VALUE_GREATER_THAN; \
1511 return GST_VALUE_LESS_THAN; \
1512 return GST_VALUE_EQUAL; \
1516 gst_value_serialize_ ## _type (const GValue * value) \
1518 GValue val = { 0, }; \
1519 g_value_init (&val, G_TYPE_STRING); \
1520 if (!g_value_transform (value, &val)) \
1521 g_assert_not_reached (); \
1522 /* NO_COPY_MADNESS!!! */ \
1523 return (char *) g_value_get_string (&val); \
1526 /* deserialize the given s into to as a gint64.
1527 * check if the result is actually storeable in the given size number of
1531 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1532 gint64 min, gint64 max, gint size)
1534 gboolean ret = FALSE;
1539 *to = g_ascii_strtoull (s, &end, 0);
1540 /* a range error is a definitive no-no */
1541 if (errno == ERANGE) {
1548 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1549 *to = G_LITTLE_ENDIAN;
1551 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1554 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1557 } else if (g_ascii_strcasecmp (s, "min") == 0) {
1560 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1566 /* by definition, a gint64 fits into a gint64; so ignore those */
1567 if (size != sizeof (mask)) {
1569 /* for positive numbers, we create a mask of 1's outside of the range
1570 * and 0's inside the range. An and will thus keep only 1 bits
1571 * outside of the range */
1572 mask <<= (size * 8);
1573 if ((mask & *to) != 0) {
1577 /* for negative numbers, we do a 2's complement version */
1578 mask <<= ((size * 8) - 1);
1579 if ((mask & *to) != mask) {
1588 #define CREATE_SERIALIZATION(_type,_macro) \
1589 CREATE_SERIALIZATION_START(_type,_macro) \
1592 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1596 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
1597 G_MAX ## _macro, sizeof (g ## _type))) { \
1598 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
1605 #define CREATE_USERIALIZATION(_type,_macro) \
1606 CREATE_SERIALIZATION_START(_type,_macro) \
1609 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1613 gboolean ret = FALSE; \
1616 x = g_ascii_strtoull (s, &end, 0); \
1617 /* a range error is a definitive no-no */ \
1618 if (errno == ERANGE) { \
1621 /* the cast ensures the range check later on makes sense */ \
1622 x = (g ## _type) x; \
1626 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
1627 x = G_LITTLE_ENDIAN; \
1629 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
1632 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
1635 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
1638 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
1639 x = G_MAX ## _macro; \
1644 if (x > G_MAX ## _macro) { \
1647 g_value_set_ ## _type (dest, x); \
1653 #define REGISTER_SERIALIZATION(_gtype, _type) \
1655 static const GstValueTable gst_value = { \
1657 gst_value_compare_ ## _type, \
1658 gst_value_serialize_ ## _type, \
1659 gst_value_deserialize_ ## _type, \
1662 gst_value_register (&gst_value); \
1665 CREATE_SERIALIZATION (int, INT);
1666 CREATE_SERIALIZATION (int64, INT64);
1667 CREATE_SERIALIZATION (long, LONG);
1669 CREATE_USERIALIZATION (uint, UINT);
1670 CREATE_USERIALIZATION (uint64, UINT64);
1671 CREATE_USERIALIZATION (ulong, ULONG);
1677 gst_value_compare_double (const GValue * value1, const GValue * value2)
1679 if (value1->data[0].v_double > value2->data[0].v_double)
1680 return GST_VALUE_GREATER_THAN;
1681 if (value1->data[0].v_double < value2->data[0].v_double)
1682 return GST_VALUE_LESS_THAN;
1683 if (value1->data[0].v_double == value2->data[0].v_double)
1684 return GST_VALUE_EQUAL;
1685 return GST_VALUE_UNORDERED;
1689 gst_value_serialize_double (const GValue * value)
1691 char d[G_ASCII_DTOSTR_BUF_SIZE];
1693 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1694 return g_strdup (d);
1698 gst_value_deserialize_double (GValue * dest, const gchar * s)
1701 gboolean ret = FALSE;
1704 x = g_ascii_strtod (s, &end);
1708 if (g_ascii_strcasecmp (s, "min") == 0) {
1711 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1717 g_value_set_double (dest, x);
1727 gst_value_compare_float (const GValue * value1, const GValue * value2)
1729 if (value1->data[0].v_float > value2->data[0].v_float)
1730 return GST_VALUE_GREATER_THAN;
1731 if (value1->data[0].v_float < value2->data[0].v_float)
1732 return GST_VALUE_LESS_THAN;
1733 if (value1->data[0].v_float == value2->data[0].v_float)
1734 return GST_VALUE_EQUAL;
1735 return GST_VALUE_UNORDERED;
1739 gst_value_serialize_float (const GValue * value)
1741 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
1743 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1744 return g_strdup (d);
1748 gst_value_deserialize_float (GValue * dest, const gchar * s)
1751 gboolean ret = FALSE;
1754 x = g_ascii_strtod (s, &end);
1758 if (g_ascii_strcasecmp (s, "min") == 0) {
1761 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1766 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1769 g_value_set_float (dest, (float) x);
1779 gst_value_compare_string (const GValue * value1, const GValue * value2)
1781 if (!value1->data[0].v_pointer || !value2->data[0].v_pointer) {
1782 return GST_VALUE_UNORDERED;
1784 int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1787 return GST_VALUE_LESS_THAN;
1789 return GST_VALUE_GREATER_THAN;
1790 return GST_VALUE_EQUAL;
1795 gst_string_measure_wrapping (const gchar * s)
1798 gboolean wrap = FALSE;
1805 if (GST_ASCII_IS_STRING (*s)) {
1807 } else if (*s < 0x20 || *s >= 0x7f) {
1817 return wrap ? len : -1;
1821 gst_string_wrap_inner (const gchar * s, int len)
1825 e = d = g_malloc (len + 3);
1829 if (GST_ASCII_IS_STRING (*s)) {
1831 } else if (*s < 0x20 || *s >= 0x7f) {
1833 *e++ = '0' + ((*(guchar *) s) >> 6);
1834 *e++ = '0' + (((*s) >> 3) & 0x7);
1835 *e++ = '0' + ((*s++) & 0x7);
1847 /* Do string wrapping/escaping */
1849 gst_string_wrap (const gchar * s)
1851 int len = gst_string_measure_wrapping (s);
1854 return g_strdup (s);
1856 return gst_string_wrap_inner (s, len);
1859 /* Same as above, but take ownership of the string */
1861 gst_string_take_and_wrap (gchar * s)
1864 int len = gst_string_measure_wrapping (s);
1869 out = gst_string_wrap_inner (s, len);
1876 * This function takes a string delimited with double quotes (")
1877 * and unescapes any \xxx octal numbers.
1879 * If sequences of \y are found where y is not in the range of
1880 * 0->3, y is copied unescaped.
1882 * If \xyy is found where x is an octal number but y is not, an
1883 * error is encountered and NULL is returned.
1885 * the input string must be \0 terminated.
1888 gst_string_unwrap (const gchar * s)
1891 gchar *read, *write;
1893 /* NULL string returns NULL */
1897 /* strings not starting with " are invalid */
1901 /* make copy of original string to hold the result. This
1902 * string will always be smaller than the original */
1907 /* need to move to the next position as we parsed the " */
1911 if (GST_ASCII_IS_STRING (*read)) {
1912 /* normal chars are just copied */
1914 } else if (*read == '"') {
1915 /* quote marks end of string */
1917 } else if (*read == '\\') {
1918 /* got an escape char, move to next position to read a tripplet
1919 * of octal numbers */
1921 /* is the next char a possible first octal number? */
1922 if (*read >= '0' && *read <= '3') {
1923 /* parse other 2 numbers, if one of them is not in the range of
1924 * an octal number, we error. We also catch the case where a zero
1925 * byte is found here. */
1926 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1929 /* now convert the octal number to a byte again. */
1930 *write++ = ((read[0] - '0') << 6) +
1931 ((read[1] - '0') << 3) + (read[2] - '0');
1935 /* if we run into a \0 here, we definately won't get a quote later */
1939 /* else copy \X sequence */
1943 /* weird character, error */
1947 /* if the string is not ending in " and zero terminated, we error */
1948 if (*read != '"' || read[1] != '\0')
1951 /* null terminate result string and return */
1961 gst_value_serialize_string (const GValue * value)
1963 return gst_string_wrap (value->data[0].v_pointer);
1967 gst_value_deserialize_string (GValue * dest, const gchar * s)
1970 if (!g_utf8_validate (s, -1, NULL))
1972 g_value_set_string (dest, s);
1975 gchar *str = gst_string_unwrap (s);
1977 if (G_UNLIKELY (!str))
1979 g_value_take_string (dest, str);
1990 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1992 GEnumValue *en1, *en2;
1993 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1994 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1996 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1997 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1998 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1999 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2000 g_type_class_unref (klass1);
2001 g_type_class_unref (klass2);
2002 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2003 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2004 if (en1->value < en2->value)
2005 return GST_VALUE_LESS_THAN;
2006 if (en1->value > en2->value)
2007 return GST_VALUE_GREATER_THAN;
2009 return GST_VALUE_EQUAL;
2013 gst_value_serialize_enum (const GValue * value)
2016 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
2018 g_return_val_if_fail (klass, NULL);
2019 en = g_enum_get_value (klass, g_value_get_enum (value));
2020 g_type_class_unref (klass);
2022 /* might be one of the custom formats registered later */
2023 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
2024 const GstFormatDefinition *format_def;
2026 format_def = gst_format_get_details (g_value_get_enum (value));
2027 g_return_val_if_fail (format_def != NULL, NULL);
2028 return g_strdup (format_def->description);
2031 g_return_val_if_fail (en, NULL);
2032 return g_strdup (en->value_name);
2036 gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
2039 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2042 return g_ascii_strcasecmp (s, format_def->description);
2046 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2049 gchar *endptr = NULL;
2050 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2052 g_return_val_if_fail (klass, FALSE);
2053 if (!(en = g_enum_get_value_by_name (klass, s))) {
2054 if (!(en = g_enum_get_value_by_nick (klass, s))) {
2055 gint i = strtol (s, &endptr, 0);
2057 if (endptr && *endptr == '\0') {
2058 en = g_enum_get_value (klass, i);
2062 g_type_class_unref (klass);
2064 /* might be one of the custom formats registered later */
2065 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2066 const GstFormatDefinition *format_def;
2069 iter = gst_format_iterate_definitions ();
2071 format_def = gst_iterator_find_custom (iter,
2072 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
2074 g_return_val_if_fail (format_def != NULL, FALSE);
2075 g_value_set_enum (dest, (gint) format_def->value);
2076 gst_iterator_free (iter);
2080 g_return_val_if_fail (en, FALSE);
2081 g_value_set_enum (dest, en->value);
2089 /* we just compare the value here */
2091 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2094 GFlagsClass *klass1 =
2095 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2096 GFlagsClass *klass2 =
2097 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2099 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2100 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2101 fl1 = g_value_get_flags (value1);
2102 fl2 = g_value_get_flags (value2);
2103 g_type_class_unref (klass1);
2104 g_type_class_unref (klass2);
2106 return GST_VALUE_LESS_THAN;
2108 return GST_VALUE_GREATER_THAN;
2110 return GST_VALUE_EQUAL;
2113 /* the different flags are serialized separated with a + */
2115 gst_value_serialize_flags (const GValue * value)
2119 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2120 gchar *result, *tmp;
2121 gboolean first = TRUE;
2123 g_return_val_if_fail (klass, NULL);
2125 flags = g_value_get_flags (value);
2127 /* if no flags are set, try to serialize to the _NONE string */
2129 fl = g_flags_get_first_value (klass, flags);
2130 return g_strdup (fl->value_name);
2133 /* some flags are set, so serialize one by one */
2134 result = g_strdup ("");
2136 fl = g_flags_get_first_value (klass, flags);
2138 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2144 flags &= ~fl->value;
2147 g_type_class_unref (klass);
2153 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2156 gchar *endptr = NULL;
2157 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2162 g_return_val_if_fail (klass, FALSE);
2164 /* split into parts delimited with + */
2165 split = g_strsplit (s, "+", 0);
2169 /* loop over each part */
2171 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2172 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2173 gint val = strtol (split[i], &endptr, 0);
2175 /* just or numeric value */
2176 if (endptr && *endptr == '\0') {
2187 g_type_class_unref (klass);
2188 g_value_set_flags (dest, flags);
2198 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2199 const GValue * src2)
2201 if (src2->data[0].v_int <= src1->data[0].v_int &&
2202 src2->data[1].v_int >= src1->data[0].v_int) {
2203 gst_value_init_and_copy (dest, src2);
2210 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2211 const GValue * src2)
2216 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2217 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2220 g_value_init (dest, GST_TYPE_INT_RANGE);
2221 gst_value_set_int_range (dest,
2222 MIN (src1->data[0].v_int, src2->data[0].v_int),
2223 MAX (src1->data[1].v_int, src2->data[1].v_int));
2235 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2236 const GValue * src2)
2238 if (src2->data[0].v_int <= src1->data[0].v_int &&
2239 src2->data[1].v_int >= src1->data[0].v_int) {
2240 gst_value_init_and_copy (dest, src1);
2248 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2249 const GValue * src2)
2254 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2255 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2258 g_value_init (dest, GST_TYPE_INT_RANGE);
2259 gst_value_set_int_range (dest, min, max);
2263 g_value_init (dest, G_TYPE_INT);
2264 g_value_set_int (dest, min);
2272 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2273 const GValue * src2)
2275 if (src2->data[0].v_double <= src1->data[0].v_double &&
2276 src2->data[1].v_double >= src1->data[0].v_double) {
2277 gst_value_init_and_copy (dest, src1);
2285 gst_value_intersect_double_range_double_range (GValue * dest,
2286 const GValue * src1, const GValue * src2)
2291 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2292 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2295 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2296 gst_value_set_double_range (dest, min, max);
2300 g_value_init (dest, G_TYPE_DOUBLE);
2301 g_value_set_int (dest, (int) min);
2309 gst_value_intersect_list (GValue * dest, const GValue * value1,
2310 const GValue * value2)
2313 GValue intersection = { 0, };
2314 gboolean ret = FALSE;
2316 size = gst_value_list_get_size (value1);
2317 for (i = 0; i < size; i++) {
2318 const GValue *cur = gst_value_list_get_value (value1, i);
2320 if (gst_value_intersect (&intersection, cur, value2)) {
2323 gst_value_init_and_copy (dest, &intersection);
2325 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2326 gst_value_list_append_value (dest, &intersection);
2328 GValue temp = { 0, };
2330 gst_value_init_and_copy (&temp, dest);
2331 g_value_unset (dest);
2332 gst_value_list_concat (dest, &temp, &intersection);
2333 g_value_unset (&temp);
2335 g_value_unset (&intersection);
2343 gst_value_intersect_array (GValue * dest, const GValue * src1,
2344 const GValue * src2)
2350 /* only works on similar-sized arrays */
2351 size = gst_value_array_get_size (src1);
2352 if (size != gst_value_array_get_size (src2))
2354 g_value_init (dest, GST_TYPE_ARRAY);
2356 for (n = 0; n < size; n++) {
2357 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2358 gst_value_array_get_value (src2, n))) {
2359 g_value_unset (dest);
2362 gst_value_array_append_value (dest, &val);
2363 g_value_unset (&val);
2370 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2371 const GValue * src2)
2375 GstValueCompareFunc compare;
2377 vals = src2->data[0].v_pointer;
2382 if ((compare = gst_value_get_compare_func (src1))) {
2383 res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2384 res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2386 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2387 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2388 gst_value_init_and_copy (dest, src1);
2397 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
2398 const GValue * src1, const GValue * src2)
2403 GValue *vals1, *vals2;
2404 GstValueCompareFunc compare;
2406 vals1 = src1->data[0].v_pointer;
2407 vals2 = src2->data[0].v_pointer;
2408 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2410 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2411 /* min = MAX (src1.start, src2.start) */
2412 res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2413 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2414 if (res == GST_VALUE_LESS_THAN)
2415 min = &vals2[0]; /* Take the max of the 2 */
2419 /* max = MIN (src1.end, src2.end) */
2420 res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2421 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2422 if (res == GST_VALUE_GREATER_THAN)
2423 max = &vals2[1]; /* Take the min of the 2 */
2427 res = gst_value_compare_with_func (min, max, compare);
2428 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2429 if (res == GST_VALUE_LESS_THAN) {
2430 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2431 vals1 = dest->data[0].v_pointer;
2432 g_value_copy (min, &vals1[0]);
2433 g_value_copy (max, &vals1[1]);
2436 if (res == GST_VALUE_EQUAL) {
2437 gst_value_init_and_copy (dest, min);
2450 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2451 const GValue * subtrahend)
2453 int min = gst_value_get_int_range_min (subtrahend);
2454 int max = gst_value_get_int_range_max (subtrahend);
2455 int val = g_value_get_int (minuend);
2457 /* subtracting a range from an int only works if the int is not in the
2459 if (val < min || val > max) {
2460 /* and the result is the int */
2461 gst_value_init_and_copy (dest, minuend);
2467 /* creates a new int range based on input values.
2470 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2475 GValue *pv1, *pv2; /* yeah, hungarian! */
2477 if (min1 <= max1 && min2 <= max2) {
2480 } else if (min1 <= max1) {
2483 } else if (min2 <= max2) {
2491 g_value_init (pv1, GST_TYPE_INT_RANGE);
2492 gst_value_set_int_range (pv1, min1, max1);
2493 } else if (min1 == max1) {
2494 g_value_init (pv1, G_TYPE_INT);
2495 g_value_set_int (pv1, min1);
2498 g_value_init (pv2, GST_TYPE_INT_RANGE);
2499 gst_value_set_int_range (pv2, min2, max2);
2500 } else if (min2 == max2) {
2501 g_value_init (pv2, G_TYPE_INT);
2502 g_value_set_int (pv2, min2);
2505 if (min1 <= max1 && min2 <= max2) {
2506 gst_value_list_concat (dest, pv1, pv2);
2507 g_value_unset (pv1);
2508 g_value_unset (pv2);
2514 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2515 const GValue * subtrahend)
2517 gint min = gst_value_get_int_range_min (minuend);
2518 gint max = gst_value_get_int_range_max (minuend);
2519 gint val = g_value_get_int (subtrahend);
2521 g_return_val_if_fail (min < max, FALSE);
2523 /* value is outside of the range, return range unchanged */
2524 if (val < min || val > max) {
2525 gst_value_init_and_copy (dest, minuend);
2528 /* max must be MAXINT too as val <= max */
2529 if (val == G_MAXINT) {
2533 /* min must be MININT too as val >= max */
2534 if (val == G_MININT) {
2538 gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2544 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2545 const GValue * subtrahend)
2547 gint min1 = gst_value_get_int_range_min (minuend);
2548 gint max1 = gst_value_get_int_range_max (minuend);
2549 gint min2 = gst_value_get_int_range_min (subtrahend);
2550 gint max2 = gst_value_get_int_range_max (subtrahend);
2552 if (max2 == G_MAXINT && min2 == G_MININT) {
2554 } else if (max2 == G_MAXINT) {
2555 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
2556 } else if (min2 == G_MININT) {
2557 return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
2559 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
2560 MAX (max2 + 1, min1), max1);
2565 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
2566 const GValue * subtrahend)
2568 gdouble min = gst_value_get_double_range_min (subtrahend);
2569 gdouble max = gst_value_get_double_range_max (subtrahend);
2570 gdouble val = g_value_get_double (minuend);
2572 if (val < min || val > max) {
2573 gst_value_init_and_copy (dest, minuend);
2580 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
2581 const GValue * subtrahend)
2583 /* since we don't have open ranges, we cannot create a hole in
2584 * a double range. We return the original range */
2585 gst_value_init_and_copy (dest, minuend);
2590 gst_value_subtract_double_range_double_range (GValue * dest,
2591 const GValue * minuend, const GValue * subtrahend)
2593 /* since we don't have open ranges, we have to approximate */
2594 /* done like with ints */
2595 gdouble min1 = gst_value_get_double_range_min (minuend);
2596 gdouble max2 = gst_value_get_double_range_max (minuend);
2597 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
2598 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
2601 GValue *pv1, *pv2; /* yeah, hungarian! */
2603 if (min1 < max1 && min2 < max2) {
2606 } else if (min1 < max1) {
2609 } else if (min2 < max2) {
2617 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
2618 gst_value_set_double_range (pv1, min1, max1);
2621 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
2622 gst_value_set_double_range (pv2, min2, max2);
2625 if (min1 < max1 && min2 < max2) {
2626 gst_value_list_concat (dest, pv1, pv2);
2627 g_value_unset (pv1);
2628 g_value_unset (pv2);
2634 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
2635 const GValue * subtrahend)
2638 GValue subtraction = { 0, };
2639 gboolean ret = FALSE;
2641 size = gst_value_list_get_size (minuend);
2642 for (i = 0; i < size; i++) {
2643 const GValue *cur = gst_value_list_get_value (minuend, i);
2645 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2647 gst_value_init_and_copy (dest, &subtraction);
2649 } else if (GST_VALUE_HOLDS_LIST (dest)
2650 && GST_VALUE_HOLDS_LIST (&subtraction)) {
2652 GValue unroll = { 0, };
2654 gst_value_init_and_copy (&unroll, dest);
2655 g_value_unset (dest);
2656 gst_value_list_concat (dest, &unroll, &subtraction);
2657 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2658 gst_value_list_append_value (dest, &subtraction);
2660 GValue temp = { 0, };
2662 gst_value_init_and_copy (&temp, dest);
2663 g_value_unset (dest);
2664 gst_value_list_concat (dest, &temp, &subtraction);
2665 g_value_unset (&temp);
2667 g_value_unset (&subtraction);
2674 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2675 const GValue * subtrahend)
2678 GValue data[2] = { {0,}, {0,} };
2679 GValue *subtraction = &data[0], *result = &data[1];
2681 gst_value_init_and_copy (result, minuend);
2682 size = gst_value_list_get_size (subtrahend);
2683 for (i = 0; i < size; i++) {
2684 const GValue *cur = gst_value_list_get_value (subtrahend, i);
2686 if (gst_value_subtract (subtraction, result, cur)) {
2687 GValue *temp = result;
2689 result = subtraction;
2691 g_value_unset (subtraction);
2693 g_value_unset (result);
2697 gst_value_init_and_copy (dest, result);
2698 g_value_unset (result);
2703 gst_value_subtract_fraction_fraction_range (GValue * dest,
2704 const GValue * minuend, const GValue * subtrahend)
2706 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
2707 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
2708 GstValueCompareFunc compare;
2710 if ((compare = gst_value_get_compare_func (minuend))) {
2711 /* subtracting a range from an fraction only works if the fraction
2712 * is not in the range */
2713 if (gst_value_compare_with_func (minuend, min, compare) ==
2714 GST_VALUE_LESS_THAN ||
2715 gst_value_compare_with_func (minuend, max, compare) ==
2716 GST_VALUE_GREATER_THAN) {
2717 /* and the result is the value */
2718 gst_value_init_and_copy (dest, minuend);
2726 gst_value_subtract_fraction_range_fraction (GValue * dest,
2727 const GValue * minuend, const GValue * subtrahend)
2729 /* since we don't have open ranges, we cannot create a hole in
2730 * a range. We return the original range */
2731 gst_value_init_and_copy (dest, minuend);
2736 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
2737 const GValue * minuend, const GValue * subtrahend)
2739 /* since we don't have open ranges, we have to approximate */
2740 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
2741 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
2742 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
2743 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
2744 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
2748 GValue *pv1, *pv2; /* yeah, hungarian! */
2749 GstValueCompareFunc compare;
2751 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
2752 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
2754 compare = gst_value_get_compare_func (min1);
2755 g_return_val_if_fail (compare, FALSE);
2757 cmp1 = gst_value_compare_with_func (max2, max1, compare);
2758 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2759 if (cmp1 == GST_VALUE_LESS_THAN)
2761 cmp1 = gst_value_compare_with_func (min1, min2, compare);
2762 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2763 if (cmp1 == GST_VALUE_GREATER_THAN)
2766 cmp1 = gst_value_compare_with_func (min1, max1, compare);
2767 cmp2 = gst_value_compare_with_func (min2, max2, compare);
2769 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2772 } else if (cmp1 == GST_VALUE_LESS_THAN) {
2775 } else if (cmp2 == GST_VALUE_LESS_THAN) {
2782 if (cmp1 == GST_VALUE_LESS_THAN) {
2783 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
2784 gst_value_set_fraction_range (pv1, min1, max1);
2786 if (cmp2 == GST_VALUE_LESS_THAN) {
2787 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
2788 gst_value_set_fraction_range (pv2, min2, max2);
2791 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2792 gst_value_list_concat (dest, pv1, pv2);
2793 g_value_unset (pv1);
2794 g_value_unset (pv2);
2805 * gst_value_can_compare:
2806 * @value1: a value to compare
2807 * @value2: another value to compare
2809 * Determines if @value1 and @value2 can be compared.
2811 * Returns: TRUE if the values can be compared
2814 gst_value_can_compare (const GValue * value1, const GValue * value2)
2816 GstValueTable *table;
2819 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2822 for (i = 0; i < gst_value_table->len; i++) {
2823 table = &g_array_index (gst_value_table, GstValueTable, i);
2824 if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2832 * gst_value_get_compare_func:
2833 * @value1: a value to get the compare function for
2835 * Determines the compare function to be used with values of the same type as
2836 * @value1. The function can be given to gst_value_compare_with_func().
2838 * Returns: A #GstValueCompareFunc value
2840 static GstValueCompareFunc
2841 gst_value_get_compare_func (const GValue * value1)
2843 GstValueTable *table, *best = NULL;
2846 /* this is a fast check */
2847 for (i = 0; i < gst_value_table->len; i++) {
2848 table = &g_array_index (gst_value_table, GstValueTable, i);
2849 if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2856 for (i = 0; i < gst_value_table->len; i++) {
2857 table = &g_array_index (gst_value_table, GstValueTable, i);
2858 if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2859 if (!best || g_type_is_a (table->type, best->type))
2865 return best->compare;
2871 * gst_value_compare:
2872 * @value1: a value to compare
2873 * @value2: another value to compare
2875 * Compares @value1 and @value2. If @value1 and @value2 cannot be
2876 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
2877 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
2878 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
2879 * If the values are equal, GST_VALUE_EQUAL is returned.
2881 * Returns: A #GstValueCompareType value
2884 gst_value_compare (const GValue * value1, const GValue * value2)
2886 GstValueCompareFunc compare;
2888 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2889 return GST_VALUE_UNORDERED;
2891 compare = gst_value_get_compare_func (value1);
2893 return compare (value1, value2);
2896 g_critical ("unable to compare values of type %s\n",
2897 g_type_name (G_VALUE_TYPE (value1)));
2898 return GST_VALUE_UNORDERED;
2902 * gst_value_compare_with_func:
2903 * @value1: a value to compare
2904 * @value2: another value to compare
2905 * @compare: compare function
2907 * Compares @value1 and @value2 using the @compare function. Works like
2908 * gst_value_compare() but allows to save time determining the compare function
2911 * Returns: A #GstValueCompareType value
2914 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
2915 GstValueCompareFunc compare)
2919 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2920 return GST_VALUE_UNORDERED;
2922 return compare (value1, value2);
2928 * gst_value_can_union:
2929 * @value1: a value to union
2930 * @value2: another value to union
2932 * Determines if @value1 and @value2 can be non-trivially unioned.
2933 * Any two values can be trivially unioned by adding both of them
2934 * to a GstValueList. However, certain types have the possibility
2935 * to be unioned in a simpler way. For example, an integer range
2936 * and an integer can be unioned if the integer is a subset of the
2937 * integer range. If there is the possibility that two values can
2938 * be unioned, this function returns TRUE.
2940 * Returns: TRUE if there is a function allowing the two values to
2944 gst_value_can_union (const GValue * value1, const GValue * value2)
2946 GstValueUnionInfo *union_info;
2949 for (i = 0; i < gst_value_union_funcs->len; i++) {
2950 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2951 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2952 union_info->type2 == G_VALUE_TYPE (value2))
2954 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2955 union_info->type2 == G_VALUE_TYPE (value1))
2964 * @dest: the destination value
2965 * @value1: a value to union
2966 * @value2: another value to union
2968 * Creates a GValue cooresponding to the union of @value1 and @value2.
2970 * Returns: always returns %TRUE
2972 /* FIXME: change return type to 'void'? */
2974 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2976 GstValueUnionInfo *union_info;
2979 for (i = 0; i < gst_value_union_funcs->len; i++) {
2980 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2981 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2982 union_info->type2 == G_VALUE_TYPE (value2)) {
2983 if (union_info->func (dest, value1, value2)) {
2987 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2988 union_info->type2 == G_VALUE_TYPE (value1)) {
2989 if (union_info->func (dest, value2, value1)) {
2995 gst_value_list_concat (dest, value1, value2);
3000 * gst_value_register_union_func:
3001 * @type1: a type to union
3002 * @type2: another type to union
3003 * @func: a function that implments creating a union between the two types
3005 * Registers a union function that can create a union between GValues
3006 * of the type @type1 and @type2.
3008 * Union functions should be registered at startup before any pipelines are
3009 * started, as gst_value_register_union_func() is not thread-safe and cannot
3010 * be used at the same time as gst_value_union() or gst_value_can_union().
3013 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
3015 GstValueUnionInfo union_info;
3017 union_info.type1 = type1;
3018 union_info.type2 = type2;
3019 union_info.func = func;
3021 g_array_append_val (gst_value_union_funcs, union_info);
3027 * gst_value_can_intersect:
3028 * @value1: a value to intersect
3029 * @value2: another value to intersect
3031 * Determines if intersecting two values will produce a valid result.
3032 * Two values will produce a valid intersection if they have the same
3033 * type, or if there is a method (registered by
3034 * gst_value_register_intersection_func()) to calculate the intersection.
3036 * Returns: TRUE if the values can intersect
3039 gst_value_can_intersect (const GValue * value1, const GValue * value2)
3041 GstValueIntersectInfo *intersect_info;
3045 if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
3048 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
3049 intersect_info = &g_array_index (gst_value_intersect_funcs,
3050 GstValueIntersectInfo, i);
3051 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
3052 intersect_info->type2 == G_VALUE_TYPE (value2))
3053 if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
3054 intersect_info->type1 == G_VALUE_TYPE (value2))
3058 return gst_value_can_compare (value1, value2);
3062 * gst_value_intersect:
3063 * @dest: a uninitialized #GValue that will hold the calculated
3064 * intersection value
3065 * @value1: a value to intersect
3066 * @value2: another value to intersect
3068 * Calculates the intersection of two values. If the values have
3069 * a non-empty intersection, the value representing the intersection
3070 * is placed in @dest. If the intersection is non-empty, @dest is
3073 * Returns: TRUE if the intersection is non-empty
3076 gst_value_intersect (GValue * dest, const GValue * value1,
3077 const GValue * value2)
3079 GstValueIntersectInfo *intersect_info;
3081 gboolean ret = FALSE;
3083 /* special cases first */
3084 if (GST_VALUE_HOLDS_LIST (value1))
3085 return gst_value_intersect_list (dest, value1, value2);
3086 if (GST_VALUE_HOLDS_LIST (value2))
3087 return gst_value_intersect_list (dest, value2, value1);
3089 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
3090 intersect_info = &g_array_index (gst_value_intersect_funcs,
3091 GstValueIntersectInfo, i);
3092 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
3093 intersect_info->type2 == G_VALUE_TYPE (value2)) {
3094 ret = intersect_info->func (dest, value1, value2);
3097 if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
3098 intersect_info->type2 == G_VALUE_TYPE (value1)) {
3099 ret = intersect_info->func (dest, value2, value1);
3104 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
3105 gst_value_init_and_copy (dest, value1);
3113 * gst_value_register_intersect_func:
3114 * @type1: the first type to intersect
3115 * @type2: the second type to intersect
3116 * @func: the intersection function
3118 * Registers a function that is called to calculate the intersection
3119 * of the values having the types @type1 and @type2.
3121 * Intersect functions should be registered at startup before any pipelines are
3122 * started, as gst_value_register_intersect_func() is not thread-safe and
3123 * cannot be used at the same time as gst_value_intersect() or
3124 * gst_value_can_intersect().
3127 gst_value_register_intersect_func (GType type1, GType type2,
3128 GstValueIntersectFunc func)
3130 GstValueIntersectInfo intersect_info;
3132 intersect_info.type1 = type1;
3133 intersect_info.type2 = type2;
3134 intersect_info.func = func;
3136 g_array_append_val (gst_value_intersect_funcs, intersect_info);
3143 * gst_value_subtract:
3144 * @dest: the destination value for the result if the subtraction is not empty
3145 * @minuend: the value to subtract from
3146 * @subtrahend: the value to subtract
3148 * Subtracts @subtrahend from @minuend and stores the result in @dest.
3149 * Note that this means subtraction as in sets, not as in mathematics.
3151 * Returns: %TRUE if the subtraction is not empty
3154 gst_value_subtract (GValue * dest, const GValue * minuend,
3155 const GValue * subtrahend)
3157 GstValueSubtractInfo *info;
3160 /* special cases first */
3161 if (GST_VALUE_HOLDS_LIST (minuend))
3162 return gst_value_subtract_from_list (dest, minuend, subtrahend);
3163 if (GST_VALUE_HOLDS_LIST (subtrahend))
3164 return gst_value_subtract_list (dest, minuend, subtrahend);
3166 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3167 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3168 if (info->minuend == G_VALUE_TYPE (minuend) &&
3169 info->subtrahend == G_VALUE_TYPE (subtrahend)) {
3170 return info->func (dest, minuend, subtrahend);
3174 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3175 gst_value_init_and_copy (dest, minuend);
3184 gst_value_subtract (GValue * dest, const GValue * minuend,
3185 const GValue * subtrahend)
3187 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3189 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
3190 gst_value_serialize (subtrahend),
3191 ret ? gst_value_serialize (dest) : "---");
3197 * gst_value_can_subtract:
3198 * @minuend: the value to subtract from
3199 * @subtrahend: the value to subtract
3201 * Checks if it's possible to subtract @subtrahend from @minuend.
3203 * Returns: TRUE if a subtraction is possible
3206 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3208 GstValueSubtractInfo *info;
3212 if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
3215 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3216 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3217 if (info->minuend == G_VALUE_TYPE (minuend) &&
3218 info->subtrahend == G_VALUE_TYPE (subtrahend))
3222 return gst_value_can_compare (minuend, subtrahend);
3226 * gst_value_register_subtract_func:
3227 * @minuend_type: type of the minuend
3228 * @subtrahend_type: type of the subtrahend
3229 * @func: function to use
3231 * Registers @func as a function capable of subtracting the values of
3232 * @subtrahend_type from values of @minuend_type.
3234 * Subtract functions should be registered at startup before any pipelines are
3235 * started, as gst_value_register_subtract_func() is not thread-safe and
3236 * cannot be used at the same time as gst_value_subtract().
3239 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3240 GstValueSubtractFunc func)
3242 GstValueSubtractInfo info;
3244 /* one type must be unfixed, other subtractions can be done as comparisons */
3245 g_return_if_fail (!gst_type_is_fixed (minuend_type)
3246 || !gst_type_is_fixed (subtrahend_type));
3248 info.minuend = minuend_type;
3249 info.subtrahend = subtrahend_type;
3252 g_array_append_val (gst_value_subtract_funcs, info);
3256 * gst_value_register:
3257 * @table: structure containing functions to register
3259 * Registers functions to perform calculations on #GValues of a given
3263 gst_value_register (const GstValueTable * table)
3265 g_array_append_val (gst_value_table, *table);
3269 * gst_value_init_and_copy:
3270 * @dest: the target value
3271 * @src: the source value
3273 * Initialises the target value to be of the same type as source and then copies
3274 * the contents from source to target.
3277 gst_value_init_and_copy (GValue * dest, const GValue * src)
3279 g_value_init (dest, G_VALUE_TYPE (src));
3280 g_value_copy (src, dest);
3284 * gst_value_serialize:
3285 * @value: a #GValue to serialize
3287 * tries to transform the given @value into a string representation that allows
3288 * getting back this string later on using gst_value_deserialize().
3290 * Returns: the serialization for @value or NULL if none exists
3293 gst_value_serialize (const GValue * value)
3296 GValue s_val = { 0 };
3297 GstValueTable *table, *best = NULL;
3300 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3302 for (i = 0; i < gst_value_table->len; i++) {
3303 table = &g_array_index (gst_value_table, GstValueTable, i);
3304 if (table->serialize == NULL)
3306 if (table->type == G_VALUE_TYPE (value)) {
3310 if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
3311 if (!best || g_type_is_a (table->type, best->type))
3316 return best->serialize (value);
3318 g_value_init (&s_val, G_TYPE_STRING);
3319 if (g_value_transform (value, &s_val)) {
3320 s = gst_string_wrap (g_value_get_string (&s_val));
3324 g_value_unset (&s_val);
3330 * gst_value_deserialize:
3331 * @dest: #GValue to fill with contents of deserialization
3332 * @src: string to deserialize
3334 * Tries to deserialize a string into the type specified by the given GValue.
3335 * If the operation succeeds, TRUE is returned, FALSE otherwise.
3337 * Returns: TRUE on success
3340 gst_value_deserialize (GValue * dest, const gchar * src)
3342 GstValueTable *table, *best = NULL;
3345 g_return_val_if_fail (src != NULL, FALSE);
3346 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
3348 for (i = 0; i < gst_value_table->len; i++) {
3349 table = &g_array_index (gst_value_table, GstValueTable, i);
3350 if (table->serialize == NULL)
3353 if (table->type == G_VALUE_TYPE (dest)) {
3358 if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
3359 if (!best || g_type_is_a (table->type, best->type))
3364 return best->deserialize (dest, src);
3371 * gst_value_is_fixed:
3372 * @value: the #GValue to check
3374 * Tests if the given GValue, if available in a GstStructure (or any other
3375 * container) contains a "fixed" (which means: one value) or an "unfixed"
3376 * (which means: multiple possible values, such as data lists or data
3379 * Returns: true if the value is "fixed".
3383 gst_value_is_fixed (const GValue * value)
3385 GType type = G_VALUE_TYPE (value);
3387 /* the most common types are just basic plain glib types */
3388 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
3392 if (type == GST_TYPE_ARRAY) {
3396 /* check recursively */
3397 size = gst_value_array_get_size (value);
3398 for (n = 0; n < size; n++) {
3399 kid = gst_value_array_get_value (value, n);
3400 if (!gst_value_is_fixed (kid))
3405 return gst_type_is_fixed (type);
3412 /* helper functions */
3414 /* Finds the greatest common divisor.
3415 * Returns 1 if none other found.
3416 * This is Euclid's algorithm. */
3418 gst_greatest_common_divisor (gint a, gint b)
3431 gst_value_init_fraction (GValue * value)
3433 value->data[0].v_int = 0;
3434 value->data[1].v_int = 1;
3438 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
3440 dest_value->data[0].v_int = src_value->data[0].v_int;
3441 dest_value->data[1].v_int = src_value->data[1].v_int;
3445 gst_value_collect_fraction (GValue * value, guint n_collect_values,
3446 GTypeCValue * collect_values, guint collect_flags)
3448 gst_value_set_fraction (value,
3449 collect_values[0].v_int, collect_values[1].v_int);
3455 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
3456 GTypeCValue * collect_values, guint collect_flags)
3458 gint *numerator = collect_values[0].v_pointer;
3459 gint *denominator = collect_values[1].v_pointer;
3462 return g_strdup_printf ("numerator for `%s' passed as NULL",
3463 G_VALUE_TYPE_NAME (value));
3465 return g_strdup_printf ("denominator for `%s' passed as NULL",
3466 G_VALUE_TYPE_NAME (value));
3468 *numerator = value->data[0].v_int;
3469 *denominator = value->data[1].v_int;
3475 * gst_value_set_fraction:
3476 * @value: a GValue initialized to #GST_TYPE_FRACTION
3477 * @numerator: the numerator of the fraction
3478 * @denominator: the denominator of the fraction
3480 * Sets @value to the fraction specified by @numerator over @denominator.
3481 * The fraction gets reduced to the smallest numerator and denominator,
3482 * and if necessary the sign is moved to the numerator.
3485 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
3489 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
3490 g_return_if_fail (denominator != 0);
3491 g_return_if_fail (denominator >= -G_MAXINT);
3492 g_return_if_fail (numerator >= -G_MAXINT);
3494 /* normalize sign */
3495 if (denominator < 0) {
3496 numerator = -numerator;
3497 denominator = -denominator;
3500 /* check for reduction */
3501 gcd = gst_greatest_common_divisor (numerator, denominator);
3507 g_assert (denominator > 0);
3509 value->data[0].v_int = numerator;
3510 value->data[1].v_int = denominator;
3514 * gst_value_get_fraction_numerator:
3515 * @value: a GValue initialized to #GST_TYPE_FRACTION
3517 * Gets the numerator of the fraction specified by @value.
3519 * Returns: the numerator of the fraction.
3522 gst_value_get_fraction_numerator (const GValue * value)
3524 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
3526 return value->data[0].v_int;
3530 * gst_value_get_fraction_denominator:
3531 * @value: a GValue initialized to #GST_TYPE_FRACTION
3533 * Gets the denominator of the fraction specified by @value.
3535 * Returns: the denominator of the fraction.
3538 gst_value_get_fraction_denominator (const GValue * value)
3540 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
3542 return value->data[1].v_int;
3546 * gst_value_fraction_multiply:
3547 * @product: a GValue initialized to #GST_TYPE_FRACTION
3548 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
3549 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
3551 * Multiplies the two GValues containing a GstFraction and sets @product
3552 * to the product of the two fractions.
3554 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3557 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
3558 const GValue * factor2)
3560 gint gcd, n1, n2, d1, d2;
3562 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
3563 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
3565 n1 = factor1->data[0].v_int;
3566 n2 = factor2->data[0].v_int;
3567 d1 = factor1->data[1].v_int;
3568 d2 = factor2->data[1].v_int;
3570 gcd = gst_greatest_common_divisor (n1, d2);
3573 gcd = gst_greatest_common_divisor (n2, d1);
3577 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
3578 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3580 gst_value_set_fraction (product, n1 * n2, d1 * d2);
3586 * gst_value_fraction_subtract:
3587 * @dest: a GValue initialized to #GST_TYPE_FRACTION
3588 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
3589 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
3591 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
3593 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3596 gst_value_fraction_subtract (GValue * dest,
3597 const GValue * minuend, const GValue * subtrahend)
3599 gint n1, n2, d1, d2;
3601 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
3602 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
3604 n1 = minuend->data[0].v_int;
3605 n2 = subtrahend->data[0].v_int;
3606 d1 = minuend->data[1].v_int;
3607 d2 = subtrahend->data[1].v_int;
3610 gst_value_set_fraction (dest, -n2, d2);
3614 gst_value_set_fraction (dest, n1, d1);
3618 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (d2), FALSE);
3619 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (n2), FALSE);
3620 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3622 gst_value_set_fraction (dest, (n1 * d2) - (n2 * d1), d1 * d2);
3628 gst_value_serialize_fraction (const GValue * value)
3630 gint32 numerator = value->data[0].v_int;
3631 gint32 denominator = value->data[1].v_int;
3632 gboolean positive = TRUE;
3634 /* get the sign and make components absolute */
3635 if (numerator < 0) {
3636 numerator = -numerator;
3637 positive = !positive;
3639 if (denominator < 0) {
3640 denominator = -denominator;
3641 positive = !positive;
3644 return g_strdup_printf ("%s%d/%d",
3645 positive ? "" : "-", numerator, denominator);
3649 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
3653 if (G_UNLIKELY (s == NULL))
3656 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
3659 if (sscanf (s, "%d/%d", &num, &den) == 2) {
3660 gst_value_set_fraction (dest, num, den);
3663 if (sscanf (s, "%d", &num) == 1) {
3664 gst_value_set_fraction (dest, num, 1);
3667 if (g_ascii_strcasecmp (s, "min") == 0) {
3668 gst_value_set_fraction (dest, -G_MAXINT, 1);
3670 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3671 gst_value_set_fraction (dest, G_MAXINT, 1);
3679 gst_value_transform_fraction_string (const GValue * src_value,
3680 GValue * dest_value)
3682 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
3686 gst_value_transform_string_fraction (const GValue * src_value,
3687 GValue * dest_value)
3689 if (!gst_value_deserialize_fraction (dest_value,
3690 src_value->data[0].v_pointer))
3691 /* If the deserialize fails, ensure we leave the fraction in a
3692 * valid, if incorrect, state */
3693 gst_value_set_fraction (dest_value, 0, 1);
3696 #define MAX_TERMS 30
3697 #define MIN_DIVISOR 1.0e-10
3698 #define MAX_ERROR 1.0e-20
3700 /* use continued fractions to transform a double into a fraction,
3701 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3702 * This algorithm takes care of overflows.
3705 gst_value_transform_double_fraction (const GValue * src_value,
3706 GValue * dest_value)
3708 gdouble V, F; /* double being converted */
3709 gint N, D; /* will contain the result */
3710 gint A; /* current term in continued fraction */
3711 gint64 N1, D1; /* numerator, denominator of last approx */
3712 gint64 N2, D2; /* numerator, denominator of previous approx */
3714 gboolean negative = FALSE;
3716 /* initialize fraction being converted */
3717 F = src_value->data[0].v_double;
3724 /* initialize fractions with 1/0, 0/1 */
3732 for (i = 0; i < MAX_TERMS; i++) {
3734 A = (gint) F; /* no floor() needed, F is always >= 0 */
3735 /* get new divisor */
3738 /* calculate new fraction in temp */
3742 /* guard against overflow */
3743 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3750 /* save last two fractions */
3756 /* quit if dividing by zero or close enough to target */
3757 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3761 /* Take reciprocal */
3764 /* fix for overflow */
3769 /* fix for negative */
3773 /* will also simplify */
3774 gst_value_set_fraction (dest_value, N, D);
3778 gst_value_transform_fraction_double (const GValue * src_value,
3779 GValue * dest_value)
3781 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
3782 ((double) src_value->data[1].v_int);
3786 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
3794 n1 = value1->data[0].v_int;
3795 n2 = value2->data[0].v_int;
3796 d1 = value1->data[1].v_int;
3797 d2 = value2->data[1].v_int;
3799 /* fractions are reduced when set, so we can quickly see if they're equal */
3800 if (n1 == n2 && d1 == d2)
3801 return GST_VALUE_EQUAL;
3803 /* extend to 64 bits */
3804 new_num_1 = ((gint64) n1) * d2;
3805 new_num_2 = ((gint64) n2) * d1;
3806 if (new_num_1 < new_num_2)
3807 return GST_VALUE_LESS_THAN;
3808 if (new_num_1 > new_num_2)
3809 return GST_VALUE_GREATER_THAN;
3811 /* new_num_1 == new_num_2 implies that both denominators must have
3812 * been 0, beause otherwise simplification would have caught the
3814 return GST_VALUE_UNORDERED;
3822 * gst_value_set_date:
3823 * @value: a GValue initialized to GST_TYPE_DATE
3824 * @date: the date to set the value to
3826 * Sets the contents of @value to coorespond to @date. The actual
3827 * #GDate structure is copied before it is used.
3830 gst_value_set_date (GValue * value, const GDate * date)
3832 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
3833 g_return_if_fail (g_date_valid (date));
3835 g_value_set_boxed (value, date);
3839 * gst_value_get_date:
3840 * @value: a GValue initialized to GST_TYPE_DATE
3842 * Gets the contents of @value.
3844 * Returns: the contents of @value
3847 gst_value_get_date (const GValue * value)
3849 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3851 return (const GDate *) g_value_get_boxed (value);
3855 gst_date_copy (gpointer boxed)
3857 const GDate *date = (const GDate *) boxed;
3859 if (!g_date_valid (date)) {
3860 GST_WARNING ("invalid GDate");
3864 return g_date_new_julian (g_date_get_julian (date));
3868 gst_value_compare_date (const GValue * value1, const GValue * value2)
3870 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3871 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3875 return GST_VALUE_EQUAL;
3877 if ((date1 == NULL || !g_date_valid (date1))
3878 && (date2 != NULL && g_date_valid (date2))) {
3879 return GST_VALUE_LESS_THAN;
3882 if ((date2 == NULL || !g_date_valid (date2))
3883 && (date1 != NULL && g_date_valid (date1))) {
3884 return GST_VALUE_GREATER_THAN;
3887 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3888 || !g_date_valid (date2)) {
3889 return GST_VALUE_UNORDERED;
3892 j1 = g_date_get_julian (date1);
3893 j2 = g_date_get_julian (date2);
3896 return GST_VALUE_EQUAL;
3898 return GST_VALUE_LESS_THAN;
3900 return GST_VALUE_GREATER_THAN;
3904 gst_value_serialize_date (const GValue * val)
3906 const GDate *date = (const GDate *) g_value_get_boxed (val);
3908 if (date == NULL || !g_date_valid (date))
3909 return g_strdup ("9999-99-99");
3911 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3912 g_date_get_month (date), g_date_get_day (date));
3916 gst_value_deserialize_date (GValue * dest, const char *s)
3918 guint year, month, day;
3920 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3923 if (!g_date_valid_dmy (day, month, year))
3926 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3931 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3933 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3937 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3939 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3943 gst_value_transform_object_string (const GValue * src_value,
3944 GValue * dest_value)
3949 obj = g_value_get_object (src_value);
3952 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
3953 GST_OBJECT_NAME (obj));
3955 str = g_strdup ("NULL");
3958 dest_value->data[0].v_pointer = str;
3961 static GTypeInfo _info = {
3974 static GTypeFundamentalInfo _finfo = {
3978 #define FUNC_VALUE_GET_TYPE(type, name) \
3979 GType gst_ ## type ## _get_type (void) \
3981 static GType gst_ ## type ## _type = 0; \
3983 if (G_UNLIKELY (gst_ ## type ## _type == 0)) { \
3984 _info.value_table = & _gst_ ## type ## _value_table; \
3985 gst_ ## type ## _type = g_type_register_fundamental ( \
3986 g_type_fundamental_next (), \
3987 name, &_info, &_finfo, 0); \
3990 return gst_ ## type ## _type; \
3993 static const GTypeValueTable _gst_fourcc_value_table = {
3994 gst_value_init_fourcc,
3996 gst_value_copy_fourcc,
3999 gst_value_collect_fourcc,
4001 gst_value_lcopy_fourcc
4004 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
4006 static const GTypeValueTable _gst_int_range_value_table = {
4007 gst_value_init_int_range,
4009 gst_value_copy_int_range,
4012 gst_value_collect_int_range,
4014 gst_value_lcopy_int_range
4017 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
4019 static const GTypeValueTable _gst_double_range_value_table = {
4020 gst_value_init_double_range,
4022 gst_value_copy_double_range,
4025 gst_value_collect_double_range,
4027 gst_value_lcopy_double_range
4030 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
4032 static const GTypeValueTable _gst_fraction_range_value_table = {
4033 gst_value_init_fraction_range,
4034 gst_value_free_fraction_range,
4035 gst_value_copy_fraction_range,
4038 gst_value_collect_fraction_range,
4040 gst_value_lcopy_fraction_range
4043 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
4045 static const GTypeValueTable _gst_value_list_value_table = {
4046 gst_value_init_list_or_array,
4047 gst_value_free_list_or_array,
4048 gst_value_copy_list_or_array,
4049 gst_value_list_or_array_peek_pointer,
4051 gst_value_collect_list_or_array,
4053 gst_value_lcopy_list_or_array
4056 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
4058 static const GTypeValueTable _gst_value_array_value_table = {
4059 gst_value_init_list_or_array,
4060 gst_value_free_list_or_array,
4061 gst_value_copy_list_or_array,
4062 gst_value_list_or_array_peek_pointer,
4064 gst_value_collect_list_or_array,
4066 gst_value_lcopy_list_or_array
4069 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
4071 static const GTypeValueTable _gst_fraction_value_table = {
4072 gst_value_init_fraction,
4074 gst_value_copy_fraction,
4077 gst_value_collect_fraction,
4079 gst_value_lcopy_fraction
4082 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
4086 gst_date_get_type (void)
4088 static GType gst_date_type = 0;
4090 if (G_UNLIKELY (gst_date_type == 0)) {
4091 /* FIXME 0.11: we require GLib 2.8 already
4092 * Not using G_TYPE_DATE here on purpose, even if we could
4093 * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
4094 * serialised strings to have different type strings depending
4095 * on what version is used, so FIXME when we require GLib-2.8 */
4096 gst_date_type = g_boxed_type_register_static ("GstDate",
4097 (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
4100 return gst_date_type;
4104 _gst_value_initialize (void)
4106 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
4107 gst_value_union_funcs = g_array_new (FALSE, FALSE,
4108 sizeof (GstValueUnionInfo));
4109 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
4110 sizeof (GstValueIntersectInfo));
4111 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4112 sizeof (GstValueSubtractInfo));
4115 static GstValueTable gst_value = {
4117 gst_value_compare_fourcc,
4118 gst_value_serialize_fourcc,
4119 gst_value_deserialize_fourcc,
4122 gst_value.type = gst_fourcc_get_type ();
4123 gst_value_register (&gst_value);
4127 static GstValueTable gst_value = {
4129 gst_value_compare_int_range,
4130 gst_value_serialize_int_range,
4131 gst_value_deserialize_int_range,
4134 gst_value.type = gst_int_range_get_type ();
4135 gst_value_register (&gst_value);
4139 static GstValueTable gst_value = {
4141 gst_value_compare_double_range,
4142 gst_value_serialize_double_range,
4143 gst_value_deserialize_double_range,
4146 gst_value.type = gst_double_range_get_type ();
4147 gst_value_register (&gst_value);
4151 static GstValueTable gst_value = {
4153 gst_value_compare_fraction_range,
4154 gst_value_serialize_fraction_range,
4155 gst_value_deserialize_fraction_range,
4158 gst_value.type = gst_fraction_range_get_type ();
4159 gst_value_register (&gst_value);
4163 static GstValueTable gst_value = {
4165 gst_value_compare_list,
4166 gst_value_serialize_list,
4167 gst_value_deserialize_list,
4170 gst_value.type = gst_value_list_get_type ();
4171 gst_value_register (&gst_value);
4175 static GstValueTable gst_value = {
4177 gst_value_compare_array,
4178 gst_value_serialize_array,
4179 gst_value_deserialize_array,
4182 gst_value.type = gst_value_array_get_type ();;
4183 gst_value_register (&gst_value);
4188 static const GTypeValueTable value_table = {
4189 gst_value_init_buffer,
4191 gst_value_copy_buffer,
4194 NULL, /*gst_value_collect_buffer, */
4196 NULL /*gst_value_lcopy_buffer */
4199 static GstValueTable gst_value = {
4201 gst_value_compare_buffer,
4202 gst_value_serialize_buffer,
4203 gst_value_deserialize_buffer,
4206 gst_value.type = GST_TYPE_BUFFER;
4207 gst_value_register (&gst_value);
4210 static GstValueTable gst_value = {
4212 gst_value_compare_fraction,
4213 gst_value_serialize_fraction,
4214 gst_value_deserialize_fraction,
4217 gst_value.type = gst_fraction_get_type ();
4218 gst_value_register (&gst_value);
4221 static GstValueTable gst_value = {
4224 gst_value_serialize_caps,
4225 gst_value_deserialize_caps,
4228 gst_value.type = GST_TYPE_CAPS;
4229 gst_value_register (&gst_value);
4232 static GstValueTable gst_value = {
4235 gst_value_serialize_structure,
4236 gst_value_deserialize_structure,
4239 gst_value.type = GST_TYPE_STRUCTURE;
4240 gst_value_register (&gst_value);
4243 static GstValueTable gst_value = {
4245 gst_value_compare_date,
4246 gst_value_serialize_date,
4247 gst_value_deserialize_date,
4250 gst_value.type = gst_date_get_type ();
4251 gst_value_register (&gst_value);
4254 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
4255 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
4257 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
4258 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
4259 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
4261 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
4263 REGISTER_SERIALIZATION (G_TYPE_INT, int);
4265 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
4266 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
4268 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
4269 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
4270 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
4272 g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
4273 gst_value_transform_fourcc_string);
4274 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
4275 gst_value_transform_int_range_string);
4276 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
4277 gst_value_transform_double_range_string);
4278 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
4279 gst_value_transform_fraction_range_string);
4280 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
4281 gst_value_transform_list_string);
4282 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
4283 gst_value_transform_array_string);
4284 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
4285 gst_value_transform_fraction_string);
4286 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
4287 gst_value_transform_string_fraction);
4288 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
4289 gst_value_transform_fraction_double);
4290 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
4291 gst_value_transform_double_fraction);
4292 g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
4293 gst_value_transform_date_string);
4294 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
4295 gst_value_transform_string_date);
4296 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
4297 gst_value_transform_object_string);
4299 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4300 gst_value_intersect_int_int_range);
4301 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4302 gst_value_intersect_int_range_int_range);
4303 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4304 gst_value_intersect_double_double_range);
4305 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
4306 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
4307 gst_value_register_intersect_func (GST_TYPE_ARRAY,
4308 GST_TYPE_ARRAY, gst_value_intersect_array);
4309 gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4310 gst_value_intersect_fraction_fraction_range);
4311 gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
4312 GST_TYPE_FRACTION_RANGE,
4313 gst_value_intersect_fraction_range_fraction_range);
4315 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4316 gst_value_subtract_int_int_range);
4317 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
4318 gst_value_subtract_int_range_int);
4319 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4320 gst_value_subtract_int_range_int_range);
4321 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4322 gst_value_subtract_double_double_range);
4323 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
4324 gst_value_subtract_double_range_double);
4325 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
4326 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
4328 gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4329 gst_value_subtract_fraction_fraction_range);
4330 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
4331 gst_value_subtract_fraction_range_fraction);
4332 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
4333 GST_TYPE_FRACTION_RANGE,
4334 gst_value_subtract_fraction_range_fraction_range);
4336 /* see bug #317246, #64994, #65041 */
4338 volatile GType date_type = G_TYPE_DATE;
4340 g_type_name (date_type);
4343 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4344 gst_value_union_int_int_range);
4345 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4346 gst_value_union_int_range_int_range);
4349 /* Implement these if needed */
4350 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4351 gst_value_union_fraction_fraction_range);
4352 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
4353 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);