g_type_init() is no longer required and deprecated in glib >= 2.35.0
[platform/upstream/gstreamer.git] / gst / gstvalue.c
index 4ca72f8..de9fa06 100644 (file)
 #include <gobject/gvaluecollector.h>
 #include "gstutils.h"
 
+/* GstValueUnionFunc:
+ * @dest: a #GValue for the result
+ * @value1: a #GValue operand
+ * @value2: a #GValue operand
+ *
+ * Used by gst_value_union() to perform unification for a specific #GValue
+ * type. Register a new implementation with gst_value_register_union_func().
+ *
+ * Returns: %TRUE if a union was successful
+ */
+typedef gboolean (*GstValueUnionFunc) (GValue * dest,
+    const GValue * value1, const GValue * value2);
+
+/* GstValueIntersectFunc:
+ * @dest: (out caller-allocates): a #GValue for the result
+ * @value1: a #GValue operand
+ * @value2: a #GValue operand
+ *
+ * Used by gst_value_intersect() to perform intersection for a specific #GValue
+ * type. 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.
+ * Register a new implementation with gst_value_register_intersect_func().
+ *
+ * Returns: %TRUE if the values can intersect
+ */
+typedef gboolean (*GstValueIntersectFunc) (GValue * dest,
+    const GValue * value1, const GValue * value2);
+
+/* GstValueSubtractFunc:
+ * @dest: (out caller-allocates): a #GValue for the result
+ * @minuend: a #GValue operand
+ * @subtrahend: a #GValue operand
+ *
+ * Used by gst_value_subtract() to perform subtraction for a specific #GValue
+ * type. Register a new implementation with gst_value_register_subtract_func().
+ *
+ * Returns: %TRUE if the subtraction is not empty
+ */
+typedef gboolean (*GstValueSubtractFunc) (GValue * dest,
+    const GValue * minuend, const GValue * subtrahend);
+
+static void gst_value_register_union_func (GType type1,
+    GType type2, GstValueUnionFunc func);
+static void gst_value_register_intersect_func (GType type1,
+    GType type2, GstValueIntersectFunc func);
+static void gst_value_register_subtract_func (GType minuend_type,
+    GType subtrahend_type, GstValueSubtractFunc func);
+
 typedef struct _GstValueUnionInfo GstValueUnionInfo;
 struct _GstValueUnionInfo
 {
@@ -137,10 +186,15 @@ gst_value_serialize_any_list (const GValue * value, const gchar * 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 < alen - 1) {
-      g_string_append_len (s, ", ", 2);
+    if (s_val != NULL) {
+      g_string_append (s, s_val);
+      g_free (s_val);
+      if (i < alen - 1) {
+        g_string_append_len (s, ", ", 2);
+      }
+    } else {
+      GST_WARNING ("Could not serialize list/array value of type '%s'",
+          G_VALUE_TYPE_NAME (v));
     }
   }
   g_string_append (s, end);
@@ -296,6 +350,63 @@ gst_value_lcopy_list_or_array (const GValue * value, guint n_collect_values,
   return NULL;
 }
 
+static gboolean
+gst_value_list_or_array_get_basic_type (const GValue * value, GType * type)
+{
+  if (G_UNLIKELY (value == NULL))
+    return FALSE;
+
+  if (GST_VALUE_HOLDS_LIST (value)) {
+    if (VALUE_LIST_SIZE (value) == 0)
+      return FALSE;
+    return gst_value_list_or_array_get_basic_type (VALUE_LIST_GET_VALUE (value,
+            0), type);
+  }
+  if (GST_VALUE_HOLDS_ARRAY (value)) {
+    const GArray *array = (const GArray *) value->data[0].v_pointer;
+    if (array->len == 0)
+      return FALSE;
+    return gst_value_list_or_array_get_basic_type (&g_array_index (array,
+            GValue, 0), type);
+  }
+
+  *type = G_VALUE_TYPE (value);
+
+  return TRUE;
+}
+
+#define IS_RANGE_COMPAT(type1,type2,t1,t2) \
+  (((t1) == (type1) && (t2) == (type2)) || ((t2) == (type1) && (t1) == (type2)))
+
+static gboolean
+gst_value_list_or_array_are_compatible (const GValue * value1,
+    const GValue * value2)
+{
+  GType basic_type1, basic_type2;
+
+  /* empty or same type is OK */
+  if (!gst_value_list_or_array_get_basic_type (value1, &basic_type1) ||
+      !gst_value_list_or_array_get_basic_type (value2, &basic_type2) ||
+      basic_type1 == basic_type2)
+    return TRUE;
+
+  /* ranges are distinct types for each bound type... */
+  if (IS_RANGE_COMPAT (G_TYPE_INT, GST_TYPE_INT_RANGE, basic_type1,
+          basic_type2))
+    return TRUE;
+  if (IS_RANGE_COMPAT (G_TYPE_INT64, GST_TYPE_INT64_RANGE, basic_type1,
+          basic_type2))
+    return TRUE;
+  if (IS_RANGE_COMPAT (G_TYPE_DOUBLE, GST_TYPE_DOUBLE_RANGE, basic_type1,
+          basic_type2))
+    return TRUE;
+  if (IS_RANGE_COMPAT (GST_TYPE_FRACTION, GST_TYPE_FRACTION_RANGE, basic_type1,
+          basic_type2))
+    return TRUE;
+
+  return FALSE;
+}
+
 /**
  * gst_value_list_append_value:
  * @value: a #GValue of type #GST_TYPE_LIST
@@ -310,6 +421,8 @@ gst_value_list_append_value (GValue * value, const GValue * append_value)
 
   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
   g_return_if_fail (G_IS_VALUE (append_value));
+  g_return_if_fail (gst_value_list_or_array_are_compatible (value,
+          append_value));
 
   gst_value_init_and_copy (&val, append_value);
   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
@@ -329,6 +442,8 @@ gst_value_list_prepend_value (GValue * value, const GValue * prepend_value)
 
   g_return_if_fail (GST_VALUE_HOLDS_LIST (value));
   g_return_if_fail (G_IS_VALUE (prepend_value));
+  g_return_if_fail (gst_value_list_or_array_are_compatible (value,
+          prepend_value));
 
   gst_value_init_and_copy (&val, prepend_value);
   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
@@ -355,6 +470,7 @@ gst_value_list_concat (GValue * dest, const GValue * value1,
   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));
+  g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
 
   value1_length =
       (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
@@ -396,8 +512,6 @@ gst_value_list_concat (GValue * dest, const GValue * value1,
  * 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,
@@ -412,6 +526,7 @@ gst_value_list_merge (GValue * dest, const GValue * value1,
   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));
+  g_return_if_fail (gst_value_list_or_array_are_compatible (value1, value2));
 
   value1_length =
       (GST_VALUE_HOLDS_LIST (value1) ? VALUE_LIST_SIZE (value1) : 1);
@@ -537,6 +652,8 @@ gst_value_array_append_value (GValue * value, const GValue * append_value)
 
   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
   g_return_if_fail (G_IS_VALUE (append_value));
+  g_return_if_fail (gst_value_list_or_array_are_compatible (value,
+          append_value));
 
   gst_value_init_and_copy (&val, append_value);
   g_array_append_vals ((GArray *) value->data[0].v_pointer, &val, 1);
@@ -556,6 +673,8 @@ gst_value_array_prepend_value (GValue * value, const GValue * prepend_value)
 
   g_return_if_fail (GST_VALUE_HOLDS_ARRAY (value));
   g_return_if_fail (G_IS_VALUE (prepend_value));
+  g_return_if_fail (gst_value_list_or_array_are_compatible (value,
+          prepend_value));
 
   gst_value_init_and_copy (&val, prepend_value);
   g_array_prepend_vals ((GArray *) value->data[0].v_pointer, &val, 1);
@@ -716,26 +835,56 @@ gst_value_deserialize_array (GValue * dest, const gchar * s)
 
 /*************
  * int range *
+ *
+ * Values in the range are defined as any value greater or equal
+ * to min*step, AND lesser or equal to max*step.
+ * For step == 1, this falls back to the traditional range semantics.
  *************/
 
+#define INT_RANGE_MIN(v) (((gint *)((v)->data[0].v_pointer))[0])
+#define INT_RANGE_MAX(v) (((gint *)((v)->data[0].v_pointer))[1])
+#define INT_RANGE_STEP(v) (((gint *)((v)->data[0].v_pointer))[2])
+
 static void
 gst_value_init_int_range (GValue * value)
 {
-  value->data[0].v_int = 0;
-  value->data[1].v_int = 0;
+  gint *vals = g_slice_alloc0 (3 * sizeof (gint));
+  value->data[0].v_pointer = vals;
+  INT_RANGE_MIN (value) = 0;
+  INT_RANGE_MAX (value) = 0;
+  INT_RANGE_STEP (value) = 1;
+}
+
+static void
+gst_value_free_int_range (GValue * value)
+{
+  g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
+  g_slice_free1 (3 * sizeof (gint), value->data[0].v_pointer);
+  value->data[0].v_pointer = NULL;
 }
 
 static void
 gst_value_copy_int_range (const GValue * src_value, GValue * dest_value)
 {
-  dest_value->data[0].v_int = src_value->data[0].v_int;
-  dest_value->data[1].v_int = src_value->data[1].v_int;
+  gint *vals = (gint *) dest_value->data[0].v_pointer;
+  gint *src_vals = (gint *) src_value->data[0].v_pointer;
+
+  if (vals == NULL) {
+    gst_value_init_int_range (dest_value);
+  }
+  if (src_vals != NULL) {
+    INT_RANGE_MIN (dest_value) = INT_RANGE_MIN (src_value);
+    INT_RANGE_MAX (dest_value) = INT_RANGE_MAX (src_value);
+    INT_RANGE_STEP (dest_value) = INT_RANGE_STEP (src_value);
+  }
 }
 
 static gchar *
 gst_value_collect_int_range (GValue * value, guint n_collect_values,
     GTypeCValue * collect_values, guint collect_flags)
 {
+  gint *vals = value->data[0].v_pointer;
+
   if (n_collect_values != 2)
     return g_strdup_printf ("not enough value locations for `%s' passed",
         G_VALUE_TYPE_NAME (value));
@@ -743,8 +892,12 @@ gst_value_collect_int_range (GValue * value, guint n_collect_values,
     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;
+  if (vals == NULL) {
+    gst_value_init_int_range (value);
+  }
+
+  gst_value_set_int_range_step (value, collect_values[0].v_int,
+      collect_values[1].v_int, 1);
 
   return NULL;
 }
@@ -755,6 +908,8 @@ gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
 {
   guint32 *int_range_start = collect_values[0].v_pointer;
   guint32 *int_range_end = collect_values[1].v_pointer;
+  guint32 *int_range_step = collect_values[2].v_pointer;
+  gint *vals = (gint *) value->data[0].v_pointer;
 
   if (!int_range_start)
     return g_strdup_printf ("start value location for `%s' passed as NULL",
@@ -762,29 +917,57 @@ gst_value_lcopy_int_range (const GValue * value, guint n_collect_values,
   if (!int_range_end)
     return g_strdup_printf ("end value location for `%s' passed as NULL",
         G_VALUE_TYPE_NAME (value));
+  if (!int_range_step)
+    return g_strdup_printf ("step value location for `%s' passed as NULL",
+        G_VALUE_TYPE_NAME (value));
+
+  if (G_UNLIKELY (vals == NULL)) {
+    return g_strdup_printf ("Uninitialised `%s' passed",
+        G_VALUE_TYPE_NAME (value));
+  }
 
-  *int_range_start = value->data[0].v_int;
-  *int_range_end = value->data[1].v_int;
+  *int_range_start = INT_RANGE_MIN (value);
+  *int_range_end = INT_RANGE_MAX (value);
+  *int_range_step = INT_RANGE_STEP (value);
 
   return NULL;
 }
 
 /**
- * gst_value_set_int_range:
+ * gst_value_set_int_range_step:
  * @value: a GValue initialized to GST_TYPE_INT_RANGE
  * @start: the start of the range
  * @end: the end of the range
+ * @step: the step of the range
  *
- * Sets @value to the range specified by @start and @end.
+ * Sets @value to the range specified by @start, @end and @step.
  */
 void
-gst_value_set_int_range (GValue * value, gint start, gint end)
+gst_value_set_int_range_step (GValue * value, gint start, gint end, gint step)
 {
   g_return_if_fail (GST_VALUE_HOLDS_INT_RANGE (value));
   g_return_if_fail (start < end);
+  g_return_if_fail (step > 0);
+  g_return_if_fail (start % step == 0);
+  g_return_if_fail (end % step == 0);
 
-  value->data[0].v_int = start;
-  value->data[1].v_int = end;
+  INT_RANGE_MIN (value) = start / step;
+  INT_RANGE_MAX (value) = end / step;
+  INT_RANGE_STEP (value) = step;
+}
+
+/**
+ * gst_value_set_int_range:
+ * @value: a GValue initialized to GST_TYPE_INT_RANGE
+ * @start: the start of the range
+ * @end: the end of the range
+ *
+ * Sets @value to the range specified by @start and @end.
+ */
+void
+gst_value_set_int_range (GValue * value, gint start, gint end)
+{
+  gst_value_set_int_range_step (value, start, end, 1);
 }
 
 /**
@@ -800,7 +983,7 @@ gst_value_get_int_range_min (const GValue * value)
 {
   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
 
-  return value->data[0].v_int;
+  return INT_RANGE_MIN (value) * INT_RANGE_STEP (value);
 }
 
 /**
@@ -816,31 +999,81 @@ gst_value_get_int_range_max (const GValue * value)
 {
   g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
 
-  return value->data[1].v_int;
+  return INT_RANGE_MAX (value) * INT_RANGE_STEP (value);
+}
+
+/**
+ * gst_value_get_int_range_step:
+ * @value: a GValue initialized to GST_TYPE_INT_RANGE
+ *
+ * Gets the step of the range specified by @value.
+ *
+ * Returns: the step of the range
+ */
+gint
+gst_value_get_int_range_step (const GValue * value)
+{
+  g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value), 0);
+
+  return INT_RANGE_STEP (value);
 }
 
 static void
 gst_value_transform_int_range_string (const GValue * src_value,
     GValue * dest_value)
 {
-  dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
-      (int) src_value->data[0].v_int, (int) src_value->data[1].v_int);
+  if (INT_RANGE_STEP (src_value) == 1)
+    dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d]",
+        INT_RANGE_MIN (src_value), INT_RANGE_MAX (src_value));
+  else
+    dest_value->data[0].v_pointer = g_strdup_printf ("[%d,%d,%d]",
+        INT_RANGE_MIN (src_value) * INT_RANGE_STEP (src_value),
+        INT_RANGE_MAX (src_value) * INT_RANGE_STEP (src_value),
+        INT_RANGE_STEP (src_value));
 }
 
 static gint
 gst_value_compare_int_range (const GValue * value1, const GValue * value2)
 {
-  if (value2->data[0].v_int == value1->data[0].v_int &&
-      value2->data[1].v_int == value1->data[1].v_int)
+  /* calculate the number of values in each range */
+  gint n1 = INT_RANGE_MAX (value1) - INT_RANGE_MIN (value1) + 1;
+  gint n2 = INT_RANGE_MAX (value2) - INT_RANGE_MIN (value2) + 1;
+
+  /* they must be equal */
+  if (n1 != n2)
+    return GST_VALUE_UNORDERED;
+
+  /* if empty, equal */
+  if (n1 == 0)
     return GST_VALUE_EQUAL;
-  return GST_VALUE_UNORDERED;
+
+  /* if more than one value, then it is only equal if the step is equal
+     and bounds lie on the same value */
+  if (n1 > 1) {
+    if (INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2) &&
+        INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2) &&
+        INT_RANGE_STEP (value1) == INT_RANGE_STEP (value2)) {
+      return GST_VALUE_EQUAL;
+    }
+    return GST_VALUE_UNORDERED;
+  } else {
+    /* if just one, only if the value is equal */
+    if (INT_RANGE_MIN (value1) == INT_RANGE_MIN (value2))
+      return GST_VALUE_EQUAL;
+    return GST_VALUE_UNORDERED;
+  }
 }
 
 static gchar *
 gst_value_serialize_int_range (const GValue * value)
 {
-  return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
-      value->data[1].v_int);
+  if (INT_RANGE_STEP (value) == 1)
+    return g_strdup_printf ("[ %d, %d ]", INT_RANGE_MIN (value),
+        INT_RANGE_MAX (value));
+  else
+    return g_strdup_printf ("[ %d, %d, %d ]",
+        INT_RANGE_MIN (value) * INT_RANGE_STEP (value),
+        INT_RANGE_MAX (value) * INT_RANGE_STEP (value), INT_RANGE_STEP (value));
 }
 
 static gboolean
