documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / gst / bayer / gstbayer2rgb.c
index c8c0640..e13a5cc 100644 (file)
@@ -14,8 +14,8 @@
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  *
  * March 2008
  * Logic enhanced by William Brack <wbrack@mmm.com.hk>
@@ -23,6 +23,7 @@
 
 /**
  * SECTION:element-bayer2rgb
+ * @title: bayer2rgb
  *
  * Decodes raw camera bayer (fourcc BA81) to RGB.
  */
@@ -63,7 +64,7 @@
  * "nearest neighbor" principal, with some additional complexity for the
  * calculation of the "green" element, where an "adaptive" pairing is used.
  *
- * For purposes of documentation and indentification, each element of the
+ * For purposes of documentation and identification, each element of the
  * original array can be put into one of four classes:
  *   R   A red element
  *   B   A blue element
 #include <gst/video/video.h>
 #include <string.h>
 #include <stdlib.h>
-#include "_stdint.h"
+
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+
+#include "gstbayerorc.h"
 
 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
@@ -110,10 +116,9 @@ struct _GstBayer2RGB
   GstBaseTransform basetransform;
 
   /* < private > */
+  GstVideoInfo info;
   int width;
   int height;
-  int stride;
-  int pixsize;                  /* bytes per pixel */
   int r_off;                    /* offset for red */
   int g_off;                    /* offset for green */
   int b_off;                    /* offset for blue */
@@ -125,33 +130,21 @@ struct _GstBayer2RGBClass
   GstBaseTransformClass parent;
 };
 
-//#define SRC_CAPS GST_VIDEO_CAPS_RGBx
 #define        SRC_CAPS                                 \
-  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 ";"                         \
-  GST_VIDEO_CAPS_BGR
-
-#define SINK_CAPS "video/x-raw-bayer,format=(string){bggr,grbg,gbrg,rggb}," \
-  "width=(int)[1,MAX],height=(int)[1,MAX]"
+  GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, RGBA, ARGB, BGRA, ABGR }")
+
+#define SINK_CAPS "video/x-bayer,format=(string){bggr,grbg,gbrg,rggb}," \
+  "width=(int)[1,MAX],height=(int)[1,MAX],framerate=(fraction)[0/1,MAX]"
 
 enum
 {
   PROP_0
 };
 
-#define DEBUG_INIT(bla) \
-  GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0, "bayer2rgb element");
-
 GType gst_bayer2rgb_get_type (void);
-GST_BOILERPLATE_FULL (GstBayer2RGB, gst_bayer2rgb, GstBaseTransform,
-    GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
+
+#define gst_bayer2rgb_parent_class parent_class
+G_DEFINE_TYPE (GstBayer2RGB, gst_bayer2rgb, GST_TYPE_BASE_TRANSFORM);
 
 static void gst_bayer2rgb_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec);
@@ -164,37 +157,34 @@ static GstFlowReturn gst_bayer2rgb_transform (GstBaseTransform * base,
     GstBuffer * inbuf, GstBuffer * outbuf);
 static void gst_bayer2rgb_reset (GstBayer2RGB * filter);
 static GstCaps *gst_bayer2rgb_transform_caps (GstBaseTransform * base,
-    GstPadDirection direction, GstCaps * caps);
+    GstPadDirection direction, GstCaps * caps, GstCaps * filter);
 static gboolean gst_bayer2rgb_get_unit_size (GstBaseTransform * base,
-    GstCaps * caps, guint * size);
+    GstCaps * caps, gsize * size);
 
 
 static void
