gst/audioconvert/gstaudioconvert.c: Lower debug, use g_assert in _get_unit_size
[platform/upstream/gstreamer.git] / gst / ffmpegcolorspace / gstffmpegcolorspace.c
index a033e29..b3a0b72 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+/**
+ * SECTION:element-ffmpegcolorspace
+ *
+ * <refsect2>
+ * <title>Example launch line</title>
+ * <para>
+ * <programlisting>
+ * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! ffmpegcolorspace ! ximagesink
+ * </programlisting>
+ * </para>
+ * </refsect2>
+ */
+
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#  include "config.h"
 #endif
 
-#include <gst/gst.h>
-#include <avcodec.h>
-
+#include "gstffmpegcolorspace.h"
 #include "gstffmpegcodecmap.h"
 
-#define GST_TYPE_FFMPEGCOLORSPACE \
-  (gst_ffmpegcolorspace_get_type())
-#define GST_FFMPEGCOLORSPACE(obj) \
-  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGCOLORSPACE,GstFFMpegColorspace))
-#define GST_FFMPEGCOLORSPACE_CLASS(klass) \
-  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGCOLORSPACE,GstFFMpegColorspace))
-#define GST_IS_FFMPEGCOLORSPACE(obj) \
-  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGCOLORSPACE))
-#define GST_IS_FFMPEGCOLORSPACE_CLASS(obj) \
-  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGCOLORSPACE))
-
-typedef struct _GstFFMpegColorspace GstFFMpegColorspace;
-typedef struct _GstFFMpegColorspaceClass GstFFMpegColorspaceClass;
-
-struct _GstFFMpegColorspace
-{
-  GstElement element;
-
-  GstPad *sinkpad, *srcpad;
-
-  gint width, height;
-  gfloat fps;
-  enum PixelFormat from_pixfmt, to_pixfmt;
-  AVPicture from_frame, to_frame;
-  GstCaps *sinkcaps;
-};
-
-struct _GstFFMpegColorspaceClass
-{
-  GstElementClass parent_class;
-};
+GST_DEBUG_CATEGORY_STATIC (ffmpegcolorspace_debug);
+#define GST_CAT_DEFAULT ffmpegcolorspace_debug
 
 /* elementfactory information */
-static GstElementDetails ffmpegcolorspace_details = {
-  "FFMPEG-based colorspace converter in gst-plugins",
-  "Filter/Converter/Video",
-  "Converts video from one colorspace to another",
-  "Ronald Bultje <rbultje@ronald.bitfreak.net>",
-};
+static const GstElementDetails ffmpegcsp_details =
+GST_ELEMENT_DETAILS ("FFMPEG Colorspace converter",
+    "Filter/Converter/Video",
+    "Converts video from one colorspace to another",
+    "Ronald Bultje <rbultje@ronald.bitfreak.net>");
 
 
 /* Stereo signals and args */
@@ -78,39 +59,41 @@ enum
 
 enum
 {
-  ARG_0
+  ARG_0,
 };
 
-static GType gst_ffmpegcolorspace_get_type (void);
-
-static void gst_ffmpegcolorspace_base_init (GstFFMpegColorspaceClass * klass);
-static void gst_ffmpegcolorspace_class_init (GstFFMpegColorspaceClass * klass);
-static void gst_ffmpegcolorspace_init (GstFFMpegColorspace * space);
-
-static void gst_ffmpegcolorspace_set_property (GObject * object,
-    guint prop_id, const GValue * value, GParamSpec * pspec);
-static void gst_ffmpegcolorspace_get_property (GObject * object,
-    guint prop_id, GValue * value, GParamSpec * pspec);
-
-static GstPadLinkReturn
-gst_ffmpegcolorspace_pad_link (GstPad * pad, const GstCaps * caps);
-
-static void gst_ffmpegcolorspace_chain (GstPad * pad, GstData * data);
-static GstElementStateReturn
-gst_ffmpegcolorspace_change_state (GstElement * element);
+static GType gst_ffmpegcsp_get_type (void);
+
+static void gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass);
+static void gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass);
+static void gst_ffmpegcsp_init (GstFFMpegCsp * space);
+
+static gboolean gst_ffmpegcsp_set_caps (GstBaseTransform * btrans,
+    GstCaps * incaps, GstCaps * outcaps);
+static gboolean gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans,
+    GstCaps * caps, guint * size);
+static GstFlowReturn gst_ffmpegcsp_transform (GstBaseTransform * btrans,
+    GstBuffer * inbuf, GstBuffer * outbuf);
+#if 0
+static GstFlowReturn gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans,
+    GstBuffer * inbuf);
+#endif
 
