2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
24 #include <gst/base/gstbasetransform.h>
25 #include <gst/video/video.h>
29 GST_DEBUG_CATEGORY (alpha_color_debug);
30 #define GST_CAT_DEFAULT alpha_color_debug
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))
43 typedef struct _GstAlphaColor GstAlphaColor;
44 typedef struct _GstAlphaColorClass GstAlphaColorClass;
48 GstBaseTransform element;
51 gint in_width, in_height;
53 gint out_width, out_height;
56 struct _GstAlphaColorClass
58 GstBaseTransformClass parent_class;
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>");
68 static GstStaticPadTemplate gst_alpha_color_sink_template =
69 GST_STATIC_PAD_TEMPLATE ("sink",
72 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_BGRA)
75 static GstStaticPadTemplate gst_alpha_color_src_template =
76 GST_STATIC_PAD_TEMPLATE ("src",
79 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV"))
82 GST_BOILERPLATE (GstAlphaColor, gst_alpha_color, GstBaseTransform,
83 GST_TYPE_BASE_TRANSFORM);
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,
93 gst_alpha_color_base_init (gpointer g_class)
95 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
97 gst_element_class_set_details (element_class, &gst_alpha_color_details);
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));
106 gst_alpha_color_class_init (GstAlphaColorClass * klass)
108 GObjectClass *gobject_class;
109 GstElementClass *gstelement_class;
110 GstBaseTransformClass *gstbasetransform_class;
112 gobject_class = (GObjectClass *) klass;
113 gstelement_class = (GstElementClass *) klass;
114 gstbasetransform_class = (GstBaseTransformClass *) klass;
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);
123 GST_DEBUG_CATEGORY_INIT (alpha_color_debug, "alphacolor", 0,
124 "RGB->YUV colorspace conversion preserving the alpha channels");
128 gst_alpha_color_init (GstAlphaColor * alpha, GstAlphaColorClass * g_class)
130 GstBaseTransform *btrans = NULL;
132 btrans = GST_BASE_TRANSFORM (alpha);
134 btrans->always_in_place = TRUE;
138 gst_alpha_color_transform_caps (GstBaseTransform * btrans,
139 GstPadDirection direction, GstCaps * caps)
141 GstAlphaColor *alpha = NULL;
142 GstCaps *result = NULL, *local_caps = NULL;
143 GstPadTemplate *tmpl = NULL;
146 alpha = GST_ALPHA_COLOR (btrans);
148 local_caps = gst_caps_copy (caps);
150 for (i = 0; i < gst_caps_get_size (local_caps); i++) {
151 GstStructure *structure = gst_caps_get_structure (local_caps, i);
153 /* Throw away the structure name and set it to transformed format */
154 if (direction == GST_PAD_SINK) {
155 gst_structure_set_name (structure, "video/x-raw-yuv");
156 } else if (direction == GST_PAD_SRC) {
157 gst_structure_set_name (structure, "video/x-raw-rgb");
159 /* Remove any specific parameter from the structure */
160 gst_structure_remove_field (structure, "format");
161 gst_structure_remove_field (structure, "endianness");
162 gst_structure_remove_field (structure, "depth");
163 gst_structure_remove_field (structure, "bpp");
164 gst_structure_remove_field (structure, "red_mask");
165 gst_structure_remove_field (structure, "green_mask");
166 gst_structure_remove_field (structure, "blue_mask");
167 gst_structure_remove_field (structure, "alpha_mask");
170 /* Get the appropriate template */
171 if (direction == GST_PAD_SINK) {
172 tmpl = gst_static_pad_template_get (&gst_alpha_color_src_template);
173 } else if (direction == GST_PAD_SRC) {
174 tmpl = gst_static_pad_template_get (&gst_alpha_color_sink_template);
177 /* Intersect with our template caps */
178 result = gst_caps_intersect (local_caps, gst_pad_template_get_caps (tmpl));
180 gst_caps_unref (local_caps);
181 gst_caps_do_simplify (result);
183 GST_LOG ("transformed %s to %s", gst_caps_to_string (caps),
184 gst_caps_to_string (result));
190 gst_alpha_color_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
193 GstAlphaColor *alpha;
194 GstStructure *structure;
199 alpha = GST_ALPHA_COLOR (btrans);
200 structure = gst_caps_get_structure (incaps, 0);
202 ret = gst_structure_get_int (structure, "width", &alpha->in_width);
203 ret &= gst_structure_get_int (structure, "height", &alpha->in_height);
204 fps = gst_structure_get_value (structure, "framerate");
205 ret &= (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps));
206 ret &= gst_structure_get_int (structure, "red_mask", &red_mask);
211 alpha->in_rgba = TRUE;
212 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
213 if (red_mask != 0x000000ff)
215 if (red_mask != 0x00ff0000)
217 alpha->in_rgba = FALSE;
223 transform_rgb (guint8 * data, gint size)
228 y = data[0] * 0.299 + data[1] * 0.587 + data[2] * 0.114 + 0;
229 u = data[0] * -0.169 + data[1] * -0.332 + data[2] * 0.500 + 128;
230 v = data[0] * 0.500 + data[1] * -0.419 + data[2] * -0.0813 + 128;
243 transform_bgr (guint8 * data, gint size)
248 y = data[2] * 0.299 + data[1] * 0.587 + data[0] * 0.114 + 0;
249 u = data[2] * -0.169 + data[1] * -0.332 + data[0] * 0.500 + 128;
250 v = data[2] * 0.500 + data[1] * -0.419 + data[0] * -0.0813 + 128;
263 gst_alpha_color_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
265 GstFlowReturn ret = GST_FLOW_OK;
266 GstAlphaColor *alpha;
268 alpha = GST_ALPHA_COLOR (btrans);
270 /* Transform in place */
272 transform_rgb (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
274 transform_bgr (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
280 plugin_init (GstPlugin * plugin)
282 return gst_element_register (plugin, "alphacolor", GST_RANK_NONE,
283 GST_TYPE_ALPHA_COLOR);
286 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
289 "RGB->YUV colorspace conversion preserving the alpha channels",
290 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)