-gst_bayer2rgb_base_init (gpointer klass)
+gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
 {
-  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+  GObjectClass *gobject_class;
+  GstElementClass *gstelement_class;
+
+  gobject_class = (GObjectClass *) klass;
+  gstelement_class = (GstElementClass *) klass;
+
+  gobject_class->set_property = gst_bayer2rgb_set_property;
+  gobject_class->get_property = gst_bayer2rgb_get_property;
 
-  gst_element_class_set_details_simple (element_class,
+  gst_element_class_set_static_metadata (gstelement_class,
       "Bayer to RGB decoder for cameras", "Filter/Converter/Video",
-      "Converts video/x-raw-bayer to video/x-raw-rgb",
+      "Converts video/x-bayer to video/x-raw",
       "William Brack <wbrack@mmm.com.hk>");
 
-  gst_element_class_add_pad_template (element_class,
+  gst_element_class_add_pad_template (gstelement_class,
       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
           gst_caps_from_string (SRC_CAPS)));
-  gst_element_class_add_pad_template (element_class,
+  gst_element_class_add_pad_template (gstelement_class,
       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
           gst_caps_from_string (SINK_CAPS)));
-}
-
-static void
-gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
-{
-  GObjectClass *gobject_class;
-
-  gobject_class = (GObjectClass *) klass;
-  gobject_class->set_property = gst_bayer2rgb_set_property;
-  gobject_class->get_property = gst_bayer2rgb_get_property;
 
   GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform_caps);
@@ -204,10 +194,13 @@ gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
       GST_DEBUG_FUNCPTR (gst_bayer2rgb_set_caps);
   GST_BASE_TRANSFORM_CLASS (klass)->transform =
       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform);
+
+  GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0,
+      "bayer2rgb element");
 }
 
 static void
-gst_bayer2rgb_init (GstBayer2RGB * filter, GstBayer2RGBClass * klass)
+gst_bayer2rgb_init (GstBayer2RGB * filter)
 {
   gst_bayer2rgb_reset (filter);
   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
@@ -238,35 +231,14 @@ gst_bayer2rgb_get_property (GObject * object, guint prop_id,
   }
 }
 
-/* Routine to convert colormask value into relative byte offset */
-static int
-get_pix_offset (int mask, int bpp)
-{
-  int bpp32 = (bpp / 8) - 3;
-
-  switch (mask) {
-    case 255:
-      return 2 + bpp32;
-    case 65280:
-      return 1 + bpp32;
-    case 16711680:
-      return 0 + bpp32;
-    case -16777216:
-      return 0;
-    default:
-      GST_ERROR ("Invalid color mask 0x%08x", mask);
-      return -1;
-  }
-}
-
 static gboolean
 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
     GstCaps * outcaps)
 {
   GstBayer2RGB *bayer2rgb = GST_BAYER2RGB (base);
   GstStructure *structure;
-  int val, bpp;
   const char *format;
+  GstVideoInfo info;
 
   GST_DEBUG ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
       outcaps);
@@ -275,7 +247,6 @@ gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
 
   gst_structure_get_int (structure, "width", &bayer2rgb->width);
   gst_structure_get_int (structure, "height", &bayer2rgb->height);
-  bayer2rgb->stride = GST_ROUND_UP_4 (bayer2rgb->width);
 
   format = gst_structure_get_string (structure, "format");
   if (g_str_equal (format, "bggr")) {
@@ -291,15 +262,12 @@ gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
   }
 
   /* To cater for different RGB formats, we need to set params for later */
-  structure = gst_caps_get_structure (outcaps, 0);
-  gst_structure_get_int (structure, "bpp", &bpp);
-  bayer2rgb->pixsize = bpp / 8;
-  gst_structure_get_int (structure, "red_mask", &val);
-  bayer2rgb->r_off = get_pix_offset (val, bpp);
-  gst_structure_get_int (structure, "green_mask", &val);
-  bayer2rgb->g_off = get_pix_offset (val, bpp);
-  gst_structure_get_int (structure, "blue_mask", &val);
-  bayer2rgb->b_off = get_pix_offset (val, bpp);
+  gst_video_info_from_caps (&info, outcaps);
+  bayer2rgb->r_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 0);
+  bayer2rgb->g_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 1);
+  bayer2rgb->b_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 2);
+
+  bayer2rgb->info = info;
 
   return TRUE;
 }
@@ -309,53 +277,54 @@ gst_bayer2rgb_reset (GstBayer2RGB * filter)
 {
   filter->width = 0;
   filter->height = 0;
-  filter->stride = 0;
-  filter->pixsize = 0;
   filter->r_off = 0;
   filter->g_off = 0;
   filter->b_off = 0;
+  gst_video_info_init (&filter->info);
 }
 
 static GstCaps *
 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
