2 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 * @short_description: GValue implementations specific
25 * GValue implementations specific to GStreamer.
27 * Note that operations on the same #GValue from multiple threads may lead to
28 * undefined behaviour.
30 * Last reviewed on 2008-03-11 (0.10.18)
42 #include "gst_private.h"
43 #include "glib-compat-private.h"
45 #include <gobject/gvaluecollector.h>
48 typedef struct _GstValueUnionInfo GstValueUnionInfo;
49 struct _GstValueUnionInfo
53 GstValueUnionFunc func;
56 typedef struct _GstValueIntersectInfo GstValueIntersectInfo;
57 struct _GstValueIntersectInfo
61 GstValueIntersectFunc func;
64 typedef struct _GstValueSubtractInfo GstValueSubtractInfo;
65 struct _GstValueSubtractInfo
69 GstValueSubtractFunc func;
72 #define FUNDAMENTAL_TYPE_ID_MAX \
73 (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
74 #define FUNDAMENTAL_TYPE_ID(type) \
75 ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
77 #define VALUE_LIST_SIZE(v) (((GArray *) (v)->data[0].v_pointer)->len)
78 #define VALUE_LIST_GET_VALUE(v, index) ((const GValue *) &g_array_index ((GArray *) (v)->data[0].v_pointer, GValue, (index)))
80 static GArray *gst_value_table;
81 static GHashTable *gst_value_hash;
82 static GstValueTable *gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID_MAX + 1];
83 static GArray *gst_value_union_funcs;
84 static GArray *gst_value_intersect_funcs;
85 static GArray *gst_value_subtract_funcs;
87 /* Forward declarations */
88 static gchar *gst_value_serialize_fraction (const GValue * value);
90 static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
91 static gint gst_value_compare_with_func (const GValue * value1,
92 const GValue * value2, GstValueCompareFunc compare);
94 static gchar *gst_string_wrap (const gchar * s);
95 static gchar *gst_string_take_and_wrap (gchar * s);
96 static gchar *gst_string_unwrap (const gchar * s);
98 static inline GstValueTable *
99 gst_value_hash_lookup_type (GType type)
101 if (G_LIKELY (G_TYPE_IS_FUNDAMENTAL (type)))
102 return gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)];
104 return g_hash_table_lookup (gst_value_hash, (gpointer) type);
108 gst_value_hash_add_type (GType type, const GstValueTable * table)
110 if (G_TYPE_IS_FUNDAMENTAL (type))
111 gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)] = (gpointer) table;
113 g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table);
120 /* two helper functions to serialize/stringify any type of list
121 * regular lists are done with { }, arrays with < >
124 gst_value_serialize_any_list (const GValue * value, const gchar * begin,
128 GArray *array = value->data[0].v_pointer;
132 guint alen = array->len;
134 /* estimate minimum string length to minimise re-allocs in GString */
135 s = g_string_sized_new (2 + (6 * alen) + 2);
136 g_string_append (s, begin);
137 for (i = 0; i < alen; i++) {
138 v = &g_array_index (array, GValue, i);
139 s_val = gst_value_serialize (v);
140 g_string_append (s, s_val);
143 g_string_append_len (s, ", ", 2);
146 g_string_append (s, end);
147 return g_string_free (s, FALSE);
151 gst_value_transform_any_list_string (const GValue * src_value,
152 GValue * dest_value, const gchar * begin, const gchar * end)
161 array = src_value->data[0].v_pointer;
164 /* estimate minimum string length to minimise re-allocs in GString */
165 s = g_string_sized_new (2 + (10 * alen) + 2);
166 g_string_append (s, begin);
167 for (i = 0; i < alen; i++) {
168 list_value = &g_array_index (array, GValue, i);
171 g_string_append_len (s, ", ", 2);
173 list_s = g_strdup_value_contents (list_value);
174 g_string_append (s, list_s);
177 g_string_append (s, end);
179 dest_value->data[0].v_pointer = g_string_free (s, FALSE);
183 * helper function to see if a type is fixed. Is used internally here and
184 * there. Do not export, since it doesn't work for types where the content
185 * decides the fixedness (e.g. GST_TYPE_ARRAY).
188 gst_type_is_fixed (GType type)
190 /* the basic int, string, double types */
191 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
194 /* our fundamental types that are certainly not fixed */
195 if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
196 type == GST_TYPE_INT64_RANGE ||
197 type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
200 /* other (boxed) types that are fixed */
201 if (type == GST_TYPE_BUFFER) {
205 if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
206 G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
213 /* GValue functions usable for both regular lists and arrays */
215 gst_value_init_list_or_array (GValue * value)
217 value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
221 copy_garray_of_gstvalue (const GArray * src)
227 dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), len);
228 g_array_set_size (dest, len);
229 for (i = 0; i < len; i++) {
230 gst_value_init_and_copy (&g_array_index (dest, GValue, i),
231 &g_array_index (src, GValue, i));
238 gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
240 dest_value->data[0].v_pointer =
241 copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
245 gst_value_free_list_or_array (GValue * value)
248 GArray *src = (GArray *) value->data[0].v_pointer;
251 if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
252 for (i = 0; i < len; i++) {
253 g_value_unset (&g_array_index (src, GValue, i));
255 g_array_free (src, TRUE);
260 gst_value_list_or_array_peek_pointer (const GValue * value)
262 return value->data[0].v_pointer;
266 gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
267 GTypeCValue * collect_values, guint collect_flags)
269 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
270 value->data[0].v_pointer = collect_values[0].v_pointer;
271 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
273 value->data[0].v_pointer =
274 copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
280 gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
281 GTypeCValue * collect_values, guint collect_flags)
283 GArray **dest = collect_values[0].v_pointer;
286 return g_strdup_printf ("value location for `%s' passed as NULL",
287 G_VALUE_TYPE_NAME (value));
288 if (!value->data[0].v_pointer)
289 return g_strdup_printf ("invalid value given for `%s'",
290 G_VALUE_TYPE_NAME (value));
291 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
292 *dest = (GArray *) value->data[0].v_pointer;
294 *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
300 * gst_value_list_append_value:
301 * @value: a #GValue of type #GST_TYPE_LIST
302 * @append_value: the value to append
304 * Appends @append_value to the GstValueList in @value.
307 gst_value_list_append_value (GValue * value, const GValue * append_value)
311 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
312 g_return_if_fail (G_IS_VALUE (append_value));
314 gst_value_init_and_copy (&val, append_value);
315 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
319 * gst_value_list_prepend_value:
320 * @value: a #GValue of type #GST_TYPE_LIST
321 * @prepend_value: the value to prepend
323 * Prepends @prepend_value to the GstValueList in @value.
326 gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
330 g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
331 g_return_if_fail (G_IS_VALUE (prepend_value));
333 gst_value_init_and_copy (&val, prepend_value);
334 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
338 * gst_value_list_concat:
339 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
343 * Concatenates copies of @value1 and @value2 into a list. Values that are not
344 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
345 * @dest will be initialized to the type #GST_TYPE_LIST.
348 gst_value_list_concat (GValue * dest, const GValue * value1,
349 const GValue * value2)
351 guint i, value1_length, value2_length;
354 g_return_if_fail (dest != NULL);
355 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
356 g_return_if_fail (G_IS_VALUE (value1));
357 g_return_if_fail (G_IS_VALUE (value2));
360 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
362 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
363 g_value_init (dest, GST_TYPE_LIST);
364 array = (GArray *) dest->data[0].v_pointer;
365 g_array_set_size (array, value1_length + value2_length);
367 if (GST_VALUE_HOLDS_LIST (value1)) {
368 for (i = 0; i < value1_length; i++) {
369 gst_value_init_and_copy (&g_array_index (array, GValue, i),
370 VALUE_LIST_GET_VALUE (value1, i));
373 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
376 if (GST_VALUE_HOLDS_LIST (value2)) {
377 for (i = 0; i < value2_length; i++) {
378 gst_value_init_and_copy (&g_array_index (array, GValue,
379 i + value1_length), VALUE_LIST_GET_VALUE (value2, i));
382 gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
388 * gst_value_list_merge:
389 * @dest: (out caller-allocates): an uninitialized #GValue to take the result
393 * Merges copies of @value1 and @value2. Values that are not
394 * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
396 * The result will be put into @dest and will either be a list that will not
397 * contain any duplicates, or a non-list type (if @value1 and @value2
403 gst_value_list_merge (GValue * dest, const GValue * value1,
404 const GValue * value2)
406 guint i, j, k, value1_length, value2_length, skipped;
411 g_return_if_fail (dest != NULL);
412 g_return_if_fail (G_VALUE_TYPE (dest) == 0);
413 g_return_if_fail (G_IS_VALUE (value1));
414 g_return_if_fail (G_IS_VALUE (value2));
417 (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
419 (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
420 g_value_init (dest, GST_TYPE_LIST);
421 array = (GArray *) dest->data[0].v_pointer;
422 g_array_set_size (array, value1_length + value2_length);
424 if (GST_VALUE_HOLDS_LIST (value1)) {
425 for (i = 0; i < value1_length; i++) {
426 gst_value_init_and_copy (&g_array_index (array, GValue, i),
427 VALUE_LIST_GET_VALUE (value1, i));
430 gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
435 if (GST_VALUE_HOLDS_LIST (value2)) {
436 for (i = 0; i < value2_length; i++) {
438 src = VALUE_LIST_GET_VALUE (value2, i);
439 for (k = 0; k < value1_length; k++) {
440 if (gst_value_compare (&g_array_index (array, GValue, k),
441 src) == GST_VALUE_EQUAL) {
448 gst_value_init_and_copy (&g_array_index (array, GValue, j), src);
454 for (k = 0; k < value1_length; k++) {
455 if (gst_value_compare (&g_array_index (array, GValue, k),
456 value2) == GST_VALUE_EQUAL) {
463 gst_value_init_and_copy (&g_array_index (array, GValue, j), value2);
467 guint new_size = value1_length + (value2_length - skipped);
471 g_array_set_size (array, new_size);
475 /* size is 1, take single value in list and make it new dest */
476 single_dest = g_array_index (array, GValue, 0);
478 /* clean up old value allocations: must set array size to 0, because
479 * allocated values are not inited meaning g_value_unset() will not
481 g_array_set_size (array, 0);
482 g_value_unset (dest);
484 /* the single value is our new result */
491 * gst_value_list_get_size:
492 * @value: a #GValue of type #GST_TYPE_LIST
494 * Gets the number of values contained in @value.
496 * Returns: the number of values
499 gst_value_list_get_size (const GValue * value)
501 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
503 return ((GArray *) value->data[0].v_pointer)->len;
507 * gst_value_list_get_value:
508 * @value: a #GValue of type #GST_TYPE_LIST
509 * @index: index of value to get from the list
511 * Gets the value that is a member of the list contained in @value and
512 * has the index @index.
514 * Returns: (transfer none): the value at the given index
517 gst_value_list_get_value (const GValue * value, guint index)
519 g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
520 g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
522 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
527 * gst_value_array_append_value:
528 * @value: a #GValue of type #GST_TYPE_ARRAY
529 * @append_value: the value to append
531 * Appends @append_value to the GstValueArray in @value.
534 gst_value_array_append_value (GValue * value, const GValue * append_value)
538 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
539 g_return_if_fail (G_IS_VALUE (append_value));
541 gst_value_init_and_copy (&val, append_value);
542 g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
546 * gst_value_array_prepend_value:
547 * @value: a #GValue of type #GST_TYPE_ARRAY
548 * @prepend_value: the value to prepend
550 * Prepends @prepend_value to the GstValueArray in @value.
553 gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
557 g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
558 g_return_if_fail (G_IS_VALUE (prepend_value));
560 gst_value_init_and_copy (&val, prepend_value);
561 g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
565 * gst_value_array_get_size:
566 * @value: a #GValue of type #GST_TYPE_ARRAY
568 * Gets the number of values contained in @value.
570 * Returns: the number of values
573 gst_value_array_get_size (const GValue * value)
575 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
577 return ((GArray *) value->data[0].v_pointer)->len;
581 * gst_value_array_get_value:
582 * @value: a #GValue of type #GST_TYPE_ARRAY
583 * @index: index of value to get from the array
585 * Gets the value that is a member of the array contained in @value and
586 * has the index @index.
588 * Returns: (transfer none): the value at the given index
591 gst_value_array_get_value (const GValue * value, guint index)
593 g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
594 g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
596 return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
601 gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
603 gst_value_transform_any_list_string (src_value, dest_value, "{ ", " }");
607 gst_value_transform_array_string (const GValue * src_value, GValue * dest_value)
609 gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
612 /* Do an unordered compare of the contents of a list */
614 gst_value_compare_list (const GValue * value1, const GValue * value2)
617 GArray *array1 = value1->data[0].v_pointer;
618 GArray *array2 = value2->data[0].v_pointer;
623 GstValueCompareFunc compare;
625 /* get length and do initial length check. */
627 if (len != array2->len)
628 return GST_VALUE_UNORDERED;
630 /* place to mark removed value indices of array2 */
631 removed = g_newa (guint8, len);
632 memset (removed, 0, len);
635 /* loop over array1, all items should be in array2. When we find an
636 * item in array2, remove it from array2 by marking it as removed */
637 for (i = 0; i < len; i++) {
638 v1 = &g_array_index (array1, GValue, i);
639 if ((compare = gst_value_get_compare_func (v1))) {
640 for (j = 0; j < len; j++) {
641 /* item is removed, we can skip it */
644 v2 = &g_array_index (array2, GValue, j);
645 if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
646 /* mark item as removed now that we found it in array2 and
647 * decrement the number of remaining items in array2. */
653 /* item in array1 and not in array2, UNORDERED */
655 return GST_VALUE_UNORDERED;
657 return GST_VALUE_UNORDERED;
659 /* if not all items were removed, array2 contained something not in array1 */
661 return GST_VALUE_UNORDERED;
663 /* arrays are equal */
664 return GST_VALUE_EQUAL;
667 /* Perform an ordered comparison of the contents of an array */
669 gst_value_compare_array (const GValue * value1, const GValue * value2)
672 GArray *array1 = value1->data[0].v_pointer;
673 GArray *array2 = value2->data[0].v_pointer;
674 guint len = array1->len;
678 if (len != array2->len)
679 return GST_VALUE_UNORDERED;
681 for (i = 0; i < len; i++) {
682 v1 = &g_array_index (array1, GValue, i);
683 v2 = &g_array_index (array2, GValue, i);
684 if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
685 return GST_VALUE_UNORDERED;
688 return GST_VALUE_EQUAL;
692 gst_value_serialize_list (const GValue * value)
694 return gst_value_serialize_any_list (value, "{ ", " }");
698 gst_value_deserialize_list (GValue * dest, const gchar * s)
700 g_warning ("gst_value_deserialize_list: unimplemented");
705 gst_value_serialize_array (const GValue * value)
707 return gst_value_serialize_any_list (value, "< ", " >");
711 gst_value_deserialize_array (GValue * dest, const gchar * s)
713 g_warning ("gst_value_deserialize_array: unimplemented");
722 gst_value_init_fourcc (GValue * value)
724 value->data[0].v_int = 0;
728 gst_value_copy_fourcc (const GValue * src_value, GValue * dest_value)
730 dest_value->data[0].v_int = src_value->data[0].v_int;
734 gst_value_collect_fourcc (GValue * value, guint n_collect_values,
735 GTypeCValue * collect_values, guint collect_flags)
737 value->data[0].v_int = collect_values[0].v_int;
743 gst_value_lcopy_fourcc (const GValue * value, guint n_collect_values,
744 GTypeCValue * collect_values, guint collect_flags)
746 guint32 *fourcc_p = collect_values[0].v_pointer;
749 return g_strdup_printf ("value location for `%s' passed as NULL",
750 G_VALUE_TYPE_NAME (value));
752 *fourcc_p = value->data[0].v_int;
758 * gst_value_set_fourcc:
759 * @value: a GValue initialized to #GST_TYPE_FOURCC
760 * @fourcc: the #guint32 fourcc to set
762 * Sets @value to @fourcc.
765 gst_value_set_fourcc (GValue * value, guint32 fourcc)
767 g_return_if_fail (GST_VALUE_HOLDS_FOURCC (value));
769 value->data[0].v_int = fourcc;
773 * gst_value_get_fourcc:
774 * @value: a GValue initialized to #GST_TYPE_FOURCC
776 * Gets the #guint32 fourcc contained in @value.
778 * Returns: the #guint32 fourcc contained in @value.
781 gst_value_get_fourcc (const GValue * value)
783 g_return_val_if_fail (GST_VALUE_HOLDS_FOURCC (value), 0);
785 return value->data[0].v_int;
789 gst_value_transform_fourcc_string (const GValue * src_value,
792 guint32 fourcc = src_value->data[0].v_int;
793 gchar fourcc_char[4];
795 fourcc_char[0] = (fourcc >> 0) & 0xff;
796 fourcc_char[1] = (fourcc >> 8) & 0xff;
797 fourcc_char[2] = (fourcc >> 16) & 0xff;
798 fourcc_char[3] = (fourcc >> 24) & 0xff;
800 if ((g_ascii_isalnum (fourcc_char[0]) || fourcc_char[0] == ' ') &&
801 (g_ascii_isalnum (fourcc_char[1]) || fourcc_char[1] == ' ') &&
802 (g_ascii_isalnum (fourcc_char[2]) || fourcc_char[2] == ' ') &&
803 (g_ascii_isalnum (fourcc_char[3]) || fourcc_char[3] == ' ')) {
804 dest_value->data[0].v_pointer =
805 g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
807 dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
812 gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
814 if (value2->data[0].v_int == value1->data[0].v_int)
815 return GST_VALUE_EQUAL;
816 return GST_VALUE_UNORDERED;
820 gst_value_serialize_fourcc (const GValue * value)
822 guint32 fourcc = value->data[0].v_int;
823 gchar fourcc_char[4];
825 fourcc_char[0] = (fourcc >> 0) & 0xff;
826 fourcc_char[1] = (fourcc >> 8) & 0xff;
827 fourcc_char[2] = (fourcc >> 16) & 0xff;
828 fourcc_char[3] = (fourcc >> 24) & 0xff;
830 if ((g_ascii_isalnum (fourcc_char[0]) || fourcc_char[0] == ' ') &&
831 (g_ascii_isalnum (fourcc_char[1]) || fourcc_char[1] == ' ') &&
832 (g_ascii_isalnum (fourcc_char[2]) || fourcc_char[2] == ' ') &&
833 (g_ascii_isalnum (fourcc_char[3]) || fourcc_char[3] == ' ')) {
834 return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
836 return g_strdup_printf ("0x%08x", fourcc);
841 gst_value_deserialize_fourcc (GValue * dest, const gchar * s)
843 gboolean ret = FALSE;
849 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
852 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], ' ');
855 fourcc = GST_MAKE_FOURCC (s[0], s[1], ' ', ' ');
858 fourcc = GST_MAKE_FOURCC (s[0], ' ', ' ', ' ');
860 } else if (g_ascii_isdigit (*s)) {
861 fourcc = strtoul (s, &end, 0);
866 gst_value_set_fourcc (dest, fourcc);
876 gst_value_init_int_range (GValue * value)
878 value->data[0].v_int = 0;
879 value->data[1].v_int = 0;
883 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
885 dest_value->data[0].v_int = src_value->data[0].v_int;
886 dest_value->data[1].v_int = src_value->data[1].v_int;
890 gst_value_collect_int_range (GValue * value, guint n_collect_values,
891 GTypeCValue * collect_values, guint collect_flags)
893 if (n_collect_values != 2)
894 return g_strdup_printf ("not enough value locations for `%s' passed",
895 G_VALUE_TYPE_NAME (value));
896 if (collect_values[0].v_int >= collect_values[1].v_int)
897 return g_strdup_printf ("range start is not smaller than end for `%s'",
898 G_VALUE_TYPE_NAME (value));
900 value->data[0].v_int = collect_values[0].v_int;
901 value->data[1].v_int = collect_values[1].v_int;
907 gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
908 GTypeCValue * collect_values, guint collect_flags)
910 guint32 *int_range_start = collect_values[0].v_pointer;
911 guint32 *int_range_end = collect_values[1].v_pointer;
913 if (!int_range_start)
914 return g_strdup_printf ("start value location for `%s' passed as NULL",
915 G_VALUE_TYPE_NAME (value));
917 return g_strdup_printf ("end value location for `%s' passed as NULL",
918 G_VALUE_TYPE_NAME (value));
920 *int_range_start = value->data[0].v_int;
921 *int_range_end = value->data[1].v_int;
927 * gst_value_set_int_range:
928 * @value: a GValue initialized to GST_TYPE_INT_RANGE
929 * @start: the start of the range
930 * @end: the end of the range
932 * Sets @value to the range specified by @start and @end.
935 gst_value_set_int_range (GValue * value, gint start, gint end)
937 g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
938 g_return_if_fail (start < end);
940 value->data[0].v_int = start;
941 value->data[1].v_int = end;
945 * gst_value_get_int_range_min:
946 * @value: a GValue initialized to GST_TYPE_INT_RANGE
948 * Gets the minimum of the range specified by @value.
950 * Returns: the minimum of the range
953 gst_value_get_int_range_min (const GValue * value)
955 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
957 return value->data[0].v_int;
961 * gst_value_get_int_range_max:
962 * @value: a GValue initialized to GST_TYPE_INT_RANGE
964 * Gets the maximum of the range specified by @value.
966 * Returns: the maxumum of the range
969 gst_value_get_int_range_max (const GValue * value)
971 g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
973 return value->data[1].v_int;
977 gst_value_transform_int_range_string (const GValue * src_value,
980 dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
981 (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
985 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
987 if (value2->data[0].v_int == value1->data[0].v_int &&
988 value2->data[1].v_int == value1->data[1].v_int)
989 return GST_VALUE_EQUAL;
990 return GST_VALUE_UNORDERED;
994 gst_value_serialize_int_range (const GValue * value)
996 return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
997 value->data[1].v_int);
1001 gst_value_deserialize_int_range (GValue * dest, const gchar * s)
1003 g_warning ("unimplemented");
1012 gst_value_init_int64_range (GValue * value)
1014 value->data[0].v_int64 = 0;
1015 value->data[1].v_int64 = 0;
1019 gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
1021 dest_value->data[0].v_int64 = src_value->data[0].v_int64;
1022 dest_value->data[1].v_int64 = src_value->data[1].v_int64;
1026 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
1027 GTypeCValue * collect_values, guint collect_flags)
1029 if (n_collect_values != 2)
1030 return g_strdup_printf ("not enough value locations for `%s' passed",
1031 G_VALUE_TYPE_NAME (value));
1032 if (collect_values[0].v_int64 >= collect_values[1].v_int64)
1033 return g_strdup_printf ("range start is not smaller than end for `%s'",
1034 G_VALUE_TYPE_NAME (value));
1036 value->data[0].v_int64 = collect_values[0].v_int64;
1037 value->data[1].v_int64 = collect_values[1].v_int64;
1043 gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
1044 GTypeCValue * collect_values, guint collect_flags)
1046 guint64 *int_range_start = collect_values[0].v_pointer;
1047 guint64 *int_range_end = collect_values[1].v_pointer;
1049 if (!int_range_start)
1050 return g_strdup_printf ("start value location for `%s' passed as NULL",
1051 G_VALUE_TYPE_NAME (value));
1053 return g_strdup_printf ("end value location for `%s' passed as NULL",
1054 G_VALUE_TYPE_NAME (value));
1056 *int_range_start = value->data[0].v_int64;
1057 *int_range_end = value->data[1].v_int64;
1063 * gst_value_set_int64_range:
1064 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1065 * @start: the start of the range
1066 * @end: the end of the range
1068 * Sets @value to the range specified by @start and @end.
1073 gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
1075 g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
1076 g_return_if_fail (start < end);
1078 value->data[0].v_int64 = start;
1079 value->data[1].v_int64 = end;
1083 * gst_value_get_int64_range_min:
1084 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1086 * Gets the minimum of the range specified by @value.
1088 * Returns: the minimum of the range
1093 gst_value_get_int64_range_min (const GValue * value)
1095 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1097 return value->data[0].v_int64;
1101 * gst_value_get_int64_range_max:
1102 * @value: a GValue initialized to GST_TYPE_INT64_RANGE
1104 * Gets the maximum of the range specified by @value.
1106 * Returns: the maxumum of the range
1111 gst_value_get_int64_range_max (const GValue * value)
1113 g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
1115 return value->data[1].v_int64;
1119 gst_value_transform_int64_range_string (const GValue * src_value,
1120 GValue * dest_value)
1122 dest_value->data[0].v_pointer =
1123 g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
1124 src_value->data[0].v_int64, src_value->data[1].v_int64);
1128 gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
1130 if (value2->data[0].v_int64 == value1->data[0].v_int64 &&
1131 value2->data[1].v_int64 == value1->data[1].v_int64)
1132 return GST_VALUE_EQUAL;
1133 return GST_VALUE_UNORDERED;
1137 gst_value_serialize_int64_range (const GValue * value)
1139 return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
1140 value->data[0].v_int64, value->data[1].v_int64);
1144 gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
1146 g_warning ("unimplemented");
1155 gst_value_init_double_range (GValue * value)
1157 value->data[0].v_double = 0;
1158 value->data[1].v_double = 0;
1162 gst_value_copy_double_range (const GValue * src_value, GValue * dest_value)
1164 dest_value->data[0].v_double = src_value->data[0].v_double;
1165 dest_value->data[1].v_double = src_value->data[1].v_double;
1169 gst_value_collect_double_range (GValue * value, guint n_collect_values,
1170 GTypeCValue * collect_values, guint collect_flags)
1172 if (n_collect_values != 2)
1173 return g_strdup_printf ("not enough value locations for `%s' passed",
1174 G_VALUE_TYPE_NAME (value));
1175 if (collect_values[0].v_double >= collect_values[1].v_double)
1176 return g_strdup_printf ("range start is not smaller than end for `%s'",
1177 G_VALUE_TYPE_NAME (value));
1179 value->data[0].v_double = collect_values[0].v_double;
1180 value->data[1].v_double = collect_values[1].v_double;
1186 gst_value_lcopy_double_range (const GValue * value, guint n_collect_values,
1187 GTypeCValue * collect_values, guint collect_flags)
1189 gdouble *double_range_start = collect_values[0].v_pointer;
1190 gdouble *double_range_end = collect_values[1].v_pointer;
1192 if (!double_range_start)
1193 return g_strdup_printf ("start value location for `%s' passed as NULL",
1194 G_VALUE_TYPE_NAME (value));
1195 if (!double_range_end)
1196 return g_strdup_printf ("end value location for `%s' passed as NULL",
1197 G_VALUE_TYPE_NAME (value));
1199 *double_range_start = value->data[0].v_double;
1200 *double_range_end = value->data[1].v_double;
1206 * gst_value_set_double_range:
1207 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1208 * @start: the start of the range
1209 * @end: the end of the range
1211 * Sets @value to the range specified by @start and @end.
1214 gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
1216 g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
1217 g_return_if_fail (start < end);
1219 value->data[0].v_double = start;
1220 value->data[1].v_double = end;
1224 * gst_value_get_double_range_min:
1225 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1227 * Gets the minimum of the range specified by @value.
1229 * Returns: the minimum of the range
1232 gst_value_get_double_range_min (const GValue * value)
1234 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1236 return value->data[0].v_double;
1240 * gst_value_get_double_range_max:
1241 * @value: a GValue initialized to GST_TYPE_DOUBLE_RANGE
1243 * Gets the maximum of the range specified by @value.
1245 * Returns: the maxumum of the range
1248 gst_value_get_double_range_max (const GValue * value)
1250 g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
1252 return value->data[1].v_double;
1256 gst_value_transform_double_range_string (const GValue * src_value,
1257 GValue * dest_value)
1259 gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
1261 dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
1262 g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
1263 src_value->data[0].v_double),
1264 g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
1265 src_value->data[1].v_double));
1269 gst_value_compare_double_range (const GValue * value1, const GValue * value2)
1271 if (value2->data[0].v_double == value1->data[0].v_double &&
1272 value2->data[0].v_double == value1->data[0].v_double)
1273 return GST_VALUE_EQUAL;
1274 return GST_VALUE_UNORDERED;
1278 gst_value_serialize_double_range (const GValue * value)
1280 gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
1281 gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
1283 g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
1284 g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
1285 return g_strdup_printf ("[ %s, %s ]", d1, d2);
1289 gst_value_deserialize_double_range (GValue * dest, const gchar * s)
1291 g_warning ("unimplemented");
1300 gst_value_init_fraction_range (GValue * value)
1305 ftype = GST_TYPE_FRACTION;
1307 value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
1308 g_value_init (&vals[0], ftype);
1309 g_value_init (&vals[1], ftype);
1313 gst_value_free_fraction_range (GValue * value)
1315 GValue *vals = (GValue *) value->data[0].v_pointer;
1318 g_value_unset (&vals[0]);
1319 g_value_unset (&vals[1]);
1320 g_slice_free1 (2 * sizeof (GValue), vals);
1321 value->data[0].v_pointer = NULL;
1326 gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
1328 GValue *vals = (GValue *) dest_value->data[0].v_pointer;
1329 GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
1332 gst_value_init_fraction_range (dest_value);
1333 vals = dest_value->data[0].v_pointer;
1335 if (src_vals != NULL) {
1336 g_value_copy (&src_vals[0], &vals[0]);
1337 g_value_copy (&src_vals[1], &vals[1]);
1342 gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
1343 GTypeCValue * collect_values, guint collect_flags)
1345 GValue *vals = (GValue *) value->data[0].v_pointer;
1347 if (n_collect_values != 4)
1348 return g_strdup_printf ("not enough value locations for `%s' passed",
1349 G_VALUE_TYPE_NAME (value));
1350 if (collect_values[1].v_int == 0)
1351 return g_strdup_printf ("passed '0' as first denominator for `%s'",
1352 G_VALUE_TYPE_NAME (value));
1353 if (collect_values[3].v_int == 0)
1354 return g_strdup_printf ("passed '0' as second denominator for `%s'",
1355 G_VALUE_TYPE_NAME (value));
1356 if (gst_util_fraction_compare (collect_values[0].v_int,
1357 collect_values[1].v_int, collect_values[2].v_int,
1358 collect_values[3].v_int) >= 0)
1359 return g_strdup_printf ("range start is not smaller than end for `%s'",
1360 G_VALUE_TYPE_NAME (value));
1363 gst_value_init_fraction_range (value);
1364 vals = value->data[0].v_pointer;
1367 gst_value_set_fraction (&vals[0], collect_values[0].v_int,
1368 collect_values[1].v_int);
1369 gst_value_set_fraction (&vals[1], collect_values[2].v_int,
1370 collect_values[3].v_int);
1376 gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
1377 GTypeCValue * collect_values, guint collect_flags)
1380 gint *dest_values[4];
1381 GValue *vals = (GValue *) value->data[0].v_pointer;
1383 if (G_UNLIKELY (n_collect_values != 4))
1384 return g_strdup_printf ("not enough value locations for `%s' passed",
1385 G_VALUE_TYPE_NAME (value));
1387 for (i = 0; i < 4; i++) {
1388 if (G_UNLIKELY (collect_values[i].v_pointer == NULL)) {
1389 return g_strdup_printf ("value location for `%s' passed as NULL",
1390 G_VALUE_TYPE_NAME (value));
1392 dest_values[i] = collect_values[i].v_pointer;
1395 if (G_UNLIKELY (vals == NULL)) {
1396 return g_strdup_printf ("Uninitialised `%s' passed",
1397 G_VALUE_TYPE_NAME (value));
1400 dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
1401 dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
1402 dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
1403 dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
1408 * gst_value_set_fraction_range:
1409 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1410 * @start: the start of the range (a GST_TYPE_FRACTION GValue)
1411 * @end: the end of the range (a GST_TYPE_FRACTION GValue)
1413 * Sets @value to the range specified by @start and @end.
1416 gst_value_set_fraction_range (GValue * value, const GValue * start,
1421 g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1422 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
1423 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
1424 g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
1425 start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
1427 vals = (GValue *) value->data[0].v_pointer;
1429 gst_value_init_fraction_range (value);
1430 vals = value->data[0].v_pointer;
1432 g_value_copy (start, &vals[0]);
1433 g_value_copy (end, &vals[1]);
1437 * gst_value_set_fraction_range_full:
1438 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1439 * @numerator_start: the numerator start of the range
1440 * @denominator_start: the denominator start of the range
1441 * @numerator_end: the numerator end of the range
1442 * @denominator_end: the denominator end of the range
1444 * Sets @value to the range specified by @numerator_start/@denominator_start
1445 * and @numerator_end/@denominator_end.
1448 gst_value_set_fraction_range_full (GValue * value,
1449 gint numerator_start, gint denominator_start,
1450 gint numerator_end, gint denominator_end)
1452 GValue start = { 0 };
1455 g_return_if_fail (value != NULL);
1456 g_return_if_fail (denominator_start != 0);
1457 g_return_if_fail (denominator_end != 0);
1458 g_return_if_fail (gst_util_fraction_compare (numerator_start,
1459 denominator_start, numerator_end, denominator_end) < 0);
1461 g_value_init (&start, GST_TYPE_FRACTION);
1462 g_value_init (&end, GST_TYPE_FRACTION);
1464 gst_value_set_fraction (&start, numerator_start, denominator_start);
1465 gst_value_set_fraction (&end, numerator_end, denominator_end);
1466 gst_value_set_fraction_range (value, &start, &end);
1468 g_value_unset (&start);
1469 g_value_unset (&end);
1473 * gst_value_get_fraction_range_min:
1474 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1476 * Gets the minimum of the range specified by @value.
1478 * Returns: the minimum of the range
1481 gst_value_get_fraction_range_min (const GValue * value)
1485 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1487 vals = (GValue *) value->data[0].v_pointer;
1496 * gst_value_get_fraction_range_max:
1497 * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
1499 * Gets the maximum of the range specified by @value.
1501 * Returns: the maximum of the range
1504 gst_value_get_fraction_range_max (const GValue * value)
1508 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
1510 vals = (GValue *) value->data[0].v_pointer;
1519 gst_value_serialize_fraction_range (const GValue * value)
1521 GValue *vals = (GValue *) value->data[0].v_pointer;
1525 retval = g_strdup ("[ 0/1, 0/1 ]");
1529 start = gst_value_serialize_fraction (&vals[0]);
1530 end = gst_value_serialize_fraction (&vals[1]);
1532 retval = g_strdup_printf ("[ %s, %s ]", start, end);
1541 gst_value_transform_fraction_range_string (const GValue * src_value,
1542 GValue * dest_value)
1544 dest_value->data[0].v_pointer =
1545 gst_value_serialize_fraction_range (src_value);
1549 gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
1551 GValue *vals1, *vals2;
1552 GstValueCompareFunc compare;
1554 if (value2->data[0].v_pointer == value1->data[0].v_pointer)
1555 return GST_VALUE_EQUAL; /* Only possible if both are NULL */
1557 if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
1558 return GST_VALUE_UNORDERED;
1560 vals1 = (GValue *) value1->data[0].v_pointer;
1561 vals2 = (GValue *) value2->data[0].v_pointer;
1562 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
1563 if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
1565 gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
1567 return GST_VALUE_EQUAL;
1569 return GST_VALUE_UNORDERED;
1573 gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
1575 g_warning ("unimplemented");
1584 * gst_value_set_caps:
1585 * @value: a GValue initialized to GST_TYPE_CAPS
1586 * @caps: (transfer none): the caps to set the value to
1588 * Sets the contents of @value to @caps. A reference to the
1589 * provided @caps will be taken by the @value.
1592 gst_value_set_caps (GValue * value, const GstCaps * caps)
1594 g_return_if_fail (G_IS_VALUE (value));
1595 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
1596 g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
1598 g_value_set_boxed (value, caps);
1602 * gst_value_get_caps:
1603 * @value: a GValue initialized to GST_TYPE_CAPS
1605 * Gets the contents of @value. The reference count of the returned
1606 * #GstCaps will not be modified, therefore the caller must take one
1607 * before getting rid of the @value.
1609 * Returns: (transfer none): the contents of @value
1612 gst_value_get_caps (const GValue * value)
1614 g_return_val_if_fail (G_IS_VALUE (value), NULL);
1615 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
1617 return (GstCaps *) g_value_get_boxed (value);
1621 gst_value_serialize_caps (const GValue * value)
1623 GstCaps *caps = g_value_get_boxed (value);
1625 return gst_caps_to_string (caps);
1629 gst_value_deserialize_caps (GValue * dest, const gchar * s)
1633 caps = gst_caps_from_string (s);
1636 g_value_take_boxed (dest, caps);
1647 * gst_value_set_structure:
1648 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1649 * @structure: the structure to set the value to
1651 * Sets the contents of @value to @structure. The actual
1656 gst_value_set_structure (GValue * value, const GstStructure * structure)
1658 g_return_if_fail (G_IS_VALUE (value));
1659 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
1660 g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
1662 g_value_set_boxed (value, structure);
1666 * gst_value_get_structure:
1667 * @value: a GValue initialized to GST_TYPE_STRUCTURE
1669 * Gets the contents of @value.
1671 * Returns: (transfer none): the contents of @value
1675 const GstStructure *
1676 gst_value_get_structure (const GValue * value)
1678 g_return_val_if_fail (G_IS_VALUE (value), NULL);
1679 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
1681 return (GstStructure *) g_value_get_boxed (value);
1685 gst_value_serialize_structure (const GValue * value)
1687 GstStructure *structure = g_value_get_boxed (value);
1689 return gst_string_take_and_wrap (gst_structure_to_string (structure));
1693 gst_value_deserialize_structure (GValue * dest, const gchar * s)
1695 GstStructure *structure;
1698 structure = gst_structure_from_string (s, NULL);
1700 gchar *str = gst_string_unwrap (s);
1702 if (G_UNLIKELY (!str))
1705 structure = gst_structure_from_string (str, NULL);
1709 if (G_LIKELY (structure)) {
1710 g_value_take_boxed (dest, structure);
1721 gst_value_compare_buffer (const GValue * value1, const GValue * value2)
1723 GstBuffer *buf1 = gst_value_get_buffer (value1);
1724 GstBuffer *buf2 = gst_value_get_buffer (value2);
1726 gpointer data1, data2;
1727 gint result = GST_VALUE_UNORDERED;
1729 size1 = gst_buffer_get_size (buf1);
1730 size2 = gst_buffer_get_size (buf2);
1733 return GST_VALUE_UNORDERED;
1736 return GST_VALUE_EQUAL;
1738 data1 = gst_buffer_map (buf1, &size1, NULL, GST_MAP_READ);
1739 data2 = gst_buffer_map (buf2, &size2, NULL, GST_MAP_READ);
1743 if (memcmp (data1, data2, size1) == 0)
1744 result = GST_VALUE_EQUAL;
1746 gst_buffer_unmap (buf2, data2, size2);
1747 gst_buffer_unmap (buf1, data1, size1);
1753 gst_value_serialize_buffer (const GValue * value)
1761 buffer = gst_value_get_buffer (value);
1765 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1767 string = g_malloc (size * 2 + 1);
1768 for (i = 0; i < size; i++) {
1769 sprintf (string + i * 2, "%02x", data[i]);
1771 string[size * 2] = 0;
1773 gst_buffer_unmap (buffer, data, size);
1779 gst_value_deserialize_buffer (GValue * dest, const gchar * s)
1792 buffer = gst_buffer_new_allocate (NULL, len / 2, 0);
1793 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_WRITE);
1795 for (i = 0; i < len / 2; i++) {
1796 if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
1799 ts[0] = s[i * 2 + 0];
1800 ts[1] = s[i * 2 + 1];
1803 data[i] = (guint8) strtoul (ts, NULL, 16);
1805 gst_buffer_unmap (buffer, data, size);
1807 gst_value_take_buffer (dest, buffer);
1818 gst_buffer_unref (buffer);
1819 gst_buffer_unmap (buffer, data, size);
1830 gst_value_compare_boolean (const GValue * value1, const GValue * value2)
1832 if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
1833 return GST_VALUE_EQUAL;
1834 return GST_VALUE_UNORDERED;
1838 gst_value_serialize_boolean (const GValue * value)
1840 if (value->data[0].v_int) {
1841 return g_strdup ("true");
1843 return g_strdup ("false");
1847 gst_value_deserialize_boolean (GValue * dest, const gchar * s)
1849 gboolean ret = FALSE;
1851 if (g_ascii_strcasecmp (s, "true") == 0 ||
1852 g_ascii_strcasecmp (s, "yes") == 0 ||
1853 g_ascii_strcasecmp (s, "t") == 0 || strcmp (s, "1") == 0) {
1854 g_value_set_boolean (dest, TRUE);
1856 } else if (g_ascii_strcasecmp (s, "false") == 0 ||
1857 g_ascii_strcasecmp (s, "no") == 0 ||
1858 g_ascii_strcasecmp (s, "f") == 0 || strcmp (s, "0") == 0) {
1859 g_value_set_boolean (dest, FALSE);
1866 #define CREATE_SERIALIZATION_START(_type,_macro) \
1868 gst_value_compare_ ## _type \
1869 (const GValue * value1, const GValue * value2) \
1871 g ## _type val1 = g_value_get_ ## _type (value1); \
1872 g ## _type val2 = g_value_get_ ## _type (value2); \
1874 return GST_VALUE_GREATER_THAN; \
1876 return GST_VALUE_LESS_THAN; \
1877 return GST_VALUE_EQUAL; \
1881 gst_value_serialize_ ## _type (const GValue * value) \
1883 GValue val = { 0, }; \
1884 g_value_init (&val, G_TYPE_STRING); \
1885 if (!g_value_transform (value, &val)) \
1886 g_assert_not_reached (); \
1887 /* NO_COPY_MADNESS!!! */ \
1888 return (char *) g_value_get_string (&val); \
1891 /* deserialize the given s into to as a gint64.
1892 * check if the result is actually storeable in the given size number of
1896 gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
1897 gint64 min, gint64 max, gint size)
1899 gboolean ret = FALSE;
1904 *to = g_ascii_strtoull (s, &end, 0);
1905 /* a range error is a definitive no-no */
1906 if (errno == ERANGE) {
1913 if (g_ascii_strcasecmp (s, "little_endian") == 0) {
1914 *to = G_LITTLE_ENDIAN;
1916 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) {
1919 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) {
1922 } else if (g_ascii_strcasecmp (s, "min") == 0) {
1925 } else if (g_ascii_strcasecmp (s, "max") == 0) {
1931 /* by definition, a gint64 fits into a gint64; so ignore those */
1932 if (size != sizeof (mask)) {
1934 /* for positive numbers, we create a mask of 1's outside of the range
1935 * and 0's inside the range. An and will thus keep only 1 bits
1936 * outside of the range */
1937 mask <<= (size * 8);
1938 if ((mask & *to) != 0) {
1942 /* for negative numbers, we do a 2's complement version */
1943 mask <<= ((size * 8) - 1);
1944 if ((mask & *to) != mask) {
1953 #define CREATE_SERIALIZATION(_type,_macro) \
1954 CREATE_SERIALIZATION_START(_type,_macro) \
1957 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1961 if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
1962 G_MAX ## _macro, sizeof (g ## _type))) { \
1963 g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
1970 #define CREATE_USERIALIZATION(_type,_macro) \
1971 CREATE_SERIALIZATION_START(_type,_macro) \
1974 gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
1978 gboolean ret = FALSE; \
1981 x = g_ascii_strtoull (s, &end, 0); \
1982 /* a range error is a definitive no-no */ \
1983 if (errno == ERANGE) { \
1986 /* the cast ensures the range check later on makes sense */ \
1987 x = (g ## _type) x; \
1991 if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
1992 x = G_LITTLE_ENDIAN; \
1994 } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
1997 } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
2000 } else if (g_ascii_strcasecmp (s, "min") == 0) { \
2003 } else if (g_ascii_strcasecmp (s, "max") == 0) { \
2004 x = G_MAX ## _macro; \
2009 if (x > G_MAX ## _macro) { \
2012 g_value_set_ ## _type (dest, x); \
2018 #define REGISTER_SERIALIZATION(_gtype, _type) \
2020 static const GstValueTable gst_value = { \
2022 gst_value_compare_ ## _type, \
2023 gst_value_serialize_ ## _type, \
2024 gst_value_deserialize_ ## _type, \
2027 gst_value_register (&gst_value); \
2030 CREATE_SERIALIZATION (int, INT);
2031 CREATE_SERIALIZATION (int64, INT64);
2032 CREATE_SERIALIZATION (long, LONG);
2034 CREATE_USERIALIZATION (uint, UINT);
2035 CREATE_USERIALIZATION (uint64, UINT64);
2036 CREATE_USERIALIZATION (ulong, ULONG);
2038 /* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
2040 #define G_MAXUCHAR 255
2042 CREATE_USERIALIZATION (uchar, UCHAR);
2048 gst_value_compare_double (const GValue * value1, const GValue * value2)
2050 if (value1->data[0].v_double > value2->data[0].v_double)
2051 return GST_VALUE_GREATER_THAN;
2052 if (value1->data[0].v_double < value2->data[0].v_double)
2053 return GST_VALUE_LESS_THAN;
2054 if (value1->data[0].v_double == value2->data[0].v_double)
2055 return GST_VALUE_EQUAL;
2056 return GST_VALUE_UNORDERED;
2060 gst_value_serialize_double (const GValue * value)
2062 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2064 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
2065 return g_strdup (d);
2069 gst_value_deserialize_double (GValue * dest, const gchar * s)
2072 gboolean ret = FALSE;
2075 x = g_ascii_strtod (s, &end);
2079 if (g_ascii_strcasecmp (s, "min") == 0) {
2082 } else if (g_ascii_strcasecmp (s, "max") == 0) {
2088 g_value_set_double (dest, x);
2098 gst_value_compare_float (const GValue * value1, const GValue * value2)
2100 if (value1->data[0].v_float > value2->data[0].v_float)
2101 return GST_VALUE_GREATER_THAN;
2102 if (value1->data[0].v_float < value2->data[0].v_float)
2103 return GST_VALUE_LESS_THAN;
2104 if (value1->data[0].v_float == value2->data[0].v_float)
2105 return GST_VALUE_EQUAL;
2106 return GST_VALUE_UNORDERED;
2110 gst_value_serialize_float (const GValue * value)
2112 gchar d[G_ASCII_DTOSTR_BUF_SIZE];
2114 g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_float);
2115 return g_strdup (d);
2119 gst_value_deserialize_float (GValue * dest, const gchar * s)
2122 gboolean ret = FALSE;
2125 x = g_ascii_strtod (s, &end);
2129 if (g_ascii_strcasecmp (s, "min") == 0) {
2132 } else if (g_ascii_strcasecmp (s, "max") == 0) {
2137 if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
2140 g_value_set_float (dest, (float) x);
2150 gst_value_compare_string (const GValue * value1, const GValue * value2)
2152 if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
2153 /* if only one is NULL, no match - otherwise both NULL == EQUAL */
2154 if (value1->data[0].v_pointer != value2->data[0].v_pointer)
2155 return GST_VALUE_UNORDERED;
2157 gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
2160 return GST_VALUE_LESS_THAN;
2162 return GST_VALUE_GREATER_THAN;
2165 return GST_VALUE_EQUAL;
2169 gst_string_measure_wrapping (const gchar * s)
2172 gboolean wrap = FALSE;
2174 if (G_UNLIKELY (s == NULL))
2177 /* Special case: the actual string NULL needs wrapping */
2178 if (G_UNLIKELY (strcmp (s, "NULL") == 0))
2183 if (GST_ASCII_IS_STRING (*s)) {
2185 } else if (*s < 0x20 || *s >= 0x7f) {
2195 /* Wrap the string if we found something that needs
2196 * wrapping, or the empty string (len == 0) */
2197 return (wrap || len == 0) ? len : -1;
2201 gst_string_wrap_inner (const gchar * s, gint len)
2205 e = d = g_malloc (len + 3);
2209 if (GST_ASCII_IS_STRING (*s)) {
2211 } else if (*s < 0x20 || *s >= 0x7f) {
2213 *e++ = '0' + ((*(guchar *) s) >> 6);
2214 *e++ = '0' + (((*s) >> 3) & 0x7);
2215 *e++ = '0' + ((*s++) & 0x7);
2224 g_assert (e - d <= len + 3);
2228 /* Do string wrapping/escaping */
2230 gst_string_wrap (const gchar * s)
2232 gint len = gst_string_measure_wrapping (s);
2234 if (G_LIKELY (len < 0))
2235 return g_strdup (s);
2237 return gst_string_wrap_inner (s, len);
2240 /* Same as above, but take ownership of the string */
2242 gst_string_take_and_wrap (gchar * s)
2245 gint len = gst_string_measure_wrapping (s);
2247 if (G_LIKELY (len < 0))
2250 out = gst_string_wrap_inner (s, len);
2257 * This function takes a string delimited with double quotes (")
2258 * and unescapes any \xxx octal numbers.
2260 * If sequences of \y are found where y is not in the range of
2261 * 0->3, y is copied unescaped.
2263 * If \xyy is found where x is an octal number but y is not, an
2264 * error is encountered and NULL is returned.
2266 * the input string must be \0 terminated.
2269 gst_string_unwrap (const gchar * s)
2272 gchar *read, *write;
2274 /* NULL string returns NULL */
2278 /* strings not starting with " are invalid */
2282 /* make copy of original string to hold the result. This
2283 * string will always be smaller than the original */
2288 /* need to move to the next position as we parsed the " */
2292 if (GST_ASCII_IS_STRING (*read)) {
2293 /* normal chars are just copied */
2295 } else if (*read == '"') {
2296 /* quote marks end of string */
2298 } else if (*read == '\\') {
2299 /* got an escape char, move to next position to read a tripplet
2300 * of octal numbers */
2302 /* is the next char a possible first octal number? */
2303 if (*read >= '0' && *read <= '3') {
2304 /* parse other 2 numbers, if one of them is not in the range of
2305 * an octal number, we error. We also catch the case where a zero
2306 * byte is found here. */
2307 if (read[1] < '0' || read[1] > '7' || read[2] < '0' || read[2] > '7')
2310 /* now convert the octal number to a byte again. */
2311 *write++ = ((read[0] - '0') << 6) +
2312 ((read[1] - '0') << 3) + (read[2] - '0');
2316 /* if we run into a \0 here, we definately won't get a quote later */
2320 /* else copy \X sequence */
2324 /* weird character, error */
2328 /* if the string is not ending in " and zero terminated, we error */
2329 if (*read != '"' || read[1] != '\0')
2332 /* null terminate result string and return */
2342 gst_value_serialize_string (const GValue * value)
2344 return gst_string_wrap (value->data[0].v_pointer);
2348 gst_value_deserialize_string (GValue * dest, const gchar * s)
2350 if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
2351 g_value_set_string (dest, NULL);
2353 } else if (G_LIKELY (*s != '"')) {
2354 if (!g_utf8_validate (s, -1, NULL))
2356 g_value_set_string (dest, s);
2359 gchar *str = gst_string_unwrap (s);
2360 if (G_UNLIKELY (!str))
2362 g_value_take_string (dest, str);
2373 gst_value_compare_enum (const GValue * value1, const GValue * value2)
2375 GEnumValue *en1, *en2;
2376 GEnumClass *klass1 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2377 GEnumClass *klass2 = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2379 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2380 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2381 en1 = g_enum_get_value (klass1, g_value_get_enum (value1));
2382 en2 = g_enum_get_value (klass2, g_value_get_enum (value2));
2383 g_type_class_unref (klass1);
2384 g_type_class_unref (klass2);
2385 g_return_val_if_fail (en1, GST_VALUE_UNORDERED);
2386 g_return_val_if_fail (en2, GST_VALUE_UNORDERED);
2387 if (en1->value < en2->value)
2388 return GST_VALUE_LESS_THAN;
2389 if (en1->value > en2->value)
2390 return GST_VALUE_GREATER_THAN;
2392 return GST_VALUE_EQUAL;
2396 gst_value_serialize_enum (const GValue * value)
2399 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (value));
2401 g_return_val_if_fail (klass, NULL);
2402 en = g_enum_get_value (klass, g_value_get_enum (value));
2403 g_type_class_unref (klass);
2405 /* might be one of the custom formats registered later */
2406 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
2407 const GstFormatDefinition *format_def;
2409 format_def = gst_format_get_details (g_value_get_enum (value));
2410 g_return_val_if_fail (format_def != NULL, NULL);
2411 return g_strdup (format_def->description);
2414 g_return_val_if_fail (en, NULL);
2415 return g_strdup (en->value_name);
2419 gst_value_deserialize_enum_iter_cmp (const GValue * format_def_value,
2422 const GstFormatDefinition *format_def =
2423 g_value_get_pointer (format_def_value);
2425 if (g_ascii_strcasecmp (s, format_def->nick) == 0)
2428 return g_ascii_strcasecmp (s, format_def->description);
2432 gst_value_deserialize_enum (GValue * dest, const gchar * s)
2435 gchar *endptr = NULL;
2436 GEnumClass *klass = (GEnumClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2438 g_return_val_if_fail (klass, FALSE);
2439 if (!(en = g_enum_get_value_by_name (klass, s))) {
2440 if (!(en = g_enum_get_value_by_nick (klass, s))) {
2441 gint i = strtol (s, &endptr, 0);
2443 if (endptr && *endptr == '\0') {
2444 en = g_enum_get_value (klass, i);
2448 g_type_class_unref (klass);
2450 /* might be one of the custom formats registered later */
2451 if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
2452 GValue res = { 0, };
2453 const GstFormatDefinition *format_def;
2457 iter = gst_format_iterate_definitions ();
2459 found = gst_iterator_find_custom (iter,
2460 (GCompareFunc) gst_value_deserialize_enum_iter_cmp, &res, (gpointer) s);
2462 g_return_val_if_fail (found, FALSE);
2463 format_def = g_value_get_pointer (&res);
2464 g_return_val_if_fail (format_def != NULL, FALSE);
2465 g_value_set_enum (dest, (gint) format_def->value);
2466 g_value_unset (&res);
2467 gst_iterator_free (iter);
2471 g_return_val_if_fail (en, FALSE);
2472 g_value_set_enum (dest, en->value);
2480 /* we just compare the value here */
2482 gst_value_compare_flags (const GValue * value1, const GValue * value2)
2485 GFlagsClass *klass1 =
2486 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value1));
2487 GFlagsClass *klass2 =
2488 (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value2));
2490 g_return_val_if_fail (klass1, GST_VALUE_UNORDERED);
2491 g_return_val_if_fail (klass2, GST_VALUE_UNORDERED);
2492 fl1 = g_value_get_flags (value1);
2493 fl2 = g_value_get_flags (value2);
2494 g_type_class_unref (klass1);
2495 g_type_class_unref (klass2);
2497 return GST_VALUE_LESS_THAN;
2499 return GST_VALUE_GREATER_THAN;
2501 return GST_VALUE_EQUAL;
2504 /* the different flags are serialized separated with a + */
2506 gst_value_serialize_flags (const GValue * value)
2510 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (value));
2511 gchar *result, *tmp;
2512 gboolean first = TRUE;
2514 g_return_val_if_fail (klass, NULL);
2516 flags = g_value_get_flags (value);
2518 /* if no flags are set, try to serialize to the _NONE string */
2520 fl = g_flags_get_first_value (klass, flags);
2521 return g_strdup (fl->value_name);
2524 /* some flags are set, so serialize one by one */
2525 result = g_strdup ("");
2527 fl = g_flags_get_first_value (klass, flags);
2529 tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
2535 flags &= ~fl->value;
2538 g_type_class_unref (klass);
2544 gst_value_deserialize_flags (GValue * dest, const gchar * s)
2547 gchar *endptr = NULL;
2548 GFlagsClass *klass = (GFlagsClass *) g_type_class_ref (G_VALUE_TYPE (dest));
2553 g_return_val_if_fail (klass, FALSE);
2555 /* split into parts delimited with + */
2556 split = g_strsplit (s, "+", 0);
2560 /* loop over each part */
2562 if (!(fl = g_flags_get_value_by_name (klass, split[i]))) {
2563 if (!(fl = g_flags_get_value_by_nick (klass, split[i]))) {
2564 gint val = strtol (split[i], &endptr, 0);
2566 /* just or numeric value */
2567 if (endptr && *endptr == '\0') {
2578 g_type_class_unref (klass);
2579 g_value_set_flags (dest, flags);
2589 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
2590 const GValue * src2)
2592 if (src2->data[0].v_int <= src1->data[0].v_int &&
2593 src2->data[1].v_int >= src1->data[0].v_int) {
2594 gst_value_init_and_copy (dest, src2);
2601 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
2602 const GValue * src2)
2607 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2608 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2611 g_value_init (dest, GST_TYPE_INT_RANGE);
2612 gst_value_set_int_range (dest,
2613 MIN (src1->data[0].v_int, src2->data[0].v_int),
2614 MAX (src1->data[1].v_int, src2->data[1].v_int));
2626 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
2627 const GValue * src2)
2629 if (src2->data[0].v_int <= src1->data[0].v_int &&
2630 src2->data[1].v_int >= src1->data[0].v_int) {
2631 gst_value_init_and_copy (dest, src1);
2639 gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
2640 const GValue * src2)
2645 min = MAX (src1->data[0].v_int, src2->data[0].v_int);
2646 max = MIN (src1->data[1].v_int, src2->data[1].v_int);
2649 g_value_init (dest, GST_TYPE_INT_RANGE);
2650 gst_value_set_int_range (dest, min, max);
2654 g_value_init (dest, G_TYPE_INT);
2655 g_value_set_int (dest, min);
2663 gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
2664 const GValue * src2)
2666 if (src2->data[0].v_int64 <= src1->data[0].v_int64 &&
2667 src2->data[1].v_int64 >= src1->data[0].v_int64) {
2668 gst_value_init_and_copy (dest, src1);
2676 gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
2677 const GValue * src2)
2682 min = MAX (src1->data[0].v_int64, src2->data[0].v_int64);
2683 max = MIN (src1->data[1].v_int64, src2->data[1].v_int64);
2686 g_value_init (dest, GST_TYPE_INT64_RANGE);
2687 gst_value_set_int64_range (dest, min, max);
2691 g_value_init (dest, G_TYPE_INT64);
2692 g_value_set_int64 (dest, min);
2700 gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
2701 const GValue * src2)
2703 if (src2->data[0].v_double <= src1->data[0].v_double &&
2704 src2->data[1].v_double >= src1->data[0].v_double) {
2705 gst_value_init_and_copy (dest, src1);
2713 gst_value_intersect_double_range_double_range (GValue * dest,
2714 const GValue * src1, const GValue * src2)
2719 min = MAX (src1->data[0].v_double, src2->data[0].v_double);
2720 max = MIN (src1->data[1].v_double, src2->data[1].v_double);
2723 g_value_init (dest, GST_TYPE_DOUBLE_RANGE);
2724 gst_value_set_double_range (dest, min, max);
2728 g_value_init (dest, G_TYPE_DOUBLE);
2729 g_value_set_int (dest, (int) min);
2737 gst_value_intersect_list (GValue * dest, const GValue * value1,
2738 const GValue * value2)
2741 GValue intersection = { 0, };
2742 gboolean ret = FALSE;
2744 size = VALUE_LIST_SIZE (value1);
2745 for (i = 0; i < size; i++) {
2746 const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
2748 if (gst_value_intersect (&intersection, cur, value2)) {
2751 gst_value_init_and_copy (dest, &intersection);
2753 } else if (GST_VALUE_HOLDS_LIST (dest)) {
2754 gst_value_list_append_value (dest, &intersection);
2756 GValue temp = { 0, };
2758 gst_value_init_and_copy (&temp, dest);
2759 g_value_unset (dest);
2760 gst_value_list_concat (dest, &temp, &intersection);
2761 g_value_unset (&temp);
2763 g_value_unset (&intersection);
2771 gst_value_intersect_array (GValue * dest, const GValue * src1,
2772 const GValue * src2)
2778 /* only works on similar-sized arrays */
2779 size = gst_value_array_get_size (src1);
2780 if (size != gst_value_array_get_size (src2))
2782 g_value_init (dest, GST_TYPE_ARRAY);
2784 for (n = 0; n < size; n++) {
2785 if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
2786 gst_value_array_get_value (src2, n))) {
2787 g_value_unset (dest);
2790 gst_value_array_append_value (dest, &val);
2791 g_value_unset (&val);
2798 gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
2799 const GValue * src2)
2803 GstValueCompareFunc compare;
2805 vals = src2->data[0].v_pointer;
2810 if ((compare = gst_value_get_compare_func (src1))) {
2811 res1 = gst_value_compare_with_func (&vals[0], src1, compare);
2812 res2 = gst_value_compare_with_func (&vals[1], src1, compare);
2814 if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
2815 (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
2816 gst_value_init_and_copy (dest, src1);
2825 gst_value_intersect_fraction_range_fraction_range (GValue * dest,
2826 const GValue * src1, const GValue * src2)
2831 GValue *vals1, *vals2;
2832 GstValueCompareFunc compare;
2834 vals1 = src1->data[0].v_pointer;
2835 vals2 = src2->data[0].v_pointer;
2836 g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
2838 if ((compare = gst_value_get_compare_func (&vals1[0]))) {
2839 /* min = MAX (src1.start, src2.start) */
2840 res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
2841 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2842 if (res == GST_VALUE_LESS_THAN)
2843 min = &vals2[0]; /* Take the max of the 2 */
2847 /* max = MIN (src1.end, src2.end) */
2848 res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
2849 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2850 if (res == GST_VALUE_GREATER_THAN)
2851 max = &vals2[1]; /* Take the min of the 2 */
2855 res = gst_value_compare_with_func (min, max, compare);
2856 g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
2857 if (res == GST_VALUE_LESS_THAN) {
2858 g_value_init (dest, GST_TYPE_FRACTION_RANGE);
2859 vals1 = dest->data[0].v_pointer;
2860 g_value_copy (min, &vals1[0]);
2861 g_value_copy (max, &vals1[1]);
2864 if (res == GST_VALUE_EQUAL) {
2865 gst_value_init_and_copy (dest, min);
2878 gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
2879 const GValue * subtrahend)
2881 gint min = gst_value_get_int_range_min (subtrahend);
2882 gint max = gst_value_get_int_range_max (subtrahend);
2883 gint val = g_value_get_int (minuend);
2885 /* subtracting a range from an int only works if the int is not in the
2887 if (val < min || val > max) {
2888 /* and the result is the int */
2889 gst_value_init_and_copy (dest, minuend);
2895 /* creates a new int range based on input values.
2898 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
2903 GValue *pv1, *pv2; /* yeah, hungarian! */
2905 if (min1 <= max1 && min2 <= max2) {
2908 } else if (min1 <= max1) {
2911 } else if (min2 <= max2) {
2919 g_value_init (pv1, GST_TYPE_INT_RANGE);
2920 gst_value_set_int_range (pv1, min1, max1);
2921 } else if (min1 == max1) {
2922 g_value_init (pv1, G_TYPE_INT);
2923 g_value_set_int (pv1, min1);
2926 g_value_init (pv2, GST_TYPE_INT_RANGE);
2927 gst_value_set_int_range (pv2, min2, max2);
2928 } else if (min2 == max2) {
2929 g_value_init (pv2, G_TYPE_INT);
2930 g_value_set_int (pv2, min2);
2933 if (min1 <= max1 && min2 <= max2) {
2934 gst_value_list_concat (dest, pv1, pv2);
2935 g_value_unset (pv1);
2936 g_value_unset (pv2);
2942 gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
2943 const GValue * subtrahend)
2945 gint min = gst_value_get_int_range_min (minuend);
2946 gint max = gst_value_get_int_range_max (minuend);
2947 gint val = g_value_get_int (subtrahend);
2949 g_return_val_if_fail (min < max, FALSE);
2951 /* value is outside of the range, return range unchanged */
2952 if (val < min || val > max) {
2953 gst_value_init_and_copy (dest, minuend);
2956 /* max must be MAXINT too as val <= max */
2957 if (val == G_MAXINT) {
2961 /* min must be MININT too as val >= max */
2962 if (val == G_MININT) {
2966 gst_value_create_new_range (dest, min, val - 1, val + 1, max);
2972 gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
2973 const GValue * subtrahend)
2975 gint min1 = gst_value_get_int_range_min (minuend);
2976 gint max1 = gst_value_get_int_range_max (minuend);
2977 gint min2 = gst_value_get_int_range_min (subtrahend);
2978 gint max2 = gst_value_get_int_range_max (subtrahend);
2980 if (max2 == G_MAXINT && min2 == G_MININT) {
2982 } else if (max2 == G_MAXINT) {
2983 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
2984 } else if (min2 == G_MININT) {
2985 return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
2987 return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
2988 MAX (max2 + 1, min1), max1);
2993 gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
2994 const GValue * subtrahend)
2996 gint64 min = gst_value_get_int64_range_min (subtrahend);
2997 gint64 max = gst_value_get_int64_range_max (subtrahend);
2998 gint64 val = g_value_get_int64 (minuend);
3000 /* subtracting a range from an int64 only works if the int64 is not in the
3002 if (val < min || val > max) {
3003 /* and the result is the int64 */
3004 gst_value_init_and_copy (dest, minuend);
3010 /* creates a new int64 range based on input values.
3013 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
3014 gint64 min2, gint64 max2)
3018 GValue *pv1, *pv2; /* yeah, hungarian! */
3020 if (min1 <= max1 && min2 <= max2) {
3023 } else if (min1 <= max1) {
3026 } else if (min2 <= max2) {
3034 g_value_init (pv1, GST_TYPE_INT64_RANGE);
3035 gst_value_set_int64_range (pv1, min1, max1);
3036 } else if (min1 == max1) {
3037 g_value_init (pv1, G_TYPE_INT64);
3038 g_value_set_int64 (pv1, min1);
3041 g_value_init (pv2, GST_TYPE_INT64_RANGE);
3042 gst_value_set_int64_range (pv2, min2, max2);
3043 } else if (min2 == max2) {
3044 g_value_init (pv2, G_TYPE_INT64);
3045 g_value_set_int64 (pv2, min2);
3048 if (min1 <= max1 && min2 <= max2) {
3049 gst_value_list_concat (dest, pv1, pv2);
3050 g_value_unset (pv1);
3051 g_value_unset (pv2);
3057 gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
3058 const GValue * subtrahend)
3060 gint64 min = gst_value_get_int64_range_min (minuend);
3061 gint64 max = gst_value_get_int64_range_max (minuend);
3062 gint64 val = g_value_get_int64 (subtrahend);
3064 g_return_val_if_fail (min < max, FALSE);
3066 /* value is outside of the range, return range unchanged */
3067 if (val < min || val > max) {
3068 gst_value_init_and_copy (dest, minuend);
3071 /* max must be MAXINT64 too as val <= max */
3072 if (val == G_MAXINT64) {
3076 /* min must be MININT64 too as val >= max */
3077 if (val == G_MININT64) {
3081 gst_value_create_new_int64_range (dest, min, val - 1, val + 1, max);
3087 gst_value_subtract_int64_range_int64_range (GValue * dest,
3088 const GValue * minuend, const GValue * subtrahend)
3090 gint64 min1 = gst_value_get_int64_range_min (minuend);
3091 gint64 max1 = gst_value_get_int64_range_max (minuend);
3092 gint64 min2 = gst_value_get_int64_range_min (subtrahend);
3093 gint64 max2 = gst_value_get_int64_range_max (subtrahend);
3095 if (max2 == G_MAXINT64 && min2 == G_MININT64) {
3097 } else if (max2 == G_MAXINT64) {
3098 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
3100 } else if (min2 == G_MININT64) {
3101 return gst_value_create_new_int64_range (dest, MAX (max2 + 1, min1), max1,
3104 return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
3105 MAX (max2 + 1, min1), max1);
3110 gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
3111 const GValue * subtrahend)
3113 gdouble min = gst_value_get_double_range_min (subtrahend);
3114 gdouble max = gst_value_get_double_range_max (subtrahend);
3115 gdouble val = g_value_get_double (minuend);
3117 if (val < min || val > max) {
3118 gst_value_init_and_copy (dest, minuend);
3125 gst_value_subtract_double_range_double (GValue * dest, const GValue * minuend,
3126 const GValue * subtrahend)
3128 /* since we don't have open ranges, we cannot create a hole in
3129 * a double range. We return the original range */
3130 gst_value_init_and_copy (dest, minuend);
3135 gst_value_subtract_double_range_double_range (GValue * dest,
3136 const GValue * minuend, const GValue * subtrahend)
3138 /* since we don't have open ranges, we have to approximate */
3139 /* done like with ints */
3140 gdouble min1 = gst_value_get_double_range_min (minuend);
3141 gdouble max2 = gst_value_get_double_range_max (minuend);
3142 gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
3143 gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
3146 GValue *pv1, *pv2; /* yeah, hungarian! */
3148 if (min1 < max1 && min2 < max2) {
3151 } else if (min1 < max1) {
3154 } else if (min2 < max2) {
3162 g_value_init (pv1, GST_TYPE_DOUBLE_RANGE);
3163 gst_value_set_double_range (pv1, min1, max1);
3166 g_value_init (pv2, GST_TYPE_DOUBLE_RANGE);
3167 gst_value_set_double_range (pv2, min2, max2);
3170 if (min1 < max1 && min2 < max2) {
3171 gst_value_list_concat (dest, pv1, pv2);
3172 g_value_unset (pv1);
3173 g_value_unset (pv2);
3179 gst_value_subtract_from_list (GValue * dest, const GValue * minuend,
3180 const GValue * subtrahend)
3183 GValue subtraction = { 0, };
3184 gboolean ret = FALSE;
3187 ltype = gst_value_list_get_type ();
3189 size = VALUE_LIST_SIZE (minuend);
3190 for (i = 0; i < size; i++) {
3191 const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
3193 if (gst_value_subtract (&subtraction, cur, subtrahend)) {
3195 gst_value_init_and_copy (dest, &subtraction);
3197 } else if (G_VALUE_HOLDS (dest, ltype)
3198 && !G_VALUE_HOLDS (&subtraction, ltype)) {
3199 gst_value_list_append_value (dest, &subtraction);
3201 GValue temp = { 0, };
3203 gst_value_init_and_copy (&temp, dest);
3204 g_value_unset (dest);
3205 gst_value_list_concat (dest, &temp, &subtraction);
3206 g_value_unset (&temp);
3208 g_value_unset (&subtraction);
3215 gst_value_subtract_list (GValue * dest, const GValue * minuend,
3216 const GValue * subtrahend)
3219 GValue data[2] = { {0,}, {0,} };
3220 GValue *subtraction = &data[0], *result = &data[1];
3222 gst_value_init_and_copy (result, minuend);
3223 size = VALUE_LIST_SIZE (subtrahend);
3224 for (i = 0; i < size; i++) {
3225 const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
3227 if (gst_value_subtract (subtraction, result, cur)) {
3228 GValue *temp = result;
3230 result = subtraction;
3232 g_value_unset (subtraction);
3234 g_value_unset (result);
3238 gst_value_init_and_copy (dest, result);
3239 g_value_unset (result);
3244 gst_value_subtract_fraction_fraction_range (GValue * dest,
3245 const GValue * minuend, const GValue * subtrahend)
3247 const GValue *min = gst_value_get_fraction_range_min (subtrahend);
3248 const GValue *max = gst_value_get_fraction_range_max (subtrahend);
3249 GstValueCompareFunc compare;
3251 if ((compare = gst_value_get_compare_func (minuend))) {
3252 /* subtracting a range from an fraction only works if the fraction
3253 * is not in the range */
3254 if (gst_value_compare_with_func (minuend, min, compare) ==
3255 GST_VALUE_LESS_THAN ||
3256 gst_value_compare_with_func (minuend, max, compare) ==
3257 GST_VALUE_GREATER_THAN) {
3258 /* and the result is the value */
3259 gst_value_init_and_copy (dest, minuend);
3267 gst_value_subtract_fraction_range_fraction (GValue * dest,
3268 const GValue * minuend, const GValue * subtrahend)
3270 /* since we don't have open ranges, we cannot create a hole in
3271 * a range. We return the original range */
3272 gst_value_init_and_copy (dest, minuend);
3277 gst_value_subtract_fraction_range_fraction_range (GValue * dest,
3278 const GValue * minuend, const GValue * subtrahend)
3280 /* since we don't have open ranges, we have to approximate */
3281 /* done like with ints and doubles. Creates a list of 2 fraction ranges */
3282 const GValue *min1 = gst_value_get_fraction_range_min (minuend);
3283 const GValue *max2 = gst_value_get_fraction_range_max (minuend);
3284 const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
3285 const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
3289 GValue *pv1, *pv2; /* yeah, hungarian! */
3290 GstValueCompareFunc compare;
3292 g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
3293 g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
3295 compare = gst_value_get_compare_func (min1);
3296 g_return_val_if_fail (compare, FALSE);
3298 cmp1 = gst_value_compare_with_func (max2, max1, compare);
3299 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3300 if (cmp1 == GST_VALUE_LESS_THAN)
3302 cmp1 = gst_value_compare_with_func (min1, min2, compare);
3303 g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
3304 if (cmp1 == GST_VALUE_GREATER_THAN)
3307 cmp1 = gst_value_compare_with_func (min1, max1, compare);
3308 cmp2 = gst_value_compare_with_func (min2, max2, compare);
3310 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3313 } else if (cmp1 == GST_VALUE_LESS_THAN) {
3316 } else if (cmp2 == GST_VALUE_LESS_THAN) {
3323 if (cmp1 == GST_VALUE_LESS_THAN) {
3324 g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
3325 gst_value_set_fraction_range (pv1, min1, max1);
3327 if (cmp2 == GST_VALUE_LESS_THAN) {
3328 g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
3329 gst_value_set_fraction_range (pv2, min2, max2);
3332 if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
3333 gst_value_list_concat (dest, pv1, pv2);
3334 g_value_unset (pv1);
3335 g_value_unset (pv2);
3346 * gst_value_get_compare_func:
3347 * @value1: a value to get the compare function for
3349 * Determines the compare function to be used with values of the same type as
3350 * @value1. The function can be given to gst_value_compare_with_func().
3352 * Returns: A #GstValueCompareFunc value
3354 static GstValueCompareFunc
3355 gst_value_get_compare_func (const GValue * value1)
3357 GstValueTable *table, *best = NULL;
3361 type1 = G_VALUE_TYPE (value1);
3363 /* this is a fast check */
3364 best = gst_value_hash_lookup_type (type1);
3367 if (G_UNLIKELY (!best || !best->compare)) {
3368 guint len = gst_value_table->len;
3371 for (i = 0; i < len; i++) {
3372 table = &g_array_index (gst_value_table, GstValueTable, i);
3373 if (table->compare && g_type_is_a (type1, table->type)) {
3374 if (!best || g_type_is_a (table->type, best->type))
3379 if (G_LIKELY (best))
3380 return best->compare;
3386 * gst_value_can_compare:
3387 * @value1: a value to compare
3388 * @value2: another value to compare
3390 * Determines if @value1 and @value2 can be compared.
3392 * Returns: TRUE if the values can be compared
3395 gst_value_can_compare (const GValue * value1, const GValue * value2)
3397 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3398 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3400 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3403 return gst_value_get_compare_func (value1) != NULL;
3407 * gst_value_compare:
3408 * @value1: a value to compare
3409 * @value2: another value to compare
3411 * Compares @value1 and @value2. If @value1 and @value2 cannot be
3412 * compared, the function returns GST_VALUE_UNORDERED. Otherwise,
3413 * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
3414 * If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
3415 * If the values are equal, GST_VALUE_EQUAL is returned.
3417 * Returns: comparison result
3420 gst_value_compare (const GValue * value1, const GValue * value2)
3422 GstValueCompareFunc compare;
3425 g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
3426 g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
3428 /* Special case: lists and scalar values
3429 * "{ 1 }" and "1" are equal */
3430 ltype = gst_value_list_get_type ();
3431 if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)
3432 && gst_value_list_get_size (value1) == 1) {
3435 elt = gst_value_list_get_value (value1, 0);
3436 return gst_value_compare (elt, value2);
3437 } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)
3438 && gst_value_list_get_size (value2) == 1) {
3441 elt = gst_value_list_get_value (value2, 0);
3442 return gst_value_compare (elt, value1);
3445 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3446 return GST_VALUE_UNORDERED;
3448 compare = gst_value_get_compare_func (value1);
3450 return compare (value1, value2);
3453 g_critical ("unable to compare values of type %s\n",
3454 g_type_name (G_VALUE_TYPE (value1)));
3455 return GST_VALUE_UNORDERED;
3459 * gst_value_compare_with_func:
3460 * @value1: a value to compare
3461 * @value2: another value to compare
3462 * @compare: compare function
3464 * Compares @value1 and @value2 using the @compare function. Works like
3465 * gst_value_compare() but allows to save time determining the compare function
3468 * Returns: comparison result
3471 gst_value_compare_with_func (const GValue * value1, const GValue * value2,
3472 GstValueCompareFunc compare)
3476 if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
3477 return GST_VALUE_UNORDERED;
3479 return compare (value1, value2);
3485 * gst_value_can_union:
3486 * @value1: a value to union
3487 * @value2: another value to union
3489 * Determines if @value1 and @value2 can be non-trivially unioned.
3490 * Any two values can be trivially unioned by adding both of them
3491 * to a GstValueList. However, certain types have the possibility
3492 * to be unioned in a simpler way. For example, an integer range
3493 * and an integer can be unioned if the integer is a subset of the
3494 * integer range. If there is the possibility that two values can
3495 * be unioned, this function returns TRUE.
3497 * Returns: TRUE if there is a function allowing the two values to
3501 gst_value_can_union (const GValue * value1, const GValue * value2)
3503 GstValueUnionInfo *union_info;
3506 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3507 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3509 len = gst_value_union_funcs->len;
3511 for (i = 0; i < len; i++) {
3512 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
3513 if (union_info->type1 == G_VALUE_TYPE (value1) &&
3514 union_info->type2 == G_VALUE_TYPE (value2))
3516 if (union_info->type1 == G_VALUE_TYPE (value2) &&
3517 union_info->type2 == G_VALUE_TYPE (value1))
3526 * @dest: (out caller-allocates): the destination value
3527 * @value1: a value to union
3528 * @value2: another value to union
3530 * Creates a GValue corresponding to the union of @value1 and @value2.
3532 * Returns: always returns %TRUE
3534 /* FIXME: change return type to 'void'? */
3536 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
3538 GstValueUnionInfo *union_info;
3541 g_return_val_if_fail (dest != NULL, FALSE);
3542 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3543 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3545 len = gst_value_union_funcs->len;
3547 for (i = 0; i < len; i++) {
3548 union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
3549 if (union_info->type1 == G_VALUE_TYPE (value1) &&
3550 union_info->type2 == G_VALUE_TYPE (value2)) {
3551 if (union_info->func (dest, value1, value2)) {
3555 if (union_info->type1 == G_VALUE_TYPE (value2) &&
3556 union_info->type2 == G_VALUE_TYPE (value1)) {
3557 if (union_info->func (dest, value2, value1)) {
3563 gst_value_list_concat (dest, value1, value2);
3568 * gst_value_register_union_func:
3569 * @type1: a type to union
3570 * @type2: another type to union
3571 * @func: a function that implments creating a union between the two types
3573 * Registers a union function that can create a union between #GValue items
3574 * of the type @type1 and @type2.
3576 * Union functions should be registered at startup before any pipelines are
3577 * started, as gst_value_register_union_func() is not thread-safe and cannot
3578 * be used at the same time as gst_value_union() or gst_value_can_union().
3581 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
3583 GstValueUnionInfo union_info;
3585 union_info.type1 = type1;
3586 union_info.type2 = type2;
3587 union_info.func = func;
3589 g_array_append_val (gst_value_union_funcs, union_info);
3595 * gst_value_can_intersect:
3596 * @value1: a value to intersect
3597 * @value2: another value to intersect
3599 * Determines if intersecting two values will produce a valid result.
3600 * Two values will produce a valid intersection if they have the same
3601 * type, or if there is a method (registered by
3602 * gst_value_register_intersect_func()) to calculate the intersection.
3604 * Returns: TRUE if the values can intersect
3607 gst_value_can_intersect (const GValue * value1, const GValue * value2)
3609 GstValueIntersectInfo *intersect_info;
3611 GType ltype, type1, type2;
3613 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3614 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3616 ltype = gst_value_list_get_type ();
3619 if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
3622 type1 = G_VALUE_TYPE (value1);
3623 type2 = G_VALUE_TYPE (value2);
3625 /* practically all GstValue types have a compare function (_can_compare=TRUE)
3626 * GstStructure and GstCaps have npot, but are intersectable */
3630 /* check registered intersect functions */
3631 len = gst_value_intersect_funcs->len;
3632 for (i = 0; i < len; i++) {
3633 intersect_info = &g_array_index (gst_value_intersect_funcs,
3634 GstValueIntersectInfo, i);
3635 if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
3636 (intersect_info->type1 == type2 && intersect_info->type2 == type1))
3640 return gst_value_can_compare (value1, value2);
3644 * gst_value_intersect:
3645 * @dest: (out caller-allocates): a uninitialized #GValue that will hold the calculated
3646 * intersection value
3647 * @value1: a value to intersect
3648 * @value2: another value to intersect
3650 * Calculates the intersection of two values. If the values have
3651 * a non-empty intersection, the value representing the intersection
3652 * is placed in @dest. If the intersection is non-empty, @dest is
3655 * Returns: TRUE if the intersection is non-empty
3658 gst_value_intersect (GValue * dest, const GValue * value1,
3659 const GValue * value2)
3661 GstValueIntersectInfo *intersect_info;
3663 GType ltype, type1, type2;
3665 g_return_val_if_fail (dest != NULL, FALSE);
3666 g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
3667 g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
3669 ltype = gst_value_list_get_type ();
3671 /* special cases first */
3672 if (G_VALUE_HOLDS (value1, ltype))
3673 return gst_value_intersect_list (dest, value1, value2);
3674 if (G_VALUE_HOLDS (value2, ltype))
3675 return gst_value_intersect_list (dest, value2, value1);
3677 if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
3678 gst_value_init_and_copy (dest, value1);
3682 type1 = G_VALUE_TYPE (value1);
3683 type2 = G_VALUE_TYPE (value2);
3685 len = gst_value_intersect_funcs->len;
3686 for (i = 0; i < len; i++) {
3687 intersect_info = &g_array_index (gst_value_intersect_funcs,
3688 GstValueIntersectInfo, i);
3689 if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
3690 return intersect_info->func (dest, value1, value2);
3692 if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
3693 return intersect_info->func (dest, value2, value1);
3700 * gst_value_register_intersect_func:
3701 * @type1: the first type to intersect
3702 * @type2: the second type to intersect
3703 * @func: the intersection function
3705 * Registers a function that is called to calculate the intersection
3706 * of the values having the types @type1 and @type2.
3708 * Intersect functions should be registered at startup before any pipelines are
3709 * started, as gst_value_register_intersect_func() is not thread-safe and
3710 * cannot be used at the same time as gst_value_intersect() or
3711 * gst_value_can_intersect().
3714 gst_value_register_intersect_func (GType type1, GType type2,
3715 GstValueIntersectFunc func)
3717 GstValueIntersectInfo intersect_info;
3719 intersect_info.type1 = type1;
3720 intersect_info.type2 = type2;
3721 intersect_info.func = func;
3723 g_array_append_val (gst_value_intersect_funcs, intersect_info);
3730 * gst_value_subtract:
3731 * @dest: (out caller-allocates): the destination value for the result if the
3732 * subtraction is not empty
3733 * @minuend: the value to subtract from
3734 * @subtrahend: the value to subtract
3736 * Subtracts @subtrahend from @minuend and stores the result in @dest.
3737 * Note that this means subtraction as in sets, not as in mathematics.
3739 * Returns: %TRUE if the subtraction is not empty
3742 gst_value_subtract (GValue * dest, const GValue * minuend,
3743 const GValue * subtrahend)
3745 GstValueSubtractInfo *info;
3747 GType ltype, mtype, stype;
3749 g_return_val_if_fail (dest != NULL, FALSE);
3750 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
3751 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
3753 ltype = gst_value_list_get_type ();
3755 /* special cases first */
3756 if (G_VALUE_HOLDS (minuend, ltype))
3757 return gst_value_subtract_from_list (dest, minuend, subtrahend);
3758 if (G_VALUE_HOLDS (subtrahend, ltype))
3759 return gst_value_subtract_list (dest, minuend, subtrahend);
3761 mtype = G_VALUE_TYPE (minuend);
3762 stype = G_VALUE_TYPE (subtrahend);
3764 len = gst_value_subtract_funcs->len;
3765 for (i = 0; i < len; i++) {
3766 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3767 if (info->minuend == mtype && info->subtrahend == stype) {
3768 return info->func (dest, minuend, subtrahend);
3772 if (gst_value_compare (minuend, subtrahend) != GST_VALUE_EQUAL) {
3773 gst_value_init_and_copy (dest, minuend);
3782 gst_value_subtract (GValue * dest, const GValue * minuend,
3783 const GValue * subtrahend)
3785 gboolean ret = gst_value_subtract2 (dest, minuend, subtrahend);
3787 g_printerr ("\"%s\" - \"%s\" = \"%s\"\n", gst_value_serialize (minuend),
3788 gst_value_serialize (subtrahend),
3789 ret ? gst_value_serialize (dest) : "---");
3795 * gst_value_can_subtract:
3796 * @minuend: the value to subtract from
3797 * @subtrahend: the value to subtract
3799 * Checks if it's possible to subtract @subtrahend from @minuend.
3801 * Returns: TRUE if a subtraction is possible
3804 gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
3806 GstValueSubtractInfo *info;
3808 GType ltype, mtype, stype;
3810 g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
3811 g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
3813 ltype = gst_value_list_get_type ();
3816 if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
3819 mtype = G_VALUE_TYPE (minuend);
3820 stype = G_VALUE_TYPE (subtrahend);
3822 len = gst_value_subtract_funcs->len;
3823 for (i = 0; i < len; i++) {
3824 info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
3825 if (info->minuend == mtype && info->subtrahend == stype)
3829 return gst_value_can_compare (minuend, subtrahend);
3833 * gst_value_register_subtract_func:
3834 * @minuend_type: type of the minuend
3835 * @subtrahend_type: type of the subtrahend
3836 * @func: function to use
3838 * Registers @func as a function capable of subtracting the values of
3839 * @subtrahend_type from values of @minuend_type.
3841 * Subtract functions should be registered at startup before any pipelines are
3842 * started, as gst_value_register_subtract_func() is not thread-safe and
3843 * cannot be used at the same time as gst_value_subtract().
3846 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
3847 GstValueSubtractFunc func)
3849 GstValueSubtractInfo info;
3851 /* one type must be unfixed, other subtractions can be done as comparisons */
3852 g_return_if_fail (!gst_type_is_fixed (minuend_type)
3853 || !gst_type_is_fixed (subtrahend_type));
3855 info.minuend = minuend_type;
3856 info.subtrahend = subtrahend_type;
3859 g_array_append_val (gst_value_subtract_funcs, info);
3863 * gst_value_register:
3864 * @table: structure containing functions to register
3866 * Registers functions to perform calculations on #GValue items of a given
3867 * type. Each type can only be added once.
3870 gst_value_register (const GstValueTable * table)
3872 GstValueTable *found;
3874 g_return_if_fail (table != NULL);
3876 g_array_append_val (gst_value_table, *table);
3878 found = gst_value_hash_lookup_type (table->type);
3880 g_warning ("adding type %s multiple times", g_type_name (table->type));
3882 /* FIXME: we're not really doing the const justice, we assume the table is
3884 gst_value_hash_add_type (table->type, table);
3888 * gst_value_init_and_copy:
3889 * @dest: (out caller-allocates): the target value
3890 * @src: the source value
3892 * Initialises the target value to be of the same type as source and then copies
3893 * the contents from source to target.
3896 gst_value_init_and_copy (GValue * dest, const GValue * src)
3898 g_return_if_fail (G_IS_VALUE (src));
3899 g_return_if_fail (dest != NULL);
3901 g_value_init (dest, G_VALUE_TYPE (src));
3902 g_value_copy (src, dest);
3906 * gst_value_serialize:
3907 * @value: a #GValue to serialize
3909 * tries to transform the given @value into a string representation that allows
3910 * getting back this string later on using gst_value_deserialize().
3912 * Free-function: g_free
3914 * Returns: (transfer full): the serialization for @value or NULL if none exists
3917 gst_value_serialize (const GValue * value)
3920 GValue s_val = { 0 };
3921 GstValueTable *table, *best;
3925 g_return_val_if_fail (G_IS_VALUE (value), NULL);
3927 type = G_VALUE_TYPE (value);
3929 best = gst_value_hash_lookup_type (type);
3931 if (G_UNLIKELY (!best || !best->serialize)) {
3932 len = gst_value_table->len;
3934 for (i = 0; i < len; i++) {
3935 table = &g_array_index (gst_value_table, GstValueTable, i);
3936 if (table->serialize && g_type_is_a (type, table->type)) {
3937 if (!best || g_type_is_a (table->type, best->type))
3942 if (G_LIKELY (best))
3943 return best->serialize (value);
3945 g_value_init (&s_val, G_TYPE_STRING);
3946 if (g_value_transform (value, &s_val)) {
3947 s = gst_string_wrap (g_value_get_string (&s_val));
3951 g_value_unset (&s_val);
3957 * gst_value_deserialize:
3958 * @dest: (out caller-allocates): #GValue to fill with contents of
3960 * @src: string to deserialize
3962 * Tries to deserialize a string into the type specified by the given GValue.
3963 * If the operation succeeds, TRUE is returned, FALSE otherwise.
3965 * Returns: TRUE on success
3968 gst_value_deserialize (GValue * dest, const gchar * src)
3970 GstValueTable *table, *best;
3974 g_return_val_if_fail (src != NULL, FALSE);
3975 g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
3977 type = G_VALUE_TYPE (dest);
3979 best = gst_value_hash_lookup_type (type);
3980 if (G_UNLIKELY (!best || !best->deserialize)) {
3981 len = gst_value_table->len;
3983 for (i = 0; i < len; i++) {
3984 table = &g_array_index (gst_value_table, GstValueTable, i);
3985 if (table->deserialize && g_type_is_a (type, table->type)) {
3986 if (!best || g_type_is_a (table->type, best->type))
3991 if (G_LIKELY (best))
3992 return best->deserialize (dest, src);
3998 * gst_value_is_fixed:
3999 * @value: the #GValue to check
4001 * Tests if the given GValue, if available in a GstStructure (or any other
4002 * container) contains a "fixed" (which means: one value) or an "unfixed"
4003 * (which means: multiple possible values, such as data lists or data
4006 * Returns: true if the value is "fixed".
4010 gst_value_is_fixed (const GValue * value)
4014 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
4016 type = G_VALUE_TYPE (value);
4018 /* the most common types are just basic plain glib types */
4019 if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
4023 if (type == GST_TYPE_ARRAY) {
4027 /* check recursively */
4028 size = gst_value_array_get_size (value);
4029 for (n = 0; n < size; n++) {
4030 kid = gst_value_array_get_value (value, n);
4031 if (!gst_value_is_fixed (kid))
4036 return gst_type_is_fixed (type);
4041 * @dest: the #GValue destination
4042 * @src: the #GValue to fixate
4044 * Fixate @src into a new value @dest.
4045 * For ranges, the first element is taken. For lists and arrays, the
4046 * first item is fixated and returned.
4047 * If @src is already fixed, this function returns FALSE.
4049 * Returns: true if @dest contains a fixated version of @src.
4052 gst_value_fixate (GValue * dest, const GValue * src)
4054 g_return_val_if_fail (G_IS_VALUE (src), FALSE);
4055 g_return_val_if_fail (dest != NULL, FALSE);
4057 if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
4058 g_value_init (dest, G_TYPE_INT);
4059 g_value_set_int (dest, gst_value_get_int_range_min (src));
4060 } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
4061 g_value_init (dest, G_TYPE_DOUBLE);
4062 g_value_set_double (dest, gst_value_get_double_range_min (src));
4063 } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
4064 gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
4065 } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
4066 GValue temp = { 0 };
4068 /* list could be empty */
4069 if (gst_value_list_get_size (src) <= 0)
4072 gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
4074 if (!gst_value_fixate (dest, &temp))
4075 gst_value_init_and_copy (dest, &temp);
4076 g_value_unset (&temp);
4077 } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
4078 gboolean res = FALSE;
4081 len = gst_value_array_get_size (src);
4082 g_value_init (dest, GST_TYPE_ARRAY);
4083 for (n = 0; n < len; n++) {
4085 const GValue *orig_kid = gst_value_array_get_value (src, n);
4087 if (!gst_value_fixate (&kid, orig_kid))
4088 gst_value_init_and_copy (&kid, orig_kid);
4091 gst_value_array_append_value (dest, &kid);
4092 g_value_unset (&kid);
4096 g_value_unset (dest);
4110 /* helper functions */
4112 gst_value_init_fraction (GValue * value)
4114 value->data[0].v_int = 0;
4115 value->data[1].v_int = 1;
4119 gst_value_copy_fraction (const GValue * src_value, GValue * dest_value)
4121 dest_value->data[0].v_int = src_value->data[0].v_int;
4122 dest_value->data[1].v_int = src_value->data[1].v_int;
4126 gst_value_collect_fraction (GValue * value, guint n_collect_values,
4127 GTypeCValue * collect_values, guint collect_flags)
4129 if (n_collect_values != 2)
4130 return g_strdup_printf ("not enough value locations for `%s' passed",
4131 G_VALUE_TYPE_NAME (value));
4132 if (collect_values[1].v_int == 0)
4133 return g_strdup_printf ("passed '0' as denominator for `%s'",
4134 G_VALUE_TYPE_NAME (value));
4135 if (collect_values[0].v_int < -G_MAXINT)
4138 ("passed value smaller than -G_MAXINT as numerator for `%s'",
4139 G_VALUE_TYPE_NAME (value));
4140 if (collect_values[1].v_int < -G_MAXINT)
4143 ("passed value smaller than -G_MAXINT as denominator for `%s'",
4144 G_VALUE_TYPE_NAME (value));
4146 gst_value_set_fraction (value,
4147 collect_values[0].v_int, collect_values[1].v_int);
4153 gst_value_lcopy_fraction (const GValue * value, guint n_collect_values,
4154 GTypeCValue * collect_values, guint collect_flags)
4156 gint *numerator = collect_values[0].v_pointer;
4157 gint *denominator = collect_values[1].v_pointer;
4160 return g_strdup_printf ("numerator for `%s' passed as NULL",
4161 G_VALUE_TYPE_NAME (value));
4163 return g_strdup_printf ("denominator for `%s' passed as NULL",
4164 G_VALUE_TYPE_NAME (value));
4166 *numerator = value->data[0].v_int;
4167 *denominator = value->data[1].v_int;
4173 * gst_value_set_fraction:
4174 * @value: a GValue initialized to #GST_TYPE_FRACTION
4175 * @numerator: the numerator of the fraction
4176 * @denominator: the denominator of the fraction
4178 * Sets @value to the fraction specified by @numerator over @denominator.
4179 * The fraction gets reduced to the smallest numerator and denominator,
4180 * and if necessary the sign is moved to the numerator.
4183 gst_value_set_fraction (GValue * value, gint numerator, gint denominator)
4187 g_return_if_fail (GST_VALUE_HOLDS_FRACTION (value));
4188 g_return_if_fail (denominator != 0);
4189 g_return_if_fail (denominator >= -G_MAXINT);
4190 g_return_if_fail (numerator >= -G_MAXINT);
4192 /* normalize sign */
4193 if (denominator < 0) {
4194 numerator = -numerator;
4195 denominator = -denominator;
4198 /* check for reduction */
4199 gcd = gst_util_greatest_common_divisor (numerator, denominator);
4205 g_assert (denominator > 0);
4207 value->data[0].v_int = numerator;
4208 value->data[1].v_int = denominator;
4212 * gst_value_get_fraction_numerator:
4213 * @value: a GValue initialized to #GST_TYPE_FRACTION
4215 * Gets the numerator of the fraction specified by @value.
4217 * Returns: the numerator of the fraction.
4220 gst_value_get_fraction_numerator (const GValue * value)
4222 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
4224 return value->data[0].v_int;
4228 * gst_value_get_fraction_denominator:
4229 * @value: a GValue initialized to #GST_TYPE_FRACTION
4231 * Gets the denominator of the fraction specified by @value.
4233 * Returns: the denominator of the fraction.
4236 gst_value_get_fraction_denominator (const GValue * value)
4238 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
4240 return value->data[1].v_int;
4244 * gst_value_fraction_multiply:
4245 * @product: a GValue initialized to #GST_TYPE_FRACTION
4246 * @factor1: a GValue initialized to #GST_TYPE_FRACTION
4247 * @factor2: a GValue initialized to #GST_TYPE_FRACTION
4249 * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
4250 * @product to the product of the two fractions.
4252 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4255 gst_value_fraction_multiply (GValue * product, const GValue * factor1,
4256 const GValue * factor2)
4258 gint n1, n2, d1, d2;
4261 g_return_val_if_fail (product != NULL, FALSE);
4262 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
4263 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
4265 n1 = factor1->data[0].v_int;
4266 n2 = factor2->data[0].v_int;
4267 d1 = factor1->data[1].v_int;
4268 d2 = factor2->data[1].v_int;
4270 if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
4273 gst_value_set_fraction (product, res_n, res_d);
4279 * gst_value_fraction_subtract:
4280 * @dest: a GValue initialized to #GST_TYPE_FRACTION
4281 * @minuend: a GValue initialized to #GST_TYPE_FRACTION
4282 * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
4284 * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
4286 * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
4289 gst_value_fraction_subtract (GValue * dest,
4290 const GValue * minuend, const GValue * subtrahend)
4292 gint n1, n2, d1, d2;
4295 g_return_val_if_fail (dest != NULL, FALSE);
4296 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
4297 g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
4299 n1 = minuend->data[0].v_int;
4300 n2 = subtrahend->data[0].v_int;
4301 d1 = minuend->data[1].v_int;
4302 d2 = subtrahend->data[1].v_int;
4304 if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
4306 gst_value_set_fraction (dest, res_n, res_d);
4312 gst_value_serialize_fraction (const GValue * value)
4314 gint32 numerator = value->data[0].v_int;
4315 gint32 denominator = value->data[1].v_int;
4316 gboolean positive = TRUE;
4318 /* get the sign and make components absolute */
4319 if (numerator < 0) {
4320 numerator = -numerator;
4321 positive = !positive;
4323 if (denominator < 0) {
4324 denominator = -denominator;
4325 positive = !positive;
4328 return g_strdup_printf ("%s%d/%d",
4329 positive ? "" : "-", numerator, denominator);
4333 gst_value_deserialize_fraction (GValue * dest, const gchar * s)
4338 if (G_UNLIKELY (s == NULL))
4341 if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
4344 if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
4345 if (s[num_chars] != 0)
4350 gst_value_set_fraction (dest, num, den);
4352 } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
4353 gst_value_set_fraction (dest, 1, G_MAXINT);
4355 } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
4356 if (s[num_chars] != 0)
4358 gst_value_set_fraction (dest, num, 1);
4360 } else if (g_ascii_strcasecmp (s, "min") == 0) {
4361 gst_value_set_fraction (dest, -G_MAXINT, 1);
4363 } else if (g_ascii_strcasecmp (s, "max") == 0) {
4364 gst_value_set_fraction (dest, G_MAXINT, 1);
4372 gst_value_transform_fraction_string (const GValue * src_value,
4373 GValue * dest_value)
4375 dest_value->data[0].v_pointer = gst_value_serialize_fraction (src_value);
4379 gst_value_transform_string_fraction (const GValue * src_value,
4380 GValue * dest_value)
4382 if (!gst_value_deserialize_fraction (dest_value,
4383 src_value->data[0].v_pointer))
4384 /* If the deserialize fails, ensure we leave the fraction in a
4385 * valid, if incorrect, state */
4386 gst_value_set_fraction (dest_value, 0, 1);
4390 gst_value_transform_double_fraction (const GValue * src_value,
4391 GValue * dest_value)
4393 gdouble src = g_value_get_double (src_value);
4396 gst_util_double_to_fraction (src, &n, &d);
4397 gst_value_set_fraction (dest_value, n, d);
4401 gst_value_transform_float_fraction (const GValue * src_value,
4402 GValue * dest_value)
4404 gfloat src = g_value_get_float (src_value);
4407 gst_util_double_to_fraction (src, &n, &d);
4408 gst_value_set_fraction (dest_value, n, d);
4412 gst_value_transform_fraction_double (const GValue * src_value,
4413 GValue * dest_value)
4415 dest_value->data[0].v_double = ((double) src_value->data[0].v_int) /
4416 ((double) src_value->data[1].v_int);
4420 gst_value_transform_fraction_float (const GValue * src_value,
4421 GValue * dest_value)
4423 dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
4424 ((float) src_value->data[1].v_int);
4428 gst_value_compare_fraction (const GValue * value1, const GValue * value2)
4434 n1 = value1->data[0].v_int;
4435 n2 = value2->data[0].v_int;
4436 d1 = value1->data[1].v_int;
4437 d2 = value2->data[1].v_int;
4439 /* fractions are reduced when set, so we can quickly see if they're equal */
4440 if (n1 == n2 && d1 == d2)
4441 return GST_VALUE_EQUAL;
4443 if (d1 == 0 && d2 == 0)
4444 return GST_VALUE_UNORDERED;
4446 return GST_VALUE_GREATER_THAN;
4448 return GST_VALUE_LESS_THAN;
4450 ret = gst_util_fraction_compare (n1, d1, n2, d2);
4452 return GST_VALUE_LESS_THAN;
4454 return GST_VALUE_GREATER_THAN;
4456 /* Equality can't happen here because we check for that
4458 g_return_val_if_reached (GST_VALUE_UNORDERED);
4466 * gst_value_set_date:
4467 * @value: a GValue initialized to GST_TYPE_DATE
4468 * @date: the date to set the value to
4470 * Sets the contents of @value to coorespond to @date. The actual
4471 * #GDate structure is copied before it is used.
4474 gst_value_set_date (GValue * value, const GDate * date)
4476 g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
4477 g_return_if_fail (g_date_valid (date));
4479 g_value_set_boxed (value, date);
4483 * gst_value_get_date:
4484 * @value: a GValue initialized to GST_TYPE_DATE
4486 * Gets the contents of @value.
4488 * Returns: (transfer none): the contents of @value
4491 gst_value_get_date (const GValue * value)
4493 g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE, NULL);
4495 return (const GDate *) g_value_get_boxed (value);
4499 gst_date_copy (gpointer boxed)
4501 const GDate *date = (const GDate *) boxed;
4503 if (!g_date_valid (date)) {
4504 GST_WARNING ("invalid GDate");
4508 return g_date_new_julian (g_date_get_julian (date));
4512 gst_value_compare_date (const GValue * value1, const GValue * value2)
4514 const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
4515 const GDate *date2 = (const GDate *) g_value_get_boxed (value2);
4519 return GST_VALUE_EQUAL;
4521 if ((date1 == NULL || !g_date_valid (date1))
4522 && (date2 != NULL && g_date_valid (date2))) {
4523 return GST_VALUE_LESS_THAN;
4526 if ((date2 == NULL || !g_date_valid (date2))
4527 && (date1 != NULL && g_date_valid (date1))) {
4528 return GST_VALUE_GREATER_THAN;
4531 if (date1 == NULL || date2 == NULL || !g_date_valid (date1)
4532 || !g_date_valid (date2)) {
4533 return GST_VALUE_UNORDERED;
4536 j1 = g_date_get_julian (date1);
4537 j2 = g_date_get_julian (date2);
4540 return GST_VALUE_EQUAL;
4542 return GST_VALUE_LESS_THAN;
4544 return GST_VALUE_GREATER_THAN;
4548 gst_value_serialize_date (const GValue * val)
4550 const GDate *date = (const GDate *) g_value_get_boxed (val);
4552 if (date == NULL || !g_date_valid (date))
4553 return g_strdup ("9999-99-99");
4555 return g_strdup_printf ("%04u-%02u-%02u", g_date_get_year (date),
4556 g_date_get_month (date), g_date_get_day (date));
4560 gst_value_deserialize_date (GValue * dest, const gchar * s)
4562 guint year, month, day;
4564 if (!s || sscanf (s, "%04u-%02u-%02u", &year, &month, &day) != 3)
4567 if (!g_date_valid_dmy (day, month, year))
4570 g_value_take_boxed (dest, g_date_new_dmy (day, month, year));
4579 gst_value_compare_date_time (const GValue * value1, const GValue * value2)
4581 const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
4582 const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
4586 return GST_VALUE_EQUAL;
4588 if ((date1 == NULL) && (date2 != NULL)) {
4589 return GST_VALUE_LESS_THAN;
4591 if ((date2 == NULL) && (date1 != NULL)) {
4592 return GST_VALUE_LESS_THAN;
4595 ret = priv_gst_date_time_compare (date1, date2);
4598 return GST_VALUE_EQUAL;
4600 return GST_VALUE_LESS_THAN;
4602 return GST_VALUE_GREATER_THAN;
4606 gst_value_serialize_date_time (const GValue * val)
4608 GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
4610 gint tzhour, tzminute;
4613 return g_strdup ("null");
4615 offset = gst_date_time_get_time_zone_offset (date);
4617 tzhour = (gint) ABS (offset);
4618 tzminute = (gint) ((ABS (offset) - tzhour) * 60);
4620 return g_strdup_printf ("\"%04d-%02d-%02dT%02d:%02d:%02d.%06d"
4621 "%c%02d%02d\"", gst_date_time_get_year (date),
4622 gst_date_time_get_month (date), gst_date_time_get_day (date),
4623 gst_date_time_get_hour (date), gst_date_time_get_minute (date),
4624 gst_date_time_get_second (date), gst_date_time_get_microsecond (date),
4625 offset >= 0 ? '+' : '-', tzhour, tzminute);
4629 gst_value_deserialize_date_time (GValue * dest, const gchar * s)
4631 gint year, month, day, hour, minute, second, usecond;
4634 gfloat tzoffset = 0;
4637 if (!s || strcmp (s, "null") == 0) {
4641 ret = sscanf (s, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%04d",
4642 &year, &month, &day, &hour, &minute, &second, &usecond, &signal, &offset);
4644 tzoffset = (offset / 100) + ((offset % 100) / 60.0);
4646 tzoffset = -tzoffset;
4650 g_value_take_boxed (dest, gst_date_time_new (tzoffset, year, month, day, hour,
4651 minute, second + (usecond / 1000000.0)));
4656 gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
4658 dest_value->data[0].v_pointer = gst_value_serialize_date (src_value);
4662 gst_value_transform_string_date (const GValue * src_value, GValue * dest_value)
4664 gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
4668 gst_value_transform_object_string (const GValue * src_value,
4669 GValue * dest_value)
4674 obj = g_value_get_object (src_value);
4677 g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
4678 GST_OBJECT_NAME (obj));
4680 str = g_strdup ("NULL");
4683 dest_value->data[0].v_pointer = str;
4686 static GTypeInfo _info = {
4699 static GTypeFundamentalInfo _finfo = {
4703 #define FUNC_VALUE_GET_TYPE(type, name) \
4704 GType gst_ ## type ## _get_type (void) \
4706 static volatile GType gst_ ## type ## _type = 0; \
4708 if (g_once_init_enter (&gst_ ## type ## _type)) { \
4710 _info.value_table = & _gst_ ## type ## _value_table; \
4711 _type = g_type_register_fundamental ( \
4712 g_type_fundamental_next (), \
4713 name, &_info, &_finfo, 0); \
4714 g_once_init_leave(&gst_ ## type ## _type, _type); \
4717 return gst_ ## type ## _type; \
4720 static const GTypeValueTable _gst_fourcc_value_table = {
4721 gst_value_init_fourcc,
4723 gst_value_copy_fourcc,
4726 gst_value_collect_fourcc,
4728 gst_value_lcopy_fourcc
4731 FUNC_VALUE_GET_TYPE (fourcc, "GstFourcc");
4733 static const GTypeValueTable _gst_int_range_value_table = {
4734 gst_value_init_int_range,
4736 gst_value_copy_int_range,
4739 gst_value_collect_int_range,
4741 gst_value_lcopy_int_range
4744 FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
4746 static const GTypeValueTable _gst_int64_range_value_table = {
4747 gst_value_init_int64_range,
4749 gst_value_copy_int64_range,
4752 gst_value_collect_int64_range,
4754 gst_value_lcopy_int64_range
4757 FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
4759 static const GTypeValueTable _gst_double_range_value_table = {
4760 gst_value_init_double_range,
4762 gst_value_copy_double_range,
4765 gst_value_collect_double_range,
4767 gst_value_lcopy_double_range
4770 FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
4772 static const GTypeValueTable _gst_fraction_range_value_table = {
4773 gst_value_init_fraction_range,
4774 gst_value_free_fraction_range,
4775 gst_value_copy_fraction_range,
4778 gst_value_collect_fraction_range,
4780 gst_value_lcopy_fraction_range
4783 FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
4785 static const GTypeValueTable _gst_value_list_value_table = {
4786 gst_value_init_list_or_array,
4787 gst_value_free_list_or_array,
4788 gst_value_copy_list_or_array,
4789 gst_value_list_or_array_peek_pointer,
4791 gst_value_collect_list_or_array,
4793 gst_value_lcopy_list_or_array
4796 FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
4798 static const GTypeValueTable _gst_value_array_value_table = {
4799 gst_value_init_list_or_array,
4800 gst_value_free_list_or_array,
4801 gst_value_copy_list_or_array,
4802 gst_value_list_or_array_peek_pointer,
4804 gst_value_collect_list_or_array,
4806 gst_value_lcopy_list_or_array
4809 FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
4811 static const GTypeValueTable _gst_fraction_value_table = {
4812 gst_value_init_fraction,
4814 gst_value_copy_fraction,
4817 gst_value_collect_fraction,
4819 gst_value_lcopy_fraction
4822 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
4826 gst_date_get_type (void)
4828 static GType gst_date_type = 0;
4830 if (G_UNLIKELY (gst_date_type == 0)) {
4831 /* FIXME 0.11: we require GLib 2.8 already
4832 * Not using G_TYPE_DATE here on purpose, even if we could
4833 * if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
4834 * serialised strings to have different type strings depending
4835 * on what version is used, so FIXME when we require GLib-2.8 */
4836 gst_date_type = g_boxed_type_register_static ("GstDate",
4837 (GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
4840 return gst_date_type;
4844 gst_date_time_get_type (void)
4846 static GType gst_date_time_type = 0;
4848 if (G_UNLIKELY (gst_date_time_type == 0)) {
4849 gst_date_time_type = g_boxed_type_register_static ("GstDateTime",
4850 (GBoxedCopyFunc) gst_date_time_ref,
4851 (GBoxedFreeFunc) gst_date_time_unref);
4854 return gst_date_time_type;
4859 _gst_value_initialize (void)
4861 gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
4862 gst_value_hash = g_hash_table_new (NULL, NULL);
4863 gst_value_union_funcs = g_array_new (FALSE, FALSE,
4864 sizeof (GstValueUnionInfo));
4865 gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
4866 sizeof (GstValueIntersectInfo));
4867 gst_value_subtract_funcs = g_array_new (FALSE, FALSE,
4868 sizeof (GstValueSubtractInfo));
4871 static GstValueTable gst_value = {
4873 gst_value_compare_fourcc,
4874 gst_value_serialize_fourcc,
4875 gst_value_deserialize_fourcc,
4878 gst_value.type = gst_fourcc_get_type ();
4879 gst_value_register (&gst_value);
4883 static GstValueTable gst_value = {
4885 gst_value_compare_int_range,
4886 gst_value_serialize_int_range,
4887 gst_value_deserialize_int_range,
4890 gst_value.type = gst_int_range_get_type ();
4891 gst_value_register (&gst_value);
4895 static GstValueTable gst_value = {
4897 gst_value_compare_int64_range,
4898 gst_value_serialize_int64_range,
4899 gst_value_deserialize_int64_range,
4902 gst_value.type = gst_int64_range_get_type ();
4903 gst_value_register (&gst_value);
4907 static GstValueTable gst_value = {
4909 gst_value_compare_double_range,
4910 gst_value_serialize_double_range,
4911 gst_value_deserialize_double_range,
4914 gst_value.type = gst_double_range_get_type ();
4915 gst_value_register (&gst_value);
4919 static GstValueTable gst_value = {
4921 gst_value_compare_fraction_range,
4922 gst_value_serialize_fraction_range,
4923 gst_value_deserialize_fraction_range,
4926 gst_value.type = gst_fraction_range_get_type ();
4927 gst_value_register (&gst_value);
4931 static GstValueTable gst_value = {
4933 gst_value_compare_list,
4934 gst_value_serialize_list,
4935 gst_value_deserialize_list,
4938 gst_value.type = gst_value_list_get_type ();
4939 gst_value_register (&gst_value);
4943 static GstValueTable gst_value = {
4945 gst_value_compare_array,
4946 gst_value_serialize_array,
4947 gst_value_deserialize_array,
4950 gst_value.type = gst_value_array_get_type ();
4951 gst_value_register (&gst_value);
4956 static const GTypeValueTable value_table = {
4957 gst_value_init_buffer,
4959 gst_value_copy_buffer,
4962 NULL, /*gst_value_collect_buffer, */
4964 NULL /*gst_value_lcopy_buffer */
4967 static GstValueTable gst_value = {
4969 gst_value_compare_buffer,
4970 gst_value_serialize_buffer,
4971 gst_value_deserialize_buffer,
4974 gst_value.type = GST_TYPE_BUFFER;
4975 gst_value_register (&gst_value);
4978 static GstValueTable gst_value = {
4980 gst_value_compare_fraction,
4981 gst_value_serialize_fraction,
4982 gst_value_deserialize_fraction,
4985 gst_value.type = gst_fraction_get_type ();
4986 gst_value_register (&gst_value);
4989 static GstValueTable gst_value = {
4992 gst_value_serialize_caps,
4993 gst_value_deserialize_caps,
4996 gst_value.type = GST_TYPE_CAPS;
4997 gst_value_register (&gst_value);
5000 static GstValueTable gst_value = {
5003 gst_value_serialize_structure,
5004 gst_value_deserialize_structure,
5007 gst_value.type = GST_TYPE_STRUCTURE;
5008 gst_value_register (&gst_value);
5011 static GstValueTable gst_value = {
5013 gst_value_compare_date,
5014 gst_value_serialize_date,
5015 gst_value_deserialize_date,
5018 gst_value.type = gst_date_get_type ();
5019 gst_value_register (&gst_value);
5022 static GstValueTable gst_value = {
5024 gst_value_compare_date_time,
5025 gst_value_serialize_date_time,
5026 gst_value_deserialize_date_time,
5029 gst_value.type = gst_date_time_get_type ();
5030 gst_value_register (&gst_value);
5033 REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
5034 REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
5036 REGISTER_SERIALIZATION (G_TYPE_STRING, string);
5037 REGISTER_SERIALIZATION (G_TYPE_BOOLEAN, boolean);
5038 REGISTER_SERIALIZATION (G_TYPE_ENUM, enum);
5040 REGISTER_SERIALIZATION (G_TYPE_FLAGS, flags);
5042 REGISTER_SERIALIZATION (G_TYPE_INT, int);
5044 REGISTER_SERIALIZATION (G_TYPE_INT64, int64);
5045 REGISTER_SERIALIZATION (G_TYPE_LONG, long);
5047 REGISTER_SERIALIZATION (G_TYPE_UINT, uint);
5048 REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
5049 REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
5051 REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
5053 g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
5054 gst_value_transform_fourcc_string);
5055 g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
5056 gst_value_transform_int_range_string);
5057 g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
5058 gst_value_transform_int64_range_string);
5059 g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
5060 gst_value_transform_double_range_string);
5061 g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
5062 gst_value_transform_fraction_range_string);
5063 g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
5064 gst_value_transform_list_string);
5065 g_value_register_transform_func (GST_TYPE_ARRAY, G_TYPE_STRING,
5066 gst_value_transform_array_string);
5067 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_STRING,
5068 gst_value_transform_fraction_string);
5069 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_FRACTION,
5070 gst_value_transform_string_fraction);
5071 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
5072 gst_value_transform_fraction_double);
5073 g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
5074 gst_value_transform_fraction_float);
5075 g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
5076 gst_value_transform_double_fraction);
5077 g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
5078 gst_value_transform_float_fraction);
5079 g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
5080 gst_value_transform_date_string);
5081 g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
5082 gst_value_transform_string_date);
5083 g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
5084 gst_value_transform_object_string);
5086 gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5087 gst_value_intersect_int_int_range);
5088 gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5089 gst_value_intersect_int_range_int_range);
5090 gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5091 gst_value_intersect_int64_int64_range);
5092 gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5093 gst_value_intersect_int64_range_int64_range);
5094 gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5095 gst_value_intersect_double_double_range);
5096 gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
5097 GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
5098 gst_value_register_intersect_func (GST_TYPE_ARRAY,
5099 GST_TYPE_ARRAY, gst_value_intersect_array);
5100 gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5101 gst_value_intersect_fraction_fraction_range);
5102 gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
5103 GST_TYPE_FRACTION_RANGE,
5104 gst_value_intersect_fraction_range_fraction_range);
5106 gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5107 gst_value_subtract_int_int_range);
5108 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, G_TYPE_INT,
5109 gst_value_subtract_int_range_int);
5110 gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5111 gst_value_subtract_int_range_int_range);
5112 gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
5113 gst_value_subtract_int64_int64_range);
5114 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
5115 gst_value_subtract_int64_range_int64);
5116 gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
5117 gst_value_subtract_int64_range_int64_range);
5118 gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
5119 gst_value_subtract_double_double_range);
5120 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,
5121 gst_value_subtract_double_range_double);
5122 gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
5123 GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
5125 gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5126 gst_value_subtract_fraction_fraction_range);
5127 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
5128 gst_value_subtract_fraction_range_fraction);
5129 gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
5130 GST_TYPE_FRACTION_RANGE,
5131 gst_value_subtract_fraction_range_fraction_range);
5133 /* see bug #317246, #64994, #65041 */
5135 volatile GType date_type = G_TYPE_DATE;
5137 g_type_name (date_type);
5140 gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
5141 gst_value_union_int_int_range);
5142 gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
5143 gst_value_union_int_range_int_range);
5146 /* Implement these if needed */
5147 gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
5148 gst_value_union_fraction_fraction_range);
5149 gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
5150 GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);