Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / smpte / gstsmpte.c
index 91b91d9..afd30a2 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+/**
+ * SECTION:element-smpte
+ *
+ * smpte can accept I420 video streams with the same width, height and
+ * framerate. The two incomming buffers are blended together using an effect
+ * specific alpha mask. 
+ *
+ * The #GstSmpte:depth property defines the presision in bits of the mask. A
+ * higher presision will create a mask with smoother gradients in order to avoid
+ * banding.
+ *
+ * <refsect2>
+ * <title>Sample pipelines</title>
+ * |[
+ * gst-launch -v videotestsrc pattern=1 ! smpte name=s border=20000 type=234 duration=2000000000 ! ffmpegcolorspace ! ximagesink videotestsrc ! s.
+ * ]| A pipeline to demonstrate the smpte transition.
+ * It shows a pinwheel transition a from a snow videotestsrc to an smpte
+ * pattern videotestsrc. The transition will take 2 seconds to complete. The
+ * edges of the transition are smoothed with a 20000 big border.
+ * </refsect2>
+ */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 #include <string.h>
-#include <gstsmpte.h>
+#include "gstsmpte.h"
 #include <gst/video/video.h>
 #include "paint.h"
 
-/* elementfactory information */
-static GstElementDetails smpte_details = {
-  "SMPTE transitions",
-  "Filter/Editor/Video",
-  "Apply the standard SMPTE transitions on video images",
-  "Wim Taymans <wim.taymans@chello.be>"
-};
+GST_DEBUG_CATEGORY_STATIC (gst_smpte_debug);
+#define GST_CAT_DEFAULT gst_smpte_debug
 
 static GstStaticPadTemplate gst_smpte_src_template =
 GST_STATIC_PAD_TEMPLATE ("src",
@@ -65,15 +82,36 @@ enum
   LAST_SIGNAL
 };
 
+#define DEFAULT_PROP_TYPE      1
+#define DEFAULT_PROP_BORDER    0
+#define DEFAULT_PROP_DEPTH     16
+#define DEFAULT_PROP_FPS       0.
+#define DEFAULT_PROP_DURATION  GST_SECOND
+#define DEFAULT_PROP_INVERT   FALSE
+
 enum
 {
-  ARG_0,
-  ARG_TYPE,
-  ARG_BORDER,
-  ARG_DEPTH,
-  ARG_FPS,
+  PROP_0,
+  PROP_TYPE,
+  PROP_BORDER,
+  PROP_DEPTH,
+  PROP_FPS,
+  PROP_DURATION,
+  PROP_INVERT,
+  PROP_LAST,
 };
 
+#define I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
+#define I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
+#define I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(I420_Y_ROWSTRIDE(width)))/2)
+
+#define I420_Y_OFFSET(w,h) (0)
+#define I420_U_OFFSET(w,h) (I420_Y_OFFSET(w,h)+(I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
+#define I420_V_OFFSET(w,h) (I420_U_OFFSET(w,h)+(I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
+
+#define I420_SIZE(w,h)     (I420_V_OFFSET(w,h)+(I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
+
+
 #define GST_TYPE_SMPTE_TRANSITION_TYPE (gst_smpte_transition_type_get_type())
 static GType
 gst_smpte_transition_type_get_type (void)
@@ -87,7 +125,7 @@ gst_smpte_transition_type_get_type (void)
 
     definitions = gst_mask_get_definitions ();
     smpte_transitions =
-       g_new0 (GEnumValue, g_list_length ((GList *) definitions) + 1);
+        g_new0 (GEnumValue, g_list_length ((GList *) definitions) + 1);
 
     while (definitions) {
       GstMaskDefinition *definition = (GstMaskDefinition *) definitions->data;
@@ -95,14 +133,15 @@ gst_smpte_transition_type_get_type (void)
       definitions = g_list_next (definitions);
 
       smpte_transitions[i].value = definition->type;
-      smpte_transitions[i].value_name = definition->short_name;
-      smpte_transitions[i].value_nick = definition->long_name;
+      /* older GLib versions have the two fields as non-const, hence the cast */
+      smpte_transitions[i].value_nick = (gchar *) definition->short_name;
+      smpte_transitions[i].value_name = (gchar *) definition->long_name;
 
       i++;
     }
 
     smpte_transition_type =
