gst/bayer/gstbayer2rgb.c: Fix color offset calculation for 24bpp. Fixes bug #566613.
[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 static const GstElementDetails element_details =
119 GST_ELEMENT_DETAILS ("Bayer to RGB decoder for cameras",
120     "Filter/Converter/Video",
121     "Converts video/x-raw-bayer to video/x-raw-rgb",
122     "William Brack <wbrack@mmm.com.hk>");
123
124 //#define SRC_CAPS GST_VIDEO_CAPS_RGBx
125 #define SRC_CAPS                                 \
126   GST_VIDEO_CAPS_RGBx ";"                        \
127   GST_VIDEO_CAPS_xRGB ";"                        \
128   GST_VIDEO_CAPS_BGRx ";"                        \
129   GST_VIDEO_CAPS_xBGR ";"                        \
130   GST_VIDEO_CAPS_RGBA ";"                        \
131   GST_VIDEO_CAPS_ARGB ";"                        \
132   GST_VIDEO_CAPS_BGRA ";"                        \
133   GST_VIDEO_CAPS_ABGR ";"                        \
134   GST_VIDEO_CAPS_RGB ";"                         \
135   GST_VIDEO_CAPS_BGR
136
137 #define SINK_CAPS "video/x-raw-bayer,width=(int)[1,MAX],height=(int)[1,MAX]"
138
139 enum
140 {
141   PROP_0
142 };
143
144 #define DEBUG_INIT(bla) \
145   GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0, "bayer2rgb element");
146
147 GST_BOILERPLATE_FULL (GstBayer2RGB, gst_bayer2rgb, GstBaseTransform,
148     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
149
150 static void gst_bayer2rgb_set_property (GObject * object, guint prop_id,
151     const GValue * value, GParamSpec * pspec);
152 static void gst_bayer2rgb_get_property (GObject * object, guint prop_id,
153     GValue * value, GParamSpec * pspec);
154
155 static gboolean gst_bayer2rgb_set_caps (GstBaseTransform * filter,
156     GstCaps * incaps, GstCaps * outcaps);
157 static GstFlowReturn gst_bayer2rgb_transform (GstBaseTransform * base,
158     GstBuffer * inbuf, GstBuffer * outbuf);
159 static void gst_bayer2rgb_reset (GstBayer2RGB * filter);
160 static GstCaps *gst_bayer2rgb_transform_caps (GstBaseTransform * base,
161     GstPadDirection direction, GstCaps * caps);
162 static gboolean gst_bayer2rgb_get_unit_size (GstBaseTransform * base,
163     GstCaps * caps, guint * size);
164
165
166 static void
167 gst_bayer2rgb_base_init (gpointer klass)
168 {
169   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
170
171   gst_element_class_set_details (element_class, &element_details);
172
173   gst_element_class_add_pad_template (element_class,
174       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
175           gst_caps_from_string (SRC_CAPS)));
176   gst_element_class_add_pad_template (element_class,
177       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
178           gst_caps_from_string (SINK_CAPS)));
179 }
180
181 static void
182 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
183 {
184   GObjectClass *gobject_class;
185
186   gobject_class = (GObjectClass *) klass;
187   gobject_class->set_property = gst_bayer2rgb_set_property;
188   gobject_class->get_property = gst_bayer2rgb_get_property;
189
190   GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
191       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform_caps);
192   GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
193       GST_DEBUG_FUNCPTR (gst_bayer2rgb_get_unit_size);
194   GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
195       GST_DEBUG_FUNCPTR (gst_bayer2rgb_set_caps);
196   GST_BASE_TRANSFORM_CLASS (klass)->transform =
197       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform);
198 }
199
200 static void
201 gst_bayer2rgb_init (GstBayer2RGB * filter, GstBayer2RGBClass * klass)
202 {
203   gst_bayer2rgb_reset (filter);
204   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
205 }
206
207 /* No properties are implemented, so only a warning is produced */
208 static void
209 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
210     const GValue * value, GParamSpec * pspec)
211 {
212
213   switch (prop_id) {
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217   }
218 }
219
220 static void
221 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
222     GValue * value, GParamSpec * pspec)
223 {
224
225   switch (prop_id) {
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228       break;
229   }
230 }
231
232 /* Routine to convert colormask value into relative byte offset */
233 static int
234 get_pix_offset (int mask, int bpp)
235 {
236   int bpp32 = (bpp / 8) - 3;
237
238   switch (mask) {
239     case 255:
240       return 2 + bpp32;
241     case 65280:
242       return 1 + bpp32;
243     case 16711680:
244       return 0 + bpp32;
245     case -16777216:
246       return 0;
247     default:
248       GST_ERROR ("Invalid color mask 0x%08x", mask);
249       return -1;
250   }
251 }
252
253 static gboolean
254 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
255     GstCaps * outcaps)
256 {
257   GstBayer2RGB *filter = GST_BAYER2RGB (base);
258   GstStructure *structure;
259   int val, bpp;
260
261   GST_DEBUG ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
262       outcaps);
263
264   structure = gst_caps_get_structure (incaps, 0);
265
266   gst_structure_get_int (structure, "width", &filter->width);
267   gst_structure_get_int (structure, "height", &filter->height);
268   filter->stride = GST_ROUND_UP_4 (filter->width);
269
270   /* To cater for different RGB formats, we need to set params for later */
271   structure = gst_caps_get_structure (outcaps, 0);
272   gst_structure_get_int (structure, "bpp", &bpp);
273   filter->pixsize = bpp / 8;
274   gst_structure_get_int (structure, "red_mask", &val);
275   filter->r_off = get_pix_offset (val, bpp);
276   gst_structure_get_int (structure, "green_mask", &val);
277   filter->g_off = get_pix_offset (val, bpp);
278   gst_structure_get_int (structure, "blue_mask", &val);
279   filter->b_off = get_pix_offset (val, bpp);
280
281   return TRUE;
282 }
283
284 static void
285 gst_bayer2rgb_reset (GstBayer2RGB * filter)
286 {
287   filter->width = 0;
288   filter->height = 0;
289   filter->stride = 0;
290   filter->pixsize = 0;
291   filter->r_off = 0;
292   filter->g_off = 0;
293   filter->b_off = 0;
294 }
295
296 static GstCaps *
297 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
298     GstPadDirection direction, GstCaps * caps)
299 {
300   GstStructure *structure;
301   GstCaps *newcaps;
302   GstStructure *newstruct;
303
304   GST_DEBUG_OBJECT (caps, "transforming caps (from)");
305
306   structure = gst_caps_get_structure (caps, 0);
307
308   if (direction == GST_PAD_SRC) {
309     newcaps = gst_caps_new_simple ("video/x-raw-bayer", NULL);
310   } else {
311     newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL);
312   }
313   newstruct = gst_caps_get_structure (newcaps, 0);
314
315   gst_structure_set_value (newstruct, "width",
316       gst_structure_get_value (structure, "width"));
317   gst_structure_set_value (newstruct, "height",
318       gst_structure_get_value (structure, "height"));
319   gst_structure_set_value (newstruct, "framerate",
320       gst_structure_get_value (structure, "framerate"));
321
322   GST_DEBUG_OBJECT (newcaps, "transforming caps (into)");
323
324   return newcaps;
325 }
326
327 static gboolean
328 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
329     guint * size)
330 {
331   GstStructure *structure;
332   int width;
333   int height;
334   int pixsize;
335   const char *name;
336
337   structure = gst_caps_get_structure (caps, 0);
338
339   if (gst_structure_get_int (structure, "width", &width) &&
340       gst_structure_get_int (structure, "height", &height)) {
341     name = gst_structure_get_name (structure);
342     /* Our name must be either video/x-raw-bayer video/x-raw-rgb */
343     if (strcmp (name, "video/x-raw-rgb")) {
344       /* For bayer, we handle only BA81 (BGGR), which is BPP=24 */
345       *size = GST_ROUND_UP_4 (width) * height;
346       return TRUE;
347     } else {
348       /* For output, calculate according to format */
349       if (gst_structure_get_int (structure, "bpp", &pixsize)) {
350         *size = width * height * (pixsize / 8);
351         return TRUE;
352       }
353     }
354
355   }
356   GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
357       ("Incomplete caps, some required field missing"));
358   return FALSE;
359 }
360
361 /*
362  * We define values for the colors, just to make the code more readable.
363  */
364 #define RED     0               /* Pure red element */
365 #define GREENB  1               /* Green element which is on a blue line */
366 #define BLUE    2               /* Pure blue element */
367 #define GREENR  3               /* Green element which is on a red line */
368
369 /* Routine to generate the top and bottom edges (not including corners) */
370 static void
371 hborder (uint8_t * input, uint8_t * output, int bot_top,
372     int typ, GstBayer2RGB * filter)
373 {
374   uint8_t *op;                  /* output pointer */
375   uint8_t *ip;                  /* input pointer */
376   uint8_t *nx;                  /* next line pointer */
377   int ix;                       /* loop index */
378
379   op = output + (bot_top * filter->width * (filter->height - 1) + 1) *
380       filter->pixsize;
381   ip = input + bot_top * filter->stride * (filter->height - 1);
382   /* calculate minus or plus one line, depending upon bot_top flag */
383   nx = ip + (1 - 2 * bot_top) * filter->stride;
384   /* Stepping horizontally */
385   for (ix = 1; ix < filter->width - 1; ix++, op += filter->pixsize) {
386     switch (typ) {
387       case RED:
388         op[filter->r_off] = ip[ix];
389         op[filter->g_off] = (ip[ix + 1] + ip[ix - 1] + nx[ix] + 1) / 3;
390         op[filter->b_off] = (nx[ix + 1] + nx[ix - 1] + 1) / 2;
391         typ = GREENR;
392         break;
393       case GREENR:
394         op[filter->r_off] = (ip[ix + 1] + ip[ix - 1] + 1) / 2;
395         op[filter->g_off] = ip[ix];
396         op[filter->b_off] = nx[ix];
397         typ = RED;
398         break;
399       case GREENB:
400         op[filter->r_off] = nx[ix];
401         op[filter->g_off] = ip[ix];
402         op[filter->b_off] = (ip[ix + 1] + ip[ix - 1] + 1) / 2;
403         typ = BLUE;
404         break;
405       case BLUE:
406         op[filter->r_off] = (nx[ix + 1] + nx[ix - 1] + 1) / 2;
407         op[filter->g_off] = (ip[ix + 1] + ip[ix - 1] + nx[ix] + 1) / 3;
408         op[filter->b_off] = ip[ix];
409         typ = GREENB;
410         break;
411     }
412   }
413 }
414
415 /* Routine to generate the left and right edges, not including corners */
416 static void
417 vborder (uint8_t * input, uint8_t * output, int right_left,
418     int typ, GstBayer2RGB * filter)
419 {
420   uint8_t *op;                  /* output pointer */
421   uint8_t *ip;                  /* input pointer */
422   uint8_t *la;                  /* line above pointer */
423   uint8_t *lb;                  /* line below pointer */
424   int ix;                       /* loop index */
425   int lr;                       /* 'left-right' flag - +1 is right, -1 is left */
426
427   lr = (1 - 2 * right_left);
428   /* stepping vertically */
429   for (ix = 1; ix < filter->height - 1; ix++) {
430     ip = input + right_left * (filter->width - 1) + ix * filter->stride;
431     op = output + (right_left * (filter->width - 1) + ix * filter->width) *
432         filter->pixsize;
433     la = ip + filter->stride;
434     lb = ip - filter->stride;
435     switch (typ) {
436       case RED:
437         op[filter->r_off] = ip[0];
438         op[filter->g_off] = (la[0] + ip[lr] + lb[0] + 1) / 3;
439         op[filter->b_off] = (la[lr] + lb[lr] + 1) / 2;
440         typ = GREENB;
441         break;
442       case GREENR:
443         op[filter->r_off] = ip[lr];
444         op[filter->g_off] = ip[0];
445         op[filter->b_off] = (la[lr] + lb[lr] + 1) / 2;
446         typ = BLUE;
447         break;
448       case GREENB:
449         op[filter->r_off] = (la[lr] + lb[lr] + 1) / 2;
450         op[filter->g_off] = ip[0];
451         op[filter->b_off] = ip[lr];
452         typ = RED;
453         break;
454       case BLUE:
455         op[filter->r_off] = (la[lr] + lb[lr] + 1) / 2;
456         op[filter->g_off] = (la[0] + ip[lr] + lb[0] + 1) / 3;
457         op[filter->b_off] = ip[0];
458         typ = GREENR;
459         break;
460     }
461   }
462 }
463
464 /* Produce the four (top, bottom, left, right) edges */
465 static void
466 do_row0_col0 (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
467 {
468   int type;
469
470   /* Horizontal edges */
471   hborder (input, output, 0, GREENB, filter);
472   if (filter->height & 1)
473     type = GREENB;              /* odd # rows, "bottom" edge same as top */
474   else
475     type = RED;                 /* even #, bottom side different */
476   hborder (input, output, 1, type, filter);
477
478   /* Vertical edges */
479   vborder (input, output, 0, GREENR, filter);
480   if (filter->width & 1)
481     type = GREENR;              /* odd # cols, "right" edge same as left */
482   else
483     type = RED;                 /* even #, right side different */
484   vborder (input, output, 1, type, filter);
485 }
486
487 static void
488 corner (uint8_t * input, uint8_t * output, int x, int y,
489     int xd, int yd, int typ, GstBayer2RGB * filter)
490 {
491   uint8_t *ip;                  /* input pointer */
492   uint8_t *op;                  /* output pointer */
493   uint8_t *nx;                  /* adjacent line */
494
495   op = output + y * filter->width * filter->pixsize + x * filter->pixsize;
496   ip = input + y * filter->stride + x;
497   nx = ip + yd * filter->stride;
498   switch (typ) {
499     case RED:
500       op[filter->r_off] = ip[0];
501       op[filter->g_off] = (nx[0] + ip[xd] + 1) / 2;
502       op[filter->b_off] = nx[xd];
503       break;
504     case GREENR:
505       op[filter->r_off] = ip[xd];
506       op[filter->g_off] = ip[0];
507       op[filter->b_off] = nx[0];
508       break;
509     case GREENB:
510       op[filter->r_off] = nx[0];
511       op[filter->g_off] = ip[0];
512       op[filter->b_off] = ip[xd];
513       break;
514     case BLUE:
515       op[filter->r_off] = nx[xd];
516       op[filter->g_off] = (nx[0] + ip[xd] + 1) / 2;
517       op[filter->b_off] = ip[0];
518       break;
519   }
520 }
521
522 static void
523 do_corners (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
524 {
525   int typ;
526
527   /* Top left */
528   corner (input, output, 0, 0, 1, 1, BLUE, filter);
529   /* Bottom left */
530   corner (input, output, 0, filter->height - 1, 1, -1,
531       (filter->height & 1) ? BLUE : GREENR, filter);
532   /* Top right */
533   corner (input, output, filter->width - 1, 0, -1, 0,
534       (filter->width & 1) ? BLUE : GREENB, filter);
535   /* Bottom right */
536   if (filter->width & 1)        /* if odd  # cols, B or GB */
537     typ = BLUE;
538   else
539     typ = GREENB;               /* if even # cols, B or GR */
540   typ |= (filter->height & 1);  /* if odd  # rows, GB or GR */
541   corner (input, output, filter->width - 1, filter->height - 1, -1, -1,
542       typ, filter);
543 }
544
545 static void
546 do_body (uint8_t * input, uint8_t * output, GstBayer2RGB * filter)
547 {
548   int ip, op;                   /* input and output pointers */
549   int w, h;                     /* loop indices */
550   int type;                     /* calculated colour of current element */
551   int a1, a2;
552   int v1, v2, h1, h2;
553
554   /*
555    * We are processing row (line) by row, starting with the second
556    * row and continuing through the next to last.  Each row is processed
557    * column by column, starting with the second and continuing through
558    * to the next to last.
559    */
560   for (h = 1; h < filter->height - 1; h++) {
561     /*
562      * Remember we are processing "row by row". For each row, we need
563      * to set the type of the first element to be processed.  Since we
564      * have already processed the edges, the "first element" will be
565      * the pixel at position (1,1).  Assuming BG format, this should
566      * be RED for odd-numbered rows and GREENB for even rows.
567      */
568     if (h & 1)
569       type = RED;
570     else
571       type = GREENB;
572     /* Calculate the starting position for the row */
573     op = h * filter->width * filter->pixsize;   /* output (converted) pos */
574     ip = h * filter->stride;    /* input (bayer data) pos */
575     for (w = 1; w < filter->width - 1; w++) {
576       op += filter->pixsize;    /* we are processing "horizontally" */
577       ip++;
578       switch (type) {
579         case RED:
580           output[op + filter->r_off] = input[ip];
581           output[op + filter->b_off] = (input[ip - filter->stride - 1] +
582               input[ip - filter->stride + 1] +
583               input[ip + filter->stride - 1] +
584               input[ip + filter->stride + 1] + 2) / 4;
585           v1 = input[ip + filter->stride];
586           v2 = input[ip - filter->stride];
587           h1 = input[ip + 1];
588           h2 = input[ip - 1];
589           a1 = abs (v1 - v2);
590           a2 = abs (h1 - h2);
591           if (a1 < a2)
592             output[op + filter->g_off] = (v1 + v2 + 1) / 2;
593           else if (a1 > a2)
594             output[op + filter->g_off] = (h1 + h2 + 1) / 2;
595           else
596             output[op + filter->g_off] = (v1 + h1 + v2 + h2 + 2) / 4;
597           type = GREENR;
598           break;
599         case GREENR:
600           output[op + filter->r_off] = (input[ip + 1] + input[ip - 1] + 1) / 2;
601           output[op + filter->g_off] = input[ip];
602           output[op + filter->b_off] = (input[ip - filter->stride] +
603               input[ip + filter->stride] + 1) / 2;
604           type = RED;
605           break;
606         case GREENB:
607           output[op + filter->r_off] = (input[ip - filter->stride] +
608               input[ip + filter->stride] + 1) / 2;
609           output[op + filter->g_off] = input[ip];
610           output[op + filter->b_off] = (input[ip + 1] + input[ip - 1] + 1) / 2;
611           type = BLUE;
612           break;
613         case BLUE:
614           output[op + filter->r_off] = (input[ip - filter->stride - 1] +
615               input[ip - filter->stride + 1] +
616               input[ip + filter->stride - 1] +
617               input[ip + filter->stride + 1] + 2) / 4;
618           output[op + filter->b_off] = input[ip];
619           v1 = input[ip + filter->stride];
620           v2 = input[ip - filter->stride];
621           h1 = input[ip + 1];
622           h2 = input[ip - 1];
623           a1 = abs (v1 - v2);
624           a2 = abs (h1 - h2);
625           if (a1 < a2)
626             output[op + filter->g_off] = (v1 + v2 + 1) / 2;
627           else if (a1 > a2)
628             output[op + filter->g_off] = (h1 + h2 + 1) / 2;
629           else
630             output[op + filter->g_off] = (v1 + h1 + v2 + h2 + 2) / 4;
631           type = GREENB;
632           break;
633       }
634     }
635   }
636 }
637
638 static GstFlowReturn
639 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
640     GstBuffer * outbuf)
641 {
642   GstBayer2RGB *filter = GST_BAYER2RGB (base);
643   uint8_t *input, *output;
644
645   /*
646    * We need to lock our filter params to prevent changing
647    * caps in the middle of a transformation (nice way to get
648    * segfaults)
649    */
650   GST_OBJECT_LOCK (filter);
651
652   GST_DEBUG ("transforming buffer");
653   input = (uint8_t *) GST_BUFFER_DATA (inbuf);
654   output = (uint8_t *) GST_BUFFER_DATA (outbuf);
655   do_corners (input, output, filter);
656   do_row0_col0 (input, output, filter);
657   do_body (input, output, filter);
658
659   GST_OBJECT_UNLOCK (filter);
660   return GST_FLOW_OK;
661 }