From e2b6e88d5fddb5a277dd423f32aad1bd8666fa0e Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 2 Jun 2011 13:21:55 +0200 Subject: [PATCH] value: add function to fixate a value Add a function to fixate a GValue. This is the same function as is in GstPad. --- gst/gstvalue.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ gst/gstvalue.h | 2 ++ 2 files changed, 69 insertions(+) diff --git a/gst/gstvalue.c b/gst/gstvalue.c index 7ae3961b2b..c1fc50a98a 100644 --- a/gst/gstvalue.c +++ b/gst/gstvalue.c @@ -4036,6 +4036,73 @@ gst_value_is_fixed (const GValue * value) return gst_type_is_fixed (type); } +/** + * gst_value_fixate: + * @dest: the #GValue destination + * @src: the #GValue to fixate + * + * Fixate @src into a new value @dest. + * For ranges, the first element is taken. For lists and arrays, the + * first item is fixated and returned. + * If @src is already fixed, this function returns FALSE. + * + * Returns: true if @dest contains a fixated version of @src. + */ +gboolean +gst_value_fixate (GValue * dest, const GValue * src) +{ + g_return_val_if_fail (G_IS_VALUE (src), FALSE); + g_return_val_if_fail (dest != NULL, FALSE); + + if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) { + g_value_init (dest, G_TYPE_INT); + g_value_set_int (dest, gst_value_get_int_range_min (src)); + } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) { + g_value_init (dest, G_TYPE_DOUBLE); + g_value_set_double (dest, gst_value_get_double_range_min (src)); + } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) { + gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src)); + } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) { + GValue temp = { 0 }; + + /* list could be empty */ + if (gst_value_list_get_size (src) <= 0) + return FALSE; + + gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0)); + + if (!gst_value_fixate (dest, &temp)) + gst_value_init_and_copy (dest, &temp); + g_value_unset (&temp); + } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) { + gboolean res = FALSE; + guint n, len; + + len = gst_value_array_get_size (src); + g_value_init (dest, GST_TYPE_ARRAY); + for (n = 0; n < len; n++) { + GValue kid = { 0 }; + const GValue *orig_kid = gst_value_array_get_value (src, n); + + if (!gst_value_fixate (&kid, orig_kid)) + gst_value_init_and_copy (&kid, orig_kid); + else + res = TRUE; + gst_value_array_append_value (dest, &kid); + g_value_unset (&kid); + } + + if (!res) + g_value_unset (dest); + + return res; + } else { + return FALSE; + } + return TRUE; +} + + /************ * fraction * ************/ diff --git a/gst/gstvalue.h b/gst/gstvalue.h index 5de7449cd5..915581ac47 100644 --- a/gst/gstvalue.h +++ b/gst/gstvalue.h @@ -590,6 +590,8 @@ void gst_value_register_subtract_func (GType minuend_type, /* fixation */ gboolean gst_value_is_fixed (const GValue *value); +gboolean gst_value_fixate (GValue *dest, + const GValue *src); G_END_DECLS