colorspace: fix caps negotiation
[platform/upstream/gstreamer.git] / gst / colorspace / gstcolorspace.c
index d85e87e..bbd50bb 100644 (file)
@@ -1,5 +1,8 @@
 /* GStreamer
  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
+ * This file:
+ * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
+ * Copyright (C) 2010 David Schleef <ds@schleef.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * Boston, MA 02111-1307, USA.
  */
 
+/**
+ * SECTION:element-colorspace
+ *
+ * Convert video frames between a great variety of colorspace formats.
+ *
+ * <refsect2>
+ * <title>Example launch line</title>
+ * |[
+ * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! colorspace ! ximagesink
+ * ]|
+ * </refsect2>
+ */
+
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#  include "config.h"
 #endif
+
 #include "gstcolorspace.h"
-#include <gst/gst.h>
 #include <gst/video/video.h>
 
-#include "yuv2rgb.h"
-
+#include <string.h>
 
-static GstColorspaceFormat gst_colorspace_formats[] = {
-  {GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))},
-  {GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("YV12"))},
-  {GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB)},
-  {GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)},
-  {GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB_16)},
-};
+GST_DEBUG_CATEGORY (colorspace_debug);
+#define GST_CAT_DEFAULT colorspace_debug
+GST_DEBUG_CATEGORY (colorspace_performance);
 
-static GstColorspaceConverter gst_colorspace_converters[] = {
-  {GST_COLORSPACE_I420, GST_COLORSPACE_RGB32, gst_colorspace_I420_to_rgb32},
-  {GST_COLORSPACE_YV12, GST_COLORSPACE_RGB32, gst_colorspace_YV12_to_rgb32},
-  {GST_COLORSPACE_I420, GST_COLORSPACE_RGB24, gst_colorspace_I420_to_rgb24},
-  {GST_COLORSPACE_YV12, GST_COLORSPACE_RGB24, gst_colorspace_YV12_to_rgb24},
-  {GST_COLORSPACE_I420, GST_COLORSPACE_RGB16, gst_colorspace_I420_to_rgb16},
-  {GST_COLORSPACE_YV12, GST_COLORSPACE_RGB16, gst_colorspace_YV12_to_rgb16},
+enum
+{
+  PROP_0,
+  PROP_DITHER
 };
 
-static GstElementDetails colorspace_details =
-GST_ELEMENT_DETAILS ("Colorspace converter",
-    "Filter/Converter/Video",
-    "Converts video from one colorspace to another",
-    "Wim Taymans <wim.taymans@chello.be>");
+#define CSP_VIDEO_CAPS                                         \
+  "video/x-raw-yuv, width = "GST_VIDEO_SIZE_RANGE" , "                 \
+  "height="GST_VIDEO_SIZE_RANGE",framerate="GST_VIDEO_FPS_RANGE","     \
+  "format= (fourcc) { I420 , NV12 , NV21 , YV12 , YUY2 , Y42B , Y444 , YUV9 , YVU9 , Y41B , Y800 , Y8 , GREY , Y16 , UYVY , YVYU , IYU1 , v308 , AYUV, v210, v216, A420, AY64 } ;" \
+  GST_VIDEO_CAPS_RGB";"                                                        \
+  GST_VIDEO_CAPS_BGR";"                                                        \
+  GST_VIDEO_CAPS_RGBx";"                                               \
+  GST_VIDEO_CAPS_xRGB";"                                               \
+  GST_VIDEO_CAPS_BGRx";"                                               \
+  GST_VIDEO_CAPS_xBGR";"                                               \
+  GST_VIDEO_CAPS_RGBA";"                                               \
+  GST_VIDEO_CAPS_ARGB";"                                               \
+  GST_VIDEO_CAPS_BGRA";"                                               \
+  GST_VIDEO_CAPS_ABGR";"                                               \
+  GST_VIDEO_CAPS_RGB_16";"                                             \
+  GST_VIDEO_CAPS_BGR_16";"                                             \
+  GST_VIDEO_CAPS_RGB_15";"                                             \
+  GST_VIDEO_CAPS_BGR_15";"                                             \
+  GST_VIDEO_CAPS_RGB8_PALETTED "; "                                     \
+  GST_VIDEO_CAPS_GRAY8";"                                              \
+  GST_VIDEO_CAPS_GRAY16("BIG_ENDIAN")";"                               \
+  GST_VIDEO_CAPS_GRAY16("LITTLE_ENDIAN")";"                             \
+  GST_VIDEO_CAPS_r210";"                                                \
+  GST_VIDEO_CAPS_ARGB_64
+
+static GstStaticPadTemplate gst_csp_src_template =
+GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS (CSP_VIDEO_CAPS)
+    );
 
