gst/bayer/gstbayer2rgb.c: Include our own "_stdint.h" instead of <stdint.h> (which...
[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 /*
22  * SECTION:element-bayer2rgb
23  *
24  * Decodes raw camera sensor images.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <gst/gst.h>
32 #include <gst/base/gstbasetransform.h>
33 #include <gst/video/video.h>
34 #include <string.h>
35 #include "_stdint.h"
36
37 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
39
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;
48
49 typedef void (*GstBayer2RGBProcessFunc) (GstBayer2RGB *, guint8 *, guint);
50
51 struct _GstBayer2RGB
52 {
53   GstBaseTransform basetransform;
54
55   /* < private > */
56   int width;
57   int height;
58   int stride;
59
60   uint8_t *tmpdata;
61 };
62
63 struct _GstBayer2RGBClass
64 {
65   GstBaseTransformClass parent;
66 };
67
68 static const GstElementDetails element_details =
69 GST_ELEMENT_DETAILS ("RAW Camera sensor decoder",
70     "Filter/Effect",
71     "FIXME example filter",
72     "FIXME <fixme@fixme.com>");
73
74 #define SRC_CAPS GST_VIDEO_CAPS_ARGB
75 #define SINK_CAPS "video/x-raw-bayer,width=(int)[1,MAX],height=(int)[1,MAX]"
76
77 enum
78 {
79   PROP_0
80 };
81
82 #define DEBUG_INIT(bla) \
83   GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0, "bayer2rgb element");
84
85 GST_BOILERPLATE_FULL (GstBayer2RGB, gst_bayer2rgb, GstBaseTransform,
86     GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
87
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);
92
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);
102
103
104 static void
105 gst_bayer2rgb_base_init (gpointer klass)
106 {
107   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
108
109   gst_element_class_set_details (element_class, &element_details);
110
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)));
117 }
118
119 static void
120 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
121 {
122   GObjectClass *gobject_class;
123
124   gobject_class = (GObjectClass *) klass;
125   gobject_class->set_property = gst_bayer2rgb_set_property;
126   gobject_class->get_property = gst_bayer2rgb_get_property;
127
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);
136 }
137
138 static void
139 gst_bayer2rgb_init (GstBayer2RGB * filter, GstBayer2RGBClass * klass)
140 {
141   gst_bayer2rgb_reset (filter);
142   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
143 }
144
145 static void
146 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
147     const GValue * value, GParamSpec * pspec)
148 {
149   //GstBayer2RGB *filter = GST_BAYER2RGB (object);
150
151   switch (prop_id) {
152     default:
153       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154       break;
155   }
156 }
157
158 static void
159 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
160     GValue * value, GParamSpec * pspec)
161 {
162   //GstBayer2RGB *filter = GST_BAYER2RGB (object);
163
164   switch (prop_id) {
165     default:
166       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167       break;
168   }
169 }
170
171 static gboolean
172 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
173     GstCaps * outcaps)
174 {
175   GstBayer2RGB *filter = GST_BAYER2RGB (base);
176   GstStructure *structure;
177
178   GST_ERROR ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
179       outcaps);
180
181   structure = gst_caps_get_structure (incaps, 0);
182
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);
186
187   if (filter->tmpdata) {
188     g_free (filter->tmpdata);
189   }
190   filter->tmpdata = g_malloc (filter->stride * (4 * 3 + 1));
191
192   return TRUE;
193 }
194
195 static void
196 gst_bayer2rgb_reset (GstBayer2RGB * filter)
197 {
198   filter->width = 0;
199   filter->height = 0;
200   filter->stride = 0;
201   if (filter->tmpdata) {
202     g_free (filter->tmpdata);
203     filter->tmpdata = NULL;
204   }
205 }
206
207 static GstCaps *
208 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
209     GstPadDirection direction, GstCaps * caps)
210 {
211   GstStructure *structure;
212   GstCaps *newcaps;
213   GstStructure *newstruct;
214
215   GST_ERROR ("transforming caps %" GST_PTR_FORMAT, caps);
216
217   structure = gst_caps_get_structure (caps, 0);
218
219   if (direction == GST_PAD_SRC) {
220     newcaps = gst_caps_new_simple ("video/x-raw-bayer", NULL);
221   } else {
222     newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL);
223   }
224   newstruct = gst_caps_get_structure (newcaps, 0);
225
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"));
234
235   GST_ERROR ("into %" GST_PTR_FORMAT, newcaps);
236
237   return newcaps;
238 }
239
240 static gboolean
241 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
242     guint * size)
243 {
244   GstStructure *structure;
245   int width;
246   int height;
247   const char *name;
248
249   structure = gst_caps_get_structure (caps, 0);
250   gst_structure_get_int (structure, "width", &width);
251   gst_structure_get_int (structure, "height", &height);
252
253   name = gst_structure_get_name (structure);
254   if (strcmp (name, "video/x-raw-rgb")) {
255     *size = GST_ROUND_UP_4 (width) * height;
256   } else {
257     *size = 4 * width * height;
258   }
259
260   return TRUE;
261 }
262
263 #define ARGB(a,r,g,b) ((b)<<24 | (g)<<16 | (r)<<8 | a)
264
265 static void
266 upsample_even (uint8_t * dest, uint8_t * src, int width)
267 {
268   int i;
269
270   for (i = 0; i < width - 2; i += 2) {
271     dest[i] = src[i];
272     dest[i + 1] = (src[i] + src[i + 2] + 1) / 2;
273   }
274   dest[i] = src[i];
275   if (i + 1 < width) {
276     dest[i + 1] = src[i];
277   }
278 }
279
280 static void
281 upsample_odd (uint8_t * dest, uint8_t * src, int width)
282 {
283   int i;
284
285   dest[0] = src[1];
286   for (i = 1; i < width - 2; i += 2) {
287     dest[i] = src[i];
288     dest[i + 1] = (src[i] + src[i + 2] + 1) / 2;
289   }
290   dest[i] = src[i];
291   if (i + 1 < width) {
292     dest[i + 1] = src[i];
293   }
294 }
295
296 static void
297 interpolate (uint8_t * dest, uint8_t * src1, uint8_t * src2, int width)
298 {
299   int i;
300
301   for (i = 0; i < width; i++) {
302     dest[i] = (src1[i] + src2[i] + 1) / 2;
303   }
304 }
305
306 static void
307 merge (uint32_t * dest, uint8_t * r, uint8_t * g, uint8_t * b, int width)
308 {
309   int i;
310
311   for (i = 0; i < width; i++) {
312     dest[i] = ARGB (0xff, r[i], g[i], b[i]);
313   }
314 }
315
316 static GstFlowReturn
317 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
318     GstBuffer * outbuf)
319 {
320   GstBayer2RGB *filter = GST_BAYER2RGB (base);
321   uint8_t *tmpdata;
322   int j;
323
324   GST_DEBUG ("got here");
325   tmpdata = filter->tmpdata;
326
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.  */
332
333   for (j = 0; j < filter->height + 1; j++) {
334     if (j < filter->height) {
335       /* upsample horizontally */
336       if ((j & 1) == 0) {
337         upsample_even (tmpdata + (1 * 4 + (j & 3)) * filter->stride,
338             (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
339             filter->width);
340         upsample_odd (tmpdata + (0 * 4 + (j & 3)) * filter->stride,
341             (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
342             filter->width);
343       } else {
344         upsample_even (tmpdata + (2 * 4 + (j & 3)) * filter->stride,
345             (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
346             filter->width);
347         upsample_odd (tmpdata + (1 * 4 + (j & 3)) * filter->stride,
348             (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
349             filter->width);
350       }
351     }
352     if (j - 1 >= 0 && j - 1 < filter->height) {
353       int comp, j1, j2;
354
355       if (((j - 1) & 1) == 0) {
356         comp = 2;
357       } else {
358         comp = 0;
359       }
360       j1 = j - 2;
361       if (j1 < 0)
362         j1 += 2;
363       j2 = j;
364       if (j2 > filter->height - 1)
365         j2 -= 2;
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);
369
370       merge (
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);
376     }
377   }
378
379   return GST_FLOW_OK;
380 }