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 * @title: bayer2rgb
27 *
28 * Decodes raw camera bayer (fourcc BA81) to RGB.
29 */
30
31 /*
32 * In order to guard against my advancing maturity, some extra detailed
33 * information about the logic of the decode is included here. Much of
34 * this was inspired by a technical paper from siliconimaging.com, which
35 * in turn was based upon an article from IEEE,
36 * T. Sakamoto, C. Nakanishi and T. Hase,
37 * “Software pixel interpolation for digital still cameras suitable for
38 * a 32-bit MCU,”
39 * IEEE Trans. Consumer Electronics, vol. 44, no. 4, November 1998.
40 *
41 * The code assumes a Bayer matrix of the type produced by the fourcc
42 * BA81 (v4l2 format SBGGR8) of width w and height h which looks like:
43 * 0 1 2 3 w-2 w-1
44 *
45 * 0 B G B G ....B G
46 * 1 G R G R ....G R
47 * 2 B G B G ....B G
48 * ...............
49 * h-2 B G B G ....B G
50 * h-1 G R G R ....G R
51 *
52 * We expand this matrix, producing a separate {r, g, b} triple for each
53 * of the individual elements. The algorithm for doing this expansion is
54 * as follows.
55 *
56 * We are designing for speed of transformation, at a slight expense of code.
57 * First, we calculate the appropriate triples for the four corners, the
58 * remainder of the top and bottom rows, and the left and right columns.
59 * The reason for this is that those elements are transformed slightly
60 * differently than all of the remainder of the matrix. Finally, we transform
61 * all of the remainder.
62 *
63 * The transformation into the "appropriate triples" is based upon the
64 * "nearest neighbor" principal, with some additional complexity for the
65 * calculation of the "green" element, where an "adaptive" pairing is used.
66 *
67 * For purposes of documentation and identification, each element of the
68 * original array can be put into one of four classes:
69 * R A red element
70 * B A blue element
71 * GR A green element which is followed by a red one
72 * GB A green element which is followed by a blue one
73 */
74
75 #ifdef HAVE_CONFIG_H
76 #include "config.h"
77 #endif
78
79 #include <gst/gst.h>
80 #include <gst/base/gstbasetransform.h>
81 #include <gst/video/video.h>
82 #include <string.h>
83 #include <stdlib.h>
84
85 #ifdef HAVE_STDINT_H
86 #include <stdint.h>
87 #endif
88
89 #include "gstbayerelements.h"
90 #include "gstbayerorc.h"
91
92 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
93 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
94
95 enum
96 {
97 GST_BAYER_2_RGB_FORMAT_BGGR = 0,
98 GST_BAYER_2_RGB_FORMAT_GBRG,
99 GST_BAYER_2_RGB_FORMAT_GRBG,
100 GST_BAYER_2_RGB_FORMAT_RGGB
101 };
102
103
104 #define GST_TYPE_BAYER2RGB (gst_bayer2rgb_get_type())
105 #define GST_BAYER2RGB(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BAYER2RGB,GstBayer2RGB))
106 #define GST_IS_BAYER2RGB(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BAYER2RGB))
107 #define GST_BAYER2RGB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
108 #define GST_IS_BAYER2RGB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_BAYER2RGB))
109 #define GST_BAYER2RGB_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
110 typedef struct _GstBayer2RGB GstBayer2RGB;
111 typedef struct _GstBayer2RGBClass GstBayer2RGBClass;
112
113 typedef void (*GstBayer2RGBProcessFunc) (GstBayer2RGB *, guint8 *, guint);
114
115 struct _GstBayer2RGB
116 {
117 GstBaseTransform basetransform;
118
119 /* < private > */
120 GstVideoInfo info;
121 int width;
122 int height;
123 int r_off; /* offset for red */
124 int g_off; /* offset for green */
125 int b_off; /* offset for blue */
126 int format;
127 };
128
129 struct _GstBayer2RGBClass
130 {
131 GstBaseTransformClass parent;
132 };
133
134 #define SRC_CAPS \
135 GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, RGBA, ARGB, BGRA, ABGR }")
136
137 #define SINK_CAPS "video/x-bayer,format=(string){bggr,grbg,gbrg,rggb}," \
138 "width=(int)[1,MAX],height=(int)[1,MAX],framerate=(fraction)[0/1,MAX]"
139
140 enum
141 {
142 PROP_0
143 };
144
145 GType gst_bayer2rgb_get_type (void);
146
147 #define gst_bayer2rgb_parent_class parent_class
148 G_DEFINE_TYPE (GstBayer2RGB, gst_bayer2rgb, GST_TYPE_BASE_TRANSFORM);
149 GST_ELEMENT_REGISTER_DEFINE (bayer2rgb, "bayer2rgb", GST_RANK_NONE,
150 gst_bayer2rgb_get_type ());
151
152 static void gst_bayer2rgb_set_property (GObject * object, guint prop_id,
153 const GValue * value, GParamSpec * pspec);
154 static void gst_bayer2rgb_get_property (GObject * object, guint prop_id,
155 GValue * value, GParamSpec * pspec);
156
157 static gboolean gst_bayer2rgb_set_caps (GstBaseTransform * filter,
158 GstCaps * incaps, GstCaps * outcaps);
159 static GstFlowReturn gst_bayer2rgb_transform (GstBaseTransform * base,
160 GstBuffer * inbuf, GstBuffer * outbuf);
161 static void gst_bayer2rgb_reset (GstBayer2RGB * filter);
162 static GstCaps *gst_bayer2rgb_transform_caps (GstBaseTransform * base,
163 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
164 static gboolean gst_bayer2rgb_get_unit_size (GstBaseTransform * base,
165 GstCaps * caps, gsize * size);
166
167
168 static void
gst_bayer2rgb_class_init(GstBayer2RGBClass * klass)169 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
170 {
171 GObjectClass *gobject_class;
172 GstElementClass *gstelement_class;
173
174 gobject_class = (GObjectClass *) klass;
175 gstelement_class = (GstElementClass *) klass;
176
177 gobject_class->set_property = gst_bayer2rgb_set_property;
178 gobject_class->get_property = gst_bayer2rgb_get_property;
179
180 gst_element_class_set_static_metadata (gstelement_class,
181 "Bayer to RGB decoder for cameras", "Filter/Converter/Video",
182 "Converts video/x-bayer to video/x-raw",
183 "William Brack <wbrack@mmm.com.hk>");
184
185 gst_element_class_add_pad_template (gstelement_class,
186 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
187 gst_caps_from_string (SRC_CAPS)));
188 gst_element_class_add_pad_template (gstelement_class,
189 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
190 gst_caps_from_string (SINK_CAPS)));
191
192 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
193 GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform_caps);
194 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
195 GST_DEBUG_FUNCPTR (gst_bayer2rgb_get_unit_size);
196 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
197 GST_DEBUG_FUNCPTR (gst_bayer2rgb_set_caps);
198 GST_BASE_TRANSFORM_CLASS (klass)->transform =
199 GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform);
200
201 GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0,
202 "bayer2rgb element");
203 }
204
205 static void
gst_bayer2rgb_init(GstBayer2RGB * filter)206 gst_bayer2rgb_init (GstBayer2RGB * filter)
207 {
208 gst_bayer2rgb_reset (filter);
209 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
210 }
211
212 /* No properties are implemented, so only a warning is produced */
213 static void
gst_bayer2rgb_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)214 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
215 const GValue * value, GParamSpec * pspec)
216 {
217
218 switch (prop_id) {
219 default:
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 break;
222 }
223 }
224
225 static void
gst_bayer2rgb_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)226 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
227 GValue * value, GParamSpec * pspec)
228 {
229
230 switch (prop_id) {
231 default:
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 break;
234 }
235 }
236
237 static gboolean
gst_bayer2rgb_set_caps(GstBaseTransform * base,GstCaps * incaps,GstCaps * outcaps)238 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
239 GstCaps * outcaps)
240 {
241 GstBayer2RGB *bayer2rgb = GST_BAYER2RGB (base);
242 GstStructure *structure;
243 const char *format;
244 GstVideoInfo info;
245
246 GST_DEBUG ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
247 outcaps);
248
249 structure = gst_caps_get_structure (incaps, 0);
250
251 gst_structure_get_int (structure, "width", &bayer2rgb->width);
252 gst_structure_get_int (structure, "height", &bayer2rgb->height);
253
254 format = gst_structure_get_string (structure, "format");
255 if (g_str_equal (format, "bggr")) {
256 bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_BGGR;
257 } else if (g_str_equal (format, "gbrg")) {
258 bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_GBRG;
259 } else if (g_str_equal (format, "grbg")) {
260 bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_GRBG;
261 } else if (g_str_equal (format, "rggb")) {
262 bayer2rgb->format = GST_BAYER_2_RGB_FORMAT_RGGB;
263 } else {
264 return FALSE;
265 }
266
267 /* To cater for different RGB formats, we need to set params for later */
268 gst_video_info_from_caps (&info, outcaps);
269 bayer2rgb->r_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 0);
270 bayer2rgb->g_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 1);
271 bayer2rgb->b_off = GST_VIDEO_INFO_COMP_OFFSET (&info, 2);
272
273 bayer2rgb->info = info;
274
275 return TRUE;
276 }
277
278 static void
gst_bayer2rgb_reset(GstBayer2RGB * filter)279 gst_bayer2rgb_reset (GstBayer2RGB * filter)
280 {
281 filter->width = 0;
282 filter->height = 0;
283 filter->r_off = 0;
284 filter->g_off = 0;
285 filter->b_off = 0;
286 gst_video_info_init (&filter->info);
287 }
288
289 static GstCaps *
gst_bayer2rgb_transform_caps(GstBaseTransform * base,GstPadDirection direction,GstCaps * caps,GstCaps * filter)290 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
291 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
292 {
293 GstBayer2RGB *bayer2rgb;
294 GstCaps *res_caps, *tmp_caps;
295 GstStructure *structure;
296 guint i, caps_size;
297
298 bayer2rgb = GST_BAYER2RGB (base);
299
300 res_caps = gst_caps_copy (caps);
301 caps_size = gst_caps_get_size (res_caps);
302 for (i = 0; i < caps_size; i++) {
303 structure = gst_caps_get_structure (res_caps, i);
304 if (direction == GST_PAD_SINK) {
305 gst_structure_set_name (structure, "video/x-raw");
306 gst_structure_remove_field (structure, "format");
307 } else {
308 gst_structure_set_name (structure, "video/x-bayer");
309 gst_structure_remove_fields (structure, "format", "colorimetry",
310 "chroma-site", NULL);
311 }
312 }
313 if (filter) {
314 tmp_caps = res_caps;
315 res_caps =
316 gst_caps_intersect_full (filter, tmp_caps, GST_CAPS_INTERSECT_FIRST);
317 gst_caps_unref (tmp_caps);
318 }
319 GST_DEBUG_OBJECT (bayer2rgb, "transformed %" GST_PTR_FORMAT " into %"
320 GST_PTR_FORMAT, caps, res_caps);
321 return res_caps;
322 }
323
324 static gboolean
gst_bayer2rgb_get_unit_size(GstBaseTransform * base,GstCaps * caps,gsize * size)325 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
326 gsize * size)
327 {
328 GstStructure *structure;
329 int width;
330 int height;
331 const char *name;
332
333 structure = gst_caps_get_structure (caps, 0);
334
335 if (gst_structure_get_int (structure, "width", &width) &&
336 gst_structure_get_int (structure, "height", &height)) {
337 name = gst_structure_get_name (structure);
338 /* Our name must be either video/x-bayer video/x-raw */
339 if (strcmp (name, "video/x-raw")) {
340 *size = GST_ROUND_UP_4 (width) * height;
341 return TRUE;
342 } else {
343 /* For output, calculate according to format (always 32 bits) */
344 *size = width * height * 4;
345 return TRUE;
346 }
347
348 }
349 GST_ELEMENT_ERROR (base, CORE, NEGOTIATION, (NULL),
350 ("Incomplete caps, some required field missing"));
351 return FALSE;
352 }
353
354 static void
gst_bayer2rgb_split_and_upsample_horiz(guint8 * dest0,guint8 * dest1,const guint8 * src,int n)355 gst_bayer2rgb_split_and_upsample_horiz (guint8 * dest0, guint8 * dest1,
356 const guint8 * src, int n)
357 {
358 int i;
359
360 dest0[0] = src[0];
361 dest1[0] = src[1];
362 dest0[1] = (src[0] + src[2] + 1) >> 1;
363 dest1[1] = src[1];
364
365 #if defined(__i386__) || defined(__amd64__)
366 bayer_orc_horiz_upsample_unaligned (dest0 + 2, dest1 + 2, src + 1,
367 (n - 4) >> 1);
368 #else
369 bayer_orc_horiz_upsample (dest0 + 2, dest1 + 2, src + 2, (n - 4) >> 1);
370 #endif
371
372 for (i = n - 2; i < n; i++) {
373 if ((i & 1) == 0) {
374 dest0[i] = src[i];
375 dest1[i] = src[i - 1];
376 } else {
377 dest0[i] = src[i - 1];
378 dest1[i] = src[i];
379 }
380 }
381 }
382
383 typedef void (*process_func) (guint8 * d0, const guint8 * s0, const guint8 * s1,
384 const guint8 * s2, const guint8 * s3, const guint8 * s4, const guint8 * s5,
385 int n);
386
387 static void
gst_bayer2rgb_process(GstBayer2RGB * bayer2rgb,uint8_t * dest,int dest_stride,uint8_t * src,int src_stride)388 gst_bayer2rgb_process (GstBayer2RGB * bayer2rgb, uint8_t * dest,
389 int dest_stride, uint8_t * src, int src_stride)
390 {
391 int j;
392 guint8 *tmp;
393 process_func merge[2] = { NULL, NULL };
394 int r_off, g_off, b_off;
395
396 /* We exploit some symmetry in the functions here. The base functions
397 * are all named for the BGGR arrangement. For RGGB, we swap the
398 * red offset and blue offset in the output. For GRBG, we swap the
399 * order of the merge functions. For GBRG, do both. */
400 r_off = bayer2rgb->r_off;
401 g_off = bayer2rgb->g_off;
402 b_off = bayer2rgb->b_off;
403 if (bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_RGGB ||
404 bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GBRG) {
405 r_off = bayer2rgb->b_off;
406 b_off = bayer2rgb->r_off;
407 }
408
409 if (r_off == 2 && g_off == 1 && b_off == 0) {
410 merge[0] = bayer_orc_merge_bg_bgra;
411 merge[1] = bayer_orc_merge_gr_bgra;
412 } else if (r_off == 3 && g_off == 2 && b_off == 1) {
413 merge[0] = bayer_orc_merge_bg_abgr;
414 merge[1] = bayer_orc_merge_gr_abgr;
415 } else if (r_off == 1 && g_off == 2 && b_off == 3) {
416 merge[0] = bayer_orc_merge_bg_argb;
417 merge[1] = bayer_orc_merge_gr_argb;
418 } else if (r_off == 0 && g_off == 1 && b_off == 2) {
419 merge[0] = bayer_orc_merge_bg_rgba;
420 merge[1] = bayer_orc_merge_gr_rgba;
421 }
422 if (bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GRBG ||
423 bayer2rgb->format == GST_BAYER_2_RGB_FORMAT_GBRG) {
424 process_func tmp = merge[0];
425 merge[0] = merge[1];
426 merge[1] = tmp;
427 }
428
429 tmp = g_malloc (2 * 4 * bayer2rgb->width);
430 #define LINE(x) (tmp + ((x)&7) * bayer2rgb->width)
431
432 gst_bayer2rgb_split_and_upsample_horiz (LINE (3 * 2 + 0), LINE (3 * 2 + 1),
433 src + 1 * src_stride, bayer2rgb->width);
434 j = 0;
435 gst_bayer2rgb_split_and_upsample_horiz (LINE (j * 2 + 0), LINE (j * 2 + 1),
436 src + j * src_stride, bayer2rgb->width);
437
438 for (j = 0; j < bayer2rgb->height; j++) {
439 if (j < bayer2rgb->height - 1) {
440 gst_bayer2rgb_split_and_upsample_horiz (LINE ((j + 1) * 2 + 0),
441 LINE ((j + 1) * 2 + 1), src + (j + 1) * src_stride, bayer2rgb->width);
442 }
443
444 merge[j & 1] (dest + j * dest_stride,
445 LINE (j * 2 - 2), LINE (j * 2 - 1),
446 LINE (j * 2 + 0), LINE (j * 2 + 1),
447 LINE (j * 2 + 2), LINE (j * 2 + 3), bayer2rgb->width >> 1);
448 }
449
450 g_free (tmp);
451 }
452
453
454
455
456 static GstFlowReturn
gst_bayer2rgb_transform(GstBaseTransform * base,GstBuffer * inbuf,GstBuffer * outbuf)457 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
458 GstBuffer * outbuf)
459 {
460 GstBayer2RGB *filter = GST_BAYER2RGB (base);
461 GstMapInfo map;
462 uint8_t *output;
463 GstVideoFrame frame;
464
465 GST_DEBUG ("transforming buffer");
466
467 if (!gst_buffer_map (inbuf, &map, GST_MAP_READ))
468 goto map_failed;
469
470 if (!gst_video_frame_map (&frame, &filter->info, outbuf, GST_MAP_WRITE)) {
471 gst_buffer_unmap (inbuf, &map);
472 goto map_failed;
473 }
474
475 output = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
476 gst_bayer2rgb_process (filter, output, frame.info.stride[0],
477 map.data, GST_ROUND_UP_4 (filter->width));
478
479 gst_video_frame_unmap (&frame);
480 gst_buffer_unmap (inbuf, &map);
481
482 return GST_FLOW_OK;
483
484 map_failed:
485 GST_WARNING_OBJECT (base, "Could not map buffer, skipping");
486 return GST_FLOW_OK;
487 }
488