-static GstStaticPadTemplate gst_colorspace_sink_template =
+static GstStaticPadTemplate gst_csp_sink_template =
 GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
-    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YV12 }"))
+    GST_STATIC_CAPS (CSP_VIDEO_CAPS)
     );
 
-static GstStaticPadTemplate gst_colorspace_src_template =
-    GST_STATIC_PAD_TEMPLATE ("src",
-    GST_PAD_SRC,
-    GST_PAD_ALWAYS,
-    GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB "; "
-        GST_VIDEO_CAPS_RGB "; " GST_VIDEO_CAPS_RGB_16)
-    );
+GType gst_csp_get_type (void);
 
-/* Stereo signals and args */
-enum
-{
-  /* FILL ME */
-  LAST_SIGNAL
-};
-
-enum
-{
-  ARG_0,
-  ARG_SOURCE,
-  ARG_DEST
-};
-
-static void gst_colorspace_base_init (gpointer g_class);
-static void gst_colorspace_class_init (GstColorspaceClass * klass);
-static void gst_colorspace_init (GstColorspace * space);
-
-static void gst_colorspace_set_property (GObject * object, guint prop_id,
-    const GValue * value, GParamSpec * pspec);
-static void gst_colorspace_get_property (GObject * object, guint prop_id,
-    GValue * value, GParamSpec * pspec);
+static void gst_csp_set_property (GObject * object,
+    guint property_id, const GValue * value, GParamSpec * pspec);
+static void gst_csp_get_property (GObject * object,
+    guint property_id, GValue * value, GParamSpec * pspec);
 
-static GstPadLinkReturn
-gst_colorspace_link (GstPad * pad, const GstCaps * caps);
-static void gst_colorspace_chain (GstPad * pad, GstData * _data);
-static GstElementStateReturn gst_colorspace_change_state (GstElement * element);
+static gboolean gst_csp_set_caps (GstBaseTransform * btrans,
+    GstCaps * incaps, GstCaps * outcaps);
+static gboolean gst_csp_get_unit_size (GstBaseTransform * btrans,
+    GstCaps * caps, gsize * size);
+static GstFlowReturn gst_csp_transform (GstBaseTransform * btrans,
+    GstBuffer * inbuf, GstBuffer * outbuf);
 
+static GQuark _QRAWRGB;         /* "video/x-raw-rgb" */
+static GQuark _QRAWYUV;         /* "video/x-raw-yuv" */
+static GQuark _QALPHAMASK;      /* "alpha_mask" */
 
-static GstElementClass *parent_class = NULL;
 
