Add a Bayer-to-RGB converter. You know you want one, uh-huh.
[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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26 #include <gst/base/gstbasetransform.h>
27 #include <gst/video/video.h>
28 #include <string.h>
29 #include <stdint.h>
30
31 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
32 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
33
34 #define GST_TYPE_BAYER2RGB            (gst_bayer2rgb_get_type())
35 #define GST_BAYER2RGB(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BAYER2RGB,GstBayer2RGB))
36 #define GST_IS_BAYER2RGB(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BAYER2RGB))
37 #define GST_BAYER2RGB_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
38 #define GST_IS_BAYER2RGB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass) ,GST_TYPE_BAYER2RGB))
39 #define GST_BAYER2RGB_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj) ,GST_TYPE_BAYER2RGB,GstBayer2RGBClass))
40 typedef struct _GstBayer2RGB GstBayer2RGB;
41 typedef struct _GstBayer2RGBClass GstBayer2RGBClass;
42
43 typedef void (*GstBayer2RGBProcessFunc) (GstBayer2RGB *, guint8 *, guint);
44
45 struct _GstBayer2RGB
46 {
47   GstBaseTransform basetransform;
48
49   /* < private > */
50   int width;
51   int height;
52   int stride;
53
54   uint8_t *tmpdata;
55 };
56
57 struct _GstBayer2RGBClass
58 {
59   GstBaseTransformClass parent;
60 };
61
62 static const GstElementDetails element_details = GST_ELEMENT_DETAILS ("FIXME",
63     "Filter/Effect",
64     "FIXME example filter",
65     "FIXME <fixme@fixme.com>");
66
67 #define SRC_CAPS GST_VIDEO_CAPS_ARGB
68 #define SINK_CAPS "video/x-raw-bayer,width=(int)[1,MAX],height=(int)[1,MAX]"
69
70 enum
71 {
72   PROP_0
73 };
74
75 #define DEBUG_INIT(bla) \
76   GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0, "bayer2rgb element");
77
78 GST_BOILERPLATE_FULL (GstBayer2RGB, gst_bayer2rgb, GstBaseTransform,
79     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
80
81 static void gst_bayer2rgb_set_property (GObject * object, guint prop_id,
82     const GValue * value, GParamSpec * pspec);
83 static void gst_bayer2rgb_get_property (GObject * object, guint prop_id,
84     GValue * value, GParamSpec * pspec);
85
86 static gboolean gst_bayer2rgb_set_caps (GstBaseTransform * filter,
87     GstCaps * incaps, GstCaps * outcaps);
88 static GstFlowReturn gst_bayer2rgb_transform (GstBaseTransform * base,
89     GstBuffer * inbuf, GstBuffer * outbuf);
90 static void gst_bayer2rgb_reset (GstBayer2RGB * filter);
91 static GstCaps *gst_bayer2rgb_transform_caps (GstBaseTransform * base,
92     GstPadDirection direction, GstCaps * caps);
93 static gboolean gst_bayer2rgb_get_unit_size (GstBaseTransform * base,
94     GstCaps * caps, guint * size);
95
96
97 static void
98 gst_bayer2rgb_base_init (gpointer klass)
99 {
100   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
101
102   gst_element_class_set_details (element_class, &element_details);
103
104   gst_element_class_add_pad_template (element_class,
105       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
106           gst_caps_from_string (SRC_CAPS)));
107   gst_element_class_add_pad_template (element_class,
108       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
109           gst_caps_from_string (SINK_CAPS)));
110 }
111
112 static void
113 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
114 {
115   GObjectClass *gobject_class;
116
117   gobject_class = (GObjectClass *) klass;
118   gobject_class->set_property = gst_bayer2rgb_set_property;
119   gobject_class->get_property = gst_bayer2rgb_get_property;
120
121   GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
122       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform_caps);
123   GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
124       GST_DEBUG_FUNCPTR (gst_bayer2rgb_get_unit_size);
125   GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
126       GST_DEBUG_FUNCPTR (gst_bayer2rgb_set_caps);
127   GST_BASE_TRANSFORM_CLASS (klass)->transform =
128       GST_DEBUG_FUNCPTR (gst_bayer2rgb_transform);
129 }
130
131 static void
132 gst_bayer2rgb_init (GstBayer2RGB * filter, GstBayer2RGBClass * klass)
133 {
134   gst_bayer2rgb_reset (filter);
135   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
136 }
137
138 static void
139 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
140     const GValue * value, GParamSpec * pspec)
141 {
142   //GstBayer2RGB *filter = GST_BAYER2RGB (object);
143
144   switch (prop_id) {
145     default:
146       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147       break;
148   }
149 }
150
151 static void
152 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
153     GValue * value, GParamSpec * pspec)
154 {
155   //GstBayer2RGB *filter = GST_BAYER2RGB (object);
156
157   switch (prop_id) {
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161   }
162 }
163
164 static gboolean
165 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
166     GstCaps * outcaps)
167 {
168   GstBayer2RGB *filter = GST_BAYER2RGB (base);
169   GstStructure *structure;
170
171   GST_ERROR ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
172       outcaps);
173
174   structure = gst_caps_get_structure (incaps, 0);
175
176   gst_structure_get_int (structure, "width", &filter->width);
177   gst_structure_get_int (structure, "height", &filter->height);
178   filter->stride = GST_ROUND_UP_4 (filter->width);
179
180   if (filter->tmpdata) {
181     free (filter->tmpdata);
182   }
183   filter->tmpdata = malloc (filter->stride * (4 * 3 + 1));
184
185   return TRUE;
186 }
187
188 static void
189 gst_bayer2rgb_reset (GstBayer2RGB * filter)
190 {
191   filter->width = 0;
192   filter->height = 0;
193   filter->stride = 0;
194   if (filter->tmpdata) {
195     free (filter->tmpdata);
196     filter->tmpdata = NULL;
197   }
198 }
199
200 static GstCaps *
201 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
202     GstPadDirection direction, GstCaps * caps)
203 {
204   GstStructure *structure;
205   GstCaps *newcaps;
206   GstStructure *newstruct;
207
208   GST_ERROR ("transforming caps %" GST_PTR_FORMAT, caps);
209
210   structure = gst_caps_get_structure (caps, 0);
211
212   if (direction == GST_PAD_SRC) {
213     newcaps = gst_caps_new_simple ("video/x-raw-bayer", NULL);
214   } else {
215     newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL);
216   }
217   newstruct = gst_caps_get_structure (newcaps, 0);
218
219   gst_structure_set_value (newstruct, "width",
220       gst_structure_get_value (structure, "width"));
221   gst_structure_set_value (newstruct, "height",
222       gst_structure_get_value (structure, "height"));
223   gst_structure_set_value (newstruct, "framerate",
224       gst_structure_get_value (structure, "framerate"));
225   gst_structure_set_value (newstruct, "pixel-aspect-ratio",
226       gst_structure_get_value (structure, "pixel-aspect-ratio"));
227
228   GST_ERROR ("into %" GST_PTR_FORMAT, newcaps);
229
230   return newcaps;
231 }
232
233 static gboolean
234 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
235     guint * size)
236 {
237   GstStructure *structure;
238   int width;
239   int height;
240   const char *name;
241
242   structure = gst_caps_get_structure (caps, 0);
243   gst_structure_get_int (structure, "width", &width);
244   gst_structure_get_int (structure, "height", &height);
245
246   name = gst_structure_get_name (structure);
247   if (strcmp (name, "video/x-raw-rgb")) {
248     *size = GST_ROUND_UP_4 (width) * height;
249   } else {
250     *size = 4 * width * height;
251   }
252
253   return TRUE;
254 }
255
256 #define ARGB(a,r,g,b) ((b)<<24 | (g)<<16 | (r)<<8 | a)
257
258 static void
259 upsample_even (uint8_t * dest, uint8_t * src, int width)
260 {
261   int i;
262
263   for (i = 0; i < width - 2; i += 2) {
264     dest[i] = src[i];
265     dest[i + 1] = (src[i] + src[i + 2] + 1) / 2;
266   }
267   dest[i] = src[i];
268   if (i + 1 < width) {
269     dest[i + 1] = src[i];
270   }
271 }
272
273 static void
274 upsample_odd (uint8_t * dest, uint8_t * src, int width)
275 {
276   int i;
277
278   dest[0] = src[1];
279   for (i = 1; i < width - 2; i += 2) {
280     dest[i] = src[i];
281     dest[i + 1] = (src[i] + src[i + 2] + 1) / 2;
282   }
283   dest[i] = src[i];
284   if (i + 1 < width) {
285     dest[i + 1] = src[i];
286   }
287 }
288
289 static void
290 interpolate (uint8_t * dest, uint8_t * src1, uint8_t * src2, int width)
291 {
292   int i;
293
294   for (i = 0; i < width; i++) {
295     dest[i] = (src1[i] + src2[i] + 1) / 2;
296   }
297 }
298
299 static void
300 merge (uint32_t * dest, uint8_t * r, uint8_t * g, uint8_t * b, int width)
301 {
302   int i;
303
304   for (i = 0; i < width; i++) {
305     dest[i] = ARGB (0xff, r[i], g[i], b[i]);
306   }
307 }
308
309 static GstFlowReturn
310 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
311     GstBuffer * outbuf)
312 {
313   GstBayer2RGB *filter = GST_BAYER2RGB (base);
314   uint8_t *tmpdata;
315   int j;
316
317   GST_DEBUG ("got here");
318   tmpdata = filter->tmpdata;
319
320   /* This is a pretty lousy algorithm.  In particular, most higher
321    * quality algorithms will apply some non-linear weighting factors
322    * in red/blue interpolation based on the green components.  This
323    * just does a linear interpolation between surrounding pixels.
324    * For green, we only interpolate horizontally.  */
325
326   for (j = 0; j < filter->height + 1; j++) {
327     if (j < filter->height) {
328       /* upsample horizontally */
329       if ((j & 1) == 0) {
330         upsample_even (tmpdata + (1 * 4 + (j & 3)) * filter->stride,
331             (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
332             filter->width);
333         upsample_odd (tmpdata + (0 * 4 + (j & 3)) * filter->stride,
334             (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
335             filter->width);
336       } else {
337         upsample_even (tmpdata + (2 * 4 + (j & 3)) * filter->stride,
338             (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
339             filter->width);
340         upsample_odd (tmpdata + (1 * 4 + (j & 3)) * filter->stride,
341             (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
342             filter->width);
343       }
344     }
345     if (j - 1 >= 0 && j - 1 < filter->height) {
346       int comp, j1, j2;
347
348       if (((j - 1) & 1) == 0) {
349         comp = 2;
350       } else {
351         comp = 0;
352       }
353       j1 = j - 2;
354       if (j1 < 0)
355         j1 += 2;
356       j2 = j;
357       if (j2 > filter->height - 1)
358         j2 -= 2;
359       interpolate (tmpdata + (comp * 4 + ((j - 1) & 3)) * filter->stride,
360           tmpdata + (comp * 4 + (j1 & 3)) * filter->stride,
361           tmpdata + (comp * 4 + (j2 & 3)) * filter->stride, filter->width);
362
363       merge (
364           (uint32_t *) ((uint8_t *) GST_BUFFER_DATA (outbuf) +
365               4 * filter->width * (j - 1)),
366           tmpdata + (0 * 4 + ((j - 1) & 3)) * filter->stride,
367           tmpdata + (1 * 4 + ((j - 1) & 3)) * filter->stride,
368           tmpdata + (2 * 4 + ((j - 1) & 3)) * filter->stride, filter->width);
369     }
370   }
371
372   return GST_FLOW_OK;
373 }