@@ -852,26 +1085,57 @@ gst_value_deserialize_int_range (GValue * dest, const gchar * s)
 
 /***************
  * int64 range *
+ *
+ * Values in the range are defined as any value greater or equal
+ * to min*step, AND lesser or equal to max*step.
+ * For step == 1, this falls back to the traditional range semantics.
  ***************/
 
+#define INT64_RANGE_MIN(v) (((gint64 *)((v)->data[0].v_pointer))[0])
+#define INT64_RANGE_MAX(v) (((gint64 *)((v)->data[0].v_pointer))[1])
+#define INT64_RANGE_STEP(v) (((gint64 *)((v)->data[0].v_pointer))[2])
+
 static void
 gst_value_init_int64_range (GValue * value)
 {
-  value->data[0].v_int64 = 0;
-  value->data[1].v_int64 = 0;
+  gint64 *vals = g_slice_alloc0 (3 * sizeof (gint64));
+  value->data[0].v_pointer = vals;
+  INT64_RANGE_MIN (value) = 0;
+  INT64_RANGE_MAX (value) = 0;
+  INT64_RANGE_STEP (value) = 1;
+}
+
+static void
+gst_value_free_int64_range (GValue * value)
+{
+  g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
+  g_slice_free1 (3 * sizeof (gint64), value->data[0].v_pointer);
+  value->data[0].v_pointer = NULL;
 }
 
 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;