-       g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
+        g_enum_register_static ("GstSMPTETransitionType", smpte_transitions);
   }
   return smpte_transition_type;
 }
@@ -111,14 +150,19 @@ gst_smpte_transition_type_get_type (void)
 static void gst_smpte_class_init (GstSMPTEClass * klass);
 static void gst_smpte_base_init (GstSMPTEClass * klass);
 static void gst_smpte_init (GstSMPTE * smpte);
+static void gst_smpte_finalize (GstSMPTE * smpte);
 
-static void gst_smpte_loop (GstElement * element);
+static GstFlowReturn gst_smpte_collected (GstCollectPads2 * pads,
+    GstSMPTE * smpte);
 
 static void gst_smpte_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec);
 static void gst_smpte_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
 
+static GstStateChangeReturn gst_smpte_change_state (GstElement * element,
+    GstStateChange transition);
+
 static GstElementClass *parent_class = NULL;
 
 /*static guint gst_smpte_signals[LAST_SIGNAL] = { 0 }; */
@@ -140,8 +184,9 @@ gst_smpte_get_type (void)
       0,
       (GInstanceInitFunc) gst_smpte_init,
     };
+
     smpte_type =
-       g_type_register_static (GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
+        g_type_register_static (GST_TYPE_ELEMENT, "GstSMPTE", &smpte_info, 0);
   }
   return smpte_type;
 }
@@ -157,7 +202,10 @@ gst_smpte_base_init (GstSMPTEClass * klass)
       gst_static_pad_template_get (&gst_smpte_sink2_template));
   gst_element_class_add_pad_template (element_class,
       gst_static_pad_template_get (&gst_smpte_src_template));
-  gst_element_class_set_details (element_class, &smpte_details);
+  gst_element_class_set_details_simple (element_class, "SMPTE transitions",
+      "Filter/Editor/Video",
+      "Apply the standard SMPTE transitions on video images",
+      "Wim Taymans <wim.taymans@chello.be>");
 }
 
 static void
@@ -169,41 +217,55 @@ gst_smpte_class_init (GstSMPTEClass * klass)
   gobject_class = (GObjectClass *) klass;
   gstelement_class = (GstElementClass *) klass;
 
-  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
+  parent_class = g_type_class_peek_parent (klass);
 
   gobject_class->set_property = gst_smpte_set_property;
   gobject_class->get_property = gst_smpte_get_property;
+  gobject_class->finalize = (GObjectFinalizeFunc) gst_smpte_finalize;
 
   _gst_mask_init ();
 
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TYPE,
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TYPE,
       g_param_spec_enum ("type", "Type", "The type of transition to use",
-         GST_TYPE_SMPTE_TRANSITION_TYPE, 1, G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FPS,
+          GST_TYPE_SMPTE_TRANSITION_TYPE, DEFAULT_PROP_TYPE,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FPS,
       g_param_spec_float ("fps", "FPS",
-         "Frames per second if no input files are given", 0., G_MAXFLOAT, 25.,
-         G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BORDER,
+          "Frames per second if no input files are given (deprecated)", 0.,
+          G_MAXFLOAT, DEFAULT_PROP_FPS,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BORDER,
       g_param_spec_int ("border", "Border",
-         "The border width of the transition", 0, G_MAXINT, 0,
-         G_PARAM_READWRITE));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEPTH,
+          "The border width of the transition", 0, G_MAXINT,
+          DEFAULT_PROP_BORDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DEPTH,
       g_param_spec_int ("depth", "Depth", "Depth of the mask in bits", 1, 24,
-         16, G_PARAM_READWRITE));
+          DEFAULT_PROP_DEPTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DURATION,
+      g_param_spec_uint64 ("duration", "Duration",
+          "Duration of the transition effect in nanoseconds", 0, G_MAXUINT64,
+          DEFAULT_PROP_DURATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INVERT,
+      g_param_spec_boolean ("invert", "Invert",
+          "Invert transition mask", DEFAULT_PROP_INVERT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_smpte_change_state);
 }
 
