Add -Wmissing-declarations -Wmissing-prototypes to configure flags
[platform/upstream/gstreamer.git] / gst / bayer / gstbayer2rgb.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2007 David Schleef <ds@schleef.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * March 2008
21  * Logic enhanced by William Brack <wbrack@mmm.com.hk>
22  */
23
24 /**
25  * SECTION:element-bayer2rgb
26  *
27  * Decodes raw camera bayer (fourcc BA81) to RGB.
28  */
29
30 /*
31  * In order to guard against my advancing maturity, some extra detailed
32  * information about the logic of the decode is included here.  Much of
33  * this was inspired by a technical paper from siliconimaging.com, which
34  * in turn was based upon an article from IEEE,
35  * T. Sakamoto, C. Nakanishi and T. Hase,
36  * “Software pixel interpolation for digital still cameras suitable for
37  *  a 32-bit MCU,”
38  * IEEE Trans. Consumer Electronics, vol. 44, no. 4, November 1998.
39  *
40  * The code assumes a Bayer matrix of the type produced by the fourcc
41  * BA81 (v4l2 format SBGGR8) of width w and height h which looks like:
42  *       0 1 2 3  w-2 w-1
43  *
44  *   0   B G B G ....B G
45  *   1   G R G R ....G R
46  *   2   B G B G ....B G
47  *       ...............
48  * h-2   B G B G ....B G
49  * h-1   G R G R ....G R
50  *
51  * We expand this matrix, producing a separate {r, g, b} triple for each
52  * of the individual elements.  The algorithm for doing this expansion is
53  * as follows.
54  *
55  * We are designing for speed of transformation, at a slight expense of code.
56  * First, we calculate the appropriate triples for the four corners, the
57  * remainder of the top and bottom rows, and the left and right columns.
58  * The reason for this is that those elements are transformed slightly
59  * differently than all of the remainder of the matrix. Finally, we transform
60  * all of the remainder.
61  *
62  * The transformation into the "appropriate triples" is based upon the
63  * "nearest neighbor" principal, with some additional complexity for the
64  * calculation of the "green" element, where an "adaptive" pairing is used.
65  *
66  * For purposes of documentation and indentification, each element of the
67  * original array can be put into one of four classes:
68  *   R   A red element
69  *   B   A blue element
70  *   GR  A green element which is followed by a red one
71  *   GB  A green element which is followed by a blue one
72  */
73
74 #ifdef HAVE_CONFIG_H
75 #include "config.h"
76 #endif
77
78 #include <gst/gst.h>
79 #include <gst/base/gstbasetransform.h>
80 #include <gst/video/video.h>
81 #include <string.h>
82 #include <stdlib.h>
83 #include "_stdint.h"
84
85 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
86 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
87
88 #define GST_TYPE_BAYER2RGB            (gst_bayer2rgb_get_type())
89 #define GST_BAYER2RGB(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BAYER2RGB,GstBayer2RGB))
90 #define GST_IS_BAYER2RGB(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BAYER2RGB))
91 #define GST_BAYER2RGB_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
92 #define GST_IS_BAYER2RGB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_BAYER2RGB))
93 #define GST_BAYER2RGB_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
94 typedef struct _GstBayer2RGB GstBayer2RGB;
95 typedef struct _GstBayer2RGBClass GstBayer2RGBClass;
96
97 typedef void (*GstBayer2RGBProcessFunc) (GstBayer2RGB *, guint8 *, guint);
98
99 struct _GstBayer2RGB
100 {
101   GstBaseTransform basetransform;
102
103   /* < private > */
104   int width;
105   int height;
106   int stride;
107   int pixsize;                  /* bytes per pixel */
108   int r_off;                    /* offset for red */
109   int g_off;                    /* offset for green */
110   int b_off;                    /* offset for blue */
111 };
112
113 struct _GstBayer2RGBClass
114 {
115   GstBaseTransformClass parent;
116 };
117
118 //#define SRC_CAPS GST_VIDEO_CAPS_RGBx
119 #define SRC_CAPS                                 \
120   GST_VIDEO_CAPS_RGBx ";"                        \
121   GST_VIDEO_CAPS_xRGB ";"                        \
122   GST_VIDEO_CAPS_BGRx ";"                        \
123   GST_VIDEO_CAPS_xBGR ";"                        \
124   GST_VIDEO_CAPS_RGBA ";"                        \
125   GST_VIDEO_CAPS_ARGB ";"                        \
126   GST_VIDEO_CAPS_BGRA ";"                        \
127   GST_VIDEO_CAPS_ABGR ";"                        \
128   GST_VIDEO_CAPS_RGB ";"                         \
129   GST_VIDEO_CAPS_BGR
130
131 #define SINK_CAPS "video/x-raw-bayer,width=(int)[1,MAX],height=(int)[1,MAX]"
132
133 enum
134 {
135   PROP_0
136 };
137
138 #define DEBUG_INIT(bla) \
139   GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0, "bayer2rgb element");
140
141 GType gst_bayer2rgb_get_type (void);
142 GST_BOILERPLATE_FULL (GstBayer2RGB, gst_bayer2rgb, GstBaseTransform,
143     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
144
145 static void gst_bayer2rgb_set_property (GObject * object, guint prop_id,
146     const GValue * value, GParamSpec * pspec);
147 static void gst_bayer2rgb_get_property (GObject * object, guint prop_id,
148     GValue * value, GParamSpec * pspec);
149
150 static gboolean gst_bayer2rgb_set_caps (GstBaseTransform * filter,
151     GstCaps * incaps, GstCaps * outcaps);
152 static GstFlowReturn gst_bayer2rgb_transform (GstBaseTransform * base,
153     GstBuffer * inbuf, GstBuffer * outbuf);
154 static void gst_bayer2rgb_reset (GstBayer2RGB * filter);
155 static GstCaps *gst_bayer2rgb_transform_caps (GstBaseTransform * base,
156     GstPadDirection direction, GstCaps * caps);
157 static gboolean gst_bayer2rgb_get_unit_size (GstBaseTransform * base,
158     GstCaps * caps, guint * size);
159
160
161 static void
162 gst_bayer2rgb_base_init (gpointer klass)
163 {
164   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
165
166   gst_element_class_set_details_simple (element_class,
167       "Bayer to RGB decoder for cameras", "Filter/Converter/Video",
168       "Converts video/x-raw-bayer to video/x-raw-rgb",
169       "William Brack <wbrack@mmm.com.hk>");
170
171   gst_element_class_add_pad_template (element_class,
172       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
173           gst_caps_from_string (SRC_CAPS)));
174   gst_element_class_add_pad_template (element_class,
175       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
176           gst_caps_from_string (SINK_CAPS)));
177 }
178
179 static void
180 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
181 {
182   GObjectClass *gobject_class;
183
184   gobject_class = (GObjectClass *) klass;
185   gobject_class->set_property = gst_bayer2rgb_set_property;
186   gobject_class->get_property = gst_bayer2rgb_get_property;
187
188   GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
189       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform_caps);
190   GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
191       GST_DEBUG_FUNCPTR (gst_bayer2rgb_get_unit_size);
192   GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
193       GST_DEBUG_FUNCPTR (gst_bayer2rgb_set_caps);
194   GST_BASE_TRANSFORM_CLASS (klass)->transform =
195       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform);
196 }
197
198 static void
199 gst_bayer2rgb_init (GstBayer2RGB * filter, GstBayer2RGBClass * klass)
200 {
201   gst_bayer2rgb_reset (filter);
202   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
203 }
204
205 /* No properties are implemented, so only a warning is produced */
206 static void
207 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
208     const GValue * value, GParamSpec * pspec)
209 {
210
211   switch (prop_id) {
212     default:
213       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214       break;
215   }
216 }
217
218 static void
219 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
220     GValue * value, GParamSpec * pspec)
221 {
222
223   switch (prop_id) {
224     default:
225       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226       break;
227   }
228 }
229
230 /* Routine to convert colormask value into relative byte offset */
231 static int
232 get_pix_offset (int mask, int bpp)
233 {
234   int bpp32 = (bpp / 8) - 3;
235
236   switch (mask) {
237     case 255:
238       return 2 + bpp32;
239     case 65280:
240       return 1 + bpp32;
241     case 16711680:
242       return 0 + bpp32;
243     case -16777216:
244       return 0;
245     default:
246       GST_ERROR ("Invalid color mask 0x%08x", mask);
247       return -1;
248   }
249 }
250
251 static gboolean
252 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
253     GstCaps * outcaps)
254 {
255   GstBayer2RGB *filter = GST_BAYER2RGB (base);
256   GstStructure *structure;
257   int val, bpp;
258
259   GST_DEBUG ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
260       outcaps);
261
262   structure = gst_caps_get_structure (incaps, 0);
263
264   gst_structure_get_int (structure, "width", &filter->width);
265   gst_structure_get_int (structure, "height", &filter->height);
266   filter->stride = GST_ROUND_UP_4 (filter->width);
267
268   /* To cater for different RGB formats, we need to set params for later */
269   structure = gst_caps_get_structure (outcaps, 0);
270   gst_structure_get_int (structure, "bpp", &bpp);
271   filter->pixsize = bpp / 8;
272   gst_structure_get_int (structure, "red_mask", &val);
273   filter->r_off = get_pix_offset (val, bpp);
274   gst_structure_get_int (structure, "green_mask", &val);
275   filter->g_off = get_pix_offset (val, bpp);
276   gst_structure_get_int (structure, "blue_mask", &val);
277   filter->b_off = get_pix_offset (val, bpp);
278
279   return TRUE;
280 }
281
282 static void
283 gst_bayer2rgb_reset (GstBayer2RGB * filter)
284 {
285   filter->width = 0;
286   filter->height = 0;
287   filter->stride = 0;
288   filter->pixsize = 0;
289   filter->r_off = 0;
290   filter->g_off = 0;
291   filter->b_off = 0;
292 }
293
294 static GstCaps *
295 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
296     GstPadDirection direction, GstCaps * caps)
297 {
298   GstStructure *structure;
299   GstCaps *newcaps;
300   GstStructure *newstruct;
301
302   GST_DEBUG_OBJECT (caps, "transforming caps (from)");
303
304   structure = gst_caps_get_structure (caps, 0);
305
306   if (direction == GST_PAD_SRC) {
307     newcaps = gst_caps_new_simple ("video/x-raw-bayer", NULL);
308   } else {
309     newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL);
310   }
311   newstruct = gst_caps_get_structure (newcaps, 0);
312
313   gst_structure_set_value (newstruct, "width",
314       gst_structure_get_value (structure, "width"));
315   gst_structure_set_value (newstruct, "height",
316       gst_structure_get_value (structure, "height"));
317   gst_structure_set_value (newstruct, "framerate",
318       gst_structure_get_value (structure, "framerate"));
319
320   GST_DEBUG_OBJECT (newcaps, "transforming caps (into)");
321
322   return newcaps;
323 }
324
325 static gboolean
326 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
327     guint * size)
328 {
329   GstStructure *structure;
330   int width;
331   int height;
332   int pixsize;
333   const char *name;
334
335   structure = gst_caps_get_structure (caps, 0);
336
337   if (gst_structure_get_int (structure, "width", &width) &&
338       gst_structure_get_int (structure, "height", &height)) {
339     name = gst_structure_get_name (structure);
340     /* Our name must be either video/x-raw-bayer video/x-raw-rgb */
341     if (strcmp (name, "video/x-raw-rgb")) {
342       /* For bayer, we handle only BA81 (BGGR), which is BPP=24 */
343       *size = GST_ROUND_UP_4 (width) * height;
344       return TRUE;
345     } else {
346       /* For output, calculate according to format */
347       if (gst_structure_get_int (structure, "bpp", &pixsize)) {
348         *size = width * height * (pixsize / 8);
349         return TRUE;
350       }
351     }
352
353   }
354   GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
355       ("Incomplete caps, some required field missing"));
356   return FALSE;
357 }
358
359 /*
360  * We define values for the colors, just to make the code more readable.
361  */
362 #define RED     0               /* Pure red element */
363 #define GREENB  1               /* Green element which is on a blue line */
364 #define BLUE    2               /* Pure blue element */
365 #define GREENR  3               /* Green element which is on a red line */
366
367 /* Routine to generate the top and bottom edges (not including corners) */
368 static void
369 hborder (uint8_t * input, uint8_t * output, int bot_top,
370     int typ, GstBayer2RGB * filter)
371 {
372   uint8_t *op;                  /* output pointer */
373   uint8_t *ip;                  /* input pointer */
374   uint8_t *nx;                  /* next line pointer */
375   int ix;                       /* loop index */
376
377   op = output + (bot_top * filter->width * (filter->height - 1) + 1) *
378       filter->pixsize;
379   ip = input + bot_top * filter->stride * (filter->height - 1);
380   /* calculate minus or plus one line, depending upon bot_top flag */
381   nx = ip + (1 - 2 * bot_top) * filter->stride;
382   /* Stepping horizontally */
383   for (ix = 1; ix < filter->width - 1; ix++, op += filter->pixsize) {
384     switch (typ) {
385       case RED:
386         op[filter->r_off] = ip[ix];
387         op[filter->g_off] = (ip[ix + 1] + ip[ix - 1] + nx[ix] + 1) / 3;
388         op[filter->b_off] = (nx[ix + 1] + nx[ix - 1] + 1) / 2;
389         typ = GREENR;
390         break;
391       case GREENR:
392         op[filter->r_off] = (ip[ix + 1] + ip[ix - 1] + 1) / 2;
393         op[filter->g_off] = ip[ix];
394         op[filter->b_off] = nx[ix];
395         typ = RED;
396         break;
397       case GREENB:
398         op[filter->r_off] = nx[ix];
399         op[filter->g_off] = ip[ix];
400         op[filter->b_off] = (ip[ix + 1] + ip[ix - 1] + 1) / 2;
401         typ = BLUE;
402         break;
403       case BLUE:
404         op[filter->r_off] = (nx[ix + 1] + nx[ix - 1] + 1) / 2;
405         op[filter->g_off] = (ip[ix + 1] + ip[ix - 1] + nx[ix] + 1) / 3;
406         op[filter->b_off] = ip[ix];
407         typ = GREENB;
408         break;
409     }
410   }
411 }
412
413 /* Routine to generate the left and right edges, not including corners */
414 static void
415 vborder (uint8_t * input, uint8_t * output, int right_left,
416     int typ, GstBayer2RGB * filter)
417 {
418   uint8_t *op;                  /* output pointer */
419   uint8_t *ip;                  /* input pointer */
420   uint8_t *la;                  /* line above pointer */
421   uint8_t *lb;                  /* line below pointer */
422   int ix;                       /* loop index */
423   int lr;                       /* 'left-right' flag - +1 is right, -1 is left */
424
425   lr = (1 - 2 * right_left);
426   /* stepping vertically */
427   for (ix = 1; ix < filter->height - 1; ix++) {
428     ip = input + right_left * (filter->width - 1) + ix * filter->stride;
429     op = output + (right_left * (filter->width - 1) + ix * filter->width) *
430         filter->pixsize;
431     la = ip + filter->stride;
432     lb = ip - filter->stride;
433     switch (typ) {
434       case RED:
435         op[filter->r_off] = ip[0];
436         op[filter->g_off] = (la[0] + ip[lr] + lb[0] + 1) / 3;
437         op[filter->b_off] = (la[lr] + lb[lr] + 1) / 2;
438         typ = GREENB;
439         break;
440       case GREENR:
441         op[filter->r_off] = ip[lr];
442         op[filter->g_off] = ip[0];
443         op[filter->b_off] = (la[lr] + lb[lr] + 1) / 2;
444         typ = BLUE;
445         break;
446       case GREENB:
447         op[filter->r_off] = (la[lr] + lb[lr] + 1) / 2;
448         op[filter->g_off] = ip[0];
449         op[filter->b_off] = ip[lr];
450         typ = RED;
451         break;
452       case BLUE:
453         op[filter->r_off] = (la[lr] + lb[lr] + 1) / 2;
454         op[filter->g_off] = (la[0] + ip[lr] + lb[0] + 1) / 3;
455         op[filter->b_off] = ip[0];
456         typ = GREENR;
457         break;
458     }
459   }
460 }
461
462 /* Produce the four (top, bottom, left, right) edges */
463 static void
464 do_row0_col0 (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
465 {
466   int type;
467
468   /* Horizontal edges */
469   hborder (input, output, 0, GREENB, filter);
470   if (filter->height & 1)
471     type = GREENB;              /* odd # rows, "bottom" edge same as top */
472   else
473     type = RED;                 /* even #, bottom side different */
474   hborder (input, output, 1, type, filter);
475
476   /* Vertical edges */
477   vborder (input, output, 0, GREENR, filter);
478   if (filter->width & 1)
479     type = GREENR;              /* odd # cols, "right" edge same as left */
480   else
481     type = RED;                 /* even #, right side different */
482   vborder (input, output, 1, type, filter);
483 }
484
485 static void
486 corner (uint8_t * input, uint8_t * output, int x, int y,
487     int xd, int yd, int typ, GstBayer2RGB * filter)
488 {
489   uint8_t *ip;                  /* input pointer */
490   uint8_t *op;                  /* output pointer */
491   uint8_t *nx;                  /* adjacent line */
492
493   op = output + y * filter->width * filter->pixsize + x * filter->pixsize;
494   ip = input + y * filter->stride + x;
495   nx = ip + yd * filter->stride;
496   switch (typ) {
497     case RED:
498       op[filter->r_off] = ip[0];
499       op[filter->g_off] = (nx[0] + ip[xd] + 1) / 2;
500       op[filter->b_off] = nx[xd];
501       break;
502     case GREENR:
503       op[filter->r_off] = ip[xd];
504       op[filter->g_off] = ip[0];
505       op[filter->b_off] = nx[0];
506       break;
507     case GREENB:
508       op[filter->r_off] = nx[0];
509       op[filter->g_off] = ip[0];
510       op[filter->b_off] = ip[xd];
511       break;
512     case BLUE:
513       op[filter->r_off] = nx[xd];
514       op[filter->g_off] = (nx[0] + ip[xd] + 1) / 2;
515       op[filter->b_off] = ip[0];
516       break;
517   }
518 }
519
520 static void
521 do_corners (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
522 {
523   int typ;
524
525   /* Top left */
526   corner (input, output, 0, 0, 1, 1, BLUE, filter);
527   /* Bottom left */
528   corner (input, output, 0, filter->height - 1, 1, -1,
529       (filter->height & 1) ? BLUE : GREENR, filter);
530   /* Top right */
531   corner (input, output, filter->width - 1, 0, -1, 0,
532       (filter->width & 1) ? BLUE : GREENB, filter);
533   /* Bottom right */
534   if (filter->width & 1)        /* if odd  # cols, B or GB */
535     typ = BLUE;
536   else
537     typ = GREENB;               /* if even # cols, B or GR */
538   typ |= (filter->height & 1);  /* if odd  # rows, GB or GR */
539   corner (input, output, filter->width - 1, filter->height - 1, -1, -1,
540       typ, filter);
541 }
542
543 static void
544 do_body (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
545 {
546   int ip, op;                   /* input and output pointers */
547   int w, h;                     /* loop indices */
548   int type;                     /* calculated colour of current element */
549   int a1, a2;
550   int v1, v2, h1, h2;
551
552   /*
553    * We are processing row (line) by row, starting with the second
554    * row and continuing through the next to last.  Each row is processed
555    * column by column, starting with the second and continuing through
556    * to the next to last.
557    */
558   for (h = 1; h < filter->height - 1; h++) {
559     /*
560      * Remember we are processing "row by row". For each row, we need
561      * to set the type of the first element to be processed.  Since we
562      * have already processed the edges, the "first element" will be
563      * the pixel at position (1,1).  Assuming BG format, this should
564      * be RED for odd-numbered rows and GREENB for even rows.
565      */
566     if (h & 1)
567       type = RED;
568     else
569       type = GREENB;
570     /* Calculate the starting position for the row */
571     op = h * filter->width * filter->pixsize;   /* output (converted) pos */
572     ip = h * filter->stride;    /* input (bayer data) pos */
573     for (w = 1; w < filter->width - 1; w++) {
574       op += filter->pixsize;    /* we are processing "horizontally" */
575       ip++;
576       switch (type) {
577         case RED:
578           output[op + filter->r_off] = input[ip];
579           output[op + filter->b_off] = (input[ip - filter->stride - 1] +
580               input[ip - filter->stride + 1] +
581               input[ip + filter->stride - 1] +
582               input[ip + filter->stride + 1] + 2) / 4;
583           v1 = input[ip + filter->stride];
584           v2 = input[ip - filter->stride];
585           h1 = input[ip + 1];
586           h2 = input[ip - 1];
587           a1 = abs (v1 - v2);
588           a2 = abs (h1 - h2);
589           if (a1 < a2)
590             output[op + filter->g_off] = (v1 + v2 + 1) / 2;
591           else if (a1 > a2)
592             output[op + filter->g_off] = (h1 + h2 + 1) / 2;
593           else
594             output[op + filter->g_off] = (v1 + h1 + v2 + h2 + 2) / 4;
595           type = GREENR;
596           break;
597         case GREENR:
598           output[op + filter->r_off] = (input[ip + 1] + input[ip - 1] + 1) / 2;
599           output[op + filter->g_off] = input[ip];
600           output[op + filter->b_off] = (input[ip - filter->stride] +
601               input[ip + filter->stride] + 1) / 2;
602           type = RED;
603           break;
604         case GREENB:
605           output[op + filter->r_off] = (input[ip - filter->stride] +
606               input[ip + filter->stride] + 1) / 2;
607           output[op + filter->g_off] = input[ip];
608           output[op + filter->b_off] = (input[ip + 1] + input[ip - 1] + 1) / 2;
609           type = BLUE;
610           break;
611         case BLUE:
612           output[op + filter->r_off] = (input[ip - filter->stride - 1] +
613               input[ip - filter->stride + 1] +
614               input[ip + filter->stride - 1] +
615               input[ip + filter->stride + 1] + 2) / 4;
616           output[op + filter->b_off] = input[ip];
617           v1 = input[ip + filter->stride];
618           v2 = input[ip - filter->stride];
619           h1 = input[ip + 1];
620           h2 = input[ip - 1];
621           a1 = abs (v1 - v2);
622           a2 = abs (h1 - h2);
623           if (a1 < a2)
624             output[op + filter->g_off] = (v1 + v2 + 1) / 2;
625           else if (a1 > a2)
626             output[op + filter->g_off] = (h1 + h2 + 1) / 2;
627           else
628             output[op + filter->g_off] = (v1 + h1 + v2 + h2 + 2) / 4;
629           type = GREENB;
630           break;
631       }
632     }
633   }
634 }
635
636 static GstFlowReturn
637 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
638     GstBuffer * outbuf)
639 {
640   GstBayer2RGB *filter = GST_BAYER2RGB (base);
641   uint8_t *input, *output;
642
643   /*
644    * We need to lock our filter params to prevent changing
645    * caps in the middle of a transformation (nice way to get
646    * segfaults)
647    */
648   GST_OBJECT_LOCK (filter);
649
650   GST_DEBUG ("transforming buffer");
651   input = (uint8_t *) GST_BUFFER_DATA (inbuf);
652   output = (uint8_t *) GST_BUFFER_DATA (outbuf);
653   do_corners (input, output, filter);
654   do_row0_col0 (input, output, filter);
655   do_body (input, output, filter);
656
657   GST_OBJECT_UNLOCK (filter);
658   return GST_FLOW_OK;
659 }