+  gint64 *vals = (gint64 *) dest_value->data[0].v_pointer;
+  gint64 *src_vals = (gint64 *) src_value->data[0].v_pointer;
+
+  if (vals == NULL) {
+    gst_value_init_int64_range (dest_value);
+  }
+
+  if (src_vals != NULL) {
+    INT64_RANGE_MIN (dest_value) = INT64_RANGE_MIN (src_value);
+    INT64_RANGE_MAX (dest_value) = INT64_RANGE_MAX (src_value);
+    INT64_RANGE_STEP (dest_value) = INT64_RANGE_STEP (src_value);
+  }
 }
 
 static gchar *
 gst_value_collect_int64_range (GValue * value, guint n_collect_values,
     GTypeCValue * collect_values, guint collect_flags)
 {
+  gint64 *vals = value->data[0].v_pointer;
+
   if (n_collect_values != 2)
     return g_strdup_printf ("not enough value locations for `%s' passed",
         G_VALUE_TYPE_NAME (value));
@@ -879,8 +1143,12 @@ gst_value_collect_int64_range (GValue * value, guint n_collect_values,
     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;
+  if (vals == NULL) {
+    gst_value_init_int64_range (value);
+  }
+
+  gst_value_set_int64_range_step (value, collect_values[0].v_int64,
+      collect_values[1].v_int64, 1);
 
   return NULL;
 }
@@ -891,6 +1159,8 @@ gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
 {
   guint64 *int_range_start = collect_values[0].v_pointer;
   guint64 *int_range_end = collect_values[1].v_pointer;
+  guint64 *int_range_step = collect_values[2].v_pointer;
+  gint64 *vals = (gint64 *) value->data[0].v_pointer;
 
   if (!int_range_start)
     return g_strdup_printf ("start value location for `%s' passed as NULL",
@@ -898,31 +1168,58 @@ gst_value_lcopy_int64_range (const GValue * value, guint n_collect_values,
   if (!int_range_end)
     return g_strdup_printf ("end value location for `%s' passed as NULL",
         G_VALUE_TYPE_NAME (value));
+  if (!int_range_step)
+    return g_strdup_printf ("step value location for `%s' passed as NULL",
+        G_VALUE_TYPE_NAME (value));
+
+  if (G_UNLIKELY (vals == NULL)) {
+    return g_strdup_printf ("Uninitialised `%s' passed",
+        G_VALUE_TYPE_NAME (value));
+  }
 
-  *int_range_start = value->data[0].v_int64;
-  *int_range_end = value->data[1].v_int64;
+  *int_range_start = INT64_RANGE_MIN (value);
+  *int_range_end = INT64_RANGE_MAX (value);
+  *int_range_step = INT64_RANGE_STEP (value);
 
   return NULL;
 }
 
 /**
- * gst_value_set_int64_range:
+ * gst_value_set_int64_range_step:
  * @value: a GValue initialized to GST_TYPE_INT64_RANGE
  * @start: the start of the range
  * @end: the end of the range
+ * @step: the step of the range
  *
- * Sets @value to the range specified by @start and @end.
- *
- * Since: 0.10.31
+ * Sets @value to the range specified by @start, @end and @step.
  */
 void
-gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
+gst_value_set_int64_range_step (GValue * value, gint64 start, gint64 end,
+    gint64 step)
 {
   g_return_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value));
   g_return_if_fail (start < end);
+  g_return_if_fail (step > 0);
+  g_return_if_fail (start % step == 0);
+  g_return_if_fail (end % step == 0);
+
+  INT64_RANGE_MIN (value) = start / step;
+  INT64_RANGE_MAX (value) = end / step;
+  INT64_RANGE_STEP (value) = step;
+}
 
-  value->data[0].v_int64 = start;
-  value->data[1].v_int64 = end;
+/**
+ * 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.
+ */
+void
+gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
+{
+  gst_value_set_int64_range_step (value, start, end, 1);
 }
 
 /**
@@ -932,15 +1229,13 @@ gst_value_set_int64_range (GValue * value, gint64 start, gint64 end)
  * 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;
+  return INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value);
 }
 
 /**
@@ -950,40 +1245,92 @@ gst_value_get_int64_range_min (const GValue * value)
  * 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;
+  return INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value);
+}
+
+/**
+ * gst_value_get_int64_range_step:
+ * @value: a GValue initialized to GST_TYPE_INT64_RANGE
+ *
+ * Gets the step of the range specified by @value.
+ *
+ * Returns: the step of the range
+ */
+gint64
+gst_value_get_int64_range_step (const GValue * value)
+{
+  g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value), 0);
+
+  return INT64_RANGE_STEP (value);
 }
 
 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);
+  if (INT64_RANGE_STEP (src_value) == 1)
+    dest_value->data[0].v_pointer =
+        g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT "]",
+        INT64_RANGE_MIN (src_value), INT64_RANGE_MAX (src_value));
+  else
+    dest_value->data[0].v_pointer =
+        g_strdup_printf ("(gint64)[%" G_GINT64_FORMAT ",%" G_GINT64_FORMAT
+        ",%" G_GINT64_FORMAT "]",
+        INT64_RANGE_MIN (src_value) * INT64_RANGE_STEP (src_value),
+        INT64_RANGE_MAX (src_value) * INT64_RANGE_STEP (src_value),
+        INT64_RANGE_STEP (src_value));
 }
 
 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)
+  /* calculate the number of values in each range */
+  gint64 n1 = INT64_RANGE_MAX (value1) - INT64_RANGE_MIN (value1) + 1;
+  gint64 n2 = INT64_RANGE_MAX (value2) - INT64_RANGE_MIN (value2) + 1;
+
+  /* they must be equal */
+  if (n1 != n2)
+    return GST_VALUE_UNORDERED;
+
+  /* if empty, equal */
+  if (n1 == 0)
     return GST_VALUE_EQUAL;
-  return GST_VALUE_UNORDERED;
+
+  /* if more than one value, then it is only equal if the step is equal
+     and bounds lie on the same value */
+  if (n1 > 1) {
+    if (INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2) &&
+        INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2) &&
+        INT64_RANGE_STEP (value1) == INT64_RANGE_STEP (value2)) {
+      return GST_VALUE_EQUAL;
+    }
+    return GST_VALUE_UNORDERED;
+  } else {
+    /* if just one, only if the value is equal */
+    if (INT64_RANGE_MIN (value1) == INT64_RANGE_MIN (value2))
+      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);
+  if (INT64_RANGE_STEP (value) == 1)
+    return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT " ]",
+        INT64_RANGE_MIN (value), INT64_RANGE_MAX (value));
+  else
+    return g_strdup_printf ("[ %" G_GINT64_FORMAT ", %" G_GINT64_FORMAT ", %"
+        G_GINT64_FORMAT " ]",
+        INT64_RANGE_MIN (value) * INT64_RANGE_STEP (value),
+        INT64_RANGE_MAX (value) * INT64_RANGE_STEP (value),
+        INT64_RANGE_STEP (value));
 }
 
 static gboolean
@@ -1487,6 +1834,77 @@ gst_value_deserialize_caps (GValue * dest, const gchar * s)
   return FALSE;
 }
 
+/**************
+ * GstSegment *
+ **************/
+
+static gchar *
+gst_value_serialize_segment_internal (const GValue * value, gboolean escape)
+{
+  GstSegment *seg = g_value_get_boxed (value);
+  gchar *t, *res;
+  GstStructure *s;
+
+  /* FIXME: serialize segment offset as well ? */
+  s = gst_structure_new ("GstSegment",
+      "flags", GST_TYPE_SEGMENT_FLAGS, seg->flags,
+      "rate", G_TYPE_DOUBLE, seg->rate,
+      "applied-rate", G_TYPE_DOUBLE, seg->applied_rate,
+      "format", GST_TYPE_FORMAT, seg->format,
+      "base", G_TYPE_UINT64, seg->base,
+      "start", G_TYPE_UINT64, seg->start,
+      "stop", G_TYPE_UINT64, seg->stop,
+      "time", G_TYPE_UINT64, seg->time,
+      "position", G_TYPE_UINT64, seg->position,
+      "duration", G_TYPE_UINT64, seg->duration, NULL);
+  t = gst_structure_to_string (s);
+  if (escape) {
+    res = g_strdup_printf ("\"%s\"", t);
+    g_free (t);
+  } else {
+    res = t;
+  }
+  gst_structure_free (s);
+
+  return res;
+}
+
+static gchar *
+gst_value_serialize_segment (const GValue * value)
+{
+  return gst_value_serialize_segment_internal (value, TRUE);
+}
+
+static gboolean
+gst_value_deserialize_segment (GValue * dest, const gchar * s)
+{
+  GstStructure *str;
+  GstSegment seg;
+  gboolean res;
+
+  str = gst_structure_from_string (s, NULL);
+  if (str == NULL)
+    return FALSE;
+
+  res = gst_structure_get (str,
+      "flags", GST_TYPE_SEGMENT_FLAGS, &seg.flags,
+      "rate", G_TYPE_DOUBLE, &seg.rate,
+      "applied-rate", G_TYPE_DOUBLE, &seg.applied_rate,
+      "format", GST_TYPE_FORMAT, &seg.format,
+      "base", G_TYPE_UINT64, &seg.base,
+      "start", G_TYPE_UINT64, &seg.start,
+      "stop", G_TYPE_UINT64, &seg.stop,
+      "time", G_TYPE_UINT64, &seg.time,
+      "position", G_TYPE_UINT64, &seg.position,
+      "duration", G_TYPE_UINT64, &seg.duration, NULL);
+  gst_structure_free (str);
+
+  if (res)
+    g_value_set_boxed (dest, &seg);
+
+  return res;
+}
+
 /****************
  * GstStructure *
  ****************/