-/*                        wht  yel  cya  grn  mag  red  blu  blk   -I    Q */
-static int y_colors[] = { 255, 226, 179, 150, 105, 76, 29, 16, 16, 0 };
-static int u_colors[] = { 128, 0, 170, 46, 212, 85, 255, 128, 0, 128 };
-static int v_colors[] = { 128, 155, 0, 21, 235, 255, 107, 128, 128, 255 };
+/*                              wht  yel  cya  grn  mag  red  blu  blk   -I    Q */
+static const int y_colors[] = { 255, 226, 179, 150, 105, 76, 29, 16, 16, 0 };
+static const int u_colors[] = { 128, 0, 170, 46, 212, 85, 255, 128, 0, 128 };
+static const int v_colors[] = { 128, 155, 0, 21, 235, 255, 107, 128, 128, 255 };
 
 static void
 fill_i420 (guint8 * data, gint width, gint height, gint color)
 {
-  gint size = width * height, size4 = size >> 2;
+  gint size = I420_Y_ROWSTRIDE (width) * GST_ROUND_UP_2 (height);
+  gint size4 = size >> 2;
   guint8 *yp = data;
-  guint8 *up = data + size;
-  guint8 *vp = data + size + size4;
+  guint8 *up = data + I420_U_OFFSET (width, height);
+  guint8 *vp = data + I420_V_OFFSET (width, height);
 
   memset (yp, y_colors[color], size);
   memset (up, u_colors[color], size4);
@@ -211,18 +273,27 @@ fill_i420 (guint8 * data, gint width, gint height, gint color)
 }
 
 static gboolean
