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