-/*static guint gst_colorspace_signals[LAST_SIGNAL] = { 0 }; */
-
-#if 0
-static gboolean
-colorspace_setup_converter (GstColorspace * space, GstCaps * from_caps,
-    GstCaps * to_caps)
+static GType
+dither_method_get_type (void)
 {
-  guint32 from_space, to_space;
-  GstStructure *from_struct;
-  GstStructure *to_struct;
-
-  g_return_val_if_fail (to_caps != NULL, FALSE);
-  g_return_val_if_fail (from_caps != NULL, FALSE);
-
-  from_struct = gst_caps_get_structure (from_caps, 0);
-  to_struct = gst_caps_get_structure (to_caps, 0);
-
-  from_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
-  gst_structure_get_fourcc (from_struct, "format", &from_space);
-
-  to_space = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
-  gst_structure_get_fourcc (to_struct, "format", &to_space);
-
-  GST_INFO ("set up converter for " GST_FOURCC_FORMAT
-      " (%08x) to " GST_FOURCC_FORMAT " (%08x)",
-      GST_FOURCC_ARGS (from_space), from_space,
-      GST_FOURCC_ARGS (to_space), to_space);
-
-  switch (from_space) {
-    case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
-    {
-      gint from_bpp;
-
-      gst_structure_get_int (from_struct, "bpp", &from_bpp);
-
-      switch (to_space) {
-        case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
-#ifdef HAVE_HERMES
-        {
-          gint to_bpp;
-
-          gst_structure_get_int (to_struct, "bpp", &to_bpp);
-
-          gst_structure_get_int (from_struct, "red_mask", &space->source.r);
-          gst_structure_get_int (from_struct, "green_mask", &space->source.g);
-          gst_structure_get_int (from_struct, "blue_mask", &space->source.b);
-          space->source.a = 0;
-          space->srcbpp = space->source.bits = from_bpp;
-          space->source.indexed = 0;
-          space->source.has_colorkey = 0;
-
-          GST_INFO ("source red mask   %08x", space->source.r);
-          GST_INFO ("source green mask %08x", space->source.g);
-          GST_INFO ("source blue mask  %08x", space->source.b);
-          GST_INFO ("source bpp        %08x", space->srcbpp);
-
-          gst_structure_get_int (to_struct, "red_mask", &space->dest.r);
-          gst_structure_get_int (to_struct, "green_mask", &space->dest.g);
-          gst_structure_get_int (to_struct, "blue_mask", &space->dest.b);
-          space->dest.a = 0;
-          space->destbpp = space->dest.bits = to_bpp;
-          space->dest.indexed = 0;
-          space->dest.has_colorkey = 0;
-
-          GST_INFO ("dest red mask   %08x", space->dest.r);
-          GST_INFO ("dest green mask %08x", space->dest.g);
-          GST_INFO ("dest blue mask  %08x", space->dest.b);
-          GST_INFO ("dest bpp        %08x", space->destbpp);
-
-          if (!Hermes_ConverterRequest (space->h_handle, &space->source,
-                  &space->dest)) {
-            g_warning ("Hermes: could not get converter\n");
-            return FALSE;
-          }
-          GST_INFO ("converter set up");
-          space->type = GST_COLORSPACE_HERMES;
-          return TRUE;
-        }
-#else
-          g_warning ("colorspace: compiled without hermes!");
-          return FALSE;
-#endif
-        case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
-          if (from_bpp == 32) {
-            space->type = GST_COLORSPACE_RGB32_YV12;
-            space->destbpp = 12;
-            return TRUE;
-          }
-        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
-          if (from_bpp == 32) {
-            space->type = GST_COLORSPACE_RGB32_I420;
-            space->destbpp = 12;
-            return TRUE;
-          }
-        case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
-          GST_INFO ("colorspace: RGB to YUV with bpp %d not implemented!!",
-              from_bpp);
-          return FALSE;
-      }
-      break;
-    }
-    case GST_MAKE_FOURCC ('I', '4', '2', '0'):
-      switch (to_space) {
-        case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
-          GST_INFO ("colorspace: YUV to RGB");
-
-          gst_structure_get_int (to_struct, "bpp", &space->destbpp);
-          space->converter =
-              gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
-          space->type = GST_COLORSPACE_YUV_RGB;
-          return TRUE;
-        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
-          space->type = GST_COLORSPACE_NONE;
-          space->destbpp = 12;
-          return TRUE;
-        case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
-          space->type = GST_COLORSPACE_420_SWAP;
-          space->destbpp = 12;
-          return TRUE;
-
-      }
-      break;
-    case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
-      switch (to_space) {
-        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
-          space->type = GST_COLORSPACE_YUY2_I420;
-          space->destbpp = 12;
-          return TRUE;
-        case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
-          space->type = GST_COLORSPACE_NONE;
-          space->destbpp = 16;
-          return TRUE;
-        case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
-          GST_INFO ("colorspace: YUY2 to RGB not implemented!!");
-          return FALSE;
-      }
-      break;
-    case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
-      switch (to_space) {
-        case GST_MAKE_FOURCC ('R', 'G', 'B', ' '):
-          GST_INFO ("colorspace: YV12 to RGB");
-
-          gst_structure_get_int (to_struct, "bpp", &space->destbpp);
-          space->converter =
-              gst_colorspace_yuv2rgb_get_converter (from_caps, to_caps);
-          space->type = GST_COLORSPACE_YUV_RGB;
-          return TRUE;
-        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
-          space->type = GST_COLORSPACE_420_SWAP;
-          space->destbpp = 12;
-          return TRUE;
-        case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
-          space->type = GST_COLORSPACE_NONE;
-          space->destbpp = 12;
-          return TRUE;
-      }
-      break;
+  static GType gtype = 0;
+
+  if (gtype == 0) {
+    static const GEnumValue values[] = {
+      {DITHER_NONE, "No dithering (default)", "none"},
+      {DITHER_VERTERR, "Vertical error propogation", "verterr"},
+      {DITHER_HALFTONE, "Half-tone", "halftone"},
+      {0, NULL, NULL}
+    };
+
+    gtype = g_enum_register_static ("GstColorspaceDitherMethod", values);
   }
-  return FALSE;
+  return gtype;
 }
-#endif
 
+/* copies the given caps */
 static GstCaps *
-gst_colorspace_caps_remove_format_info (GstCaps * caps, const char *media_type)
+gst_csp_caps_remove_format_info (GstCaps * caps)
 {
-  int i;
-  GstStructure *structure;
-
-  for (i = 0; i < gst_caps_get_size (caps); i++) {
-    structure = gst_caps_get_structure (caps, i);
-
-    gst_structure_set_name (structure, media_type);
-    gst_structure_remove_field (structure, "format");
-    gst_structure_remove_field (structure, "endianness");
-    gst_structure_remove_field (structure, "depth");
-    gst_structure_remove_field (structure, "bpp");
-    gst_structure_remove_field (structure, "red_mask");
-    gst_structure_remove_field (structure, "green_mask");
-    gst_structure_remove_field (structure, "blue_mask");
-  }
+  GstStructure *yuvst, *rgbst, *grayst;
+  gint i, n;
+  GstCaps *res;
 
-  gst_caps_do_simplify (caps);
-  return caps;
-}
+  res = gst_caps_new_empty ();
 
-static GstCaps *
-gst_colorspace_getcaps (GstPad * pad)
-{
-  GstColorspace *space;
-  GstPad *otherpad;
-  GstCaps *othercaps;
-  GstCaps *caps;
+  n = gst_caps_get_size (caps);
+  for (i = 0; i < n; i++) {
+    yuvst = gst_caps_get_structure (caps, i);
 
-  space = GST_COLORSPACE (gst_pad_get_parent (pad));
+    /* If this is already expressed by the existing caps
+     * skip this structure */
+    if (i > 0 && gst_caps_is_subset_structure (res, yuvst))
+      continue;
 
-  otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
+    yuvst = gst_structure_copy (yuvst);
+    gst_structure_set_name (yuvst, "video/x-raw-yuv");
+    gst_structure_remove_fields (yuvst, "format", "endianness", "depth",
+        "bpp", "red_mask", "green_mask", "blue_mask", "alpha_mask",
+        "palette_data", NULL);
 
-  othercaps = gst_pad_get_allowed_caps (otherpad);
+    rgbst = gst_structure_copy (yuvst);
+    gst_structure_set_name (rgbst, "video/x-raw-rgb");
+    gst_structure_remove_fields (rgbst, "color-matrix", "chroma-site", NULL);
 
-  othercaps = gst_colorspace_caps_remove_format_info (othercaps,
-      (pad == space->srcpad) ? "video/x-raw-rgb" : "video/x-raw-yuv");
+    grayst = gst_structure_copy (rgbst);
+    gst_structure_set_name (grayst, "video/x-raw-gray");
 
-  caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
-  gst_caps_free (othercaps);
+    gst_caps_append_structure (res, yuvst);
+    gst_caps_append_structure (res, rgbst);
+    gst_caps_append_structure (res, grayst);
+  }
 
-  return caps;
+  return res;
 }
 
-static GstColorSpaceFormatType
-gst_colorspace_get_format (const GstCaps * 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_csp_transform_caps (GstBaseTransform * btrans,
+    GstPadDirection direction, GstCaps * caps, GstCaps * filter)
 {
-  int i;
+  GstCaps *template;
+  GstCaps *tmp, *tmp2;
+  GstCaps *result;
 
-  for (i = 0; i < G_N_ELEMENTS (gst_colorspace_formats); i++) {
-    GstCaps *icaps;
-    GstCaps *fcaps;
+  template = gst_static_pad_template_get_caps (&gst_csp_src_template);
+  result = gst_caps_copy (caps);
 
-    fcaps =
-        gst_caps_copy (gst_static_caps_get (&gst_colorspace_formats[i].caps));
+  /* Get all possible caps that we can transform to */
+  tmp = gst_csp_caps_remove_format_info (caps);
 
-    icaps = gst_caps_intersect (caps, fcaps);
-    if (!gst_caps_is_empty (icaps)) {
-      gst_caps_free (icaps);
-      return i;
-    }
-    gst_caps_free (icaps);
+  if (filter) {
+    tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
+    gst_caps_unref (tmp);
+    tmp = tmp2;
   }
 
-  g_assert_not_reached ();
-  return -1;
-}
+  result = tmp;
 
-#define ROUND_UP_2(x)  (((x)+1)&~1)
-#define ROUND_UP_4(x)  (((x)+3)&~3)
-#define ROUND_UP_8(x)  (((x)+7)&~7)
+  GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
+      GST_PTR_FORMAT, caps, result);
 
-static int
-gst_colorspace_format_get_size (GstColorSpaceFormatType index, int width,
-    int height)
-{
-  int size;
-
-  switch (index) {
-    case GST_COLORSPACE_I420:
-    case GST_COLORSPACE_YV12:
-      size = ROUND_UP_4 (width) * ROUND_UP_2 (height);
-      size += ROUND_UP_8 (width) / 2 * ROUND_UP_2 (height) / 2;
-      size += ROUND_UP_8 (width) / 2 * ROUND_UP_2 (height) / 2;
-      return size;
-      break;
-    case GST_COLORSPACE_RGB32:
-      return width * height * 4;
-      break;
-    case GST_COLORSPACE_RGB24:
-      return ROUND_UP_4 (width * 3) * height;
-      break;
-    case GST_COLORSPACE_RGB16:
-      return ROUND_UP_4 (width * 2) * height;
-      break;
-  }
-
-  g_assert_not_reached ();
-  return 0;
+  return result;
 }
 
-static int
-gst_colorspace_get_converter (GstColorSpaceFormatType from,
-    GstColorSpaceFormatType to)
+static gboolean
+gst_csp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
+    GstCaps * outcaps)
 {
-  int i;
+  GstCsp *space;
+  GstVideoFormat in_format;
+  GstVideoFormat out_format;
+  gint in_height, in_width;
+  gint out_height, out_width;
+  gint in_fps_n, in_fps_d, in_par_n, in_par_d;
+  gint out_fps_n, out_fps_d, out_par_n, out_par_d;
+  gboolean have_in_par, have_out_par;
+  gboolean have_in_interlaced, have_out_interlaced;
+  gboolean in_interlaced, out_interlaced;
+  gboolean ret;
+  ColorSpaceColorSpec in_spec, out_spec;
+
+  space = GST_CSP (btrans);
+
+  if (space->convert) {
+    colorspace_convert_free (space->convert);
+  }
 
-  for (i = 0; i < G_N_ELEMENTS (gst_colorspace_converters); i++) {
-    GstColorspaceConverter *converter = gst_colorspace_converters + i;
+  /* input caps */
 
-    if (from == converter->from && to == converter->to) {
-      return i;
-    }
-  }
-  g_assert_not_reached ();
-  return -1;
-}
+  ret = gst_video_format_parse_caps (incaps, &in_format, &in_width, &in_height);
+  if (!ret)
+    goto no_width_height;
 
-static GstPadLinkReturn
-gst_colorspace_link (GstPad * pad, const GstCaps * caps)
-{
-  GstColorspace *space;
-  GstPad *otherpad;
-  GstStructure *structure;
-  GstPadLinkReturn link_ret;
-  int width, height;
-  double fps;
-  int format_index;
-
-  space = GST_COLORSPACE (gst_pad_get_parent (pad));
-  otherpad = (pad == space->sinkpad) ? space->srcpad : space->sinkpad;
-
-  link_ret = gst_pad_try_set_caps (otherpad, caps);
-  if (link_ret == GST_PAD_LINK_OK) {
-    return link_ret;
-  }
+  ret = gst_video_parse_caps_framerate (incaps, &in_fps_n, &in_fps_d);
+  if (!ret)
+    goto no_framerate;
 
-  structure = gst_caps_get_structure (caps, 0);
+  have_in_par = gst_video_parse_caps_pixel_aspect_ratio (incaps,
+      &in_par_n, &in_par_d);
+  have_in_interlaced = gst_video_format_parse_caps_interlaced (incaps,
+      &in_interlaced);
 
-  format_index = gst_colorspace_get_format (caps);
-  g_print ("format index is %d\n", format_index);
+  if (gst_video_format_is_rgb (in_format)) {
+    in_spec = COLOR_SPEC_RGB;
+  } else if (gst_video_format_is_yuv (in_format)) {
+    const gchar *matrix = gst_video_parse_caps_color_matrix (incaps);
 
-  gst_structure_get_int (structure, "width", &width);
-  gst_structure_get_int (structure, "height", &height);
-  gst_structure_get_double (structure, "framerate", &fps);
+    if (matrix && g_str_equal (matrix, "hdtv"))
+      in_spec = COLOR_SPEC_YUV_BT709;
+    else
+      in_spec = COLOR_SPEC_YUV_BT470_6;
+  } else {
+    in_spec = COLOR_SPEC_GRAY;
+  }
 
-  GST_INFO ("size: %dx%d", space->width, space->height);
+  /* output caps */
 
-  if (gst_pad_is_negotiated (otherpad)) {
-    GstCaps *othercaps;
+  ret =
+      gst_video_format_parse_caps (outcaps, &out_format, &out_width,
+      &out_height);
+  if (!ret)
+    goto no_width_height;
 
-    othercaps = gst_caps_copy (gst_pad_get_negotiated_caps (otherpad));
+  ret = gst_video_parse_caps_framerate (outcaps, &out_fps_n, &out_fps_d);
+  if (!ret)
+    goto no_framerate;
 
-    gst_caps_set_simple (othercaps,
-        "width", G_TYPE_INT, width,
-        "height", G_TYPE_INT, height, "framerate", G_TYPE_DOUBLE, fps, NULL);
+  have_out_par = gst_video_parse_caps_pixel_aspect_ratio (outcaps,
+      &out_par_n, &out_par_d);
+  have_out_interlaced = gst_video_format_parse_caps_interlaced (incaps,
+      &out_interlaced);
 
-    link_ret = gst_pad_try_set_caps (otherpad, othercaps);
-    if (link_ret != GST_PAD_LINK_OK) {
-      return link_ret;
-    }
-  }
+  if (gst_video_format_is_rgb (out_format)) {
+    out_spec = COLOR_SPEC_RGB;
+  } else if (gst_video_format_is_yuv (out_format)) {
+    const gchar *matrix = gst_video_parse_caps_color_matrix (outcaps);
 
-  if (pad == space->srcpad) {
-    space->src_format_index = format_index;
+    if (matrix && g_str_equal (matrix, "hdtv"))
+      out_spec = COLOR_SPEC_YUV_BT709;
+    else
+      out_spec = COLOR_SPEC_YUV_BT470_6;
   } else {
-    space->sink_format_index = format_index;
+    out_spec = COLOR_SPEC_GRAY;
   }
 
-  if (gst_pad_is_negotiated (otherpad)) {
-    space->converter_index =
-        gst_colorspace_get_converter (space->sink_format_index,
-        space->src_format_index);
+  /* these must match */
+  if (in_width != out_width || in_height != out_height ||
+      in_fps_n != out_fps_n || in_fps_d != out_fps_d)
+    goto format_mismatch;
+
+  /* if present, these must match too */
+  if (have_in_par && have_out_par &&
+      (in_par_n != out_par_n || in_par_d != out_par_d))
+    goto format_mismatch;
+
+  /* if present, these must match too */
+  if (have_in_interlaced && have_out_interlaced &&
+      in_interlaced != out_interlaced)
+    goto format_mismatch;
+
+  space->from_format = in_format;
+  space->from_spec = in_spec;
+  space->to_format = out_format;
+  space->to_spec = out_spec;
+  space->width = in_width;
+  space->height = in_height;
+  space->interlaced = in_interlaced;
+
+  space->convert = colorspace_convert_new (out_format, out_spec, in_format,
+      in_spec, in_width, in_height);
+  if (space->convert) {
+    colorspace_convert_set_interlaced (space->convert, in_interlaced);
+  }
+  /* palette, only for from data */
+  if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED &&
+      space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
+    goto format_mismatch;
+  } else if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
+    GstBuffer *palette;
+    guint32 *data;
+
+    palette = gst_video_parse_caps_palette (incaps);
+
+    if (!palette || gst_buffer_get_size (palette) < 256 * 4) {
+      if (palette)
+        gst_buffer_unref (palette);
+      goto invalid_palette;
+    }
 
-    g_print ("using index %d\n", space->converter_index);
+    data = gst_buffer_map (palette, NULL, NULL, GST_MAP_READ);
+    colorspace_convert_set_palette (space->convert, data);
+    gst_buffer_unmap (palette, data, -1);
 
-    space->sink_size = gst_colorspace_format_get_size (space->sink_format_index,
-        width, height);
-    space->src_size = gst_colorspace_format_get_size (space->src_format_index,
-        width, height);
-    space->width = width;
-    space->height = height;
-    space->fps = fps;
-  }
-#if 0
-  if (gst_pad_is_negotiated (otherpad)) {
-    g_warning ("could not get converter\n");
-    return GST_PAD_LINK_REFUSED;
+    gst_buffer_unref (palette);
+  } else if (space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
+    const guint32 *palette;
+    GstBuffer *p_buf;
+
+    palette = colorspace_convert_get_palette (space->convert);
+
+    p_buf = gst_buffer_new_and_alloc (256 * 4);
+    gst_buffer_fill (p_buf, 0, palette, 256 * 4);
+    gst_caps_set_simple (outcaps, "palette_data", GST_TYPE_BUFFER, p_buf, NULL);
+    gst_buffer_unref (p_buf);
   }
-#endif
 
-  return GST_PAD_LINK_OK;
-}
+  GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
 
-GType
-gst_colorspace_get_type (void)
-{
-  static GType colorspace_type = 0;
-
-  if (!colorspace_type) {
-    static const GTypeInfo colorspace_info = {
-      sizeof (GstColorspaceClass),
-      gst_colorspace_base_init,
-      NULL,
-      (GClassInitFunc) gst_colorspace_class_init,
-      NULL,
-      NULL,
-      sizeof (GstColorspace),
-      0,
-      (GInstanceInitFunc) gst_colorspace_init,
-    };
+  return TRUE;
 
-    colorspace_type =
-        g_type_register_static (GST_TYPE_ELEMENT, "GstColorspace",
-        &colorspace_info, 0);
+  /* ERRORS */
+no_width_height:
+  {
+    GST_ERROR_OBJECT (space, "did not specify width or height");
+    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
+    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
+    return FALSE;
+  }
+no_framerate:
+  {
+    GST_ERROR_OBJECT (space, "did not specify framerate");
+    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
+    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
+    return FALSE;
+  }
+format_mismatch:
+  {
+    GST_ERROR_OBJECT (space, "input and output formats do not match");
+    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
+    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
+    return FALSE;
+  }
+invalid_palette:
+  {
+    GST_ERROR_OBJECT (space, "invalid palette");
+    space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
+    space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
+    return FALSE;
   }
-  return colorspace_type;
 }
 
+#define gst_csp_parent_class parent_class
+G_DEFINE_TYPE (GstCsp, gst_csp, GST_TYPE_VIDEO_FILTER);
+
 static void
-gst_colorspace_base_init (gpointer g_class)
+gst_csp_finalize (GObject * obj)
 {
-  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+  GstCsp *space = GST_CSP (obj);
 
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_colorspace_src_template));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&gst_colorspace_sink_template));
+  if (space->convert) {
+    colorspace_convert_free (space->convert);
+  }
 