-gst_smpte_update_mask (GstSMPTE * smpte, gint type, gint depth, gint width,
-    gint height)
+gst_smpte_update_mask (GstSMPTE * smpte, gint type, gboolean invert,
+    gint depth, gint width, gint height)
 {
   GstMask *newmask;
 
-  newmask = gst_mask_factory_new (type, depth, width, height);
+  if (smpte->mask) {
+    if (smpte->type == type &&
+        smpte->invert == invert &&
+        smpte->depth == depth &&
+        smpte->width == width && smpte->height == height)
+      return TRUE;
+  }
+
+  newmask = gst_mask_factory_new (type, invert, depth, width, height);
   if (newmask) {
     if (smpte->mask) {
       gst_mask_destroy (smpte->mask);
     }
     smpte->mask = newmask;
     smpte->type = type;
+    smpte->invert = invert;
     smpte->depth = depth;
     smpte->width = width;
     smpte->height = height;
@@ -233,61 +304,99 @@ gst_smpte_update_mask (GstSMPTE * smpte, gint type, gint depth, gint width,
 }
 
 static gboolean
-gst_smpte_sinkconnect (GstPad * pad, const GstCaps * caps)
+gst_smpte_setcaps (GstPad * pad, GstCaps * caps)
 {
   GstSMPTE *smpte;
   GstStructure *structure;
   gboolean ret;
 
-  smpte = GST_SMPTE (gst_pad_get_parent (pad));
+  smpte = GST_SMPTE (GST_PAD_PARENT (pad));
 
   structure = gst_caps_get_structure (caps, 0);
 
   ret = gst_structure_get_int (structure, "width", &smpte->width);
   ret &= gst_structure_get_int (structure, "height", &smpte->height);
-  ret &= gst_structure_get_double (structure, "framerate", &smpte->fps);
+  ret &= gst_structure_get_fraction (structure, "framerate",
+      &smpte->fps_num, &smpte->fps_denom);
   if (!ret)
-    return GST_PAD_LINK_REFUSED;
+    return FALSE;
 
-  gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width,
-      smpte->height);
+  /* for backward compat, we store these here */
+  smpte->fps = ((gdouble) smpte->fps_num) / smpte->fps_denom;
 
-  /* forward to the next plugin */
-  return gst_pad_try_set_caps (smpte->srcpad, caps);
+  /* figure out the duration in frames */
+  smpte->end_position = gst_util_uint64_scale (smpte->duration,
+      smpte->fps_num, GST_SECOND * smpte->fps_denom);
+
+  GST_DEBUG_OBJECT (smpte, "duration: %d frames", smpte->end_position);
+
+  ret =
+      gst_smpte_update_mask (smpte, smpte->type, smpte->invert, smpte->depth,
+      smpte->width, smpte->height);
+
+  return ret;
 }
 
 static void
 gst_smpte_init (GstSMPTE * smpte)
 {
   smpte->sinkpad1 =
-      gst_pad_new_from_template (gst_static_pad_template_get
-      (&gst_smpte_sink1_template), "sink1");
-  gst_pad_set_link_function (smpte->sinkpad1, gst_smpte_sinkconnect);
+      gst_pad_new_from_static_template (&gst_smpte_sink1_template, "sink1");
+  gst_pad_set_setcaps_function (smpte->sinkpad1,
+      GST_DEBUG_FUNCPTR (gst_smpte_setcaps));
+  gst_pad_set_getcaps_function (smpte->sinkpad1,
+      GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad1);
 
   smpte->sinkpad2 =
-      gst_pad_new_from_template (gst_static_pad_template_get
-      (&gst_smpte_sink2_template), "sink2");
-  gst_pad_set_link_function (smpte->sinkpad2, gst_smpte_sinkconnect);
+      gst_pad_new_from_static_template (&gst_smpte_sink2_template, "sink2");
+  gst_pad_set_setcaps_function (smpte->sinkpad2,
+      GST_DEBUG_FUNCPTR (gst_smpte_setcaps));
+  gst_pad_set_getcaps_function (smpte->sinkpad2,
+      GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
   gst_element_add_pad (GST_ELEMENT (smpte), smpte->sinkpad2);
 
   smpte->srcpad =
-      gst_pad_new_from_template (gst_static_pad_template_get
-      (&gst_smpte_src_template), "src");
+      gst_pad_new_from_static_template (&gst_smpte_src_template, "src");
   gst_element_add_pad (GST_ELEMENT (smpte), smpte->srcpad);
 
-  gst_element_set_loop_function (GST_ELEMENT (smpte), gst_smpte_loop);
+  smpte->collect = gst_collect_pads2_new ();
+  gst_collect_pads2_set_function (smpte->collect,
+      (GstCollectPads2Function) GST_DEBUG_FUNCPTR (gst_smpte_collected), smpte);
+  gst_collect_pads2_start (smpte->collect);
+
+  gst_collect_pads2_add_pad (smpte->collect, smpte->sinkpad1,
+      sizeof (GstCollectData2));
+  gst_collect_pads2_add_pad (smpte->collect, smpte->sinkpad2,
+      sizeof (GstCollectData2));
+
+  smpte->fps = DEFAULT_PROP_FPS;
+  smpte->type = DEFAULT_PROP_TYPE;
+  smpte->border = DEFAULT_PROP_BORDER;
+  smpte->depth = DEFAULT_PROP_DEPTH;
+  smpte->duration = DEFAULT_PROP_DURATION;
+  smpte->invert = DEFAULT_PROP_INVERT;
+  smpte->fps_num = 0;
+  smpte->fps_denom = 1;
+}
+
+static void
+gst_smpte_finalize (GstSMPTE * smpte)
+{
+  if (smpte->collect) {
+    gst_object_unref (smpte->collect);
+  }
+
+  G_OBJECT_CLASS (parent_class)->finalize ((GObject *) smpte);
+}
 
-  smpte->width = 320;
-  smpte->height = 200;
-  smpte->fps = 25.;
-  smpte->duration = 64;
+static void
+gst_smpte_reset (GstSMPTE * smpte)
+{
+  smpte->width = -1;
+  smpte->height = -1;
   smpte->position = 0;
-  smpte->type = 1;
-  smpte->border = 0;
-  smpte->depth = 16;
-  gst_smpte_update_mask (smpte, smpte->type, smpte->depth, smpte->width,
-      smpte->height);
+  smpte->end_position = 0;
 }
 
 static void
@@ -299,8 +408,7 @@ gst_smpte_blend_i420 (guint8 * in1, guint8 * in2, guint8 * out, GstMask * mask,
   gint i, j;
   gint min, max;
   guint8 *in1u, *in1v, *in2u, *in2v, *outu, *outv;
-  gint lumsize = width * height;
-  gint chromsize = lumsize >> 2;
+  gint uoffset, voffset, ystr, ustr, vstr;
 
   if (border == 0)
     border++;
@@ -308,12 +416,19 @@ gst_smpte_blend_i420 (guint8 * in1, guint8 * in2, guint8 * out, GstMask * mask,
   min = pos - border;
   max = pos;
 
-  in1u = in1 + lumsize;
-  in1v = in1u + chromsize;
-  in2u = in2 + lumsize;
-  in2v = in2u + chromsize;
-  outu = out + lumsize;
-  outv = outu + chromsize;
+  uoffset = I420_U_OFFSET (width, height);
+  voffset = I420_V_OFFSET (width, height);
+
+  ystr = I420_Y_ROWSTRIDE (width);
+  ustr = I420_U_ROWSTRIDE (width);
+  vstr = I420_V_ROWSTRIDE (width);
+
+  in1u = in1 + uoffset;
+  in1v = in1 + voffset;
+  in2u = in2 + uoffset;
+  in2v = in2 + voffset;
+  outu = out + uoffset;
+  outv = out + voffset;
 
   maskp = mask->data;
 
@@ -322,79 +437,101 @@ gst_smpte_blend_i420 (guint8 * in1, guint8 * in2, guint8 * out, GstMask * mask,
       value = *maskp++;
       value = ((CLAMP (value, min, max) - min) << 8) / border;
 
-      *out++ = ((*in1++ * value) + (*in2++ * (256 - value))) >> 8;
+      out[j] = ((in1[j] * value) + (in2[j] * (256 - value))) >> 8;
       if (!(i & 1) && !(j & 1)) {
-       *outu++ = ((*in1u++ * value) + (*in2u++ * (256 - value))) >> 8;
-       *outv++ = ((*in1v++ * value) + (*in2v++ * (256 - value))) >> 8;
+        outu[j / 2] =
+            ((in1u[j / 2] * value) + (in2u[j / 2] * (256 - value))) >> 8;
+        outv[j / 2] =
+            ((in1v[j / 2] * value) + (in2v[j / 2] * (256 - value))) >> 8;
       }
     }
+    out += ystr;
+    in1 += ystr;
+    in2 += ystr;
+    if (!(i & 1)) {
+      outu += ustr;
+      in1u += ustr;
+      in2u += ustr;
+      outv += vstr;
+      in1v += vstr;
+      in2v += vstr;
+    }
   }
 }
 
-static void
-gst_smpte_loop (GstElement * element)
+static GstFlowReturn
+gst_smpte_collected (GstCollectPads2 * pads, GstSMPTE * smpte)
 {
-  GstSMPTE *smpte;
   GstBuffer *outbuf;
   GstClockTime ts;
   GstBuffer *in1 = NULL, *in2 = NULL;
+  GSList *collected;
 
-  smpte = GST_SMPTE (element);
+  if (G_UNLIKELY (smpte->fps_num == 0))
+    goto not_negotiated;
 
-  ts = smpte->position * GST_SECOND / smpte->fps;
+  if (!GST_PAD_CAPS (smpte->sinkpad1) || !GST_PAD_CAPS (smpte->sinkpad2))
+    goto not_negotiated;
 
-  while (GST_PAD_IS_USABLE (smpte->sinkpad1) && in1 == NULL) {
-    in1 = GST_BUFFER (gst_pad_pull (smpte->sinkpad1));
-    if (GST_IS_EVENT (in1)) {
-      gst_pad_push (smpte->srcpad, GST_DATA (in1));
-      in1 = NULL;
-    } else
-      ts = GST_BUFFER_TIMESTAMP (in1);
-  }
-  if (GST_PAD_IS_USABLE (smpte->sinkpad2) && in2 == NULL) {
-    in2 = GST_BUFFER (gst_pad_pull (smpte->sinkpad2));
-    if (GST_IS_EVENT (in2)) {
-      gst_pad_push (smpte->srcpad, GST_DATA (in2));
-      in2 = NULL;
-    } else
-      ts = GST_BUFFER_TIMESTAMP (in2);
+  ts = gst_util_uint64_scale_int (smpte->position * GST_SECOND,
+      smpte->fps_denom, smpte->fps_num);
+
+  for (collected = pads->data; collected; collected = g_slist_next (collected)) {
+    GstCollectData2 *data;
+
+    data = (GstCollectData2 *) collected->data;
+
+    if (data->pad == smpte->sinkpad1)
+      in1 = gst_collect_pads2_pop (pads, data);
+    else if (data->pad == smpte->sinkpad2)
+      in2 = gst_collect_pads2_pop (pads, data);
   }
 
   if (in1 == NULL) {
-    in1 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
+    /* if no input, make picture black */
+    in1 = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
     fill_i420 (GST_BUFFER_DATA (in1), smpte->width, smpte->height, 7);
   }
   if (in2 == NULL) {
-    in2 = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
+    /* if no input, make picture white */
+    in2 = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
     fill_i420 (GST_BUFFER_DATA (in2), smpte->width, smpte->height, 0);
   }
 
-  if (smpte->position < smpte->duration) {
-    outbuf = gst_buffer_new_and_alloc (smpte->width * smpte->height * 3);
+  if (GST_BUFFER_SIZE (in1) != GST_BUFFER_SIZE (in2))
+    goto input_formats_do_not_match;
+
+  if (smpte->position < smpte->end_position) {
+    outbuf = gst_buffer_new_and_alloc (I420_SIZE (smpte->width, smpte->height));
 
+    /* set caps if not done yet */
     if (!GST_PAD_CAPS (smpte->srcpad)) {
       GstCaps *caps;
 
       caps =
-         gst_caps_copy (gst_static_caps_get (&gst_smpte_src_template.
-             static_caps));
+          gst_caps_copy (gst_static_caps_get
+          (&gst_smpte_src_template.static_caps));
       gst_caps_set_simple (caps, "width", G_TYPE_INT, smpte->width, "height",
-         G_TYPE_INT, smpte->height, "framerate", G_TYPE_DOUBLE, smpte->fps,
-         NULL);
+          G_TYPE_INT, smpte->height, "framerate", GST_TYPE_FRACTION,
+          smpte->fps_num, smpte->fps_denom, NULL);
+
+      gst_pad_set_caps (smpte->srcpad, caps);
+
+      gst_pad_push_event (smpte->srcpad,
+          gst_event_new_new_segment_full (FALSE,
+              1.0, 1.0, GST_FORMAT_TIME, 0, -1, 0));
 
-      if (!gst_pad_try_set_caps (smpte->srcpad, caps)) {
-       GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL), (NULL));
-       return;
-      }
     }
+    gst_buffer_set_caps (outbuf, GST_PAD_CAPS (smpte->srcpad));
 
     gst_smpte_blend_i420 (GST_BUFFER_DATA (in1),
-       GST_BUFFER_DATA (in2),
-       GST_BUFFER_DATA (outbuf),
-       smpte->mask, smpte->width, smpte->height,
-       smpte->border,
-       ((1 << smpte->depth) + smpte->border) *
-       smpte->position / smpte->duration);
+        GST_BUFFER_DATA (in2),
+        GST_BUFFER_DATA (outbuf),
+        smpte->mask, smpte->width, smpte->height,
+        smpte->border,
+        ((1 << smpte->depth) + smpte->border) *
+        smpte->position / smpte->end_position);
+
   } else {
     outbuf = in2;
     gst_buffer_ref (in2);
@@ -408,7 +545,23 @@ gst_smpte_loop (GstElement * element)
     gst_buffer_unref (in2);
 
   GST_BUFFER_TIMESTAMP (outbuf) = ts;
-  gst_pad_push (smpte->srcpad, GST_DATA (outbuf));
+
+  return gst_pad_push (smpte->srcpad, outbuf);
+
+  /* ERRORS */
+not_negotiated:
+  {
+    GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL),
+        ("No input format negotiated"));
+    return GST_FLOW_NOT_NEGOTIATED;
+  }
+input_formats_do_not_match:
+  {
+    GST_ELEMENT_ERROR (smpte, CORE, NEGOTIATION, (NULL),
+        ("input formats don't match: %" GST_PTR_FORMAT " vs. %" GST_PTR_FORMAT,
+            GST_PAD_CAPS (smpte->sinkpad1), GST_PAD_CAPS (smpte->sinkpad2)));
+    return GST_FLOW_ERROR;
+  }
 }
 
 static void
