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
24 * GValue implementations specific to GStreamer.
26 * Note that operations on the same GstValue (or GValue) from multiple
27 * threads may lead to undefined behaviour.
29 * Last reviewed on 2006-03-07 (0.10.4)
40 #include "gst_private.h"
41 #include "glib-compat-private.h"
43 #include <gobject/gvaluecollector.h>
45 typedef struct _GstValueUnionInfo GstValueUnionInfo;
46 struct _GstValueUnionInfo
50 GstValueUnionFunc func;
53 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
54 struct _GstValueIntersectInfo
58 GstValueIntersectFunc func;
61 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
62 struct _GstValueSubtractInfo
66 GstValueSubtractFunc func;
69 GType gst_type_double_range;
70 GType gst_type_fraction_range;
73 GType gst_type_fraction;
76 static GArray *gst_value_table;
77 static GArray *gst_value_union_funcs;
78 static GArray *gst_value_intersect_funcs;
79 static GArray *gst_value_subtract_funcs;
81 /* Forward declarations */
82 static gint gst_greatest_common_divisor (gint a, gint b);
83 static gchar *gst_value_serialize_fraction (const GValue * value);
85 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
86 static gint gst_value_compare_with_func (const GValue * value1,
87 const GValue * value2, GstValueCompareFunc compare);
93 /* two helper functions to serialize/stringify any type of list
94 * regular lists are done with { }, arrays with < >
97 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
101 GArray *array = value->data[0].v_pointer;
106 s = g_string_new (begin);
107 for (i = 0; i < array->len; i++) {
108 v = &g_array_index (array, GValue, i);
109 s_val = gst_value_serialize (v);
110 g_string_append (s, s_val);
112 if (i < array->len - 1) {
113 g_string_append (s, ", ");
116 g_string_append (s, end);
117 return g_string_free (s, FALSE);
121 gst_value_transform_any_list_string (const GValue * src_value,
122 GValue * dest_value, const gchar * begin, const gchar * end)
130 array = src_value->data[0].v_pointer;
132 s = g_string_new (begin);
133 for (i = 0; i < array->len; i++) {
134 list_value = &g_array_index (array, GValue, i);
137 g_string_append (s, ", ");
139 list_s = g_strdup_value_contents (list_value);
140 g_string_append (s, list_s);
143 g_string_append (s, end);
145 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
149 * helper function to see if a type is fixed. Is used internally here and
150 * there. Do not export, since it doesn't work for types where the content
151 * decides the fixedness (e.g. GST_TYPE_ARRAY).
155 gst_type_is_fixed (GType type)
157 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
158 type == GST_TYPE_LIST) {
161 if (G_TYPE_FUNDAMENTAL (type) <=
162 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
165 if (type == GST_TYPE_BUFFER || type == GST_TYPE_FOURCC
166 || type == GST_TYPE_ARRAY || type == GST_TYPE_FRACTION) {
173 /* GValue functions usable for both regular lists and arrays */
175 gst_value_init_list_or_array (GValue * value)
177 value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
181 copy_garray_of_gstvalue (const GArray * src)
186 dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
187 g_array_set_size (dest, src->len);
188 for (i = 0; i < src->len; i++) {
189 gst_value_init_and_copy (&g_array_index (dest, GValue, i),
190 &g_array_index (src, GValue, i));
197 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
199 dest_value->data[0].v_pointer =
200 copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
204 gst_value_free_list_or_array (GValue * value)
207 GArray *src = (GArray *) value->data[0].v_pointer;
209 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
210 for (i = 0; i < src->len; i++) {
211 g_value_unset (&g_array_index (src, GValue, i));
213 g_array_free (src, TRUE);
218 gst_value_list_or_array_peek_pointer (const GValue * value)
220 return value->data[0].v_pointer;
224 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
225 GTypeCValue * collect_values, guint collect_flags)
227 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
228 value->data[0].v_pointer = collect_values[0].v_pointer;
229 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
231 value->data[0].v_pointer =
232 copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
238 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
239 GTypeCValue * collect_values, guint collect_flags)
241 GArray **dest = collect_values[0].v_pointer;
244 return g_strdup_printf ("value location for `%s' passed as NULL",
245 G_VALUE_TYPE_NAME (value));
246 if (!value->data[0].v_pointer)
247 return g_strdup_printf ("invalid value given for `%s'",
248 G_VALUE_TYPE_NAME (value));
249 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
250 *dest = (GArray *) value->data[0].v_pointer;
252 *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
258 * gst_value_list_append_value:
259 * @value: a #GValue of type #GST_TYPE_LIST
260 * @append_value: the value to append
262 * Appends @append_value to the GstValueList in @value.
265 gst_value_list_append_value (GValue * value, const GValue * append_value)
269 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
271 gst_value_init_and_copy (&val, append_value);
272 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
276 * gst_value_list_prepend_value:
277 * @value: a #GValue of type #GST_TYPE_LIST
278 * @prepend_value: the value to prepend
280 * Prepends @prepend_value to the GstValueList in @value.
283 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
287 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
289 gst_value_init_and_copy (&val, prepend_value);
290 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
294 * gst_value_list_concat:
295 * @dest: an uninitialized #GValue to take the result
299 * Concatenates copies of @value1 and @value2 into a list. Values that are not
300 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
301 * @dest will be initialized to the type #GST_TYPE_LIST.
304 gst_value_list_concat (GValue * dest, const GValue * value1,
305 const GValue * value2)
307 guint i, value1_length, value2_length;
310 g_return_if_fail (dest != NULL);
311 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
312 g_return_if_fail (G_IS_VALUE (value1));
313 g_return_if_fail (G_IS_VALUE (value2));
316 (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
318 (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
319 g_value_init (dest, GST_TYPE_LIST);
320 array = (GArray *) dest->data[0].v_pointer;
321 g_array_set_size (array, value1_length + value2_length);
323 if (GST_VALUE_HOLDS_LIST (value1)) {
324 for (i = 0; i < value1_length; i++) {
325 gst_value_init_and_copy (&g_array_index (array, GValue, i),
326 gst_value_list_get_value (value1, i));
329 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
332 if (GST_VALUE_HOLDS_LIST (value2)) {
333 for (i = 0; i < value2_length; i++) {
334 gst_value_init_and_copy (&g_array_index (array, GValue,
335 i + value1_length), gst_value_list_get_value (value2, i));
338 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
344 * gst_value_list_get_size:
345 * @value: a #GValue of type #GST_TYPE_LIST
347 * Gets the number of values contained in @value.
349 * Returns: the number of values
352 gst_value_list_get_size (const GValue * value)
354 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
356 return ((GArray *) value->data[0].v_pointer)->len;
360 * gst_value_list_get_value:
361 * @value: a #GValue of type #GST_TYPE_LIST
362 * @index: index of value to get from the list
364 * Gets the value that is a member of the list contained in @value and
365 * has the index @index.
367 * Returns: the value at the given index
370 gst_value_list_get_value (const GValue * value, guint index)
372 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
373 g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
375 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
380 * gst_value_array_append_value:
381 * @value: a #GValue of type #GST_TYPE_ARRAY
382 * @append_value: the value to append
384 * Appends @append_value to the GstValueArray in @value.
387 gst_value_array_append_value (GValue * value, const GValue * append_value)
391 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
393 gst_value_init_and_copy (&val, append_value);
394 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
398 * gst_value_array_prepend_value:
399 * @value: a #GValue of type #GST_TYPE_ARRAY
400 * @prepend_value: the value to prepend
402 * Prepends @prepend_value to the GstValueArray in @value.
405 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
409 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
411 gst_value_init_and_copy (&val, prepend_value);
412 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
416 * gst_value_array_get_size:
417 * @value: a #GValue of type #GST_TYPE_ARRAY
419 * Gets the number of values contained in @value.
421 * Returns: the number of values
424 gst_value_array_get_size (const GValue * value)
426 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
428 return ((GArray *) value->data[0].v_pointer)->len;
432 * gst_value_array_get_value:
433 * @value: a #GValue of type #GST_TYPE_ARRAY
434 * @index: index of value to get from the array
436 * Gets the value that is a member of the array contained in @value and
437 * has the index @index.
439 * Returns: the value at the given index
442 gst_value_array_get_value (const GValue * value, guint index)
444 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
445 g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
447 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
452 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
454 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
458 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
460 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
463 /* Do an unordered compare of the contents of a list */
465 gst_value_compare_list (const GValue * value1, const GValue * value2)
468 GArray *array1 = value1->data[0].v_pointer;
469 GArray *array2 = value2->data[0].v_pointer;
474 GstValueCompareFunc compare;
476 /* get length and do initial length check. */
478 if (len != array2->len)
479 return GST_VALUE_UNORDERED;
481 /* place to mark removed value indices of array2 */
482 removed = g_newa (guint8, len);
483 memset (removed, 0, len);
486 /* loop over array1, all items should be in array2. When we find an
487 * item in array2, remove it from array2 by marking it as removed */
488 for (i = 0; i < len; i++) {
489 v1 = &g_array_index (array1, GValue, i);
490 if ((compare = gst_value_get_compare_func (v1))) {
491 for (j = 0; j < len; j++) {
492 /* item is removed, we can skip it */
495 v2 = &g_array_index (array2, GValue, j);
496 if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
497 /* mark item as removed now that we found it in array2 and
498 * decrement the number of remaining items in array2. */
504 /* item in array1 and not in array2, UNORDERED */
506 return GST_VALUE_UNORDERED;
508 return GST_VALUE_UNORDERED;
510 /* if not all items were removed, array2 contained something not in array1 */
512 return GST_VALUE_UNORDERED;
514 /* arrays are equal */
515 return GST_VALUE_EQUAL;
518 /* Perform an ordered comparison of the contents of an array */
520 gst_value_compare_array (const GValue * value1, const GValue * value2)
523 GArray *array1 = value1->data[0].v_pointer;
524 GArray *array2 = value2->data[0].v_pointer;
528 if (array1->len != array2->len)
529 return GST_VALUE_UNORDERED;
531 for (i = 0; i < array1->len; i++) {
532 v1 = &g_array_index (array1, GValue, i);
533 v2 = &g_array_index (array2, GValue, i);
534 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
535 return GST_VALUE_UNORDERED;
538 return GST_VALUE_EQUAL;
542 gst_value_serialize_list (const GValue * value)
544 return gst_value_serialize_any_list (value, "{ ", " }");
548 gst_value_deserialize_list (GValue * dest, const gchar * s)
550 g_warning ("gst_value_deserialize_list: unimplemented");
555 gst_value_serialize_array (const GValue * value)
557 return gst_value_serialize_any_list (value, "< ", " >");
561 gst_value_deserialize_array (GValue * dest, const gchar * s)
563 g_warning ("gst_value_deserialize_array: unimplemented");
572 gst_value_init_fourcc (GValue * value)
574 value->data[0].v_int = 0;
578 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
580 dest_value->data[0].v_int = src_value->data[0].v_int;
584 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
585 GTypeCValue * collect_values, guint collect_flags)
587 value->data[0].v_int = collect_values[0].v_int;
593 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
594 GTypeCValue * collect_values, guint collect_flags)
596 guint32 *fourcc_p = collect_values[0].v_pointer;
599 return g_strdup_printf ("value location for `%s' passed as NULL",
600 G_VALUE_TYPE_NAME (value));
602 *fourcc_p = value->data[0].v_int;
608 * gst_value_set_fourcc:
609 * @value: a GValue initialized to #GST_TYPE_FOURCC
610 * @fourcc: the #guint32 fourcc to set
612 * Sets @value to @fourcc.
615 gst_value_set_fourcc (GValue * value, guint32 fourcc)
617 g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
619 value->data[0].v_int = fourcc;
623 * gst_value_get_fourcc:
624 * @value: a GValue initialized to #GST_TYPE_FOURCC
626 * Gets the #guint32 fourcc contained in @value.
628 * Returns: the #guint32 fourcc contained in @value.
631 gst_value_get_fourcc (const GValue * value)
633 g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
635 return value->data[0].v_int;
639 gst_value_transform_fourcc_string (const GValue * src_value,
642 guint32 fourcc = src_value->data[0].v_int;
644 if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
645 g_ascii_isprint ((fourcc >> 8) & 0xff) &&
646 g_ascii_isprint ((fourcc >> 16) & 0xff) &&
647 g_ascii_isprint ((fourcc >> 24) & 0xff)) {
648 dest_value->data[0].v_pointer =
649 g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
651 dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
656 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
658 if (value2->data[0].v_int == value1->data[0].v_int)
659 return GST_VALUE_EQUAL;
660 return GST_VALUE_UNORDERED;
664 gst_value_serialize_fourcc (const GValue * value)
666 guint32 fourcc = value->data[0].v_int;
668 if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
669 g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
670 g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
671 g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
672 return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
674 return g_strdup_printf ("0x%08x", fourcc);
679 gst_value_deserialize_fourcc (GValue * dest, const char *s)
681 gboolean ret = FALSE;
685 if (strlen (s) == 4) {
686 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
688 } else if (g_ascii_isdigit (*s)) {
689 fourcc = strtoul (s, &end, 0);
694 gst_value_set_fourcc (dest, fourcc);
704 gst_value_init_int_range (GValue * value)
706 value->data[0].v_int = 0;
707 value->data[1].v_int = 0;
711 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
713 dest_value->data[0].v_int = src_value->data[0].v_int;
714 dest_value->data[1].v_int = src_value->data[1].v_int;
718 gst_value_collect_int_range (GValue * value, guint n_collect_values,
719 GTypeCValue * collect_values, guint collect_flags)
721 value->data[0].v_int = collect_values[0].v_int;
722 value->data[1].v_int = collect_values[1].v_int;
728 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
729 GTypeCValue * collect_values, guint collect_flags)
731 guint32 *int_range_start = collect_values[0].v_pointer;
732 guint32 *int_range_end = collect_values[1].v_pointer;
734 if (!int_range_start)
735 return g_strdup_printf ("start value location for `%s' passed as NULL",
736 G_VALUE_TYPE_NAME (value));
738 return g_strdup_printf ("end value location for `%s' passed as NULL",
739 G_VALUE_TYPE_NAME (value));
741 *int_range_start = value->data[0].v_int;
742 *int_range_end = value->data[1].v_int;
748 * gst_value_set_int_range:
749 * @value: a GValue initialized to GST_TYPE_INT_RANGE
750 * @start: the start of the range
751 * @end: the end of the range
753 * Sets @value to the range specified by @start and @end.
756 gst_value_set_int_range (GValue * value, gint start, gint end)
758 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
759 g_return_if_fail (start < end);
761 value->data[0].v_int = start;
762 value->data[1].v_int = end;
766 * gst_value_get_int_range_min:
767 * @value: a GValue initialized to GST_TYPE_INT_RANGE
769 * Gets the minimum of the range specified by @value.
771 * Returns: the minimum of the range
774 gst_value_get_int_range_min (const GValue * value)
776 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
778 return value->data[0].v_int;
782 * gst_value_get_int_range_max:
783 * @value: a GValue initialized to GST_TYPE_INT_RANGE
785 * Gets the maximum of the range specified by @value.
787 * Returns: the maxumum of the range
790 gst_value_get_int_range_max (const GValue * value)
792 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
794 return value->data[1].v_int;
798 gst_value_transform_int_range_string (const GValue * src_value,
801 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
802 (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
806 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
808 if (value2->data[0].v_int == value1->data[0].v_int &&
809 value2->data[1].v_int == value1->data[1].v_int)
810 return GST_VALUE_EQUAL;
811 return GST_VALUE_UNORDERED;
815 gst_value_serialize_int_range (const GValue * value)
817 return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
818 value->data[1].v_int);
822 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
824 g_warning ("unimplemented");
833 gst_value_init_double_range (GValue * value)
835 value->data[0].v_double = 0;
836 value->data[1].v_double = 0;
840 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
842 dest_value->data[0].v_double = src_value->data[0].v_double;
843 dest_value->data[1].v_double = src_value->data[1].v_double;
847 gst_value_collect_double_range (GValue * value, guint n_collect_values,
848 GTypeCValue * collect_values, guint collect_flags)
850 value->data[0].v_double = collect_values[0].v_double;
851 value->data[1].v_double = collect_values[1].v_double;
857 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
858 GTypeCValue * collect_values, guint collect_flags)
860 gdouble *double_range_start = collect_values[0].v_pointer;
861 gdouble *double_range_end = collect_values[1].v_pointer;
863 if (!double_range_start)
864 return g_strdup_printf ("start value location for `%s' passed as NULL",
865 G_VALUE_TYPE_NAME (value));
866 if (!double_range_end)
867 return g_strdup_printf ("end value location for `%s' passed as NULL",
868 G_VALUE_TYPE_NAME (value));
870 *double_range_start = value->data[0].v_double;
871 *double_range_end = value->data[1].v_double;
877 * gst_value_set_double_range:
878 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
879 * @start: the start of the range
880 * @end: the end of the range
882 * Sets @value to the range specified by @start and @end.
885 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
887 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
889 value->data[0].v_double = start;
890 value->data[1].v_double = end;
894 * gst_value_get_double_range_min:
895 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
897 * Gets the minimum of the range specified by @value.
899 * Returns: the minumum of the range
902 gst_value_get_double_range_min (const GValue * value)
904 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
906 return value->data[0].v_double;
910 * gst_value_get_double_range_max:
911 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
913 * Gets the maximum of the range specified by @value.
915 * Returns: the maxumum of the range
918 gst_value_get_double_range_max (const GValue * value)
920 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
922 return value->data[1].v_double;
926 gst_value_transform_double_range_string (const GValue * src_value,
929 char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
931 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
932 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
933 src_value->data[0].v_double),
934 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
935 src_value->data[1].v_double));
939 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
941 if (value2->data[0].v_double == value1->data[0].v_double &&
942 value2->data[0].v_double == value1->data[0].v_double)
943 return GST_VALUE_EQUAL;
944 return GST_VALUE_UNORDERED;
948 gst_value_serialize_double_range (const GValue * value)
950 char d1[G_ASCII_DTOSTR_BUF_SIZE];
951 char d2[G_ASCII_DTOSTR_BUF_SIZE];
953 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
954 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
955 return g_strdup_printf ("[ %s, %s ]", d1, d2);
959 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
961 g_warning ("unimplemented");
970 gst_value_init_fraction_range (GValue * value)
974 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
975 g_value_init (&vals[0], GST_TYPE_FRACTION);
976 g_value_init (&vals[1], GST_TYPE_FRACTION);
980 gst_value_free_fraction_range (GValue * value)
982 GValue *vals = (GValue *) value->data[0].v_pointer;
985 g_value_unset (&vals[0]);
986 g_value_unset (&vals[1]);
988 value->data[0].v_pointer = NULL;
993 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
995 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
996 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
999 dest_value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1000 g_return_if_fail (vals != NULL);
1001 g_value_init (&vals[0], GST_TYPE_FRACTION);
1002 g_value_init (&vals[1], GST_TYPE_FRACTION);
1005 if (src_vals != NULL) {
1006 g_value_copy (&src_vals[0], &vals[0]);
1007 g_value_copy (&src_vals[1], &vals[1]);
1012 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1013 GTypeCValue * collect_values, guint collect_flags)
1015 GValue *vals = (GValue *) value->data[0].v_pointer;
1017 if (n_collect_values != 4)
1018 return g_strdup_printf ("not enough value locations for `%s' passed",
1019 G_VALUE_TYPE_NAME (value));
1021 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1023 return g_strdup_printf ("Could not initialise`%s' during collect",
1024 G_VALUE_TYPE_NAME (value));
1025 g_value_init (&vals[0], GST_TYPE_FRACTION);
1026 g_value_init (&vals[1], GST_TYPE_FRACTION);
1029 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1030 collect_values[1].v_int);
1031 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1032 collect_values[3].v_int);
1038 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1039 GTypeCValue * collect_values, guint collect_flags)
1042 int *dest_values[4];
1043 GValue *vals = (GValue *) value->data[0].v_pointer;
1045 if (n_collect_values != 4)
1046 return g_strdup_printf ("not enough value locations for `%s' passed",
1047 G_VALUE_TYPE_NAME (value));
1049 for (i = 0; i < 4; i++) {
1050 if (collect_values[i].v_pointer == NULL) {
1051 return g_strdup_printf ("value location for `%s' passed as NULL",
1052 G_VALUE_TYPE_NAME (value));
1054 dest_values[i] = collect_values[i].v_pointer;
1058 return g_strdup_printf ("Uninitialised `%s' passed",
1059 G_VALUE_TYPE_NAME (value));
1062 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1063 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1064 dest_values[2][0] = gst_value_get_fraction_denominator (&vals[1]);
1065 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1070 * gst_value_set_fraction_range:
1071 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1072 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1073 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1075 * Sets @value to the range specified by @start and @end.
1078 gst_value_set_fraction_range (GValue * value, const GValue * start,
1083 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1085 vals = (GValue *) value->data[0].v_pointer;
1087 value->data[0].v_pointer = vals = g_new0 (GValue, 2);
1088 g_value_init (&vals[0], GST_TYPE_FRACTION);
1089 g_value_init (&vals[1], GST_TYPE_FRACTION);
1092 g_value_copy (start, &vals[0]);
1093 g_value_copy (end, &vals[1]);
1097 * gst_value_set_fraction_range_full:
1098 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1099 * @numerator_start: the numerator start of the range
1100 * @denominator_start: the denominator start of the range
1101 * @numerator_end: the numerator end of the range
1102 * @denominator_end: the denominator end of the range
1104 * Sets @value to the range specified by @numerator_start/@denominator_start
1105 * and @numerator_end/@denominator_end.
1108 gst_value_set_fraction_range_full (GValue * value,
1109 gint numerator_start, gint denominator_start,
1110 gint numerator_end, gint denominator_end)
1112 GValue start = { 0 };
1115 g_value_init (&start, GST_TYPE_FRACTION);
1116 g_value_init (&end, GST_TYPE_FRACTION);
1118 gst_value_set_fraction (&start, numerator_start, denominator_start);
1119 gst_value_set_fraction (&end, numerator_end, denominator_end);
1120 gst_value_set_fraction_range (value, &start, &end);
1122 g_value_unset (&start);
1123 g_value_unset (&end);
1127 * gst_value_get_fraction_range_min:
1128 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1130 * Gets the minimum of the range specified by @value.
1132 * Returns: the minumum of the range
1135 gst_value_get_fraction_range_min (const GValue * value)
1139 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1141 vals = (GValue *) value->data[0].v_pointer;
1150 * gst_value_get_fraction_range_max:
1151 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1153 * Gets the maximum of the range specified by @value.
1155 * Returns: the maximum of the range
1158 gst_value_get_fraction_range_max (const GValue * value)
1162 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
1164 vals = (GValue *) value->data[0].v_pointer;
1173 gst_value_serialize_fraction_range (const GValue * value)
1175 GValue *vals = (GValue *) value->data[0].v_pointer;
1179 retval = g_strdup ("[ 0/1, 0/1 ]");
1183 start = gst_value_serialize_fraction (&vals[0]);
1184 end = gst_value_serialize_fraction (&vals[1]);
1186 retval = g_strdup_printf ("[ %s, %s ]", start, end);
1195 gst_value_transform_fraction_range_string (const GValue * src_value,
1196 GValue * dest_value)
1198 dest_value->data[0].v_pointer =
1199 gst_value_serialize_fraction_range (src_value);
1203 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1205 GValue *vals1, *vals2;
1206 GstValueCompareFunc compare;
1208 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1209 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
1211 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1212 return GST_VALUE_UNORDERED;
1214 vals1 = (GValue *) value1->data[0].v_pointer;
1215 vals2 = (GValue *) value2->data[0].v_pointer;
1216 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1217 if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1219 gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1221 return GST_VALUE_EQUAL;
1223 return GST_VALUE_UNORDERED;
1227 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1229 g_warning ("unimplemented");
1238 * gst_value_set_caps:
1239 * @value: a GValue initialized to GST_TYPE_CAPS
1240 * @caps: the caps to set the value to
1242 * Sets the contents of @value to coorespond to @caps. The actual
1243 * #GstCaps structure is copied before it is used.
1246 gst_value_set_caps (GValue * value, const GstCaps * caps)
1248 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1250 g_value_set_boxed (value, caps);
1254 * gst_value_get_caps:
1255 * @value: a GValue initialized to GST_TYPE_CAPS
1257 * Gets the contents of @value.
1259 * Returns: the contents of @value
1262 gst_value_get_caps (const GValue * value)
1264 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1266 return (GstCaps *) g_value_get_boxed (value);
1270 gst_value_serialize_caps (const GValue * value)
1272 GstCaps *caps = g_value_get_boxed (value);
1274 return gst_caps_to_string (caps);
1278 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1282 caps = gst_caps_from_string (s);
1285 g_value_set_boxed (dest, caps);
1297 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1299 GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
1300 GstBuffer *buf2 = GST_BUFFER (gst_value_get_mini_object (value2));
1302 if (GST_BUFFER_SIZE (buf1) != GST_BUFFER_SIZE (buf2))
1303 return GST_VALUE_UNORDERED;
1304 if (GST_BUFFER_SIZE (buf1) == 0)
1305 return GST_VALUE_EQUAL;
1306 g_assert (GST_BUFFER_DATA (buf1));
1307 g_assert (GST_BUFFER_DATA (buf2));
1308 if (memcmp (GST_BUFFER_DATA (buf1), GST_BUFFER_DATA (buf2),
1309 GST_BUFFER_SIZE (buf1)) == 0)
1310 return GST_VALUE_EQUAL;
1312 return GST_VALUE_UNORDERED;
1316 gst_value_serialize_buffer (const GValue * value)
1324 buffer = gst_value_get_buffer (value);
1328 data = GST_BUFFER_DATA (buffer);
1329 size = GST_BUFFER_SIZE (buffer);
1331 string = g_malloc (size * 2 + 1);
1332 for (i = 0; i < size; i++) {
1333 sprintf (string + i * 2, "%02x", data[i]);
1335 string[size * 2] = 0;
1341 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1353 buffer = gst_buffer_new_and_alloc (len / 2);
1354 data = GST_BUFFER_DATA (buffer);
1355 for (i = 0; i < len / 2; i++) {
1356 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1359 ts[0] = s[i * 2 + 0];
1360 ts[1] = s[i * 2 + 1];
1363 data[i] = (guint8) strtoul (ts, NULL, 16);
1366 gst_value_take_buffer (dest, buffer);
1377 gst_buffer_unref (buffer);
1388 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1390 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1391 return GST_VALUE_EQUAL;
1392 return GST_VALUE_UNORDERED;
1396 gst_value_serialize_boolean (const GValue * value)
1398 if (value->data[0].v_int) {
1399 return g_strdup ("true");
1401 return g_strdup ("false");
1405 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1407 gboolean ret = FALSE;
1409 if (g_ascii_strcasecmp (s, "true") == 0 ||
1410 g_ascii_strcasecmp (s, "yes") == 0 ||
1411 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1412 g_value_set_boolean (dest, TRUE);
1414 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1415 g_ascii_strcasecmp (s, "no") == 0 ||
1416 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1417 g_value_set_boolean (dest, FALSE);
1424 #define CREATE_SERIALIZATION_START(_type,_macro) \
1426 gst_value_compare_ ## _type \
1427 (const GValue * value1, const GValue * value2) \
1429 g ## _type val1 = g_value_get_ ## _type (value1); \
1430 g ## _type val2 = g_value_get_ ## _type (value2); \
1432 return GST_VALUE_GREATER_THAN; \
1434 return GST_VALUE_LESS_THAN; \
1435 return GST_VALUE_EQUAL; \
1439 gst_value_serialize_ ## _type (const GValue * value) \
1441 GValue val = { 0, }; \
1442 g_value_init (&val, G_TYPE_STRING); \
1443 if (!g_value_transform (value, &val)) \
1444 g_assert_not_reached (); \
1445 /* NO_COPY_MADNESS!!! */ \
1446 return (char *) g_value_get_string (&val); \
1449 /* deserialize the given s into to as a gint64.
1450 * check if the result is actually storeable in the given size number of
1454 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1455 gint64 min, gint64 max, gint size)
1457 gboolean ret = FALSE;
1462 *to = g_ascii_strtoull (s, &end, 0);
1463 /* a range error is a definitive no-no */
1464 if (errno == ERANGE) {
1471 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1472 *to = G_LITTLE_ENDIAN;
1474 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1477 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1480 } else if (g_ascii_strcasecmp (s, "min") == 0) {
1483 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1489 /* by definition, a gint64 fits into a gint64; so ignore those */
1490 if (size != sizeof (mask)) {
1492 /* for positive numbers, we create a mask of 1's outside of the range
1493 * and 0's inside the range. An and will thus keep only 1 bits
1494 * outside of the range */
1495 mask <<= (size * 8);
1496 if ((mask & *to) != 0) {
1500 /* for negative numbers, we do a 2's complement version */
1501 mask <<= ((size * 8) - 1);
1502 if ((mask & *to) != mask) {
1511 #define CREATE_SERIALIZATION(_type,_macro) \
1512 CREATE_SERIALIZATION_START(_type,_macro) \
1515 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1519 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
1520 G_MAX ## _macro, sizeof (g ## _type))) { \
1521 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
1528 #define CREATE_USERIALIZATION(_type,_macro) \
1529 CREATE_SERIALIZATION_START(_type,_macro) \
1532 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1536 gboolean ret = FALSE; \
1539 x = g_ascii_strtoull (s, &end, 0); \
1540 /* a range error is a definitive no-no */ \
1541 if (errno == ERANGE) { \
1544 /* the cast ensures the range check later on makes sense */ \
1545 x = (g ## _type) x; \
1549 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
1550 x = G_LITTLE_ENDIAN; \
1552 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
1555 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
1558 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
1561 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
1562 x = G_MAX ## _macro; \
1567 if (x > G_MAX ## _macro) { \
1570 g_value_set_ ## _type (dest, x); \
1576 #define REGISTER_SERIALIZATION(_gtype, _type) \
1578 static const GstValueTable gst_value = { \
1580 gst_value_compare_ ## _type, \
1581 gst_value_serialize_ ## _type, \
1582 gst_value_deserialize_ ## _type, \
1585 gst_value_register (&gst_value); \
1588 CREATE_SERIALIZATION (int, INT);
1589 CREATE_SERIALIZATION (int64, INT64);
1590 CREATE_SERIALIZATION (long, LONG);
1592 CREATE_USERIALIZATION (uint, UINT);
1593 CREATE_USERIALIZATION (uint64, UINT64);
1594 CREATE_USERIALIZATION (ulong, ULONG);
1600 gst_value_compare_double (const GValue * value1, const GValue * value2)
1602 if (value1->data[0].v_double > value2->data[0].v_double)
1603 return GST_VALUE_GREATER_THAN;
1604 if (value1->data[0].v_double < value2->data[0].v_double)
1605 return GST_VALUE_LESS_THAN;
1606 if (value1->data[0].v_double == value2->data[0].v_double)
1607 return GST_VALUE_EQUAL;
1608 return GST_VALUE_UNORDERED;
1612 gst_value_serialize_double (const GValue * value)
1614 char d[G_ASCII_DTOSTR_BUF_SIZE];
1616 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1617 return g_strdup (d);
1621 gst_value_deserialize_double (GValue * dest, const gchar * s)
1624 gboolean ret = FALSE;
1627 x = g_ascii_strtod (s, &end);
1631 if (g_ascii_strcasecmp (s, "min") == 0) {
1634 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1640 g_value_set_double (dest, x);
1650 gst_value_compare_float (const GValue * value1, const GValue * value2)
1652 if (value1->data[0].v_float > value2->data[0].v_float)
1653 return GST_VALUE_GREATER_THAN;
1654 if (value1->data[0].v_float < value2->data[0].v_float)
1655 return GST_VALUE_LESS_THAN;
1656 if (value1->data[0].v_float == value2->data[0].v_float)
1657 return GST_VALUE_EQUAL;
1658 return GST_VALUE_UNORDERED;
1662 gst_value_serialize_float (const GValue * value)
1664 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
1666 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
1667 return g_strdup (d);
1671 gst_value_deserialize_float (GValue * dest, const gchar * s)
1674 gboolean ret = FALSE;
1677 x = g_ascii_strtod (s, &end);
1681 if (g_ascii_strcasecmp (s, "min") == 0) {
1684 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1689 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
1692 g_value_set_float (dest, (float) x);
1702 gst_value_compare_string (const GValue * value1, const GValue * value2)
1704 int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
1707 return GST_VALUE_LESS_THAN;
1709 return GST_VALUE_GREATER_THAN;
1710 return GST_VALUE_EQUAL;
1713 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1714 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1718 gst_string_wrap (const gchar * s)
1723 gboolean wrap = FALSE;
1730 if (GST_ASCII_IS_STRING (*t)) {
1732 } else if (*t < 0x20 || *t >= 0x7f) {
1743 return g_strdup (s);
1745 e = d = g_malloc (len + 3);
1750 if (GST_ASCII_IS_STRING (*t)) {
1752 } else if (*t < 0x20 || *t >= 0x7f) {
1754 *e++ = '0' + ((*(guchar *) t) >> 6);
1755 *e++ = '0' + (((*t) >> 3) & 0x7);
1756 *e++ = '0' + ((*t++) & 0x7);
1769 * This function takes a string delimited with double quotes (")
1770 * and unescapes any \xxx octal numbers.
1772 * If sequences of \y are found where y is not in the range of
1773 * 0->3, y is copied unescaped.
1775 * If \xyy is found where x is an octal number but y is not, an
1776 * error is encountered and NULL is returned.
1778 * the input string must be \0 terminated.
1781 gst_string_unwrap (const gchar * s)
1784 gchar *read, *write;
1786 /* NULL string returns NULL */
1790 /* strings not starting with " are invalid */
1794 /* make copy of original string to hold the result. This
1795 * string will always be smaller than the original */
1800 /* need to move to the next position as we parsed the " */
1804 if (GST_ASCII_IS_STRING (*read)) {
1805 /* normal chars are just copied */
1807 } else if (*read == '"') {
1808 /* quote marks end of string */
1810 } else if (*read == '\\') {
1811 /* got an escape char, move to next position to read a tripplet
1812 * of octal numbers */
1814 /* is the next char a possible first octal number? */
1815 if (*read >= '0' && *read <= '3') {
1816 /* parse other 2 numbers, if one of them is not in the range of
1817 * an octal number, we error. We also catch the case where a zero
1818 * byte is found here. */
1819 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
1822 /* now convert the octal number to a byte again. */
1823 *write++ = ((read[0] - '0') << 6) +
1824 ((read[1] - '0') << 3) + (read[2] - '0');
1828 /* if we run into a \0 here, we definately won't get a quote later */
1832 /* else copy \X sequence */
1836 /* weird character, error */
1840 /* if the string is not ending in " and zero terminated, we error */
1841 if (*read != '"' || read[1] != '\0')
1844 /* null terminate result string and return */
1854 gst_value_serialize_string (const GValue * value)
1856 return gst_string_wrap (value->data[0].v_pointer);
1860 gst_value_deserialize_string (GValue * dest, const gchar * s)
1863 if (!g_utf8_validate (s, -1, NULL))
1865 g_value_set_string (dest, s);
1868 gchar *str = gst_string_unwrap (s);
1872 g_value_take_string (dest, str);
1883 gst_value_compare_enum (const GValue * value1, const GValue * value2)
1885 GEnumValue *en1, *en2;
1886 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1887 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1889 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1890 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1891 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
1892 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
1893 g_type_class_unref (klass1);
1894 g_type_class_unref (klass2);
1895 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
1896 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
1897 if (en1->value < en2->value)
1898 return GST_VALUE_LESS_THAN;
1899 if (en1->value > en2->value)
1900 return GST_VALUE_GREATER_THAN;
1902 return GST_VALUE_EQUAL;
1906 gst_value_serialize_enum (const GValue * value)
1909 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
1911 g_return_val_if_fail (klass, NULL);
1912 en = g_enum_get_value (klass, g_value_get_enum (value));
1913 g_type_class_unref (klass);
1915 /* might be one of the custom formats registered later */
1916 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
1917 const GstFormatDefinition *format_def;
1919 format_def = gst_format_get_details (g_value_get_enum (value));
1920 g_return_val_if_fail (format_def != NULL, NULL);
1921 return g_strdup (format_def->description);
1924 g_return_val_if_fail (en, NULL);
1925 return g_strdup (en->value_name);
1929 gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
1932 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
1935 return g_ascii_strcasecmp (s, format_def->description);
1939 gst_value_deserialize_enum (GValue * dest, const gchar * s)
1942 gchar *endptr = NULL;
1943 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
1945 g_return_val_if_fail (klass, FALSE);
1946 if (!(en = g_enum_get_value_by_name (klass, s))) {
1947 if (!(en = g_enum_get_value_by_nick (klass, s))) {
1948 gint i = strtol (s, &endptr, 0);
1950 if (endptr && *endptr == '\0') {
1951 en = g_enum_get_value (klass, i);
1955 g_type_class_unref (klass);
1957 /* might be one of the custom formats registered later */
1958 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
1959 const GstFormatDefinition *format_def;
1962 iter = gst_format_iterate_definitions ();
1964 format_def = gst_iterator_find_custom (iter,
1965 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
1967 g_return_val_if_fail (format_def != NULL, FALSE);
1968 g_value_set_enum (dest, (gint) format_def->value);
1969 gst_iterator_free (iter);
1973 g_return_val_if_fail (en, FALSE);
1974 g_value_set_enum (dest, en->value);
1982 /* we just compare the value here */
1984 gst_value_compare_flags (const GValue * value1, const GValue * value2)
1987 GFlagsClass *klass1 =
1988 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
1989 GFlagsClass *klass2 =
1990 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
1992 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
1993 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
1994 fl1 = g_value_get_flags (value1);
1995 fl2 = g_value_get_flags (value2);
1996 g_type_class_unref (klass1);
1997 g_type_class_unref (klass2);
1999 return GST_VALUE_LESS_THAN;
2001 return GST_VALUE_GREATER_THAN;
2003 return GST_VALUE_EQUAL;
2006 /* the different flags are serialized separated with a + */
2008 gst_value_serialize_flags (const GValue * value)
2012 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2013 gchar *result, *tmp;
2014 gboolean first = TRUE;
2016 g_return_val_if_fail (klass, NULL);
2018 flags = g_value_get_flags (value);
2020 /* if no flags are set, try to serialize to the _NONE string */
2022 fl = g_flags_get_first_value (klass, flags);
2023 return g_strdup (fl->value_name);
2026 /* some flags are set, so serialize one by one */
2027 result = g_strdup ("");
2029 fl = g_flags_get_first_value (klass, flags);
2031 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2037 flags &= ~fl->value;
2040 g_type_class_unref (klass);
2046 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2049 gchar *endptr = NULL;
2050 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2055 g_return_val_if_fail (klass, FALSE);
2057 /* split into parts delimited with + */
2058 split = g_strsplit (s, "+", 0);
2062 /* loop over each part */
2064 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2065 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2066 gint val = strtol (split[i], &endptr, 0);
2068 /* just or numeric value */
2069 if (endptr && *endptr == '\0') {
2080 g_type_class_unref (klass);
2081 g_value_set_flags (dest, flags);
2091 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2092 const GValue * src2)
2094 if (src2->data[0].v_int <= src1->data[0].v_int &&
2095 src2->data[1].v_int >= src1->data[0].v_int) {
2096 gst_value_init_and_copy (dest, src2);
2103 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2104 const GValue * src2)
2109 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2110 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2113 g_value_init (dest, GST_TYPE_INT_RANGE);
2114 gst_value_set_int_range (dest,
2115 MIN (src1->data[0].v_int, src2->data[0].v_int),
2116 MAX (src1->data[1].v_int, src2->data[1].v_int));
2128 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2129 const GValue * src2)
2131 if (src2->data[0].v_int <= src1->data[0].v_int &&
2132 src2->data[1].v_int >= src1->data[0].v_int) {
2133 gst_value_init_and_copy (dest, src1);
2141 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2142 const GValue * src2)
2147 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2148 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2151 g_value_init (dest, GST_TYPE_INT_RANGE);
2152 gst_value_set_int_range (dest, min, max);
2156 g_value_init (dest, G_TYPE_INT);
2157 g_value_set_int (dest, min);
2165 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2166 const GValue * src2)
2168 if (src2->data[0].v_double <= src1->data[0].v_double &&
2169 src2->data[1].v_double >= src1->data[0].v_double) {
2170 gst_value_init_and_copy (dest, src1);
2178 gst_value_intersect_double_range_double_range (GValue * dest,
2179 const GValue * src1, const GValue * src2)
2184 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2185 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2188 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2189 gst_value_set_double_range (dest, min, max);
2193 g_value_init (dest, G_TYPE_DOUBLE);
2194 g_value_set_int (dest, (int) min);
2202 gst_value_intersect_list (GValue * dest, const GValue * value1,
2203 const GValue * value2)
2206 GValue intersection = { 0, };
2207 gboolean ret = FALSE;
2209 size = gst_value_list_get_size (value1);
2210 for (i = 0; i < size; i++) {
2211 const GValue *cur = gst_value_list_get_value (value1, i);
2213 if (gst_value_intersect (&intersection, cur, value2)) {
2216 gst_value_init_and_copy (dest, &intersection);
2218 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2219 gst_value_list_append_value (dest, &intersection);
2221 GValue temp = { 0, };
2223 gst_value_init_and_copy (&temp, dest);
2224 g_value_unset (dest);
2225 gst_value_list_concat (dest, &temp, &intersection);
2226 g_value_unset (&temp);
2228 g_value_unset (&intersection);
2236 gst_value_intersect_array (GValue * dest, const GValue * src1,
2237 const GValue * src2)
2243 /* only works on similar-sized arrays */
2244 size = gst_value_array_get_size (src1);
2245 if (size != gst_value_array_get_size (src2))
2247 g_value_init (dest, GST_TYPE_ARRAY);
2249 for (n = 0; n < size; n++) {
2250 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2251 gst_value_array_get_value (src2, n))) {
2252 g_value_unset (dest);
2255 gst_value_array_append_value (dest, &val);
2256 g_value_unset (&val);
2263 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2264 const GValue * src2)
2268 GstValueCompareFunc compare;
2270 vals = src2->data[0].v_pointer;
2275 if ((compare = gst_value_get_compare_func (src1))) {
2276 res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2277 res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2279 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2280 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2281 gst_value_init_and_copy (dest, src1);
2290 gst_value_intersect_fraction_range_fraction_range
2291 (GValue * dest, const GValue * src1, const GValue * src2)
2296 GValue *vals1, *vals2;
2297 GstValueCompareFunc compare;
2299 vals1 = src1->data[0].v_pointer;
2300 vals2 = src2->data[0].v_pointer;
2301 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2303 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2304 /* min = MAX (src1.start, src2.start) */
2305 res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2306 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2307 if (res == GST_VALUE_LESS_THAN)
2308 min = &vals2[0]; /* Take the max of the 2 */
2312 /* max = MIN (src1.end, src2.end) */
2313 res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2314 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2315 if (res == GST_VALUE_GREATER_THAN)
2316 max = &vals2[1]; /* Take the min of the 2 */
2320 res = gst_value_compare_with_func (min, max, compare);
2321 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2322 if (res == GST_VALUE_LESS_THAN) {
2323 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2324 vals1 = dest->data[0].v_pointer;
2325 g_value_copy (min, &vals1[0]);
2326 g_value_copy (max, &vals1[1]);
2329 if (res == GST_VALUE_EQUAL) {
2330 gst_value_init_and_copy (dest, min);
2343 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2344 const GValue * subtrahend)
2346 int min = gst_value_get_int_range_min (subtrahend);
2347 int max = gst_value_get_int_range_max (subtrahend);
2348 int val = g_value_get_int (minuend);
2350 /* subtracting a range from an int only works if the int is not in the
2352 if (val < min || val > max) {
2353 /* and the result is the int */
2354 gst_value_init_and_copy (dest, minuend);
2360 /* creates a new int range based on input values.
2363 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2368 GValue *pv1, *pv2; /* yeah, hungarian! */
2370 if (min1 <= max1 && min2 <= max2) {
2373 } else if (min1 <= max1) {
2376 } else if (min2 <= max2) {
2384 g_value_init (pv1, GST_TYPE_INT_RANGE);
2385 gst_value_set_int_range (pv1, min1, max1);
2386 } else if (min1 == max1) {
2387 g_value_init (pv1, G_TYPE_INT);
2388 g_value_set_int (pv1, min1);
2391 g_value_init (pv2, GST_TYPE_INT_RANGE);
2392 gst_value_set_int_range (pv2, min2, max2);
2393 } else if (min2 == max2) {
2394 g_value_init (pv2, G_TYPE_INT);
2395 g_value_set_int (pv2, min2);
2398 if (min1 <= max1 && min2 <= max2) {
2399 gst_value_list_concat (dest, pv1, pv2);
2400 g_value_unset (pv1);
2401 g_value_unset (pv2);
2407 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2408 const GValue * subtrahend)
2410 gint min = gst_value_get_int_range_min (minuend);
2411 gint max = gst_value_get_int_range_max (minuend);
2412 gint val = g_value_get_int (subtrahend);
2414 g_return_val_if_fail (min < max, FALSE);
2416 /* value is outside of the range, return range unchanged */
2417 if (val < min || val > max) {
2418 gst_value_init_and_copy (dest, minuend);
2421 /* max must be MAXINT too as val <= max */
2422 if (val == G_MAXINT) {
2426 /* min must be MININT too as val >= max */
2427 if (val == G_MININT) {
2431 gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2437 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2438 const GValue * subtrahend)
2440 gint min1 = gst_value_get_int_range_min (minuend);
2441 gint max1 = gst_value_get_int_range_max (minuend);
2442 gint min2 = gst_value_get_int_range_min (subtrahend);
2443 gint max2 = gst_value_get_int_range_max (subtrahend);
2445 if (max2 == G_MAXINT && min2 == G_MININT) {
2447 } else if (max2 == G_MAXINT) {
2448 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
2449 } else if (min2 == G_MININT) {
2450 return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
2452 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
2453 MAX (max2 + 1, min1), max1);
2458 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
2459 const GValue * subtrahend)
2461 gdouble min = gst_value_get_double_range_min (subtrahend);
2462 gdouble max = gst_value_get_double_range_max (subtrahend);
2463 gdouble val = g_value_get_double (minuend);
2465 if (val < min || val > max) {
2466 gst_value_init_and_copy (dest, minuend);
2473 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
2474 const GValue * subtrahend)
2476 /* since we don't have open ranges, we cannot create a hole in
2477 * a double range. We return the original range */
2478 gst_value_init_and_copy (dest, minuend);
2483 gst_value_subtract_double_range_double_range (GValue * dest,
2484 const GValue * minuend, const GValue * subtrahend)
2486 /* since we don't have open ranges, we have to approximate */
2487 /* done like with ints */
2488 gdouble min1 = gst_value_get_double_range_min (minuend);
2489 gdouble max2 = gst_value_get_double_range_max (minuend);
2490 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
2491 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
2494 GValue *pv1, *pv2; /* yeah, hungarian! */
2496 if (min1 < max1 && min2 < max2) {
2499 } else if (min1 < max1) {
2502 } else if (min2 < max2) {
2510 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
2511 gst_value_set_double_range (pv1, min1, max1);
2514 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
2515 gst_value_set_double_range (pv2, min2, max2);
2518 if (min1 < max1 && min2 < max2) {
2519 gst_value_list_concat (dest, pv1, pv2);
2520 g_value_unset (pv1);
2521 g_value_unset (pv2);
2527 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
2528 const GValue * subtrahend)
2531 GValue subtraction = { 0, };
2532 gboolean ret = FALSE;
2534 size = gst_value_list_get_size (minuend);
2535 for (i = 0; i < size; i++) {
2536 const GValue *cur = gst_value_list_get_value (minuend, i);
2538 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
2540 gst_value_init_and_copy (dest, &subtraction);
2542 } else if (GST_VALUE_HOLDS_LIST (dest)
2543 && GST_VALUE_HOLDS_LIST (&subtraction)) {
2545 GValue unroll = { 0, };
2547 gst_value_init_and_copy (&unroll, dest);
2548 g_value_unset (dest);
2549 gst_value_list_concat (dest, &unroll, &subtraction);
2550 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2551 gst_value_list_append_value (dest, &subtraction);
2553 GValue temp = { 0, };
2555 gst_value_init_and_copy (&temp, dest);
2556 g_value_unset (dest);
2557 gst_value_list_concat (dest, &temp, &subtraction);
2558 g_value_unset (&temp);
2560 g_value_unset (&subtraction);
2567 gst_value_subtract_list (GValue * dest, const GValue * minuend,
2568 const GValue * subtrahend)
2571 GValue data[2] = { {0,}, {0,} };
2572 GValue *subtraction = &data[0], *result = &data[1];
2574 gst_value_init_and_copy (result, minuend);
2575 size = gst_value_list_get_size (subtrahend);
2576 for (i = 0; i < size; i++) {
2577 const GValue *cur = gst_value_list_get_value (subtrahend, i);
2579 if (gst_value_subtract (subtraction, result, cur)) {
2580 GValue *temp = result;
2582 result = subtraction;
2584 g_value_unset (subtraction);
2586 g_value_unset (result);
2590 gst_value_init_and_copy (dest, result);
2591 g_value_unset (result);
2596 gst_value_subtract_fraction_fraction_range (GValue * dest,
2597 const GValue * minuend, const GValue * subtrahend)
2599 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
2600 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
2601 GstValueCompareFunc compare;
2603 if ((compare = gst_value_get_compare_func (minuend))) {
2604 /* subtracting a range from an fraction only works if the fraction
2605 * is not in the range */
2606 if (gst_value_compare_with_func (minuend, min, compare) ==
2607 GST_VALUE_LESS_THAN ||
2608 gst_value_compare_with_func (minuend, max, compare) ==
2609 GST_VALUE_GREATER_THAN) {
2610 /* and the result is the value */
2611 gst_value_init_and_copy (dest, minuend);
2619 gst_value_subtract_fraction_range_fraction (GValue * dest,
2620 const GValue * minuend, const GValue * subtrahend)
2622 /* since we don't have open ranges, we cannot create a hole in
2623 * a range. We return the original range */
2624 gst_value_init_and_copy (dest, minuend);
2629 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
2630 const GValue * minuend, const GValue * subtrahend)
2632 /* since we don't have open ranges, we have to approximate */
2633 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
2634 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
2635 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
2636 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
2637 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
2641 GValue *pv1, *pv2; /* yeah, hungarian! */
2642 GstValueCompareFunc compare;
2644 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
2645 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
2647 compare = gst_value_get_compare_func (min1);
2648 g_return_val_if_fail (compare, FALSE);
2650 cmp1 = gst_value_compare_with_func (max2, max1, compare);
2651 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2652 if (cmp1 == GST_VALUE_LESS_THAN)
2654 cmp1 = gst_value_compare_with_func (min1, min2, compare);
2655 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
2656 if (cmp1 == GST_VALUE_GREATER_THAN)
2659 cmp1 = gst_value_compare_with_func (min1, max1, compare);
2660 cmp2 = gst_value_compare_with_func (min2, max2, compare);
2662 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2665 } else if (cmp1 == GST_VALUE_LESS_THAN) {
2668 } else if (cmp2 == GST_VALUE_LESS_THAN) {
2675 if (cmp1 == GST_VALUE_LESS_THAN) {
2676 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
2677 gst_value_set_fraction_range (pv1, min1, max1);
2679 if (cmp2 == GST_VALUE_LESS_THAN) {
2680 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
2681 gst_value_set_fraction_range (pv2, min2, max2);
2684 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
2685 gst_value_list_concat (dest, pv1, pv2);
2686 g_value_unset (pv1);
2687 g_value_unset (pv2);
2698 * gst_value_can_compare:
2699 * @value1: a value to compare
2700 * @value2: another value to compare
2702 * Determines if @value1 and @value2 can be compared.
2704 * Returns: TRUE if the values can be compared
2707 gst_value_can_compare (const GValue * value1, const GValue * value2)
2709 GstValueTable *table;
2712 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2715 for (i = 0; i < gst_value_table->len; i++) {
2716 table = &g_array_index (gst_value_table, GstValueTable, i);
2717 if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
2725 * gst_value_get_compare_func:
2726 * @value1: a value to get the compare function for
2728 * Determines the compare function to be used with values of the same type as
2729 * @value1. The function can be given to gst_value_compare_with_func().
2731 * Returns: A #GstValueCompareFunc value
2733 static GstValueCompareFunc
2734 gst_value_get_compare_func (const GValue * value1)
2736 GstValueTable *table, *best = NULL;
2739 for (i = 0; i < gst_value_table->len; i++) {
2740 table = &g_array_index (gst_value_table, GstValueTable, i);
2741 if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
2745 if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
2746 if (!best || g_type_is_a (table->type, best->type))
2751 return best->compare;
2757 * gst_value_compare:
2758 * @value1: a value to compare
2759 * @value2: another value to compare
2761 * Compares @value1 and @value2. If @value1 and @value2 cannot be
2762 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
2763 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
2764 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
2765 * If the values are equal, GST_VALUE_EQUAL is returned.
2767 * Returns: A #GstValueCompareType value
2770 gst_value_compare (const GValue * value1, const GValue * value2)
2772 GstValueCompareFunc compare;
2774 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2775 return GST_VALUE_UNORDERED;
2777 compare = gst_value_get_compare_func (value1);
2779 return compare (value1, value2);
2782 g_critical ("unable to compare values of type %s\n",
2783 g_type_name (G_VALUE_TYPE (value1)));
2784 return GST_VALUE_UNORDERED;
2788 * gst_value_compare_with_func:
2789 * @value1: a value to compare
2790 * @value2: another value to compare
2791 * @compare: compare function
2793 * Compares @value1 and @value2 using the @compare function. Works like
2794 * gst_value_compare() but allows to save time determining the compare function
2797 * Returns: A #GstValueCompareType value
2800 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
2801 GstValueCompareFunc compare)
2805 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
2806 return GST_VALUE_UNORDERED;
2808 return compare (value1, value2);
2814 * gst_value_can_union:
2815 * @value1: a value to union
2816 * @value2: another value to union
2818 * Determines if @value1 and @value2 can be non-trivially unioned.
2819 * Any two values can be trivially unioned by adding both of them
2820 * to a GstValueList. However, certain types have the possibility
2821 * to be unioned in a simpler way. For example, an integer range
2822 * and an integer can be unioned if the integer is a subset of the
2823 * integer range. If there is the possibility that two values can
2824 * be unioned, this function returns TRUE.
2826 * Returns: TRUE if there is a function allowing the two values to
2830 gst_value_can_union (const GValue * value1, const GValue * value2)
2832 GstValueUnionInfo *union_info;
2835 for (i = 0; i < gst_value_union_funcs->len; i++) {
2836 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2837 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2838 union_info->type2 == G_VALUE_TYPE (value2))
2840 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2841 union_info->type2 == G_VALUE_TYPE (value1))
2850 * @dest: the destination value
2851 * @value1: a value to union
2852 * @value2: another value to union
2854 * Creates a GValue cooresponding to the union of @value1 and @value2.
2856 * Returns: always returns %TRUE
2858 /* FIXME: change return type to 'void'? */
2860 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
2862 GstValueUnionInfo *union_info;
2865 for (i = 0; i < gst_value_union_funcs->len; i++) {
2866 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
2867 if (union_info->type1 == G_VALUE_TYPE (value1) &&
2868 union_info->type2 == G_VALUE_TYPE (value2)) {
2869 if (union_info->func (dest, value1, value2)) {
2873 if (union_info->type1 == G_VALUE_TYPE (value2) &&
2874 union_info->type2 == G_VALUE_TYPE (value1)) {
2875 if (union_info->func (dest, value2, value1)) {
2881 gst_value_list_concat (dest, value1, value2);
2886 * gst_value_register_union_func:
2887 * @type1: a type to union
2888 * @type2: another type to union
2889 * @func: a function that implments creating a union between the two types
2891 * Registers a union function that can create a union between GValues
2892 * of the type @type1 and @type2.
2894 * Union functions should be registered at startup before any pipelines are
2895 * started, as gst_value_register_union_func() is not thread-safe and cannot
2896 * be used at the same time as gst_value_union() or gst_value_can_union().
2899 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
2901 GstValueUnionInfo union_info;
2903 union_info.type1 = type1;
2904 union_info.type2 = type2;
2905 union_info.func = func;
2907 g_array_append_val (gst_value_union_funcs, union_info);
2913 * gst_value_can_intersect:
2914 * @value1: a value to intersect
2915 * @value2: another value to intersect
2917 * Determines if intersecting two values will produce a valid result.
2918 * Two values will produce a valid intersection if they have the same
2919 * type, or if there is a method (registered by
2920 * gst_value_register_intersection_func()) to calculate the intersection.
2922 * Returns: TRUE if the values can intersect
2925 gst_value_can_intersect (const GValue * value1, const GValue * value2)
2927 GstValueIntersectInfo *intersect_info;
2931 if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
2934 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2935 intersect_info = &g_array_index (gst_value_intersect_funcs,
2936 GstValueIntersectInfo, i);
2937 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2938 intersect_info->type2 == G_VALUE_TYPE (value2))
2939 if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
2940 intersect_info->type1 == G_VALUE_TYPE (value2))
2944 return gst_value_can_compare (value1, value2);
2948 * gst_value_intersect:
2949 * @dest: a uninitialized #GValue that will hold the calculated
2950 * intersection value
2951 * @value1: a value to intersect
2952 * @value2: another value to intersect
2954 * Calculates the intersection of two values. If the values have
2955 * a non-empty intersection, the value representing the intersection
2956 * is placed in @dest. If the intersection is non-empty, @dest is
2959 * Returns: TRUE if the intersection is non-empty
2962 gst_value_intersect (GValue * dest, const GValue * value1,
2963 const GValue * value2)
2965 GstValueIntersectInfo *intersect_info;
2967 gboolean ret = FALSE;
2969 /* special cases first */
2970 if (GST_VALUE_HOLDS_LIST (value1))
2971 return gst_value_intersect_list (dest, value1, value2);
2972 if (GST_VALUE_HOLDS_LIST (value2))
2973 return gst_value_intersect_list (dest, value2, value1);
2975 for (i = 0; i < gst_value_intersect_funcs->len; i++) {
2976 intersect_info = &g_array_index (gst_value_intersect_funcs,
2977 GstValueIntersectInfo, i);
2978 if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
2979 intersect_info->type2 == G_VALUE_TYPE (value2)) {
2980 ret = intersect_info->func (dest, value1, value2);
2983 if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
2984 intersect_info->type2 == G_VALUE_TYPE (value1)) {
2985 ret = intersect_info->func (dest, value2, value1);
2990 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
2991 gst_value_init_and_copy (dest, value1);
2999 * gst_value_register_intersect_func:
3000 * @type1: the first type to intersect
3001 * @type2: the second type to intersect
3002 * @func: the intersection function
3004 * Registers a function that is called to calculate the intersection
3005 * of the values having the types @type1 and @type2.
3007 * Intersect functions should be registered at startup before any pipelines are
3008 * started, as gst_value_register_intersect_func() is not thread-safe and
3009 * cannot be used at the same time as gst_value_intersect() or
3010 * gst_value_can_intersect().
3013 gst_value_register_intersect_func (GType type1, GType type2,
3014 GstValueIntersectFunc func)
3016 GstValueIntersectInfo intersect_info;
3018 intersect_info.type1 = type1;
3019 intersect_info.type2 = type2;
3020 intersect_info.func = func;
3022 g_array_append_val (gst_value_intersect_funcs, intersect_info);
3029 * gst_value_subtract:
3030 * @dest: the destination value for the result if the subtraction is not empty
3031 * @minuend: the value to subtract from
3032 * @subtrahend: the value to subtract
3034 * Subtracts @subtrahend from @minuend and stores the result in @dest.
3035 * Note that this means subtraction as in sets, not as in mathematics.
3037 * Returns: %TRUE if the subtraction is not empty
3040 gst_value_subtract (GValue * dest, const GValue * minuend,
3041 const GValue * subtrahend)
3043 GstValueSubtractInfo *info;
3046 /* special cases first */
3047 if (GST_VALUE_HOLDS_LIST (minuend))
3048 return gst_value_subtract_from_list (dest, minuend, subtrahend);
3049 if (GST_VALUE_HOLDS_LIST (subtrahend))
3050 return gst_value_subtract_list (dest, minuend, subtrahend);
3052 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3053 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3054 if (info->minuend == G_VALUE_TYPE (minuend) &&
3055 info->subtrahend == G_VALUE_TYPE (subtrahend)) {
3056 return info->func (dest, minuend, subtrahend);
3060 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3061 gst_value_init_and_copy (dest, minuend);
3070 gst_value_subtract (GValue * dest, const GValue * minuend,
3071 const GValue * subtrahend)
3073 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3075 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
3076 gst_value_serialize (subtrahend),
3077 ret ? gst_value_serialize (dest) : "---");
3083 * gst_value_can_subtract:
3084 * @minuend: the value to subtract from
3085 * @subtrahend: the value to subtract
3087 * Checks if it's possible to subtract @subtrahend from @minuend.
3089 * Returns: TRUE if a subtraction is possible
3092 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3094 GstValueSubtractInfo *info;
3098 if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
3101 for (i = 0; i < gst_value_subtract_funcs->len; i++) {
3102 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3103 if (info->minuend == G_VALUE_TYPE (minuend) &&
3104 info->subtrahend == G_VALUE_TYPE (subtrahend))
3108 return gst_value_can_compare (minuend, subtrahend);
3112 * gst_value_register_subtract_func:
3113 * @minuend_type: type of the minuend
3114 * @subtrahend_type: type of the subtrahend
3115 * @func: function to use
3117 * Registers @func as a function capable of subtracting the values of
3118 * @subtrahend_type from values of @minuend_type.
3120 * Subtract functions should be registered at startup before any pipelines are
3121 * started, as gst_value_register_subtract_func() is not thread-safe and
3122 * cannot be used at the same time as gst_value_subtract().
3125 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3126 GstValueSubtractFunc func)
3128 GstValueSubtractInfo info;
3130 /* one type must be unfixed, other subtractions can be done as comparisons */
3131 g_return_if_fail (!gst_type_is_fixed (minuend_type)
3132 || !gst_type_is_fixed (subtrahend_type));
3134 info.minuend = minuend_type;
3135 info.subtrahend = subtrahend_type;
3138 g_array_append_val (gst_value_subtract_funcs, info);
3142 * gst_value_register:
3143 * @table: structure containing functions to register
3145 * Registers functions to perform calculations on #GValues of a given
3150 * @type: GType that the functions operate on.
3151 * @compare: A function that compares two values of this type.
3152 * @serialize: A function that transforms a value of this type to a
3153 * string. Strings created by this function must be unique and should
3154 * be human readable.
3155 * @deserialize: A function that transforms a string to a value of
3156 * this type. This function must transform strings created by the
3157 * serialize function back to the original value. This function may
3158 * optionally transform other strings into values.
3161 gst_value_register (const GstValueTable * table)
3163 g_array_append_val (gst_value_table, *table);
3167 * gst_value_init_and_copy:
3168 * @dest: the target value
3169 * @src: the source value
3171 * Initialises the target value to be of the same type as source and then copies
3172 * the contents from source to target.
3175 gst_value_init_and_copy (GValue * dest, const GValue * src)
3177 g_value_init (dest, G_VALUE_TYPE (src));
3178 g_value_copy (src, dest);
3182 * gst_value_serialize:
3183 * @value: a #GValue to serialize
3185 * tries to transform the given @value into a string representation that allows
3186 * getting back this string later on using gst_value_deserialize().
3188 * Returns: the serialization for @value or NULL if none exists
3191 gst_value_serialize (const GValue * value)
3194 GValue s_val = { 0 };
3195 GstValueTable *table, *best = NULL;
3198 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3200 for (i = 0; i < gst_value_table->len; i++) {
3201 table = &g_array_index (gst_value_table, GstValueTable, i);
3202 if (table->serialize == NULL)
3204 if (table->type == G_VALUE_TYPE (value)) {
3208 if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
3209 if (!best || g_type_is_a (table->type, best->type))
3214 return best->serialize (value);
3216 g_value_init (&s_val, G_TYPE_STRING);
3217 if (g_value_transform (value, &s_val)) {
3218 s = gst_string_wrap (g_value_get_string (&s_val));
3222 g_value_unset (&s_val);
3228 * gst_value_deserialize:
3229 * @dest: #GValue to fill with contents of deserialization
3230 * @src: string to deserialize
3232 * Tries to deserialize a string into the type specified by the given GValue.
3233 * If the operation succeeds, TRUE is returned, FALSE otherwise.
3235 * Returns: TRUE on success
3238 gst_value_deserialize (GValue * dest, const gchar * src)
3240 GstValueTable *table, *best = NULL;
3243 g_return_val_if_fail (src != NULL, FALSE);
3244 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
3246 for (i = 0; i < gst_value_table->len; i++) {
3247 table = &g_array_index (gst_value_table, GstValueTable, i);
3248 if (table->serialize == NULL)
3251 if (table->type == G_VALUE_TYPE (dest)) {
3256 if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
3257 if (!best || g_type_is_a (table->type, best->type))
3262 return best->deserialize (dest, src);
3269 * gst_value_is_fixed:
3270 * @value: the #GValue to check
3272 * Tests if the given GValue, if available in a GstStructure (or any other
3273 * container) contains a "fixed" (which means: one value) or an "unfixed"
3274 * (which means: multiple possible values, such as data lists or data
3277 * Returns: true if the value is "fixed".
3281 gst_value_is_fixed (const GValue * value)
3283 GType type = G_VALUE_TYPE (value);
3285 if (type == GST_TYPE_ARRAY) {
3286 gboolean fixed = TRUE;
3290 /* check recursively */
3291 size = gst_value_array_get_size (value);
3292 for (n = 0; n < size; n++) {
3293 kid = gst_value_array_get_value (value, n);
3294 fixed &= gst_value_is_fixed (kid);
3300 return gst_type_is_fixed (type);
3307 /* helper functions */
3309 /* Finds the greatest common divisor.
3310 * Returns 1 if none other found.
3311 * This is Euclid's algorithm. */
3313 gst_greatest_common_divisor (gint a, gint b)
3326 gst_value_init_fraction (GValue * value)
3328 value->data[0].v_int = 0;
3329 value->data[1].v_int = 1;
3333 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
3335 dest_value->data[0].v_int = src_value->data[0].v_int;
3336 dest_value->data[1].v_int = src_value->data[1].v_int;
3340 gst_value_collect_fraction (GValue * value, guint n_collect_values,
3341 GTypeCValue * collect_values, guint collect_flags)
3343 gst_value_set_fraction (value,
3344 collect_values[0].v_int, collect_values[1].v_int);
3350 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
3351 GTypeCValue * collect_values, guint collect_flags)
3353 gint *numerator = collect_values[0].v_pointer;
3354 gint *denominator = collect_values[1].v_pointer;
3357 return g_strdup_printf ("numerator for `%s' passed as NULL",
3358 G_VALUE_TYPE_NAME (value));
3360 return g_strdup_printf ("denominator for `%s' passed as NULL",
3361 G_VALUE_TYPE_NAME (value));
3363 *numerator = value->data[0].v_int;
3364 *denominator = value->data[1].v_int;
3370 * gst_value_set_fraction:
3371 * @value: a GValue initialized to #GST_TYPE_FRACTION
3372 * @numerator: the numerator of the fraction
3373 * @denominator: the denominator of the fraction
3375 * Sets @value to the fraction specified by @numerator over @denominator.
3376 * The fraction gets reduced to the smallest numerator and denominator,
3377 * and if necessary the sign is moved to the numerator.
3380 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
3384 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
3385 g_return_if_fail (denominator != 0);
3386 g_return_if_fail (denominator >= -G_MAXINT);
3387 g_return_if_fail (numerator >= -G_MAXINT);
3389 /* normalize sign */
3390 if (denominator < 0) {
3391 numerator = -numerator;
3392 denominator = -denominator;
3395 /* check for reduction */
3396 gcd = gst_greatest_common_divisor (numerator, denominator);
3402 g_assert (denominator > 0);
3404 value->data[0].v_int = numerator;
3405 value->data[1].v_int = denominator;
3409 * gst_value_get_fraction_numerator:
3410 * @value: a GValue initialized to #GST_TYPE_FRACTION
3412 * Gets the numerator of the fraction specified by @value.
3414 * Returns: the numerator of the fraction.
3417 gst_value_get_fraction_numerator (const GValue * value)
3419 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
3421 return value->data[0].v_int;
3425 * gst_value_get_fraction_denominator:
3426 * @value: a GValue initialized to #GST_TYPE_FRACTION
3428 * Gets the denominator of the fraction specified by @value.
3430 * Returns: the denominator of the fraction.
3433 gst_value_get_fraction_denominator (const GValue * value)
3435 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
3437 return value->data[1].v_int;
3441 * gst_value_fraction_multiply:
3442 * @product: a GValue initialized to #GST_TYPE_FRACTION
3443 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
3444 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
3446 * Multiplies the two GValues containing a GstFraction and sets @product
3447 * to the product of the two fractions.
3449 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3452 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
3453 const GValue * factor2)
3455 gint gcd, n1, n2, d1, d2;
3457 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
3458 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
3460 n1 = factor1->data[0].v_int;
3461 n2 = factor2->data[0].v_int;
3462 d1 = factor1->data[1].v_int;
3463 d2 = factor2->data[1].v_int;
3465 gcd = gst_greatest_common_divisor (n1, d2);
3468 gcd = gst_greatest_common_divisor (n2, d1);
3472 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
3473 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3475 gst_value_set_fraction (product, n1 * n2, d1 * d2);
3481 * gst_value_fraction_subtract:
3482 * @dest: a GValue initialized to #GST_TYPE_FRACTION
3483 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
3484 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
3486 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
3488 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
3491 gst_value_fraction_subtract (GValue * dest,
3492 const GValue * minuend, const GValue * subtrahend)
3494 gint n1, n2, d1, d2;
3496 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
3497 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
3499 n1 = minuend->data[0].v_int;
3500 n2 = subtrahend->data[0].v_int;
3501 d1 = minuend->data[1].v_int;
3502 d2 = subtrahend->data[1].v_int;
3505 gst_value_set_fraction (dest, -n2, d2);
3509 gst_value_set_fraction (dest, n1, d1);
3513 g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (d2), FALSE);
3514 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (n2), FALSE);
3515 g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
3517 gst_value_set_fraction (dest, (n1 * d2) - (n2 * d1), d1 * d2);
3523 gst_value_serialize_fraction (const GValue * value)
3525 gint32 numerator = value->data[0].v_int;
3526 gint32 denominator = value->data[1].v_int;
3527 gboolean positive = TRUE;
3529 /* get the sign and make components absolute */
3530 if (numerator < 0) {
3531 numerator = -numerator;
3532 positive = !positive;
3534 if (denominator < 0) {
3535 denominator = -denominator;
3536 positive = !positive;
3539 return g_strdup_printf ("%s%d/%d",
3540 positive ? "" : "-", numerator, denominator);
3544 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
3548 if (G_UNLIKELY (s == NULL))
3551 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
3554 if (sscanf (s, "%d/%d", &num, &den) == 2) {
3555 gst_value_set_fraction (dest, num, den);
3558 if (sscanf (s, "%d", &num) == 1) {
3559 gst_value_set_fraction (dest, num, 1);
3562 if (g_ascii_strcasecmp (s, "min") == 0) {
3563 gst_value_set_fraction (dest, -G_MAXINT, 1);
3565 } else if (g_ascii_strcasecmp (s, "max") == 0) {
3566 gst_value_set_fraction (dest, G_MAXINT, 1);
3574 gst_value_transform_fraction_string (const GValue * src_value,
3575 GValue * dest_value)
3577 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
3581 gst_value_transform_string_fraction (const GValue * src_value,
3582 GValue * dest_value)
3584 if (!gst_value_deserialize_fraction (dest_value,
3585 src_value->data[0].v_pointer))
3586 /* If the deserialize fails, ensure we leave the fraction in a
3587 * valid, if incorrect, state */
3588 gst_value_set_fraction (dest_value, 0, 1);
3591 #define MAX_TERMS 30
3592 #define MIN_DIVISOR 1.0e-10
3593 #define MAX_ERROR 1.0e-20
3595 /* use continued fractions to transform a double into a fraction,
3596 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3597 * This algorithm takes care of overflows.
3600 gst_value_transform_double_fraction (const GValue * src_value,
3601 GValue * dest_value)
3603 gdouble V, F; /* double being converted */
3604 gint N, D; /* will contain the result */
3605 gint A; /* current term in continued fraction */
3606 gint64 N1, D1; /* numerator, denominator of last approx */
3607 gint64 N2, D2; /* numerator, denominator of previous approx */
3609 gboolean negative = FALSE;
3611 /* initialize fraction being converted */
3612 F = src_value->data[0].v_double;
3619 /* initialize fractions with 1/0, 0/1 */
3627 for (i = 0; i < MAX_TERMS; i++) {
3629 A = (gint) F; /* no floor() needed, F is always >= 0 */
3630 /* get new divisor */
3633 /* calculate new fraction in temp */
3637 /* guard against overflow */
3638 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3645 /* save last two fractions */
3651 /* quit if dividing by zero or close enough to target */
3652 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3656 /* Take reciprocal */
3659 /* fix for overflow */
3664 /* fix for negative */
3668 /* will also simplify */
3669 gst_value_set_fraction (dest_value, N, D);
3673 gst_value_transform_fraction_double (const GValue * src_value,
3674 GValue * dest_value)
3676 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
3677 ((double) src_value->data[1].v_int);
3681 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
3689 n1 = value1->data[0].v_int;
3690 n2 = value2->data[0].v_int;
3691 d1 = value1->data[1].v_int;
3692 d2 = value2->data[1].v_int;
3694 /* fractions are reduced when set, so we can quickly see if they're equal */
3695 if (n1 == n2 && d1 == d2)
3696 return GST_VALUE_EQUAL;
3698 /* extend to 64 bits */
3699 new_num_1 = ((gint64) n1) * d2;
3700 new_num_2 = ((gint64) n2) * d1;
3701 if (new_num_1 < new_num_2)
3702 return GST_VALUE_LESS_THAN;
3703 if (new_num_1 > new_num_2)
3704 return GST_VALUE_GREATER_THAN;
3706 /* new_num_1 == new_num_2 implies that both denominators must have
3707 * been 0, beause otherwise simplification would have caught the
3709 return GST_VALUE_UNORDERED;
3717 * gst_value_set_date:
3718 * @value: a GValue initialized to GST_TYPE_DATE
3719 * @date: the date to set the value to
3721 * Sets the contents of @value to coorespond to @date. The actual
3722 * #GDate structure is copied before it is used.
3725 gst_value_set_date (GValue * value, const GDate * date)
3727 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
3728 g_return_if_fail (g_date_valid (date));
3730 g_value_set_boxed (value, date);
3734 * gst_value_get_date:
3735 * @value: a GValue initialized to GST_TYPE_DATE
3737 * Gets the contents of @value.
3739 * Returns: the contents of @value
3742 gst_value_get_date (const GValue * value)
3744 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
3746 return (const GDate *) g_value_get_boxed (value);
3750 gst_date_copy (gpointer boxed)
3752 const GDate *date = (const GDate *) boxed;
3754 if (!g_date_valid (date)) {
3755 GST_WARNING ("invalid GDate");
3759 return g_date_new_julian (g_date_get_julian (date));
3763 gst_value_compare_date (const GValue * value1, const GValue * value2)
3765 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
3766 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
3770 return GST_VALUE_EQUAL;
3772 if ((date1 == NULL || !g_date_valid (date1))
3773 && (date2 != NULL && g_date_valid (date2))) {
3774 return GST_VALUE_LESS_THAN;
3777 if ((date2 == NULL || !g_date_valid (date2))
3778 && (date1 != NULL && g_date_valid (date1))) {
3779 return GST_VALUE_GREATER_THAN;
3782 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
3783 || !g_date_valid (date2)) {
3784 return GST_VALUE_UNORDERED;
3787 j1 = g_date_get_julian (date1);
3788 j2 = g_date_get_julian (date2);
3791 return GST_VALUE_EQUAL;
3793 return GST_VALUE_LESS_THAN;
3795 return GST_VALUE_GREATER_THAN;
3799 gst_value_serialize_date (const GValue * val)
3801 const GDate *date = (const GDate *) g_value_get_boxed (val);
3803 if (date == NULL || !g_date_valid (date))
3804 return g_strdup ("9999-99-99");
3806 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
3807 g_date_get_month (date), g_date_get_day (date));
3811 gst_value_deserialize_date (GValue * dest, const char *s)
3813 guint year, month, day;
3815 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
3818 if (!g_date_valid_dmy (day, month, year))
3821 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
3826 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
3828 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
3832 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
3834 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
3838 gst_value_transform_object_string (const GValue * src_value,
3839 GValue * dest_value)
3844 obj = g_value_get_object (src_value);
3845 str = obj ? GST_OBJECT_NAME (obj) : "NULL";
3847 dest_value->data[0].v_pointer = g_strdup (str);
3850 static GTypeInfo _info = {
3863 static GTypeFundamentalInfo _finfo = {
3867 #define FUNC_VALUE_GET_TYPE(type, name) \
3868 GType gst_ ## type ## _get_type (void) \
3870 static GType gst_ ## type ## _type = 0; \
3872 if (G_UNLIKELY (gst_ ## type ## _type == 0)) { \
3873 _info.value_table = & _gst_ ## type ## _value_table; \
3874 gst_ ## type ## _type = g_type_register_fundamental ( \
3875 g_type_fundamental_next (), \
3876 name, &_info, &_finfo, 0); \
3879 return gst_ ## type ## _type; \
3882 static const GTypeValueTable _gst_fourcc_value_table = {
3883 gst_value_init_fourcc,
3885 gst_value_copy_fourcc,
3888 gst_value_collect_fourcc,
3890 gst_value_lcopy_fourcc
3893 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
3895 static const GTypeValueTable _gst_int_range_value_table = {
3896 gst_value_init_int_range,
3898 gst_value_copy_int_range,
3901 gst_value_collect_int_range,
3903 gst_value_lcopy_int_range
3906 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
3908 static const GTypeValueTable _gst_double_range_value_table = {
3909 gst_value_init_double_range,
3911 gst_value_copy_double_range,
3914 gst_value_collect_double_range,
3916 gst_value_lcopy_double_range
3919 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
3921 static const GTypeValueTable _gst_fraction_range_value_table = {
3922 gst_value_init_fraction_range,
3923 gst_value_free_fraction_range,
3924 gst_value_copy_fraction_range,
3927 gst_value_collect_fraction_range,
3929 gst_value_lcopy_fraction_range
3932 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
3934 static const GTypeValueTable _gst_value_list_value_table = {
3935 gst_value_init_list_or_array,
3936 gst_value_free_list_or_array,
3937 gst_value_copy_list_or_array,
3938 gst_value_list_or_array_peek_pointer,
3940 gst_value_collect_list_or_array,
3942 gst_value_lcopy_list_or_array
3945 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
3947 static const GTypeValueTable _gst_value_array_value_table = {
3948 gst_value_init_list_or_array,
3949 gst_value_free_list_or_array,
3950 gst_value_copy_list_or_array,
3951 gst_value_list_or_array_peek_pointer,
3953 gst_value_collect_list_or_array,
3955 gst_value_lcopy_list_or_array
3958 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
3960 static const GTypeValueTable _gst_fraction_value_table = {
3961 gst_value_init_fraction,
3963 gst_value_copy_fraction,
3966 gst_value_collect_fraction,
3968 gst_value_lcopy_fraction
3971 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
3975 gst_date_get_type (void)
3977 static GType gst_date_type = 0;
3979 if (G_UNLIKELY (gst_date_type == 0)) {
3980 /* FIXME 0.11: we require GLib 2.8 already
3981 * Not using G_TYPE_DATE here on purpose, even if we could
3982 * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
3983 * serialised strings to have different type strings depending
3984 * on what version is used, so FIXME when we require GLib-2.8 */
3985 gst_date_type = g_boxed_type_register_static ("GstDate",
3986 (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
3989 return gst_date_type;
3993 _gst_value_initialize (void)
3995 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
3996 gst_value_union_funcs = g_array_new (FALSE, FALSE,
3997 sizeof (GstValueUnionInfo));
3998 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
3999 sizeof (GstValueIntersectInfo));
4000 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4001 sizeof (GstValueSubtractInfo));
4004 static GstValueTable gst_value = {
4006 gst_value_compare_fourcc,
4007 gst_value_serialize_fourcc,
4008 gst_value_deserialize_fourcc,
4011 gst_value.type = gst_fourcc_get_type ();
4012 gst_value_register (&gst_value);
4016 static GstValueTable gst_value = {
4018 gst_value_compare_int_range,
4019 gst_value_serialize_int_range,
4020 gst_value_deserialize_int_range,
4023 gst_value.type = gst_int_range_get_type ();
4024 gst_value_register (&gst_value);
4028 static GstValueTable gst_value = {
4030 gst_value_compare_double_range,
4031 gst_value_serialize_double_range,
4032 gst_value_deserialize_double_range,
4035 gst_value.type = gst_double_range_get_type ();
4036 gst_value_register (&gst_value);
4040 static GstValueTable gst_value = {
4042 gst_value_compare_fraction_range,
4043 gst_value_serialize_fraction_range,
4044 gst_value_deserialize_fraction_range,
4047 gst_value.type = gst_fraction_range_get_type ();
4048 gst_value_register (&gst_value);
4052 static GstValueTable gst_value = {
4054 gst_value_compare_list,
4055 gst_value_serialize_list,
4056 gst_value_deserialize_list,
4059 gst_value.type = gst_value_list_get_type ();
4060 gst_value_register (&gst_value);
4064 static GstValueTable gst_value = {
4066 gst_value_compare_array,
4067 gst_value_serialize_array,
4068 gst_value_deserialize_array,
4071 gst_value.type = gst_value_array_get_type ();;
4072 gst_value_register (&gst_value);
4077 static const GTypeValueTable value_table = {
4078 gst_value_init_buffer,
4080 gst_value_copy_buffer,
4083 NULL, /*gst_value_collect_buffer, */
4085 NULL /*gst_value_lcopy_buffer */
4088 static GstValueTable gst_value = {
4090 gst_value_compare_buffer,
4091 gst_value_serialize_buffer,
4092 gst_value_deserialize_buffer,
4095 gst_value.type = GST_TYPE_BUFFER;
4096 gst_value_register (&gst_value);
4099 static GstValueTable gst_value = {
4101 gst_value_compare_fraction,
4102 gst_value_serialize_fraction,
4103 gst_value_deserialize_fraction,
4106 gst_value.type = gst_fraction_get_type ();
4107 gst_value_register (&gst_value);
4110 static GstValueTable gst_value = {
4113 gst_value_serialize_caps,
4114 gst_value_deserialize_caps,
4117 gst_value.type = GST_TYPE_CAPS;
4118 gst_value_register (&gst_value);
4121 static GstValueTable gst_value = {
4123 gst_value_compare_date,
4124 gst_value_serialize_date,
4125 gst_value_deserialize_date,
4128 gst_value.type = gst_date_get_type ();
4129 gst_value_register (&gst_value);
4132 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
4133 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
4135 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
4136 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
4137 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
4139 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
4141 REGISTER_SERIALIZATION (G_TYPE_INT, int);
4143 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
4144 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
4146 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
4147 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
4148 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
4150 g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
4151 gst_value_transform_fourcc_string);
4152 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
4153 gst_value_transform_int_range_string);
4154 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
4155 gst_value_transform_double_range_string);
4156 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
4157 gst_value_transform_fraction_range_string);
4158 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
4159 gst_value_transform_list_string);
4160 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
4161 gst_value_transform_array_string);
4162 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
4163 gst_value_transform_fraction_string);
4164 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
4165 gst_value_transform_string_fraction);
4166 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
4167 gst_value_transform_fraction_double);
4168 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
4169 gst_value_transform_double_fraction);
4170 g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
4171 gst_value_transform_date_string);
4172 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
4173 gst_value_transform_string_date);
4174 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
4175 gst_value_transform_object_string);
4177 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4178 gst_value_intersect_int_int_range);
4179 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4180 gst_value_intersect_int_range_int_range);
4181 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4182 gst_value_intersect_double_double_range);
4183 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
4184 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
4185 gst_value_register_intersect_func (GST_TYPE_ARRAY,
4186 GST_TYPE_ARRAY, gst_value_intersect_array);
4187 gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4188 gst_value_intersect_fraction_fraction_range);
4189 gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
4190 GST_TYPE_FRACTION_RANGE,
4191 gst_value_intersect_fraction_range_fraction_range);
4193 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4194 gst_value_subtract_int_int_range);
4195 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
4196 gst_value_subtract_int_range_int);
4197 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4198 gst_value_subtract_int_range_int_range);
4199 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
4200 gst_value_subtract_double_double_range);
4201 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
4202 gst_value_subtract_double_range_double);
4203 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
4204 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
4206 gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4207 gst_value_subtract_fraction_fraction_range);
4208 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
4209 gst_value_subtract_fraction_range_fraction);
4210 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
4211 GST_TYPE_FRACTION_RANGE,
4212 gst_value_subtract_fraction_range_fraction_range);
4214 /* see bug #317246, #64994, #65041 */
4216 volatile GType date_type = G_TYPE_DATE;
4218 g_type_name (date_type);
4221 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
4222 gst_value_union_int_int_range);
4223 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
4224 gst_value_union_int_range_int_range);
4227 /* Implement these if needed */
4228 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
4229 gst_value_union_fraction_fraction_range);
4230 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
4231 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);