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.
26 #include <gst/base/gstbasetransform.h>
27 #include <gst/video/video.h>
31 #define GST_CAT_DEFAULT gst_bayer2rgb_debug
32 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
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;
43 typedef void (*GstBayer2RGBProcessFunc) (GstBayer2RGB *, guint8 *, guint);
47 GstBaseTransform basetransform;
57 struct _GstBayer2RGBClass
59 GstBaseTransformClass parent;
62 static const GstElementDetails element_details = GST_ELEMENT_DETAILS ("FIXME",
64 "FIXME example filter",
65 "FIXME <fixme@fixme.com>");
67 #define SRC_CAPS GST_VIDEO_CAPS_ARGB
68 #define SINK_CAPS "video/x-raw-bayer,width=(int)[1,MAX],height=(int)[1,MAX]"
75 #define DEBUG_INIT(bla) \
76 GST_DEBUG_CATEGORY_INIT (gst_bayer2rgb_debug, "bayer2rgb", 0, "bayer2rgb element");
78 GST_BOILERPLATE_FULL (GstBayer2RGB, gst_bayer2rgb, GstBaseTransform,
79 GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
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);
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);
98 gst_bayer2rgb_base_init (gpointer klass)
100 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
102 gst_element_class_set_details (element_class, &element_details);
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)));
113 gst_bayer2rgb_class_init (GstBayer2RGBClass * klass)
115 GObjectClass *gobject_class;
117 gobject_class = (GObjectClass *) klass;
118 gobject_class->set_property = gst_bayer2rgb_set_property;
119 gobject_class->get_property = gst_bayer2rgb_get_property;
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);
132 gst_bayer2rgb_init (GstBayer2RGB * filter, GstBayer2RGBClass * klass)
134 gst_bayer2rgb_reset (filter);
135 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
139 gst_bayer2rgb_set_property (GObject * object, guint prop_id,
140 const GValue * value, GParamSpec * pspec)
142 //GstBayer2RGB *filter = GST_BAYER2RGB (object);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 gst_bayer2rgb_get_property (GObject * object, guint prop_id,
153 GValue * value, GParamSpec * pspec)
155 //GstBayer2RGB *filter = GST_BAYER2RGB (object);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 gst_bayer2rgb_set_caps (GstBaseTransform * base, GstCaps * incaps,
168 GstBayer2RGB *filter = GST_BAYER2RGB (base);
169 GstStructure *structure;
171 GST_ERROR ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
174 structure = gst_caps_get_structure (incaps, 0);
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);
180 if (filter->tmpdata) {
181 free (filter->tmpdata);
183 filter->tmpdata = malloc (filter->stride * (4 * 3 + 1));
189 gst_bayer2rgb_reset (GstBayer2RGB * filter)
194 if (filter->tmpdata) {
195 free (filter->tmpdata);
196 filter->tmpdata = NULL;
201 gst_bayer2rgb_transform_caps (GstBaseTransform * base,
202 GstPadDirection direction, GstCaps * caps)
204 GstStructure *structure;
206 GstStructure *newstruct;
208 GST_ERROR ("transforming caps %" GST_PTR_FORMAT, caps);
210 structure = gst_caps_get_structure (caps, 0);
212 if (direction == GST_PAD_SRC) {
213 newcaps = gst_caps_new_simple ("video/x-raw-bayer", NULL);
215 newcaps = gst_caps_new_simple ("video/x-raw-rgb", NULL);
217 newstruct = gst_caps_get_structure (newcaps, 0);
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"));
228 GST_ERROR ("into %" GST_PTR_FORMAT, newcaps);
234 gst_bayer2rgb_get_unit_size (GstBaseTransform * base, GstCaps * caps,
237 GstStructure *structure;
242 structure = gst_caps_get_structure (caps, 0);
243 gst_structure_get_int (structure, "width", &width);
244 gst_structure_get_int (structure, "height", &height);
246 name = gst_structure_get_name (structure);
247 if (strcmp (name, "video/x-raw-rgb")) {
248 *size = GST_ROUND_UP_4 (width) * height;
250 *size = 4 * width * height;
256 #define ARGB(a,r,g,b) ((b)<<24 | (g)<<16 | (r)<<8 | a)
259 upsample_even (uint8_t * dest, uint8_t * src, int width)
263 for (i = 0; i < width - 2; i += 2) {
265 dest[i + 1] = (src[i] + src[i + 2] + 1) / 2;
269 dest[i + 1] = src[i];
274 upsample_odd (uint8_t * dest, uint8_t * src, int width)
279 for (i = 1; i < width - 2; i += 2) {
281 dest[i + 1] = (src[i] + src[i + 2] + 1) / 2;
285 dest[i + 1] = src[i];
290 interpolate (uint8_t * dest, uint8_t * src1, uint8_t * src2, int width)
294 for (i = 0; i < width; i++) {
295 dest[i] = (src1[i] + src2[i] + 1) / 2;
300 merge (uint32_t * dest, uint8_t * r, uint8_t * g, uint8_t * b, int width)
304 for (i = 0; i < width; i++) {
305 dest[i] = ARGB (0xff, r[i], g[i], b[i]);
310 gst_bayer2rgb_transform (GstBaseTransform * base, GstBuffer * inbuf,
313 GstBayer2RGB *filter = GST_BAYER2RGB (base);
317 GST_DEBUG ("got here");
318 tmpdata = filter->tmpdata;
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. */
326 for (j = 0; j < filter->height + 1; j++) {
327 if (j < filter->height) {
328 /* upsample horizontally */
330 upsample_even (tmpdata + (1 * 4 + (j & 3)) * filter->stride,
331 (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
333 upsample_odd (tmpdata + (0 * 4 + (j & 3)) * filter->stride,
334 (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
337 upsample_even (tmpdata + (2 * 4 + (j & 3)) * filter->stride,
338 (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
340 upsample_odd (tmpdata + (1 * 4 + (j & 3)) * filter->stride,
341 (uint8_t *) GST_BUFFER_DATA (inbuf) + filter->stride * j,
345 if (j - 1 >= 0 && j - 1 < filter->height) {
348 if (((j - 1) & 1) == 0) {
357 if (j2 > filter->height - 1)
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);
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);