/**
* SECTION:gstvalue
- * @short_description: GValue implementations specific to GStreamer
+ * @short_description: GValue 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.
+ *
+ * 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 "gst_private.h"
-#include "glib-compat.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;
};
-GType gst_type_double_range;
-GType gst_type_list;
-GType gst_type_array;
-GType gst_type_fraction;
-GType gst_type_date;
+#define FUNDAMENTAL_TYPE_ID_MAX \
+ (G_TYPE_FUNDAMENTAL_MAX >> G_TYPE_FUNDAMENTAL_SHIFT)
+#define FUNDAMENTAL_TYPE_ID(type) \
+ ((type) >> G_TYPE_FUNDAMENTAL_SHIFT)
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 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 *
********/
/* two helper functions to serialize/stringify any type of list
* regular lists are done with { }, arrays with < >
*/
-static char *
-gst_value_serialize_any_list (const GValue * value, const char *begin,
- const char *end)
+static gchar *
+gst_value_serialize_any_list (const GValue * value, const gchar * begin,
+ const gchar * end)
{
guint i;
GArray *array = value->data[0].v_pointer;
GString *s;
GValue *v;
gchar *s_val;
+ guint alen = array->len;
- s = g_string_new (begin);
- for (i = 0; i < array->len; i++) {
+ /* estimate minimum string length to minimise re-allocs in GString */
+ s = g_string_sized_new (2 + (6 * alen) + 2);
+ g_string_append (s, begin);
+ 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) {
- g_string_append (s, ", ");
+ if (i < alen - 1) {
+ g_string_append_len (s, ", ", 2);
}
}
g_string_append (s, end);
static void
gst_value_transform_any_list_string (const GValue * src_value,
- GValue * dest_value, const char *begin, const char *end)
+ GValue * dest_value, const gchar * begin, const gchar * end)
{
GValue *list_value;
GArray *array;
GString *s;
guint i;
- char *list_s;
+ gchar *list_s;
+ guint alen;
array = src_value->data[0].v_pointer;
+ alen = array->len;
- s = g_string_new (begin);
- for (i = 0; i < array->len; i++) {
+ /* estimate minimum string length to minimise re-allocs in GString */
+ s = g_string_sized_new (2 + (10 * alen) + 2);
+ g_string_append (s, begin);
+ for (i = 0; i < alen; i++) {
list_value = &g_array_index (array, GValue, i);
if (i != 0) {
- g_string_append (s, ", ");
+ g_string_append_len (s, ", ", 2);
}
list_s = g_strdup_value_contents (list_value);
g_string_append (s, list_s);
* 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_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;
}
/* GValue functions usable for both regular lists and arrays */
static void
-gst_value_init_list (GValue * value)
+gst_value_init_list_or_array (GValue * value)
{
value->data[0].v_pointer = g_array_new (FALSE, TRUE, sizeof (GValue));
}
static GArray *
-gst_value_list_array_copy (const GArray * src)
+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_copy_list (const GValue * src_value, GValue * dest_value)
+gst_value_copy_list_or_array (const GValue * src_value, GValue * dest_value)
{
dest_value->data[0].v_pointer =
- gst_value_list_array_copy ((GArray *) src_value->data[0].v_pointer);
+ copy_garray_of_gstvalue ((GArray *) src_value->data[0].v_pointer);
}
static void
-gst_value_free_list (GValue * value)
+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);
}
static gpointer
-gst_value_list_peek_pointer (const GValue * value)
+gst_value_list_or_array_peek_pointer (const GValue * value)
{
return value->data[0].v_pointer;
}
static gchar *
-gst_value_collect_list (GValue * value, guint n_collect_values,
+gst_value_collect_list_or_array (GValue * value, guint n_collect_values,
GTypeCValue * collect_values, guint collect_flags)
{
if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
} else {
value->data[0].v_pointer =
- gst_value_list_array_copy ((GArray *) collect_values[0].v_pointer);
+ copy_garray_of_gstvalue ((GArray *) collect_values[0].v_pointer);
}
return NULL;
}
static gchar *
-gst_value_lcopy_list (const GValue * value, guint n_collect_values,
+gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
GTypeCValue * collect_values, guint collect_flags)
{
GArray **dest = collect_values[0].v_pointer;
if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
*dest = (GArray *) value->data[0].v_pointer;
} else {
- *dest = gst_value_list_array_copy ((GArray *) value->data[0].v_pointer);
+ *dest = copy_garray_of_gstvalue ((GArray *) value->data[0].v_pointer);
}
return NULL;
}
/**
- * gst_value_list_prepend_value:
- * @value: a GstValueList to prepend a value to
- * @prepend_value: the value to prepend
- *
- * Prepends @prepend_value to the GstValueList in @value.
- *
- */
-void
-gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
-{
- GValue val = { 0, };
-
- g_return_if_fail (GST_VALUE_HOLDS_LIST (value)
- || GST_VALUE_HOLDS_ARRAY (value));
-
- gst_value_init_and_copy (&val, prepend_value);
- g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
-}
-
-/**
* gst_value_list_append_value:
- * @value: a GstValueList to append a value to
+ * @value: a #GValue of type #GST_TYPE_LIST
* @append_value: the value to append
*
* Appends @append_value to the GstValueList in @value.
{
GValue val = { 0, };
- g_return_if_fail (GST_VALUE_HOLDS_LIST (value)
- || GST_VALUE_HOLDS_ARRAY (value));
+ g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
gst_value_init_and_copy (&val, append_value);
g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
}
/**
- * gst_value_list_get_size:
- * @value: a #GValue of type #GST_LIST_TYPE or #GST_ARRAY_TYPE
- *
- * Gets the number of values contained in @value.
+ * gst_value_list_prepend_value:
+ * @value: a #GValue of type #GST_TYPE_LIST
+ * @prepend_value: the value to prepend
*
- * Returns: the number of values
+ * Prepends @prepend_value to the GstValueList in @value.
*/
-guint
-gst_value_list_get_size (const GValue * value)
+void
+gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
{
- g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value)
- || GST_VALUE_HOLDS_ARRAY (value), 0);
-
- return ((GArray *) value->data[0].v_pointer)->len;
-}
+ GValue val = { 0, };
-/**
- * gst_value_list_get_value:
- * @value: a #GValue of type #GST_LIST_TYPE or #GST_ARRAY_TYPE
- * @index: index of value to get from the 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
- */
-const GValue *
-gst_value_list_get_value (const GValue * value, guint index)
-{
- g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value)
- || GST_VALUE_HOLDS_ARRAY (value), NULL);
- g_return_val_if_fail (index < gst_value_list_get_size (value), NULL);
+ g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
- return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
- GValue, index);
+ 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
- * @value1: first value to put into the union
- * @value2: second value to put into the union
+ * @value1: a #GValue
+ * @value2: a #GValue
*
- * Concatenates copies of value1 and value2 into a list. The value
- * @dest is initialized to the type GST_TYPE_LIST.
+ * Concatenates copies of @value1 and @value2 into a list. Values that are not
+ * of type #GST_TYPE_LIST are treated as if they were lists of length 1.
+ * @dest will be initialized to the type #GST_TYPE_LIST.
*/
void
gst_value_list_concat (GValue * dest, const GValue * value1,
}
}
+/**
+ * gst_value_list_get_size:
+ * @value: a #GValue of type #GST_TYPE_LIST
+ *
+ * Gets the number of values contained in @value.
+ *
+ * Returns: the number of values
+ */
+guint
+gst_value_list_get_size (const GValue * value)
+{
+ g_return_val_if_fail (GST_VALUE_HOLDS_LIST (value), 0);
+
+ return ((GArray *) value->data[0].v_pointer)->len;
+}
+
+/**
+ * gst_value_list_get_value:
+ * @value: a #GValue of type #GST_TYPE_LIST
+ * @index: index of value to get from the 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
+ */
+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);
+
+ return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
+ GValue, index);
+}
+
+/**
+ * gst_value_array_append_value:
+ * @value: a #GValue of type #GST_TYPE_ARRAY
+ * @append_value: the value to append
+ *
+ * Appends @append_value to the GstValueArray in @value.
+ */
+void
+gst_value_array_append_value (GValue * value, const GValue * append_value)
+{
+ GValue val = { 0, };
+
+ g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
+
+ gst_value_init_and_copy (&val, append_value);
+ g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
+}
+
+/**
+ * gst_value_array_prepend_value:
+ * @value: a #GValue of type #GST_TYPE_ARRAY
+ * @prepend_value: the value to prepend
+ *
+ * Prepends @prepend_value to the GstValueArray in @value.
+ */
+void
+gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
+{
+ GValue val = { 0, };
+
+ g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
+
+ gst_value_init_and_copy (&val, prepend_value);
+ g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
+}
+
+/**
+ * gst_value_array_get_size:
+ * @value: a #GValue of type #GST_TYPE_ARRAY
+ *
+ * Gets the number of values contained in @value.
+ *
+ * Returns: the number of values
+ */
+guint
+gst_value_array_get_size (const GValue * value)
+{
+ g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), 0);
+
+ return ((GArray *) value->data[0].v_pointer)->len;
+}
+
+/**
+ * gst_value_array_get_value:
+ * @value: a #GValue of type #GST_TYPE_ARRAY
+ * @index: index of value to get from the array
+ *
+ * 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
+ */
+const GValue *
+gst_value_array_get_value (const GValue * value, guint index)
+{
+ g_return_val_if_fail (GST_VALUE_HOLDS_ARRAY (value), NULL);
+ g_return_val_if_fail (index < gst_value_array_get_size (value), NULL);
+
+ return (const GValue *) &g_array_index ((GArray *) value->data[0].v_pointer,
+ GValue, index);
+}
+
static void
gst_value_transform_list_string (const GValue * src_value, GValue * dest_value)
{
gst_value_transform_any_list_string (src_value, dest_value, "< ", " >");
}
+/* Do an unordered compare of the contents of a list */
static int
gst_value_compare_list (const GValue * value1, const GValue * value2)
{
GArray *array2 = value2->data[0].v_pointer;
GValue *v1;
GValue *v2;
+ gint len, to_remove;
+ guint8 *removed;
+ GstValueCompareFunc compare;
- if (array1->len != array2->len)
+ /* get length and do initial length check. */
+ len = array1->len;
+ if (len != array2->len)
return GST_VALUE_UNORDERED;
- for (i = 0; i < array1->len; i++) {
+ /* place to mark removed value indices of array2 */
+ removed = g_newa (guint8, len);
+ memset (removed, 0, len);
+ to_remove = len;
+
+ /* loop over array1, all items should be in array2. When we find an
+ * item in array2, remove it from array2 by marking it as removed */
+ for (i = 0; i < len; i++) {
v1 = &g_array_index (array1, GValue, i);
- for (j = 0; j < array1->len; j++) {
- v2 = &g_array_index (array2, GValue, j);
- if (gst_value_compare (v1, v2) == GST_VALUE_EQUAL)
- break;
- }
- if (j == array1->len) {
+ if ((compare = gst_value_get_compare_func (v1))) {
+ for (j = 0; j < len; j++) {
+ /* item is removed, we can skip it */
+ if (removed[j])
+ continue;
+ v2 = &g_array_index (array2, GValue, j);
+ if (gst_value_compare_with_func (v1, v2, compare) == GST_VALUE_EQUAL) {
+ /* mark item as removed now that we found it in array2 and
+ * decrement the number of remaining items in array2. */
+ removed[j] = 1;
+ to_remove--;
+ break;
+ }
+ }
+ /* item in array1 and not in array2, UNORDERED */
+ if (j == len)
+ return GST_VALUE_UNORDERED;
+ } else
return GST_VALUE_UNORDERED;
- }
}
+ /* if not all items were removed, array2 contained something not in array1 */
+ if (to_remove != 0)
+ return GST_VALUE_UNORDERED;
+ /* arrays are equal */
return GST_VALUE_EQUAL;
}
-static char *
+/* Perform an ordered comparison of the contents of an array */
+static int
+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 (len != array2->len)
+ return GST_VALUE_UNORDERED;
+
+ 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)
+ return GST_VALUE_UNORDERED;
+ }
+
+ return GST_VALUE_EQUAL;
+}
+
+static gchar *
gst_value_serialize_list (const GValue * value)
{
return gst_value_serialize_any_list (value, "{ ", " }");
}
static gboolean
-gst_value_deserialize_list (GValue * dest, const char *s)
+gst_value_deserialize_list (GValue * dest, const gchar * s)
{
- g_warning ("unimplemented");
+ g_warning ("gst_value_deserialize_list: unimplemented");
return FALSE;
}
-static char *
+static gchar *
gst_value_serialize_array (const GValue * value)
{
return gst_value_serialize_any_list (value, "< ", " >");
}
static gboolean
-gst_value_deserialize_array (GValue * dest, const char *s)
+gst_value_deserialize_array (GValue * dest, const gchar * s)
{
- g_warning ("unimplemented");
+ g_warning ("gst_value_deserialize_array: unimplemented");
return FALSE;
}
g_ascii_isprint ((fourcc >> 16) & 0xff) &&
g_ascii_isprint ((fourcc >> 24) & 0xff)) {
dest_value->data[0].v_pointer =
- g_strdup_printf (GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
+ g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
} else {
dest_value->data[0].v_pointer = g_strdup_printf ("0x%08x", fourcc);
}
}
-static int
+static gint
gst_value_compare_fourcc (const GValue * value1, const GValue * value2)
{
if (value2->data[0].v_int == value1->data[0].v_int)
return GST_VALUE_UNORDERED;
}
-static char *
+static gchar *
gst_value_serialize_fourcc (const GValue * value)
{
guint32 fourcc = value->data[0].v_int;
g_ascii_isalnum ((fourcc >> 8) & 0xff) &&
g_ascii_isalnum ((fourcc >> 16) & 0xff) &&
g_ascii_isalnum ((fourcc >> 24) & 0xff)) {
- return g_strdup_printf (GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
+ return g_strdup_printf ("%" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (fourcc));
} else {
return g_strdup_printf ("0x%08x", fourcc);
}
* Sets @value to the range specified by @start and @end.
*/
void
-gst_value_set_int_range (GValue * value, int start, int end)
+gst_value_set_int_range (GValue * value, gint start, gint end)
{
g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
g_return_if_fail (start < end);
*
* Returns: the minimum of the range
*/
-int
+gint
gst_value_get_int_range_min (const GValue * value)
{
g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
*
* Returns: the maxumum of the range
*/
-int
+gint
gst_value_get_int_range_max (const GValue * value)
{
g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
(int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
}
-static int
+static gint
gst_value_compare_int_range (const GValue * value1, const GValue * value2)
{
if (value2->data[0].v_int == value1->data[0].v_int &&
return GST_VALUE_UNORDERED;
}
-static char *
+static gchar *
gst_value_serialize_int_range (const GValue * value)
{
return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
}
static gboolean
-gst_value_deserialize_int_range (GValue * dest, const char *s)
+gst_value_deserialize_int_range (GValue * dest, const gchar * s)
{
g_warning ("unimplemented");
return FALSE;
* Sets @value to the range specified by @start and @end.
*/
void
-gst_value_set_double_range (GValue * value, double start, double end)
+gst_value_set_double_range (GValue * value, gdouble start, gdouble end)
{
g_return_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value));
*
* Gets the minimum of the range specified by @value.
*
- * Returns: the minumum of the range
+ * Returns: the minimum of the range
*/
-double
+gdouble
gst_value_get_double_range_min (const GValue * value)
{
g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
*
* Returns: the maxumum of the range
*/
-double
+gdouble
gst_value_get_double_range_max (const GValue * value)
{
g_return_val_if_fail (GST_VALUE_HOLDS_DOUBLE_RANGE (value), 0);
- return value->data[1].v_double;
+ return value->data[1].v_double;
+}
+
+static void
+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];
+
+ dest_value->data[0].v_pointer = g_strdup_printf ("[%s,%s]",
+ g_ascii_dtostr (s1, G_ASCII_DTOSTR_BUF_SIZE,
+ src_value->data[0].v_double),
+ g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
+ src_value->data[1].v_double));
+}
+
+static gint
+gst_value_compare_double_range (const GValue * value1, const GValue * value2)
+{
+ if (value2->data[0].v_double == value1->data[0].v_double &&
+ value2->data[0].v_double == value1->data[0].v_double)
+ return GST_VALUE_EQUAL;
+ return GST_VALUE_UNORDERED;
+}
+
+static gchar *
+gst_value_serialize_double_range (const GValue * value)
+{
+ char d1[G_ASCII_DTOSTR_BUF_SIZE];
+ char 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);
+ return g_strdup_printf ("[ %s, %s ]", d1, d2);
+}
+
+static gboolean
+gst_value_deserialize_double_range (GValue * dest, const gchar * s)
+{
+ g_warning ("unimplemented");
+ return FALSE;
+}
+
+/****************
+ * fraction range *
+ ****************/
+
+static void
+gst_value_init_fraction_range (GValue * value)
+{
+ GValue *vals;
+ GType ftype;
+
+ ftype = 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
+gst_value_free_fraction_range (GValue * value)
+{
+ GValue *vals = (GValue *) value->data[0].v_pointer;
+
+ if (vals != NULL) {
+ g_value_unset (&vals[0]);
+ g_value_unset (&vals[1]);
+ g_slice_free1 (2 * sizeof (GValue), vals);
+ value->data[0].v_pointer = NULL;
+ }
+}
+
+static void
+gst_value_copy_fraction_range (const GValue * src_value, GValue * dest_value)
+{
+ GValue *vals = (GValue *) dest_value->data[0].v_pointer;
+ GValue *src_vals = (GValue *) src_value->data[0].v_pointer;
+
+ if (vals == NULL) {
+ 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]);
+ }
+}
+
+static gchar *
+gst_value_collect_fraction_range (GValue * value, guint n_collect_values,
+ GTypeCValue * collect_values, guint collect_flags)
+{
+ GValue *vals = (GValue *) value->data[0].v_pointer;
+
+ if (n_collect_values != 4)
+ return g_strdup_printf ("not enough value locations for `%s' passed",
+ G_VALUE_TYPE_NAME (value));
+ if (vals == NULL) {
+ gst_value_init_fraction_range (value);
+ vals = value->data[0].v_pointer;
+ }
+
+ gst_value_set_fraction (&vals[0], collect_values[0].v_int,
+ collect_values[1].v_int);
+ gst_value_set_fraction (&vals[1], collect_values[2].v_int,
+ collect_values[3].v_int);
+
+ return NULL;
+}
+
+static gchar *
+gst_value_lcopy_fraction_range (const GValue * value, guint n_collect_values,
+ GTypeCValue * collect_values, guint collect_flags)
+{
+ int i;
+ int *dest_values[4];
+ GValue *vals = (GValue *) value->data[0].v_pointer;
+
+ 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 (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 (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_numerator (&vals[1]);
+ dest_values[3][0] = gst_value_get_fraction_denominator (&vals[1]);
+ return NULL;
+}
+
+/**
+ * gst_value_set_fraction_range:
+ * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
+ * @start: the start of the range (a GST_TYPE_FRACTION GValue)
+ * @end: the end of the range (a GST_TYPE_FRACTION GValue)
+ *
+ * Sets @value to the range specified by @start and @end.
+ */
+void
+gst_value_set_fraction_range (GValue * value, const GValue * start,
+ const GValue * end)
+{
+ GValue *vals;
+
+ g_return_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value));
+
+ vals = (GValue *) value->data[0].v_pointer;
+ if (vals == NULL) {
+ gst_value_init_fraction_range (value);
+ vals = value->data[0].v_pointer;
+ }
+ g_value_copy (start, &vals[0]);
+ g_value_copy (end, &vals[1]);
+}
+
+/**
+ * gst_value_set_fraction_range_full:
+ * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
+ * @numerator_start: the numerator start of the range
+ * @denominator_start: the denominator start of the range
+ * @numerator_end: the numerator end of the range
+ * @denominator_end: the denominator end of the range
+ *
+ * Sets @value to the range specified by @numerator_start/@denominator_start
+ * and @numerator_end/@denominator_end.
+ */
+void
+gst_value_set_fraction_range_full (GValue * value,
+ gint numerator_start, gint denominator_start,
+ gint numerator_end, gint denominator_end)
+{
+ GValue start = { 0 };
+ GValue end = { 0 };
+
+ g_value_init (&start, GST_TYPE_FRACTION);
+ g_value_init (&end, GST_TYPE_FRACTION);
+
+ gst_value_set_fraction (&start, numerator_start, denominator_start);
+ gst_value_set_fraction (&end, numerator_end, denominator_end);
+ gst_value_set_fraction_range (value, &start, &end);
+
+ g_value_unset (&start);
+ g_value_unset (&end);
+}
+
+/**
+ * gst_value_get_fraction_range_min:
+ * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
+ *
+ * Gets the minimum of the range specified by @value.
+ *
+ * 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);
+
+ vals = (GValue *) value->data[0].v_pointer;
+ if (vals != NULL) {
+ return &vals[0];
+ }
+
+ return NULL;
+}
+
+/**
+ * gst_value_get_fraction_range_max:
+ * @value: a GValue initialized to GST_TYPE_FRACTION_RANGE
+ *
+ * Gets the maximum of the range specified by @value.
+ *
+ * Returns: the maximum of the range
+ */
+const GValue *
+gst_value_get_fraction_range_max (const GValue * value)
+{
+ GValue *vals;
+
+ g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION_RANGE (value), FALSE);
+
+ vals = (GValue *) value->data[0].v_pointer;
+ if (vals != NULL) {
+ return &vals[1];
+ }
+
+ return NULL;
+}
+
+static char *
+gst_value_serialize_fraction_range (const GValue * value)
+{
+ GValue *vals = (GValue *) value->data[0].v_pointer;
+ gchar *retval;
+
+ if (vals == NULL) {
+ retval = g_strdup ("[ 0/1, 0/1 ]");
+ } else {
+ gchar *start, *end;
+
+ start = gst_value_serialize_fraction (&vals[0]);
+ end = gst_value_serialize_fraction (&vals[1]);
+
+ retval = g_strdup_printf ("[ %s, %s ]", start, end);
+ g_free (start);
+ g_free (end);
+ }
+
+ return retval;
}
static void
-gst_value_transform_double_range_string (const GValue * src_value,
+gst_value_transform_fraction_range_string (const GValue * src_value,
GValue * dest_value)
{
- char 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,
- src_value->data[0].v_double),
- g_ascii_dtostr (s2, G_ASCII_DTOSTR_BUF_SIZE,
- src_value->data[1].v_double));
+ dest_value->data[0].v_pointer =
+ gst_value_serialize_fraction_range (src_value);
}
-static int
-gst_value_compare_double_range (const GValue * value1, const GValue * value2)
+static gint
+gst_value_compare_fraction_range (const GValue * value1, const GValue * value2)
{
- if (value2->data[0].v_double == value1->data[0].v_double &&
- value2->data[0].v_double == value1->data[0].v_double)
- return GST_VALUE_EQUAL;
- return GST_VALUE_UNORDERED;
-}
+ GValue *vals1, *vals2;
+ GstValueCompareFunc compare;
-static char *
-gst_value_serialize_double_range (const GValue * value)
-{
- char d1[G_ASCII_DTOSTR_BUF_SIZE];
- char d2[G_ASCII_DTOSTR_BUF_SIZE];
+ if (value2->data[0].v_pointer == value1->data[0].v_pointer)
+ return GST_VALUE_EQUAL; /* Only possible if both are NULL */
- 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);
- return g_strdup_printf ("[ %s, %s ]", d1, d2);
+ if (value2->data[0].v_pointer == NULL || value1->data[0].v_pointer == NULL)
+ return GST_VALUE_UNORDERED;
+
+ vals1 = (GValue *) value1->data[0].v_pointer;
+ vals2 = (GValue *) value2->data[0].v_pointer;
+ if ((compare = gst_value_get_compare_func (&vals1[0]))) {
+ if (gst_value_compare_with_func (&vals1[0], &vals2[0], compare) ==
+ GST_VALUE_EQUAL &&
+ gst_value_compare_with_func (&vals1[1], &vals2[1], compare) ==
+ GST_VALUE_EQUAL)
+ return GST_VALUE_EQUAL;
+ }
+ return GST_VALUE_UNORDERED;
}
static gboolean
-gst_value_deserialize_double_range (GValue * dest, const char *s)
+gst_value_deserialize_fraction_range (GValue * dest, const gchar * s)
{
g_warning ("unimplemented");
return FALSE;
* @value: a GValue initialized to GST_TYPE_CAPS
* @caps: the caps to set the value to
*
- * Sets the contents of @value to coorespond to @caps. The actual
+ * Sets the contents of @value to @caps. The actual
* #GstCaps structure is copied before it is used.
*/
void
}
static gboolean
-gst_value_deserialize_caps (GValue * dest, const char *s)
+gst_value_deserialize_caps (GValue * dest, const gchar * s)
{
GstCaps *caps;
caps = gst_caps_from_string (s);
if (caps) {
- g_value_set_boxed (dest, caps);
+ g_value_take_boxed (dest, caps);
return TRUE;
}
return FALSE;
}
+/****************
+ * GstStructure *
+ ****************/
+
+/**
+ * gst_value_set_structure:
+ * @value: a GValue initialized to GST_TYPE_STRUCTURE
+ * @structure: the structure to set the value to
+ *
+ * Sets the contents of @value to @structure. The actual
+ *
+ * Since: 0.10.15
+ */
+void
+gst_value_set_structure (GValue * value, const GstStructure * structure)
+{
+ g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE);
+
+ g_value_set_boxed (value, structure);
+}
+
+/**
+ * gst_value_get_structure:
+ * @value: a GValue initialized to GST_TYPE_STRUCTURE
+ *
+ * Gets the contents of @value.
+ *
+ * Returns: the contents of @value
+ *
+ * Since: 0.10.15
+ */
+const GstStructure *
+gst_value_get_structure (const GValue * value)
+{
+ g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, NULL);
+
+ return (GstStructure *) g_value_get_boxed (value);
+}
+
+static char *
+gst_value_serialize_structure (const GValue * value)
+{
+ GstStructure *structure = g_value_get_boxed (value);
+
+ return gst_string_take_and_wrap (gst_structure_to_string (structure));
+}
+
+static gboolean
+gst_value_deserialize_structure (GValue * dest, const gchar * s)
+{
+ GstStructure *structure;
+
+ 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 (G_LIKELY (structure)) {
+ g_value_take_boxed (dest, structure);
+ return TRUE;
+ }
+ return FALSE;
+}
/*************
* GstBuffer *
int i;
int size;
char *string;
- GstBuffer *buffer = GST_BUFFER (gst_value_get_mini_object (value));
+ GstBuffer *buffer;
+
+ buffer = gst_value_get_buffer (value);
+ if (buffer == NULL)
+ return NULL;
data = GST_BUFFER_DATA (buffer);
size = GST_BUFFER_SIZE (buffer);
}
static gboolean
-gst_value_deserialize_buffer (GValue * dest, const char *s)
+gst_value_deserialize_buffer (GValue * dest, const gchar * s)
{
GstBuffer *buffer;
- gboolean ret = TRUE;
int len;
char ts[3];
guint8 *data;
len = strlen (s);
if (len & 1)
- return FALSE;
+ goto wrong_length;
+
buffer = gst_buffer_new_and_alloc (len / 2);
data = GST_BUFFER_DATA (buffer);
for (i = 0; i < len / 2; i++) {
- if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1])) {
- ret = FALSE;
- break;
- }
+ if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
+ goto wrong_char;
+
ts[0] = s[i * 2 + 0];
ts[1] = s[i * 2 + 1];
ts[2] = 0;
- data[i] = strtoul (ts, NULL, 16);
+ data[i] = (guint8) strtoul (ts, NULL, 16);
}
- if (ret) {
- gst_value_take_mini_object (dest, GST_MINI_OBJECT (buffer));
- return TRUE;
- } else {
+ gst_value_take_buffer (dest, buffer);
+
+ return TRUE;
+
+ /* ERRORS */
+wrong_length:
+ {
+ return FALSE;
+ }
+wrong_char:
+ {
gst_buffer_unref (buffer);
return FALSE;
}
}
static gboolean
-gst_value_deserialize_boolean (GValue * dest, const char *s)
+gst_value_deserialize_boolean (GValue * dest, const gchar * s)
{
gboolean ret = FALSE;
return ret;
}
-#define CREATE_SERIALIZATION_START(_type,_macro) \
-static gint \
-gst_value_compare_ ## _type \
-(const GValue * value1, const GValue * value2) \
-{ \
- g ## _type val1 = g_value_get_ ## _type (value1); \
- g ## _type val2 = g_value_get_ ## _type (value2); \
- if (val1 > val2) \
- return GST_VALUE_GREATER_THAN; \
- if (val1 < val2) \
- return GST_VALUE_LESS_THAN; \
- return GST_VALUE_EQUAL; \
-} \
- \
-static char * \
-gst_value_serialize_ ## _type (const GValue * value) \
-{ \
- GValue val = { 0, }; \
- g_value_init (&val, G_TYPE_STRING); \
- if (!g_value_transform (value, &val)) \
- g_assert_not_reached (); \
- /* NO_COPY_MADNESS!!! */ \
- return (char *) g_value_get_string (&val); \
+#define CREATE_SERIALIZATION_START(_type,_macro) \
+static gint \
+gst_value_compare_ ## _type \
+(const GValue * value1, const GValue * value2) \
+{ \
+ g ## _type val1 = g_value_get_ ## _type (value1); \
+ g ## _type val2 = g_value_get_ ## _type (value2); \
+ if (val1 > val2) \
+ return GST_VALUE_GREATER_THAN; \
+ if (val1 < val2) \
+ return GST_VALUE_LESS_THAN; \
+ return GST_VALUE_EQUAL; \
+} \
+ \
+static char * \
+gst_value_serialize_ ## _type (const GValue * value) \
+{ \
+ GValue val = { 0, }; \
+ g_value_init (&val, G_TYPE_STRING); \
+ if (!g_value_transform (value, &val)) \
+ g_assert_not_reached (); \
+ /* NO_COPY_MADNESS!!! */ \
+ return (char *) g_value_get_string (&val); \
}
/* deserialize the given s into to as a gint64.
* bytes.
*/
static gboolean
-gst_value_deserialize_int_helper (gint64 * to, const char *s,
- gint64 min, gint64 max, int size)
+gst_value_deserialize_int_helper (gint64 * to, const gchar * s,
+ gint64 min, gint64 max, gint size)
{
gboolean ret = FALSE;
char *end;
return ret;
}
-#define CREATE_SERIALIZATION(_type,_macro) \
-CREATE_SERIALIZATION_START(_type,_macro) \
- \
-static gboolean \
-gst_value_deserialize_ ## _type (GValue * dest, const char *s) \
-{ \
- gint64 x; \
- \
- if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
- G_MAX ## _macro, sizeof (g ## _type))) { \
- g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
- return TRUE; \
- } else { \
- return FALSE; \
- } \
-}
-
-#define CREATE_USERIALIZATION(_type,_macro) \
-CREATE_SERIALIZATION_START(_type,_macro) \
- \
-static gboolean \
-gst_value_deserialize_ ## _type (GValue * dest, const char *s) \
-{ \
- gint64 x; \
- char *end; \
- gboolean ret = FALSE; \
- \
- errno = 0; \
- x = g_ascii_strtoull (s, &end, 0); \
- /* a range error is a definitive no-no */ \
- if (errno == ERANGE) { \
- return FALSE; \
- } \
- /* the cast ensures the range check later on makes sense */ \
- x = (g ## _type) x; \
- if (*end == 0) { \
- ret = TRUE; \
- } else { \
- if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
- x = G_LITTLE_ENDIAN; \
- ret = TRUE; \
- } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
- x = G_BIG_ENDIAN; \
- ret = TRUE; \
- } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
- x = G_BYTE_ORDER; \
- ret = TRUE; \
- } else if (g_ascii_strcasecmp (s, "min") == 0) { \
- x = 0; \
- ret = TRUE; \
- } else if (g_ascii_strcasecmp (s, "max") == 0) { \
- x = G_MAX ## _macro; \
- ret = TRUE; \
- } \
- } \
- if (ret) { \
- if (x > G_MAX ## _macro) { \
- ret = FALSE; \
- } else { \
- g_value_set_ ## _type (dest, x); \
- } \
- } \
- return ret; \
-}
-
-#define REGISTER_SERIALIZATION(_gtype, _type) \
-G_STMT_START { \
- static const GstValueTable gst_value = { \
- _gtype, \
- gst_value_compare_ ## _type, \
- gst_value_serialize_ ## _type, \
- gst_value_deserialize_ ## _type, \
- }; \
- \
- gst_value_register (&gst_value); \
+#define CREATE_SERIALIZATION(_type,_macro) \
+CREATE_SERIALIZATION_START(_type,_macro) \
+ \
+static gboolean \
+gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
+{ \
+ gint64 x; \
+ \
+ if (gst_value_deserialize_int_helper (&x, s, G_MIN ## _macro, \
+ G_MAX ## _macro, sizeof (g ## _type))) { \
+ g_value_set_ ## _type (dest, /*(g ## _type)*/ x); \
+ return TRUE; \
+ } else { \
+ return FALSE; \
+ } \
+}
+
+#define CREATE_USERIALIZATION(_type,_macro) \
+CREATE_SERIALIZATION_START(_type,_macro) \
+ \
+static gboolean \
+gst_value_deserialize_ ## _type (GValue * dest, const gchar *s) \
+{ \
+ gint64 x; \
+ char *end; \
+ gboolean ret = FALSE; \
+ \
+ errno = 0; \
+ x = g_ascii_strtoull (s, &end, 0); \
+ /* a range error is a definitive no-no */ \
+ if (errno == ERANGE) { \
+ return FALSE; \
+ } \
+ /* the cast ensures the range check later on makes sense */ \
+ x = (g ## _type) x; \
+ if (*end == 0) { \
+ ret = TRUE; \
+ } else { \
+ if (g_ascii_strcasecmp (s, "little_endian") == 0) { \
+ x = G_LITTLE_ENDIAN; \
+ ret = TRUE; \
+ } else if (g_ascii_strcasecmp (s, "big_endian") == 0) { \
+ x = G_BIG_ENDIAN; \
+ ret = TRUE; \
+ } else if (g_ascii_strcasecmp (s, "byte_order") == 0) { \
+ x = G_BYTE_ORDER; \
+ ret = TRUE; \
+ } else if (g_ascii_strcasecmp (s, "min") == 0) { \
+ x = 0; \
+ ret = TRUE; \
+ } else if (g_ascii_strcasecmp (s, "max") == 0) { \
+ x = G_MAX ## _macro; \
+ ret = TRUE; \
+ } \
+ } \
+ if (ret) { \
+ if (x > G_MAX ## _macro) { \
+ ret = FALSE; \
+ } else { \
+ g_value_set_ ## _type (dest, x); \
+ } \
+ } \
+ return ret; \
+}
+
+#define REGISTER_SERIALIZATION(_gtype, _type) \
+G_STMT_START { \
+ static const GstValueTable gst_value = { \
+ _gtype, \
+ gst_value_compare_ ## _type, \
+ gst_value_serialize_ ## _type, \
+ gst_value_deserialize_ ## _type, \
+ }; \
+ \
+ gst_value_register (&gst_value); \
} G_STMT_END
CREATE_SERIALIZATION (int, INT);
}
static gboolean
-gst_value_deserialize_double (GValue * dest, const char *s)
+gst_value_deserialize_double (GValue * dest, const gchar * s)
{
double x;
gboolean ret = FALSE;
* float *
*********/
-static int
+static gint
gst_value_compare_float (const GValue * value1, const GValue * value2)
{
if (value1->data[0].v_float > value2->data[0].v_float)
return GST_VALUE_UNORDERED;
}
-static char *
+static gchar *
gst_value_serialize_float (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_float);
return g_strdup (d);
}
static gboolean
-gst_value_deserialize_float (GValue * dest, const char *s)
+gst_value_deserialize_float (GValue * dest, const gchar * s)
{
double x;
gboolean ret = FALSE;
if (x > G_MAXFLOAT || x < -G_MAXFLOAT)
ret = FALSE;
if (ret) {
- g_value_set_float (dest, x);
+ g_value_set_float (dest, (float) x);
}
return ret;
}
* string *
**********/
-static int
+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 {
+ int 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;
}
-#define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
- ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
- ((c) == '.'))
-
-static gchar *
-gst_string_wrap (const char *s)
+static int
+gst_string_measure_wrapping (const gchar * s)
{
- const gchar *t;
int len;
- gchar *d, *e;
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 g_strdup ("");
- 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, int 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;
}
+/* Do string wrapping/escaping */
+static gchar *
+gst_string_wrap (const gchar * s)
+{
+ int 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;
+ int 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.
*
* the input string must be \0 terminated.
*/
-static char *
+static gchar *
gst_string_unwrap (const gchar * s)
{
gchar *ret;
goto beach;
/* null terminate result string and return */
- *write++ = '\0';
+ *write = '\0';
return ret;
beach:
return NULL;
}
-static char *
+static gchar *
gst_value_serialize_string (const GValue * value)
{
return gst_string_wrap (value->data[0].v_pointer);
}
static gboolean
-gst_value_deserialize_string (GValue * dest, const char *s)
+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);
}
* enum *
********/
-static int
+static gint
gst_value_compare_enum (const GValue * value1, const GValue * value2)
{
GEnumValue *en1, *en2;
return GST_VALUE_EQUAL;
}
-static char *
+static gchar *
gst_value_serialize_enum (const GValue * value)
{
GEnumValue *en;
g_return_val_if_fail (klass, NULL);
en = g_enum_get_value (klass, g_value_get_enum (value));
g_type_class_unref (klass);
+
+ /* might be one of the custom formats registered later */
+ 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));
+ g_return_val_if_fail (format_def != NULL, NULL);
+ return g_strdup (format_def->description);
+ }
+
g_return_val_if_fail (en, NULL);
return g_strdup (en->value_name);
}
+static gint
+gst_value_deserialize_enum_iter_cmp (const GstFormatDefinition * format_def,
+ const gchar * s)
+{
+ if (g_ascii_strcasecmp (s, format_def->nick) == 0)
+ return 0;
+
+ return g_ascii_strcasecmp (s, format_def->description);
+}
+
static gboolean
-gst_value_deserialize_enum (GValue * dest, const char *s)
+gst_value_deserialize_enum (GValue * dest, const gchar * s)
{
GEnumValue *en;
gchar *endptr = NULL;
}
}
g_type_class_unref (klass);
+
+ /* might be one of the custom formats registered later */
+ if (G_UNLIKELY (en == NULL && G_VALUE_TYPE (dest) == GST_TYPE_FORMAT)) {
+ const GstFormatDefinition *format_def;
+ GstIterator *iter;
+
+ iter = gst_format_iterate_definitions ();
+
+ format_def = gst_iterator_find_custom (iter,
+ (GCompareFunc) gst_value_deserialize_enum_iter_cmp, (gpointer) s);
+
+ g_return_val_if_fail (format_def != NULL, FALSE);
+ g_value_set_enum (dest, (gint) format_def->value);
+ gst_iterator_free (iter);
+ return TRUE;
+ }
+
g_return_val_if_fail (en, FALSE);
g_value_set_enum (dest, en->value);
return TRUE;
********/
/* we just compare the value here */
-static int
+static gint
gst_value_compare_flags (const GValue * value1, const GValue * value2)
{
guint fl1, fl2;
}
/* the different flags are serialized separated with a + */
-static char *
+static gchar *
gst_value_serialize_flags (const GValue * value)
{
guint flags;
g_return_val_if_fail (klass, NULL);
- result = g_strdup ("");
flags = g_value_get_flags (value);
+
+ /* if no flags are set, try to serialize to the _NONE string */
+ if (!flags) {
+ fl = g_flags_get_first_value (klass, flags);
+ return g_strdup (fl->value_name);
+ }
+
+ /* some flags are set, so serialize one by one */
+ result = g_strdup ("");
while (flags) {
- fl = gst_flags_get_first_value (klass, flags);
+ fl = g_flags_get_first_value (klass, flags);
if (fl != NULL) {
tmp = g_strconcat (result, (first ? "" : "+"), fl->value_name, NULL);
g_free (result);
result = tmp;
first = FALSE;
+
+ /* clear flag */
+ flags &= ~fl->value;
}
- /* clear flag */
- flags &= ~fl->value;
}
g_type_class_unref (klass);
}
static gboolean
-gst_value_deserialize_flags (GValue * dest, const char *s)
+gst_value_deserialize_flags (GValue * dest, const gchar * s)
{
GFlagsValue *fl;
gchar *endptr = NULL;
gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
const GValue * src2)
{
- int min;
- int max;
+ gint min;
+ gint max;
min = MAX (src1->data[0].v_int, src2->data[0].v_int);
max = MIN (src1->data[1].v_int, src2->data[1].v_int);
gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
const GValue * src2)
{
- int min;
- int max;
+ gint min;
+ gint max;
min = MAX (src1->data[0].v_int, src2->data[0].v_int);
max = MIN (src1->data[1].v_int, src2->data[1].v_int);
gst_value_intersect_double_range_double_range (GValue * dest,
const GValue * src1, const GValue * src2)
{
- double min;
- double max;
+ gdouble min;
+ gdouble max;
min = MAX (src1->data[0].v_double, src2->data[0].v_double);
max = MIN (src1->data[1].v_double, src2->data[1].v_double);
}
if (min == max) {
g_value_init (dest, G_TYPE_DOUBLE);
- g_value_set_int (dest, min);
+ g_value_set_int (dest, (int) min);
return TRUE;
}
GValue val = { 0 };
/* only works on similar-sized arrays */
- size = gst_value_list_get_size (src1);
- if (size != gst_value_list_get_size (src2))
+ size = gst_value_array_get_size (src1);
+ if (size != gst_value_array_get_size (src2))
return FALSE;
g_value_init (dest, GST_TYPE_ARRAY);
for (n = 0; n < size; n++) {
- if (!gst_value_intersect (&val, gst_value_list_get_value (src1, n),
- gst_value_list_get_value (src2, n))) {
+ if (!gst_value_intersect (&val, gst_value_array_get_value (src1, n),
+ gst_value_array_get_value (src2, n))) {
g_value_unset (dest);
return FALSE;
}
- gst_value_list_append_value (dest, &val);
+ gst_value_array_append_value (dest, &val);
g_value_unset (&val);
}
return TRUE;
}
+static gboolean
+gst_value_intersect_fraction_fraction_range (GValue * dest, const GValue * src1,
+ const GValue * src2)
+{
+ int res1, res2;
+ GValue *vals;
+ GstValueCompareFunc compare;
+
+ vals = src2->data[0].v_pointer;
+
+ if (vals == NULL)
+ return FALSE;
+
+ if ((compare = gst_value_get_compare_func (src1))) {
+ res1 = gst_value_compare_with_func (&vals[0], src1, compare);
+ res2 = gst_value_compare_with_func (&vals[1], src1, compare);
+
+ if ((res1 == GST_VALUE_EQUAL || res1 == GST_VALUE_LESS_THAN) &&
+ (res2 == GST_VALUE_EQUAL || res2 == GST_VALUE_GREATER_THAN)) {
+ gst_value_init_and_copy (dest, src1);
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+static gboolean
+gst_value_intersect_fraction_range_fraction_range (GValue * dest,
+ const GValue * src1, const GValue * src2)
+{
+ GValue *min;
+ GValue *max;
+ int res;
+ GValue *vals1, *vals2;
+ GstValueCompareFunc compare;
+
+ vals1 = src1->data[0].v_pointer;
+ vals2 = src2->data[0].v_pointer;
+ g_return_val_if_fail (vals1 != NULL && vals2 != NULL, FALSE);
+
+ if ((compare = gst_value_get_compare_func (&vals1[0]))) {
+ /* min = MAX (src1.start, src2.start) */
+ res = gst_value_compare_with_func (&vals1[0], &vals2[0], compare);
+ g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
+ if (res == GST_VALUE_LESS_THAN)
+ min = &vals2[0]; /* Take the max of the 2 */
+ else
+ min = &vals1[0];
+
+ /* max = MIN (src1.end, src2.end) */
+ res = gst_value_compare_with_func (&vals1[1], &vals2[1], compare);
+ g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
+ if (res == GST_VALUE_GREATER_THAN)
+ max = &vals2[1]; /* Take the min of the 2 */
+ else
+ max = &vals1[1];
+
+ res = gst_value_compare_with_func (min, max, compare);
+ g_return_val_if_fail (res != GST_VALUE_UNORDERED, FALSE);
+ if (res == GST_VALUE_LESS_THAN) {
+ g_value_init (dest, GST_TYPE_FRACTION_RANGE);
+ vals1 = dest->data[0].v_pointer;
+ g_value_copy (min, &vals1[0]);
+ g_value_copy (max, &vals1[1]);
+ return TRUE;
+ }
+ if (res == GST_VALUE_EQUAL) {
+ gst_value_init_and_copy (dest, min);
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
/***************
* subtraction *
***************/
/* creates a new int range based on input values.
*/
static gboolean
-gst_value_create_new_range (GValue * dest, int min1, int max1, int min2,
- int max2)
+gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
+ gint max2)
{
GValue v1 = { 0, };
GValue v2 = { 0, };
gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
const GValue * subtrahend)
{
- int min = gst_value_get_int_range_min (minuend);
- int max = gst_value_get_int_range_max (minuend);
- int val = g_value_get_int (subtrahend);
+ gint min = gst_value_get_int_range_min (minuend);
+ gint max = gst_value_get_int_range_max (minuend);
+ gint val = g_value_get_int (subtrahend);
g_return_val_if_fail (min < max, FALSE);
gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
const GValue * subtrahend)
{
- int min1 = gst_value_get_int_range_min (minuend);
- int max1 = gst_value_get_int_range_max (minuend);
- int min2 = gst_value_get_int_range_min (subtrahend);
- int max2 = gst_value_get_int_range_max (subtrahend);
+ gint min1 = gst_value_get_int_range_min (minuend);
+ gint max1 = gst_value_get_int_range_max (minuend);
+ gint min2 = gst_value_get_int_range_min (subtrahend);
+ gint max2 = gst_value_get_int_range_max (subtrahend);
if (max2 == G_MAXINT && min2 == G_MININT) {
return FALSE;
gst_value_subtract_double_double_range (GValue * dest, const GValue * minuend,
const GValue * subtrahend)
{
- double min = gst_value_get_double_range_min (subtrahend);
- double max = gst_value_get_double_range_max (subtrahend);
- double val = g_value_get_double (minuend);
+ gdouble min = gst_value_get_double_range_min (subtrahend);
+ gdouble max = gst_value_get_double_range_max (subtrahend);
+ gdouble val = g_value_get_double (minuend);
if (val < min || val > max) {
gst_value_init_and_copy (dest, minuend);
{
/* since we don't have open ranges, we have to approximate */
/* done like with ints */
- double min1 = gst_value_get_double_range_min (minuend);
- double max2 = gst_value_get_double_range_max (minuend);
- double max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
- double min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
+ gdouble min1 = gst_value_get_double_range_min (minuend);
+ gdouble max2 = gst_value_get_double_range_max (minuend);
+ gdouble max1 = MIN (gst_value_get_double_range_min (subtrahend), max2);
+ gdouble min2 = MAX (gst_value_get_double_range_max (subtrahend), min1);
GValue v1 = { 0, };
GValue v2 = { 0, };
GValue *pv1, *pv2; /* yeah, hungarian! */
guint i, size;
GValue subtraction = { 0, };
gboolean ret = FALSE;
+ GType ltype;
+
+ ltype = gst_value_list_get_type ();
size = gst_value_list_get_size (minuend);
for (i = 0; i < size; i++) {
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, };
return ret;
}
-static gboolean
-gst_value_subtract_list (GValue * dest, const GValue * minuend,
- const GValue * subtrahend)
-{
- guint i, size;
- GValue data[2] = { {0,}, {0,} };
- GValue *subtraction = &data[0], *result = &data[1];
+static gboolean
+gst_value_subtract_list (GValue * dest, const GValue * minuend,
+ const GValue * subtrahend)
+{
+ guint i, size;
+ GValue data[2] = { {0,}, {0,} };
+ GValue *subtraction = &data[0], *result = &data[1];
+
+ gst_value_init_and_copy (result, minuend);
+ size = gst_value_list_get_size (subtrahend);
+ for (i = 0; i < size; i++) {
+ const GValue *cur = gst_value_list_get_value (subtrahend, i);
+
+ if (gst_value_subtract (subtraction, result, cur)) {
+ GValue *temp = result;
+
+ result = subtraction;
+ subtraction = temp;
+ g_value_unset (subtraction);
+ } else {
+ g_value_unset (result);
+ return FALSE;
+ }
+ }
+ gst_value_init_and_copy (dest, result);
+ g_value_unset (result);
+ return TRUE;
+}
+
+static gboolean
+gst_value_subtract_fraction_fraction_range (GValue * dest,
+ const GValue * minuend, const GValue * subtrahend)
+{
+ const GValue *min = gst_value_get_fraction_range_min (subtrahend);
+ const GValue *max = gst_value_get_fraction_range_max (subtrahend);
+ GstValueCompareFunc compare;
+
+ if ((compare = gst_value_get_compare_func (minuend))) {
+ /* subtracting a range from an fraction only works if the fraction
+ * is not in the range */
+ if (gst_value_compare_with_func (minuend, min, compare) ==
+ GST_VALUE_LESS_THAN ||
+ gst_value_compare_with_func (minuend, max, compare) ==
+ GST_VALUE_GREATER_THAN) {
+ /* and the result is the value */
+ gst_value_init_and_copy (dest, minuend);
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+static gboolean
+gst_value_subtract_fraction_range_fraction (GValue * dest,
+ const GValue * minuend, const GValue * subtrahend)
+{
+ /* since we don't have open ranges, we cannot create a hole in
+ * a range. We return the original range */
+ gst_value_init_and_copy (dest, minuend);
+ return TRUE;
+}
+
+static gboolean
+gst_value_subtract_fraction_range_fraction_range (GValue * dest,
+ const GValue * minuend, const GValue * subtrahend)
+{
+ /* since we don't have open ranges, we have to approximate */
+ /* done like with ints and doubles. Creates a list of 2 fraction ranges */
+ const GValue *min1 = gst_value_get_fraction_range_min (minuend);
+ 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;
+ GValue v1 = { 0, };
+ GValue v2 = { 0, };
+ GValue *pv1, *pv2; /* yeah, hungarian! */
+ GstValueCompareFunc compare;
+
+ g_return_val_if_fail (min1 != NULL && max1 != NULL, FALSE);
+ g_return_val_if_fail (min2 != NULL && max2 != NULL, FALSE);
+
+ compare = gst_value_get_compare_func (min1);
+ g_return_val_if_fail (compare, FALSE);
- gst_value_init_and_copy (result, minuend);
- size = gst_value_list_get_size (subtrahend);
- for (i = 0; i < size; i++) {
- const GValue *cur = gst_value_list_get_value (subtrahend, i);
+ cmp1 = gst_value_compare_with_func (max2, max1, compare);
+ g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
+ if (cmp1 == GST_VALUE_LESS_THAN)
+ max1 = max2;
+ cmp1 = gst_value_compare_with_func (min1, min2, compare);
+ g_return_val_if_fail (cmp1 != GST_VALUE_UNORDERED, FALSE);
+ if (cmp1 == GST_VALUE_GREATER_THAN)
+ min2 = min1;
- if (gst_value_subtract (subtraction, result, cur)) {
- GValue *temp = result;
+ cmp1 = gst_value_compare_with_func (min1, max1, compare);
+ cmp2 = gst_value_compare_with_func (min2, max2, compare);
- result = subtraction;
- subtraction = temp;
- g_value_unset (subtraction);
- } else {
- g_value_unset (result);
- return FALSE;
- }
+ if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
+ pv1 = &v1;
+ pv2 = &v2;
+ } else if (cmp1 == GST_VALUE_LESS_THAN) {
+ pv1 = dest;
+ pv2 = NULL;
+ } else if (cmp2 == GST_VALUE_LESS_THAN) {
+ pv1 = NULL;
+ pv2 = dest;
+ } else {
+ return FALSE;
+ }
+
+ if (cmp1 == GST_VALUE_LESS_THAN) {
+ g_value_init (pv1, GST_TYPE_FRACTION_RANGE);
+ gst_value_set_fraction_range (pv1, min1, max1);
+ }
+ if (cmp2 == GST_VALUE_LESS_THAN) {
+ g_value_init (pv2, GST_TYPE_FRACTION_RANGE);
+ gst_value_set_fraction_range (pv2, min2, max2);
+ }
+
+ if (cmp1 == GST_VALUE_LESS_THAN && cmp2 == GST_VALUE_LESS_THAN) {
+ gst_value_list_concat (dest, pv1, pv2);
+ g_value_unset (pv1);
+ g_value_unset (pv2);
}
- gst_value_init_and_copy (dest, result);
- g_value_unset (result);
return TRUE;
}
* comparison *
**************/
+/*
+ * gst_value_get_compare_func:
+ * @value1: a value to get the compare function for
+ *
+ * Determines the compare function to be used with values of the same type as
+ * @value1. The function can be given to gst_value_compare_with_func().
+ *
+ * Returns: A #GstValueCompareFunc value
+ */
+static GstValueCompareFunc
+gst_value_get_compare_func (const GValue * value1)
+{
+ GstValueTable *table, *best = NULL;
+ guint i;
+ GType type1;
+
+ 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 (G_LIKELY (best))
+ return best->compare;
+
+ return NULL;
+}
+
/**
* gst_value_can_compare:
* @value1: a value to compare
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;
+ return gst_value_get_compare_func (value1) != NULL;
}
/**
*
* Compares @value1 and @value2. If @value1 and @value2 cannot be
* compared, the function returns GST_VALUE_UNORDERED. Otherwise,
- * if @value1 is greater than @value2, GST_VALUE_GREATER is returned.
- * If @value1 is less than @value2, GST_VALUE_LESSER is returned.
+ * if @value1 is greater than @value2, GST_VALUE_GREATER_THAN is returned.
+ * 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: A #GstValueCompareType value
*/
-int
+gint
gst_value_compare (const GValue * value1, const GValue * value2)
{
- GstValueTable *table, *best = NULL;
- guint i;
+ GstValueCompareFunc compare;
if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
return GST_VALUE_UNORDERED;
- 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;
- }
- }
- if (best) {
- return best->compare (value1, value2);
+ compare = gst_value_get_compare_func (value1);
+ if (compare) {
+ return compare (value1, value2);
}
g_critical ("unable to compare values of type %s\n",
return GST_VALUE_UNORDERED;
}
+/*
+ * gst_value_compare_with_func:
+ * @value1: a value to compare
+ * @value2: another value to compare
+ * @compare: compare function
+ *
+ * Compares @value1 and @value2 using the @compare function. Works like
+ * gst_value_compare() but allows to save time determining the compare function
+ * a multiple times.
+ *
+ * Returns: A #GstValueCompareType value
+ */
+static gint
+gst_value_compare_with_func (const GValue * value1, const GValue * value2,
+ GstValueCompareFunc compare)
+{
+ g_assert (compare);
+
+ if (G_VALUE_TYPE (value1) != G_VALUE_TYPE (value2))
+ return GST_VALUE_UNORDERED;
+
+ return compare (value1, value2);
+}
+
/* union */
/**
gst_value_can_union (const GValue * value1, const GValue * value2)
{
GstValueUnionInfo *union_info;
- guint i;
+ guint i, len;
- 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))
* @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: %TRUE if a union was successful
+ * Returns: always returns %TRUE
*/
/* FIXME: change return type to 'void'? */
gboolean
gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
{
GstValueUnionInfo *union_info;
- guint i;
+ guint i, len;
- 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)) {
* Registers a union function that can create a union between GValues
* of the type @type1 and @type2.
*
+ * Union functions should be registered at startup before any pipelines are
+ * started, as gst_value_register_union_func() is not thread-safe and cannot
+ * be used at the same time as gst_value_union() or gst_value_can_union().
*/
void
gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
* 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_intersection_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;
+
+ 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;
+
+ 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;
- for (i = 0; i < gst_value_intersect_funcs->len; i++) {
+ /* 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);
const GValue * value2)
{
GstValueIntersectInfo *intersect_info;
- guint i;
- gboolean ret = FALSE;
+ guint i, len;
+ GType ltype, type1, type2;
+
+ 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_register_intersection_func:
+ * gst_value_register_intersect_func:
* @type1: the first type to intersect
* @type2: the second type to intersect
* @func: the intersection function
*
* Registers a function that is called to calculate the intersection
* of the values having the types @type1 and @type2.
- */
-/**
- * GstValueIntersectFunc:
- * @dest: a uninitialized #GValue that will hold the calculated
- * intersection value
- * @value1: a value to intersect
- * @value2: another value to intersect
- *
- * Functions having this type calculate the intersection of @value1
- * and @value2. If the intersection is non-empty, the result is
- * placed in @dest and TRUE is returned. If the intersection is
- * empty, @dest is unmodified and FALSE is returned.
*
- * Returns: TRUE if the intersection is non-empty, FALSE otherwise
+ * Intersect functions should be registered at startup before any pipelines are
+ * started, as gst_value_register_intersect_func() is not thread-safe and
+ * cannot be used at the same time as gst_value_intersect() or
+ * gst_value_can_intersect().
*/
void
gst_value_register_intersect_func (GType type1, GType type2,
const GValue * subtrahend)
{
GstValueSubtractInfo *info;
- guint i;
+ guint i, len;
+ GType ltype, mtype, stype;
+
+ 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;
+
+ 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;
}
*
* Registers @func as a function capable of subtracting the values of
* @subtrahend_type from values of @minuend_type.
+ *
+ * Subtract functions should be registered at startup before any pipelines are
+ * started, as gst_value_register_subtract_func() is not thread-safe and
+ * cannot be used at the same time as gst_value_subtract().
*/
void
gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
* @table: structure containing functions to register
*
* Registers functions to perform calculations on #GValues of a given
- * type.
- */
-/**
- * GstValueTable:
- * @type: GType that the functions operate on.
- * @compare: A function that compares two values of this type.
- * @serialize: A function that transforms a value of this type to a
- * string. Strings created by this function must be unique and should
- * be human readable.
- * @deserialize: A function that transforms a string to a value of
- * this type. This function must transform strings created by the
- * serialize function back to the original value. This function may
- * optionally transform other strings into values.
+ * type. Each type can only be added once.
*/
void
gst_value_register (const GstValueTable * table)
{
+ GstValueTable *found;
+
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);
}
/**
gchar *
gst_value_serialize (const GValue * value)
{
- guint i;
+ guint i, len;
GValue s_val = { 0 };
- GstValueTable *table, *best = NULL;
+ GstValueTable *table, *best;
char *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);
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;
}
{
GType 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;
/* check recursively */
- size = gst_value_list_get_size (value);
+ size = gst_value_array_get_size (value);
for (n = 0; n < size; n++) {
- kid = gst_value_list_get_value (value, n);
- fixed &= gst_value_is_fixed (kid);
+ kid = gst_value_array_get_value (value, n);
+ 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)
{
- value->data[0].v_int = collect_values[0].v_int;
- value->data[1].v_int = collect_values[1].v_int;
+ gst_value_set_fraction (value,
+ collect_values[0].v_int, collect_values[1].v_int);
return NULL;
}
}
/* check for reduction */
- gcd = gst_greatest_common_divisor (numerator, denominator);
+ gcd = gst_util_greatest_common_divisor (numerator, denominator);
if (gcd) {
numerator /= gcd;
denominator /= gcd;
}
+
+ g_assert (denominator > 0);
+
value->data[0].v_int = numerator;
value->data[1].v_int = denominator;
}
*
* Returns: the numerator of the fraction.
*/
-int
+gint
gst_value_get_fraction_numerator (const GValue * value)
{
g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
*
* Returns: the denominator of the fraction.
*/
-int
+gint
gst_value_get_fraction_denominator (const GValue * value)
{
- g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 0);
+ g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (value), 1);
return value->data[1].v_int;
}
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 (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;
+ if (!gst_util_fraction_multiply (n1, d1, n2, d2, &res_n, &res_d))
+ return FALSE;
+
+ gst_value_set_fraction (product, res_n, res_d);
+
+ return TRUE;
+}
+
+/**
+ * gst_value_fraction_subtract:
+ * @dest: a GValue initialized to #GST_TYPE_FRACTION
+ * @minuend: a GValue initialized to #GST_TYPE_FRACTION
+ * @subtrahend: a GValue initialized to #GST_TYPE_FRACTION
+ *
+ * Subtracts the @subtrahend from the @minuend and sets @dest to the result.
+ *
+ * Returns: FALSE in case of an error (like integer overflow), TRUE otherwise.
+ */
+gboolean
+gst_value_fraction_subtract (GValue * dest,
+ const GValue * minuend, const GValue * subtrahend)
+{
+ gint n1, n2, d1, d2;
+ gint res_n, res_d;
+
+ g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (minuend), FALSE);
+ g_return_val_if_fail (GST_VALUE_HOLDS_FRACTION (subtrahend), FALSE);
- 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);
+ n1 = minuend->data[0].v_int;
+ n2 = subtrahend->data[0].v_int;
+ d1 = minuend->data[1].v_int;
+ d2 = subtrahend->data[1].v_int;
- gst_value_set_fraction (product, n1 * n2, 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;
}
-static char *
+static gchar *
gst_value_serialize_fraction (const GValue * value)
{
gint32 numerator = value->data[0].v_int;
}
static gboolean
-gst_value_deserialize_fraction (GValue * dest, const char *s)
+gst_value_deserialize_fraction (GValue * dest, const gchar * s)
{
gint num, den;
- if (s && sscanf (s, "%d/%d", &num, &den) == 2) {
+ 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) {
gst_value_set_fraction (dest, num, den);
return TRUE;
}
+ if (sscanf (s, "%d", &num) == 1) {
+ gst_value_set_fraction (dest, num, 1);
+ return TRUE;
+ }
+ 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, G_MAXINT, 1);
+ return TRUE;
+ }
return FALSE;
}
gst_value_transform_string_fraction (const GValue * src_value,
GValue * dest_value)
{
- gst_value_deserialize_fraction (dest_value, src_value->data[0].v_pointer);
+ if (!gst_value_deserialize_fraction (dest_value,
+ src_value->data[0].v_pointer))
+ /* If the deserialize fails, ensure we leave the fraction in a
+ * valid, if incorrect, state */
+ 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 = floor (F);
- /* 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 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;
if (new_num_1 > new_num_2)
return GST_VALUE_GREATER_THAN;
- g_assert_not_reached ();
+ /* 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;
}
gst_value_set_date (GValue * value, const GDate * date)
{
g_return_if_fail (G_VALUE_TYPE (value) == GST_TYPE_DATE);
+ g_return_if_fail (g_date_valid (date));
g_value_set_boxed (value, date);
}
{
const GDate *date = (const GDate *) boxed;
+ if (!g_date_valid (date)) {
+ GST_WARNING ("invalid GDate");
+ return NULL;
+ }
+
return g_date_new_julian (g_date_get_julian (date));
}
-static int
+static gint
gst_value_compare_date (const GValue * value1, const GValue * value2)
{
const GDate *date1 = (const GDate *) g_value_get_boxed (value1);
return GST_VALUE_GREATER_THAN;
}
-static char *
+static gchar *
gst_value_serialize_date (const GValue * val)
{
const GDate *date = (const GDate *) g_value_get_boxed (val);
gst_value_deserialize_date (dest_value, src_value->data[0].v_pointer);
}
+static void
+gst_value_transform_object_string (const GValue * src_value,
+ GValue * dest_value)
+{
+ GstObject *obj;
+ gchar *str;
+
+ obj = g_value_get_object (src_value);
+ 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 = str;
+}
+
static GTypeInfo _info = {
0,
NULL,
0
};
-#define FUNC_VALUE_GET_TYPE(type, name) \
-GType gst_ ## type ## _get_type (void) \
-{ \
- static GType gst_ ## type ## _type = 0; \
- \
- if (!gst_ ## type ## _type) { \
- _info.value_table = & _gst_ ## type ## _value_table; \
- gst_ ## type ## _type = g_type_register_fundamental ( \
- g_type_fundamental_next (), \
- name, &_info, &_finfo, 0); \
- } \
- \
- return gst_ ## type ## _type; \
+#define FUNC_VALUE_GET_TYPE(type, name) \
+GType gst_ ## type ## _get_type (void) \
+{ \
+ static GType gst_ ## type ## _type = 0; \
+ \
+ if (G_UNLIKELY (gst_ ## type ## _type == 0)) { \
+ _info.value_table = & _gst_ ## type ## _value_table; \
+ gst_ ## type ## _type = g_type_register_fundamental ( \
+ g_type_fundamental_next (), \
+ name, &_info, &_finfo, 0); \
+ } \
+ \
+ return gst_ ## type ## _type; \
}
static const GTypeValueTable _gst_fourcc_value_table = {
FUNC_VALUE_GET_TYPE (double_range, "GstDoubleRange");
+static const GTypeValueTable _gst_fraction_range_value_table = {
+ gst_value_init_fraction_range,
+ gst_value_free_fraction_range,
+ gst_value_copy_fraction_range,
+ NULL,
+ "iiii",
+ gst_value_collect_fraction_range,
+ "pppp",
+ gst_value_lcopy_fraction_range
+};
+
+FUNC_VALUE_GET_TYPE (fraction_range, "GstFractionRange");
+
static const GTypeValueTable _gst_value_list_value_table = {
- gst_value_init_list,
- gst_value_free_list,
- gst_value_copy_list,
- gst_value_list_peek_pointer,
+ gst_value_init_list_or_array,
+ gst_value_free_list_or_array,
+ gst_value_copy_list_or_array,
+ gst_value_list_or_array_peek_pointer,
"p",
- gst_value_collect_list,
+ gst_value_collect_list_or_array,
"p",
- gst_value_lcopy_list
+ gst_value_lcopy_list_or_array
};
FUNC_VALUE_GET_TYPE (value_list, "GstValueList");
static const GTypeValueTable _gst_value_array_value_table = {
- gst_value_init_list,
- gst_value_free_list,
- gst_value_copy_list,
- gst_value_list_peek_pointer,
+ gst_value_init_list_or_array,
+ gst_value_free_list_or_array,
+ gst_value_copy_list_or_array,
+ gst_value_list_or_array_peek_pointer,
"p",
- gst_value_collect_list,
+ gst_value_collect_list_or_array,
"p",
- gst_value_lcopy_list
+ gst_value_lcopy_list_or_array
};
FUNC_VALUE_GET_TYPE (value_array, "GstValueArray");
{
static GType gst_date_type = 0;
- if (!gst_date_type) {
- /* Not using G_TYPE_DATE here on purpose, even if we could
+ if (G_UNLIKELY (gst_date_type == 0)) {
+ /* FIXME 0.11: we require GLib 2.8 already
+ * Not using G_TYPE_DATE here on purpose, even if we could
* if GLIB_CHECK_VERSION(2,8,0) was true: we don't want the
* serialised strings to have different type strings depending
- * on what version is used, so FIXME in 0.11 when we
- * require GLib-2.8 */
+ * on what version is used, so FIXME when we require GLib-2.8 */
gst_date_type = g_boxed_type_register_static ("GstDate",
(GBoxedCopyFunc) gst_date_copy, (GBoxedFreeFunc) g_date_free);
}
void
_gst_value_initialize (void)
{
- //const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
-
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_fraction_range,
+ gst_value_serialize_fraction_range,
+ gst_value_deserialize_fraction_range,
+ };
+
+ gst_value.type = gst_fraction_range_get_type ();
+ gst_value_register (&gst_value);
+ }
+
+ {
+ static GstValueTable gst_value = {
+ 0,
gst_value_compare_list,
gst_value_serialize_list,
gst_value_deserialize_list,
{
static GstValueTable gst_value = {
0,
- gst_value_compare_list,
+ gst_value_compare_array,
gst_value_serialize_array,
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);
}
{
static GstValueTable gst_value = {
0,
+ NULL,
+ gst_value_serialize_structure,
+ gst_value_deserialize_structure,
+ };
+
+ gst_value.type = GST_TYPE_STRUCTURE;
+ gst_value_register (&gst_value);
+ }
+ {
+ static GstValueTable gst_value = {
+ 0,
gst_value_compare_date,
gst_value_serialize_date,
gst_value_deserialize_date,
gst_value_transform_int_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_fraction_range_string);
g_value_register_transform_func (GST_TYPE_LIST, G_TYPE_STRING,
gst_value_transform_list_string);
g_value_register_transform_func (GST_TYPE_ARRAY, 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_transform_string_date);
+ g_value_register_transform_func (GST_TYPE_OBJECT, G_TYPE_STRING,
+ gst_value_transform_object_string);
gst_value_register_intersect_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
gst_value_intersect_int_int_range);
GST_TYPE_DOUBLE_RANGE, gst_value_intersect_double_range_double_range);
gst_value_register_intersect_func (GST_TYPE_ARRAY,
GST_TYPE_ARRAY, gst_value_intersect_array);
+ gst_value_register_intersect_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
+ gst_value_intersect_fraction_fraction_range);
+ gst_value_register_intersect_func (GST_TYPE_FRACTION_RANGE,
+ GST_TYPE_FRACTION_RANGE,
+ gst_value_intersect_fraction_range_fraction_range);
gst_value_register_subtract_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
gst_value_subtract_int_int_range);
gst_value_register_subtract_func (GST_TYPE_DOUBLE_RANGE,
GST_TYPE_DOUBLE_RANGE, gst_value_subtract_double_range_double_range);
-#if GLIB_CHECK_VERSION(2,8,0)
+ gst_value_register_subtract_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
+ gst_value_subtract_fraction_fraction_range);
+ gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE, GST_TYPE_FRACTION,
+ gst_value_subtract_fraction_range_fraction);
+ gst_value_register_subtract_func (GST_TYPE_FRACTION_RANGE,
+ GST_TYPE_FRACTION_RANGE,
+ gst_value_subtract_fraction_range_fraction_range);
+
/* see bug #317246, #64994, #65041 */
{
volatile GType date_type = G_TYPE_DATE;
- GST_LOG ("Faking out the compiler: %d", date_type);
+ g_type_name (date_type);
}
-#endif
gst_value_register_union_func (G_TYPE_INT, GST_TYPE_INT_RANGE,
gst_value_union_int_int_range);
gst_value_register_union_func (GST_TYPE_INT_RANGE, GST_TYPE_INT_RANGE,
gst_value_union_int_range_int_range);
+
+#if 0
+ /* Implement these if needed */
+ gst_value_register_union_func (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE,
+ gst_value_union_fraction_fraction_range);
+ gst_value_register_union_func (GST_TYPE_FRACTION_RANGE,
+ GST_TYPE_FRACTION_RANGE, gst_value_union_fraction_range_fraction_range);
+#endif
}