-  gst_element_class_set_details (element_class, &colorspace_details);
+  G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
 
 static void
-gst_colorspace_class_init (GstColorspaceClass * klass)
+gst_csp_class_init (GstCspClass * klass)
 {
-  GObjectClass *gobject_class;
-  GstElementClass *gstelement_class;
-
-  gobject_class = (GObjectClass *) klass;
-  gstelement_class = (GstElementClass *) klass;
-
-  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
-
-  gobject_class->set_property = gst_colorspace_set_property;
-  gobject_class->get_property = gst_colorspace_get_property;
-
-  gstelement_class->change_state = gst_colorspace_change_state;
-
-  gst_colorspace_table_init (NULL);
+  GObjectClass *gobject_class = (GObjectClass *) klass;
+  GstElementClass *gstelement_class = (GstElementClass *) klass;
+  GstBaseTransformClass *gstbasetransform_class =
+      (GstBaseTransformClass *) klass;
+
+  gobject_class->set_property = gst_csp_set_property;
+  gobject_class->get_property = gst_csp_get_property;
+  gobject_class->finalize = gst_csp_finalize;
+
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&gst_csp_src_template));
+  gst_element_class_add_pad_template (gstelement_class,
+      gst_static_pad_template_get (&gst_csp_sink_template));
+
+  gst_element_class_set_details_simple (gstelement_class,
+      " Colorspace converter", "Filter/Converter/Video",
+      "Converts video from one colorspace to another",
+      "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
+
+  _QRAWRGB = g_quark_from_string ("video/x-raw-rgb");
+  _QRAWYUV = g_quark_from_string ("video/x-raw-yuv");
+  _QALPHAMASK = g_quark_from_string ("alpha_mask");
+
+  gstbasetransform_class->transform_caps =
+      GST_DEBUG_FUNCPTR (gst_csp_transform_caps);
+  gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_csp_set_caps);
+  gstbasetransform_class->get_unit_size =
+      GST_DEBUG_FUNCPTR (gst_csp_get_unit_size);
+  gstbasetransform_class->transform = GST_DEBUG_FUNCPTR (gst_csp_transform);
+
+  gstbasetransform_class->passthrough_on_same_caps = TRUE;
+
+  g_object_class_install_property (gobject_class, PROP_DITHER,
+      g_param_spec_enum ("dither", "Dither", "Apply dithering while converting",
+          dither_method_get_type (), DITHER_NONE,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 }
 
 static void
-gst_colorspace_init (GstColorspace * space)
+gst_csp_init (GstCsp * space)
 {
-  space->sinkpad =
-      gst_pad_new_from_template (gst_static_pad_template_get
-      (&gst_colorspace_sink_template), "sink");
-  gst_pad_set_link_function (space->sinkpad, gst_colorspace_link);
-  gst_pad_set_getcaps_function (space->sinkpad, gst_colorspace_getcaps);
-  gst_pad_set_chain_function (space->sinkpad, gst_colorspace_chain);
-  gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
-
-  space->srcpad =
-      gst_pad_new_from_template (gst_static_pad_template_get
-      (&gst_colorspace_src_template), "src");
-  gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
-  gst_pad_set_link_function (space->srcpad, gst_colorspace_link);
+  space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
+  space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
 }
 
-static void
-gst_colorspace_chain (GstPad * pad, GstData * _data)
+void
+gst_csp_set_property (GObject * object, guint property_id,
+    const GValue * value, GParamSpec * pspec)
 {
-  GstBuffer *buf = GST_BUFFER (_data);
-  GstColorspace *space;
-  GstBuffer *outbuf = NULL;
-  GstColorspaceConverter *converter;
-
-  g_return_if_fail (pad != NULL);
-  g_return_if_fail (GST_IS_PAD (pad));
-  g_return_if_fail (buf != NULL);
+  GstCsp *csp;
 
-  space = GST_COLORSPACE (gst_pad_get_parent (pad));
+  g_return_if_fail (GST_IS_CSP (object));
+  csp = GST_CSP (object);
 
-  g_return_if_fail (space != NULL);
-  g_return_if_fail (GST_IS_COLORSPACE (space));
-
-  if (GST_BUFFER_SIZE (buf) < space->sink_size) {
-    g_critical ("input size is smaller than expected");
+  switch (property_id) {
+    case PROP_DITHER:
+      csp->dither = g_value_get_enum (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
   }
-
-  outbuf = gst_pad_alloc_buffer (space->srcpad, GST_BUFFER_OFFSET_NONE,
-      space->src_size);
-
-  converter = gst_colorspace_converters + space->converter_index;
-  converter->convert (space, GST_BUFFER_DATA (outbuf), GST_BUFFER_DATA (buf));
-
-  GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
-  GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
-
-  gst_buffer_unref (buf);
-  gst_pad_push (space->srcpad, GST_DATA (outbuf));
 }
 
-static GstElementStateReturn
-gst_colorspace_change_state (GstElement * element)
+void
+gst_csp_get_property (GObject * object, guint property_id,
+    GValue * value, GParamSpec * pspec)
 {
-  GstColorspace *space;
+  GstCsp *csp;
 
-  space = GST_COLORSPACE (element);
+  g_return_if_fail (GST_IS_CSP (object));
+  csp = GST_CSP (object);
 
-  switch (GST_STATE_TRANSITION (element)) {
-    case GST_STATE_PAUSED_TO_PLAYING:
-      break;
-    case GST_STATE_PLAYING_TO_PAUSED:
+  switch (property_id) {
+    case PROP_DITHER:
+      g_value_set_enum (value, csp->dither);
       break;
-    case GST_STATE_PAUSED_TO_READY:
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
       break;
   }
-
-  return parent_class->change_state (element);
 }
 
-static void
-gst_colorspace_set_property (GObject * object, guint prop_id,
-    const GValue * value, GParamSpec * pspec)
+static gboolean
+gst_csp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps, gsize * size)
 {
-  GstColorspace *space;
+  gboolean ret = TRUE;
+  GstVideoFormat format;
+  gint width, height;
 
-  /* it's not null if we got it, but it might not be ours */
-  g_return_if_fail (GST_IS_COLORSPACE (object));
-  space = GST_COLORSPACE (object);
+  g_assert (size);
 
-  switch (prop_id) {
-    default:
-      break;
+  ret = gst_video_format_parse_caps (caps, &format, &width, &height);
+  if (ret) {
+    *size = gst_video_format_get_size (format, width, height);
   }
+
+  return ret;
 }
 
-static void
-gst_colorspace_get_property (GObject * object, guint prop_id, GValue * value,
-    GParamSpec * pspec)
+static GstFlowReturn
+gst_csp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
+    GstBuffer * outbuf)
 {
-  GstColorspace *space;
+  GstCsp *space;
+  guint8 *indata, *outdata;
+  gsize insize, outsize;
 
-  /* it's not null if we got it, but it might not be ours */
-  g_return_if_fail (GST_IS_COLORSPACE (object));
-  space = GST_COLORSPACE (object);
+  space = GST_CSP (btrans);
 
-  switch (prop_id) {
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
+  GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
+
+  if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
+          space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
+    goto unknown_format;
+
+  colorspace_convert_set_dither (space->convert, space->dither);
+
+  indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
+  outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
+
+  colorspace_convert_convert (space->convert, outdata, indata);
+
+  gst_buffer_unmap (outbuf, outdata, outsize);
+  gst_buffer_unmap (inbuf, indata, insize);
+
+  /* baseclass copies timestamps */
+  GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
+
+  return GST_FLOW_OK;
+
+  /* ERRORS */
+unknown_format:
+  {
+    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
+        ("attempting to convert colorspaces between unknown formats"));
+    return GST_FLOW_NOT_NEGOTIATED;
+  }
+#if 0
+not_supported:
+  {
+    GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
+        ("cannot convert between formats"));
+    return GST_FLOW_NOT_SUPPORTED;
   }
+#endif
 }
 
 static gboolean
 plugin_init (GstPlugin * plugin)
 {
-  if (!gst_element_register (plugin, "colorspace", GST_RANK_PRIMARY - 1,
-          GST_TYPE_COLORSPACE))
-    return FALSE;
+  GST_DEBUG_CATEGORY_INIT (colorspace_debug, "colorspace", 0,
+      "Colorspace Converter");
+  GST_DEBUG_CATEGORY_GET (colorspace_performance, "GST_PERFORMANCE");
 
-  return TRUE;
+  return gst_element_register (plugin, "colorspace",
+      GST_RANK_NONE, GST_TYPE_CSP);
 }
 
 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
     GST_VERSION_MINOR,
-    "colorspace",
-    "internal colorspace converter",
-    plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)
+    "colorspace", "Colorspace conversion", plugin_init, VERSION, "LGPL", "", "")