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