@@ -1497,8 +1915,6 @@ gst_value_deserialize_caps (GValue * dest, const gchar * s)
  * @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)
@@ -1517,8 +1933,6 @@ gst_value_set_structure (GValue * value, const GstStructure * structure)
  * Gets the contents of @value.
  *
  * Returns: (transfer none): the contents of @value
- *
- * Since: 0.10.15
  */
 const GstStructure *
 gst_value_get_structure (const GValue * value)
@@ -1561,18 +1975,56 @@ gst_value_deserialize_structure (GValue * dest, const gchar * s)
   return FALSE;
 }
 
+/**************
+ * GstTagList *
+ **************/
+
+static gboolean
+gst_value_deserialize_tag_list (GValue * dest, const gchar * s)
+{
+  GstTagList *taglist;
+
+  if (*s != '"') {
+    taglist = gst_tag_list_new_from_string (s);
+  } else {
+    gchar *str = gst_string_unwrap (s);
+
+    if (G_UNLIKELY (!str))
+      return FALSE;
+
+    taglist = gst_tag_list_new_from_string (str);
+    g_free (str);
+  }
+
+  if (G_LIKELY (taglist != NULL)) {
+    g_value_take_boxed (dest, taglist);
+    return TRUE;
+  }
+  return FALSE;
+}
+
+static gchar *
+gst_value_serialize_tag_list (const GValue * value)
+{
+  GstTagList *taglist = g_value_get_boxed (value);
+
+  return gst_string_take_and_wrap (gst_tag_list_to_string (taglist));
+}
+
+
 /*************
  * GstBuffer *
  *************/
 
 static gint
-gst_value_compare_buffer (const GValue * value1, const GValue * value2)
+compare_buffer (GstBuffer * buf1, GstBuffer * buf2)
 {
-  GstBuffer *buf1 = gst_value_get_buffer (value1);
-  GstBuffer *buf2 = gst_value_get_buffer (value2);
   gsize size1, size2;
-  gpointer data1, data2;
-  gint result = GST_VALUE_UNORDERED;
+  GstMapInfo info1, info2;
+  gint result, mret;
+
+  if (buf1 == buf2)
+    return GST_VALUE_EQUAL;
 
   size1 = gst_buffer_get_size (buf1);
   size2 = gst_buffer_get_size (buf2);
@@ -1583,26 +2035,43 @@ gst_value_compare_buffer (const GValue * value1, const GValue * value2)
   if (size1 == 0)
     return GST_VALUE_EQUAL;
 
-  data1 = gst_buffer_map (buf1, &size1, NULL, GST_MAP_READ);
-  data2 = gst_buffer_map (buf2, &size2, NULL, GST_MAP_READ);
-  g_assert (data1);
-  g_assert (data2);
+  if (!gst_buffer_map (buf1, &info1, GST_MAP_READ))
+    return GST_VALUE_UNORDERED;
 
-  if (memcmp (data1, data2, size1) == 0)
+  if (!gst_buffer_map (buf2, &info2, GST_MAP_READ)) {
+    gst_buffer_unmap (buf1, &info1);
+    return GST_VALUE_UNORDERED;
+  }
+
+  mret = memcmp (info1.data, info2.data, info1.size);
+  if (mret == 0)
     result = GST_VALUE_EQUAL;
+  else if (mret < 0)
+    result = GST_VALUE_LESS_THAN;
+  else
+    result = GST_VALUE_GREATER_THAN;
 
-  gst_buffer_unmap (buf2, data2, size2);
-  gst_buffer_unmap (buf1, data1, size1);
+  gst_buffer_unmap (buf1, &info1);
+  gst_buffer_unmap (buf2, &info2);
 
   return result;
 }
 
+static gint
+gst_value_compare_buffer (const GValue * value1, const GValue * value2)
+{
+  GstBuffer *buf1 = gst_value_get_buffer (value1);
+  GstBuffer *buf2 = gst_value_get_buffer (value2);
+
+  return compare_buffer (buf1, buf2);
+}
+
 static gchar *
 gst_value_serialize_buffer (const GValue * value)
 {
+  GstMapInfo info;
   guint8 *data;
   gint i;
-  gsize size;
   gchar *string;
   GstBuffer *buffer;
 
@@ -1610,15 +2079,18 @@ gst_value_serialize_buffer (const GValue * value)
   if (buffer == NULL)
     return NULL;
 
-  data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
+  if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
+    return NULL;
+
+  data = info.data;
 
-  string = g_malloc (size * 2 + 1);
-  for (i = 0; i < size; i++) {
+  string = g_malloc (info.size * 2 + 1);
+  for (i = 0; i < info.size; i++) {
     sprintf (string + i * 2, "%02x", data[i]);
   }
-  string[size * 2] = 0;
+  string[info.size * 2] = 0;
 
-  gst_buffer_unmap (buffer, data, size);
+  gst_buffer_unmap (buffer, &info);
 
   return string;
 }
@@ -1629,16 +2101,18 @@ gst_value_deserialize_buffer (GValue * dest, const gchar * s)
   GstBuffer *buffer;
   gint len;
   gchar ts[3];
+  GstMapInfo info;
   guint8 *data;
   gint i;
-  gsize size;
 
   len = strlen (s);
   if (len & 1)
     goto wrong_length;
 
-  buffer = gst_buffer_new_allocate (NULL, len / 2, 0);
-  data = gst_buffer_map (buffer, &size, NULL, GST_MAP_WRITE);
+  buffer = gst_buffer_new_allocate (NULL, len / 2, NULL);
+  if (!gst_buffer_map (buffer, &info, GST_MAP_WRITE))
+    goto map_failed;
+  data = info.data;
 
   for (i = 0; i < len / 2; i++) {
     if (!isxdigit ((int) s[i * 2]) || !isxdigit ((int) s[i * 2 + 1]))
@@ -1650,7 +2124,7 @@ gst_value_deserialize_buffer (GValue * dest, const gchar * s)
 
     data[i] = (guint8) strtoul (ts, NULL, 16);
   }
-  gst_buffer_unmap (buffer, data, size);
+  gst_buffer_unmap (buffer, &info);
 
   gst_value_take_buffer (dest, buffer);
 
@@ -1661,14 +2135,175 @@ wrong_length:
   {
     return FALSE;
   }
+map_failed:
+  {
+    return FALSE;
+  }
 wrong_char:
   {
     gst_buffer_unref (buffer);
-    gst_buffer_unmap (buffer, data, size);
+    gst_buffer_unmap (buffer, &info);
     return FALSE;
   }
 }
 