-static GstPadTemplate *srctempl, *sinktempl;
+static GstPadTemplate *sinktempl, *srctempl;
 static GstElementClass *parent_class = NULL;
 
-/*static guint gst_ffmpegcolorspace_signals[LAST_SIGNAL] = { 0 }; */
-
+/*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
 
+/* copies the given caps */
 static GstCaps *
-gst_ffmpegcolorspace_caps_remove_format_info (GstCaps * caps)
+gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
 {
   int i;
   GstStructure *structure;
   GstCaps *rgbcaps;
+  GstCaps *graycaps;
+
+  caps = gst_caps_copy (caps);
 
   for (i = 0; i < gst_caps_get_size (caps); i++) {
     structure = gst_caps_get_structure (caps, i);
@@ -123,6 +106,8 @@ gst_ffmpegcolorspace_caps_remove_format_info (GstCaps * caps)
     gst_structure_remove_field (structure, "red_mask");
     gst_structure_remove_field (structure, "green_mask");
     gst_structure_remove_field (structure, "blue_mask");
+    gst_structure_remove_field (structure, "alpha_mask");
+    gst_structure_remove_field (structure, "palette_data");
   }
 
   gst_caps_do_simplify (caps);
@@ -133,260 +118,362 @@ gst_ffmpegcolorspace_caps_remove_format_info (GstCaps * caps)
 
     gst_structure_set_name (structure, "video/x-raw-rgb");
   }
+  graycaps = gst_caps_copy (caps);
 
+  for (i = 0; i < gst_caps_get_size (graycaps); i++) {
+    structure = gst_caps_get_structure (graycaps, i);
+
+    gst_structure_set_name (structure, "video/x-raw-gray");
+  }
+
+  gst_caps_append (caps, graycaps);
   gst_caps_append (caps, rgbcaps);
 
   return caps;
 }
 
+/* The caps can be transformed into any other caps with format info removed.
+ * However, we should prefer passthrough, so if passthrough is possible,
+ * put it first in the list. */
 static GstCaps *
-gst_ffmpegcolorspace_getcaps (GstPad * pad)
+gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
+    GstPadDirection direction, GstCaps * caps)
 {
-  GstFFMpegColorspace *space;
-  GstCaps *othercaps = NULL;
-  GstCaps *caps;
-  GstPad *otherpad;
+  GstFFMpegCsp *space;
+  GstCaps *template;
+  GstCaps *result;
 
-  space = GST_FFMPEGCOLORSPACE (gst_pad_get_parent (pad));
+  space = GST_FFMPEGCSP (btrans);
 
-  otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
+  template = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
+  result = gst_caps_intersect (caps, template);
+  gst_caps_unref (template);
 
-  othercaps = gst_pad_get_allowed_caps (otherpad);
+  gst_caps_append (result, gst_ffmpegcsp_caps_remove_format_info (caps));
 
-  othercaps = gst_ffmpegcolorspace_caps_remove_format_info (othercaps);
+  GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
+      GST_PTR_FORMAT, caps, result);
 
-  caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
-  gst_caps_free (othercaps);
-
-  return caps;
+  return result;
 }
 
