bayer2rgb: Read stride from the video info
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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 #include "gstbayerorc.h"
85
86 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
87 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
88
89 enum
90 {
91   GST_BAYER_2_RGB_FORMAT_BGGR = 0,
92   GST_BAYER_2_RGB_FORMAT_GBRG,
93   GST_BAYER_2_RGB_FORMAT_GRBG,
94   GST_BAYER_2_RGB_FORMAT_RGGB
95 };
96
97
98 #define GST_TYPE_BAYER2RGB            (gst_bayer2rgb_get_type())
99 #define GST_BAYER2RGB(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BAYER2RGB,GstBayer2RGB))
100 #define GST_IS_BAYER2RGB(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BAYER2RGB))
101 #define GST_BAYER2RGB_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
102 #define GST_IS_BAYER2RGB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_BAYER2RGB))
103 #define GST_BAYER2RGB_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
104 typedef struct _GstBayer2RGB GstBayer2RGB;
105 typedef struct _GstBayer2RGBClass GstBayer2RGBClass;
106
107 typedef void (*GstBayer2RGBProcessFunc) (GstBayer2RGB *, guint8 *, guint);
108
109 struct _GstBayer2RGB
110 {
111   GstBaseTransform basetransform;
112
113   /* < private > */
114   GstVideoInfo info;
115   int width;
116   int height;
117   int r_off;                    /* offset for red */
118   int g_off;                    /* offset for green */
119   int b_off;                    /* offset for blue */
120   int format;
121 };
122
123 struct _GstBayer2RGBClass
124 {
125   GstBaseTransformClass parent;
126 };
127
128 #define SRC_CAPS                                 \
129   GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, RGBA, ARGB, BGRA, ABGR }")
130
131 #define SINK_CAPS "video/x-bayer,format=(string){bggr,grbg,gbrg,rggb}," \
132   "width=(int)[1,MAX],height=(int)[1,MAX],framerate=(fraction)[0/1,MAX]"
133
134 enum
135 {
136   PROP_0
137 };
138
139 GType gst_bayer2rgb_get_type (void);
140
141 #define gst_bayer2rgb_parent_class parent_class
142 G_DEFINE_TYPE (GstBayer2RGB, gst_bayer2rgb, GST_TYPE_BASE_TRANSFORM);
143
144 static void gst_bayer2rgb_set_property (GObject * object, guint prop_id,
145     const GValue * value, GParamSpec * pspec);
146 static void gst_bayer2rgb_get_property (GObject * object, guint prop_id,
147     GValue * value, GParamSpec * pspec);
148
149 static gboolean gst_bayer2rgb_set_caps (GstBaseTransform * filter,
150     GstCaps * incaps, GstCaps * outcaps);
151 static GstFlowReturn gst_bayer2rgb_transform (GstBaseTransform * base,
152     GstBuffer * inbuf, GstBuffer * outbuf);
153 static void gst_bayer2rgb_reset (GstBayer2RGB * filter);
154 static GstCaps *gst_bayer2rgb_transform_caps (GstBaseTransform * base,
155     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
156 static gboolean gst_bayer2rgb_get_unit_size (GstBaseTransform * base,
157     GstCaps * caps, gsize * size);
158
159
160 static void
161 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
162 {
163   GObjectClass *gobject_class;
164   GstElementClass *gstelement_class;
165
166   gobject_class = (GObjectClass *) klass;
167   gstelement_class = (GstElementClass *) klass;
168
169   gobject_class->set_property = gst_bayer2rgb_set_property;
170   gobject_class->get_property = gst_bayer2rgb_get_property;
171
172   gst_element_class_set_static_metadata (gstelement_class,
173       "Bayer to RGB decoder for cameras", "Filter/Converter/Video",
174       "Converts video/x-bayer to video/x-raw",
175       "William Brack <wbrack@mmm.com.hk>");
176
177   gst_element_class_add_pad_template (gstelement_class,
178       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
179           gst_caps_from_string (SRC_CAPS)));
180   gst_element_class_add_pad_template (gstelement_class,
181       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
182           gst_caps_from_string (SINK_CAPS)));
183
184   GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
185       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform_caps);
186   GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
187       GST_DEBUG_FUNCPTR (gst_bayer2rgb_get_unit_size);
188   GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
189       GST_DEBUG_FUNCPTR (gst_bayer2rgb_set_caps);
190   GST_BASE_TRANSFORM_CLASS (klass)->transform =
191       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform);
192
193   GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0,
194       "bayer2rgb element");
195 }
196
197 static void
198 gst_bayer2rgb_init (GstBayer2RGB * filter)
199 {
200   gst_bayer2rgb_reset (filter);
201   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
202 }
203
204 /* No properties are implemented, so only a warning is produced */
205 static void
206 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
207     const GValue * value, GParamSpec * pspec)
208 {
209
210   switch (prop_id) {
211     default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214   }
215 }
216
217 static void
218 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
219     GValue * value, GParamSpec * pspec)
220 {
221
222   switch (prop_id) {
223     default:
224       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225       break;
226   }
227 }
228
229 static gboolean
230 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
231     GstCaps * outcaps)
232 {
233   GstBayer2RGB *bayer2rgb = GST_BAYER2RGB (base);
234   GstStructure *structure;
235   const char *format;
236   GstVideoInfo info;
237
238   GST_DEBUG ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
239       outcaps);
240
241   structure = gst_caps_get_structure (incaps, 0);
242
243   gst_structure_get_int (structure, "width", &bayer2rgb->width);
244   gst_structure_get_int (structure, "height", &bayer2rgb->height);
245
246   format = gst_structure_get_string (structure, "format");
247   if (g_str_equal (format, "bggr")) {
248     bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_BGGR;
249   } else if (g_str_equal (format, "gbrg")) {
250     bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_GBRG;
251   } else if (g_str_equal (format, "grbg")) {
252     bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_GRBG;
253   } else if (g_str_equal (format, "rggb")) {
254     bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_RGGB;
255   } else {
256     return FALSE;
257   }
258
259   /* To cater for different RGB formats, we need to set params for later */
260   gst_video_info_from_caps (&info, outcaps);
261   bayer2rgb->r_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 0);
262   bayer2rgb->g_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 1);
263   bayer2rgb->b_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 2);
264
265   bayer2rgb->info = info;
266
267   return TRUE;
268 }
269
270 static void
271 gst_bayer2rgb_reset (GstBayer2RGB * filter)
272 {
273   filter->width = 0;
274   filter->height = 0;
275   filter->r_off = 0;
276   filter->g_off = 0;
277   filter->b_off = 0;
278   gst_video_info_init (&filter->info);
279 }
280
281 static GstCaps *
282 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
283     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
284 {
285   GstStructure *structure;
286   GstCaps *newcaps;
287   GstStructure *newstruct;
288
289   GST_DEBUG_OBJECT (base, "transforming caps from %" GST_PTR_FORMAT, caps);
290
291   structure = gst_caps_get_structure (caps, 0);
292
293   if (direction == GST_PAD_SRC) {
294     newcaps = gst_caps_from_string ("video/x-bayer,"
295         "format=(string){bggr,grbg,gbrg,rggb}");
296   } else {
297     newcaps = gst_caps_new_empty_simple ("video/x-raw");
298   }
299   newstruct = gst_caps_get_structure (newcaps, 0);
300
301   gst_structure_set_value (newstruct, "width",
302       gst_structure_get_value (structure, "width"));
303   gst_structure_set_value (newstruct, "height",
304       gst_structure_get_value (structure, "height"));
305   gst_structure_set_value (newstruct, "framerate",
306       gst_structure_get_value (structure, "framerate"));
307
308   if (filter != NULL) {
309     GstCaps *icaps;
310
311     GST_DEBUG_OBJECT (base, "                filter %" GST_PTR_FORMAT, filter);
312
313     icaps = gst_caps_intersect_full (filter, newcaps, GST_CAPS_INTERSECT_FIRST);
314     gst_caps_unref (newcaps);
315     newcaps = icaps;
316   }
317
318   GST_DEBUG_OBJECT (base, "                  into %" GST_PTR_FORMAT, newcaps);
319
320   return newcaps;
321 }
322
323 static gboolean
324 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
325     gsize * size)
326 {
327   GstStructure *structure;
328   int width;
329   int height;
330   const char *name;
331
332   structure = gst_caps_get_structure (caps, 0);
333
334   if (gst_structure_get_int (structure, "width", &width) &&
335       gst_structure_get_int (structure, "height", &height)) {
336     name = gst_structure_get_name (structure);
337     /* Our name must be either video/x-bayer video/x-raw */
338     if (strcmp (name, "video/x-raw")) {
339       *size = GST_ROUND_UP_4 (width) * height;
340       return TRUE;
341     } else {
342       /* For output, calculate according to format (always 32 bits) */
343       *size = width * height * 4;
344       return TRUE;
345     }
346
347   }
348   GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
349       ("Incomplete caps, some required field missing"));
350   return FALSE;
351 }
352
353 static void
354 gst_bayer2rgb_split_and_upsample_horiz (guint8 * dest0, guint8 * dest1,
355     const guint8 * src, int n)
356 {
357   int i;
358
359   dest0[0] = src[0];
360   dest1[0] = src[1];
361   dest0[1] = (src[0] + src[2] + 1) >> 1;
362   dest1[1] = src[1];
363
364 #if defined(__i386__) || defined(__amd64__)
365   bayer_orc_horiz_upsample_unaligned (dest0 + 2, dest1 + 2, src + 1,
366       (n - 4) >> 1);
367 #else
368   bayer_orc_horiz_upsample (dest0 + 2, dest1 + 2, src + 2, (n - 4) >> 1);
369 #endif
370
371   for (i = n - 2; i < n; i++) {
372     if ((i & 1) == 0) {
373       dest0[i] = src[i];
374       dest1[i] = src[i - 1];
375     } else {
376       dest0[i] = src[i - 1];
377       dest1[i] = src[i];
378     }
379   }
380 }
381
382 typedef void (*process_func) (guint8 * d0, const guint8 * s0, const guint8 * s1,
383     const guint8 * s2, const guint8 * s3, const guint8 * s4, const guint8 * s5,
384     int n);
385
386 static void
387 gst_bayer2rgb_process (GstBayer2RGB * bayer2rgb, uint8_t * dest,
388     int dest_stride, uint8_t * src, int src_stride)
389 {
390   int j;
391   guint8 *tmp;
392   process_func merge[2] = { NULL, NULL };
393   int r_off, g_off, b_off;
394
395   /* We exploit some symmetry in the functions here.  The base functions
396    * are all named for the BGGR arrangement.  For RGGB, we swap the
397    * red offset and blue offset in the output.  For GRBG, we swap the
398    * order of the merge functions.  For GBRG, do both. */
399   r_off = bayer2rgb->r_off;
400   g_off = bayer2rgb->g_off;
401   b_off = bayer2rgb->b_off;
402   if (bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_RGGB ||
403       bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GBRG) {
404     r_off = bayer2rgb->b_off;
405     b_off = bayer2rgb->r_off;
406   }
407
408   if (r_off == 2 && g_off == 1 && b_off == 0) {
409     merge[0] = bayer_orc_merge_bg_bgra;
410     merge[1] = bayer_orc_merge_gr_bgra;
411   } else if (r_off == 3 && g_off == 2 && b_off == 1) {
412     merge[0] = bayer_orc_merge_bg_abgr;
413     merge[1] = bayer_orc_merge_gr_abgr;
414   } else if (r_off == 1 && g_off == 2 && b_off == 3) {
415     merge[0] = bayer_orc_merge_bg_argb;
416     merge[1] = bayer_orc_merge_gr_argb;
417   } else if (r_off == 0 && g_off == 1 && b_off == 2) {
418     merge[0] = bayer_orc_merge_bg_rgba;
419     merge[1] = bayer_orc_merge_gr_rgba;
420   }
421   if (bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GRBG ||
422       bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GBRG) {
423     process_func tmp = merge[0];
424     merge[0] = merge[1];
425     merge[1] = tmp;
426   }
427
428   tmp = g_malloc (2 * 4 * bayer2rgb->width);
429 #define LINE(x) (tmp + ((x)&7) * bayer2rgb->width)
430
431   gst_bayer2rgb_split_and_upsample_horiz (LINE (3 * 2 + 0), LINE (3 * 2 + 1),
432       src + 1 * src_stride, bayer2rgb->width);
433   j = 0;
434   gst_bayer2rgb_split_and_upsample_horiz (LINE (j * 2 + 0), LINE (j * 2 + 1),
435       src + j * src_stride, bayer2rgb->width);
436
437   for (j = 0; j < bayer2rgb->height; j++) {
438     if (j < bayer2rgb->height - 1) {
439       gst_bayer2rgb_split_and_upsample_horiz (LINE ((j + 1) * 2 + 0),
440           LINE ((j + 1) * 2 + 1), src + (j + 1) * src_stride, bayer2rgb->width);
441     }
442
443     merge[j & 1] (dest + j * dest_stride,
444         LINE (j * 2 - 2), LINE (j * 2 - 1),
445         LINE (j * 2 + 0), LINE (j * 2 + 1),
446         LINE (j * 2 + 2), LINE (j * 2 + 3), bayer2rgb->width >> 1);
447   }
448
449   g_free (tmp);
450 }
451
452
453
454
455 static GstFlowReturn
456 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
457     GstBuffer * outbuf)
458 {
459   GstBayer2RGB *filter = GST_BAYER2RGB (base);
460   GstMapInfo map;
461   uint8_t *output;
462   GstVideoFrame frame;
463
464   GST_DEBUG ("transforming buffer");
465
466   if (!gst_buffer_map (inbuf, &map, GST_MAP_READ))
467     goto map_failed;
468
469   if (!gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE)) {
470     gst_buffer_unmap (inbuf, &map);
471     goto map_failed;
472   }
473
474   output = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
475   gst_bayer2rgb_process (filter, output, frame.info.stride[0],
476       map.data, filter->width);
477
478   gst_video_frame_unmap (&frame);
479   gst_buffer_unmap (inbuf, &map);
480
481   return GST_FLOW_OK;
482
483 map_failed:
484   GST_WARNING_OBJECT (base, "Could not map buffer, skipping");
485   return GST_FLOW_OK;
486 }