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/video/video.h>
28 #define GST_TYPE_ALPHA \
29 (gst_alpha_get_type())
30 #define GST_ALPHA(obj) \
31 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_ALPHA,GstAlpha))
32 #define GST_ALPHA_CLASS(klass) \
33 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ALPHA,GstAlphaClass))
34 #define GST_IS_ALPHA(obj) \
35 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_ALPHA))
36 #define GST_IS_ALPHA_CLASS(obj) \
37 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_ALPHA))
39 typedef struct _GstAlpha GstAlpha;
40 typedef struct _GstAlphaClass GstAlphaClass;
50 #define DEFAULT_METHOD ALPHA_METHOD_ADD
51 #define DEFAULT_ALPHA 1.0
52 #define DEFAULT_TARGET_CR 116
53 #define DEFAULT_TARGET_CB 116
64 gint in_width, in_height;
65 gint out_width, out_height;
69 guint target_cr, target_cb;
71 GstAlphaMethod method;
76 GstElementClass parent_class;
79 /* elementfactory information */
80 static GstElementDetails gst_alpha_details =
81 GST_ELEMENT_DETAILS ("alpha filter",
82 "Filter/Effect/Video",
83 "Adds an alpha channel to video",
84 "Wim Taymans <wim@fluendo.com>");
87 /* Alpha signals and args */
104 static GstStaticPadTemplate gst_alpha_src_template =
105 GST_STATIC_PAD_TEMPLATE ("src",
108 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV"))
111 static GstStaticPadTemplate gst_alpha_sink_template =
112 GST_STATIC_PAD_TEMPLATE ("sink",
115 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
119 static void gst_alpha_base_init (gpointer g_class);
120 static void gst_alpha_class_init (GstAlphaClass * klass);
121 static void gst_alpha_init (GstAlpha * alpha);
123 static void gst_alpha_set_property (GObject * object, guint prop_id,
124 const GValue * value, GParamSpec * pspec);
125 static void gst_alpha_get_property (GObject * object, guint prop_id,
126 GValue * value, GParamSpec * pspec);
128 static GstPadLinkReturn
129 gst_alpha_sink_link (GstPad * pad, const GstCaps * caps);
130 static void gst_alpha_chain (GstPad * pad, GstData * _data);
132 static GstElementStateReturn gst_alpha_change_state (GstElement * element);
135 static GstElementClass *parent_class = NULL;
137 #define GST_TYPE_ALPHA_METHOD (gst_alpha_method_get_type())
139 gst_alpha_method_get_type (void)
141 static GType alpha_method_type = 0;
142 static GEnumValue alpha_method[] = {
143 {ALPHA_METHOD_ADD, "0", "Add alpha channel"},
144 {ALPHA_METHOD_GREEN, "1", "Chroma Key green"},
145 {ALPHA_METHOD_BLUE, "2", "Chroma Key blue"},
149 if (!alpha_method_type) {
150 alpha_method_type = g_enum_register_static ("GstAlphaMethod", alpha_method);
152 return alpha_method_type;
155 /* static guint gst_alpha_signals[LAST_SIGNAL] = { 0 }; */
158 gst_alpha_get_type (void)
160 static GType alpha_type = 0;
163 static const GTypeInfo alpha_info = {
164 sizeof (GstAlphaClass),
167 (GClassInitFunc) gst_alpha_class_init,
172 (GInstanceInitFunc) gst_alpha_init,
176 g_type_register_static (GST_TYPE_ELEMENT, "GstAlpha", &alpha_info, 0);
182 gst_alpha_base_init (gpointer g_class)
184 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
186 gst_element_class_set_details (element_class, &gst_alpha_details);
188 gst_element_class_add_pad_template (element_class,
189 gst_static_pad_template_get (&gst_alpha_sink_template));
190 gst_element_class_add_pad_template (element_class,
191 gst_static_pad_template_get (&gst_alpha_src_template));
194 gst_alpha_class_init (GstAlphaClass * klass)
196 GObjectClass *gobject_class;
197 GstElementClass *gstelement_class;
199 gobject_class = (GObjectClass *) klass;
200 gstelement_class = (GstElementClass *) klass;
202 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
204 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_METHOD,
205 g_param_spec_enum ("method", "Method",
206 "How the alpha channels should be created", GST_TYPE_ALPHA_METHOD,
207 DEFAULT_METHOD, (GParamFlags) G_PARAM_READWRITE));
208 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ALPHA,
209 g_param_spec_double ("alpha", "Alpha", "The value for the alpha channel",
210 0.0, 1.0, DEFAULT_ALPHA, (GParamFlags) G_PARAM_READWRITE));
211 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TARGET_CR,
212 g_param_spec_uint ("target_cr", "Target Red", "The Red Chroma target", 0,
213 255, 116, (GParamFlags) G_PARAM_READWRITE));
214 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TARGET_CB,
215 g_param_spec_uint ("target_cb", "Target Blue", "The Blue Chroma target",
216 0, 255, 116, (GParamFlags) G_PARAM_READWRITE));
218 gobject_class->set_property = gst_alpha_set_property;
219 gobject_class->get_property = gst_alpha_get_property;
221 gstelement_class->change_state = gst_alpha_change_state;
225 gst_alpha_init (GstAlpha * alpha)
227 /* create the sink and src pads */
229 gst_pad_new_from_template (gst_static_pad_template_get
230 (&gst_alpha_sink_template), "sink");
231 gst_element_add_pad (GST_ELEMENT (alpha), alpha->sinkpad);
232 gst_pad_set_chain_function (alpha->sinkpad, gst_alpha_chain);
233 gst_pad_set_link_function (alpha->sinkpad, gst_alpha_sink_link);
236 gst_pad_new_from_template (gst_static_pad_template_get
237 (&gst_alpha_src_template), "src");
238 gst_element_add_pad (GST_ELEMENT (alpha), alpha->srcpad);
240 alpha->alpha = DEFAULT_ALPHA;
241 alpha->method = DEFAULT_METHOD;
242 alpha->target_cr = DEFAULT_TARGET_CR;
243 alpha->target_cb = DEFAULT_TARGET_CB;
245 GST_FLAG_SET (alpha, GST_ELEMENT_EVENT_AWARE);
248 /* do we need this function? */
250 gst_alpha_set_property (GObject * object, guint prop_id,
251 const GValue * value, GParamSpec * pspec)
255 /* it's not null if we got it, but it might not be ours */
256 g_return_if_fail (GST_IS_ALPHA (object));
258 alpha = GST_ALPHA (object);
262 alpha->method = g_value_get_enum (value);
265 alpha->alpha = g_value_get_double (value);
268 alpha->target_cb = g_value_get_uint (value);
271 alpha->target_cr = g_value_get_uint (value);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 gst_alpha_get_property (GObject * object, guint prop_id, GValue * value,
284 /* it's not null if we got it, but it might not be ours */
285 g_return_if_fail (GST_IS_ALPHA (object));
287 alpha = GST_ALPHA (object);
291 g_value_set_enum (value, alpha->method);
294 g_value_set_double (value, alpha->alpha);
297 g_value_set_uint (value, alpha->target_cr);
300 g_value_set_uint (value, alpha->target_cb);
303 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 static GstPadLinkReturn
309 gst_alpha_sink_link (GstPad * pad, const GstCaps * caps)
312 GstStructure *structure;
315 alpha = GST_ALPHA (gst_pad_get_parent (pad));
316 structure = gst_caps_get_structure (caps, 0);
318 ret = gst_structure_get_int (structure, "width", &alpha->in_width);
319 ret &= gst_structure_get_int (structure, "height", &alpha->in_height);
321 return GST_PAD_LINK_OK;
325 static int yuv_colors_Y[] = { 16, 150, 29 };
326 static int yuv_colors_U[] = { 128, 46, 255 };
327 static int yuv_colors_V[] = { 128, 21, 107 };
331 gst_alpha_add (guint8 * src, guint8 * dest, gint width, gint height,
334 gint b_alpha = (gint) (alpha * 255);
339 gint half_width = width / 2;
342 size = width * height;
346 srcV = srcU + size / 4;
348 for (i = 0; i < height; i++) {
349 for (j = 0; j < width / 2; j++) {
367 gst_alpha_chroma_key (gchar * src, gchar * dest, gint width, gint height,
368 gboolean soft, gint target_u, gint target_v, gfloat edge_factor,
372 gint f_alpha = (gint) (alpha * 255);
373 guint8 *srcY1, *srcY2, *srcU, *srcV;
374 guint8 *dest1, *dest2;
379 size = width * height;
384 srcV = srcU + size / 4;
387 dest2 = dest + width * 4;
389 for (i = 0; i < height / 2; i++) {
390 for (j = 0; j < width / 2; j++) {
397 // only filter if in top left square
398 if ((x > 0) && (z > 0)) {
399 // only calculate lot of stuff if we'll use soft edges
401 gint ds = (x > z) ? z : x;
403 gfloat df = (gfloat) (ds) / edge_factor;
408 // suppress foreground
416 b_alpha = (int) (f_alpha * (1.0 - df));
418 // kill color and alpha
451 gst_alpha_chain (GstPad * pad, GstData * _data)
456 gint new_width, new_height;
458 alpha = GST_ALPHA (gst_pad_get_parent (pad));
460 if (GST_IS_EVENT (_data)) {
461 GstEvent *event = GST_EVENT (_data);
463 switch (GST_EVENT_TYPE (event)) {
465 gst_pad_event_default (pad, event);
471 buffer = GST_BUFFER (_data);
473 new_width = alpha->in_width;
474 new_height = alpha->in_height;
476 if (new_width != alpha->out_width ||
477 new_height != alpha->out_height || !GST_PAD_CAPS (alpha->srcpad)) {
480 newcaps = gst_caps_copy (gst_pad_get_negotiated_caps (alpha->sinkpad));
481 gst_caps_set_simple (newcaps,
482 "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
483 "width", G_TYPE_INT, new_width, "height", G_TYPE_INT, new_height, NULL);
485 if (!gst_pad_try_set_caps (alpha->srcpad, newcaps)) {
486 GST_ELEMENT_ERROR (alpha, CORE, NEGOTIATION, (NULL), (NULL));
490 alpha->out_width = new_width;
491 alpha->out_height = new_height;
494 outbuf = gst_buffer_new_and_alloc (new_width * new_height * 4);
495 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
496 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
498 switch (alpha->method) {
499 case ALPHA_METHOD_ADD:
500 gst_alpha_add (GST_BUFFER_DATA (buffer),
501 GST_BUFFER_DATA (outbuf), new_width, new_height, alpha->alpha);
503 case ALPHA_METHOD_GREEN:
504 gst_alpha_chroma_key (GST_BUFFER_DATA (buffer),
505 GST_BUFFER_DATA (outbuf),
506 new_width, new_height,
507 TRUE, alpha->target_cr, alpha->target_cb, 1.0, alpha->alpha);
509 case ALPHA_METHOD_BLUE:
510 gst_alpha_chroma_key (GST_BUFFER_DATA (buffer),
511 GST_BUFFER_DATA (outbuf),
512 new_width, new_height, TRUE, 100, 100, 1.0, alpha->alpha);
516 gst_buffer_unref (buffer);
518 gst_pad_push (alpha->srcpad, GST_DATA (outbuf));
521 static GstElementStateReturn
522 gst_alpha_change_state (GstElement * element)
526 alpha = GST_ALPHA (element);
528 switch (GST_STATE_TRANSITION (element)) {
529 case GST_STATE_NULL_TO_READY:
531 case GST_STATE_READY_TO_PAUSED:
533 case GST_STATE_PAUSED_TO_PLAYING:
535 case GST_STATE_PLAYING_TO_PAUSED:
537 case GST_STATE_PAUSED_TO_READY:
539 case GST_STATE_READY_TO_NULL:
543 parent_class->change_state (element);
545 return GST_STATE_SUCCESS;
549 plugin_init (GstPlugin * plugin)
551 return gst_element_register (plugin, "alpha", GST_RANK_NONE, GST_TYPE_ALPHA);
554 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
557 "resizes a video by adding borders or cropping",
558 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)