-static GstPadLinkReturn
-gst_ffmpegcolorspace_pad_link (GstPad * pad, const GstCaps * caps)
+static gboolean
+gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
+    GstCaps * outcaps)
 {
-  GstFFMpegColorspace *space;
-  const GstCaps *othercaps;
-  GstPad *otherpad;
-  GstPadLinkReturn ret;
-  int height, width;
-  double framerate;
-  enum PixelFormat pix_fmt;
-
-  space = GST_FFMPEGCOLORSPACE (gst_pad_get_parent (pad));
-
-  otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
-
-  /* FIXME attempt and/or check for passthru */
-
-  /* loop over all possibilities and select the first one we can convert and
-   * is accepted by the peer */
-  pix_fmt = gst_ffmpeg_caps_to_pix_fmt (caps, &width, &height, &framerate);
-  if (pix_fmt == PIX_FMT_NB) {
-    /* we disable ourself here */
-    if (pad == space->srcpad) {
-      space->to_pixfmt = PIX_FMT_NB;
-    } else {
-      space->from_pixfmt = PIX_FMT_NB;
-    }
-
-    return GST_PAD_LINK_REFUSED;
+  GstFFMpegCsp *space;
+  GstStructure *structure;
+  gint in_height, in_width;
+  gint out_height, out_width;
+  const GValue *in_framerate = NULL;
+  const GValue *out_framerate = NULL;
+  const GValue *in_par = NULL;
+  const GValue *out_par = NULL;
+  AVCodecContext *ctx;
+  gboolean res;
+
+  space = GST_FFMPEGCSP (btrans);
+
+  /* parse in and output values */
+  structure = gst_caps_get_structure (incaps, 0);
+
+  /* we have to have width and height */
+  res = gst_structure_get_int (structure, "width", &in_width);
+  res &= gst_structure_get_int (structure, "height", &in_height);
+  if (!res)
+    goto no_width_height;
+
+  /* and framerate */
+  in_framerate = gst_structure_get_value (structure, "framerate");
+  if (in_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (in_framerate))
+    goto no_framerate;
+
+  /* this is optional */
+  in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
+
+  structure = gst_caps_get_structure (outcaps, 0);
+
+  /* we have to have width and height */
+  res = gst_structure_get_int (structure, "width", &out_width);
+  res &= gst_structure_get_int (structure, "height", &out_height);
+  if (!res)
+    goto no_width_height;
+
+  /* and framerate */
+  out_framerate = gst_structure_get_value (structure, "framerate");
+  if (out_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (out_framerate))
+    goto no_framerate;
+
+  /* this is optional */
+  out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
+
+  /* these must match */
+  if (in_width != out_width || in_height != out_height ||
+      gst_value_compare (in_framerate, out_framerate) != GST_VALUE_EQUAL)
+    goto format_mismatch;
+
+  /* if present, these must match too */
+  if (in_par && out_par
+      && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
+    goto format_mismatch;
+
+  ctx = avcodec_alloc_context ();
+
+  space->width = ctx->width = in_width;
+  space->height = ctx->height = in_height;
+
+  /* get from format */
+  ctx->pix_fmt = PIX_FMT_NB;
+  gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
+  if (ctx->pix_fmt == PIX_FMT_NB)
+    goto invalid_in_caps;
+  space->from_pixfmt = ctx->pix_fmt;
+
+  /* palette, only for from data */
+  if (space->palette)
+    av_free (space->palette);
+  space->palette = ctx->palctrl;
+  ctx->palctrl = NULL;
+
+  /* get to format */
+  ctx->pix_fmt = PIX_FMT_NB;
+  gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
+  if (ctx->pix_fmt == PIX_FMT_NB)
+    goto invalid_out_caps;
+  space->to_pixfmt = ctx->pix_fmt;
+
+  GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
+
+  av_free (ctx);
+
+  return TRUE;
+
+  /* ERRORS */
+no_width_height:
+  {
+    GST_DEBUG_OBJECT (space, "did not specify width or height");
+    space->from_pixfmt = PIX_FMT_NB;
+    space->to_pixfmt = PIX_FMT_NB;
+    return FALSE;
   }
-
-  /* set the size on the otherpad */
-  othercaps = gst_pad_get_negotiated_caps (otherpad);
-  if (othercaps) {
-    GstCaps *caps = gst_caps_copy (othercaps);
-
-    gst_caps_set_simple (caps,
-        "width", G_TYPE_INT, width,
-        "height", G_TYPE_INT, height,
-        "framerate", G_TYPE_DOUBLE, framerate, NULL);
-    ret = gst_pad_try_set_caps (otherpad, caps);
-    if (GST_PAD_LINK_FAILED (ret)) {
-      return ret;
-    }
+no_framerate:
+  {
+    GST_DEBUG_OBJECT (space, "did not specify framerate");
+    space->from_pixfmt = PIX_FMT_NB;
+    space->to_pixfmt = PIX_FMT_NB;
+    return FALSE;
   }
-
-  if (pad == space->srcpad) {
-    space->to_pixfmt = pix_fmt;
-  } else {
-    space->from_pixfmt = pix_fmt;
+format_mismatch:
+  {
+    GST_DEBUG_OBJECT (space, "input and output formats do not match");
+    space->from_pixfmt = PIX_FMT_NB;
+    space->to_pixfmt = PIX_FMT_NB;
+    return FALSE;
+  }
+invalid_in_caps:
+  {
+    GST_DEBUG_OBJECT (space, "could not configure context for input format");
+    av_free (ctx);
+    space->from_pixfmt = PIX_FMT_NB;
+    space->to_pixfmt = PIX_FMT_NB;
+    return FALSE;
+  }
+invalid_out_caps:
+  {
+    GST_DEBUG_OBJECT (space, "could not configure context for output format");
+    av_free (ctx);
+    space->from_pixfmt = PIX_FMT_NB;
+    space->to_pixfmt = PIX_FMT_NB;
+    return FALSE;
   }
-
-  space->width = width;
-  space->height = height;
-
-  return GST_PAD_LINK_OK;
 }
 
 static GType
-gst_ffmpegcolorspace_get_type (void)
+gst_ffmpegcsp_get_type (void)
 {
-  static GType ffmpegcolorspace_type = 0;
+  static GType ffmpegcsp_type = 0;
 
-  if (!ffmpegcolorspace_type) {
-    static const GTypeInfo ffmpegcolorspace_info = {
-      sizeof (GstFFMpegColorspaceClass),
-      (GBaseInitFunc) gst_ffmpegcolorspace_base_init,
+  if (!ffmpegcsp_type) {
+    static const GTypeInfo ffmpegcsp_info = {
+      sizeof (GstFFMpegCspClass),
+      (GBaseInitFunc) gst_ffmpegcsp_base_init,
       NULL,
-      (GClassInitFunc) gst_ffmpegcolorspace_class_init,
+      (GClassInitFunc) gst_ffmpegcsp_class_init,
       NULL,
       NULL,
-      sizeof (GstFFMpegColorspace),
+      sizeof (GstFFMpegCsp),
       0,
-      (GInstanceInitFunc) gst_ffmpegcolorspace_init,
+      (GInstanceInitFunc) gst_ffmpegcsp_init,
     };
 
-    ffmpegcolorspace_type = g_type_register_static (GST_TYPE_ELEMENT,
-        "GstFFMpegColorspace", &ffmpegcolorspace_info, 0);
+    ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
+        "GstFFMpegCsp", &ffmpegcsp_info, 0);
   }
 
-  return ffmpegcolorspace_type;
+  return ffmpegcsp_type;
 }
 
 static void
-gst_ffmpegcolorspace_base_init (GstFFMpegColorspaceClass * klass)
+gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
 {
   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
 
   gst_element_class_add_pad_template (element_class, srctempl);
   gst_element_class_add_pad_template (element_class, sinktempl);
-  gst_element_class_set_details (element_class, &ffmpegcolorspace_details);
+  gst_element_class_set_details (element_class, &ffmpegcsp_details);
+}
+
+static void
+gst_ffmpegcsp_finalize (GObject * obj)
+{
+  GstFFMpegCsp *space = GST_FFMPEGCSP (obj);
+
+  if (space->palette)
+    av_free (space->palette);
+
+  G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
 
 static void
-gst_ffmpegcolorspace_class_init (GstFFMpegColorspaceClass * klass)
+gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
 {
   GObjectClass *gobject_class;
   GstElementClass *gstelement_class;
+  GstBaseTransformClass *gstbasetransform_class;
 
   gobject_class = (GObjectClass *) klass;
   gstelement_class = (GstElementClass *) klass;
+  gstbasetransform_class = (GstBaseTransformClass *) klass;
+
+  parent_class = g_type_class_peek_parent (klass);
+
+  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_finalize);
+
+  gstbasetransform_class->transform_caps =
+      GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
+  gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
+  gstbasetransform_class->get_unit_size =
+      GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
+  gstbasetransform_class->transform =
+      GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
+#if 0
+  gstbasetransform_class->transform_ip =
+      GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
+#endif
 
-  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
-
-  gobject_class->set_property = gst_ffmpegcolorspace_set_property;
-  gobject_class->get_property = gst_ffmpegcolorspace_get_property;
+  gstbasetransform_class->passthrough_on_same_caps = TRUE;
 
-  gstelement_class->change_state = gst_ffmpegcolorspace_change_state;
+  GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
+      "FFMPEG-based colorspace converter");
 }
 
 static void
-gst_ffmpegcolorspace_init (GstFFMpegColorspace * space)
+gst_ffmpegcsp_init (GstFFMpegCsp * space)
 {
-  space->sinkpad = gst_pad_new_from_template (sinktempl, "sink");
-  gst_pad_set_link_function (space->sinkpad, gst_ffmpegcolorspace_pad_link);
-  gst_pad_set_getcaps_function (space->sinkpad, gst_ffmpegcolorspace_getcaps);
-  gst_pad_set_chain_function (space->sinkpad, gst_ffmpegcolorspace_chain);
-  gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
-
-  space->srcpad = gst_pad_new_from_template (srctempl, "src");
-  gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
-  gst_pad_set_link_function (space->srcpad, gst_ffmpegcolorspace_pad_link);
-  gst_pad_set_getcaps_function (space->srcpad, gst_ffmpegcolorspace_getcaps);
-
+  gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (space), TRUE);
   space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
+  space->palette = NULL;
 }
 
-static void
-gst_ffmpegcolorspace_chain (GstPad * pad, GstData * data)
+static gboolean
+gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
+    guint * size)
 {
-  GstBuffer *inbuf = GST_BUFFER (data);
-  GstFFMpegColorspace *space;
-  GstBuffer *outbuf = NULL;
+  GstFFMpegCsp *space = NULL;
+  GstStructure *structure = NULL;
+  AVCodecContext *ctx = NULL;
+  gint width, height;
 
-  g_return_if_fail (pad != NULL);
-  g_return_if_fail (GST_IS_PAD (pad));
-  g_return_if_fail (inbuf != NULL);
+  g_assert (size);
 
-  space = GST_FFMPEGCOLORSPACE (gst_pad_get_parent (pad));
+  space = GST_FFMPEGCSP (btrans);
 
-  g_return_if_fail (space != NULL);
-  g_return_if_fail (GST_IS_FFMPEGCOLORSPACE (space));
+  structure = gst_caps_get_structure (caps, 0);
+  gst_structure_get_int (structure, "width", &width);
+  gst_structure_get_int (structure, "height", &height);
 
-  if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB) {
-    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
-        ("attempting to convert colorspaces between unknown formats"));
-    gst_buffer_unref (inbuf);
-    return;
-  }
+  ctx = avcodec_alloc_context ();
+
+  g_assert (ctx != NULL);
+
+  gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
+
+  *size = avpicture_get_size (ctx->pix_fmt, width, height);
 
-  if (space->from_pixfmt == space->to_pixfmt) {
-    outbuf = inbuf;
-  } else {
-    /* use bufferpool here */
-    guint size = avpicture_get_size (space->to_pixfmt,
-        space->width,
-        space->height);
-
-    outbuf = gst_pad_alloc_buffer (space->srcpad, GST_BUFFER_OFFSET_NONE, size);
-
-    /* convert */
-    avpicture_fill ((AVPicture *) & space->from_frame, GST_BUFFER_DATA (inbuf),
-        space->from_pixfmt, space->width, space->height);
-    avpicture_fill ((AVPicture *) & space->to_frame, GST_BUFFER_DATA (outbuf),
-        space->to_pixfmt, space->width, space->height);
-    img_convert ((AVPicture *) & space->to_frame, space->to_pixfmt,
-        (AVPicture *) & space->from_frame, space->from_pixfmt,
-        space->width, space->height);
-
-    GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (inbuf);
-    GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (inbuf);
-
-    gst_buffer_unref (inbuf);
+  /* ffmpeg frames have the palette after the frame data, whereas
+   * GStreamer currently puts it into the caps as 'palette_data' field,
+   * so for paletted data the frame size avpicture_get_size() returns is
+   * 1024 bytes larger than what GStreamer expects. */
+  if (gst_structure_has_field (structure, "palette_data")) {
+    *size -= 4 * 256;           /* = AVPALETTE_SIZE */
   }
 
-  gst_pad_push (space->srcpad, GST_DATA (outbuf));
+  if (ctx->palctrl)
+    av_free (ctx->palctrl);
+  av_free (ctx);
+
+  return TRUE;
 }
 
-static GstElementStateReturn
-gst_ffmpegcolorspace_change_state (GstElement * element)
+#if 0
+/* FIXME: Could use transform_ip to implement endianness swap type operations */
+static GstFlowReturn
+gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
 {
-  GstFFMpegColorspace *space;
+  /* do nothing */
+  return GST_FLOW_OK;
+}
+#endif
 
-  space = GST_FFMPEGCOLORSPACE (element);
+static GstFlowReturn
+gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
+    GstBuffer * outbuf)
+{
+  GstFFMpegCsp *space;
+  gint result;
 
-  switch (GST_STATE_TRANSITION (element)) {
-    case GST_STATE_PAUSED_TO_READY:
-      break;
-  }
+  space = GST_FFMPEGCSP (btrans);
 
-  if (parent_class->change_state)
-    return parent_class->change_state (element);
+  GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
+  if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
+    goto unknown_format;
 
-  return GST_STATE_SUCCESS;
-}
+  /* fill from with source data */
+  gst_ffmpegcsp_avpicture_fill (&space->from_frame,
+      GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height);
 
