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 to GStreamer
34 #include "gst_private.h"
36 #include <gobject/gvaluecollector.h>
38 typedef struct _GstValueUnionInfo GstValueUnionInfo;
39 struct _GstValueUnionInfo
43 GstValueUnionFunc func;
46 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
47 struct _GstValueIntersectInfo
51 GstValueIntersectFunc func;
54 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
55 struct _GstValueSubtractInfo
59 GstValueSubtractFunc func;
62 GType gst_type_fourcc;
63 GType gst_type_int_range;
64 GType gst_type_double_range;
67 GType gst_type_fraction;
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;
79 /* two helper functions to serialize/stringify any type of list
80 * regular lists are done with { }, arrays with < >
83 gst_value_serialize_any_list (const GValue * value, const char *begin,
87 GArray *array = value->data[0].v_pointer;
92 s = g_string_new (begin);
93 for (i = 0; i < array->len; i++) {
94 v = &g_array_index (array, GValue, i);
95 s_val = gst_value_serialize (v);
96 g_string_append (s, s_val);
98 if (i < array->len - 1) {
99 g_string_append (s, ", ");
102 g_string_append (s, end);
103 return g_string_free (s, FALSE);
107 gst_value_transform_any_list_string (const GValue * src_value,
108 GValue * dest_value, const char *begin, const char *end)
116 array = src_value->data[0].v_pointer;
118 s = g_string_new (begin);
119 for (i = 0; i < array->len; i++) {
120 list_value = &g_array_index (array, GValue, i);
123 g_string_append (s, ", ");
125 list_s = g_strdup_value_contents (list_value);
126 g_string_append (s, list_s);
129 g_string_append (s, end);
131 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
135 * helper function to see if a type is fixed. Is used internally here and
136 * there. Do not export, since it doesn't work for types where the content
137 * decides the fixedness (e.g. GST_TYPE_ARRAY).
141 gst_type_is_fixed (GType type)
143 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
144 type == GST_TYPE_LIST) {
147 if (G_TYPE_FUNDAMENTAL (type) <=
148 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
151 if (type == GST_TYPE_BUFFER || type == GST_TYPE_FOURCC
152 || type == GST_TYPE_ARRAY || type == GST_TYPE_FRACTION) {
159 /* GValue functions usable for both regular lists and arrays */
161 gst_value_init_list (GValue * value)
163 value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
167 gst_value_list_array_copy (const GArray * src)
172 dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
173 g_array_set_size (dest, src->len);
174 for (i = 0; i < src->len; i++) {
175 gst_value_init_and_copy (&g_array_index (dest, GValue, i),
176 &g_array_index (src, GValue, i));
183 gst_value_copy_list (const GValue * src_value, GValue * dest_value)
185 dest_value->data[0].v_pointer =
186 gst_value_list_array_copy ((GArray *) src_value->data[0].v_pointer);
190 gst_value_free_list (GValue * value)
193 GArray *src = (GArray *) value->data[0].v_pointer;
195 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
196 for (i = 0; i < src->len; i++) {
197 g_value_unset (&g_array_index (src, GValue, i));
199 g_array_free (src, TRUE);
204 gst_value_list_peek_pointer (const GValue * value)
206 return value->data[0].v_pointer;
210 gst_value_collect_list (GValue * value, guint n_collect_values,
211 GTypeCValue * collect_values, guint collect_flags)
213 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
214 value->data[0].v_pointer = collect_values[0].v_pointer;
215 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
217 value->data[0].v_pointer =
218 gst_value_list_array_copy ((GArray *) collect_values[0].v_pointer);
224 gst_value_lcopy_list (const GValue * value, guint n_collect_values,
225 GTypeCValue * collect_values, guint collect_flags)
227 GArray **dest = collect_values[0].v_pointer;
230 return g_strdup_printf ("value location for `%s' passed as NULL",
231 G_VALUE_TYPE_NAME (value));
232 if (!value->data[0].v_pointer)
233 return g_strdup_printf ("invalid value given for `%s'",
234 G_VALUE_TYPE_NAME (value));
235 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
236 *dest = (GArray *) value->data[0].v_pointer;
238 *dest = gst_value_list_array_copy ((GArray *) value->data[0].v_pointer);
244 * gst_value_list_prepend_value:
245 * @value: a GstValueList to prepend a value to
246 * @prepend_value: the value to prepend
248 * Prepends @prepend_value to the GstValueList in @value.
252 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
256 g_return_if_fail (GST_VALUE_HOLDS_LIST (value)
257 || GST_VALUE_HOLDS_ARRAY (value));
259 gst_value_init_and_copy (&val, prepend_value);
260 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
264 * gst_value_list_append_value:
265 * @value: a GstValueList to append a value to
266 * @append_value: the value to append
268 * Appends @append_value to the GstValueList in @value.
271 gst_value_list_append_value (GValue * value, const GValue * append_value)
275 g_return_if_fail (GST_VALUE_HOLDS_LIST (value)
276 || GST_VALUE_HOLDS_ARRAY (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_get_size:
284 * @value: a #GValue of type #GST_LIST_TYPE or #GST_ARRAY_TYPE
286 * Gets the number of values contained in @value.
288 * Returns: the number of values
291 gst_value_list_get_size (const GValue * value)
293 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value)
294 || GST_VALUE_HOLDS_ARRAY (value), 0);
296 return ((GArray *) value->data[0].v_pointer)->len;
300 * gst_value_list_get_value:
301 * @value: a #GValue of type #GST_LIST_TYPE or #GST_ARRAY_TYPE
302 * @index: index of value to get from the list
304 * Gets the value that is a member of the list contained in @value and
305 * has the index @index.
307 * Returns: the value at the given index
310 gst_value_list_get_value (const GValue * value, guint index)
312 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value)
313 || GST_VALUE_HOLDS_ARRAY (value), NULL);
314 g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
316 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
321 * gst_value_list_concat:
322 * @dest: an uninitialized #GValue to take the result
323 * @value1: first value to put into the union
324 * @value2: second value to put into the union
326 * Concatenates copies of value1 and value2 into a list. The value
327 * @dest is initialized to the type GST_TYPE_LIST.
330 gst_value_list_concat (GValue * dest, const GValue * value1,
331 const GValue * value2)
333 guint i, value1_length, value2_length;
336 g_return_if_fail (dest != NULL);
337 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
338 g_return_if_fail (G_IS_VALUE (value1));
339 g_return_if_fail (G_IS_VALUE (value2));
342 (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
344 (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
345 g_value_init (dest, GST_TYPE_LIST);
346 array = (GArray *) dest->data[0].v_pointer;
347 g_array_set_size (array, value1_length + value2_length);
349 if (GST_VALUE_HOLDS_LIST (value1)) {
350 for (i = 0; i < value1_length; i++) {
351 gst_value_init_and_copy (&g_array_index (array, GValue, i),
352 gst_value_list_get_value (value1, i));
355 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
358 if (GST_VALUE_HOLDS_LIST (value2)) {
359 for (i = 0; i < value2_length; i++) {
360 gst_value_init_and_copy (&g_array_index (array, GValue,
361 i + value1_length), gst_value_list_get_value (value2, i));
364 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
370 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
372 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
376 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
378 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
382 gst_value_compare_list (const GValue * value1, const GValue * value2)
385 GArray *array1 = value1->data[0].v_pointer;
386 GArray *array2 = value2->data[0].v_pointer;
390 if (array1->len != array2->len)
391 return GST_VALUE_UNORDERED;
393 for (i = 0; i < array1->len; i++) {
394 v1 = &g_array_index (array1, GValue, i);
395 for (j = 0; j < array1->len; j++) {
396 v2 = &g_array_index (array2, GValue, j);
397 if (gst_value_compare (v1, v2) == GST_VALUE_EQUAL)
400 if (j == array1->len) {
401 return GST_VALUE_UNORDERED;
405 return GST_VALUE_EQUAL;
409 gst_value_serialize_list (const GValue * value)
411 return gst_value_serialize_any_list (value, "{ ", " }");
415 gst_value_deserialize_list (GValue * dest, const char *s)
417 g_warning ("unimplemented");
422 gst_value_serialize_array (const GValue * value)
424 return gst_value_serialize_any_list (value, "< ", " >");
428 gst_value_deserialize_array (GValue * dest, const char *s)
430 g_warning ("unimplemented");
439 gst_value_init_fourcc (GValue * value)
441 value->data[0].v_int = 0;
445 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
447 dest_value->data[0].v_int = src_value->data[0].v_int;
451 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
452 GTypeCValue * collect_values, guint collect_flags)
454 value->data[0].v_int = collect_values[0].v_int;
460 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
461 GTypeCValue * collect_values, guint collect_flags)
463 guint32 *fourcc_p = collect_values[0].v_pointer;
466 return g_strdup_printf ("value location for `%s' passed as NULL",
467 G_VALUE_TYPE_NAME (value));
469 *fourcc_p = value->data[0].v_int;
475 * gst_value_set_fourcc:
476 * @value: a GValue initialized to #GST_TYPE_FOURCC
477 * @fourcc: the #guint32 fourcc to set
479 * Sets @value to @fourcc.
482 gst_value_set_fourcc (GValue * value, guint32 fourcc)
484 g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
486 value->data[0].v_int = fourcc;
490 * gst_value_get_fourcc:
491 * @value: a GValue initialized to #GST_TYPE_FOURCC
493 * Gets the #guint32 fourcc contained in @value.
495 * Returns: the #guint32 fourcc contained in @value.
498 gst_value_get_fourcc (const GValue * value)
500 g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
502 return value->data[0].v_int;
506 gst_value_transform_fourcc_string (const GValue * src_value,
509 guint32 fourcc = src_value->data[0].v_int;
511 if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
512 g_ascii_isprint ((fourcc >> 8) & 0xff) &&
513 g_ascii_isprint ((fourcc >> 16) & 0xff) &&
514 g_ascii_isprint ((fourcc >> 24) & 0xff)) {
515 dest_value->data[0].v_pointer =
516 g_strdup_printf (GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
518 dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
523 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
525 if (value2->data[0].v_int == value1->data[0].v_int)
526 return GST_VALUE_EQUAL;
527 return GST_VALUE_UNORDERED;
531 gst_value_serialize_fourcc (const GValue * value)
533 guint32 fourcc = value->data[0].v_int;
535 if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
536 g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
537 g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
538 g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
539 return g_strdup_printf (GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
541 return g_strdup_printf ("0x%08x", fourcc);
546 gst_value_deserialize_fourcc (GValue * dest, const char *s)
548 gboolean ret = FALSE;
552 if (strlen (s) == 4) {
553 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
555 } else if (g_ascii_isdigit (*s)) {
556 fourcc = strtoul (s, &end, 0);
561 gst_value_set_fourcc (dest, fourcc);
571 gst_value_init_int_range (GValue * value)
573 value->data[0].v_int = 0;
574 value->data[1].v_int = 0;
578 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
580 dest_value->data[0].v_int = src_value->data[0].v_int;
581 dest_value->data[1].v_int = src_value->data[1].v_int;
585 gst_value_collect_int_range (GValue * value, guint n_collect_values,
586 GTypeCValue * collect_values, guint collect_flags)
588 value->data[0].v_int = collect_values[0].v_int;
589 value->data[1].v_int = collect_values[1].v_int;
595 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
596 GTypeCValue * collect_values, guint collect_flags)
598 guint32 *int_range_start = collect_values[0].v_pointer;
599 guint32 *int_range_end = collect_values[1].v_pointer;
601 if (!int_range_start)
602 return g_strdup_printf ("start value location for `%s' passed as NULL",
603 G_VALUE_TYPE_NAME (value));
605 return g_strdup_printf ("end value location for `%s' passed as NULL",
606 G_VALUE_TYPE_NAME (value));
608 *int_range_start = value->data[0].v_int;
609 *int_range_end = value->data[1].v_int;
615 * gst_value_set_int_range:
616 * @value: a GValue initialized to GST_TYPE_INT_RANGE
617 * @start: the start of the range
618 * @end: the end of the range
620 * Sets @value to the range specified by @start and @end.
623 gst_value_set_int_range (GValue * value, int start, int end)
625 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
626 g_return_if_fail (start < end);
628 value->data[0].v_int = start;
629 value->data[1].v_int = end;
633 * gst_value_get_int_range_min:
634 * @value: a GValue initialized to GST_TYPE_INT_RANGE
636 * Gets the minimum of the range specified by @value.
638 * Returns: the minimum of the range
641 gst_value_get_int_range_min (const GValue * value)
643 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
645 return value->data[0].v_int;
649 * gst_value_get_int_range_max:
650 * @value: a GValue initialized to GST_TYPE_INT_RANGE
652 * Gets the maximum of the range specified by @value.
654 * Returns: the maxumum of the range
657 gst_value_get_int_range_max (const GValue * value)
659 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
661 return value->data[1].v_int;
665 gst_value_transform_int_range_string (const GValue * src_value,
668 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
669 (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
673 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
675 if (value2->data[0].v_int == value1->data[0].v_int &&
676 value2->data[1].v_int == value1->data[1].v_int)
677 return GST_VALUE_EQUAL;
678 return GST_VALUE_UNORDERED;
682 gst_value_serialize_int_range (const GValue * value)
684 return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
685 value->data[1].v_int);
689 gst_value_deserialize_int_range (GValue * dest, const char *s)
691 g_warning ("unimplemented");
700 gst_value_init_double_range (GValue * value)
702 value->data[0].v_double = 0;
703 value->data[1].v_double = 0;
707 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
709 dest_value->data[0].v_double = src_value->data[0].v_double;
710 dest_value->data[1].v_double = src_value->data[1].v_double;
714 gst_value_collect_double_range (GValue * value, guint n_collect_values,
715 GTypeCValue * collect_values, guint collect_flags)
717 value->data[0].v_double = collect_values[0].v_double;
718 value->data[1].v_double = collect_values[1].v_double;
724 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
725 GTypeCValue * collect_values, guint collect_flags)
727 gdouble *double_range_start = collect_values[0].v_pointer;
728 gdouble *double_range_end = collect_values[1].v_pointer;
730 if (!double_range_start)
731 return g_strdup_printf ("start value location for `%s' passed as NULL",
732 G_VALUE_TYPE_NAME (value));
733 if (!double_range_end)
734 return g_strdup_printf ("end value location for `%s' passed as NULL",
735 G_VALUE_TYPE_NAME (value));
737 *double_range_start = value->data[0].v_double;
738 *double_range_end = value->data[1].v_double;
744 * gst_value_set_double_range:
745 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
746 * @start: the start of the range
747 * @end: the end of the range
749 * Sets @value to the range specified by @start and @end.
752 gst_value_set_double_range (GValue * value, double start, double end)
754 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
756 value->data[0].v_double = start;
757 value->data[1].v_double = end;
761 * gst_value_get_double_range_min:
762 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
764 * Gets the minimum of the range specified by @value.
766 * Returns: the minumum of the range
769 gst_value_get_double_range_min (const GValue * value)
771 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
773 return value->data[0].v_double;
777 * gst_value_get_double_range_max:
778 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
780 * Gets the maximum of the range specified by @value.
782 * Returns: the maxumum of the range
785 gst_value_get_double_range_max (const GValue * value)
787 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
789 return value->data[1].v_double;
793 gst_value_transform_double_range_string (const GValue * src_value,
796 char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
798 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
799 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
800 src_value->data[0].v_double),
801 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
802 src_value->data[1].v_double));
806 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
808 if (value2->data[0].v_double == value1->data[0].v_double &&
809 value2->data[0].v_double == value1->data[0].v_double)
810 return GST_VALUE_EQUAL;
811 return GST_VALUE_UNORDERED;
815 gst_value_serialize_double_range (const GValue * value)
817 char d1[G_ASCII_DTOSTR_BUF_SIZE];
818 char d2[G_ASCII_DTOSTR_BUF_SIZE];
820 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
821 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
822 return g_strdup_printf ("[ %s, %s ]", d1, d2);
826 gst_value_deserialize_double_range (GValue * dest, const char *s)
828 g_warning ("unimplemented");
837 * gst_value_set_caps:
838 * @value: a GValue initialized to GST_TYPE_CAPS
839 * @caps: the caps to set the value to
841 * Sets the contents of @value to coorespond to @caps. The actual
842 * #GstCaps structure is copied before it is used.
845 gst_value_set_caps (GValue * value, const GstCaps * caps)
847 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
849 g_value_set_boxed (value, caps);
853 * gst_value_get_caps:
854 * @value: a GValue initialized to GST_TYPE_CAPS
856 * Gets the contents of @value.
858 * Returns: the contents of @value
861 gst_value_get_caps (const GValue * value)
863 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
865 return (GstCaps *) g_value_get_boxed (value);
869 gst_value_serialize_caps (const GValue * value)
871 GstCaps *caps = g_value_get_boxed (value);
873 return gst_caps_to_string (caps);
877 gst_value_deserialize_caps (GValue * dest, const char *s)
881 caps = gst_caps_from_string (s);
884 g_value_set_boxed (dest, caps);
896 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
898 GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
899 GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
901 if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
902 return GST_VALUE_UNORDERED;
903 if (GST_BUFFER_SIZE (buf1) == 0)
904 return GST_VALUE_EQUAL;
905 g_assert (GST_BUFFER_DATA (buf1));
906 g_assert (GST_BUFFER_DATA (buf2));
907 if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
908 GST_BUFFER_SIZE (buf1)) == 0)
909 return GST_VALUE_EQUAL;
911 return GST_VALUE_UNORDERED;
915 gst_value_serialize_buffer (const GValue * value)
921 GstBuffer *buffer = GST_BUFFER (gst_value_get_mini_object (value));
923 data = GST_BUFFER_DATA (buffer);
924 size = GST_BUFFER_SIZE (buffer);
926 string = g_malloc (size * 2 + 1);
927 for (i = 0; i < size; i++) {
928 sprintf (string + i * 2, "%02x", data[i]);
930 string[size * 2] = 0;
936 gst_value_deserialize_buffer (GValue * dest, const char *s)
948 buffer = gst_buffer_new_and_alloc (len / 2);
949 data = GST_BUFFER_DATA (buffer);
950 for (i = 0; i < len / 2; i++) {
951 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1])) {
955 ts[0] = s[i * 2 + 0];
956 ts[1] = s[i * 2 + 1];
959 data[i] = strtoul (ts, NULL, 16);
963 gst_value_take_mini_object (dest, GST_MINI_OBJECT (buffer));
966 gst_buffer_unref (buffer);
977 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
979 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
980 return GST_VALUE_EQUAL;
981 return GST_VALUE_UNORDERED;
985 gst_value_serialize_boolean (const GValue * value)
987 if (value->data[0].v_int) {
988 return g_strdup ("true");
990 return g_strdup ("false");
994 gst_value_deserialize_boolean (GValue * dest, const char *s)
996 gboolean ret = FALSE;
998 if (g_ascii_strcasecmp (s, "true") == 0 ||
999 g_ascii_strcasecmp (s, "yes") == 0 ||
1000 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1001 g_value_set_boolean (dest, TRUE);
1003 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1004 g_ascii_strcasecmp (s, "no") == 0 ||
1005 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1006 g_value_set_boolean (dest, FALSE);
1013 #define CREATE_SERIALIZATION_START(_type,_macro) \
1015 gst_value_compare_ ## _type \
1016 (const GValue * value1, const GValue * value2) \
1018 g ## _type val1 = g_value_get_ ## _type (value1); \
1019 g ## _type val2 = g_value_get_ ## _type (value2); \
1021 return GST_VALUE_GREATER_THAN; \
1023 return GST_VALUE_LESS_THAN; \
1024 return GST_VALUE_EQUAL; \
1028 gst_value_serialize_ ## _type (const GValue * value) \
1030 GValue val = { 0, }; \
1031 g_value_init (&val, G_TYPE_STRING); \
1032 if (!g_value_transform (value, &val)) \
1033 g_assert_not_reached (); \
1034 /* NO_COPY_MADNESS!!! */ \
1035 return (char *) g_value_get_string (&val); \
1038 /* deserialize the given s into to as a gint64.
1039 * check if the result is actually storeable in the given size number of
1043 gst_value_deserialize_int_helper (gint64 * to, const char *s,
1044 gint64 min, gint64 max, int size)
1046 gboolean ret = FALSE;
1051 *to = g_ascii_strtoull (s, &end, 0);
1052 /* a range error is a definitive no-no */
1053 if (errno == ERANGE) {
1060 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1061 *to = G_LITTLE_ENDIAN;
1063 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1066 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1069 } else if (g_ascii_strcasecmp (s, "min") == 0) {
1072 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1078 /* by definition, a gint64 fits into a gint64; so ignore those */
1079 if (size != sizeof (mask)) {
1081 /* for positive numbers, we create a mask of 1's outside of the range
1082 * and 0's inside the range. An and will thus keep only 1 bits
1083 * outside of the range */
1084 mask <<= (size * 8);
1085 if ((mask & *to) != 0) {
1089 /* for negative numbers, we do a 2's complement version */
1090 mask <<= ((size * 8) - 1);
1091 if ((mask & *to) != mask) {
1100 #define CREATE_SERIALIZATION(_type,_macro) \
1101 CREATE_SERIALIZATION_START(_type,_macro) \
1104 gst_value_deserialize_ ## _type (GValue * dest, const char *s) \
1108 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
1109 G_MAX ## _macro, sizeof (g ## _type))) { \
1110 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
1117 #define CREATE_USERIALIZATION(_type,_macro) \
1118 CREATE_SERIALIZATION_START(_type,_macro) \
1121 gst_value_deserialize_ ## _type (GValue * dest, const char *s) \
1125 gboolean ret = FALSE; \
1128 x = g_ascii_strtoull (s, &end, 0); \
1129 /* a range error is a definitive no-no */ \
1130 if (errno == ERANGE) { \
1133 /* the cast ensures the range check later on makes sense */ \
1134 x = (g ## _type) x; \
1138 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
1139 x = G_LITTLE_ENDIAN; \
1141 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
1144 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
1147 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
1150 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
1151 x = G_MAX ## _macro; \
1156 if (x > G_MAX ## _macro) { \
1159 g_value_set_ ## _type (dest, x); \
1165 #define REGISTER_SERIALIZATION(_gtype, _type) \
1167 static const GstValueTable gst_value = { \
1169 gst_value_compare_ ## _type, \
1170 gst_value_serialize_ ## _type, \
1171 gst_value_deserialize_ ## _type, \
1174 gst_value_register (&gst_value); \
1177 CREATE_SERIALIZATION (int, INT);
1178 CREATE_SERIALIZATION (int64, INT64);
1179 CREATE_SERIALIZATION (long, LONG);
1181 CREATE_USERIALIZATION (uint, UINT);
1182 CREATE_USERIALIZATION (uint64, UINT64);
1183 CREATE_USERIALIZATION (ulong, ULONG);
1189 gst_value_compare_double (const GValue * value1, const GValue * value2)
1191 if (value1->data[0].v_double > value2->data[0].v_double)
1192 return GST_VALUE_GREATER_THAN;
1193 if (value1->data[0].v_double < value2->data[0].v_double)
1194 return GST_VALUE_LESS_THAN;
1195 if (value1->data[0].v_double == value2->data[0].v_double)
1196 return GST_VALUE_EQUAL;
1197 return GST_VALUE_UNORDERED;
1201 gst_value_serialize_double (const GValue * value)
1203 char d[G_ASCII_DTOSTR_BUF_SIZE];
1205 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1206 return g_strdup (d);
1210 gst_value_deserialize_double (GValue * dest, const char *s)
1213 gboolean ret = FALSE;
1216 x = g_ascii_strtod (s, &end);
1220 if (g_ascii_strcasecmp (s, "min") == 0) {
1223 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1229 g_value_set_double (dest, x);
1239 gst_value_compare_float (const GValue * value1, const GValue * value2)
1241 if (value1->data[0].v_float > value2->data[0].v_float)
1242 return GST_VALUE_GREATER_THAN;
1243 if (value1->data[0].v_float < value2->data[0].v_float)
1244 return GST_VALUE_LESS_THAN;
1245 if (value1->data[0].v_float == value2->data[0].v_float)
1246 return GST_VALUE_EQUAL;
1247 return GST_VALUE_UNORDERED;
1251 gst_value_serialize_float (const GValue * value)
1253 char d[G_ASCII_DTOSTR_BUF_SIZE];
1255 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1256 return g_strdup (d);
1260 gst_value_deserialize_float (GValue * dest, const char *s)
1263 gboolean ret = FALSE;
1266 x = g_ascii_strtod (s, &end);
1270 if (g_ascii_strcasecmp (s, "min") == 0) {
1273 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1278 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1281 g_value_set_float (dest, x);
1291 gst_value_compare_string (const GValue * value1, const GValue * value2)
1293 int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1296 return GST_VALUE_LESS_THAN;
1298 return GST_VALUE_GREATER_THAN;
1299 return GST_VALUE_EQUAL;
1302 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1303 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1307 gst_string_wrap (const char *s)
1312 gboolean wrap = FALSE;
1317 return g_strdup ("");
1319 if (GST_ASCII_IS_STRING (*t)) {
1321 } else if (*t < 0x20 || *t >= 0x7f) {
1332 return g_strdup (s);
1334 e = d = g_malloc (len + 3);
1339 if (GST_ASCII_IS_STRING (*t)) {
1341 } else if (*t < 0x20 || *t >= 0x7f) {
1343 *e++ = '0' + ((*(guchar *) t) >> 6);
1344 *e++ = '0' + (((*t) >> 3) & 0x7);
1345 *e++ = '0' + ((*t++) & 0x7);
1358 * This function takes a string delimited with double quotes (")
1359 * and unescapes any \xxx octal numbers.
1361 * If sequences of \y are found where y is not in the range of
1362 * 0->3, y is copied unescaped.
1364 * If \xyy is found where x is an octal number but y is not, an
1365 * error is encountered and NULL is returned.
1367 * the input string must be \0 terminated.
1370 gst_string_unwrap (const gchar * s)
1373 gchar *read, *write;
1375 /* NULL string returns NULL */
1379 /* strings not starting with " are invalid */
1383 /* make copy of original string to hold the result. This
1384 * string will always be smaller than the original */
1389 /* need to move to the next position as we parsed the " */
1393 if (GST_ASCII_IS_STRING (*read)) {
1394 /* normal chars are just copied */
1396 } else if (*read == '"') {
1397 /* quote marks end of string */
1399 } else if (*read == '\\') {
1400 /* got an escape char, move to next position to read a tripplet
1401 * of octal numbers */
1403 /* is the next char a possible first octal number? */
1404 if (*read >= '0' && *read <= '3') {
1405 /* parse other 2 numbers, if one of them is not in the range of
1406 * an octal number, we error. We also catch the case where a zero
1407 * byte is found here. */
1408 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1411 /* now convert the octal number to a byte again. */
1412 *write++ = ((read[0] - '0') << 6) +
1413 ((read[1] - '0') << 3) + (read[2] - '0');
1417 /* if we run into a \0 here, we definately won't get a quote later */
1421 /* else copy \X sequence */
1425 /* weird character, error */
1429 /* if the string is not ending in " and zero terminated, we error */
1430 if (*read != '"' || read[1] != '\0')
1433 /* null terminate result string and return */
1443 gst_value_serialize_string (const GValue * value)
1445 return gst_string_wrap (value->data[0].v_pointer);
1449 gst_value_deserialize_string (GValue * dest, const char *s)
1452 if (!g_utf8_validate (s, -1, NULL))
1454 g_value_set_string (dest, s);
1457 gchar *str = gst_string_unwrap (s);
1461 g_value_take_string (dest, str);
1472 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1474 GEnumValue *en1, *en2;
1475 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1476 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1478 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1479 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1480 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1481 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
1482 g_type_class_unref (klass1);
1483 g_type_class_unref (klass2);
1484 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
1485 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
1486 if (en1->value < en2->value)
1487 return GST_VALUE_LESS_THAN;
1488 if (en1->value > en2->value)
1489 return GST_VALUE_GREATER_THAN;
1491 return GST_VALUE_EQUAL;
1495 gst_value_serialize_enum (const GValue * value)
1498 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
1500 g_return_val_if_fail (klass, NULL);
1501 en = g_enum_get_value (klass, g_value_get_enum (value));
1502 g_type_class_unref (klass);
1503 g_return_val_if_fail (en, NULL);
1504 return g_strdup (en->value_name);
1508 gst_value_deserialize_enum (GValue * dest, const char *s)
1511 gchar *endptr = NULL;
1512 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1514 g_return_val_if_fail (klass, FALSE);
1515 if (!(en = g_enum_get_value_by_name (klass, s))) {
1516 if (!(en = g_enum_get_value_by_nick (klass, s))) {
1517 gint i = strtol (s, &endptr, 0);
1519 if (endptr && *endptr == '\0') {
1520 en = g_enum_get_value (klass, i);
1524 g_type_class_unref (klass);
1525 g_return_val_if_fail (en, FALSE);
1526 g_value_set_enum (dest, en->value);
1534 /* we just compare the value here */
1536 gst_value_compare_flags (const GValue * value1, const GValue * value2)
1539 GFlagsClass *klass1 =
1540 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1541 GFlagsClass *klass2 =
1542 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1544 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1545 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1546 fl1 = g_value_get_flags (value1);
1547 fl2 = g_value_get_flags (value2);
1548 g_type_class_unref (klass1);
1549 g_type_class_unref (klass2);
1551 return GST_VALUE_LESS_THAN;
1553 return GST_VALUE_GREATER_THAN;
1555 return GST_VALUE_EQUAL;
1558 /* the different flags are serialized separated with a + */
1560 gst_value_serialize_flags (const GValue * value)
1564 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
1565 gchar *result, *tmp;
1566 gboolean first = TRUE;
1568 g_return_val_if_fail (klass, NULL);
1570 result = g_strdup ("");
1571 flags = g_value_get_flags (value);
1573 fl = g_flags_get_first_value (klass, flags);
1575 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
1581 flags &= ~fl->value;
1583 g_type_class_unref (klass);
1589 gst_value_deserialize_flags (GValue * dest, const char *s)
1592 gchar *endptr = NULL;
1593 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1598 g_return_val_if_fail (klass, FALSE);
1600 /* split into parts delimited with + */
1601 split = g_strsplit (s, "+", 0);
1605 /* loop over each part */
1607 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
1608 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
1609 gint val = strtol (split[i], &endptr, 0);
1611 /* just or numeric value */
1612 if (endptr && *endptr == '\0') {
1623 g_type_class_unref (klass);
1624 g_value_set_flags (dest, flags);
1634 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
1635 const GValue * src2)
1637 if (src2->data[0].v_int <= src1->data[0].v_int &&
1638 src2->data[1].v_int >= src1->data[0].v_int) {
1639 gst_value_init_and_copy (dest, src2);
1646 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
1647 const GValue * src2)
1652 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
1653 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
1656 g_value_init (dest, GST_TYPE_INT_RANGE);
1657 gst_value_set_int_range (dest,
1658 MIN (src1->data[0].v_int, src2->data[0].v_int),
1659 MAX (src1->data[1].v_int, src2->data[1].v_int));
1671 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
1672 const GValue * src2)
1674 if (src2->data[0].v_int <= src1->data[0].v_int &&
1675 src2->data[1].v_int >= src1->data[0].v_int) {
1676 gst_value_init_and_copy (dest, src1);
1684 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
1685 const GValue * src2)
1690 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
1691 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
1694 g_value_init (dest, GST_TYPE_INT_RANGE);
1695 gst_value_set_int_range (dest, min, max);
1699 g_value_init (dest, G_TYPE_INT);
1700 g_value_set_int (dest, min);
1708 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
1709 const GValue * src2)
1711 if (src2->data[0].v_double <= src1->data[0].v_double &&
1712 src2->data[1].v_double >= src1->data[0].v_double) {
1713 gst_value_init_and_copy (dest, src1);
1721 gst_value_intersect_double_range_double_range (GValue * dest,
1722 const GValue * src1, const GValue * src2)
1727 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
1728 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
1731 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
1732 gst_value_set_double_range (dest, min, max);
1736 g_value_init (dest, G_TYPE_DOUBLE);
1737 g_value_set_int (dest, min);
1745 gst_value_intersect_list (GValue * dest, const GValue * value1,
1746 const GValue * value2)
1749 GValue intersection = { 0, };
1750 gboolean ret = FALSE;
1752 size = gst_value_list_get_size (value1);
1753 for (i = 0; i < size; i++) {
1754 const GValue *cur = gst_value_list_get_value (value1, i);
1756 if (gst_value_intersect (&intersection, cur, value2)) {
1759 gst_value_init_and_copy (dest, &intersection);
1761 } else if (GST_VALUE_HOLDS_LIST (dest)) {
1762 gst_value_list_append_value (dest, &intersection);
1764 GValue temp = { 0, };
1766 gst_value_init_and_copy (&temp, dest);
1767 g_value_unset (dest);
1768 gst_value_list_concat (dest, &temp, &intersection);
1769 g_value_unset (&temp);
1771 g_value_unset (&intersection);
1779 gst_value_intersect_array (GValue * dest, const GValue * src1,
1780 const GValue * src2)
1785 /* only works on similar-sized arrays */
1786 size = gst_value_list_get_size (src1);
1787 if (size != gst_value_list_get_size (src2))
1789 g_value_init (dest, GST_TYPE_ARRAY);
1791 for (n = 0; n < size; n++) {
1792 if (!gst_value_intersect (&val, gst_value_list_get_value (src1, n),
1793 gst_value_list_get_value (src2, n))) {
1794 g_value_unset (dest);
1797 gst_value_list_append_value (dest, &val);
1798 g_value_unset (&val);
1809 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
1810 const GValue * subtrahend)
1812 int min = gst_value_get_int_range_min (subtrahend);
1813 int max = gst_value_get_int_range_max (subtrahend);
1814 int val = g_value_get_int (minuend);
1816 /* subtracting a range from an int only works if the int is not in the
1818 if (val < min || val > max) {
1819 /* and the result is the int */
1820 gst_value_init_and_copy (dest, minuend);
1826 /* creates a new int range based on input values.
1829 gst_value_create_new_range (GValue * dest, int min1, int max1, int min2,
1834 GValue *pv1, *pv2; /* yeah, hungarian! */
1836 if (min1 <= max1 && min2 <= max2) {
1839 } else if (min1 <= max1) {
1842 } else if (min2 <= max2) {
1850 g_value_init (pv1, GST_TYPE_INT_RANGE);
1851 gst_value_set_int_range (pv1, min1, max1);
1852 } else if (min1 == max1) {
1853 g_value_init (pv1, G_TYPE_INT);
1854 g_value_set_int (pv1, min1);
1857 g_value_init (pv2, GST_TYPE_INT_RANGE);
1858 gst_value_set_int_range (pv2, min2, max2);
1859 } else if (min2 == max2) {
1860 g_value_init (pv2, G_TYPE_INT);
1861 g_value_set_int (pv2, min2);
1864 if (min1 <= max1 && min2 <= max2) {
1865 gst_value_list_concat (dest, pv1, pv2);
1866 g_value_unset (pv1);
1867 g_value_unset (pv2);
1873 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
1874 const GValue * subtrahend)
1876 int min = gst_value_get_int_range_min (minuend);
1877 int max = gst_value_get_int_range_max (minuend);
1878 int val = g_value_get_int (subtrahend);
1880 g_return_val_if_fail (min < max, FALSE);
1882 /* value is outside of the range, return range unchanged */
1883 if (val < min || val > max) {
1884 gst_value_init_and_copy (dest, minuend);
1887 /* max must be MAXINT too as val <= max */
1888 if (val == G_MAXINT) {
1892 /* min must be MININT too as val >= max */
1893 if (val == G_MININT) {
1897 gst_value_create_new_range (dest, min, val - 1, val + 1, max);
1903 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
1904 const GValue * subtrahend)
1906 int min1 = gst_value_get_int_range_min (minuend);
1907 int max1 = gst_value_get_int_range_max (minuend);
1908 int min2 = gst_value_get_int_range_min (subtrahend);
1909 int max2 = gst_value_get_int_range_max (subtrahend);
1911 if (max2 == G_MAXINT && min2 == G_MININT) {
1913 } else if (max2 == G_MAXINT) {
1914 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
1915 } else if (min2 == G_MININT) {
1916 return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
1918 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
1919 MAX (max2 + 1, min1), max1);
1924 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
1925 const GValue * subtrahend)
1927 double min = gst_value_get_double_range_min (subtrahend);
1928 double max = gst_value_get_double_range_max (subtrahend);
1929 double val = g_value_get_double (minuend);
1931 if (val < min || val > max) {
1932 gst_value_init_and_copy (dest, minuend);
1939 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
1940 const GValue * subtrahend)
1942 /* since we don't have open ranges, we cannot create a hole in
1943 * a double range. We return the original range */
1944 gst_value_init_and_copy (dest, minuend);
1949 gst_value_subtract_double_range_double_range (GValue * dest,
1950 const GValue * minuend, const GValue * subtrahend)
1952 /* since we don't have open ranges, we have to approximate */
1953 /* done like with ints */
1954 double min1 = gst_value_get_double_range_min (minuend);
1955 double max2 = gst_value_get_double_range_max (minuend);
1956 double max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
1957 double min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
1960 GValue *pv1, *pv2; /* yeah, hungarian! */
1962 if (min1 < max1 && min2 < max2) {
1965 } else if (min1 < max1) {
1968 } else if (min2 < max2) {
1976 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
1977 gst_value_set_double_range (pv1, min1, max1);
1980 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
1981 gst_value_set_double_range (pv2, min2, max2);
1984 if (min1 < max1 && min2 < max2) {
1985 gst_value_list_concat (dest, pv1, pv2);
1986 g_value_unset (pv1);
1987 g_value_unset (pv2);
1993 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
1994 const GValue * subtrahend)
1997 GValue subtraction = { 0, };
1998 gboolean ret = FALSE;
2000 size = gst_value_list_get_size (minuend);
2001 for (i = 0; i < size; i++) {
2002 const GValue *cur = gst_value_list_get_value (minuend, i);
2004 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2006 gst_value_init_and_copy (dest, &subtraction);
2008 } else if (GST_VALUE_HOLDS_LIST (dest)
2009 && GST_VALUE_HOLDS_LIST (&subtraction)) {
2011 GValue unroll = { 0, };
2013 gst_value_init_and_copy (&unroll, dest);
2014 g_value_unset (dest);
2015 gst_value_list_concat (dest, &unroll, &subtraction);
2016 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2017 gst_value_list_append_value (dest, &subtraction);
2019 GValue temp = { 0, };
2021 gst_value_init_and_copy (&temp, dest);
2022 g_value_unset (dest);
2023 gst_value_list_concat (dest, &temp, &subtraction);
2024 g_value_unset (&temp);
2026 g_value_unset (&subtraction);
2033 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2034 const GValue * subtrahend)
2037 GValue data[2] = { {0,}, {0,} };
2038 GValue *subtraction = &data[0], *result = &data[1];
2040 gst_value_init_and_copy (result, minuend);
2041 size = gst_value_list_get_size (subtrahend);
2042 for (i = 0; i < size; i++) {
2043 const GValue *cur = gst_value_list_get_value (subtrahend, i);
2045 if (gst_value_subtract (subtraction, result, cur)) {
2046 GValue *temp = result;
2048 result = subtraction;
2050 g_value_unset (subtraction);
2052 g_value_unset (result);
2056 gst_value_init_and_copy (dest, result);
2057 g_value_unset (result);
2067 * gst_value_can_compare:
2068 * @value1: a value to compare
2069 * @value2: another value to compare
2071 * Determines if @value1 and @value2 can be compared.
2073 * Returns: TRUE if the values can be compared
2076 gst_value_can_compare (const GValue * value1, const GValue * value2)
2078 GstValueTable *table;
2081 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2084 for (i = 0; i < gst_value_table->len; i++) {
2085 table = &g_array_index (gst_value_table, GstValueTable, i);
2086 if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2094 * gst_value_compare:
2095 * @value1: a value to compare
2096 * @value2: another value to compare
2098 * Compares @value1 and @value2. If @value1 and @value2 cannot be
2099 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
2100 * if @value1 is greater than @value2, GST_VALUE_GREATER is returned.
2101 * If @value1 is less than @value2, GST_VALUE_LESSER is returned.
2102 * If the values are equal, GST_VALUE_EQUAL is returned.
2104 * Returns: A GstValueCompareType value
2107 gst_value_compare (const GValue * value1, const GValue * value2)
2109 GstValueTable *table, *best = NULL;
2112 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2113 return GST_VALUE_UNORDERED;
2115 for (i = 0; i < gst_value_table->len; i++) {
2116 table = &g_array_index (gst_value_table, GstValueTable, i);
2117 if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2121 if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2122 if (!best || g_type_is_a (table->type, best->type))
2127 return best->compare (value1, value2);
2130 g_critical ("unable to compare values of type %s\n",
2131 g_type_name (G_VALUE_TYPE (value1)));
2132 return GST_VALUE_UNORDERED;
2138 * gst_value_can_union:
2139 * @value1: a value to union
2140 * @value2: another value to union
2142 * Determines if @value1 and @value2 can be non-trivially unioned.
2143 * Any two values can be trivially unioned by adding both of them
2144 * to a GstValueList. However, certain types have the possibility
2145 * to be unioned in a simpler way. For example, an integer range
2146 * and an integer can be unioned if the integer is a subset of the
2147 * integer range. If there is the possibility that two values can
2148 * be unioned, this function returns TRUE.
2150 * Returns: TRUE if there is a function allowing the two values to
2154 gst_value_can_union (const GValue * value1, const GValue * value2)
2156 GstValueUnionInfo *union_info;
2159 for (i = 0; i < gst_value_union_funcs->len; i++) {
2160 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2161 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2162 union_info->type2 == G_VALUE_TYPE (value2))
2164 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2165 union_info->type2 == G_VALUE_TYPE (value1))
2174 * @dest: the destination value
2175 * @value1: a value to union
2176 * @value2: another value to union
2178 * Creates a GValue cooresponding to the union of @value1 and @value2.
2180 * Returns: TRUE if the values could be unioned
2183 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2185 GstValueUnionInfo *union_info;
2188 for (i = 0; i < gst_value_union_funcs->len; i++) {
2189 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2190 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2191 union_info->type2 == G_VALUE_TYPE (value2)) {
2192 if (union_info->func (dest, value1, value2)) {
2196 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2197 union_info->type2 == G_VALUE_TYPE (value1)) {
2198 if (union_info->func (dest, value2, value1)) {
2204 gst_value_list_concat (dest, value1, value2);
2209 * gst_value_register_union_func:
2210 * @type1: a type to union
2211 * @type2: another type to union
2212 * @func: a function that implments creating a union between the two types
2214 * Registers a union function that can create a union between GValues
2215 * of the type @type1 and @type2.
2219 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
2221 GstValueUnionInfo union_info;
2223 union_info.type1 = type1;
2224 union_info.type2 = type2;
2225 union_info.func = func;
2227 g_array_append_val (gst_value_union_funcs, union_info);
2233 * gst_value_can_intersect:
2234 * @value1: a value to intersect
2235 * @value2: another value to intersect
2237 * Determines if intersecting two values will produce a valid result.
2238 * Two values will produce a valid intersection if they have the same
2239 * type, or if there is a method (registered by
2240 * #gst_value_register_intersection_func) to calculate the intersection.
2242 * Returns: TRUE if the values can intersect
2245 gst_value_can_intersect (const GValue * value1, const GValue * value2)
2247 GstValueIntersectInfo *intersect_info;
2251 if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
2254 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2255 intersect_info = &g_array_index (gst_value_intersect_funcs,
2256 GstValueIntersectInfo, i);
2257 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2258 intersect_info->type2 == G_VALUE_TYPE (value2))
2259 if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
2260 intersect_info->type1 == G_VALUE_TYPE (value2))
2264 return gst_value_can_compare (value1, value2);
2268 * gst_value_intersect:
2269 * @dest: a uninitialized #GValue that will hold the calculated
2270 * intersection value
2271 * @value1: a value to intersect
2272 * @value2: another value to intersect
2274 * Calculates the intersection of two values. If the values have
2275 * a non-empty intersection, the value representing the intersection
2276 * is placed in @dest. If the intersection is non-empty, @dest is
2279 * Returns: TRUE if the intersection is non-empty
2282 gst_value_intersect (GValue * dest, const GValue * value1,
2283 const GValue * value2)
2285 GstValueIntersectInfo *intersect_info;
2289 /* special cases first */
2290 if (GST_VALUE_HOLDS_LIST (value1))
2291 return gst_value_intersect_list (dest, value1, value2);
2292 if (GST_VALUE_HOLDS_LIST (value2))
2293 return gst_value_intersect_list (dest, value2, value1);
2295 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2296 intersect_info = &g_array_index (gst_value_intersect_funcs,
2297 GstValueIntersectInfo, i);
2298 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2299 intersect_info->type2 == G_VALUE_TYPE (value2)) {
2300 ret = intersect_info->func (dest, value1, value2);
2303 if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
2304 intersect_info->type2 == G_VALUE_TYPE (value1)) {
2305 ret = intersect_info->func (dest, value2, value1);
2310 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
2311 gst_value_init_and_copy (dest, value1);
2319 * gst_value_register_intersection_func:
2320 * @type1: the first type to intersect
2321 * @type2: the second type to intersect
2322 * @func: the intersection function
2324 * Registers a function that is called to calculate the intersection
2325 * of the values having the types @type1 and @type2.
2328 * GstValueIntersectFunc:
2329 * @dest: a uninitialized #GValue that will hold the calculated
2330 * intersection value
2331 * @value1: a value to intersect
2332 * @value2: another value to intersect
2334 * Functions having this type calculate the intersection of @value1
2335 * and @value2. If the intersection is non-empty, the result is
2336 * placed in @dest and TRUE is returned. If the intersection is
2337 * empty, @dest is unmodified and FALSE is returned.
2339 * Returns: TRUE if the intersection is non-empty, FALSE otherwise
2342 gst_value_register_intersect_func (GType type1, GType type2,
2343 GstValueIntersectFunc func)
2345 GstValueIntersectInfo intersect_info;
2347 intersect_info.type1 = type1;
2348 intersect_info.type2 = type2;
2349 intersect_info.func = func;
2351 g_array_append_val (gst_value_intersect_funcs, intersect_info);
2358 * gst_value_subtract:
2359 * @dest: the destination value for the result if the subtraction is not empty
2360 * @minuend: the value to subtract from
2361 * @subtrahend: the value to subtract
2363 * Subtracts @subtrahend from @minuend and stores the result in @dest.
2364 * Note that this means subtraction as in sets, not as in mathematics.
2366 * Returns: TRUE if the subtraction is not empty
2369 gst_value_subtract (GValue * dest, const GValue * minuend,
2370 const GValue * subtrahend)
2372 GstValueSubtractInfo *info;
2375 /* special cases first */
2376 if (GST_VALUE_HOLDS_LIST (minuend))
2377 return gst_value_subtract_from_list (dest, minuend, subtrahend);
2378 if (GST_VALUE_HOLDS_LIST (subtrahend))
2379 return gst_value_subtract_list (dest, minuend, subtrahend);
2381 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
2382 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
2383 if (info->minuend == G_VALUE_TYPE (minuend) &&
2384 info->subtrahend == G_VALUE_TYPE (subtrahend)) {
2385 return info->func (dest, minuend, subtrahend);
2389 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
2390 gst_value_init_and_copy (dest, minuend);
2399 gst_value_subtract (GValue * dest, const GValue * minuend,
2400 const GValue * subtrahend)
2402 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
2404 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
2405 gst_value_serialize (subtrahend),
2406 ret ? gst_value_serialize (dest) : "---");
2412 * gst_value_can_subtract:
2413 * @minuend: the value to subtract from
2414 * @subtrahend: the value to subtract
2416 * Checks if it's possible to subtract @subtrahend from @minuend.
2418 * Returns: TRUE if a subtraction is possible
2421 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
2423 GstValueSubtractInfo *info;
2427 if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
2430 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
2431 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
2432 if (info->minuend == G_VALUE_TYPE (minuend) &&
2433 info->subtrahend == G_VALUE_TYPE (subtrahend))
2437 return gst_value_can_compare (minuend, subtrahend);
2441 * gst_value_register_subtract_func:
2442 * @minuend_type: type of the minuend
2443 * @subtrahend_type: type of the subtrahend
2444 * @func: function to use
2446 * Registers @func as a function capable of subtracting the values of
2447 * @subtrahend_type from values of @minuend_type.
2450 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
2451 GstValueSubtractFunc func)
2453 GstValueSubtractInfo info;
2455 /* one type must be unfixed, other subtractions can be done as comparisons */
2456 g_return_if_fail (!gst_type_is_fixed (minuend_type)
2457 || !gst_type_is_fixed (subtrahend_type));
2459 info.minuend = minuend_type;
2460 info.subtrahend = subtrahend_type;
2463 g_array_append_val (gst_value_subtract_funcs, info);
2467 * gst_value_register:
2468 * @table: structure containing functions to register
2470 * Registers functions to perform calculations on #GValues of a given
2475 * @type: GType that the functions operate on.
2476 * @compare: A function that compares two values of this type.
2477 * @serialize: A function that transforms a value of this type to a
2478 * string. Strings created by this function must be unique and should
2479 * be human readable.
2480 * @deserialize: A function that transforms a string to a value of
2481 * this type. This function must transform strings created by the
2482 * serialize function back to the original value. This function may
2483 * optionally transform other strings into values.
2486 gst_value_register (const GstValueTable * table)
2488 g_array_append_val (gst_value_table, *table);
2492 * gst_value_init_and_copy:
2493 * @dest: the target value
2494 * @src: the source value
2496 * Initialises the target value to be of the same type as source and then copies
2497 * the contents from source to target.
2500 gst_value_init_and_copy (GValue * dest, const GValue * src)
2502 g_value_init (dest, G_VALUE_TYPE (src));
2503 g_value_copy (src, dest);
2507 * gst_value_serialize:
2508 * @value: a #GValue to serialize
2510 * tries to transform the given @value into a string representation that allows
2511 * getting back this string later on using gst_value_deserialize().
2513 * Returns: the serialization for @value or NULL if none exists
2516 gst_value_serialize (const GValue * value)
2519 GValue s_val = { 0 };
2520 GstValueTable *table, *best = NULL;
2523 g_return_val_if_fail (G_IS_VALUE (value), NULL);
2525 for (i = 0; i < gst_value_table->len; i++) {
2526 table = &g_array_index (gst_value_table, GstValueTable, i);
2527 if (table->serialize == NULL)
2529 if (table->type == G_VALUE_TYPE (value)) {
2533 if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
2534 if (!best || g_type_is_a (table->type, best->type))
2539 return best->serialize (value);
2541 g_value_init (&s_val, G_TYPE_STRING);
2542 if (g_value_transform (value, &s_val)) {
2543 s = gst_string_wrap (g_value_get_string (&s_val));
2547 g_value_unset (&s_val);
2553 * gst_value_deserialize:
2554 * @dest: #GValue to fill with contents of deserialization
2555 * @src: string to deserialize
2557 * Tries to deserialize a string into the type specified by the given GValue.
2558 * If the operation succeeds, TRUE is returned, FALSE otherwise.
2560 * Returns: TRUE on success
2563 gst_value_deserialize (GValue * dest, const gchar * src)
2565 GstValueTable *table, *best = NULL;
2568 g_return_val_if_fail (src != NULL, FALSE);
2569 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
2571 for (i = 0; i < gst_value_table->len; i++) {
2572 table = &g_array_index (gst_value_table, GstValueTable, i);
2573 if (table->serialize == NULL)
2576 if (table->type == G_VALUE_TYPE (dest)) {
2581 if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
2582 if (!best || g_type_is_a (table->type, best->type))
2587 return best->deserialize (dest, src);
2594 * gst_value_is_fixed:
2595 * @value: the #GValue to check
2597 * Tests if the given GValue, if available in a GstStructure (or any other
2598 * container) contains a "fixed" (which means: one value) or an "unfixed"
2599 * (which means: multiple possible values, such as data lists or data
2602 * Returns: true if the value is "fixed".
2606 gst_value_is_fixed (const GValue * value)
2608 GType type = G_VALUE_TYPE (value);
2610 if (type == GST_TYPE_ARRAY) {
2611 gboolean fixed = TRUE;
2615 /* check recursively */
2616 size = gst_value_list_get_size (value);
2617 for (n = 0; n < size; n++) {
2618 kid = gst_value_list_get_value (value, n);
2619 fixed &= gst_value_is_fixed (kid);
2625 return gst_type_is_fixed (type);
2632 /* helper functions */
2634 /* Finds the greatest common divisor.
2635 * Returns 1 if none other found.
2636 * This is Euclid's algorithm. */
2638 gst_greatest_common_divisor (gint a, gint b)
2651 gst_value_init_fraction (GValue * value)
2653 value->data[0].v_int = 0;
2654 value->data[1].v_int = 1;
2658 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
2660 dest_value->data[0].v_int = src_value->data[0].v_int;
2661 dest_value->data[1].v_int = src_value->data[1].v_int;
2665 gst_value_collect_fraction (GValue * value, guint n_collect_values,
2666 GTypeCValue * collect_values, guint collect_flags)
2668 value->data[0].v_int = collect_values[0].v_int;
2669 value->data[1].v_int = collect_values[1].v_int;
2675 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
2676 GTypeCValue * collect_values, guint collect_flags)
2678 gint *numerator = collect_values[0].v_pointer;
2679 gint *denominator = collect_values[1].v_pointer;
2682 return g_strdup_printf ("numerator for `%s' passed as NULL",
2683 G_VALUE_TYPE_NAME (value));
2685 return g_strdup_printf ("denominator for `%s' passed as NULL",
2686 G_VALUE_TYPE_NAME (value));
2688 *numerator = value->data[0].v_int;
2689 *denominator = value->data[1].v_int;
2695 * gst_value_set_fraction:
2696 * @value: a GValue initialized to #GST_TYPE_FRACTION
2697 * @numerator: the numerator of the fraction
2698 * @denominator: the denominator of the fraction
2700 * Sets @value to the fraction specified by @numerator over @denominator.
2701 * The fraction gets reduced to the smallest numerator and denominator,
2702 * and if necessary the sign is moved to the numerator.
2705 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
2709 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
2710 g_return_if_fail (denominator != 0);
2711 g_return_if_fail (denominator >= -G_MAXINT);
2712 g_return_if_fail (numerator >= -G_MAXINT);
2714 /* normalize sign */
2715 if (denominator < 0) {
2716 numerator = -numerator;
2717 denominator = -denominator;
2720 /* check for reduction */
2721 gcd = gst_greatest_common_divisor (numerator, denominator);
2726 value->data[0].v_int = numerator;
2727 value->data[1].v_int = denominator;
2731 * gst_value_get_fraction_numerator:
2732 * @value: a GValue initialized to #GST_TYPE_FRACTION
2734 * Gets the numerator of the fraction specified by @value.
2736 * Returns: the numerator of the fraction.
2739 gst_value_get_fraction_numerator (const GValue * value)
2741 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
2743 return value->data[0].v_int;
2747 * gst_value_get_fraction_denominator:
2748 * @value: a GValue initialized to #GST_TYPE_FRACTION
2750 * Gets the denominator of the fraction specified by @value.
2752 * Returns: the denominator of the fraction.
2755 gst_value_get_fraction_denominator (const GValue * value)
2757 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
2759 return value->data[1].v_int;
2763 * gst_value_fraction_multiply:
2764 * @product: a GValue initialized to #GST_TYPE_FRACTION
2765 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
2766 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
2768 * Multiplies the two GValues containing a GstFraction and sets @product
2769 * to the product of the two fractions.
2771 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
2774 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
2775 const GValue * factor2)
2777 gint gcd, n1, n2, d1, d2;
2779 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
2780 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
2782 n1 = factor1->data[0].v_int;
2783 n2 = factor2->data[0].v_int;
2784 d1 = factor1->data[1].v_int;
2785 d2 = factor2->data[1].v_int;
2787 gcd = gst_greatest_common_divisor (n1, d2);
2790 gcd = gst_greatest_common_divisor (n2, d1);
2794 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
2795 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
2797 gst_value_set_fraction (product, n1 * n2, d1 * d2);
2803 gst_value_serialize_fraction (const GValue * value)
2805 gint32 numerator = value->data[0].v_int;
2806 gint32 denominator = value->data[1].v_int;
2807 gboolean positive = TRUE;
2809 /* get the sign and make components absolute */
2810 if (numerator < 0) {
2811 numerator = -numerator;
2812 positive = !positive;
2814 if (denominator < 0) {
2815 denominator = -denominator;
2816 positive = !positive;
2819 return g_strdup_printf ("%s%d/%d",
2820 positive ? "" : "-", numerator, denominator);
2824 gst_value_deserialize_fraction (GValue * dest, const char *s)
2828 if (s && sscanf (s, "%d/%d", &num, &den) == 2) {
2829 gst_value_set_fraction (dest, num, den);
2837 gst_value_transform_fraction_string (const GValue * src_value,
2838 GValue * dest_value)
2840 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
2844 gst_value_transform_string_fraction (const GValue * src_value,
2845 GValue * dest_value)
2847 gst_value_deserialize_fraction (dest_value, src_value->data[0].v_pointer);
2850 #define MAX_TERMS 30
2851 #define MIN_DIVISOR 1.0e-10
2852 #define MAX_ERROR 1.0e-20
2854 /* use continued fractions to transform a double into a fraction,
2855 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
2856 * This algorithm takes care of overflows.
2859 gst_value_transform_double_fraction (const GValue * src_value,
2860 GValue * dest_value)
2862 gdouble V, F; /* double being converted */
2863 gint N, D; /* will contain the result */
2864 gint A; /* current term in continued fraction */
2865 gint64 N1, D1; /* numerator, denominator of last approx */
2866 gint64 N2, D2; /* numerator, denominator of previous approx */
2868 gboolean negative = FALSE;
2870 /* initialize fraction being converted */
2871 F = src_value->data[0].v_double;
2878 /* initialize fractions with 1/0, 0/1 */
2886 for (i = 0; i < MAX_TERMS; i++) {
2889 /* get new divisor */
2892 /* calculate new fraction in temp */
2896 /* guard against overflow */
2897 if (N2 > G_MAXINT || D2 > G_MAXINT) {
2904 /* save last two fractions */
2910 /* quit if dividing by zero or close enough to target */
2911 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
2915 /* Take reciprocal */
2918 /* fix for overflow */
2923 /* fix for negative */
2927 /* will also simplify */
2928 gst_value_set_fraction (dest_value, N, D);
2932 gst_value_transform_fraction_double (const GValue * src_value,
2933 GValue * dest_value)
2935 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
2936 ((double) src_value->data[1].v_int);
2940 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
2948 n1 = value1->data[0].v_int;
2949 n2 = value2->data[0].v_int;
2950 d1 = value1->data[1].v_int;
2951 d2 = value2->data[1].v_int;
2953 /* fractions are reduced when set, so we can quickly see if they're equal */
2954 if (n1 == n2 && d1 == d2)
2955 return GST_VALUE_EQUAL;
2957 /* extend to 64 bits */
2958 new_num_1 = ((gint64) n1) * d2;
2959 new_num_2 = ((gint64) n2) * d1;
2960 if (new_num_1 < new_num_2)
2961 return GST_VALUE_LESS_THAN;
2962 if (new_num_1 > new_num_2)
2963 return GST_VALUE_GREATER_THAN;
2965 g_assert_not_reached ();
2966 return GST_VALUE_UNORDERED;
2974 * gst_value_set_date:
2975 * @value: a GValue initialized to GST_TYPE_DATE
2976 * @date: the date to set the value to
2978 * Sets the contents of @value to coorespond to @date. The actual
2979 * #GDate structure is copied before it is used.
2982 gst_value_set_date (GValue * value, const GDate * date)
2984 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
2986 g_value_set_boxed (value, date);
2990 * gst_value_get_date:
2991 * @value: a GValue initialized to GST_TYPE_DATE
2993 * Gets the contents of @value.
2995 * Returns: the contents of @value
2998 gst_value_get_date (const GValue * value)
3000 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3002 return (const GDate *) g_value_get_boxed (value);
3006 gst_date_copy (gpointer boxed)
3008 const GDate *date = (const GDate *) boxed;
3010 return g_date_new_julian (g_date_get_julian (date));
3014 gst_value_compare_date (const GValue * value1, const GValue * value2)
3016 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3017 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3021 return GST_VALUE_EQUAL;
3023 if ((date1 == NULL || !g_date_valid (date1))
3024 && (date2 != NULL && g_date_valid (date2))) {
3025 return GST_VALUE_LESS_THAN;
3028 if ((date2 == NULL || !g_date_valid (date2))
3029 && (date1 != NULL && g_date_valid (date1))) {
3030 return GST_VALUE_GREATER_THAN;
3033 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3034 || !g_date_valid (date2)) {
3035 return GST_VALUE_UNORDERED;
3038 j1 = g_date_get_julian (date1);
3039 j2 = g_date_get_julian (date2);
3042 return GST_VALUE_EQUAL;
3044 return GST_VALUE_LESS_THAN;
3046 return GST_VALUE_GREATER_THAN;
3050 gst_value_serialize_date (const GValue * val)
3052 const GDate *date = (const GDate *) g_value_get_boxed (val);
3054 if (date == NULL || !g_date_valid (date))
3055 return g_strdup ("9999-99-99");
3057 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3058 g_date_get_month (date), g_date_get_day (date));
3062 gst_value_deserialize_date (GValue * dest, const char *s)
3064 guint year, month, day;
3066 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3069 if (!g_date_valid_dmy (day, month, year))
3072 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3077 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3079 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3083 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3085 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3089 _gst_value_initialize (void)
3103 GTypeFundamentalInfo finfo = {
3107 //const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
3109 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
3110 gst_value_union_funcs = g_array_new (FALSE, FALSE,
3111 sizeof (GstValueUnionInfo));
3112 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
3113 sizeof (GstValueIntersectInfo));
3114 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
3115 sizeof (GstValueSubtractInfo));
3118 static const GTypeValueTable value_table = {
3119 gst_value_init_fourcc,
3121 gst_value_copy_fourcc,
3124 gst_value_collect_fourcc,
3126 gst_value_lcopy_fourcc
3128 static GstValueTable gst_value = {
3130 gst_value_compare_fourcc,
3131 gst_value_serialize_fourcc,
3132 gst_value_deserialize_fourcc,
3135 info.value_table = &value_table;
3136 gst_type_fourcc = g_type_register_fundamental (g_type_fundamental_next (),
3137 "GstFourcc", &info, &finfo, 0);
3138 gst_value.type = gst_type_fourcc;
3139 gst_value_register (&gst_value);
3143 static const GTypeValueTable value_table = {
3144 gst_value_init_int_range,
3146 gst_value_copy_int_range,
3149 gst_value_collect_int_range,
3151 gst_value_lcopy_int_range
3153 static GstValueTable gst_value = {
3155 gst_value_compare_int_range,
3156 gst_value_serialize_int_range,
3157 gst_value_deserialize_int_range,
3160 info.value_table = &value_table;
3161 gst_type_int_range =
3162 g_type_register_fundamental (g_type_fundamental_next (), "GstIntRange",
3164 gst_value.type = gst_type_int_range;
3165 gst_value_register (&gst_value);
3169 static const GTypeValueTable value_table = {
3170 gst_value_init_double_range,
3172 gst_value_copy_double_range,
3175 gst_value_collect_double_range,
3177 gst_value_lcopy_double_range
3179 static GstValueTable gst_value = {
3181 gst_value_compare_double_range,
3182 gst_value_serialize_double_range,
3183 gst_value_deserialize_double_range,
3186 info.value_table = &value_table;
3187 gst_type_double_range =
3188 g_type_register_fundamental (g_type_fundamental_next (),
3189 "GstDoubleRange", &info, &finfo, 0);
3190 gst_value.type = gst_type_double_range;
3191 gst_value_register (&gst_value);
3195 static const GTypeValueTable value_table = {
3196 gst_value_init_list,
3197 gst_value_free_list,
3198 gst_value_copy_list,
3199 gst_value_list_peek_pointer,
3201 gst_value_collect_list,
3203 gst_value_lcopy_list
3205 static GstValueTable gst_value = {
3207 gst_value_compare_list,
3208 gst_value_serialize_list,
3209 gst_value_deserialize_list,
3212 info.value_table = &value_table;
3213 gst_type_list = g_type_register_fundamental (g_type_fundamental_next (),
3214 "GstValueList", &info, &finfo, 0);
3215 gst_value.type = gst_type_list;
3216 gst_value_register (&gst_value);
3220 static const GTypeValueTable value_table = {
3221 gst_value_init_list,
3222 gst_value_free_list,
3223 gst_value_copy_list,
3224 gst_value_list_peek_pointer,
3226 gst_value_collect_list,
3228 gst_value_lcopy_list
3230 static GstValueTable gst_value = {
3232 gst_value_compare_list,
3233 gst_value_serialize_array,
3234 gst_value_deserialize_array,
3237 info.value_table = &value_table;
3239 g_type_register_fundamental (g_type_fundamental_next (),
3240 "GstValueArray", &info, &finfo, 0);
3241 gst_value.type = gst_type_array;
3242 gst_value_register (&gst_value);
3247 static const GTypeValueTable value_table = {
3248 gst_value_init_buffer,
3250 gst_value_copy_buffer,
3253 NULL, /*gst_value_collect_buffer, */
3255 NULL /*gst_value_lcopy_buffer */
3258 static GstValueTable gst_value = {
3260 gst_value_compare_buffer,
3261 gst_value_serialize_buffer,
3262 gst_value_deserialize_buffer,
3265 gst_value.type = GST_TYPE_BUFFER;
3266 gst_value_register (&gst_value);
3269 static const GTypeValueTable value_table = {
3270 gst_value_init_fraction,
3272 gst_value_copy_fraction,
3275 gst_value_collect_fraction,
3277 gst_value_lcopy_fraction
3279 static GstValueTable gst_value = {
3281 gst_value_compare_fraction,
3282 gst_value_serialize_fraction,
3283 gst_value_deserialize_fraction,
3286 info.value_table = &value_table;
3288 g_type_register_fundamental (g_type_fundamental_next (), "GstFraction",
3290 gst_value.type = gst_type_fraction;
3291 gst_value_register (&gst_value);
3294 static GstValueTable gst_value = {
3297 gst_value_serialize_caps,
3298 gst_value_deserialize_caps,
3301 gst_value.type = GST_TYPE_CAPS;
3302 gst_value_register (&gst_value);
3305 static GstValueTable gst_value = {
3307 gst_value_compare_date,
3308 gst_value_serialize_date,
3309 gst_value_deserialize_date,
3312 /* Not using G_TYPE_DATE here on purpose, even if we could
3313 * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
3314 * serialised strings to have different type strings depending
3315 * on what version is used, so FIXME in 0.11 when we
3316 * require GLib-2.8 */
3317 gst_type_date = g_boxed_type_register_static ("GstDate",
3318 (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
3320 gst_value.type = gst_type_date;
3321 gst_value_register (&gst_value);
3324 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
3325 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
3327 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
3328 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
3329 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
3331 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
3333 REGISTER_SERIALIZATION (G_TYPE_INT, int);
3335 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
3336 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
3338 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
3339 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
3340 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
3342 g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
3343 gst_value_transform_fourcc_string);
3344 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
3345 gst_value_transform_int_range_string);
3346 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
3347 gst_value_transform_double_range_string);
3348 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
3349 gst_value_transform_list_string);
3350 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
3351 gst_value_transform_array_string);
3352 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
3353 gst_value_transform_fraction_string);
3354 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
3355 gst_value_transform_string_fraction);
3356 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
3357 gst_value_transform_fraction_double);
3358 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
3359 gst_value_transform_double_fraction);
3360 g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
3361 gst_value_transform_date_string);
3362 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
3363 gst_value_transform_string_date);
3365 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3366 gst_value_intersect_int_int_range);
3367 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3368 gst_value_intersect_int_range_int_range);
3369 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
3370 gst_value_intersect_double_double_range);
3371 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
3372 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
3373 gst_value_register_intersect_func (GST_TYPE_ARRAY,
3374 GST_TYPE_ARRAY, gst_value_intersect_array);
3376 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3377 gst_value_subtract_int_int_range);
3378 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
3379 gst_value_subtract_int_range_int);
3380 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3381 gst_value_subtract_int_range_int_range);
3382 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
3383 gst_value_subtract_double_double_range);
3384 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
3385 gst_value_subtract_double_range_double);
3386 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
3387 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
3389 #if GLIB_CHECK_VERSION(2,8,0)
3390 /* see bug #317246, #64994, #65041 */
3392 volatile GType date_type = G_TYPE_DATE;
3394 GST_LOG ("Faking out the compiler: %d", date_type);
3398 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3399 gst_value_union_int_int_range);
3400 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3401 gst_value_union_int_range_int_range);