3 * Copyright (C) 2007 David Schleef <ds@schleef.org>
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.
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.
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.
22 * SECTION:element-bayer2rgb
24 * Decodes raw camera sensor images.
32 #include <gst/base/gstbasetransform.h>
33 #include <gst/video/video.h>
37 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
40 #define GST_TYPE_BAYER2RGB (gst_bayer2rgb_get_type())
41 #define GST_BAYER2RGB(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BAYER2RGB,GstBayer2RGB))
42 #define GST_IS_BAYER2RGB(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BAYER2RGB))
43 #define GST_BAYER2RGB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
44 #define GST_IS_BAYER2RGB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_BAYER2RGB))
45 #define GST_BAYER2RGB_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
46 typedef struct _GstBayer2RGB GstBayer2RGB;
47 typedef struct _GstBayer2RGBClass GstBayer2RGBClass;
49 typedef void (*GstBayer2RGBProcessFunc) (GstBayer2RGB *, guint8 *, guint);
53 GstBaseTransform basetransform;
63 struct _GstBayer2RGBClass
65 GstBaseTransformClass parent;
68 static const GstElementDetails element_details =
69 GST_ELEMENT_DETAILS ("RAW Camera sensor decoder",
71 "FIXME example filter",
72 "FIXME <fixme@fixme.com>");
74 #define SRC_CAPS GST_VIDEO_CAPS_ARGB
75 #define SINK_CAPS "video/x-raw-bayer,width=(int)[1,MAX],height=(int)[1,MAX]"
82 #define DEBUG_INIT(bla) \
83 GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0, "bayer2rgb element");
85 GST_BOILERPLATE_FULL (GstBayer2RGB, gst_bayer2rgb, GstBaseTransform,
86 GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
88 static void gst_bayer2rgb_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
90 static void gst_bayer2rgb_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec);
93 static gboolean gst_bayer2rgb_set_caps (GstBaseTransform * filter,
94 GstCaps * incaps, GstCaps * outcaps);
95 static GstFlowReturn gst_bayer2rgb_transform (GstBaseTransform * base,
96 GstBuffer * inbuf, GstBuffer * outbuf);
97 static void gst_bayer2rgb_reset (GstBayer2RGB * filter);
98 static GstCaps *gst_bayer2rgb_transform_caps (GstBaseTransform * base,
99 GstPadDirection direction, GstCaps * caps);
100 static gboolean gst_bayer2rgb_get_unit_size (GstBaseTransform * base,
101 GstCaps * caps, guint * size);
105 gst_bayer2rgb_base_init (gpointer klass)
107 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
109 gst_element_class_set_details (element_class, &element_details);
111 gst_element_class_add_pad_template (element_class,
112 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
113 gst_caps_from_string (SRC_CAPS)));
114 gst_element_class_add_pad_template (element_class,
115 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
116 gst_caps_from_string (SINK_CAPS)));
120 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
122 GObjectClass *gobject_class;
124 gobject_class = (GObjectClass *) klass;
125 gobject_class->set_property = gst_bayer2rgb_set_property;
126 gobject_class->get_property = gst_bayer2rgb_get_property;
128 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
129 GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform_caps);
130 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
131 GST_DEBUG_FUNCPTR (gst_bayer2rgb_get_unit_size);
132 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
133 GST_DEBUG_FUNCPTR (gst_bayer2rgb_set_caps);
134 GST_BASE_TRANSFORM_CLASS (klass)->transform =
135 GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform);
139 gst_bayer2rgb_init (GstBayer2RGB * filter, GstBayer2RGBClass * klass)
141 gst_bayer2rgb_reset (filter);
142 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
146 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
147 const GValue * value, GParamSpec * pspec)
149 //GstBayer2RGB *filter = GST_BAYER2RGB (object);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
160 GValue * value, GParamSpec * pspec)
162 //GstBayer2RGB *filter = GST_BAYER2RGB (object);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
175 GstBayer2RGB *filter = GST_BAYER2RGB (base);
176 GstStructure *structure;
178 GST_ERROR ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
181 structure = gst_caps_get_structure (incaps, 0);
183 gst_structure_get_int (structure, "width", &filter->width);
184 gst_structure_get_int (structure, "height", &filter->height);
185 filter->stride = GST_ROUND_UP_4 (filter->width);
187 if (filter->tmpdata) {
188 g_free (filter->tmpdata);
190 filter->tmpdata = g_malloc (filter->stride * (4 * 3 + 1));
196 gst_bayer2rgb_reset (GstBayer2RGB * filter)
201 if (filter->tmpdata) {
202 g_free (filter->tmpdata);
203 filter->tmpdata = NULL;
208 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
209 GstPadDirection direction, GstCaps * caps)
211 GstStructure *structure;
213 GstStructure *newstruct;
215 GST_ERROR ("transforming caps %" GST_PTR_FORMAT, caps);
217 structure = gst_caps_get_structure (caps, 0);
219 if (direction == GST_PAD_SRC) {
220 newcaps = gst_caps_new_simple ("video/x-raw-bayer", NULL);
222 newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL);
224 newstruct = gst_caps_get_structure (newcaps, 0);
226 gst_structure_set_value (newstruct, "width",
227 gst_structure_get_value (structure, "width"));
228 gst_structure_set_value (newstruct, "height",
229 gst_structure_get_value (structure, "height"));
230 gst_structure_set_value (newstruct, "framerate",
231 gst_structure_get_value (structure, "framerate"));
232 gst_structure_set_value (newstruct, "pixel-aspect-ratio",
233 gst_structure_get_value (structure, "pixel-aspect-ratio"));
235 GST_ERROR ("into %" GST_PTR_FORMAT, newcaps);
241 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
244 GstStructure *structure;
249 structure = gst_caps_get_structure (caps, 0);
250 gst_structure_get_int (structure, "width", &width);
251 gst_structure_get_int (structure, "height", &height);
253 name = gst_structure_get_name (structure);
254 if (strcmp (name, "video/x-raw-rgb")) {
255 *size = GST_ROUND_UP_4 (width) * height;
257 *size = 4 * width * height;
263 #define ARGB(a,r,g,b) ((b)<<24 | (g)<<16 | (r)<<8 | a)
266 upsample_even (uint8_t * dest, uint8_t * src, int width)
270 for (i = 0; i < width - 2; i += 2) {
272 dest[i + 1] = (src[i] + src[i + 2] + 1) / 2;
276 dest[i + 1] = src[i];
281 upsample_odd (uint8_t * dest, uint8_t * src, int width)
286 for (i = 1; i < width - 2; i += 2) {
288 dest[i + 1] = (src[i] + src[i + 2] + 1) / 2;
292 dest[i + 1] = src[i];
297 interpolate (uint8_t * dest, uint8_t * src1, uint8_t * src2, int width)
301 for (i = 0; i < width; i++) {
302 dest[i] = (src1[i] + src2[i] + 1) / 2;
307 merge (uint32_t * dest, uint8_t * r, uint8_t * g, uint8_t * b, int width)
311 for (i = 0; i < width; i++) {
312 dest[i] = ARGB (0xff, r[i], g[i], b[i]);
317 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
320 GstBayer2RGB *filter = GST_BAYER2RGB (base);
324 GST_DEBUG ("got here");
325 tmpdata = filter->tmpdata;
327 /* This is a pretty lousy algorithm. In particular, most higher
328 * quality algorithms will apply some non-linear weighting factors
329 * in red/blue interpolation based on the green components. This
330 * just does a linear interpolation between surrounding pixels.
331 * For green, we only interpolate horizontally. */
333 for (j = 0; j < filter->height + 1; j++) {
334 if (j < filter->height) {
335 /* upsample horizontally */
337 upsample_even (tmpdata + (1 * 4 + (j & 3)) * filter->stride,
338 (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
340 upsample_odd (tmpdata + (0 * 4 + (j & 3)) * filter->stride,
341 (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
344 upsample_even (tmpdata + (2 * 4 + (j & 3)) * filter->stride,
345 (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
347 upsample_odd (tmpdata + (1 * 4 + (j & 3)) * filter->stride,
348 (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
352 if (j - 1 >= 0 && j - 1 < filter->height) {
355 if (((j - 1) & 1) == 0) {
364 if (j2 > filter->height - 1)
366 interpolate (tmpdata + (comp * 4 + ((j - 1) & 3)) * filter->stride,
367 tmpdata + (comp * 4 + (j1 & 3)) * filter->stride,
368 tmpdata + (comp * 4 + (j2 & 3)) * filter->stride, filter->width);
371 (uint32_t *) ((uint8_t *) GST_BUFFER_DATA (outbuf) +
372 4 * filter->width * (j - 1)),
373 tmpdata + (0 * 4 + ((j - 1) & 3)) * filter->stride,
374 tmpdata + (1 * 4 + ((j - 1) & 3)) * filter->stride,
375 tmpdata + (2 * 4 + ((j - 1) & 3)) * filter->stride, filter->width);