-static void
-gst_ffmpegcolorspace_set_property (GObject * object,
-    guint prop_id, const GValue * value, GParamSpec * pspec)
-{
-  GstFFMpegColorspace *space;
+  /* fill optional palette */
+  if (space->palette)
+    space->from_frame.data[1] = (uint8_t *) space->palette->palette;
 
-  /* it's not null if we got it, but it might not be ours */
-  g_return_if_fail (GST_IS_FFMPEGCOLORSPACE (object));
-  space = GST_FFMPEGCOLORSPACE (object);
+  /* fill target frame */
+  gst_ffmpegcsp_avpicture_fill (&space->to_frame,
+      GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height);
 
-  switch (prop_id) {
-    default:
-      break;
-  }
-}
+  /* and convert */
+  result = img_convert (&space->to_frame, space->to_pixfmt,
+      &space->from_frame, space->from_pixfmt, space->width, space->height);
+  if (result == -1)
+    goto not_supported;
 
-static void
-gst_ffmpegcolorspace_get_property (GObject * object,
-    guint prop_id, GValue * value, GParamSpec * pspec)
-{
-  GstFFMpegColorspace *space;
+  /* copy timestamps */
+  gst_buffer_stamp (outbuf, inbuf);
+  GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
 
-  /* it's not null if we got it, but it might not be ours */
-  g_return_if_fail (GST_IS_FFMPEGCOLORSPACE (object));
-  space = GST_FFMPEGCOLORSPACE (object);
+  return GST_FLOW_OK;
 
-  switch (prop_id) {
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
+  /* ERRORS */
+unknown_format:
+  {
+    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
+        ("attempting to convert colorspaces between unknown formats"));
+    return GST_FLOW_NOT_NEGOTIATED;
+  }
+not_supported:
+  {
+    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
+        ("cannot convert between formats"));
+    return GST_FLOW_NOT_SUPPORTED;
   }
 }
 
@@ -396,14 +483,15 @@ gst_ffmpegcolorspace_register (GstPlugin * plugin)
   GstCaps *caps;
 
   /* template caps */
-  caps = gst_ffmpeg_pix_fmt_to_caps ();
+  caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
 
   /* build templates */
   srctempl = gst_pad_template_new ("src",
       GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
+
+  /* the sink template will do palette handling as well... */
   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
 
-  avcodec_init ();
   return gst_element_register (plugin, "ffmpegcolorspace",
-      GST_RANK_PRIMARY, GST_TYPE_FFMPEGCOLORSPACE);
+      GST_RANK_NONE, GST_TYPE_FFMPEGCSP);
 }