1 /* GStreamer
2 * Copyright (C) 2010 David Schleef <ds@entropywave.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
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 "gstrgb2bayer.h"
29
30 #define GST_CAT_DEFAULT gst_rgb2bayer_debug
31 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
32
33 static void gst_rgb2bayer_finalize (GObject * object);
34
35 static GstCaps *gst_rgb2bayer_transform_caps (GstBaseTransform * trans,
36 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
37 static gboolean
38 gst_rgb2bayer_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
39 gsize * size);
40 static gboolean
41 gst_rgb2bayer_set_caps (GstBaseTransform * trans, GstCaps * incaps,
42 GstCaps * outcaps);
43 static GstFlowReturn gst_rgb2bayer_transform (GstBaseTransform * trans,
44 GstBuffer * inbuf, GstBuffer * outbuf);
45
46 static GstStaticPadTemplate gst_rgb2bayer_sink_template =
47 GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_PAD_SINK,
49 GST_PAD_ALWAYS,
50 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("ARGB"))
51 );
52
53 #if 0
54 /* do these later */
55 static GstStaticPadTemplate gst_rgb2bayer_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_PAD_SINK,
58 GST_PAD_ALWAYS,
59 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
60 GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR ";"
61 GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_ARGB ";"
62 GST_VIDEO_CAPS_BGRA ";" GST_VIDEO_CAPS_ABGR)
63 );
64 #endif
65
66 static GstStaticPadTemplate gst_rgb2bayer_src_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
68 GST_PAD_SRC,
69 GST_PAD_ALWAYS,
70 GST_STATIC_CAPS ("video/x-bayer,"
71 "format=(string){bggr,gbrg,grbg,rggb},"
72 "width=[1,MAX],height=[1,MAX]," "framerate=(fraction)[0/1,MAX]")
73 );
74
75 /* class initialization */
76
77 #define gst_rgb2bayer_parent_class parent_class
78 G_DEFINE_TYPE (GstRGB2Bayer, gst_rgb2bayer, GST_TYPE_BASE_TRANSFORM);
79
80 static void
gst_rgb2bayer_class_init(GstRGB2BayerClass * klass)81 gst_rgb2bayer_class_init (GstRGB2BayerClass * klass)
82 {
83 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
84 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
85 GstBaseTransformClass *base_transform_class =
86 GST_BASE_TRANSFORM_CLASS (klass);
87
88 gobject_class->finalize = gst_rgb2bayer_finalize;
89
90 gst_element_class_add_static_pad_template (element_class,
91 &gst_rgb2bayer_src_template);
92 gst_element_class_add_static_pad_template (element_class,
93 &gst_rgb2bayer_sink_template);
94
95 gst_element_class_set_static_metadata (element_class,
96 "RGB to Bayer converter",
97 "Filter/Converter/Video",
98 "Converts video/x-raw to video/x-bayer",
99 "David Schleef <ds@entropywave.com>");
100
101 base_transform_class->transform_caps =
102 GST_DEBUG_FUNCPTR (gst_rgb2bayer_transform_caps);
103 base_transform_class->get_unit_size =
104 GST_DEBUG_FUNCPTR (gst_rgb2bayer_get_unit_size);
105 base_transform_class->set_caps = GST_DEBUG_FUNCPTR (gst_rgb2bayer_set_caps);
106 base_transform_class->transform = GST_DEBUG_FUNCPTR (gst_rgb2bayer_transform);
107
108 GST_DEBUG_CATEGORY_INIT (gst_rgb2bayer_debug, "rgb2bayer", 0,
109 "rgb2bayer element");
110 }
111
112 static void
gst_rgb2bayer_init(GstRGB2Bayer * rgb2bayer)113 gst_rgb2bayer_init (GstRGB2Bayer * rgb2bayer)
114 {
115
116 }
117
118 void
gst_rgb2bayer_finalize(GObject * object)119 gst_rgb2bayer_finalize (GObject * object)
120 {
121 G_OBJECT_CLASS (parent_class)->finalize (object);
122 }
123
124
125 static GstCaps *
gst_rgb2bayer_transform_caps(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,GstCaps * filter)126 gst_rgb2bayer_transform_caps (GstBaseTransform * trans,
127 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
128 {
129 GstRGB2Bayer *rgb2bayer;
130 GstCaps *res_caps, *tmp_caps;
131 GstStructure *structure;
132 guint i, caps_size;
133
134 rgb2bayer = GST_RGB_2_BAYER (trans);
135
136 res_caps = gst_caps_copy (caps);
137 caps_size = gst_caps_get_size (res_caps);
138 for (i = 0; i < caps_size; i++) {
139 structure = gst_caps_get_structure (res_caps, i);
140 if (direction == GST_PAD_SRC) {
141 gst_structure_set_name (structure, "video/x-raw");
142 gst_structure_remove_field (structure, "format");
143 } else {
144 gst_structure_set_name (structure, "video/x-bayer");
145 gst_structure_remove_fields (structure, "format", "colorimetry",
146 "chroma-site", NULL);
147 }
148 }
149 if (filter) {
150 tmp_caps = res_caps;
151 res_caps =
152 gst_caps_intersect_full (filter, tmp_caps, GST_CAPS_INTERSECT_FIRST);
153 gst_caps_unref (tmp_caps);
154 }
155 GST_DEBUG_OBJECT (rgb2bayer, "transformed %" GST_PTR_FORMAT " into %"
156 GST_PTR_FORMAT, caps, res_caps);
157 return res_caps;
158 }
159
160 static gboolean
gst_rgb2bayer_get_unit_size(GstBaseTransform * trans,GstCaps * caps,gsize * size)161 gst_rgb2bayer_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
162 gsize * size)
163 {
164 GstStructure *structure;
165 int width;
166 int height;
167 const char *name;
168
169 structure = gst_caps_get_structure (caps, 0);
170
171 if (gst_structure_get_int (structure, "width", &width) &&
172 gst_structure_get_int (structure, "height", &height)) {
173 name = gst_structure_get_name (structure);
174 /* Our name must be either video/x-bayer video/x-raw */
175 if (g_str_equal (name, "video/x-bayer")) {
176 *size = GST_ROUND_UP_4 (width) * height;
177 return TRUE;
178 } else {
179 /* For output, calculate according to format */
180 *size = width * height * 4;
181 return TRUE;
182 }
183
184 }
185
186 return FALSE;
187 }
188
189 static gboolean
gst_rgb2bayer_set_caps(GstBaseTransform * trans,GstCaps * incaps,GstCaps * outcaps)190 gst_rgb2bayer_set_caps (GstBaseTransform * trans, GstCaps * incaps,
191 GstCaps * outcaps)
192 {
193 GstRGB2Bayer *rgb2bayer = GST_RGB_2_BAYER (trans);
194 GstStructure *structure;
195 const char *format;
196 GstVideoInfo info;
197
198 GST_DEBUG ("in caps %" GST_PTR_FORMAT " out caps %" GST_PTR_FORMAT, incaps,
199 outcaps);
200
201 if (!gst_video_info_from_caps (&info, incaps))
202 return FALSE;
203
204 rgb2bayer->info = info;
205
206 structure = gst_caps_get_structure (outcaps, 0);
207
208 gst_structure_get_int (structure, "width", &rgb2bayer->width);
209 gst_structure_get_int (structure, "height", &rgb2bayer->height);
210
211 format = gst_structure_get_string (structure, "format");
212 if (g_str_equal (format, "bggr")) {
213 rgb2bayer->format = GST_RGB_2_BAYER_FORMAT_BGGR;
214 } else if (g_str_equal (format, "gbrg")) {
215 rgb2bayer->format = GST_RGB_2_BAYER_FORMAT_GBRG;
216 } else if (g_str_equal (format, "grbg")) {
217 rgb2bayer->format = GST_RGB_2_BAYER_FORMAT_GRBG;
218 } else if (g_str_equal (format, "rggb")) {
219 rgb2bayer->format = GST_RGB_2_BAYER_FORMAT_RGGB;
220 } else {
221 return FALSE;
222 }
223
224 return TRUE;
225 }
226
227 static GstFlowReturn
gst_rgb2bayer_transform(GstBaseTransform * trans,GstBuffer * inbuf,GstBuffer * outbuf)228 gst_rgb2bayer_transform (GstBaseTransform * trans, GstBuffer * inbuf,
229 GstBuffer * outbuf)
230 {
231 GstRGB2Bayer *rgb2bayer = GST_RGB_2_BAYER (trans);
232 GstMapInfo map;
233 guint8 *dest;
234 guint8 *src;
235 int i, j;
236 int height = rgb2bayer->height;
237 int width = rgb2bayer->width;
238 GstVideoFrame frame;
239
240 if (!gst_video_frame_map (&frame, &rgb2bayer->info, inbuf, GST_MAP_READ))
241 goto map_failed;
242
243 if (!gst_buffer_map (outbuf, &map, GST_MAP_READ)) {
244 gst_video_frame_unmap (&frame);
245 goto map_failed;
246 }
247
248 dest = map.data;
249 src = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
250
251 for (j = 0; j < height; j++) {
252 guint8 *dest_line = dest + GST_ROUND_UP_4 (width) * j;
253 guint8 *src_line = src + frame.info.stride[0] * j;
254
255 for (i = 0; i < width; i++) {
256 int is_blue = ((j & 1) << 1) | (i & 1);
257 if (is_blue == rgb2bayer->format) {
258 dest_line[i] = src_line[i * 4 + 3];
259 } else if ((is_blue ^ 3) == rgb2bayer->format) {
260 dest_line[i] = src_line[i * 4 + 1];
261 } else {
262 dest_line[i] = src_line[i * 4 + 2];
263 }
264 }
265 }
266
267 gst_buffer_unmap (outbuf, &map);
268 gst_video_frame_unmap (&frame);
269
270 return GST_FLOW_OK;
271
272 map_failed:
273 GST_WARNING_OBJECT (trans, "Could not map buffer, skipping");
274 return GST_FLOW_OK;
275 }
276