+/*************
+ * GstSample *
+ *************/
+
+/* This function is mostly used for comparing image/buffer tags in taglists */
+static gint
+gst_value_compare_sample (const GValue * value1, const GValue * value2)
+{
+  GstBuffer *buf1 = gst_sample_get_buffer (gst_value_get_sample (value1));
+  GstBuffer *buf2 = gst_sample_get_buffer (gst_value_get_sample (value2));
+
+  /* FIXME: should we take into account anything else such as caps? */
+  return compare_buffer (buf1, buf2);
+}
+
+static gchar *
+gst_value_serialize_sample (const GValue * value)
+{
+  const GstStructure *info_structure;
+  GstSegment *segment;
+  GstBuffer *buffer;
+  GstCaps *caps;
+  GstSample *sample;
+  GValue val = { 0, };
+  gchar *info_str, *caps_str, *tmp;
+  gchar *buf_str, *seg_str, *s;
+
+  sample = g_value_get_boxed (value);
+
+  buffer = gst_sample_get_buffer (sample);
+  if (buffer) {
+    g_value_init (&val, GST_TYPE_BUFFER);
+    g_value_set_boxed (&val, buffer);
+    buf_str = gst_value_serialize_buffer (&val);
+    g_value_unset (&val);
+  } else {
+    buf_str = g_strdup ("None");
+  }
+
+  caps = gst_sample_get_caps (sample);
+  if (caps) {
+    tmp = gst_caps_to_string (caps);
+    caps_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
+    g_strdelimit (caps_str, "=", '_');
+    g_free (tmp);
+  } else {
+    caps_str = g_strdup ("None");
+  }
+
+  segment = gst_sample_get_segment (sample);
+  if (segment) {
+    g_value_init (&val, GST_TYPE_SEGMENT);
+    g_value_set_boxed (&val, segment);
+    tmp = gst_value_serialize_segment_internal (&val, FALSE);
+    seg_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
+    g_strdelimit (seg_str, "=", '_');
+    g_free (tmp);
+    g_value_unset (&val);
+  } else {
+    seg_str = g_strdup ("None");
+  }
+
+  info_structure = gst_sample_get_info (sample);
+  if (info_structure) {
+    tmp = gst_structure_to_string (info_structure);
+    info_str = g_base64_encode ((guchar *) tmp, strlen (tmp) + 1);
+    g_strdelimit (info_str, "=", '_');
+    g_free (tmp);
+  } else {
+    info_str = g_strdup ("None");
+  }
+
+  s = g_strconcat (buf_str, ":", caps_str, ":", seg_str, ":", info_str, NULL);
+  g_free (buf_str);
+  g_free (caps_str);
+  g_free (seg_str);
+  g_free (info_str);
+
+  return s;
+}
+
+static gboolean
+gst_value_deserialize_sample (GValue * dest, const gchar * s)
+{
+  GValue bval = G_VALUE_INIT, sval = G_VALUE_INIT;
+  GstStructure *info;
+  GstSample *sample;
+  GstCaps *caps;
+  gboolean ret = FALSE;
+  gchar **fields;
+  gsize outlen;
+  gint len;
+
+  GST_TRACE ("deserialize '%s'", s);
+
+  fields = g_strsplit (s, ":", -1);
+  len = g_strv_length (fields);
+  if (len != 4)
+    goto wrong_length;
+
+  g_value_init (&bval, GST_TYPE_BUFFER);
+  g_value_init (&sval, GST_TYPE_SEGMENT);
+
+  if (!gst_value_deserialize_buffer (&bval, fields[0]))
+    goto fail;
+
+  if (strcmp (fields[1], "None") != 0) {
+    g_strdelimit (fields[1], "_", '=');
+    g_base64_decode_inplace (fields[1], &outlen);
+    GST_TRACE ("caps    : %s", fields[1]);
+    caps = gst_caps_from_string (fields[1]);
+    if (caps == NULL)
+      goto fail;
+  } else {
+    caps = NULL;
+  }
+
+  if (strcmp (fields[2], "None") != 0) {
+    g_strdelimit (fields[2], "_", '=');
+    g_base64_decode_inplace (fields[2], &outlen);
+    GST_TRACE ("segment : %s", fields[2]);
+    if (!gst_value_deserialize_segment (&sval, fields[2]))
+      goto fail;
+  }
+
+  if (strcmp (fields[3], "None") != 0) {
+    g_strdelimit (fields[3], "_", '=');
+    g_base64_decode_inplace (fields[3], &outlen);
+    GST_TRACE ("info    : %s", fields[3]);
+    info = gst_structure_from_string (fields[3], NULL);
+    if (info == NULL)
+      goto fail;
+  } else {
+    info = NULL;
+  }
+
+  sample = gst_sample_new (gst_value_get_buffer (&bval), caps,
+      g_value_get_boxed (&sval), info);
+
+  g_value_take_boxed (dest, sample);
+
+  if (caps)
+    gst_caps_unref (caps);
+
+  ret = TRUE;
+
+fail:
+
+  g_value_unset (&bval);
+  g_value_unset (&sval);
+
+wrong_length:
+
+  g_strfreev (fields);
+
+  return ret;
+}
 
 /***********
  * boolean *
@@ -2366,7 +3001,10 @@ gst_value_serialize_flags (const GValue * 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);
+    if (fl)
+      return g_strdup (fl->value_name);
+    else
+      return g_strdup ("0");
   }
 
   /* some flags are set, so serialize one by one */
@@ -2429,6 +3067,123 @@ gst_value_deserialize_flags (GValue * dest, const gchar * s)
   return TRUE;
 }
 
+/****************
+ * subset *
+ ****************/
+
+static gboolean
+gst_value_is_subset_int_range_int_range (const GValue * value1,
+    const GValue * value2)
+{
+  gint gcd;
+
+  g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value1), FALSE);
+  g_return_val_if_fail (GST_VALUE_HOLDS_INT_RANGE (value2), FALSE);
+
+  if (INT_RANGE_MIN (value1) * INT_RANGE_STEP (value1) <
+      INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2))
+    return FALSE;
+  if (INT_RANGE_MAX (value1) * INT_RANGE_STEP (value1) >
+      INT_RANGE_MAX (value2) * INT_RANGE_STEP (value2))
+    return FALSE;
+
+  if (INT_RANGE_MIN (value2) == INT_RANGE_MAX (value2)) {
+    if ((INT_RANGE_MIN (value2) * INT_RANGE_STEP (value2)) %
+        INT_RANGE_STEP (value1))
+      return FALSE;
+    return TRUE;
+  }
+
+  gcd =
+      gst_util_greatest_common_divisor (INT_RANGE_STEP (value1),
+      INT_RANGE_STEP (value2));
+  if (gcd != MIN (INT_RANGE_STEP (value1), INT_RANGE_STEP (value2)))
+    return FALSE;
+
+  return TRUE;
+}
+
+static gboolean
+gst_value_is_subset_int64_range_int64_range (const GValue * value1,
+    const GValue * value2)
+{
+  gint64 gcd;
+
+  g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value1), FALSE);
+  g_return_val_if_fail (GST_VALUE_HOLDS_INT64_RANGE (value2), FALSE);
+
+  if (INT64_RANGE_MIN (value1) < INT64_RANGE_MIN (value2))
+    return FALSE;
+  if (INT64_RANGE_MAX (value1) > INT64_RANGE_MAX (value2))
+    return FALSE;
+
+  if (INT64_RANGE_MIN (value2) == INT64_RANGE_MAX (value2)) {
+    if ((INT64_RANGE_MIN (value2) * INT64_RANGE_STEP (value2)) %
+        INT64_RANGE_STEP (value1))
+      return FALSE;
+    return TRUE;
+  }
+
+  gcd =
+      gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (value1),
+      INT64_RANGE_STEP (value2));
+  if (gcd != MIN (INT64_RANGE_STEP (value1), INT64_RANGE_STEP (value2)))
+    return FALSE;
+
+  return TRUE;
+}
+
+/**
+ * gst_value_is_subset:
+ * @value1: a #GValue
+ * @value2: a #GValue
+ *
+ * Check that @value1 is a subset of @value2.
+ *
+ * Return: %TRUE is @value1 is a subset of @value2
+ */
+gboolean
+gst_value_is_subset (const GValue * value1, const GValue * value2)
+{
+  /* special case for int/int64 ranges, since we cannot compute
+     the difference for those when they have different steps,
+     and it's actually a lot simpler to compute whether a range
+     is a subset of another. */
+  if (GST_VALUE_HOLDS_INT_RANGE (value1) && GST_VALUE_HOLDS_INT_RANGE (value2)) {
+    return gst_value_is_subset_int_range_int_range (value1, value2);
+  } else if (GST_VALUE_HOLDS_INT64_RANGE (value1)
+      && GST_VALUE_HOLDS_INT64_RANGE (value2)) {
+    return gst_value_is_subset_int64_range_int64_range (value1, value2);
+  }
+
+  /*
+   * 1 - [1,2] = empty
+   * -> !subset
+   *
+   * [1,2] - 1 = 2
+   *  -> 1 - [1,2] = empty
+   *  -> subset
+   *
+   * [1,3] - [1,2] = 3
+   * -> [1,2] - [1,3] = empty
+   * -> subset
+   *
+   * {1,2} - {1,3} = 2
+   * -> {1,3} - {1,2} = 3
+   * -> !subset
+   *
+   *  First caps subtraction needs to return a non-empty set, second
+   *  subtractions needs to give en empty set.
+   *  Both substractions are switched below, as it's faster that way.
+   */
+  if (!gst_value_subtract (NULL, value1, value2)) {
+    if (gst_value_subtract (NULL, value2, value1)) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
 /*********
  * union *
  *********/
@@ -2437,11 +3192,33 @@ static gboolean
 gst_value_union_int_int_range (GValue * dest, const GValue * src1,
     const GValue * src2)
 {
-  if (src2->data[0].v_int <= src1->data[0].v_int &&
-      src2->data[1].v_int >= src1->data[0].v_int) {
-    gst_value_init_and_copy (dest, src2);
+  gint v = src1->data[0].v_int;
+
+  /* check if it's already in the range */
+  if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= v &&
+      INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= v &&
+      v % INT_RANGE_STEP (src2) == 0) {
+    if (dest)
+      gst_value_init_and_copy (dest, src2);
+    return TRUE;
+  }
+
+  /* check if it extends the range */
+  if (v == (INT_RANGE_MIN (src2) - 1) * INT_RANGE_STEP (src2)) {
+    if (dest) {
+      gst_value_init_and_copy (dest, src2);
+      --INT_RANGE_MIN (src2);
+    }
+    return TRUE;
+  }
+  if (v == (INT_RANGE_MAX (src2) + 1) * INT_RANGE_STEP (src2)) {
+    if (dest) {
+      gst_value_init_and_copy (dest, src2);
+      ++INT_RANGE_MAX (src2);
+    }
     return TRUE;
   }
+
   return FALSE;
 }
 
@@ -2449,20 +3226,77 @@ static gboolean
 gst_value_union_int_range_int_range (GValue * dest, const GValue * src1,
     const GValue * src2)
 {
-  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);
+  /* We can union in several special cases:
+     1 - one is a subset of another
+     2 - same step and not disjoint
+     3 - different step, at least one with one value which matches a 'next' or 'previous'
+     - anything else ?
+   */
 
-  if (min <= max) {
-    g_value_init (dest, GST_TYPE_INT_RANGE);
-    gst_value_set_int_range (dest,
-        MIN (src1->data[0].v_int, src2->data[0].v_int),
-        MAX (src1->data[1].v_int, src2->data[1].v_int));
+  /* 1 - subset */
+  if (gst_value_is_subset_int_range_int_range (src1, src2)) {
+    if (dest)
+      gst_value_init_and_copy (dest, src2);
+    return TRUE;
+  }
+  if (gst_value_is_subset_int_range_int_range (src2, src1)) {
+    if (dest)
+      gst_value_init_and_copy (dest, src1);
     return TRUE;
   }
 
+  /* 2 - same step and not disjoint */
+  if (INT_RANGE_STEP (src1) == INT_RANGE_STEP (src2)) {
+    if ((INT_RANGE_MIN (src1) <= INT_RANGE_MAX (src2) + 1 &&
+            INT_RANGE_MAX (src1) >= INT_RANGE_MIN (src2) - 1) ||
+        (INT_RANGE_MIN (src2) <= INT_RANGE_MAX (src1) + 1 &&
+            INT_RANGE_MAX (src2) >= INT_RANGE_MIN (src1) - 1)) {
+      if (dest) {
+        gint step = INT_RANGE_STEP (src1);
+        gint min = step * MIN (INT_RANGE_MIN (src1), INT_RANGE_MIN (src2));
+        gint max = step * MAX (INT_RANGE_MAX (src1), INT_RANGE_MAX (src2));
+        g_value_init (dest, GST_TYPE_INT_RANGE);
+        gst_value_set_int_range_step (dest, min, max, step);
+      }
+      return TRUE;
+    }
+  }
+
+  /* 3 - single value matches next or previous */
+  if (INT_RANGE_STEP (src1) != INT_RANGE_STEP (src2)) {
+    gint n1 = INT_RANGE_MAX (src1) - INT_RANGE_MIN (src1) + 1;
+    gint n2 = INT_RANGE_MAX (src2) - INT_RANGE_MIN (src2) + 1;
+    if (n1 == 1 || n2 == 1) {
+      const GValue *range_value = NULL;
+      gint scalar = 0;
+      if (n1 == 1) {
+        range_value = src2;
+        scalar = INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1);
+      } else if (n2 == 1) {
+        range_value = src1;
+        scalar = INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2);
+      }
+
+      if (scalar ==
+          (INT_RANGE_MIN (range_value) - 1) * INT_RANGE_STEP (range_value)) {
+        if (dest) {
+          gst_value_init_and_copy (dest, range_value);
+          --INT_RANGE_MIN (range_value);
+        }
+        return TRUE;
+      } else if (scalar ==
+          (INT_RANGE_MAX (range_value) + 1) * INT_RANGE_STEP (range_value)) {
+        if (dest) {
+          gst_value_init_and_copy (dest, range_value);
+          ++INT_RANGE_MIN (range_value);
+        }
+        return TRUE;
+      }
+    }
+  }
+
+  /* If we get there, we did not find a way to make a union that can be
+     represented with our simplistic model. */
   return FALSE;
 }
 