-    GstPadDirection direction, GstCaps * caps)
+    GstPadDirection direction, GstCaps * caps, GstCaps * filter)
 {
+  GstBayer2RGB *bayer2rgb;
+  GstCaps *res_caps, *tmp_caps;
   GstStructure *structure;
-  GstCaps *newcaps;
-  GstStructure *newstruct;
+  guint i, caps_size;
 
-  GST_DEBUG_OBJECT (caps, "transforming caps (from)");
+  bayer2rgb = GST_BAYER2RGB (base);
 
-  structure = gst_caps_get_structure (caps, 0);
-
-  if (direction == GST_PAD_SRC) {
-    newcaps = gst_caps_from_string ("video/x-raw-bayer,"
-        "format=(string){bggr,grbg,gbrg,rggb}");
-  } else {
-    newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL);
+  res_caps = gst_caps_copy (caps);
+  caps_size = gst_caps_get_size (res_caps);
+  for (i = 0; i < caps_size; i++) {
+    structure = gst_caps_get_structure (res_caps, i);
+    if (direction == GST_PAD_SINK) {
+      gst_structure_set_name (structure, "video/x-raw");
+      gst_structure_remove_field (structure, "format");
+    } else {
+      gst_structure_set_name (structure, "video/x-bayer");
+      gst_structure_remove_fields (structure, "format", "colorimetry",
+          "chroma-site", NULL);
+    }
   }
-  newstruct = gst_caps_get_structure (newcaps, 0);
-
-  gst_structure_set_value (newstruct, "width",
-      gst_structure_get_value (structure, "width"));
-  gst_structure_set_value (newstruct, "height",
-      gst_structure_get_value (structure, "height"));
-  gst_structure_set_value (newstruct, "framerate",
-      gst_structure_get_value (structure, "framerate"));
-
-  GST_DEBUG_OBJECT (newcaps, "transforming caps (into)");
-
-  return newcaps;
+  if (filter) {
+    tmp_caps = res_caps;
+    res_caps =
+        gst_caps_intersect_full (filter, tmp_caps, GST_CAPS_INTERSECT_FIRST);
+    gst_caps_unref (tmp_caps);
+  }
+  GST_DEBUG_OBJECT (bayer2rgb, "transformed %" GST_PTR_FORMAT " into %"
+      GST_PTR_FORMAT, caps, res_caps);
+  return res_caps;
 }
 
 static gboolean
 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
