configure.ac: Adding videomixer.
[platform/upstream/gst-plugins-good.git] / gst / alpha / gstalphacolor.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <gst/gst.h>
24 #include <gst/base/gstbasetransform.h>
25 #include <gst/video/video.h>
26
27 #include <string.h>
28
29 GST_DEBUG_CATEGORY (alpha_color_debug);
30 #define GST_CAT_DEFAULT alpha_color_debug
31
32 #define GST_TYPE_ALPHA_COLOR \
33   (gst_alpha_color_get_type())
34 #define GST_ALPHA_COLOR(obj) \
35   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_ALPHA_COLOR,GstAlphaColor))
36 #define GST_ALPHA_COLOR_CLASS(klass) \
37   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ALPHA_COLOR,GstAlphaColorClass))
38 #define GST_IS_ALPHA_COLOR(obj) \
39   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_ALPHA_COLOR))
40 #define GST_IS_ALPHA_COLOR_CLASS(obj) \
41   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_ALPHA_COLOR))
42
43 typedef struct _GstAlphaColor GstAlphaColor;
44 typedef struct _GstAlphaColorClass GstAlphaColorClass;
45
46 struct _GstAlphaColor
47 {
48   GstBaseTransform element;
49
50   /* caps */
51   gint in_width, in_height;
52   gboolean in_rgba;
53   gint out_width, out_height;
54 };
55
56 struct _GstAlphaColorClass
57 {
58   GstBaseTransformClass parent_class;
59 };
60
61 /* elementfactory information */
62 static GstElementDetails gst_alpha_color_details =
63 GST_ELEMENT_DETAILS ("alpha color filter",
64     "Filter/Effect/Video",
65     "RGB->YUV colorspace conversion preserving the alpha channels",
66     "Wim Taymans <wim@fluendo.com>");
67
68 static GstStaticPadTemplate gst_alpha_color_sink_template =
69     GST_STATIC_PAD_TEMPLATE ("sink",
70     GST_PAD_SINK,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_BGRA)
73     );
74
75 static GstStaticPadTemplate gst_alpha_color_src_template =
76 GST_STATIC_PAD_TEMPLATE ("src",
77     GST_PAD_SRC,
78     GST_PAD_ALWAYS,
79     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV"))
80     );
81
82 GST_BOILERPLATE (GstAlphaColor, gst_alpha_color, GstBaseTransform,
83     GST_TYPE_BASE_TRANSFORM);
84
85 static GstCaps *gst_alpha_color_transform_caps (GstBaseTransform * btrans,
86     GstPadDirection direction, GstCaps * caps);
87 static gboolean gst_alpha_color_set_caps (GstBaseTransform * btrans,
88     GstCaps * incaps, GstCaps * outcaps);
89 static GstFlowReturn gst_alpha_color_transform_ip (GstBaseTransform * btrans,
90     GstBuffer * inbuf);
91
92 static void
93 gst_alpha_color_base_init (gpointer g_class)
94 {
95   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
96
97   gst_element_class_set_details (element_class, &gst_alpha_color_details);
98
99   gst_element_class_add_pad_template (element_class,
100       gst_static_pad_template_get (&gst_alpha_color_sink_template));
101   gst_element_class_add_pad_template (element_class,
102       gst_static_pad_template_get (&gst_alpha_color_src_template));
103 }
104
105 static void
106 gst_alpha_color_class_init (GstAlphaColorClass * klass)
107 {
108   GObjectClass *gobject_class;
109   GstElementClass *gstelement_class;
110   GstBaseTransformClass *gstbasetransform_class;
111
112   gobject_class = (GObjectClass *) klass;
113   gstelement_class = (GstElementClass *) klass;
114   gstbasetransform_class = (GstBaseTransformClass *) klass;
115
116   gstbasetransform_class->transform_caps =
117       GST_DEBUG_FUNCPTR (gst_alpha_color_transform_caps);
118   gstbasetransform_class->set_caps =
119       GST_DEBUG_FUNCPTR (gst_alpha_color_set_caps);
120   gstbasetransform_class->transform_ip =
121       GST_DEBUG_FUNCPTR (gst_alpha_color_transform_ip);
122
123   GST_DEBUG_CATEGORY_INIT (alpha_color_debug, "alphacolor", 0,
124       "RGB->YUV colorspace conversion preserving the alpha channels");
125 }
126
127 static void
128 gst_alpha_color_init (GstAlphaColor * alpha, GstAlphaColorClass * g_class)
129 {
130   GstBaseTransform *btrans = NULL;
131
132   btrans = GST_BASE_TRANSFORM (alpha);
133
134   btrans->always_in_place = TRUE;
135 }
136
137 static GstCaps *
138 gst_alpha_color_transform_caps (GstBaseTransform * btrans,
139     GstPadDirection direction, GstCaps * caps)
140 {
141   GstAlphaColor *alpha;
142   GstCaps *result;
143
144   alpha = GST_ALPHA_COLOR (btrans);
145
146   GST_DEBUG ("transforming direction %d caps %s", direction,
147       gst_caps_to_string (caps));
148   result = gst_caps_new_simple ("video/x-raw-yuv",
149       "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'),
150       "width", GST_TYPE_INT_RANGE, 16, 4096,
151       "height", GST_TYPE_INT_RANGE, 16, 4096,
152       "framerate", GST_TYPE_DOUBLE_RANGE, 0.0, G_MAXDOUBLE, NULL);
153
154   return result;
155 }
156
157 static gboolean
158 gst_alpha_color_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
159     GstCaps * outcaps)
160 {
161   GstAlphaColor *alpha;
162   GstStructure *structure;
163   gboolean ret;
164   gdouble fps;
165   gint red_mask;
166
167   alpha = GST_ALPHA_COLOR (btrans);
168   structure = gst_caps_get_structure (incaps, 0);
169
170   ret = gst_structure_get_int (structure, "width", &alpha->in_width);
171   ret &= gst_structure_get_int (structure, "height", &alpha->in_height);
172   ret &= gst_structure_get_double (structure, "framerate", &fps);
173   ret &= gst_structure_get_int (structure, "red_mask", &red_mask);
174
175   if (!ret)
176     return FALSE;
177
178   alpha->in_rgba = TRUE;
179 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
180   if (red_mask != 0x000000ff)
181 #else
182   if (red_mask != 0x00ff0000)
183 #endif
184     alpha->in_rgba = FALSE;
185
186   return TRUE;
187 }
188
189 static void
190 transform_rgb (guint8 * data, gint size)
191 {
192   guint8 y, u, v;
193
194   while (size > 0) {
195     y = data[0] * 0.299 + data[1] * 0.587 + data[2] * 0.114 + 0;
196     u = data[0] * -0.169 + data[1] * -0.332 + data[2] * 0.500 + 128;
197     v = data[0] * 0.500 + data[1] * -0.419 + data[2] * -0.0813 + 128;
198
199     data[0] = data[3];
200     data[1] = y;
201     data[2] = u;
202     data[3] = v;
203
204     data += 4;
205     size -= 4;
206   }
207 }
208
209 static void
210 transform_bgr (guint8 * data, gint size)
211 {
212   guint8 y, u, v;
213
214   while (size > 0) {
215     y = data[2] * 0.299 + data[1] * 0.587 + data[0] * 0.114 + 0;
216     u = data[2] * -0.169 + data[1] * -0.332 + data[0] * 0.500 + 128;
217     v = data[2] * 0.500 + data[1] * -0.419 + data[0] * -0.0813 + 128;
218
219     data[0] = data[3];
220     data[1] = y;
221     data[2] = u;
222     data[3] = v;
223
224     data += 4;
225     size -= 4;
226   }
227 }
228
229 static GstFlowReturn
230 gst_alpha_color_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
231 {
232   GstFlowReturn ret = GST_FLOW_OK;
233   GstAlphaColor *alpha;
234
235   alpha = GST_ALPHA_COLOR (btrans);
236
237   /* Transform in place */
238   if (alpha->in_rgba)
239     transform_rgb (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
240   else
241     transform_bgr (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
242
243   return ret;
244 }
245
246 static gboolean
247 plugin_init (GstPlugin * plugin)
248 {
249   return gst_element_register (plugin, "alphacolor", GST_RANK_NONE,
250       GST_TYPE_ALPHA_COLOR);
251 }
252
253 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
254     GST_VERSION_MINOR,
255     "alphacolor",
256     "RGB->YUV colorspace conversion preserving the alpha channels",
257     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)