@@ -420,28 +573,24 @@ gst_smpte_set_property (GObject * object, guint prop_id,
   smpte = GST_SMPTE (object);
 
   switch (prop_id) {
-    case ARG_TYPE:
-    {
-      gint type = g_value_get_enum (value);
-
-      gst_smpte_update_mask (smpte, type, smpte->depth,
-         smpte->width, smpte->height);
+    case PROP_TYPE:
+      smpte->type = g_value_get_enum (value);
       break;
-    }
-    case ARG_BORDER:
+    case PROP_BORDER:
       smpte->border = g_value_get_int (value);
       break;
-    case ARG_FPS:
+    case PROP_FPS:
       smpte->fps = g_value_get_float (value);
       break;
-    case ARG_DEPTH:
-    {
-      gint depth = g_value_get_int (value);
-
-      gst_smpte_update_mask (smpte, smpte->type, depth,
-         smpte->width, smpte->height);
+    case PROP_DEPTH:
+      smpte->depth = g_value_get_int (value);
+      break;
+    case PROP_DURATION:
+      smpte->duration = g_value_get_uint64 (value);
+      break;
+    case PROP_INVERT:
+      smpte->invert = g_value_get_boolean (value);
       break;
-    }
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -457,35 +606,69 @@ gst_smpte_get_property (GObject * object, guint prop_id,
   smpte = GST_SMPTE (object);
 
   switch (prop_id) {
-    case ARG_TYPE:
-      if (smpte->mask) {
-       g_value_set_enum (value, smpte->mask->type);
-      }
+    case PROP_TYPE:
+      g_value_set_enum (value, smpte->type);
       break;
-    case ARG_FPS:
+    case PROP_FPS:
       g_value_set_float (value, smpte->fps);
       break;
-    case ARG_BORDER:
+    case PROP_BORDER:
       g_value_set_int (value, smpte->border);
       break;
-    case ARG_DEPTH:
+    case PROP_DEPTH:
       g_value_set_int (value, smpte->depth);
       break;
+    case PROP_DURATION:
+      g_value_set_uint64 (value, smpte->duration);
+      break;
+    case PROP_INVERT:
+      g_value_set_boolean (value, smpte->invert);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
   }
 }
 
+static GstStateChangeReturn
+gst_smpte_change_state (GstElement * element, GstStateChange transition)
+{
+  GstStateChangeReturn ret;
+  GstSMPTE *smpte;
 
-static gboolean
-plugin_init (GstPlugin * plugin)
+  smpte = GST_SMPTE (element);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_READY_TO_PAUSED:
+      gst_smpte_reset (smpte);
+      GST_LOG_OBJECT (smpte, "starting collectpads");
+      gst_collect_pads2_start (smpte->collect);
+      break;
+    case GST_STATE_CHANGE_PAUSED_TO_READY:
+      GST_LOG_OBJECT (smpte, "stopping collectpads");
+      gst_collect_pads2_stop (smpte->collect);
+      break;
+    default:
+      break;
+  }
+
+  ret = parent_class->change_state (element, transition);
+
+  switch (transition) {
+    case GST_STATE_CHANGE_PAUSED_TO_READY:
+      gst_smpte_reset (smpte);
+      break;
+    default:
+      break;
+  }
+  return ret;
+}
+
+gboolean
+gst_smpte_plugin_init (GstPlugin * plugin)
 {
+  GST_DEBUG_CATEGORY_INIT (gst_smpte_debug, "smpte", 0,
+      "SMPTE transition effect");
+
   return gst_element_register (plugin, "smpte", GST_RANK_NONE, GST_TYPE_SMPTE);
 }
-
-GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
-    GST_VERSION_MINOR,
-    "smpte",
-    "Apply the standard SMPTE transitions on video images",
-    plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)