-    guint * size)
+    gsize * size)
 {
   GstStructure *structure;
   int width;
   int height;
-  int pixsize;
   const char *name;
 
   structure = gst_caps_get_structure (caps, 0);
@@ -363,17 +332,14 @@ gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
   if (gst_structure_get_int (structure, "width", &width) &&
       gst_structure_get_int (structure, "height", &height)) {
     name = gst_structure_get_name (structure);
-    /* Our name must be either video/x-raw-bayer video/x-raw-rgb */
-    if (strcmp (name, "video/x-raw-rgb")) {
-      /* For bayer, we handle only BA81 (BGGR), which is BPP=24 */
+    /* Our name must be either video/x-bayer video/x-raw */
+    if (strcmp (name, "video/x-raw")) {
       *size = GST_ROUND_UP_4 (width) * height;
       return TRUE;
     } else {
-      /* For output, calculate according to format */
-      if (gst_structure_get_int (structure, "bpp", &pixsize)) {
-        *size = width * height * (pixsize / 8);
-        return TRUE;
-      }
+      /* For output, calculate according to format (always 32 bits) */
+      *size = width * height * 4;
+      return TRUE;
     }
 
   }
@@ -382,305 +348,137 @@ gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
   return FALSE;
 }
 
-/*
- * We define values for the colors, just to make the code more readable.
- */
-#define        RED     0               /* Pure red element */
-#define        GREENB  1               /* Green element which is on a blue line */
-#define        BLUE    2               /* Pure blue element */
-#define        GREENR  3               /* Green element which is on a red line */
-
-static int
-get_pixel_type (GstBayer2RGB * filter, int x, int y)
-{
-  int type;
-
-  if (((x ^ filter->format) & 1)) {
-    if ((y ^ (filter->format >> 1)) & 1)
-      type = RED;
-    else
-      type = GREENB;
-  } else {
-    if ((y ^ (filter->format >> 1)) & 1)
-      type = GREENR;
-    else
-      type = BLUE;
-  }
-  return type;
-}
-
-/* Routine to generate the top and bottom edges (not including corners) */
 static void
-hborder (uint8_t * input, uint8_t * output, int bot_top,
-    int typ, GstBayer2RGB * filter)
+gst_bayer2rgb_split_and_upsample_horiz (guint8 * dest0, guint8 * dest1,
+    const guint8 * src, int n)
 {
-  uint8_t *op;                  /* output pointer */
-  uint8_t *ip;                  /* input pointer */
-  uint8_t *nx;                  /* next line pointer */
-  int ix;                       /* loop index */
-
-  op = output + (bot_top * filter->width * (filter->height - 1) + 1) *
-      filter->pixsize;
-  ip = input + bot_top * filter->stride * (filter->height - 1);
-  /* calculate minus or plus one line, depending upon bot_top flag */
-  nx = ip + (1 - 2 * bot_top) * filter->stride;
-  /* Stepping horizontally */
-  for (ix = 1; ix < filter->width - 1; ix++, op += filter->pixsize) {
-    switch (typ) {
-      case RED:
-        op[filter->r_off] = ip[ix];
-        op[filter->g_off] = (ip[ix + 1] + ip[ix - 1] + nx[ix] + 1) / 3;
-        op[filter->b_off] = (nx[ix + 1] + nx[ix - 1] + 1) / 2;
-        typ = GREENR;
-        break;
-      case GREENR:
-        op[filter->r_off] = (ip[ix + 1] + ip[ix - 1] + 1) / 2;
-        op[filter->g_off] = ip[ix];
-        op[filter->b_off] = nx[ix];
-        typ = RED;
-        break;
-      case GREENB:
-        op[filter->r_off] = nx[ix];
-        op[filter->g_off] = ip[ix];
-        op[filter->b_off] = (ip[ix + 1] + ip[ix - 1] + 1) / 2;
-        typ = BLUE;
-        break;
-      case BLUE:
-        op[filter->r_off] = (nx[ix + 1] + nx[ix - 1] + 1) / 2;
-        op[filter->g_off] = (ip[ix + 1] + ip[ix - 1] + nx[ix] + 1) / 3;
-        op[filter->b_off] = ip[ix];
-        typ = GREENB;
-        break;
-    }
-  }
-}
+  int i;
+
+  dest0[0] = src[0];
+  dest1[0] = src[1];
+  dest0[1] = (src[0] + src[2] + 1) >> 1;
+  dest1[1] = src[1];
+
+#if defined(__i386__) || defined(__amd64__)
+  bayer_orc_horiz_upsample_unaligned (dest0 + 2, dest1 + 2, src + 1,
+      (n - 4) >> 1);
+#else
+  bayer_orc_horiz_upsample (dest0 + 2, dest1 + 2, src + 2, (n - 4) >> 1);
+#endif
 
-/* Routine to generate the left and right edges, not including corners */
-static void
-vborder (uint8_t * input, uint8_t * output, int right_left,
-    int typ, GstBayer2RGB * filter)
-{
-  uint8_t *op;                  /* output pointer */
-  uint8_t *ip;                  /* input pointer */
-  uint8_t *la;                  /* line above pointer */
-  uint8_t *lb;                  /* line below pointer */
-  int ix;                       /* loop index */
-  int lr;                       /* 'left-right' flag - +1 is right, -1 is left */
-
-  lr = (1 - 2 * right_left);
-  /* stepping vertically */
-  for (ix = 1; ix < filter->height - 1; ix++) {
-    ip = input + right_left * (filter->width - 1) + ix * filter->stride;
-    op = output + (right_left * (filter->width - 1) + ix * filter->width) *
-        filter->pixsize;
-    la = ip + filter->stride;
-    lb = ip - filter->stride;
-    switch (typ) {
-      case RED:
-        op[filter->r_off] = ip[0];
-        op[filter->g_off] = (la[0] + ip[lr] + lb[0] + 1) / 3;
-        op[filter->b_off] = (la[lr] + lb[lr] + 1) / 2;
-        typ = GREENB;
-        break;
-      case GREENR:
-        op[filter->r_off] = ip[lr];
-        op[filter->g_off] = ip[0];
-        op[filter->b_off] = (la[lr] + lb[lr] + 1) / 2;
-        typ = BLUE;
-        break;
-      case GREENB:
-        op[filter->r_off] = (la[lr] + lb[lr] + 1) / 2;
-        op[filter->g_off] = ip[0];
-        op[filter->b_off] = ip[lr];
-        typ = RED;
-        break;
-      case BLUE:
-        op[filter->r_off] = (la[lr] + lb[lr] + 1) / 2;
-        op[filter->g_off] = (la[0] + ip[lr] + lb[0] + 1) / 3;
-        op[filter->b_off] = ip[0];
-        typ = GREENR;
-        break;
+  for (i = n - 2; i < n; i++) {
+    if ((i & 1) == 0) {
+      dest0[i] = src[i];
+      dest1[i] = src[i - 1];
+    } else {
+      dest0[i] = src[i - 1];
+      dest1[i] = src[i];
     }
   }
 }
 
-/* Produce the four (top, bottom, left, right) edges */
-static void
-do_row0_col0 (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
-{
-  /* Horizontal edges */
-  hborder (input, output, 0, get_pixel_type (filter, 1, 0), filter);
-  hborder (input, output, 1, get_pixel_type (filter, 1, filter->height - 1),
-      filter);
-
-  /* Vertical edges */
-  vborder (input, output, 0, get_pixel_type (filter, 0, 1), filter);
-  vborder (input, output, 1, get_pixel_type (filter, filter->width - 1, 1),
-      filter);
-}
+typedef void (*process_func) (guint8 * d0, const guint8 * s0, const guint8 * s1,
+    const guint8 * s2, const guint8 * s3, const guint8 * s4, const guint8 * s5,
+    int n);
 
 static void
-corner (uint8_t * input, uint8_t * output, int x, int y,
-    int xd, int yd, int typ, GstBayer2RGB * filter)
+gst_bayer2rgb_process (GstBayer2RGB * bayer2rgb, uint8_t * dest,
+    int dest_stride, uint8_t * src, int src_stride)
 {
-  uint8_t *ip;                  /* input pointer */
-  uint8_t *op;                  /* output pointer */
-  uint8_t *nx;                  /* adjacent line */
-
-  op = output + y * filter->width * filter->pixsize + x * filter->pixsize;
-  ip = input + y * filter->stride + x;
-  nx = ip + yd * filter->stride;
-  switch (typ) {
-    case RED:
-      op[filter->r_off] = ip[0];
-      op[filter->g_off] = (nx[0] + ip[xd] + 1) / 2;
-      op[filter->b_off] = nx[xd];
-      break;
-    case GREENR:
-      op[filter->r_off] = ip[xd];
-      op[filter->g_off] = ip[0];
-      op[filter->b_off] = nx[0];
-      break;
-    case GREENB:
-      op[filter->r_off] = nx[0];
-      op[filter->g_off] = ip[0];
-      op[filter->b_off] = ip[xd];
-      break;
-    case BLUE:
-      op[filter->r_off] = nx[xd];
-      op[filter->g_off] = (nx[0] + ip[xd] + 1) / 2;
-      op[filter->b_off] = ip[0];
-      break;
+  int j;
+  guint8 *tmp;
+  process_func merge[2] = { NULL, NULL };
+  int r_off, g_off, b_off;
+
+  /* We exploit some symmetry in the functions here.  The base functions
+   * are all named for the BGGR arrangement.  For RGGB, we swap the
+   * red offset and blue offset in the output.  For GRBG, we swap the
+   * order of the merge functions.  For GBRG, do both. */
+  r_off = bayer2rgb->r_off;
+  g_off = bayer2rgb->g_off;
+  b_off = bayer2rgb->b_off;
+  if (bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_RGGB ||
+      bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GBRG) {
+    r_off = bayer2rgb->b_off;
+    b_off = bayer2rgb->r_off;
   }
-}
 
-static void
-do_corners (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
-{
-  /* Top left */
-  corner (input, output, 0, 0, 1, 1, get_pixel_type (filter, 0, 0), filter);
-  /* Bottom left */
-  corner (input, output, 0, filter->height - 1, 1, -1,
-      get_pixel_type (filter, 0, filter->height - 1), filter);
-  /* Top right */
-  corner (input, output, filter->width - 1, 0, -1, 0,
-      get_pixel_type (filter, filter->width - 1, 0), filter);
-  /* Bottom right */
-  corner (input, output, filter->width - 1, filter->height - 1, -1, -1,
-      get_pixel_type (filter, filter->width - 1, filter->height - 1), filter);
-}
+  if (r_off == 2 && g_off == 1 && b_off == 0) {
+    merge[0] = bayer_orc_merge_bg_bgra;
+    merge[1] = bayer_orc_merge_gr_bgra;
+  } else if (r_off == 3 && g_off == 2 && b_off == 1) {
+    merge[0] = bayer_orc_merge_bg_abgr;
+    merge[1] = bayer_orc_merge_gr_abgr;
+  } else if (r_off == 1 && g_off == 2 && b_off == 3) {
+    merge[0] = bayer_orc_merge_bg_argb;
+    merge[1] = bayer_orc_merge_gr_argb;
+  } else if (r_off == 0 && g_off == 1 && b_off == 2) {
+    merge[0] = bayer_orc_merge_bg_rgba;
+    merge[1] = bayer_orc_merge_gr_rgba;
+  }
+  if (bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GRBG ||
+      bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GBRG) {
+    process_func tmp = merge[0];
+    merge[0] = merge[1];
+    merge[1] = tmp;
+  }
 
-static void
-do_body (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
-{
-  int ip, op;                   /* input and output pointers */
-  int w, h;                     /* loop indices */
-  int type;                     /* calculated colour of current element */
-  int a1, a2;
-  int v1, v2, h1, h2;
-
-  /*
-   * We are processing row (line) by row, starting with the second
-   * row and continuing through the next to last.  Each row is processed
-   * column by column, starting with the second and continuing through
-   * to the next to last.
-   */
-  for (h = 1; h < filter->height - 1; h++) {
-    /*
-     * Remember we are processing "row by row". For each row, we need
-     * to set the type of the first element to be processed.  Since we
-     * have already processed the edges, the "first element" will be
-     * the pixel at position (1,1).  Assuming BG format, this should
-     * be RED for odd-numbered rows and GREENB for even rows.
-     */
-    type = get_pixel_type (filter, 1, h);
-    /* Calculate the starting position for the row */
-    op = h * filter->width * filter->pixsize;   /* output (converted) pos */
-    ip = h * filter->stride;    /* input (bayer data) pos */
-    for (w = 1; w < filter->width - 1; w++) {
-      op += filter->pixsize;    /* we are processing "horizontally" */
-      ip++;
-      switch (type) {
-        case RED:
-          output[op + filter->r_off] = input[ip];
-          output[op + filter->b_off] = (input[ip - filter->stride - 1] +
-              input[ip - filter->stride + 1] +
-              input[ip + filter->stride - 1] +
-              input[ip + filter->stride + 1] + 2) / 4;
-          v1 = input[ip + filter->stride];
-          v2 = input[ip - filter->stride];
-          h1 = input[ip + 1];
-          h2 = input[ip - 1];
-          a1 = abs (v1 - v2);
-          a2 = abs (h1 - h2);
-          if (a1 < a2)
-            output[op + filter->g_off] = (v1 + v2 + 1) / 2;
-          else if (a1 > a2)
-            output[op + filter->g_off] = (h1 + h2 + 1) / 2;
-          else
-            output[op + filter->g_off] = (v1 + h1 + v2 + h2 + 2) / 4;
-          type = GREENR;
-          break;
-        case GREENR:
-          output[op + filter->r_off] = (input[ip + 1] + input[ip - 1] + 1) / 2;
-          output[op + filter->g_off] = input[ip];
-          output[op + filter->b_off] = (input[ip - filter->stride] +
-              input[ip + filter->stride] + 1) / 2;
-          type = RED;
-          break;
-        case GREENB:
-          output[op + filter->r_off] = (input[ip - filter->stride] +
-              input[ip + filter->stride] + 1) / 2;
-          output[op + filter->g_off] = input[ip];
-          output[op + filter->b_off] = (input[ip + 1] + input[ip - 1] + 1) / 2;
-          type = BLUE;
-          break;
-        case BLUE:
-          output[op + filter->r_off] = (input[ip - filter->stride - 1] +
-              input[ip - filter->stride + 1] +
-              input[ip + filter->stride - 1] +
-              input[ip + filter->stride + 1] + 2) / 4;
-          output[op + filter->b_off] = input[ip];
-          v1 = input[ip + filter->stride];
-          v2 = input[ip - filter->stride];
-          h1 = input[ip + 1];
-          h2 = input[ip - 1];
-          a1 = abs (v1 - v2);
-          a2 = abs (h1 - h2);
-          if (a1 < a2)
-            output[op + filter->g_off] = (v1 + v2 + 1) / 2;
-          else if (a1 > a2)
-            output[op + filter->g_off] = (h1 + h2 + 1) / 2;
-          else
-            output[op + filter->g_off] = (v1 + h1 + v2 + h2 + 2) / 4;
-          type = GREENB;
-          break;
-      }
+  tmp = g_malloc (2 * 4 * bayer2rgb->width);
+#define LINE(x) (tmp + ((x)&7) * bayer2rgb->width)
+
+  gst_bayer2rgb_split_and_upsample_horiz (LINE (3 * 2 + 0), LINE (3 * 2 + 1),
+      src + 1 * src_stride, bayer2rgb->width);
+  j = 0;
+  gst_bayer2rgb_split_and_upsample_horiz (LINE (j * 2 + 0), LINE (j * 2 + 1),
+      src + j * src_stride, bayer2rgb->width);
+
+  for (j = 0; j < bayer2rgb->height; j++) {
+    if (j < bayer2rgb->height - 1) {
+      gst_bayer2rgb_split_and_upsample_horiz (LINE ((j + 1) * 2 + 0),
+          LINE ((j + 1) * 2 + 1), src + (j + 1) * src_stride, bayer2rgb->width);
     }
+
+    merge[j & 1] (dest + j * dest_stride,
+        LINE (j * 2 - 2), LINE (j * 2 - 1),
+        LINE (j * 2 + 0), LINE (j * 2 + 1),
+        LINE (j * 2 + 2), LINE (j * 2 + 3), bayer2rgb->width >> 1);
   }
+
+  g_free (tmp);
 }
 
+
+
+
 static GstFlowReturn
 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
     GstBuffer * outbuf)
 {
   GstBayer2RGB *filter = GST_BAYER2RGB (base);
-  uint8_t *input, *output;
-
-  /*
-   * We need to lock our filter params to prevent changing
-   * caps in the middle of a transformation (nice way to get
-   * segfaults)
-   */
-  GST_OBJECT_LOCK (filter);
+  GstMapInfo map;
+  uint8_t *output;
+  GstVideoFrame frame;
 
   GST_DEBUG ("transforming buffer");
-  input = (uint8_t *) GST_BUFFER_DATA (inbuf);
-  output = (uint8_t *) GST_BUFFER_DATA (outbuf);
-  do_corners (input, output, filter);
-  do_row0_col0 (input, output, filter);
-  do_body (input, output, filter);
 
-  GST_OBJECT_UNLOCK (filter);
+  if (!gst_buffer_map (inbuf, &map, GST_MAP_READ))
+    goto map_failed;
+
+  if (!gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE)) {
+    gst_buffer_unmap (inbuf, &map);
+    goto map_failed;
+  }
+
+  output = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
+  gst_bayer2rgb_process (filter, output, frame.info.stride[0],
+      map.data, GST_ROUND_UP_4 (filter->width));
+
+  gst_video_frame_unmap (&frame);
+  gst_buffer_unmap (inbuf, &map);
+
+  return GST_FLOW_OK;
+
+map_failed:
+  GST_WARNING_OBJECT (base, "Could not map buffer, skipping");
   return GST_FLOW_OK;
 }