More fractional framerate conversions
[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 = NULL;
142   GstCaps *result = NULL, *local_caps = NULL;
143   GstPadTemplate *tmpl = NULL;
144   guint i;
145
146   alpha = GST_ALPHA_COLOR (btrans);
147
148   local_caps = gst_caps_copy (caps);
149
150   for (i = 0; i < gst_caps_get_size (local_caps); i++) {
151     GstStructure *structure = gst_caps_get_structure (local_caps, i);
152
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");
158     }
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");
168   }
169
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);
175   }
176
177   /* Intersect with our template caps */
178   result = gst_caps_intersect (local_caps, gst_pad_template_get_caps (tmpl));
179
180   gst_caps_unref (local_caps);
181   gst_caps_do_simplify (result);
182
183   GST_LOG ("transformed %s to %s", gst_caps_to_string (caps),
184       gst_caps_to_string (result));
185
186   return result;
187 }
188
189 static gboolean
190 gst_alpha_color_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
191     GstCaps * outcaps)
192 {
193   GstAlphaColor *alpha;
194   GstStructure *structure;
195   gboolean ret;
196   const GValue *fps;
197   gint red_mask;
198
199   alpha = GST_ALPHA_COLOR (btrans);
200   structure = gst_caps_get_structure (incaps, 0);
201
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);
207
208   if (!ret)
209     return FALSE;
210
211   alpha->in_rgba = TRUE;
212 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
213   if (red_mask != 0x000000ff)
214 #else
215   if (red_mask != 0x00ff0000)
216 #endif
217     alpha->in_rgba = FALSE;
218
219   return TRUE;
220 }
221
222 static void
223 transform_rgb (guint8 * data, gint size)
224 {
225   guint8 y, u, v;
226
227   while (size > 0) {
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;
231
232     data[0] = data[3];
233     data[1] = y;
234     data[2] = u;
235     data[3] = v;
236
237     data += 4;
238     size -= 4;
239   }
240 }
241
242 static void
243 transform_bgr (guint8 * data, gint size)
244 {
245   guint8 y, u, v;
246
247   while (size > 0) {
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;
251
252     data[0] = data[3];
253     data[1] = y;
254     data[2] = u;
255     data[3] = v;
256
257     data += 4;
258     size -= 4;
259   }
260 }
261
262 static GstFlowReturn
263 gst_alpha_color_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
264 {
265   GstFlowReturn ret = GST_FLOW_OK;
266   GstAlphaColor *alpha;
267
268   alpha = GST_ALPHA_COLOR (btrans);
269
270   /* Transform in place */
271   if (alpha->in_rgba)
272     transform_rgb (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
273   else
274     transform_bgr (GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE (inbuf));
275
276   return ret;
277 }
278
279 static gboolean
280 plugin_init (GstPlugin * plugin)
281 {
282   return gst_element_register (plugin, "alphacolor", GST_RANK_NONE,
283       GST_TYPE_ALPHA_COLOR);
284 }
285
286 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
287     GST_VERSION_MINOR,
288     "alphacolor",
289     "RGB->YUV colorspace conversion preserving the alpha channels",
290     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)