/**
* SECTION:gstvalue
- * @short_description: GValue and GParamSpec implementations specific
+ * @short_description: GValue implementations specific
* to GStreamer
*
- * GValue and GParamSpec implementations specific to GStreamer.
+ * GValue implementations specific to GStreamer.
*
- * Note that operations on the same GstValue (or GValue) from multiple
- * threads may lead to undefined behaviour.
+ * Note that operations on the same #GValue from multiple threads may lead to
+ * undefined behaviour.
*
- * Last reviewed on 2006-03-07 (0.10.4)
+ * Last reviewed on 2008-03-11 (0.10.18)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "glib-compat-private.h"
#include <gst/gst.h>
#include <gobject/gvaluecollector.h>
+#include "gstutils.h"
typedef struct _GstValueUnionInfo GstValueUnionInfo;
struct _GstValueUnionInfo
GstValueSubtractFunc func;
};
+#define FUNDAMENTAL_TYPE_ID_MAX \
+ (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
+#define FUNDAMENTAL_TYPE_ID(type) \
+ ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
+
+#define VALUE_LIST_SIZE(v) (((GArray *) (v)->data[0].v_pointer)->len)
+#define VALUE_LIST_GET_VALUE(v, index) ((const GValue *) &g_array_index ((GArray *) (v)->data[0].v_pointer, GValue, (index)))
+
static GArray *gst_value_table;
+static GHashTable *gst_value_hash;
+static GstValueTable *gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID_MAX + 1];
static GArray *gst_value_union_funcs;
static GArray *gst_value_intersect_funcs;
static GArray *gst_value_subtract_funcs;
/* Forward declarations */
-static gint gst_greatest_common_divisor (gint a, gint b);
static gchar *gst_value_serialize_fraction (const GValue * value);
static GstValueCompareFunc gst_value_get_compare_func (const GValue * value1);
static gint gst_value_compare_with_func (const GValue * value1,
const GValue * value2, GstValueCompareFunc compare);
+static gchar *gst_string_wrap (const gchar * s);
+static gchar *gst_string_take_and_wrap (gchar * s);
+static gchar *gst_string_unwrap (const gchar * s);
+
+static inline GstValueTable *
+gst_value_hash_lookup_type (GType type)
+{
+ if (G_LIKELY (G_TYPE_IS_FUNDAMENTAL (type)))
+ return gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)];
+ else
+ return g_hash_table_lookup (gst_value_hash, (gpointer) type);
+}
+
+static void
+gst_value_hash_add_type (GType type, const GstValueTable * table)
+{
+ if (G_TYPE_IS_FUNDAMENTAL (type))
+ gst_value_tables_fundamental[FUNDAMENTAL_TYPE_ID (type)] = (gpointer) table;
+
+ g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table);
+}
+
/********
* list *
********/
GString *s;
GValue *v;
gchar *s_val;
+ guint alen = array->len;
/* estimate minimum string length to minimise re-allocs in GString */
- s = g_string_sized_new (2 + (6 * array->len) + 2);
+ s = g_string_sized_new (2 + (6 * alen) + 2);
g_string_append (s, begin);
- for (i = 0; i < array->len; i++) {
+ for (i = 0; i < alen; i++) {
v = &g_array_index (array, GValue, i);
s_val = gst_value_serialize (v);
g_string_append (s, s_val);
g_free (s_val);
- if (i < array->len - 1) {
+ if (i < alen - 1) {
g_string_append_len (s, ", ", 2);
}
}
GString *s;
guint i;
gchar *list_s;
+ guint alen;
array = src_value->data[0].v_pointer;
+ alen = array->len;
/* estimate minimum string length to minimise re-allocs in GString */
- s = g_string_sized_new (2 + (10 * array->len) + 2);
+ s = g_string_sized_new (2 + (10 * alen) + 2);
g_string_append (s, begin);
- for (i = 0; i < array->len; i++) {
+ for (i = 0; i < alen; i++) {
list_value = &g_array_index (array, GValue, i);
if (i != 0) {
* there. Do not export, since it doesn't work for types where the content
* decides the fixedness (e.g. GST_TYPE_ARRAY).
*/
-
static gboolean
gst_type_is_fixed (GType type)
{
+ /* the basic int, string, double types */
+ if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
+ return TRUE;
+ }
+ /* our fundamental types that are certainly not fixed */
if (type == GST_TYPE_INT_RANGE || type == GST_TYPE_DOUBLE_RANGE ||
- type == GST_TYPE_LIST) {
+ type == GST_TYPE_INT64_RANGE ||
+ type == GST_TYPE_LIST || type == GST_TYPE_FRACTION_RANGE) {
return FALSE;
}
- if (G_TYPE_FUNDAMENTAL (type) <=
- G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
+ /* other (boxed) types that are fixed */
+ if (type == GST_TYPE_BUFFER) {
return TRUE;
}
- if (type == GST_TYPE_BUFFER || type == GST_TYPE_FOURCC
- || type == GST_TYPE_ARRAY || type == GST_TYPE_FRACTION) {
+ /* heavy checks */
+ if (G_TYPE_IS_FUNDAMENTAL (type) || G_TYPE_FUNDAMENTAL (type) <=
+ G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
return TRUE;
}
copy_garray_of_gstvalue (const GArray * src)
{
GArray *dest;
- guint i;
+ guint i, len;
- dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), src->len);
- g_array_set_size (dest, src->len);
- for (i = 0; i < src->len; i++) {
+ len = src->len;
+ dest = g_array_sized_new (FALSE, TRUE, sizeof (GValue), len);
+ g_array_set_size (dest, len);
+ for (i = 0; i < len; i++) {
gst_value_init_and_copy (&g_array_index (dest, GValue, i),
&g_array_index (src, GValue, i));
}
static void
gst_value_free_list_or_array (GValue * value)
{
- guint i;
+ guint i, len;
GArray *src = (GArray *) value->data[0].v_pointer;
+ len = src->len;
if ((value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) == 0) {
- for (i = 0; i < src->len; i++) {
+ for (i = 0; i < len; i++) {
g_value_unset (&g_array_index (src, GValue, i));
}
g_array_free (src, TRUE);
GValue val = { 0, };
g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
+ g_return_if_fail (G_IS_VALUE (append_value));
gst_value_init_and_copy (&val, append_value);
g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
GValue val = { 0, };
g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
+ g_return_if_fail (G_IS_VALUE (prepend_value));
gst_value_init_and_copy (&val, prepend_value);
g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
/**
* gst_value_list_concat:
- * @dest: an uninitialized #GValue to take the result
+ * @dest: (out caller-allocates): an uninitialized #GValue to take the result
* @value1: a #GValue
* @value2: a #GValue
*
g_return_if_fail (G_IS_VALUE (value2));
value1_length =
- (GST_VALUE_HOLDS_LIST (value1) ? gst_value_list_get_size (value1) : 1);
+ (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
value2_length =
- (GST_VALUE_HOLDS_LIST (value2) ? gst_value_list_get_size (value2) : 1);
+ (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
g_value_init (dest, GST_TYPE_LIST);
array = (GArray *) dest->data[0].v_pointer;
g_array_set_size (array, value1_length + value2_length);
if (GST_VALUE_HOLDS_LIST (value1)) {
for (i = 0; i < value1_length; i++) {
gst_value_init_and_copy (&g_array_index (array, GValue, i),
- gst_value_list_get_value (value1, i));
+ VALUE_LIST_GET_VALUE (value1, i));
}
} else {
gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
if (GST_VALUE_HOLDS_LIST (value2)) {
for (i = 0; i < value2_length; i++) {
gst_value_init_and_copy (&g_array_index (array, GValue,
- i + value1_length), gst_value_list_get_value (value2, i));
+ i + value1_length), VALUE_LIST_GET_VALUE (value2, i));
}
} else {
gst_value_init_and_copy (&g_array_index (array, GValue, value1_length),
}
/**
+ * gst_value_list_merge:
+ * @dest: (out caller-allocates): an uninitialized #GValue to take the result
+ * @value1: a #GValue
+ * @value2: a #GValue
+ *
+ * Merges copies of @value1 and @value2. Values that are not
+ * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
+ *
+ * The result will be put into @dest and will either be a list that will not
+ * contain any duplicates, or a non-list type (if @value1 and @value2
+ * were equal).
+ *
+ * Since: 0.10.32
+ */
+void
+gst_value_list_merge (GValue * dest, const GValue * value1,
+ const GValue * value2)
+{
+ guint i, j, k, value1_length, value2_length, skipped;
+ const GValue *src;
+ gboolean skip;
+ GArray *array;
+
+ g_return_if_fail (dest != NULL);
+ g_return_if_fail (G_VALUE_TYPE (dest) == 0);
+ g_return_if_fail (G_IS_VALUE (value1));
+ g_return_if_fail (G_IS_VALUE (value2));
+
+ value1_length =
+ (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
+ value2_length =
+ (GST_VALUE_HOLDS_LIST (value2) ? VALUE_LIST_SIZE (value2) : 1);
+ g_value_init (dest, GST_TYPE_LIST);
+ array = (GArray *) dest->data[0].v_pointer;
+ g_array_set_size (array, value1_length + value2_length);
+
+ if (GST_VALUE_HOLDS_LIST (value1)) {
+ for (i = 0; i < value1_length; i++) {
+ gst_value_init_and_copy (&g_array_index (array, GValue, i),
+ VALUE_LIST_GET_VALUE (value1, i));
+ }
+ } else {
+ gst_value_init_and_copy (&g_array_index (array, GValue, 0), value1);
+ }
+
+ j = value1_length;
+ skipped = 0;
+ if (GST_VALUE_HOLDS_LIST (value2)) {
+ for (i = 0; i < value2_length; i++) {
+ skip = FALSE;
+ src = VALUE_LIST_GET_VALUE (value2, i);
+ for (k = 0; k < value1_length; k++) {
+ if (gst_value_compare (&g_array_index (array, GValue, k),
+ src) == GST_VALUE_EQUAL) {
+ skip = TRUE;
+ skipped++;
+ break;
+ }
+ }
+ if (!skip) {
+ gst_value_init_and_copy (&g_array_index (array, GValue, j), src);
+ j++;
+ }
+ }
+ } else {
+ skip = FALSE;
+ for (k = 0; k < value1_length; k++) {
+ if (gst_value_compare (&g_array_index (array, GValue, k),
+ value2) == GST_VALUE_EQUAL) {
+ skip = TRUE;
+ skipped++;
+ break;
+ }
+ }
+ if (!skip) {
+ gst_value_init_and_copy (&g_array_index (array, GValue, j), value2);
+ }
+ }
+ if (skipped) {
+ guint new_size = value1_length + (value2_length - skipped);
+
+ if (new_size > 1) {
+ /* shrink list */
+ g_array_set_size (array, new_size);
+ } else {
+ GValue single_dest;
+
+ /* size is 1, take single value in list and make it new dest */
+ single_dest = g_array_index (array, GValue, 0);
+
+ /* clean up old value allocations: must set array size to 0, because
+ * allocated values are not inited meaning g_value_unset() will not
+ * work on them */
+ g_array_set_size (array, 0);
+ g_value_unset (dest);
+
+ /* the single value is our new result */
+ *dest = single_dest;
+ }
+ }
+}
+
+/**
* gst_value_list_get_size:
* @value: a #GValue of type #GST_TYPE_LIST
*
* Gets the value that is a member of the list contained in @value and
* has the index @index.
*
- * Returns: the value at the given index
+ * Returns: (transfer none): the value at the given index
*/
const GValue *
gst_value_list_get_value (const GValue * value, guint index)
{
g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), NULL);
- g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
+ g_return_val_if_fail (index < VALUE_LIST_SIZE (value), NULL);
return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
GValue, index);
GValue val = { 0, };
g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
+ g_return_if_fail (G_IS_VALUE (append_value));
gst_value_init_and_copy (&val, append_value);
g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
GValue val = { 0, };
g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
+ g_return_if_fail (G_IS_VALUE (prepend_value));
gst_value_init_and_copy (&val, prepend_value);
g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
* Gets the value that is a member of the array contained in @value and
* has the index @index.
*
- * Returns: the value at the given index
+ * Returns: (transfer none): the value at the given index
*/
const GValue *
gst_value_array_get_value (const GValue * value, guint index)
}
/* Do an unordered compare of the contents of a list */
-static int
+static gint
gst_value_compare_list (const GValue * value1, const GValue * value2)
{
guint i, j;
}
/* Perform an ordered comparison of the contents of an array */
-static int
+static gint
gst_value_compare_array (const GValue * value1, const GValue * value2)
{
guint i;
GArray *array1 = value1->data[0].v_pointer;
GArray *array2 = value2->data[0].v_pointer;
+ guint len = array1->len;
GValue *v1;
GValue *v2;
- if (array1->len != array2->len)
+ if (len != array2->len)
return GST_VALUE_UNORDERED;
- for (i = 0; i < array1->len; i++) {
+ for (i = 0; i < len; i++) {
v1 = &g_array_index (array1, GValue, i);
v2 = &g_array_index (array2, GValue, i);
if (gst_value_compare (v1, v2) != GST_VALUE_EQUAL)
GValue * dest_value)
{
guint32 fourcc = src_value->data[0].v_int;
+ gchar fourcc_char[4];
- if (g_ascii_isprint ((fourcc >> 0) & 0xff) &&
- g_ascii_isprint ((fourcc >> 8) & 0xff) &&
- g_ascii_isprint ((fourcc >> 16) & 0xff) &&
- g_ascii_isprint ((fourcc >> 24) & 0xff)) {
+ fourcc_char[0] = (fourcc >> 0) & 0xff;
+ fourcc_char[1] = (fourcc >> 8) & 0xff;
+ fourcc_char[2] = (fourcc >> 16) & 0xff;
+ fourcc_char[3] = (fourcc >> 24) & 0xff;
+
+ if ((g_ascii_isalnum (fourcc_char[0]) || fourcc_char[0] == ' ') &&
+ (g_ascii_isalnum (fourcc_char[1]) || fourcc_char[1] == ' ') &&
+ (g_ascii_isalnum (fourcc_char[2]) || fourcc_char[2] == ' ') &&
+ (g_ascii_isalnum (fourcc_char[3]) || fourcc_char[3] == ' ')) {
dest_value->data[0].v_pointer =
g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
} else {
gst_value_serialize_fourcc (const GValue * value)
{
guint32 fourcc = value->data[0].v_int;
+ gchar fourcc_char[4];
+
+ fourcc_char[0] = (fourcc >> 0) & 0xff;
+ fourcc_char[1] = (fourcc >> 8) & 0xff;
+ fourcc_char[2] = (fourcc >> 16) & 0xff;
+ fourcc_char[3] = (fourcc >> 24) & 0xff;
- if (g_ascii_isalnum ((fourcc >> 0) & 0xff) &&
- g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
- g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
- g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
+ if ((g_ascii_isalnum (fourcc_char[0]) || fourcc_char[0] == ' ') &&
+ (g_ascii_isalnum (fourcc_char[1]) || fourcc_char[1] == ' ') &&
+ (g_ascii_isalnum (fourcc_char[2]) || fourcc_char[2] == ' ') &&
+ (g_ascii_isalnum (fourcc_char[3]) || fourcc_char[3] == ' ')) {
return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
} else {
return g_strdup_printf ("0x%08x", fourcc);
}
static gboolean
-gst_value_deserialize_fourcc (GValue * dest, const char *s)
+gst_value_deserialize_fourcc (GValue * dest, const gchar * s)
{
gboolean ret = FALSE;
guint32 fourcc = 0;
- char *end;
+ gchar *end;
+ gint l = strlen (s);
- if (strlen (s) == 4) {
+ if (l == 4) {
fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
ret = TRUE;
+ } else if (l == 3) {
+ fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], ' ');
+ ret = TRUE;
+ } else if (l == 2) {
+ fourcc = GST_MAKE_FOURCC (s[0], s[1], ' ', ' ');
+ ret = TRUE;
+ } else if (l == 1) {
+ fourcc = GST_MAKE_FOURCC (s[0], ' ', ' ', ' ');
+ ret = TRUE;
} else if (g_ascii_isdigit (*s)) {
fourcc = strtoul (s, &end, 0);
if (*end == 0) {
gst_value_collect_int_range (GValue * value, guint n_collect_values,
GTypeCValue * collect_values, guint collect_flags)
{
+ if (n_collect_values != 2)
+ return g_strdup_printf ("not enough value locations for `%s' passed",
+ G_VALUE_TYPE_NAME (value));
+ if (collect_values[0].v_int >= collect_values[1].v_int)
+ return g_strdup_printf ("range start is not smaller than end for `%s'",
+ G_VALUE_TYPE_NAME (value));
+
value->data[0].v_int = collect_values[0].v_int;
value->data[1].v_int = collect_values[1].v_int;
return FALSE;
}
+/***************
+ * int64 range *
+ ***************/
+
+static void
+gst_value_init_int64_range (GValue * value)
+{
+ value->data[0].v_int64 = 0;
+ value->data[1].v_int64 = 0;
+}
+
+static void
+gst_value_copy_int64_range (const GValue * src_value, GValue * dest_value)
+{
+ dest_value->data[0].v_int64 = src_value->data[0].v_int64;
+ dest_value->data[1].v_int64 = src_value->data[1].v_int64;
+}
+
+static gchar *
+gst_value_collect_int64_range (GValue * value, guint n_collect_values,
+ GTypeCValue * collect_values, guint collect_flags)
+{
+ if (n_collect_values != 2)
+ return g_strdup_printf ("not enough value locations for `%s' passed",
+ G_VALUE_TYPE_NAME (value));
+ if (collect_values[0].v_int64 >= collect_values[1].v_int64)
+ return g_strdup_printf ("range start is not smaller than end for `%s'",
+ G_VALUE_TYPE_NAME (value));
+
+ value->data[0].v_int64 = collect_values[0].v_int64;
+ value->data[1].v_int64 = collect_values[1].v_int64;
+
+ return NULL;
+}
+
+static gchar *
+gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
+ GTypeCValue * collect_values, guint collect_flags)
+{
+ guint64 *int_range_start = collect_values[0].v_pointer;
+ guint64 *int_range_end = collect_values[1].v_pointer;
+
+ if (!int_range_start)
+ return g_strdup_printf ("start value location for `%s' passed as NULL",
+ G_VALUE_TYPE_NAME (value));
+ if (!int_range_end)
+ return g_strdup_printf ("end value location for `%s' passed as NULL",
+ G_VALUE_TYPE_NAME (value));
+
+ *int_range_start = value->data[0].v_int64;
+ *int_range_end = value->data[1].v_int64;
+
+ return NULL;
+}
+
+/**
+ * gst_value_set_int64_range:
+ * @value: a GValue initialized to GST_TYPE_INT64_RANGE
+ * @start: the start of the range
+ * @end: the end of the range
+ *
+ * Sets @value to the range specified by @start and @end.
+ *
+ * Since: 0.10.31
+ */
+void
+gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
+{
+ g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
+ g_return_if_fail (start < end);
+
+ value->data[0].v_int64 = start;
+ value->data[1].v_int64 = end;
+}
+
+/**
+ * gst_value_get_int64_range_min:
+ * @value: a GValue initialized to GST_TYPE_INT64_RANGE
+ *
+ * Gets the minimum of the range specified by @value.
+ *
+ * Returns: the minimum of the range
+ *
+ * Since: 0.10.31
+ */
+gint64
+gst_value_get_int64_range_min (const GValue * value)
+{
+ g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
+
+ return value->data[0].v_int64;
+}
+
+/**
+ * gst_value_get_int64_range_max:
+ * @value: a GValue initialized to GST_TYPE_INT64_RANGE
+ *
+ * Gets the maximum of the range specified by @value.
+ *
+ * Returns: the maxumum of the range
+ *
+ * Since: 0.10.31
+ */
+gint64
+gst_value_get_int64_range_max (const GValue * value)
+{
+ g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
+
+ return value->data[1].v_int64;
+}
+
+static void
+gst_value_transform_int64_range_string (const GValue * src_value,
+ GValue * dest_value)
+{
+ dest_value->data[0].v_pointer =
+ g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
+ src_value->data[0].v_int64, src_value->data[1].v_int64);
+}
+
+static gint
+gst_value_compare_int64_range (const GValue * value1, const GValue * value2)
+{
+ if (value2->data[0].v_int64 == value1->data[0].v_int64 &&
+ value2->data[1].v_int64 == value1->data[1].v_int64)
+ return GST_VALUE_EQUAL;
+ return GST_VALUE_UNORDERED;
+}
+
+static gchar *
+gst_value_serialize_int64_range (const GValue * value)
+{
+ return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
+ value->data[0].v_int64, value->data[1].v_int64);
+}
+
+static gboolean
+gst_value_deserialize_int64_range (GValue * dest, const gchar * s)
+{
+ g_warning ("unimplemented");
+ return FALSE;
+}
+
/****************
* double range *
****************/
gst_value_collect_double_range (GValue * value, guint n_collect_values,
GTypeCValue * collect_values, guint collect_flags)
{
+ if (n_collect_values != 2)
+ return g_strdup_printf ("not enough value locations for `%s' passed",
+ G_VALUE_TYPE_NAME (value));
+ if (collect_values[0].v_double >= collect_values[1].v_double)
+ return g_strdup_printf ("range start is not smaller than end for `%s'",
+ G_VALUE_TYPE_NAME (value));
+
value->data[0].v_double = collect_values[0].v_double;
value->data[1].v_double = collect_values[1].v_double;
gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
{
g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
+ g_return_if_fail (start < end);
value->data[0].v_double = start;
value->data[1].v_double = end;
*
* Gets the minimum of the range specified by @value.
*
- * Returns: the minumum of the range
+ * Returns: the minimum of the range
*/
gdouble
gst_value_get_double_range_min (const GValue * value)
gst_value_transform_double_range_string (const GValue * src_value,
GValue * dest_value)
{
- char s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
+ gchar s1[G_ASCII_DTOSTR_BUF_SIZE], s2[G_ASCII_DTOSTR_BUF_SIZE];
dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
static gchar *
gst_value_serialize_double_range (const GValue * value)
{
- char d1[G_ASCII_DTOSTR_BUF_SIZE];
- char d2[G_ASCII_DTOSTR_BUF_SIZE];
+ gchar d1[G_ASCII_DTOSTR_BUF_SIZE];
+ gchar d2[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
g_ascii_dtostr (d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
gst_value_init_fraction_range (GValue * value)
{
GValue *vals;
+ GType ftype;
+
+ ftype = GST_TYPE_FRACTION;
- value->data[0].v_pointer = vals = g_new0 (GValue, 2);
- g_value_init (&vals[0], GST_TYPE_FRACTION);
- g_value_init (&vals[1], GST_TYPE_FRACTION);
+ value->data[0].v_pointer = vals = g_slice_alloc0 (2 * sizeof (GValue));
+ g_value_init (&vals[0], ftype);
+ g_value_init (&vals[1], ftype);
}
static void
if (vals != NULL) {
g_value_unset (&vals[0]);
g_value_unset (&vals[1]);
- g_free (vals);
+ g_slice_free1 (2 * sizeof (GValue), vals);
value->data[0].v_pointer = NULL;
}
}
GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
if (vals == NULL) {
- dest_value->data[0].v_pointer = vals = g_new0 (GValue, 2);
- g_return_if_fail (vals != NULL);
- g_value_init (&vals[0], GST_TYPE_FRACTION);
- g_value_init (&vals[1], GST_TYPE_FRACTION);
+ gst_value_init_fraction_range (dest_value);
+ vals = dest_value->data[0].v_pointer;
}
-
if (src_vals != NULL) {
g_value_copy (&src_vals[0], &vals[0]);
g_value_copy (&src_vals[1], &vals[1]);
if (n_collect_values != 4)
return g_strdup_printf ("not enough value locations for `%s' passed",
G_VALUE_TYPE_NAME (value));
+ if (collect_values[1].v_int == 0)
+ return g_strdup_printf ("passed '0' as first denominator for `%s'",
+ G_VALUE_TYPE_NAME (value));
+ if (collect_values[3].v_int == 0)
+ return g_strdup_printf ("passed '0' as second denominator for `%s'",
+ G_VALUE_TYPE_NAME (value));
+ if (gst_util_fraction_compare (collect_values[0].v_int,
+ collect_values[1].v_int, collect_values[2].v_int,
+ collect_values[3].v_int) >= 0)
+ return g_strdup_printf ("range start is not smaller than end for `%s'",
+ G_VALUE_TYPE_NAME (value));
+
if (vals == NULL) {
- value->data[0].v_pointer = vals = g_new0 (GValue, 2);
- if (vals == NULL)
- return g_strdup_printf ("Could not initialise`%s' during collect",
- G_VALUE_TYPE_NAME (value));
- g_value_init (&vals[0], GST_TYPE_FRACTION);
- g_value_init (&vals[1], GST_TYPE_FRACTION);
+ gst_value_init_fraction_range (value);
+ vals = value->data[0].v_pointer;
}
gst_value_set_fraction (&vals[0], collect_values[0].v_int,
gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
GTypeCValue * collect_values, guint collect_flags)
{
- int i;
- int *dest_values[4];
+ gint i;
+ gint *dest_values[4];
GValue *vals = (GValue *) value->data[0].v_pointer;
- if (n_collect_values != 4)
+ if (G_UNLIKELY (n_collect_values != 4))
return g_strdup_printf ("not enough value locations for `%s' passed",
G_VALUE_TYPE_NAME (value));
for (i = 0; i < 4; i++) {
- if (collect_values[i].v_pointer == NULL) {
+ if (G_UNLIKELY (collect_values[i].v_pointer == NULL)) {
return g_strdup_printf ("value location for `%s' passed as NULL",
G_VALUE_TYPE_NAME (value));
}
dest_values[i] = collect_values[i].v_pointer;
}
- if (vals == NULL) {
+ if (G_UNLIKELY (vals == NULL)) {
return g_strdup_printf ("Uninitialised `%s' passed",
G_VALUE_TYPE_NAME (value));
}
dest_values[0][0] = gst_value_get_fraction_numerator (&vals[0]);
dest_values[1][0] = gst_value_get_fraction_denominator (&vals[0]);
- dest_values[2][0] = gst_value_get_fraction_denominator (&vals[1]);
+ dest_values[2][0] = gst_value_get_fraction_numerator (&vals[1]);
dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
return NULL;
}
GValue *vals;
g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
+ g_return_if_fail (GST_VALUE_HOLDS_FRACTION (start));
+ g_return_if_fail (GST_VALUE_HOLDS_FRACTION (end));
+ g_return_if_fail (gst_util_fraction_compare (start->data[0].v_int,
+ start->data[1].v_int, end->data[0].v_int, end->data[1].v_int) < 0);
vals = (GValue *) value->data[0].v_pointer;
if (vals == NULL) {
- value->data[0].v_pointer = vals = g_new0 (GValue, 2);
- g_value_init (&vals[0], GST_TYPE_FRACTION);
- g_value_init (&vals[1], GST_TYPE_FRACTION);
+ gst_value_init_fraction_range (value);
+ vals = value->data[0].v_pointer;
}
-
g_value_copy (start, &vals[0]);
g_value_copy (end, &vals[1]);
}
GValue start = { 0 };
GValue end = { 0 };
+ g_return_if_fail (value != NULL);
+ g_return_if_fail (denominator_start != 0);
+ g_return_if_fail (denominator_end != 0);
+ g_return_if_fail (gst_util_fraction_compare (numerator_start,
+ denominator_start, numerator_end, denominator_end) < 0);
+
g_value_init (&start, GST_TYPE_FRACTION);
g_value_init (&end, GST_TYPE_FRACTION);
*
* Gets the minimum of the range specified by @value.
*
- * Returns: the minumum of the range
+ * Returns: the minimum of the range
*/
const GValue *
gst_value_get_fraction_range_min (const GValue * value)
{
GValue *vals;
- g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
+ g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
vals = (GValue *) value->data[0].v_pointer;
if (vals != NULL) {
{
GValue *vals;
- g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
+ g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), NULL);
vals = (GValue *) value->data[0].v_pointer;
if (vals != NULL) {
return NULL;
}
-static char *
+static gchar *
gst_value_serialize_fraction_range (const GValue * value)
{
GValue *vals = (GValue *) value->data[0].v_pointer;
/**
* gst_value_set_caps:
* @value: a GValue initialized to GST_TYPE_CAPS
- * @caps: the caps to set the value to
+ * @caps: (transfer none): the caps to set the value to
*
- * Sets the contents of @value to @caps. The actual
- * #GstCaps structure is copied before it is used.
+ * Sets the contents of @value to @caps. A reference to the
+ * provided @caps will be taken by the @value.
*/
void
gst_value_set_caps (GValue * value, const GstCaps * caps)
{
+ g_return_if_fail (G_IS_VALUE (value));
g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS);
+ g_return_if_fail (caps == NULL || GST_IS_CAPS (caps));
g_value_set_boxed (value, caps);
}
* gst_value_get_caps:
* @value: a GValue initialized to GST_TYPE_CAPS
*
- * Gets the contents of @value.
+ * Gets the contents of @value. The reference count of the returned
+ * #GstCaps will not be modified, therefore the caller must take one
+ * before getting rid of the @value.
*
- * Returns: the contents of @value
+ * Returns: (transfer none): the contents of @value
*/
const GstCaps *
gst_value_get_caps (const GValue * value)
{
+ g_return_val_if_fail (G_IS_VALUE (value), NULL);
g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_CAPS, NULL);
return (GstCaps *) g_value_get_boxed (value);
}
-static char *
+static gchar *
gst_value_serialize_caps (const GValue * value)
{
GstCaps *caps = g_value_get_boxed (value);
caps = gst_caps_from_string (s);
if (caps) {
- g_value_set_boxed (dest, caps);
+ g_value_take_boxed (dest, caps);
return TRUE;
}
return FALSE;
void
gst_value_set_structure (GValue * value, const GstStructure * structure)
{
+ g_return_if_fail (G_IS_VALUE (value));
g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
+ g_return_if_fail (structure == NULL || GST_IS_STRUCTURE (structure));
g_value_set_boxed (value, structure);
}
*
* Gets the contents of @value.
*
- * Returns: the contents of @value
+ * Returns: (transfer none): the contents of @value
*
* Since: 0.10.15
*/
const GstStructure *
gst_value_get_structure (const GValue * value)
{
+ g_return_val_if_fail (G_IS_VALUE (value), NULL);
g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
return (GstStructure *) g_value_get_boxed (value);
}
-static char *
+static gchar *
gst_value_serialize_structure (const GValue * value)
{
GstStructure *structure = g_value_get_boxed (value);
- return gst_structure_to_string (structure);
+ return gst_string_take_and_wrap (gst_structure_to_string (structure));
}
static gboolean
{
GstStructure *structure;
- structure = gst_structure_from_string (s, NULL);
+ if (*s != '"') {
+ structure = gst_structure_from_string (s, NULL);
+ } else {
+ gchar *str = gst_string_unwrap (s);
+
+ if (G_UNLIKELY (!str))
+ return FALSE;
+
+ structure = gst_structure_from_string (str, NULL);
+ g_free (str);
+ }
- if (structure) {
- g_value_set_boxed (dest, structure);
+ if (G_LIKELY (structure)) {
+ g_value_take_boxed (dest, structure);
return TRUE;
}
return FALSE;
* GstBuffer *
*************/
-static int
+static gint
gst_value_compare_buffer (const GValue * value1, const GValue * value2)
{
GstBuffer *buf1 = GST_BUFFER (gst_value_get_mini_object (value1));
return GST_VALUE_UNORDERED;
}
-static char *
+static gchar *
gst_value_serialize_buffer (const GValue * value)
{
guint8 *data;
- int i;
- int size;
- char *string;
+ gint i;
+ gint size;
+ gchar *string;
GstBuffer *buffer;
buffer = gst_value_get_buffer (value);
gst_value_deserialize_buffer (GValue * dest, const gchar * s)
{
GstBuffer *buffer;
- int len;
- char ts[3];
+ gint len;
+ gchar ts[3];
guint8 *data;
- int i;
+ gint i;
len = strlen (s);
if (len & 1)
* boolean *
***********/
-static int
+static gint
gst_value_compare_boolean (const GValue * value1, const GValue * value2)
{
if ((value1->data[0].v_int != 0) == (value2->data[0].v_int != 0))
return GST_VALUE_UNORDERED;
}
-static char *
+static gchar *
gst_value_serialize_boolean (const GValue * value)
{
if (value->data[0].v_int) {
return GST_VALUE_EQUAL; \
} \
\
-static char * \
+static gchar * \
gst_value_serialize_ ## _type (const GValue * value) \
{ \
GValue val = { 0, }; \
gint64 min, gint64 max, gint size)
{
gboolean ret = FALSE;
- char *end;
+ gchar *end;
gint64 mask = -1;
errno = 0;
gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
{ \
gint64 x; \
- char *end; \
+ gchar *end; \
gboolean ret = FALSE; \
\
errno = 0; \
CREATE_USERIALIZATION (uint64, UINT64);
CREATE_USERIALIZATION (ulong, ULONG);
+/* FIXME 0.11: remove this again, plugins shouldn't have uchar properties */
+#ifndef G_MAXUCHAR
+#define G_MAXUCHAR 255
+#endif
+CREATE_USERIALIZATION (uchar, UCHAR);
+
/**********
* double *
**********/
-static int
+static gint
gst_value_compare_double (const GValue * value1, const GValue * value2)
{
if (value1->data[0].v_double > value2->data[0].v_double)
return GST_VALUE_UNORDERED;
}
-static char *
+static gchar *
gst_value_serialize_double (const GValue * value)
{
- char d[G_ASCII_DTOSTR_BUF_SIZE];
+ gchar d[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr (d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
return g_strdup (d);
static gboolean
gst_value_deserialize_double (GValue * dest, const gchar * s)
{
- double x;
+ gdouble x;
gboolean ret = FALSE;
- char *end;
+ gchar *end;
x = g_ascii_strtod (s, &end);
if (*end == 0) {
static gboolean
gst_value_deserialize_float (GValue * dest, const gchar * s)
{
- double x;
+ gdouble x;
gboolean ret = FALSE;
- char *end;
+ gchar *end;
x = g_ascii_strtod (s, &end);
if (*end == 0) {
static gint
gst_value_compare_string (const GValue * value1, const GValue * value2)
{
- int x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
+ if (G_UNLIKELY (!value1->data[0].v_pointer || !value2->data[0].v_pointer)) {
+ /* if only one is NULL, no match - otherwise both NULL == EQUAL */
+ if (value1->data[0].v_pointer != value2->data[0].v_pointer)
+ return GST_VALUE_UNORDERED;
+ } else {
+ gint x = strcmp (value1->data[0].v_pointer, value2->data[0].v_pointer);
+
+ if (x < 0)
+ return GST_VALUE_LESS_THAN;
+ if (x > 0)
+ return GST_VALUE_GREATER_THAN;
+ }
- if (x < 0)
- return GST_VALUE_LESS_THAN;
- if (x > 0)
- return GST_VALUE_GREATER_THAN;
return GST_VALUE_EQUAL;
}
-/* keep in sync with gststructure.c */
-#define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
- ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
- ((c) == '.'))
-
-static gchar *
-gst_string_wrap (const gchar * s)
+static gint
+gst_string_measure_wrapping (const gchar * s)
{
- const gchar *t;
- int len;
- gchar *d, *e;
+ gint len;
gboolean wrap = FALSE;
+ if (G_UNLIKELY (s == NULL))
+ return -1;
+
+ /* Special case: the actual string NULL needs wrapping */
+ if (G_UNLIKELY (strcmp (s, "NULL") == 0))
+ return 4;
+
len = 0;
- t = s;
- if (!s)
- return NULL;
- while (*t) {
- if (GST_ASCII_IS_STRING (*t)) {
+ while (*s) {
+ if (GST_ASCII_IS_STRING (*s)) {
len++;
- } else if (*t < 0x20 || *t >= 0x7f) {
+ } else if (*s < 0x20 || *s >= 0x7f) {
wrap = TRUE;
len += 4;
} else {
wrap = TRUE;
len += 2;
}
- t++;
+ s++;
}
- if (!wrap)
- return g_strdup (s);
+ /* Wrap the string if we found something that needs
+ * wrapping, or the empty string (len == 0) */
+ return (wrap || len == 0) ? len : -1;
+}
+
+static gchar *
+gst_string_wrap_inner (const gchar * s, gint len)
+{
+ gchar *d, *e;
e = d = g_malloc (len + 3);
*e++ = '\"';
- t = s;
- while (*t) {
- if (GST_ASCII_IS_STRING (*t)) {
- *e++ = *t++;
- } else if (*t < 0x20 || *t >= 0x7f) {
+ while (*s) {
+ if (GST_ASCII_IS_STRING (*s)) {
+ *e++ = *s++;
+ } else if (*s < 0x20 || *s >= 0x7f) {
*e++ = '\\';
- *e++ = '0' + ((*(guchar *) t) >> 6);
- *e++ = '0' + (((*t) >> 3) & 0x7);
- *e++ = '0' + ((*t++) & 0x7);
+ *e++ = '0' + ((*(guchar *) s) >> 6);
+ *e++ = '0' + (((*s) >> 3) & 0x7);
+ *e++ = '0' + ((*s++) & 0x7);
} else {
*e++ = '\\';
- *e++ = *t++;
+ *e++ = *s++;
}
}
*e++ = '\"';
*e = 0;
+ g_assert (e - d <= len + 3);
return d;
}
-/*
- * This function takes a string delimited with double quotes (")
- * and unescapes any \xxx octal numbers.
- *
- * If sequences of \y are found where y is not in the range of
- * 0->3, y is copied unescaped.
- *
- * If \xyy is found where x is an octal number but y is not, an
- * error is encountered and NULL is returned.
- *
- * the input string must be \0 terminated.
+/* Do string wrapping/escaping */
+static gchar *
+gst_string_wrap (const gchar * s)
+{
+ gint len = gst_string_measure_wrapping (s);
+
+ if (G_LIKELY (len < 0))
+ return g_strdup (s);
+
+ return gst_string_wrap_inner (s, len);
+}
+
+/* Same as above, but take ownership of the string */
+static gchar *
+gst_string_take_and_wrap (gchar * s)
+{
+ gchar *out;
+ gint len = gst_string_measure_wrapping (s);
+
+ if (G_LIKELY (len < 0))
+ return s;
+
+ out = gst_string_wrap_inner (s, len);
+ g_free (s);
+
+ return out;
+}
+
+/*
+ * This function takes a string delimited with double quotes (")
+ * and unescapes any \xxx octal numbers.
+ *
+ * If sequences of \y are found where y is not in the range of
+ * 0->3, y is copied unescaped.
+ *
+ * If \xyy is found where x is an octal number but y is not, an
+ * error is encountered and NULL is returned.
+ *
+ * the input string must be \0 terminated.
*/
static gchar *
gst_string_unwrap (const gchar * s)
read += 3;
} else {
- /* if we run into a \0 here, we definately won't get a quote later */
+ /* if we run into a \0 here, we definitely won't get a quote later */
if (*read == 0)
goto beach;
goto beach;
/* null terminate result string and return */
- *write++ = '\0';
+ *write = '\0';
return ret;
beach:
static gboolean
gst_value_deserialize_string (GValue * dest, const gchar * s)
{
- if (*s != '"') {
+ if (G_UNLIKELY (strcmp (s, "NULL") == 0)) {
+ g_value_set_string (dest, NULL);
+ return TRUE;
+ } else if (G_LIKELY (*s != '"')) {
if (!g_utf8_validate (s, -1, NULL))
return FALSE;
g_value_set_string (dest, s);
return TRUE;
} else {
gchar *str = gst_string_unwrap (s);
-
- if (!str)
+ if (G_UNLIKELY (!str))
return FALSE;
g_value_take_string (dest, str);
}
if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (value) == GST_TYPE_FORMAT)) {
const GstFormatDefinition *format_def;
- format_def = gst_format_get_details (g_value_get_enum (value));
+ format_def = gst_format_get_details ((GstFormat) g_value_get_enum (value));
g_return_val_if_fail (format_def != NULL, NULL);
return g_strdup (format_def->description);
}
}
static gboolean
+gst_value_intersect_int64_int64_range (GValue * dest, const GValue * src1,
+ const GValue * src2)
+{
+ if (src2->data[0].v_int64 <= src1->data[0].v_int64 &&
+ src2->data[1].v_int64 >= src1->data[0].v_int64) {
+ gst_value_init_and_copy (dest, src1);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
+ const GValue * src2)
+{
+ gint64 min;
+ gint64 max;
+
+ min = MAX (src1->data[0].v_int64, src2->data[0].v_int64);
+ max = MIN (src1->data[1].v_int64, src2->data[1].v_int64);
+
+ if (min < max) {
+ g_value_init (dest, GST_TYPE_INT64_RANGE);
+ gst_value_set_int64_range (dest, min, max);
+ return TRUE;
+ }
+ if (min == max) {
+ g_value_init (dest, G_TYPE_INT64);
+ g_value_set_int64 (dest, min);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
gst_value_intersect_double_double_range (GValue * dest, const GValue * src1,
const GValue * src2)
{
GValue intersection = { 0, };
gboolean ret = FALSE;
- size = gst_value_list_get_size (value1);
+ size = VALUE_LIST_SIZE (value1);
for (i = 0; i < size; i++) {
- const GValue *cur = gst_value_list_get_value (value1, i);
+ const GValue *cur = VALUE_LIST_GET_VALUE (value1, i);
if (gst_value_intersect (&intersection, cur, value2)) {
/* append value */
gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
const GValue * src2)
{
- int res1, res2;
+ gint res1, res2;
GValue *vals;
GstValueCompareFunc compare;
}
static gboolean
- gst_value_intersect_fraction_range_fraction_range
- (GValue * dest, const GValue * src1, const GValue * src2)
+gst_value_intersect_fraction_range_fraction_range (GValue * dest,
+ const GValue * src1, const GValue * src2)
{
GValue *min;
GValue *max;
- int res;
+ gint res;
GValue *vals1, *vals2;
GstValueCompareFunc compare;
gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
const GValue * subtrahend)
{
- int min = gst_value_get_int_range_min (subtrahend);
- int max = gst_value_get_int_range_max (subtrahend);
- int val = g_value_get_int (minuend);
+ gint min = gst_value_get_int_range_min (subtrahend);
+ gint max = gst_value_get_int_range_max (subtrahend);
+ gint val = g_value_get_int (minuend);
/* subtracting a range from an int only works if the int is not in the
* range */
}
static gboolean
+gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
+ const GValue * subtrahend)
+{
+ gint64 min = gst_value_get_int64_range_min (subtrahend);
+ gint64 max = gst_value_get_int64_range_max (subtrahend);
+ gint64 val = g_value_get_int64 (minuend);
+
+ /* subtracting a range from an int64 only works if the int64 is not in the
+ * range */
+ if (val < min || val > max) {
+ /* and the result is the int64 */
+ gst_value_init_and_copy (dest, minuend);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/* creates a new int64 range based on input values.
+ */
+static gboolean
+gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
+ gint64 min2, gint64 max2)
+{
+ GValue v1 = { 0, };
+ GValue v2 = { 0, };
+ GValue *pv1, *pv2; /* yeah, hungarian! */
+
+ if (min1 <= max1 && min2 <= max2) {
+ pv1 = &v1;
+ pv2 = &v2;
+ } else if (min1 <= max1) {
+ pv1 = dest;
+ pv2 = NULL;
+ } else if (min2 <= max2) {
+ pv1 = NULL;
+ pv2 = dest;
+ } else {
+ return FALSE;
+ }
+
+ if (min1 < max1) {
+ g_value_init (pv1, GST_TYPE_INT64_RANGE);
+ gst_value_set_int64_range (pv1, min1, max1);
+ } else if (min1 == max1) {
+ g_value_init (pv1, G_TYPE_INT64);
+ g_value_set_int64 (pv1, min1);
+ }
+ if (min2 < max2) {
+ g_value_init (pv2, GST_TYPE_INT64_RANGE);
+ gst_value_set_int64_range (pv2, min2, max2);
+ } else if (min2 == max2) {
+ g_value_init (pv2, G_TYPE_INT64);
+ g_value_set_int64 (pv2, min2);
+ }
+
+ if (min1 <= max1 && min2 <= max2) {
+ gst_value_list_concat (dest, pv1, pv2);
+ g_value_unset (pv1);
+ g_value_unset (pv2);
+ }
+ return TRUE;
+}
+
+static gboolean
+gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
+ const GValue * subtrahend)
+{
+ gint64 min = gst_value_get_int64_range_min (minuend);
+ gint64 max = gst_value_get_int64_range_max (minuend);
+ gint64 val = g_value_get_int64 (subtrahend);
+
+ g_return_val_if_fail (min < max, FALSE);
+
+ /* value is outside of the range, return range unchanged */
+ if (val < min || val > max) {
+ gst_value_init_and_copy (dest, minuend);
+ return TRUE;
+ } else {
+ /* max must be MAXINT64 too as val <= max */
+ if (val == G_MAXINT64) {
+ max--;
+ val--;
+ }
+ /* min must be MININT64 too as val >= max */
+ if (val == G_MININT64) {
+ min++;
+ val++;
+ }
+ gst_value_create_new_int64_range (dest, min, val - 1, val + 1, max);
+ }
+ return TRUE;
+}
+
+static gboolean
+gst_value_subtract_int64_range_int64_range (GValue * dest,
+ const GValue * minuend, const GValue * subtrahend)
+{
+ gint64 min1 = gst_value_get_int64_range_min (minuend);
+ gint64 max1 = gst_value_get_int64_range_max (minuend);
+ gint64 min2 = gst_value_get_int64_range_min (subtrahend);
+ gint64 max2 = gst_value_get_int64_range_max (subtrahend);
+
+ if (max2 == G_MAXINT64 && min2 == G_MININT64) {
+ return FALSE;
+ } else if (max2 == G_MAXINT64) {
+ return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
+ 1, 0);
+ } else if (min2 == G_MININT64) {
+ return gst_value_create_new_int64_range (dest, MAX (max2 + 1, min1), max1,
+ 1, 0);
+ } else {
+ return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
+ MAX (max2 + 1, min1), max1);
+ }
+}
+
+static gboolean
gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
const GValue * subtrahend)
{
guint i, size;
GValue subtraction = { 0, };
gboolean ret = FALSE;
+ GType ltype;
+
+ ltype = gst_value_list_get_type ();
- size = gst_value_list_get_size (minuend);
+ size = VALUE_LIST_SIZE (minuend);
for (i = 0; i < size; i++) {
- const GValue *cur = gst_value_list_get_value (minuend, i);
+ const GValue *cur = VALUE_LIST_GET_VALUE (minuend, i);
if (gst_value_subtract (&subtraction, cur, subtrahend)) {
if (!ret) {
gst_value_init_and_copy (dest, &subtraction);
ret = TRUE;
- } else if (GST_VALUE_HOLDS_LIST (dest)
- && GST_VALUE_HOLDS_LIST (&subtraction)) {
- /* unroll */
- GValue unroll = { 0, };
-
- gst_value_init_and_copy (&unroll, dest);
- g_value_unset (dest);
- gst_value_list_concat (dest, &unroll, &subtraction);
- } else if (GST_VALUE_HOLDS_LIST (dest)) {
+ } else if (G_VALUE_HOLDS (dest, ltype)
+ && !G_VALUE_HOLDS (&subtraction, ltype)) {
gst_value_list_append_value (dest, &subtraction);
} else {
GValue temp = { 0, };
GValue *subtraction = &data[0], *result = &data[1];
gst_value_init_and_copy (result, minuend);
- size = gst_value_list_get_size (subtrahend);
+ size = VALUE_LIST_SIZE (subtrahend);
for (i = 0; i < size; i++) {
- const GValue *cur = gst_value_list_get_value (subtrahend, i);
+ const GValue *cur = VALUE_LIST_GET_VALUE (subtrahend, i);
if (gst_value_subtract (subtraction, result, cur)) {
GValue *temp = result;
const GValue *max2 = gst_value_get_fraction_range_max (minuend);
const GValue *max1 = gst_value_get_fraction_range_min (subtrahend);
const GValue *min2 = gst_value_get_fraction_range_max (subtrahend);
- int cmp1, cmp2;
+ gint cmp1, cmp2;
GValue v1 = { 0, };
GValue v2 = { 0, };
GValue *pv1, *pv2; /* yeah, hungarian! */
* comparison *
**************/
-/**
- * gst_value_can_compare:
- * @value1: a value to compare
- * @value2: another value to compare
- *
- * Determines if @value1 and @value2 can be compared.
- *
- * Returns: TRUE if the values can be compared
- */
-gboolean
-gst_value_can_compare (const GValue * value1, const GValue * value2)
-{
- GstValueTable *table;
- guint i;
-
- if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
- return FALSE;
-
- for (i = 0; i < gst_value_table->len; i++) {
- table = &g_array_index (gst_value_table, GstValueTable, i);
- if (g_type_is_a (G_VALUE_TYPE (value1), table->type) && table->compare)
- return TRUE;
- }
-
- return FALSE;
-}
-
/*
* gst_value_get_compare_func:
* @value1: a value to get the compare function for
{
GstValueTable *table, *best = NULL;
guint i;
+ GType type1;
- for (i = 0; i < gst_value_table->len; i++) {
- table = &g_array_index (gst_value_table, GstValueTable, i);
- if (table->type == G_VALUE_TYPE (value1) && table->compare != NULL) {
- best = table;
- break;
- }
- if (g_type_is_a (G_VALUE_TYPE (value1), table->type)) {
- if (!best || g_type_is_a (table->type, best->type))
- best = table;
+ type1 = G_VALUE_TYPE (value1);
+
+ /* this is a fast check */
+ best = gst_value_hash_lookup_type (type1);
+
+ /* slower checks */
+ if (G_UNLIKELY (!best || !best->compare)) {
+ guint len = gst_value_table->len;
+
+ best = NULL;
+ for (i = 0; i < len; i++) {
+ table = &g_array_index (gst_value_table, GstValueTable, i);
+ if (table->compare && g_type_is_a (type1, table->type)) {
+ if (!best || g_type_is_a (table->type, best->type))
+ best = table;
+ }
}
}
- if (best) {
+ if (G_LIKELY (best))
return best->compare;
- }
+
return NULL;
}
/**
+ * gst_value_can_compare:
+ * @value1: a value to compare
+ * @value2: another value to compare
+ *
+ * Determines if @value1 and @value2 can be compared.
+ *
+ * Returns: TRUE if the values can be compared
+ */
+gboolean
+gst_value_can_compare (const GValue * value1, const GValue * value2)
+{
+ g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
+ g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
+
+ if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
+ return FALSE;
+
+ return gst_value_get_compare_func (value1) != NULL;
+}
+
+/**
* gst_value_compare:
* @value1: a value to compare
* @value2: another value to compare
* If @value1 is less than @value2, GST_VALUE_LESS_THAN is returned.
* If the values are equal, GST_VALUE_EQUAL is returned.
*
- * Returns: A #GstValueCompareType value
+ * Returns: comparison result
*/
gint
gst_value_compare (const GValue * value1, const GValue * value2)
{
GstValueCompareFunc compare;
+ GType ltype;
+
+ g_return_val_if_fail (G_IS_VALUE (value1), GST_VALUE_LESS_THAN);
+ g_return_val_if_fail (G_IS_VALUE (value2), GST_VALUE_GREATER_THAN);
+
+ /* Special case: lists and scalar values
+ * "{ 1 }" and "1" are equal */
+ ltype = gst_value_list_get_type ();
+ if (G_VALUE_HOLDS (value1, ltype) && !G_VALUE_HOLDS (value2, ltype)
+ && gst_value_list_get_size (value1) == 1) {
+ const GValue *elt;
+
+ elt = gst_value_list_get_value (value1, 0);
+ return gst_value_compare (elt, value2);
+ } else if (G_VALUE_HOLDS (value2, ltype) && !G_VALUE_HOLDS (value1, ltype)
+ && gst_value_list_get_size (value2) == 1) {
+ const GValue *elt;
+
+ elt = gst_value_list_get_value (value2, 0);
+ return gst_value_compare (elt, value1);
+ }
if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
return GST_VALUE_UNORDERED;
* gst_value_compare() but allows to save time determining the compare function
* a multiple times.
*
- * Returns: A #GstValueCompareType value
+ * Returns: comparison result
*/
static gint
gst_value_compare_with_func (const GValue * value1, const GValue * value2,
gst_value_can_union (const GValue * value1, const GValue * value2)
{
GstValueUnionInfo *union_info;
- guint i;
+ guint i, len;
+
+ g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
+ g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
+
+ len = gst_value_union_funcs->len;
- for (i = 0; i < gst_value_union_funcs->len; i++) {
+ for (i = 0; i < len; i++) {
union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
if (union_info->type1 == G_VALUE_TYPE (value1) &&
union_info->type2 == G_VALUE_TYPE (value2))
/**
* gst_value_union:
- * @dest: the destination value
+ * @dest: (out caller-allocates): the destination value
* @value1: a value to union
* @value2: another value to union
*
- * Creates a GValue cooresponding to the union of @value1 and @value2.
+ * Creates a GValue corresponding to the union of @value1 and @value2.
*
* Returns: always returns %TRUE
*/
gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
{
GstValueUnionInfo *union_info;
- guint i;
+ guint i, len;
+
+ g_return_val_if_fail (dest != NULL, FALSE);
+ g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
+ g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
- for (i = 0; i < gst_value_union_funcs->len; i++) {
+ len = gst_value_union_funcs->len;
+
+ for (i = 0; i < len; i++) {
union_info = &g_array_index (gst_value_union_funcs, GstValueUnionInfo, i);
if (union_info->type1 == G_VALUE_TYPE (value1) &&
union_info->type2 == G_VALUE_TYPE (value2)) {
* gst_value_register_union_func:
* @type1: a type to union
* @type2: another type to union
- * @func: a function that implments creating a union between the two types
+ * @func: a function that implements creating a union between the two types
*
- * Registers a union function that can create a union between GValues
+ * Registers a union function that can create a union between #GValue items
* of the type @type1 and @type2.
*
* Union functions should be registered at startup before any pipelines are
* Determines if intersecting two values will produce a valid result.
* Two values will produce a valid intersection if they have the same
* type, or if there is a method (registered by
- * gst_value_register_intersection_func()) to calculate the intersection.
+ * gst_value_register_intersect_func()) to calculate the intersection.
*
* Returns: TRUE if the values can intersect
*/
gst_value_can_intersect (const GValue * value1, const GValue * value2)
{
GstValueIntersectInfo *intersect_info;
- guint i;
+ guint i, len;
+ GType ltype, type1, type2;
+
+ g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
+ g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
+
+ ltype = gst_value_list_get_type ();
/* special cases */
- if (GST_VALUE_HOLDS_LIST (value1) || GST_VALUE_HOLDS_LIST (value2))
+ if (G_VALUE_HOLDS (value1, ltype) || G_VALUE_HOLDS (value2, ltype))
return TRUE;
- for (i = 0; i < gst_value_intersect_funcs->len; i++) {
+ type1 = G_VALUE_TYPE (value1);
+ type2 = G_VALUE_TYPE (value2);
+
+ /* practically all GstValue types have a compare function (_can_compare=TRUE)
+ * GstStructure and GstCaps have npot, but are intersectable */
+ if (type1 == type2)
+ return TRUE;
+
+ /* check registered intersect functions */
+ len = gst_value_intersect_funcs->len;
+ for (i = 0; i < len; i++) {
intersect_info = &g_array_index (gst_value_intersect_funcs,
GstValueIntersectInfo, i);
- if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
- intersect_info->type2 == G_VALUE_TYPE (value2))
- if (intersect_info->type2 == G_VALUE_TYPE (value1) &&
- intersect_info->type1 == G_VALUE_TYPE (value2))
- return TRUE;
+ if ((intersect_info->type1 == type1 && intersect_info->type2 == type2) ||
+ (intersect_info->type1 == type2 && intersect_info->type2 == type1))
+ return TRUE;
}
return gst_value_can_compare (value1, value2);
/**
* gst_value_intersect:
- * @dest: a uninitialized #GValue that will hold the calculated
+ * @dest: (out caller-allocates): a uninitialized #GValue that will hold the calculated
* intersection value
* @value1: a value to intersect
* @value2: another value to intersect
const GValue * value2)
{
GstValueIntersectInfo *intersect_info;
- guint i;
- gboolean ret = FALSE;
+ guint i, len;
+ GType ltype, type1, type2;
+
+ g_return_val_if_fail (dest != NULL, FALSE);
+ g_return_val_if_fail (G_IS_VALUE (value1), FALSE);
+ g_return_val_if_fail (G_IS_VALUE (value2), FALSE);
+
+ ltype = gst_value_list_get_type ();
/* special cases first */
- if (GST_VALUE_HOLDS_LIST (value1))
+ if (G_VALUE_HOLDS (value1, ltype))
return gst_value_intersect_list (dest, value1, value2);
- if (GST_VALUE_HOLDS_LIST (value2))
+ if (G_VALUE_HOLDS (value2, ltype))
return gst_value_intersect_list (dest, value2, value1);
- for (i = 0; i < gst_value_intersect_funcs->len; i++) {
+ if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
+ gst_value_init_and_copy (dest, value1);
+ return TRUE;
+ }
+
+ type1 = G_VALUE_TYPE (value1);
+ type2 = G_VALUE_TYPE (value2);
+
+ len = gst_value_intersect_funcs->len;
+ for (i = 0; i < len; i++) {
intersect_info = &g_array_index (gst_value_intersect_funcs,
GstValueIntersectInfo, i);
- if (intersect_info->type1 == G_VALUE_TYPE (value1) &&
- intersect_info->type2 == G_VALUE_TYPE (value2)) {
- ret = intersect_info->func (dest, value1, value2);
- return ret;
+ if (intersect_info->type1 == type1 && intersect_info->type2 == type2) {
+ return intersect_info->func (dest, value1, value2);
}
- if (intersect_info->type1 == G_VALUE_TYPE (value2) &&
- intersect_info->type2 == G_VALUE_TYPE (value1)) {
- ret = intersect_info->func (dest, value2, value1);
- return ret;
+ if (intersect_info->type1 == type2 && intersect_info->type2 == type1) {
+ return intersect_info->func (dest, value2, value1);
}
}
-
- if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL) {
- gst_value_init_and_copy (dest, value1);
- ret = TRUE;
- }
-
- return ret;
+ return FALSE;
}
/**
/**
* gst_value_subtract:
- * @dest: the destination value for the result if the subtraction is not empty
+ * @dest: (out caller-allocates): the destination value for the result if the
+ * subtraction is not empty
* @minuend: the value to subtract from
* @subtrahend: the value to subtract
*
const GValue * subtrahend)
{
GstValueSubtractInfo *info;
- guint i;
+ guint i, len;
+ GType ltype, mtype, stype;
+
+ g_return_val_if_fail (dest != NULL, FALSE);
+ g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
+ g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
+
+ ltype = gst_value_list_get_type ();
/* special cases first */
- if (GST_VALUE_HOLDS_LIST (minuend))
+ if (G_VALUE_HOLDS (minuend, ltype))
return gst_value_subtract_from_list (dest, minuend, subtrahend);
- if (GST_VALUE_HOLDS_LIST (subtrahend))
+ if (G_VALUE_HOLDS (subtrahend, ltype))
return gst_value_subtract_list (dest, minuend, subtrahend);
- for (i = 0; i < gst_value_subtract_funcs->len; i++) {
+ mtype = G_VALUE_TYPE (minuend);
+ stype = G_VALUE_TYPE (subtrahend);
+
+ len = gst_value_subtract_funcs->len;
+ for (i = 0; i < len; i++) {
info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
- if (info->minuend == G_VALUE_TYPE (minuend) &&
- info->subtrahend == G_VALUE_TYPE (subtrahend)) {
+ if (info->minuend == mtype && info->subtrahend == stype) {
return info->func (dest, minuend, subtrahend);
}
}
gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
{
GstValueSubtractInfo *info;
- guint i;
+ guint i, len;
+ GType ltype, mtype, stype;
+
+ g_return_val_if_fail (G_IS_VALUE (minuend), FALSE);
+ g_return_val_if_fail (G_IS_VALUE (subtrahend), FALSE);
+
+ ltype = gst_value_list_get_type ();
/* special cases */
- if (GST_VALUE_HOLDS_LIST (minuend) || GST_VALUE_HOLDS_LIST (subtrahend))
+ if (G_VALUE_HOLDS (minuend, ltype) || G_VALUE_HOLDS (subtrahend, ltype))
return TRUE;
- for (i = 0; i < gst_value_subtract_funcs->len; i++) {
+ mtype = G_VALUE_TYPE (minuend);
+ stype = G_VALUE_TYPE (subtrahend);
+
+ len = gst_value_subtract_funcs->len;
+ for (i = 0; i < len; i++) {
info = &g_array_index (gst_value_subtract_funcs, GstValueSubtractInfo, i);
- if (info->minuend == G_VALUE_TYPE (minuend) &&
- info->subtrahend == G_VALUE_TYPE (subtrahend))
+ if (info->minuend == mtype && info->subtrahend == stype)
return TRUE;
}
* gst_value_register:
* @table: structure containing functions to register
*
- * Registers functions to perform calculations on #GValues of a given
- * type.
+ * Registers functions to perform calculations on #GValue items of a given
+ * type. Each type can only be added once.
*/
void
gst_value_register (const GstValueTable * table)
{
+ GstValueTable *found;
+
+ g_return_if_fail (table != NULL);
+
g_array_append_val (gst_value_table, *table);
+
+ found = gst_value_hash_lookup_type (table->type);
+ if (found)
+ g_warning ("adding type %s multiple times", g_type_name (table->type));
+
+ /* FIXME: we're not really doing the const justice, we assume the table is
+ * static */
+ gst_value_hash_add_type (table->type, table);
}
/**
* gst_value_init_and_copy:
- * @dest: the target value
+ * @dest: (out caller-allocates): the target value
* @src: the source value
*
* Initialises the target value to be of the same type as source and then copies
void
gst_value_init_and_copy (GValue * dest, const GValue * src)
{
+ g_return_if_fail (G_IS_VALUE (src));
+ g_return_if_fail (dest != NULL);
+
g_value_init (dest, G_VALUE_TYPE (src));
g_value_copy (src, dest);
}
* tries to transform the given @value into a string representation that allows
* getting back this string later on using gst_value_deserialize().
*
- * Returns: the serialization for @value or NULL if none exists
+ * Free-function: g_free
+ *
+ * Returns: (transfer full): the serialization for @value or NULL if none exists
*/
gchar *
gst_value_serialize (const GValue * value)
{
- guint i;
+ guint i, len;
GValue s_val = { 0 };
- GstValueTable *table, *best = NULL;
- char *s;
+ GstValueTable *table, *best;
+ gchar *s;
+ GType type;
g_return_val_if_fail (G_IS_VALUE (value), NULL);
- for (i = 0; i < gst_value_table->len; i++) {
- table = &g_array_index (gst_value_table, GstValueTable, i);
- if (table->serialize == NULL)
- continue;
- if (table->type == G_VALUE_TYPE (value)) {
- best = table;
- break;
- }
- if (g_type_is_a (G_VALUE_TYPE (value), table->type)) {
- if (!best || g_type_is_a (table->type, best->type))
- best = table;
+ type = G_VALUE_TYPE (value);
+
+ best = gst_value_hash_lookup_type (type);
+
+ if (G_UNLIKELY (!best || !best->serialize)) {
+ len = gst_value_table->len;
+ best = NULL;
+ for (i = 0; i < len; i++) {
+ table = &g_array_index (gst_value_table, GstValueTable, i);
+ if (table->serialize && g_type_is_a (type, table->type)) {
+ if (!best || g_type_is_a (table->type, best->type))
+ best = table;
+ }
}
}
- if (best)
+ if (G_LIKELY (best))
return best->serialize (value);
g_value_init (&s_val, G_TYPE_STRING);
/**
* gst_value_deserialize:
- * @dest: #GValue to fill with contents of deserialization
+ * @dest: (out caller-allocates): #GValue to fill with contents of
+ * deserialization
* @src: string to deserialize
*
* Tries to deserialize a string into the type specified by the given GValue.
gboolean
gst_value_deserialize (GValue * dest, const gchar * src)
{
- GstValueTable *table, *best = NULL;
- guint i;
+ GstValueTable *table, *best;
+ guint i, len;
+ GType type;
g_return_val_if_fail (src != NULL, FALSE);
g_return_val_if_fail (G_IS_VALUE (dest), FALSE);
- for (i = 0; i < gst_value_table->len; i++) {
- table = &g_array_index (gst_value_table, GstValueTable, i);
- if (table->serialize == NULL)
- continue;
-
- if (table->type == G_VALUE_TYPE (dest)) {
- best = table;
- break;
- }
-
- if (g_type_is_a (G_VALUE_TYPE (dest), table->type)) {
- if (!best || g_type_is_a (table->type, best->type))
- best = table;
+ type = G_VALUE_TYPE (dest);
+
+ best = gst_value_hash_lookup_type (type);
+ if (G_UNLIKELY (!best || !best->deserialize)) {
+ len = gst_value_table->len;
+ best = NULL;
+ for (i = 0; i < len; i++) {
+ table = &g_array_index (gst_value_table, GstValueTable, i);
+ if (table->deserialize && g_type_is_a (type, table->type)) {
+ if (!best || g_type_is_a (table->type, best->type))
+ best = table;
+ }
}
}
- if (best) {
+ if (G_LIKELY (best))
return best->deserialize (dest, src);
- }
return FALSE;
}
gboolean
gst_value_is_fixed (const GValue * value)
{
- GType type = G_VALUE_TYPE (value);
+ GType type;
+
+ g_return_val_if_fail (G_IS_VALUE (value), FALSE);
+
+ type = G_VALUE_TYPE (value);
+
+ /* the most common types are just basic plain glib types */
+ if (type <= G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_GLIB_LAST)) {
+ return TRUE;
+ }
if (type == GST_TYPE_ARRAY) {
- gboolean fixed = TRUE;
gint size, n;
const GValue *kid;
size = gst_value_array_get_size (value);
for (n = 0; n < size; n++) {
kid = gst_value_array_get_value (value, n);
- fixed &= gst_value_is_fixed (kid);
+ if (!gst_value_is_fixed (kid))
+ return FALSE;
}
-
- return fixed;
+ return TRUE;
}
-
return gst_type_is_fixed (type);
}
************/
/* helper functions */
-
-/* Finds the greatest common divisor.
- * Returns 1 if none other found.
- * This is Euclid's algorithm. */
-static gint
-gst_greatest_common_divisor (gint a, gint b)
-{
- while (b != 0) {
- int temp = a;
-
- a = b;
- b = temp % b;
- }
-
- return ABS (a);
-}
-
static void
gst_value_init_fraction (GValue * value)
{
gst_value_collect_fraction (GValue * value, guint n_collect_values,
GTypeCValue * collect_values, guint collect_flags)
{
+ if (n_collect_values != 2)
+ return g_strdup_printf ("not enough value locations for `%s' passed",
+ G_VALUE_TYPE_NAME (value));
+ if (collect_values[1].v_int == 0)
+ return g_strdup_printf ("passed '0' as denominator for `%s'",
+ G_VALUE_TYPE_NAME (value));
+ if (collect_values[0].v_int < -G_MAXINT)
+ return
+ g_strdup_printf
+ ("passed value smaller than -G_MAXINT as numerator for `%s'",
+ G_VALUE_TYPE_NAME (value));
+ if (collect_values[1].v_int < -G_MAXINT)
+ return
+ g_strdup_printf
+ ("passed value smaller than -G_MAXINT as denominator for `%s'",
+ G_VALUE_TYPE_NAME (value));
+
gst_value_set_fraction (value,
collect_values[0].v_int, collect_values[1].v_int);
}
/* check for reduction */
- gcd = gst_greatest_common_divisor (numerator, denominator);
+ gcd = gst_util_greatest_common_divisor (numerator, denominator);
if (gcd) {
numerator /= gcd;
denominator /= gcd;
* @factor1: a GValue initialized to #GST_TYPE_FRACTION
* @factor2: a GValue initialized to #GST_TYPE_FRACTION
*
- * Multiplies the two GValues containing a GstFraction and sets @product
- * to the product of the two fractions.
+ * Multiplies the two #GValue items containing a #GST_TYPE_FRACTION and sets
+ * @product to the product of the two fractions.
*
* Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
*/
gst_value_fraction_multiply (GValue * product, const GValue * factor1,
const GValue * factor2)
{
- gint gcd, n1, n2, d1, d2;
+ gint n1, n2, d1, d2;
+ gint res_n, res_d;
+ g_return_val_if_fail (product != NULL, FALSE);
g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor1), FALSE);
g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (factor2), FALSE);
d1 = factor1->data[1].v_int;
d2 = factor2->data[1].v_int;
- gcd = gst_greatest_common_divisor (n1, d2);
- n1 /= gcd;
- d2 /= gcd;
- gcd = gst_greatest_common_divisor (n2, d1);
- n2 /= gcd;
- d1 /= gcd;
-
- g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (n2), FALSE);
- g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
+ if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
+ return FALSE;
- gst_value_set_fraction (product, n1 * n2, d1 * d2);
+ gst_value_set_fraction (product, res_n, res_d);
return TRUE;
}
const GValue * minuend, const GValue * subtrahend)
{
gint n1, n2, d1, d2;
+ gint res_n, res_d;
+ g_return_val_if_fail (dest != NULL, FALSE);
g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
d1 = minuend->data[1].v_int;
d2 = subtrahend->data[1].v_int;
- if (n1 == 0) {
- gst_value_set_fraction (dest, -n2, d2);
- return TRUE;
- }
- if (n2 == 0) {
- gst_value_set_fraction (dest, n1, d1);
- return TRUE;
- }
-
- g_return_val_if_fail (n1 == 0 || G_MAXINT / ABS (n1) >= ABS (d2), FALSE);
- g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (n2), FALSE);
- g_return_val_if_fail (G_MAXINT / ABS (d1) >= ABS (d2), FALSE);
-
- gst_value_set_fraction (dest, (n1 * d2) - (n2 * d1), d1 * d2);
+ if (!gst_util_fraction_add (n1, d1, -n2, d2, &res_n, &res_d))
+ return FALSE;
+ gst_value_set_fraction (dest, res_n, res_d);
return TRUE;
}
gst_value_deserialize_fraction (GValue * dest, const gchar * s)
{
gint num, den;
+ gint num_chars;
if (G_UNLIKELY (s == NULL))
return FALSE;
if (G_UNLIKELY (dest == NULL || !GST_VALUE_HOLDS_FRACTION (dest)))
return FALSE;
- if (sscanf (s, "%d/%d", &num, &den) == 2) {
+ if (sscanf (s, "%d/%d%n", &num, &den, &num_chars) >= 2) {
+ if (s[num_chars] != 0)
+ return FALSE;
+ if (den == 0)
+ return FALSE;
+
gst_value_set_fraction (dest, num, den);
return TRUE;
- }
- if (sscanf (s, "%d", &num) == 1) {
+ } else if (g_ascii_strcasecmp (s, "1/max") == 0) {
+ gst_value_set_fraction (dest, 1, G_MAXINT);
+ return TRUE;
+ } else if (sscanf (s, "%d%n", &num, &num_chars) >= 1) {
+ if (s[num_chars] != 0)
+ return FALSE;
gst_value_set_fraction (dest, num, 1);
return TRUE;
- }
- if (g_ascii_strcasecmp (s, "min") == 0) {
+ } else if (g_ascii_strcasecmp (s, "min") == 0) {
gst_value_set_fraction (dest, -G_MAXINT, 1);
return TRUE;
} else if (g_ascii_strcasecmp (s, "max") == 0) {
gst_value_set_fraction (dest_value, 0, 1);
}
-#define MAX_TERMS 30
-#define MIN_DIVISOR 1.0e-10
-#define MAX_ERROR 1.0e-20
-
-/* use continued fractions to transform a double into a fraction,
- * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
- * This algorithm takes care of overflows.
- */
static void
gst_value_transform_double_fraction (const GValue * src_value,
GValue * dest_value)
{
- gdouble V, F; /* double being converted */
- gint N, D; /* will contain the result */
- gint A; /* current term in continued fraction */
- gint64 N1, D1; /* numerator, denominator of last approx */
- gint64 N2, D2; /* numerator, denominator of previous approx */
- gint i;
- gboolean negative = FALSE;
-
- /* initialize fraction being converted */
- F = src_value->data[0].v_double;
- if (F < 0.0) {
- F = -F;
- negative = TRUE;
- }
-
- V = F;
- /* initialize fractions with 1/0, 0/1 */
- N1 = 1;
- D1 = 0;
- N2 = 0;
- D2 = 1;
- N = 1;
- D = 1;
-
- for (i = 0; i < MAX_TERMS; i++) {
- /* get next term */
- A = (gint) F; /* no floor() needed, F is always >= 0 */
- /* get new divisor */
- F = F - A;
-
- /* calculate new fraction in temp */
- N2 = N1 * A + N2;
- D2 = D1 * A + D2;
-
- /* guard against overflow */
- if (N2 > G_MAXINT || D2 > G_MAXINT) {
- break;
- }
+ gdouble src = g_value_get_double (src_value);
+ gint n, d;
- N = N2;
- D = D2;
-
- /* save last two fractions */
- N2 = N1;
- D2 = D1;
- N1 = N;
- D1 = D;
-
- /* quit if dividing by zero or close enough to target */
- if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
- break;
- }
+ gst_util_double_to_fraction (src, &n, &d);
+ gst_value_set_fraction (dest_value, n, d);
+}
- /* Take reciprocal */
- F = 1 / F;
- }
- /* fix for overflow */
- if (D == 0) {
- N = G_MAXINT;
- D = 1;
- }
- /* fix for negative */
- if (negative)
- N = -N;
+static void
+gst_value_transform_float_fraction (const GValue * src_value,
+ GValue * dest_value)
+{
+ gfloat src = g_value_get_float (src_value);
+ gint n, d;
- /* will also simplify */
- gst_value_set_fraction (dest_value, N, D);
+ gst_util_double_to_fraction (src, &n, &d);
+ gst_value_set_fraction (dest_value, n, d);
}
static void
((double) src_value->data[1].v_int);
}
+static void
+gst_value_transform_fraction_float (const GValue * src_value,
+ GValue * dest_value)
+{
+ dest_value->data[0].v_float = ((float) src_value->data[0].v_int) /
+ ((float) src_value->data[1].v_int);
+}
+
static gint
gst_value_compare_fraction (const GValue * value1, const GValue * value2)
{
gint n1, n2;
gint d1, d2;
-
- gint64 new_num_1;
- gint64 new_num_2;
+ gint ret;
n1 = value1->data[0].v_int;
n2 = value2->data[0].v_int;
if (n1 == n2 && d1 == d2)
return GST_VALUE_EQUAL;
- /* extend to 64 bits */
- new_num_1 = ((gint64) n1) * d2;
- new_num_2 = ((gint64) n2) * d1;
- if (new_num_1 < new_num_2)
+ if (d1 == 0 && d2 == 0)
+ return GST_VALUE_UNORDERED;
+ else if (d1 == 0)
+ return GST_VALUE_GREATER_THAN;
+ else if (d2 == 0)
+ return GST_VALUE_LESS_THAN;
+
+ ret = gst_util_fraction_compare (n1, d1, n2, d2);
+ if (ret == -1)
return GST_VALUE_LESS_THAN;
- if (new_num_1 > new_num_2)
+ else if (ret == 1)
return GST_VALUE_GREATER_THAN;
- /* new_num_1 == new_num_2 implies that both denominators must have
- * been 0, beause otherwise simplification would have caught the
- * equivalence */
- return GST_VALUE_UNORDERED;
+ /* Equality can't happen here because we check for that
+ * first already */
+ g_return_val_if_reached (GST_VALUE_UNORDERED);
}
/*********
* @value: a GValue initialized to GST_TYPE_DATE
* @date: the date to set the value to
*
- * Sets the contents of @value to coorespond to @date. The actual
+ * Sets the contents of @value to correspond to @date. The actual
* #GDate structure is copied before it is used.
*/
void
*
* Gets the contents of @value.
*
- * Returns: the contents of @value
+ * Returns: (transfer none): the contents of @value
*/
const GDate *
gst_value_get_date (const GValue * value)
}
static gboolean
-gst_value_deserialize_date (GValue * dest, const char *s)
+gst_value_deserialize_date (GValue * dest, const gchar * s)
{
guint year, month, day;
return TRUE;
}
+/*************
+ * GstDateTime *
+ *************/
+
+static gint
+gst_value_compare_date_time (const GValue * value1, const GValue * value2)
+{
+ const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1);
+ const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2);
+ gint ret;
+
+ if (date1 == date2)
+ return GST_VALUE_EQUAL;
+
+ if ((date1 == NULL) && (date2 != NULL)) {
+ return GST_VALUE_LESS_THAN;
+ }
+ if ((date2 == NULL) && (date1 != NULL)) {
+ return GST_VALUE_LESS_THAN;
+ }
+
+ ret = priv_gst_date_time_compare (date1, date2);
+
+ if (ret == 0)
+ return GST_VALUE_EQUAL;
+ else if (ret < 0)
+ return GST_VALUE_LESS_THAN;
+ else
+ return GST_VALUE_GREATER_THAN;
+}
+
+static gchar *
+gst_value_serialize_date_time (const GValue * val)
+{
+ GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
+ gfloat offset;
+ gint tzhour, tzminute;
+
+ if (date == NULL)
+ return g_strdup ("null");
+
+ offset = gst_date_time_get_time_zone_offset (date);
+
+ tzhour = (gint) ABS (offset);
+ tzminute = (gint) ((ABS (offset) - tzhour) * 60);
+
+ return g_strdup_printf ("\"%04d-%02d-%02dT%02d:%02d:%02d.%06d"
+ "%c%02d%02d\"", gst_date_time_get_year (date),
+ gst_date_time_get_month (date), gst_date_time_get_day (date),
+ gst_date_time_get_hour (date), gst_date_time_get_minute (date),
+ gst_date_time_get_second (date), gst_date_time_get_microsecond (date),
+ offset >= 0 ? '+' : '-', tzhour, tzminute);
+}
+
+static gboolean
+gst_value_deserialize_date_time (GValue * dest, const gchar * s)
+{
+ gint year, month, day, hour, minute, second, usecond;
+ gchar signal;
+ gint offset = 0;
+ gfloat tzoffset = 0;
+ gint ret;
+
+ if (!s || strcmp (s, "null") == 0) {
+ return FALSE;
+ }
+
+ ret = sscanf (s, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%04d",
+ &year, &month, &day, &hour, &minute, &second, &usecond, &signal, &offset);
+ if (ret >= 9) {
+ tzoffset = (offset / 100) + ((offset % 100) / 60.0);
+ if (signal == '-')
+ tzoffset = -tzoffset;
+ } else
+ return FALSE;
+
+ g_value_take_boxed (dest, gst_date_time_new (tzoffset, year, month, day, hour,
+ minute, second + (usecond / 1000000.0)));
+ return TRUE;
+}
+
static void
gst_value_transform_date_string (const GValue * src_value, GValue * dest_value)
{
gchar *str;
obj = g_value_get_object (src_value);
- str = obj ? GST_OBJECT_NAME (obj) : "NULL";
+ if (obj) {
+ str =
+ g_strdup_printf ("(%s) %s", G_OBJECT_TYPE_NAME (obj),
+ GST_OBJECT_NAME (obj));
+ } else {
+ str = g_strdup ("NULL");
+ }
- dest_value->data[0].v_pointer = g_strdup (str);
+ dest_value->data[0].v_pointer = str;
}
static GTypeInfo _info = {
#define FUNC_VALUE_GET_TYPE(type, name) \
GType gst_ ## type ## _get_type (void) \
{ \
- static GType gst_ ## type ## _type = 0; \
+ static volatile GType gst_ ## type ## _type = 0; \
\
- if (G_UNLIKELY (gst_ ## type ## _type == 0)) { \
+ if (g_once_init_enter (&gst_ ## type ## _type)) { \
+ GType _type; \
_info.value_table = & _gst_ ## type ## _value_table; \
- gst_ ## type ## _type = g_type_register_fundamental ( \
+ _type = g_type_register_fundamental ( \
g_type_fundamental_next (), \
name, &_info, &_finfo, 0); \
+ g_once_init_leave(&gst_ ## type ## _type, _type); \
} \
\
return gst_ ## type ## _type; \
NULL,
gst_value_copy_fourcc,
NULL,
- "i",
+ (char *) "i",
gst_value_collect_fourcc,
- "p",
+ (char *) "p",
gst_value_lcopy_fourcc
};
NULL,
gst_value_copy_int_range,
NULL,
- "ii",
+ (char *) "ii",
gst_value_collect_int_range,
- "pp",
+ (char *) "pp",
gst_value_lcopy_int_range
};
FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
+static const GTypeValueTable _gst_int64_range_value_table = {
+ gst_value_init_int64_range,
+ NULL,
+ gst_value_copy_int64_range,
+ NULL,
+ (char *) "qq",
+ gst_value_collect_int64_range,
+ (char *) "pp",
+ gst_value_lcopy_int64_range
+};
+
+FUNC_VALUE_GET_TYPE (int64_range, "GstInt64Range");
+
static const GTypeValueTable _gst_double_range_value_table = {
gst_value_init_double_range,
NULL,
gst_value_copy_double_range,
NULL,
- "dd",
+ (char *) "dd",
gst_value_collect_double_range,
- "pp",
+ (char *) "pp",
gst_value_lcopy_double_range
};
gst_value_free_fraction_range,
gst_value_copy_fraction_range,
NULL,
- "iiii",
+ (char *) "iiii",
gst_value_collect_fraction_range,
- "pppp",
+ (char *) "pppp",
gst_value_lcopy_fraction_range
};
gst_value_free_list_or_array,
gst_value_copy_list_or_array,
gst_value_list_or_array_peek_pointer,
- "p",
+ (char *) "p",
gst_value_collect_list_or_array,
- "p",
+ (char *) "p",
gst_value_lcopy_list_or_array
};
gst_value_free_list_or_array,
gst_value_copy_list_or_array,
gst_value_list_or_array_peek_pointer,
- "p",
+ (char *) "p",
gst_value_collect_list_or_array,
- "p",
+ (char *) "p",
gst_value_lcopy_list_or_array
};
NULL,
gst_value_copy_fraction,
NULL,
- "ii",
+ (char *) "ii",
gst_value_collect_fraction,
- "pp",
+ (char *) "pp",
gst_value_lcopy_fraction
};
return gst_date_type;
}
+GType
+gst_date_time_get_type (void)
+{
+ static GType gst_date_time_type = 0;
+
+ if (G_UNLIKELY (gst_date_time_type == 0)) {
+ gst_date_time_type = g_boxed_type_register_static ("GstDateTime",
+ (GBoxedCopyFunc) gst_date_time_ref,
+ (GBoxedFreeFunc) gst_date_time_unref);
+ }
+
+ return gst_date_time_type;
+}
+
+
void
_gst_value_initialize (void)
{
gst_value_table = g_array_new (FALSE, FALSE, sizeof (GstValueTable));
+ gst_value_hash = g_hash_table_new (NULL, NULL);
gst_value_union_funcs = g_array_new (FALSE, FALSE,
sizeof (GstValueUnionInfo));
gst_value_intersect_funcs = g_array_new (FALSE, FALSE,
{
static GstValueTable gst_value = {
0,
+ gst_value_compare_int64_range,
+ gst_value_serialize_int64_range,
+ gst_value_deserialize_int64_range,
+ };
+
+ gst_value.type = gst_int64_range_get_type ();
+ gst_value_register (&gst_value);
+ }
+
+ {
+ static GstValueTable gst_value = {
+ 0,
gst_value_compare_double_range,
gst_value_serialize_double_range,
gst_value_deserialize_double_range,
gst_value_deserialize_array,
};
- gst_value.type = gst_value_array_get_type ();;
+ gst_value.type = gst_value_array_get_type ();
gst_value_register (&gst_value);
}
gst_value.type = gst_date_get_type ();
gst_value_register (&gst_value);
}
+ {
+ static GstValueTable gst_value = {
+ 0,
+ gst_value_compare_date_time,
+ gst_value_serialize_date_time,
+ gst_value_deserialize_date_time,
+ };
+
+ gst_value.type = gst_date_time_get_type ();
+ gst_value_register (&gst_value);
+ }
REGISTER_SERIALIZATION (G_TYPE_DOUBLE, double);
REGISTER_SERIALIZATION (G_TYPE_FLOAT, float);
REGISTER_SERIALIZATION (G_TYPE_UINT64, uint64);
REGISTER_SERIALIZATION (G_TYPE_ULONG, ulong);
+ REGISTER_SERIALIZATION (G_TYPE_UCHAR, uchar);
+
g_value_register_transform_func (GST_TYPE_FOURCC, G_TYPE_STRING,
gst_value_transform_fourcc_string);
g_value_register_transform_func (GST_TYPE_INT_RANGE, G_TYPE_STRING,
gst_value_transform_int_range_string);
+ g_value_register_transform_func (GST_TYPE_INT64_RANGE, G_TYPE_STRING,
+ gst_value_transform_int64_range_string);
g_value_register_transform_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_STRING,
gst_value_transform_double_range_string);
g_value_register_transform_func (GST_TYPE_FRACTION_RANGE, G_TYPE_STRING,
gst_value_transform_string_fraction);
g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_DOUBLE,
gst_value_transform_fraction_double);
+ g_value_register_transform_func (GST_TYPE_FRACTION, G_TYPE_FLOAT,
+ gst_value_transform_fraction_float);
g_value_register_transform_func (G_TYPE_DOUBLE, GST_TYPE_FRACTION,
gst_value_transform_double_fraction);
+ g_value_register_transform_func (G_TYPE_FLOAT, GST_TYPE_FRACTION,
+ gst_value_transform_float_fraction);
g_value_register_transform_func (GST_TYPE_DATE, G_TYPE_STRING,
gst_value_transform_date_string);
g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_DATE,
gst_value_intersect_int_int_range);
gst_value_register_intersect_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
gst_value_intersect_int_range_int_range);
+ gst_value_register_intersect_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
+ gst_value_intersect_int64_int64_range);
+ gst_value_register_intersect_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
+ gst_value_intersect_int64_range_int64_range);
gst_value_register_intersect_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
gst_value_intersect_double_double_range);
gst_value_register_intersect_func (GST_TYPE_DOUBLE_RANGE,
gst_value_subtract_int_range_int);
gst_value_register_subtract_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
gst_value_subtract_int_range_int_range);
+ gst_value_register_subtract_func (G_TYPE_INT64, GST_TYPE_INT64_RANGE,
+ gst_value_subtract_int64_int64_range);
+ gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, G_TYPE_INT64,
+ gst_value_subtract_int64_range_int64);
+ gst_value_register_subtract_func (GST_TYPE_INT64_RANGE, GST_TYPE_INT64_RANGE,
+ gst_value_subtract_int64_range_int64_range);
gst_value_register_subtract_func (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE,
gst_value_subtract_double_double_range);
gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE, G_TYPE_DOUBLE,