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 long long.
1039 * check if the result is actually storeable in the given size number of
1043 gst_value_deserialize_int_helper (long long *to, const char *s,
1044 long long min, long long max, int size)
1046 gboolean ret = FALSE;
1048 long long mask = -1;
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 long long fits into a long long; 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)
1180 CREATE_USERIALIZATION (uint, UINT)
1181 CREATE_USERIALIZATION (uint64, UINT64)
1182 CREATE_USERIALIZATION (ulong, ULONG)
1188 gst_value_compare_double (const GValue * value1, const GValue * value2)
1190 if (value1->data[0].v_double > value2->data[0].v_double)
1191 return GST_VALUE_GREATER_THAN;
1192 if (value1->data[0].v_double < value2->data[0].v_double)
1193 return GST_VALUE_LESS_THAN;
1194 if (value1->data[0].v_double == value2->data[0].v_double)
1195 return GST_VALUE_EQUAL;
1196 return GST_VALUE_UNORDERED;
1200 gst_value_serialize_double (const GValue * value)
1202 char d[G_ASCII_DTOSTR_BUF_SIZE];
1204 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1205 return g_strdup (d);
1209 gst_value_deserialize_double (GValue * dest, const char *s)
1212 gboolean ret = FALSE;
1215 x = g_ascii_strtod (s, &end);
1219 if (g_ascii_strcasecmp (s, "min") == 0) {
1222 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1228 g_value_set_double (dest, x);
1238 gst_value_compare_float (const GValue * value1, const GValue * value2)
1240 if (value1->data[0].v_float > value2->data[0].v_float)
1241 return GST_VALUE_GREATER_THAN;
1242 if (value1->data[0].v_float < value2->data[0].v_float)
1243 return GST_VALUE_LESS_THAN;
1244 if (value1->data[0].v_float == value2->data[0].v_float)
1245 return GST_VALUE_EQUAL;
1246 return GST_VALUE_UNORDERED;
1250 gst_value_serialize_float (const GValue * value)
1252 char d[G_ASCII_DTOSTR_BUF_SIZE];
1254 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1255 return g_strdup (d);
1259 gst_value_deserialize_float (GValue * dest, const char *s)
1262 gboolean ret = FALSE;
1265 x = g_ascii_strtod (s, &end);
1269 if (g_ascii_strcasecmp (s, "min") == 0) {
1272 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1277 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1280 g_value_set_float (dest, x);
1290 gst_value_compare_string (const GValue * value1, const GValue * value2)
1292 int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1295 return GST_VALUE_LESS_THAN;
1297 return GST_VALUE_GREATER_THAN;
1298 return GST_VALUE_EQUAL;
1301 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1302 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1306 gst_string_wrap (const char *s)
1311 gboolean wrap = FALSE;
1316 return g_strdup ("");
1318 if (GST_ASCII_IS_STRING (*t)) {
1320 } else if (*t < 0x20 || *t >= 0x7f) {
1331 return g_strdup (s);
1333 e = d = g_malloc (len + 3);
1338 if (GST_ASCII_IS_STRING (*t)) {
1340 } else if (*t < 0x20 || *t >= 0x7f) {
1342 *e++ = '0' + ((*(guchar *) t) >> 6);
1343 *e++ = '0' + (((*t) >> 3) & 0x7);
1344 *e++ = '0' + ((*t++) & 0x7);
1357 * This function takes a string delimited with double quotes (")
1358 * and unescapes any \xxx octal numbers.
1360 * If sequences of \y are found where y is not in the range of
1361 * 0->3, y is copied unescaped.
1363 * If \xyy is found where x is an octal number but y is not, an
1364 * error is encountered and NULL is returned.
1366 * the input string must be \0 terminated.
1369 gst_string_unwrap (const gchar * s)
1372 gchar *read, *write;
1374 /* NULL string returns NULL */
1378 /* strings not starting with " are invalid */
1382 /* make copy of original string to hold the result. This
1383 * string will always be smaller than the original */
1388 /* need to move to the next position as we parsed the " */
1392 if (GST_ASCII_IS_STRING (*read)) {
1393 /* normal chars are just copied */
1395 } else if (*read == '"') {
1396 /* quote marks end of string */
1398 } else if (*read == '\\') {
1399 /* got an escape char, move to next position to read a tripplet
1400 * of octal numbers */
1402 /* is the next char a possible first octal number? */
1403 if (*read >= '0' && *read <= '3') {
1404 /* parse other 2 numbers, if one of them is not in the range of
1405 * an octal number, we error. We also catch the case where a zero
1406 * byte is found here. */
1407 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1410 /* now convert the octal number to a byte again. */
1411 *write++ = ((read[0] - '0') << 6) +
1412 ((read[1] - '0') << 3) + (read[2] - '0');
1416 /* if we run into a \0 here, we definately won't get a quote later */
1420 /* else copy \X sequence */
1424 /* weird character, error */
1428 /* if the string is not ending in " and zero terminated, we error */
1429 if (*read != '"' || read[1] != '\0')
1432 /* null terminate result string and return */
1442 gst_value_serialize_string (const GValue * value)
1444 return gst_string_wrap (value->data[0].v_pointer);
1448 gst_value_deserialize_string (GValue * dest, const char *s)
1451 if (!g_utf8_validate (s, -1, NULL))
1453 g_value_set_string (dest, s);
1456 gchar *str = gst_string_unwrap (s);
1460 g_value_take_string (dest, str);
1471 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1473 GEnumValue *en1, *en2;
1474 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1475 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1477 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1478 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1479 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1480 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
1481 g_type_class_unref (klass1);
1482 g_type_class_unref (klass2);
1483 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
1484 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
1485 if (en1->value < en2->value)
1486 return GST_VALUE_LESS_THAN;
1487 if (en1->value > en2->value)
1488 return GST_VALUE_GREATER_THAN;
1490 return GST_VALUE_EQUAL;
1494 gst_value_serialize_enum (const GValue * value)
1497 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
1499 g_return_val_if_fail (klass, NULL);
1500 en = g_enum_get_value (klass, g_value_get_enum (value));
1501 g_type_class_unref (klass);
1502 g_return_val_if_fail (en, NULL);
1503 return g_strdup (en->value_name);
1507 gst_value_deserialize_enum (GValue * dest, const char *s)
1510 gchar *endptr = NULL;
1511 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1513 g_return_val_if_fail (klass, FALSE);
1514 if (!(en = g_enum_get_value_by_name (klass, s))) {
1515 if (!(en = g_enum_get_value_by_nick (klass, s))) {
1516 gint i = strtol (s, &endptr, 0);
1518 if (endptr && *endptr == '\0') {
1519 en = g_enum_get_value (klass, i);
1523 g_type_class_unref (klass);
1524 g_return_val_if_fail (en, FALSE);
1525 g_value_set_enum (dest, en->value);
1533 /* we just compare the value here */
1535 gst_value_compare_flags (const GValue * value1, const GValue * value2)
1538 GFlagsClass *klass1 =
1539 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1540 GFlagsClass *klass2 =
1541 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1543 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1544 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1545 fl1 = g_value_get_flags (value1);
1546 fl2 = g_value_get_flags (value2);
1547 g_type_class_unref (klass1);
1548 g_type_class_unref (klass2);
1550 return GST_VALUE_LESS_THAN;
1552 return GST_VALUE_GREATER_THAN;
1554 return GST_VALUE_EQUAL;
1557 /* the different flags are serialized separated with a + */
1559 gst_value_serialize_flags (const GValue * value)
1563 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
1564 gchar *result, *tmp;
1565 gboolean first = TRUE;
1567 g_return_val_if_fail (klass, NULL);
1569 result = g_strdup ("");
1570 flags = g_value_get_flags (value);
1572 fl = g_flags_get_first_value (klass, flags);
1574 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
1580 flags &= ~fl->value;
1582 g_type_class_unref (klass);
1588 gst_value_deserialize_flags (GValue * dest, const char *s)
1591 gchar *endptr = NULL;
1592 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1597 g_return_val_if_fail (klass, FALSE);
1599 /* split into parts delimited with + */
1600 split = g_strsplit (s, "+", 0);
1604 /* loop over each part */
1606 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
1607 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
1608 gint val = strtol (split[i], &endptr, 0);
1610 /* just or numeric value */
1611 if (endptr && *endptr == '\0') {
1622 g_type_class_unref (klass);
1623 g_value_set_flags (dest, flags);
1633 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
1634 const GValue * src2)
1636 if (src2->data[0].v_int <= src1->data[0].v_int &&
1637 src2->data[1].v_int >= src1->data[0].v_int) {
1638 gst_value_init_and_copy (dest, src2);
1645 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
1646 const GValue * src2)
1651 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
1652 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
1655 g_value_init (dest, GST_TYPE_INT_RANGE);
1656 gst_value_set_int_range (dest,
1657 MIN (src1->data[0].v_int, src2->data[0].v_int),
1658 MAX (src1->data[1].v_int, src2->data[1].v_int));
1670 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
1671 const GValue * src2)
1673 if (src2->data[0].v_int <= src1->data[0].v_int &&
1674 src2->data[1].v_int >= src1->data[0].v_int) {
1675 gst_value_init_and_copy (dest, src1);
1683 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
1684 const GValue * src2)
1689 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
1690 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
1693 g_value_init (dest, GST_TYPE_INT_RANGE);
1694 gst_value_set_int_range (dest, min, max);
1698 g_value_init (dest, G_TYPE_INT);
1699 g_value_set_int (dest, min);
1707 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
1708 const GValue * src2)
1710 if (src2->data[0].v_double <= src1->data[0].v_double &&
1711 src2->data[1].v_double >= src1->data[0].v_double) {
1712 gst_value_init_and_copy (dest, src1);
1720 gst_value_intersect_double_range_double_range (GValue * dest,
1721 const GValue * src1, const GValue * src2)
1726 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
1727 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
1730 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
1731 gst_value_set_double_range (dest, min, max);
1735 g_value_init (dest, G_TYPE_DOUBLE);
1736 g_value_set_int (dest, min);
1744 gst_value_intersect_list (GValue * dest, const GValue * value1,
1745 const GValue * value2)
1748 GValue intersection = { 0, };
1749 gboolean ret = FALSE;
1751 size = gst_value_list_get_size (value1);
1752 for (i = 0; i < size; i++) {
1753 const GValue *cur = gst_value_list_get_value (value1, i);
1755 if (gst_value_intersect (&intersection, cur, value2)) {
1758 gst_value_init_and_copy (dest, &intersection);
1760 } else if (GST_VALUE_HOLDS_LIST (dest)) {
1761 gst_value_list_append_value (dest, &intersection);
1763 GValue temp = { 0, };
1765 gst_value_init_and_copy (&temp, dest);
1766 g_value_unset (dest);
1767 gst_value_list_concat (dest, &temp, &intersection);
1768 g_value_unset (&temp);
1770 g_value_unset (&intersection);
1778 gst_value_intersect_array (GValue * dest, const GValue * src1,
1779 const GValue * src2)
1784 /* only works on similar-sized arrays */
1785 size = gst_value_list_get_size (src1);
1786 if (size != gst_value_list_get_size (src2))
1788 g_value_init (dest, GST_TYPE_ARRAY);
1790 for (n = 0; n < size; n++) {
1791 if (!gst_value_intersect (&val, gst_value_list_get_value (src1, n),
1792 gst_value_list_get_value (src2, n))) {
1793 g_value_unset (dest);
1796 gst_value_list_append_value (dest, &val);
1797 g_value_unset (&val);
1808 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
1809 const GValue * subtrahend)
1811 int min = gst_value_get_int_range_min (subtrahend);
1812 int max = gst_value_get_int_range_max (subtrahend);
1813 int val = g_value_get_int (minuend);
1815 /* subtracting a range from an int only works if the int is not in the
1817 if (val < min || val > max) {
1818 /* and the result is the int */
1819 gst_value_init_and_copy (dest, minuend);
1825 /* creates a new int range based on input values.
1828 gst_value_create_new_range (GValue * dest, int min1, int max1, int min2,
1833 GValue *pv1, *pv2; /* yeah, hungarian! */
1835 if (min1 <= max1 && min2 <= max2) {
1838 } else if (min1 <= max1) {
1841 } else if (min2 <= max2) {
1849 g_value_init (pv1, GST_TYPE_INT_RANGE);
1850 gst_value_set_int_range (pv1, min1, max1);
1851 } else if (min1 == max1) {
1852 g_value_init (pv1, G_TYPE_INT);
1853 g_value_set_int (pv1, min1);
1856 g_value_init (pv2, GST_TYPE_INT_RANGE);
1857 gst_value_set_int_range (pv2, min2, max2);
1858 } else if (min2 == max2) {
1859 g_value_init (pv2, G_TYPE_INT);
1860 g_value_set_int (pv2, min2);
1863 if (min1 <= max1 && min2 <= max2) {
1864 gst_value_list_concat (dest, pv1, pv2);
1865 g_value_unset (pv1);
1866 g_value_unset (pv2);
1872 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
1873 const GValue * subtrahend)
1875 int min = gst_value_get_int_range_min (minuend);
1876 int max = gst_value_get_int_range_max (minuend);
1877 int val = g_value_get_int (subtrahend);
1879 g_return_val_if_fail (min < max, FALSE);
1881 /* value is outside of the range, return range unchanged */
1882 if (val < min || val > max) {
1883 gst_value_init_and_copy (dest, minuend);
1886 /* max must be MAXINT too as val <= max */
1887 if (val == G_MAXINT) {
1891 /* min must be MININT too as val >= max */
1892 if (val == G_MININT) {
1896 gst_value_create_new_range (dest, min, val - 1, val + 1, max);
1902 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
1903 const GValue * subtrahend)
1905 int min1 = gst_value_get_int_range_min (minuend);
1906 int max1 = gst_value_get_int_range_max (minuend);
1907 int min2 = gst_value_get_int_range_min (subtrahend);
1908 int max2 = gst_value_get_int_range_max (subtrahend);
1910 if (max2 == G_MAXINT && min2 == G_MININT) {
1912 } else if (max2 == G_MAXINT) {
1913 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
1914 } else if (min2 == G_MININT) {
1915 return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
1917 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
1918 MAX (max2 + 1, min1), max1);
1923 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
1924 const GValue * subtrahend)
1926 double min = gst_value_get_double_range_min (subtrahend);
1927 double max = gst_value_get_double_range_max (subtrahend);
1928 double val = g_value_get_double (minuend);
1930 if (val < min || val > max) {
1931 gst_value_init_and_copy (dest, minuend);
1938 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
1939 const GValue * subtrahend)
1941 /* since we don't have open ranges, we cannot create a hole in
1942 * a double range. We return the original range */
1943 gst_value_init_and_copy (dest, minuend);
1948 gst_value_subtract_double_range_double_range (GValue * dest,
1949 const GValue * minuend, const GValue * subtrahend)
1951 /* since we don't have open ranges, we have to approximate */
1952 /* done like with ints */
1953 double min1 = gst_value_get_double_range_min (minuend);
1954 double max2 = gst_value_get_double_range_max (minuend);
1955 double max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
1956 double min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
1959 GValue *pv1, *pv2; /* yeah, hungarian! */
1961 if (min1 < max1 && min2 < max2) {
1964 } else if (min1 < max1) {
1967 } else if (min2 < max2) {
1975 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
1976 gst_value_set_double_range (pv1, min1, max1);
1979 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
1980 gst_value_set_double_range (pv2, min2, max2);
1983 if (min1 < max1 && min2 < max2) {
1984 gst_value_list_concat (dest, pv1, pv2);
1985 g_value_unset (pv1);
1986 g_value_unset (pv2);
1992 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
1993 const GValue * subtrahend)
1996 GValue subtraction = { 0, };
1997 gboolean ret = FALSE;
1999 size = gst_value_list_get_size (minuend);
2000 for (i = 0; i < size; i++) {
2001 const GValue *cur = gst_value_list_get_value (minuend, i);
2003 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2005 gst_value_init_and_copy (dest, &subtraction);
2007 } else if (GST_VALUE_HOLDS_LIST (dest)
2008 && GST_VALUE_HOLDS_LIST (&subtraction)) {
2010 GValue unroll = { 0, };
2012 gst_value_init_and_copy (&unroll, dest);
2013 g_value_unset (dest);
2014 gst_value_list_concat (dest, &unroll, &subtraction);
2015 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2016 gst_value_list_append_value (dest, &subtraction);
2018 GValue temp = { 0, };
2020 gst_value_init_and_copy (&temp, dest);
2021 g_value_unset (dest);
2022 gst_value_list_concat (dest, &temp, &subtraction);
2023 g_value_unset (&temp);
2025 g_value_unset (&subtraction);
2032 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2033 const GValue * subtrahend)
2036 GValue data[2] = { {0,}, {0,} };
2037 GValue *subtraction = &data[0], *result = &data[1];
2039 gst_value_init_and_copy (result, minuend);
2040 size = gst_value_list_get_size (subtrahend);
2041 for (i = 0; i < size; i++) {
2042 const GValue *cur = gst_value_list_get_value (subtrahend, i);
2044 if (gst_value_subtract (subtraction, result, cur)) {
2045 GValue *temp = result;
2047 result = subtraction;
2049 g_value_unset (subtraction);
2051 g_value_unset (result);
2055 gst_value_init_and_copy (dest, result);
2056 g_value_unset (result);
2066 * gst_value_can_compare:
2067 * @value1: a value to compare
2068 * @value2: another value to compare
2070 * Determines if @value1 and @value2 can be compared.
2072 * Returns: TRUE if the values can be compared
2075 gst_value_can_compare (const GValue * value1, const GValue * value2)
2077 GstValueTable *table;
2080 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2083 for (i = 0; i < gst_value_table->len; i++) {
2084 table = &g_array_index (gst_value_table, GstValueTable, i);
2085 if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2093 * gst_value_compare:
2094 * @value1: a value to compare
2095 * @value2: another value to compare
2097 * Compares @value1 and @value2. If @value1 and @value2 cannot be
2098 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
2099 * if @value1 is greater than @value2, GST_VALUE_GREATER is returned.
2100 * If @value1 is less than @value2, GST_VALUE_LESSER is returned.
2101 * If the values are equal, GST_VALUE_EQUAL is returned.
2103 * Returns: A GstValueCompareType value
2106 gst_value_compare (const GValue * value1, const GValue * value2)
2108 GstValueTable *table, *best = NULL;
2111 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2112 return GST_VALUE_UNORDERED;
2114 for (i = 0; i < gst_value_table->len; i++) {
2115 table = &g_array_index (gst_value_table, GstValueTable, i);
2116 if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2120 if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2121 if (!best || g_type_is_a (table->type, best->type))
2126 return best->compare (value1, value2);
2129 g_critical ("unable to compare values of type %s\n",
2130 g_type_name (G_VALUE_TYPE (value1)));
2131 return GST_VALUE_UNORDERED;
2137 * gst_value_can_union:
2138 * @value1: a value to union
2139 * @value2: another value to union
2141 * Determines if @value1 and @value2 can be non-trivially unioned.
2142 * Any two values can be trivially unioned by adding both of them
2143 * to a GstValueList. However, certain types have the possibility
2144 * to be unioned in a simpler way. For example, an integer range
2145 * and an integer can be unioned if the integer is a subset of the
2146 * integer range. If there is the possibility that two values can
2147 * be unioned, this function returns TRUE.
2149 * Returns: TRUE if there is a function allowing the two values to
2153 gst_value_can_union (const GValue * value1, const GValue * value2)
2155 GstValueUnionInfo *union_info;
2158 for (i = 0; i < gst_value_union_funcs->len; i++) {
2159 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2160 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2161 union_info->type2 == G_VALUE_TYPE (value2))
2163 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2164 union_info->type2 == G_VALUE_TYPE (value1))
2173 * @dest: the destination value
2174 * @value1: a value to union
2175 * @value2: another value to union
2177 * Creates a GValue cooresponding to the union of @value1 and @value2.
2179 * Returns: TRUE if the values could be unioned
2182 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2184 GstValueUnionInfo *union_info;
2187 for (i = 0; i < gst_value_union_funcs->len; i++) {
2188 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2189 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2190 union_info->type2 == G_VALUE_TYPE (value2)) {
2191 if (union_info->func (dest, value1, value2)) {
2195 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2196 union_info->type2 == G_VALUE_TYPE (value1)) {
2197 if (union_info->func (dest, value2, value1)) {
2203 gst_value_list_concat (dest, value1, value2);
2208 * gst_value_register_union_func:
2209 * @type1: a type to union
2210 * @type2: another type to union
2211 * @func: a function that implments creating a union between the two types
2213 * Registers a union function that can create a union between GValues
2214 * of the type @type1 and @type2.
2218 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
2220 GstValueUnionInfo union_info;
2222 union_info.type1 = type1;
2223 union_info.type2 = type2;
2224 union_info.func = func;
2226 g_array_append_val (gst_value_union_funcs, union_info);
2232 * gst_value_can_intersect:
2233 * @value1: a value to intersect
2234 * @value2: another value to intersect
2236 * Determines if intersecting two values will produce a valid result.
2237 * Two values will produce a valid intersection if they have the same
2238 * type, or if there is a method (registered by
2239 * #gst_value_register_intersection_func) to calculate the intersection.
2241 * Returns: TRUE if the values can intersect
2244 gst_value_can_intersect (const GValue * value1, const GValue * value2)
2246 GstValueIntersectInfo *intersect_info;
2250 if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
2253 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2254 intersect_info = &g_array_index (gst_value_intersect_funcs,
2255 GstValueIntersectInfo, i);
2256 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2257 intersect_info->type2 == G_VALUE_TYPE (value2))
2258 if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
2259 intersect_info->type1 == G_VALUE_TYPE (value2))
2263 return gst_value_can_compare (value1, value2);
2267 * gst_value_intersect:
2268 * @dest: a uninitialized #GValue that will hold the calculated
2269 * intersection value
2270 * @value1: a value to intersect
2271 * @value2: another value to intersect
2273 * Calculates the intersection of two values. If the values have
2274 * a non-empty intersection, the value representing the intersection
2275 * is placed in @dest. If the intersection is non-empty, @dest is
2278 * Returns: TRUE if the intersection is non-empty
2281 gst_value_intersect (GValue * dest, const GValue * value1,
2282 const GValue * value2)
2284 GstValueIntersectInfo *intersect_info;
2288 /* special cases first */
2289 if (GST_VALUE_HOLDS_LIST (value1))
2290 return gst_value_intersect_list (dest, value1, value2);
2291 if (GST_VALUE_HOLDS_LIST (value2))
2292 return gst_value_intersect_list (dest, value2, value1);
2294 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2295 intersect_info = &g_array_index (gst_value_intersect_funcs,
2296 GstValueIntersectInfo, i);
2297 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2298 intersect_info->type2 == G_VALUE_TYPE (value2)) {
2299 ret = intersect_info->func (dest, value1, value2);
2302 if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
2303 intersect_info->type2 == G_VALUE_TYPE (value1)) {
2304 ret = intersect_info->func (dest, value2, value1);
2309 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
2310 gst_value_init_and_copy (dest, value1);
2318 * gst_value_register_intersection_func:
2319 * @type1: the first type to intersect
2320 * @type2: the second type to intersect
2321 * @func: the intersection function
2323 * Registers a function that is called to calculate the intersection
2324 * of the values having the types @type1 and @type2.
2327 * GstValueIntersectFunc:
2328 * @dest: a uninitialized #GValue that will hold the calculated
2329 * intersection value
2330 * @value1: a value to intersect
2331 * @value2: another value to intersect
2333 * Functions having this type calculate the intersection of @value1
2334 * and @value2. If the intersection is non-empty, the result is
2335 * placed in @dest and TRUE is returned. If the intersection is
2336 * empty, @dest is unmodified and FALSE is returned.
2338 * Returns: TRUE if the intersection is non-empty, FALSE otherwise
2341 gst_value_register_intersect_func (GType type1, GType type2,
2342 GstValueIntersectFunc func)
2344 GstValueIntersectInfo intersect_info;
2346 intersect_info.type1 = type1;
2347 intersect_info.type2 = type2;
2348 intersect_info.func = func;
2350 g_array_append_val (gst_value_intersect_funcs, intersect_info);
2357 * gst_value_subtract:
2358 * @dest: the destination value for the result if the subtraction is not empty
2359 * @minuend: the value to subtract from
2360 * @subtrahend: the value to subtract
2362 * Subtracts @subtrahend from @minuend and stores the result in @dest.
2363 * Note that this means subtraction as in sets, not as in mathematics.
2365 * Returns: TRUE if the subtraction is not empty
2368 gst_value_subtract (GValue * dest, const GValue * minuend,
2369 const GValue * subtrahend)
2371 GstValueSubtractInfo *info;
2374 /* special cases first */
2375 if (GST_VALUE_HOLDS_LIST (minuend))
2376 return gst_value_subtract_from_list (dest, minuend, subtrahend);
2377 if (GST_VALUE_HOLDS_LIST (subtrahend))
2378 return gst_value_subtract_list (dest, minuend, subtrahend);
2380 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
2381 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
2382 if (info->minuend == G_VALUE_TYPE (minuend) &&
2383 info->subtrahend == G_VALUE_TYPE (subtrahend)) {
2384 return info->func (dest, minuend, subtrahend);
2388 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
2389 gst_value_init_and_copy (dest, minuend);
2398 gst_value_subtract (GValue * dest, const GValue * minuend,
2399 const GValue * subtrahend)
2401 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
2403 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
2404 gst_value_serialize (subtrahend),
2405 ret ? gst_value_serialize (dest) : "---");
2411 * gst_value_can_subtract:
2412 * @minuend: the value to subtract from
2413 * @subtrahend: the value to subtract
2415 * Checks if it's possible to subtract @subtrahend from @minuend.
2417 * Returns: TRUE if a subtraction is possible
2420 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
2422 GstValueSubtractInfo *info;
2426 if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
2429 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
2430 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
2431 if (info->minuend == G_VALUE_TYPE (minuend) &&
2432 info->subtrahend == G_VALUE_TYPE (subtrahend))
2436 return gst_value_can_compare (minuend, subtrahend);
2440 * gst_value_register_subtract_func:
2441 * @minuend_type: type of the minuend
2442 * @subtrahend_type: type of the subtrahend
2443 * @func: function to use
2445 * Registers @func as a function capable of subtracting the values of
2446 * @subtrahend_type from values of @minuend_type.
2449 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
2450 GstValueSubtractFunc func)
2452 GstValueSubtractInfo info;
2454 /* one type must be unfixed, other subtractions can be done as comparisons */
2455 g_return_if_fail (!gst_type_is_fixed (minuend_type)
2456 || !gst_type_is_fixed (subtrahend_type));
2458 info.minuend = minuend_type;
2459 info.subtrahend = subtrahend_type;
2462 g_array_append_val (gst_value_subtract_funcs, info);
2466 * gst_value_register:
2467 * @table: structure containing functions to register
2469 * Registers functions to perform calculations on #GValues of a given
2474 * @type: GType that the functions operate on.
2475 * @compare: A function that compares two values of this type.
2476 * @serialize: A function that transforms a value of this type to a
2477 * string. Strings created by this function must be unique and should
2478 * be human readable.
2479 * @deserialize: A function that transforms a string to a value of
2480 * this type. This function must transform strings created by the
2481 * serialize function back to the original value. This function may
2482 * optionally transform other strings into values.
2485 gst_value_register (const GstValueTable * table)
2487 g_array_append_val (gst_value_table, *table);
2491 * gst_value_init_and_copy:
2492 * @dest: the target value
2493 * @src: the source value
2495 * Initialises the target value to be of the same type as source and then copies
2496 * the contents from source to target.
2499 gst_value_init_and_copy (GValue * dest, const GValue * src)
2501 g_value_init (dest, G_VALUE_TYPE (src));
2502 g_value_copy (src, dest);
2506 * gst_value_serialize:
2507 * @value: a #GValue to serialize
2509 * tries to transform the given @value into a string representation that allows
2510 * getting back this string later on using gst_value_deserialize().
2512 * Returns: the serialization for @value or NULL if none exists
2515 gst_value_serialize (const GValue * value)
2518 GValue s_val = { 0 };
2519 GstValueTable *table, *best = NULL;
2522 g_return_val_if_fail (G_IS_VALUE (value), NULL);
2524 for (i = 0; i < gst_value_table->len; i++) {
2525 table = &g_array_index (gst_value_table, GstValueTable, i);
2526 if (table->serialize == NULL)
2528 if (table->type == G_VALUE_TYPE (value)) {
2532 if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
2533 if (!best || g_type_is_a (table->type, best->type))
2538 return best->serialize (value);
2540 g_value_init (&s_val, G_TYPE_STRING);
2541 if (g_value_transform (value, &s_val)) {
2542 s = gst_string_wrap (g_value_get_string (&s_val));
2546 g_value_unset (&s_val);
2552 * gst_value_deserialize:
2553 * @dest: #GValue to fill with contents of deserialization
2554 * @src: string to deserialize
2556 * Tries to deserialize a string into the type specified by the given GValue.
2557 * If the operation succeeds, TRUE is returned, FALSE otherwise.
2559 * Returns: TRUE on success
2562 gst_value_deserialize (GValue * dest, const gchar * src)
2564 GstValueTable *table, *best = NULL;
2567 g_return_val_if_fail (src != NULL, FALSE);
2568 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
2570 for (i = 0; i < gst_value_table->len; i++) {
2571 table = &g_array_index (gst_value_table, GstValueTable, i);
2572 if (table->serialize == NULL)
2575 if (table->type == G_VALUE_TYPE (dest)) {
2580 if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
2581 if (!best || g_type_is_a (table->type, best->type))
2586 return best->deserialize (dest, src);
2593 * gst_value_is_fixed:
2594 * @value: the #GValue to check
2596 * Tests if the given GValue, if available in a GstStructure (or any other
2597 * container) contains a "fixed" (which means: one value) or an "unfixed"
2598 * (which means: multiple possible values, such as data lists or data
2601 * Returns: true if the value is "fixed".
2605 gst_value_is_fixed (const GValue * value)
2607 GType type = G_VALUE_TYPE (value);
2609 if (type == GST_TYPE_ARRAY) {
2610 gboolean fixed = TRUE;
2614 /* check recursively */
2615 size = gst_value_list_get_size (value);
2616 for (n = 0; n < size; n++) {
2617 kid = gst_value_list_get_value (value, n);
2618 fixed &= gst_value_is_fixed (kid);
2624 return gst_type_is_fixed (type);
2631 /* helper functions */
2633 /* Finds the greatest common divisor.
2634 * Returns 1 if none other found.
2635 * This is Euclid's algorithm. */
2637 gst_greatest_common_divisor (gint a, gint b)
2650 gst_value_init_fraction (GValue * value)
2652 value->data[0].v_int = 0;
2653 value->data[1].v_int = 1;
2657 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
2659 dest_value->data[0].v_int = src_value->data[0].v_int;
2660 dest_value->data[1].v_int = src_value->data[1].v_int;
2664 gst_value_collect_fraction (GValue * value, guint n_collect_values,
2665 GTypeCValue * collect_values, guint collect_flags)
2667 value->data[0].v_int = collect_values[0].v_int;
2668 value->data[1].v_int = collect_values[1].v_int;
2674 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
2675 GTypeCValue * collect_values, guint collect_flags)
2677 gint *numerator = collect_values[0].v_pointer;
2678 gint *denominator = collect_values[1].v_pointer;
2681 return g_strdup_printf ("numerator for `%s' passed as NULL",
2682 G_VALUE_TYPE_NAME (value));
2684 return g_strdup_printf ("denominator for `%s' passed as NULL",
2685 G_VALUE_TYPE_NAME (value));
2687 *numerator = value->data[0].v_int;
2688 *denominator = value->data[1].v_int;
2694 * gst_value_set_fraction:
2695 * @value: a GValue initialized to #GST_TYPE_FRACTION
2696 * @numerator: the numerator of the fraction
2697 * @denominator: the denominator of the fraction
2699 * Sets @value to the fraction specified by @numerator over @denominator.
2700 * The fraction gets reduced to the smallest numerator and denominator,
2701 * and if necessary the sign is moved to the numerator.
2704 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
2708 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
2709 g_return_if_fail (denominator != 0);
2710 g_return_if_fail (denominator >= -G_MAXINT);
2711 g_return_if_fail (numerator >= -G_MAXINT);
2713 /* normalize sign */
2714 if (denominator < 0) {
2715 numerator = -numerator;
2716 denominator = -denominator;
2719 /* check for reduction */
2720 gcd = gst_greatest_common_divisor (numerator, denominator);
2725 value->data[0].v_int = numerator;
2726 value->data[1].v_int = denominator;
2730 * gst_value_get_fraction_numerator:
2731 * @value: a GValue initialized to #GST_TYPE_FRACTION
2733 * Gets the numerator of the fraction specified by @value.
2735 * Returns: the numerator of the fraction.
2738 gst_value_get_fraction_numerator (const GValue * value)
2740 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
2742 return value->data[0].v_int;
2746 * gst_value_get_fraction_denominator:
2747 * @value: a GValue initialized to #GST_TYPE_FRACTION
2749 * Gets the denominator of the fraction specified by @value.
2751 * Returns: the denominator of the fraction.
2754 gst_value_get_fraction_denominator (const GValue * value)
2756 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
2758 return value->data[1].v_int;
2762 * gst_value_fraction_multiply:
2763 * @product: a GValue initialized to #GST_TYPE_FRACTION
2764 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
2765 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
2767 * Multiplies the two GValues containing a GstFraction and sets @product
2768 * to the product of the two fractions.
2770 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
2773 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
2774 const GValue * factor2)
2776 gint gcd, n1, n2, d1, d2;
2778 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
2779 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
2781 n1 = factor1->data[0].v_int;
2782 n2 = factor2->data[0].v_int;
2783 d1 = factor1->data[1].v_int;
2784 d2 = factor2->data[1].v_int;
2786 gcd = gst_greatest_common_divisor (n1, d2);
2789 gcd = gst_greatest_common_divisor (n2, d1);
2793 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
2794 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
2796 gst_value_set_fraction (product, n1 * n2, d1 * d2);
2802 gst_value_serialize_fraction (const GValue * value)
2804 gint32 numerator = value->data[0].v_int;
2805 gint32 denominator = value->data[1].v_int;
2806 gboolean positive = TRUE;
2808 /* get the sign and make components absolute */
2809 if (numerator < 0) {
2810 numerator = -numerator;
2811 positive = !positive;
2813 if (denominator < 0) {
2814 denominator = -denominator;
2815 positive = !positive;
2818 return g_strdup_printf ("%s%d/%d",
2819 positive ? "" : "-", numerator, denominator);
2823 gst_value_deserialize_fraction (GValue * dest, const char *s)
2829 div = strstr (s, "/");
2832 tmp = g_strndup (s, (size_t) (div - s));
2835 den = atoi (div + 1);
2837 gst_value_set_fraction (dest, num, den);
2843 gst_value_transform_fraction_string (const GValue * src_value,
2844 GValue * dest_value)
2846 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
2850 gst_value_transform_string_fraction (const GValue * src_value,
2851 GValue * dest_value)
2853 gst_value_deserialize_fraction (dest_value, src_value->data[0].v_pointer);
2856 #define MAX_TERMS 30
2857 #define MIN_DIVISOR 1.0e-10
2858 #define MAX_ERROR 1.0e-20
2860 /* use continued fractions to transform a double into a fraction,
2861 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
2862 * This algorithm takes care of overflows.
2865 gst_value_transform_double_fraction (const GValue * src_value,
2866 GValue * dest_value)
2868 gdouble V, F; /* double being converted */
2869 gint N, D; /* will contain the result */
2870 gint A; /* current term in continued fraction */
2871 gint64 N1, D1; /* numerator, denominator of last approx */
2872 gint64 N2, D2; /* numerator, denominator of previous approx */
2874 gboolean negative = FALSE;
2876 /* initialize fraction being converted */
2877 F = src_value->data[0].v_double;
2884 /* initialize fractions with 1/0, 0/1 */
2892 for (i = 0; i < MAX_TERMS; i++) {
2895 /* get new divisor */
2898 /* calculate new fraction in temp */
2902 /* guard against overflow */
2903 if (N2 > G_MAXINT || D2 > G_MAXINT) {
2910 /* save last two fractions */
2916 /* quit if dividing by zero or close enough to target */
2917 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
2921 /* Take reciprocal */
2924 /* fix for overflow */
2929 /* fix for negative */
2933 /* will also simplify */
2934 gst_value_set_fraction (dest_value, N, D);
2938 gst_value_transform_fraction_double (const GValue * src_value,
2939 GValue * dest_value)
2941 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
2942 ((double) src_value->data[1].v_int);
2946 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
2954 n1 = value1->data[0].v_int;
2955 n2 = value2->data[0].v_int;
2956 d1 = value1->data[1].v_int;
2957 d2 = value2->data[1].v_int;
2959 /* fractions are reduced when set, so we can quickly see if they're equal */
2960 if (n1 == n2 && d1 == d2)
2961 return GST_VALUE_EQUAL;
2963 /* extend to 64 bits */
2964 new_num_1 = ((gint64) n1) * d2;
2965 new_num_2 = ((gint64) n2) * d1;
2966 if (new_num_1 < new_num_2)
2967 return GST_VALUE_LESS_THAN;
2968 if (new_num_1 > new_num_2)
2969 return GST_VALUE_GREATER_THAN;
2971 g_assert_not_reached ();
2972 return GST_VALUE_UNORDERED;
2980 * gst_value_set_date:
2981 * @value: a GValue initialized to GST_TYPE_DATE
2982 * @date: the date to set the value to
2984 * Sets the contents of @value to coorespond to @date. The actual
2985 * #GDate structure is copied before it is used.
2988 gst_value_set_date (GValue * value, const GDate * date)
2990 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
2992 g_value_set_boxed (value, date);
2996 * gst_value_get_date:
2997 * @value: a GValue initialized to GST_TYPE_DATE
2999 * Gets the contents of @value.
3001 * Returns: the contents of @value
3004 gst_value_get_date (const GValue * value)
3006 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3008 return (const GDate *) g_value_get_boxed (value);
3012 gst_date_copy (gpointer boxed)
3014 const GDate *date = (const GDate *) boxed;
3016 return g_date_new_julian (g_date_get_julian (date));
3020 gst_value_compare_date (const GValue * value1, const GValue * value2)
3022 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3023 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3027 return GST_VALUE_EQUAL;
3029 if ((date1 == NULL || !g_date_valid (date1))
3030 && (date2 != NULL && g_date_valid (date2))) {
3031 return GST_VALUE_LESS_THAN;
3034 if ((date2 == NULL || !g_date_valid (date2))
3035 && (date1 != NULL && g_date_valid (date1))) {
3036 return GST_VALUE_GREATER_THAN;
3039 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3040 || !g_date_valid (date2)) {
3041 return GST_VALUE_UNORDERED;
3044 j1 = g_date_get_julian (date1);
3045 j2 = g_date_get_julian (date2);
3048 return GST_VALUE_EQUAL;
3050 return GST_VALUE_LESS_THAN;
3052 return GST_VALUE_GREATER_THAN;
3056 gst_value_serialize_date (const GValue * val)
3058 const GDate *date = (const GDate *) g_value_get_boxed (val);
3060 if (date == NULL || !g_date_valid (date))
3061 return g_strdup ("9999-99-99");
3063 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3064 g_date_get_month (date), g_date_get_day (date));
3068 gst_value_deserialize_date (GValue * dest, const char *s)
3070 guint year, month, day;
3072 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3075 if (!g_date_valid_dmy (day, month, year))
3078 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3083 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3085 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3089 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3091 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3095 _gst_value_initialize (void)
3097 volatile GType date_type;
3111 GTypeFundamentalInfo finfo = {
3115 //const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
3117 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
3118 gst_value_union_funcs = g_array_new (FALSE, FALSE,
3119 sizeof (GstValueUnionInfo));
3120 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
3121 sizeof (GstValueIntersectInfo));
3122 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
3123 sizeof (GstValueSubtractInfo));
3126 static const GTypeValueTable value_table = {
3127 gst_value_init_fourcc,
3129 gst_value_copy_fourcc,
3132 gst_value_collect_fourcc,
3134 gst_value_lcopy_fourcc
3136 static GstValueTable gst_value = {
3138 gst_value_compare_fourcc,
3139 gst_value_serialize_fourcc,
3140 gst_value_deserialize_fourcc,
3143 info.value_table = &value_table;
3144 gst_type_fourcc = g_type_register_fundamental (g_type_fundamental_next (),
3145 "GstFourcc", &info, &finfo, 0);
3146 gst_value.type = gst_type_fourcc;
3147 gst_value_register (&gst_value);
3151 static const GTypeValueTable value_table = {
3152 gst_value_init_int_range,
3154 gst_value_copy_int_range,
3157 gst_value_collect_int_range,
3159 gst_value_lcopy_int_range
3161 static GstValueTable gst_value = {
3163 gst_value_compare_int_range,
3164 gst_value_serialize_int_range,
3165 gst_value_deserialize_int_range,
3168 info.value_table = &value_table;
3169 gst_type_int_range =
3170 g_type_register_fundamental (g_type_fundamental_next (), "GstIntRange",
3172 gst_value.type = gst_type_int_range;
3173 gst_value_register (&gst_value);
3177 static const GTypeValueTable value_table = {
3178 gst_value_init_double_range,
3180 gst_value_copy_double_range,
3183 gst_value_collect_double_range,
3185 gst_value_lcopy_double_range
3187 static GstValueTable gst_value = {
3189 gst_value_compare_double_range,
3190 gst_value_serialize_double_range,
3191 gst_value_deserialize_double_range,
3194 info.value_table = &value_table;
3195 gst_type_double_range =
3196 g_type_register_fundamental (g_type_fundamental_next (),
3197 "GstDoubleRange", &info, &finfo, 0);
3198 gst_value.type = gst_type_double_range;
3199 gst_value_register (&gst_value);
3203 static const GTypeValueTable value_table = {
3204 gst_value_init_list,
3205 gst_value_free_list,
3206 gst_value_copy_list,
3207 gst_value_list_peek_pointer,
3209 gst_value_collect_list,
3211 gst_value_lcopy_list
3213 static GstValueTable gst_value = {
3215 gst_value_compare_list,
3216 gst_value_serialize_list,
3217 gst_value_deserialize_list,
3220 info.value_table = &value_table;
3221 gst_type_list = g_type_register_fundamental (g_type_fundamental_next (),
3222 "GstValueList", &info, &finfo, 0);
3223 gst_value.type = gst_type_list;
3224 gst_value_register (&gst_value);
3228 static const GTypeValueTable value_table = {
3229 gst_value_init_list,
3230 gst_value_free_list,
3231 gst_value_copy_list,
3232 gst_value_list_peek_pointer,
3234 gst_value_collect_list,
3236 gst_value_lcopy_list
3238 static GstValueTable gst_value = {
3240 gst_value_compare_list,
3241 gst_value_serialize_array,
3242 gst_value_deserialize_array,
3245 info.value_table = &value_table;
3247 g_type_register_fundamental (g_type_fundamental_next (),
3248 "GstValueArray", &info, &finfo, 0);
3249 gst_value.type = gst_type_array;
3250 gst_value_register (&gst_value);
3255 static const GTypeValueTable value_table = {
3256 gst_value_init_buffer,
3258 gst_value_copy_buffer,
3261 NULL, /*gst_value_collect_buffer, */
3263 NULL /*gst_value_lcopy_buffer */
3266 static GstValueTable gst_value = {
3268 gst_value_compare_buffer,
3269 gst_value_serialize_buffer,
3270 gst_value_deserialize_buffer,
3273 gst_value.type = GST_TYPE_BUFFER;
3274 gst_value_register (&gst_value);
3277 static const GTypeValueTable value_table = {
3278 gst_value_init_fraction,
3280 gst_value_copy_fraction,
3283 gst_value_collect_fraction,
3285 gst_value_lcopy_fraction
3287 static GstValueTable gst_value = {
3289 gst_value_compare_fraction,
3290 gst_value_serialize_fraction,
3291 gst_value_deserialize_fraction,
3294 info.value_table = &value_table;
3296 g_type_register_fundamental (g_type_fundamental_next (), "GstFraction",
3298 gst_value.type = gst_type_fraction;
3299 gst_value_register (&gst_value);
3302 static GstValueTable gst_value = {
3305 gst_value_serialize_caps,
3306 gst_value_deserialize_caps,
3309 gst_value.type = GST_TYPE_CAPS;
3310 gst_value_register (&gst_value);
3313 static GstValueTable gst_value = {
3315 gst_value_compare_date,
3316 gst_value_serialize_date,
3317 gst_value_deserialize_date,
3320 /* Not using G_TYPE_DATE here on purpose, even if we could
3321 * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
3322 * serialised strings to have different type strings depending
3323 * on what version is used, so FIXME in 0.11 when we
3324 * require GLib-2.8 */
3325 gst_type_date = g_boxed_type_register_static ("GstDate",
3326 (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
3328 gst_value.type = gst_type_date;
3329 gst_value_register (&gst_value);
3332 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
3333 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
3335 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
3336 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
3337 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
3339 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
3341 REGISTER_SERIALIZATION (G_TYPE_INT, int);
3343 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
3344 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
3346 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
3347 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
3348 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
3350 g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
3351 gst_value_transform_fourcc_string);
3352 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
3353 gst_value_transform_int_range_string);
3354 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
3355 gst_value_transform_double_range_string);
3356 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
3357 gst_value_transform_list_string);
3358 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
3359 gst_value_transform_array_string);
3360 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
3361 gst_value_transform_fraction_string);
3362 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
3363 gst_value_transform_string_fraction);
3364 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
3365 gst_value_transform_fraction_double);
3366 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
3367 gst_value_transform_double_fraction);
3368 g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
3369 gst_value_transform_date_string);
3370 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
3371 gst_value_transform_string_date);
3373 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3374 gst_value_intersect_int_int_range);
3375 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3376 gst_value_intersect_int_range_int_range);
3377 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
3378 gst_value_intersect_double_double_range);
3379 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
3380 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
3381 gst_value_register_intersect_func (GST_TYPE_ARRAY,
3382 GST_TYPE_ARRAY, gst_value_intersect_array);
3384 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3385 gst_value_subtract_int_int_range);
3386 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
3387 gst_value_subtract_int_range_int);
3388 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3389 gst_value_subtract_int_range_int_range);
3390 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
3391 gst_value_subtract_double_double_range);
3392 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
3393 gst_value_subtract_double_range_double);
3394 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
3395 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
3397 #if GLIB_CHECK_VERSION(2,8,0)
3398 /* see bug #317246, #64994, #65041 */
3399 date_type = G_TYPE_DATE;
3400 GST_LOG ("Faking out the compiler: %d", date_type);
3403 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
3404 gst_value_union_int_int_range);
3405 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
3406 gst_value_union_int_range_int_range);