@@ -2474,8 +3308,9 @@ static gboolean
 gst_value_intersect_int_int_range (GValue * dest, const GValue * src1,
     const GValue * src2)
 {
-  if (src2->data[0].v_int <= src1->data[0].v_int &&
-      src2->data[1].v_int >= src1->data[0].v_int) {
+  if (INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2) <= src1->data[0].v_int &&
+      INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2) >= src1->data[0].v_int &&
+      src1->data[0].v_int % INT_RANGE_STEP (src2) == 0) {
     if (dest)
       gst_value_init_and_copy (dest, src1);
     return TRUE;
@@ -2490,14 +3325,29 @@ gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
 {
   gint min;
   gint max;
+  gint step;
 
-  min = MAX (src1->data[0].v_int, src2->data[0].v_int);
-  max = MIN (src1->data[1].v_int, src2->data[1].v_int);
+  step =
+      INT_RANGE_STEP (src1) /
+      gst_util_greatest_common_divisor (INT_RANGE_STEP (src1),
+      INT_RANGE_STEP (src2));
+  if (G_MAXINT32 / INT_RANGE_STEP (src2) < step)
+    return FALSE;
+  step *= INT_RANGE_STEP (src2);
+
+  min =
+      MAX (INT_RANGE_MIN (src1) * INT_RANGE_STEP (src1),
+      INT_RANGE_MIN (src2) * INT_RANGE_STEP (src2));
+  min = (min + step - 1) / step * step;
+  max =
+      MIN (INT_RANGE_MAX (src1) * INT_RANGE_STEP (src1),
+      INT_RANGE_MAX (src2) * INT_RANGE_STEP (src2));
+  max = max / step * step;
 
   if (min < max) {
     if (dest) {
       g_value_init (dest, GST_TYPE_INT_RANGE);
-      gst_value_set_int_range (dest, min, max);
+      gst_value_set_int_range_step (dest, min, max, step);
     }
     return TRUE;
   }
@@ -2512,12 +3362,16 @@ gst_value_intersect_int_range_int_range (GValue * dest, const GValue * src1,
   return FALSE;
 }
 
+#define INT64_RANGE_MIN_VAL(v) (INT64_RANGE_MIN (v) * INT64_RANGE_STEP (v))
+#define INT64_RANGE_MAX_VAL(v) (INT64_RANGE_MAX (v) * INT64_RANGE_STEP (v))
+
 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) {
+  if (INT64_RANGE_MIN_VAL (src2) <= src1->data[0].v_int64 &&
+      INT64_RANGE_MAX_VAL (src2) >= src1->data[0].v_int64 &&
+      src1->data[0].v_int64 % INT64_RANGE_STEP (src2) == 0) {
     if (dest)
       gst_value_init_and_copy (dest, src1);
     return TRUE;
@@ -2532,14 +3386,29 @@ gst_value_intersect_int64_range_int64_range (GValue * dest, const GValue * src1,
 {
   gint64 min;
   gint64 max;
+  gint64 step;
+
+  step =
+      INT64_RANGE_STEP (src1) /
+      gst_util_greatest_common_divisor_int64 (INT64_RANGE_STEP (src1),
+      INT64_RANGE_STEP (src2));
+  if (G_MAXINT64 / INT64_RANGE_STEP (src2) < step)
+    return FALSE;
+  step *= INT64_RANGE_STEP (src2);
 
-  min = MAX (src1->data[0].v_int64, src2->data[0].v_int64);
-  max = MIN (src1->data[1].v_int64, src2->data[1].v_int64);
+  min =
+      MAX (INT64_RANGE_MIN (src1) * INT64_RANGE_STEP (src1),
+      INT64_RANGE_MIN (src2) * INT64_RANGE_STEP (src2));
+  min = (min + step - 1) / step * step;
+  max =
+      MIN (INT64_RANGE_MAX (src1) * INT64_RANGE_STEP (src1),
+      INT64_RANGE_MAX (src2) * INT64_RANGE_STEP (src2));
+  max = max / step * step;
 
   if (min < max) {
     if (dest) {
       g_value_init (dest, GST_TYPE_INT64_RANGE);
-      gst_value_set_int64_range (dest, min, max);
+      gst_value_set_int64_range_step (dest, min, max, step);
     }
     return TRUE;
   }
@@ -2629,7 +3498,7 @@ gst_value_intersect_list (GValue * dest, const GValue * value1,
 
         gst_value_init_and_copy (&temp, dest);
         g_value_unset (dest);
-        gst_value_list_concat (dest, &temp, &intersection);
+        gst_value_list_merge (dest, &temp, &intersection);
         g_value_unset (&temp);
       }
       g_value_unset (&intersection);
@@ -2768,11 +3637,12 @@ gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
 {
   gint min = gst_value_get_int_range_min (subtrahend);
   gint max = gst_value_get_int_range_max (subtrahend);
+  gint step = gst_value_get_int_range_step (subtrahend);
   gint val = g_value_get_int (minuend);
 
   /* subtracting a range from an int only works if the int is not in the
    * range */
-  if (val < min || val > max) {
+  if (val < min || val > max || val % step) {
     /* and the result is the int */
     if (dest)
       gst_value_init_and_copy (dest, minuend);
@@ -2785,12 +3655,18 @@ gst_value_subtract_int_int_range (GValue * dest, const GValue * minuend,
  */
 static gboolean
 gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
-    gint max2)
+    gint max2, gint step)
 {
   GValue v1 = { 0, };
   GValue v2 = { 0, };
   GValue *pv1, *pv2;            /* yeah, hungarian! */
 
+  g_return_val_if_fail (step > 0, FALSE);
+  g_return_val_if_fail (min1 % step == 0, FALSE);
+  g_return_val_if_fail (max1 % step == 0, FALSE);
+  g_return_val_if_fail (min2 % step == 0, FALSE);
+  g_return_val_if_fail (max2 % step == 0, FALSE);
+
   if (min1 <= max1 && min2 <= max2) {
     pv1 = &v1;
     pv2 = &v2;
@@ -2809,14 +3685,14 @@ gst_value_create_new_range (GValue * dest, gint min1, gint max1, gint min2,
 
   if (min1 < max1) {
     g_value_init (pv1, GST_TYPE_INT_RANGE);
-    gst_value_set_int_range (pv1, min1, max1);
+    gst_value_set_int_range_step (pv1, min1, max1, step);
   } else if (min1 == max1) {
     g_value_init (pv1, G_TYPE_INT);
     g_value_set_int (pv1, min1);
   }
   if (min2 < max2) {
     g_value_init (pv2, GST_TYPE_INT_RANGE);
-    gst_value_set_int_range (pv2, min2, max2);
+    gst_value_set_int_range_step (pv2, min2, max2, step);
   } else if (min2 == max2) {
     g_value_init (pv2, G_TYPE_INT);
     g_value_set_int (pv2, min2);
@@ -2836,28 +3712,29 @@ gst_value_subtract_int_range_int (GValue * dest, const GValue * minuend,
 {
   gint min = gst_value_get_int_range_min (minuend);
   gint max = gst_value_get_int_range_max (minuend);
+  gint step = gst_value_get_int_range_step (minuend);
   gint val = g_value_get_int (subtrahend);
 
   g_return_val_if_fail (min < max, FALSE);
 
   /* value is outside of the range, return range unchanged */
-  if (val < min || val > max) {
+  if (val < min || val > max || val % step) {
     if (dest)
       gst_value_init_and_copy (dest, minuend);
     return TRUE;
   } else {
     /* max must be MAXINT too as val <= max */
-    if (val == G_MAXINT) {
-      max--;
-      val--;
+    if (val >= G_MAXINT - step + 1) {
+      max -= step;
+      val -= step;
     }
     /* min must be MININT too as val >= max */
-    if (val == G_MININT) {
-      min++;
-      val++;
+    if (val <= G_MININT + step - 1) {
+      min += step;
+      val += step;
     }
     if (dest)
-      gst_value_create_new_range (dest, min, val - 1, val + 1, max);
+      gst_value_create_new_range (dest, min, val - step, val + step, max, step);
   }
   return TRUE;
 }
@@ -2868,18 +3745,30 @@ gst_value_subtract_int_range_int_range (GValue * dest, const GValue * minuend,
 {
   gint min1 = gst_value_get_int_range_min (minuend);
   gint max1 = gst_value_get_int_range_max (minuend);
+  gint step1 = gst_value_get_int_range_step (minuend);
   gint min2 = gst_value_get_int_range_min (subtrahend);
   gint max2 = gst_value_get_int_range_max (subtrahend);
+  gint step2 = gst_value_get_int_range_step (subtrahend);
+  gint step;
 
-  if (max2 == G_MAXINT && min2 == G_MININT) {
+  if (step1 != step2) {
+    /* ENOIMPL */
+    g_assert (FALSE);
     return FALSE;
-  } else if (max2 == G_MAXINT) {
-    return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1), 1, 0);
-  } else if (min2 == G_MININT) {
-    return gst_value_create_new_range (dest, MAX (max2 + 1, min1), max1, 1, 0);
+  }
+  step = step1;
+
+  if (max2 >= max1 && min2 <= min1) {
+    return FALSE;
+  } else if (max2 >= max1) {
+    return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
+        step, 0, step);
+  } else if (min2 <= min1) {
+    return gst_value_create_new_range (dest, MAX (max2 + step, min1), max1,
+        step, 0, step);
   } else {
-    return gst_value_create_new_range (dest, min1, MIN (min2 - 1, max1),
-        MAX (max2 + 1, min1), max1);
+    return gst_value_create_new_range (dest, min1, MIN (min2 - step, max1),
+        MAX (max2 + step, min1), max1, step);
   }
 }
 
@@ -2889,11 +3778,12 @@ gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
 {
   gint64 min = gst_value_get_int64_range_min (subtrahend);
   gint64 max = gst_value_get_int64_range_max (subtrahend);
+  gint64 step = gst_value_get_int64_range_step (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) {
+  if (val < min || val > max || val % step) {
     /* and the result is the int64 */
     if (dest)
       gst_value_init_and_copy (dest, minuend);
@@ -2906,12 +3796,18 @@ gst_value_subtract_int64_int64_range (GValue * dest, const GValue * minuend,
  */
 static gboolean
 gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
-    gint64 min2, gint64 max2)
+    gint64 min2, gint64 max2, gint64 step)
 {
   GValue v1 = { 0, };
   GValue v2 = { 0, };
   GValue *pv1, *pv2;            /* yeah, hungarian! */
 
+  g_return_val_if_fail (step > 0, FALSE);
+  g_return_val_if_fail (min1 % step == 0, FALSE);
+  g_return_val_if_fail (max1 % step == 0, FALSE);
+  g_return_val_if_fail (min2 % step == 0, FALSE);
+  g_return_val_if_fail (max2 % step == 0, FALSE);
+
   if (min1 <= max1 && min2 <= max2) {
     pv1 = &v1;
     pv2 = &v2;
@@ -2930,14 +3826,14 @@ gst_value_create_new_int64_range (GValue * dest, gint64 min1, gint64 max1,
 
   if (min1 < max1) {
     g_value_init (pv1, GST_TYPE_INT64_RANGE);
-    gst_value_set_int64_range (pv1, min1, max1);
+    gst_value_set_int64_range_step (pv1, min1, max1, step);
   } 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);
+    gst_value_set_int64_range_step (pv2, min2, max2, step);
   } else if (min2 == max2) {
     g_value_init (pv2, G_TYPE_INT64);
     g_value_set_int64 (pv2, min2);
@@ -2957,28 +3853,30 @@ gst_value_subtract_int64_range_int64 (GValue * dest, const GValue * minuend,
 {
   gint64 min = gst_value_get_int64_range_min (minuend);
   gint64 max = gst_value_get_int64_range_max (minuend);
+  gint64 step = gst_value_get_int64_range_step (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) {
+  if (val < min || val > max || val % step) {
     if (dest)
       gst_value_init_and_copy (dest, minuend);
     return TRUE;
   } else {
     /* max must be MAXINT64 too as val <= max */
-    if (val == G_MAXINT64) {
-      max--;
-      val--;
+    if (val >= G_MAXINT64 - step + 1) {
+      max -= step;
+      val -= step;
     }
     /* min must be MININT64 too as val >= max */
-    if (val == G_MININT64) {
-      min++;
-      val++;
+    if (val <= G_MININT64 + step - 1) {
+      min += step;
+      val += step;
     }
     if (dest)
-      gst_value_create_new_int64_range (dest, min, val - 1, val + 1, max);
+      gst_value_create_new_int64_range (dest, min, val - step, val + step, max,
+          step);
   }
   return TRUE;
 }
@@ -2989,20 +3887,30 @@ gst_value_subtract_int64_range_int64_range (GValue * dest,
 {
   gint64 min1 = gst_value_get_int64_range_min (minuend);
   gint64 max1 = gst_value_get_int64_range_max (minuend);
+  gint64 step1 = gst_value_get_int64_range_step (minuend);
   gint64 min2 = gst_value_get_int64_range_min (subtrahend);
   gint64 max2 = gst_value_get_int64_range_max (subtrahend);
+  gint64 step2 = gst_value_get_int64_range_step (subtrahend);
+  gint64 step;
 
-  if (max2 == G_MAXINT64 && min2 == G_MININT64) {
+  if (step1 != step2) {
+    /* ENOIMPL */
+    g_assert (FALSE);
     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);
+  }
+  step = step1;
+
+  if (max2 >= max1 && min2 <= min1) {
+    return FALSE;
+  } else if (max2 >= max1) {
+    return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
+            max1), step, 0, step);
+  } else if (min2 <= min1) {
+    return gst_value_create_new_int64_range (dest, MAX (max2 + step, min1),
+        max1, step, 0, step);
   } else {
-    return gst_value_create_new_int64_range (dest, min1, MIN (min2 - 1, max1),
-        MAX (max2 + 1, min1), max1);
+    return gst_value_create_new_int64_range (dest, min1, MIN (min2 - step,
+            max1), MAX (max2 + step, min1), max1, step);
   }
 }
 
@@ -3345,14 +4253,15 @@ gst_value_list_equals_range (const GValue * list, const GValue * value)
   if (CHECK_TYPES (INT, G)) {
     const gint rmin = gst_value_get_int_range_min (value);
     const gint rmax = gst_value_get_int_range_max (value);
+    const gint rstep = gst_value_get_int_range_step (value);
     /* note: this will overflow for min 0 and max INT_MAX, but this
        would only be equal to a list of INT_MAX elements, which seems
        very unlikely */
-    if (list_size != rmax - rmin + 1)
+    if (list_size != rmax / rstep - rmin / rstep + 1)
       return FALSE;
     for (n = 0; n < list_size; ++n) {
       gint v = g_value_get_int (VALUE_LIST_GET_VALUE (list, n));
-      if (v < rmin || v > rmax) {
+      if (v < rmin || v > rmax || v % rstep) {
         return FALSE;
       }
     }
@@ -3360,12 +4269,13 @@ gst_value_list_equals_range (const GValue * list, const GValue * value)
   } else if (CHECK_TYPES (INT64, G)) {
     const gint64 rmin = gst_value_get_int64_range_min (value);
     const gint64 rmax = gst_value_get_int64_range_max (value);
+    const gint64 rstep = gst_value_get_int64_range_step (value);
     GST_DEBUG ("List/range of int64s");
-    if (list_size != rmax - rmin + 1)
+    if (list_size != rmax / rstep - rmin / rstep + 1)
       return FALSE;
     for (n = 0; n < list_size; ++n) {
       gint64 v = g_value_get_int64 (VALUE_LIST_GET_VALUE (list, n));
-      if (v < rmin || v > rmax)
+      if (v < rmin || v > rmax || v % rstep)
         return FALSE;
     }
     return TRUE;
@@ -3509,34 +4419,32 @@ gst_value_can_union (const GValue * value1, const GValue * value2)
  *
  * Creates a GValue corresponding to the union of @value1 and @value2.
  *
- * Returns: always returns %TRUE
+ * Returns: TRUE if the union suceeded.
  */
-/* FIXME: change return type to 'void'? */
 gboolean
 gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
 {
-  GstValueUnionInfo *union_info;
+  const GstValueUnionInfo *union_info;
   guint i, len;
+  GType 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);
+  g_return_val_if_fail (gst_value_list_or_array_are_compatible (value1, value2),
+      FALSE);
 
   len = gst_value_union_funcs->len;
+  type1 = G_VALUE_TYPE (value1);
+  type2 = G_VALUE_TYPE (value2);
 
   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)) {
-      if (union_info->func (dest, value1, value2)) {
-        return TRUE;
-      }
+    if (union_info->type1 == type1 && union_info->type2 == type2) {
+      return union_info->func (dest, value1, value2);
     }
-    if (union_info->type1 == G_VALUE_TYPE (value2) &&
-        union_info->type2 == G_VALUE_TYPE (value1)) {
-      if (union_info->func (dest, value2, value1)) {
-        return TRUE;
-      }
+    if (union_info->type1 == type2 && union_info->type2 == type1) {
+      return union_info->func (dest, value2, value1);
     }
   }
 
@@ -3544,8 +4452,7 @@ gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
   return TRUE;
 }
 
-/**
- * gst_value_register_union_func: (skip)
+/* gst_value_register_union_func: (skip)
  * @type1: a type to union
  * @type2: another type to union
  * @func: a function that implements creating a union between the two types
@@ -3557,7 +4464,7 @@ gst_value_union (GValue * dest, const GValue * value1, const GValue * value2)
  * 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
+static void
 gst_value_register_union_func (GType type1, GType type2, GstValueUnionFunc func)
 {
   GstValueUnionInfo union_info;
@@ -3678,8 +4585,7 @@ gst_value_intersect (GValue * dest, const GValue * value1,
 
 
 
-/**
- * gst_value_register_intersect_func: (skip)
+/* gst_value_register_intersect_func: (skip)
  * @type1: the first type to intersect
  * @type2: the second type to intersect
  * @func: the intersection function
@@ -3692,7 +4598,7 @@ gst_value_intersect (GValue * dest, const GValue * value1,
  * cannot be used at the same time as gst_value_intersect() or
  * gst_value_can_intersect().
  */
-void
+static void
 gst_value_register_intersect_func (GType type1, GType type2,
     GstValueIntersectFunc func)
 {
@@ -3812,8 +4718,7 @@ gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
   return gst_value_can_compare (minuend, subtrahend);
 }
 
-/**
- * gst_value_register_subtract_func: (skip)
+/* gst_value_register_subtract_func: (skip)
  * @minuend_type: type of the minuend
  * @subtrahend_type: type of the subtrahend
  * @func: function to use
@@ -3825,7 +4730,7 @@ gst_value_can_subtract (const GValue * minuend, const GValue * subtrahend)
  * started, as gst_value_register_subtract_func() is not thread-safe and
  * cannot be used at the same time as gst_value_subtract().
  */
-void
+static void
 gst_value_register_subtract_func (GType minuend_type, GType subtrahend_type,
     GstValueSubtractFunc func)
 {
@@ -4519,7 +5424,6 @@ 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;
@@ -4531,64 +5435,37 @@ gst_value_compare_date_time (const GValue * value1, const GValue * value2)
     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;
+  /* returns GST_VALUE_* */
+  return __gst_date_time_compare (date1, date2);
 }
 
 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);
+  return __gst_date_time_serialize (date, TRUE);
 }
 
 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;
+  GstDateTime *datetime;
 
   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;
+  datetime = gst_date_time_new_from_iso8601_string (s);
+  if (datetime != NULL) {
+    g_value_take_boxed (dest, datetime);
+    return TRUE;
+  }
+  GST_WARNING ("Failed to deserialize date time string '%s'", s);
+  return FALSE;
 }
 
 static void
@@ -4749,8 +5626,10 @@ gst_value_intersect_bitmask_bitmask (GValue * dest, const GValue * src1,
   s1 = gst_value_get_bitmask (src1);
   s2 = gst_value_get_bitmask (src2);
 
-  g_value_init (dest, GST_TYPE_BITMASK);
-  gst_value_set_bitmask (dest, s1 & s2);
+  if (dest) {
+    g_value_init (dest, GST_TYPE_BITMASK);
+    gst_value_set_bitmask (dest, s1 & s2);
+  }
 
   return TRUE;
 }
@@ -4859,7 +5738,7 @@ GType gst_ ## type ## _get_type (void)                          \
 
 static const GTypeValueTable _gst_int_range_value_table = {
   gst_value_init_int_range,
-  NULL,
+  gst_value_free_int_range,
   gst_value_copy_int_range,
   NULL,
   (char *) "ii",
@@ -4872,7 +5751,7 @@ FUNC_VALUE_GET_TYPE (int_range, "GstIntRange");
 
 static const GTypeValueTable _gst_int64_range_value_table = {
   gst_value_init_int64_range,
-  NULL,
+  gst_value_free_int64_range,
   gst_value_copy_int64_range,
   NULL,
   (char *) "qq",
@@ -4948,19 +5827,8 @@ static const GTypeValueTable _gst_fraction_value_table = {
 
 FUNC_VALUE_GET_TYPE (fraction, "GstFraction");
 
-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;
-}
+G_DEFINE_BOXED_TYPE (GstDateTime, gst_date_time,
+    (GBoxedCopyFunc) gst_date_time_ref, (GBoxedFreeFunc) gst_date_time_unref);
 
 static const GTypeValueTable _gst_bitmask_value_table = {
   gst_value_init_bitmask,
@@ -5086,6 +5954,17 @@ _priv_gst_value_initialize (void)
   {
     static GstValueTable gst_value = {
       0,
+      gst_value_compare_sample,
+      gst_value_serialize_sample,
+      gst_value_deserialize_sample,
+    };
+
+    gst_value.type = GST_TYPE_SAMPLE;
+    gst_value_register (&gst_value);
+  }
+  {
+    static GstValueTable gst_value = {
+      0,
       gst_value_compare_fraction,
       gst_value_serialize_fraction,
       gst_value_deserialize_fraction,
@@ -5109,6 +5988,17 @@ _priv_gst_value_initialize (void)
     static GstValueTable gst_value = {
       0,
       NULL,
+      gst_value_serialize_segment,
+      gst_value_deserialize_segment,
+    };
+
+    gst_value.type = GST_TYPE_SEGMENT;
+    gst_value_register (&gst_value);
+  }
+  {
+    static GstValueTable gst_value = {
+      0,
+      NULL,
       gst_value_serialize_structure,
       gst_value_deserialize_structure,
     };
@@ -5119,6 +6009,17 @@ _priv_gst_value_initialize (void)
   {
     static GstValueTable gst_value = {
       0,
+      NULL,
+      gst_value_serialize_tag_list,
+      gst_value_deserialize_tag_list,
+    };
+
+    gst_value.type = GST_TYPE_TAG_LIST;
+    gst_value_register (&gst_value);
+  }
+  {
+    static GstValueTable gst_value = {
+      0,
       gst_value_compare_date,
       gst_value_serialize_date,
       gst_value_deserialize_date,