2 * Copyright (C) 2009,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
21 * SECTION:element-shapewipe
23 * The shapewipe element provides custom transitions on video streams
24 * based on a grayscale bitmap. The state of the transition can be
25 * controlled by the position property and an optional blended border
26 * can be added by the border property.
28 * Transition bitmaps can be downloaded from the
29 * <ulink url="http://cinelerra.org/transitions.php">Cinelerra transition</ulink>
33 * <title>Example launch line</title>
35 * gst-launch -v videotestsrc ! video/x-raw,format=(string)AYUV,width=640,height=480 ! shapewipe position=0.5 name=shape ! videomixer name=mixer ! videoconvert ! autovideosink filesrc location=mask.png ! typefind ! decodebin2 ! videoconvert ! videoscale ! queue ! shape.mask_sink videotestsrc pattern=snow ! video/x-raw,format=(string)AYUV,width=640,height=480 ! queue ! mixer.
36 * ]| This pipeline adds the transition from mask.png with position 0.5 to an SMPTE test screen and snow.
48 #include <gst/controller/gstcontroller.h>
50 #include "gstshapewipe.h"
52 static void gst_shape_wipe_finalize (GObject * object);
53 static void gst_shape_wipe_get_property (GObject * object, guint prop_id,
54 GValue * value, GParamSpec * pspec);
55 static void gst_shape_wipe_set_property (GObject * object, guint prop_id,
56 const GValue * value, GParamSpec * pspec);
58 static void gst_shape_wipe_reset (GstShapeWipe * self);
59 static void gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
60 GstClockTimeDiff diff, GstClockTime time);
61 static void gst_shape_wipe_reset_qos (GstShapeWipe * self);
62 static void gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
65 static GstStateChangeReturn gst_shape_wipe_change_state (GstElement * element,
66 GstStateChange transition);
68 static GstFlowReturn gst_shape_wipe_video_sink_chain (GstPad * pad,
70 static gboolean gst_shape_wipe_video_sink_event (GstPad * pad,
72 static gboolean gst_shape_wipe_video_sink_setcaps (GstShapeWipe * self,
74 static GstCaps *gst_shape_wipe_video_sink_getcaps (GstPad * pad,
76 static gboolean gst_shape_wipe_video_sink_query (GstPad * pad,
78 static GstFlowReturn gst_shape_wipe_mask_sink_chain (GstPad * pad,
80 static gboolean gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event);
81 static gboolean gst_shape_wipe_mask_sink_setcaps (GstShapeWipe * self,
83 static GstCaps *gst_shape_wipe_mask_sink_getcaps (GstPad * pad,
85 static gboolean gst_shape_wipe_src_event (GstPad * pad, GstEvent * event);
86 static GstCaps *gst_shape_wipe_src_getcaps (GstPad * pad, GstCaps * filter);
87 static gboolean gst_shape_wipe_src_query (GstPad * pad, GstQuery * query);
96 #define DEFAULT_POSITION 0.0
97 #define DEFAULT_BORDER 0.0
99 static GstStaticPadTemplate video_sink_pad_template =
100 GST_STATIC_PAD_TEMPLATE ("video_sink",
103 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, ARGB, BGRA, ABGR, RGBA }")));
105 static GstStaticPadTemplate mask_sink_pad_template =
106 GST_STATIC_PAD_TEMPLATE ("mask_sink",
109 GST_STATIC_CAPS ("video/x-raw, "
110 "format = (string) GRAY8, "
111 "width = " GST_VIDEO_SIZE_RANGE ", "
112 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1 ; "
113 "video/x-raw, " "format = (string) " GST_VIDEO_NE (GRAY16) ", "
114 "width = " GST_VIDEO_SIZE_RANGE ", "
115 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1"));
117 static GstStaticPadTemplate src_pad_template =
118 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
119 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, ARGB, BGRA, ABGR, RGBA }")));
121 GST_DEBUG_CATEGORY_STATIC (gst_shape_wipe_debug);
122 #define GST_CAT_DEFAULT gst_shape_wipe_debug
124 #define gst_shape_wipe_parent_class parent_class
125 G_DEFINE_TYPE (GstShapeWipe, gst_shape_wipe, GST_TYPE_ELEMENT);
128 gst_shape_wipe_class_init (GstShapeWipeClass * klass)
130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
133 gobject_class->finalize = gst_shape_wipe_finalize;
134 gobject_class->set_property = gst_shape_wipe_set_property;
135 gobject_class->get_property = gst_shape_wipe_get_property;
137 g_object_class_install_property (gobject_class, PROP_POSITION,
138 g_param_spec_float ("position", "Position", "Position of the mask",
139 0.0, 1.0, DEFAULT_POSITION,
140 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
141 g_object_class_install_property (gobject_class, PROP_BORDER,
142 g_param_spec_float ("border", "Border", "Border of the mask",
143 0.0, 1.0, DEFAULT_BORDER,
144 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
146 gstelement_class->change_state =
147 GST_DEBUG_FUNCPTR (gst_shape_wipe_change_state);
149 gst_element_class_set_details_simple (gstelement_class,
150 "Shape Wipe transition filter",
151 "Filter/Editor/Video",
152 "Adds a shape wipe transition to a video stream",
153 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
155 gst_element_class_add_pad_template (gstelement_class,
156 gst_static_pad_template_get (&video_sink_pad_template));
157 gst_element_class_add_pad_template (gstelement_class,
158 gst_static_pad_template_get (&mask_sink_pad_template));
159 gst_element_class_add_pad_template (gstelement_class,
160 gst_static_pad_template_get (&src_pad_template));
164 gst_shape_wipe_init (GstShapeWipe * self)
166 self->video_sinkpad =
167 gst_pad_new_from_static_template (&video_sink_pad_template, "video_sink");
168 gst_pad_set_chain_function (self->video_sinkpad,
169 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_chain));
170 gst_pad_set_event_function (self->video_sinkpad,
171 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_event));
172 gst_pad_set_getcaps_function (self->video_sinkpad,
173 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_getcaps));
174 gst_pad_set_query_function (self->video_sinkpad,
175 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_query));
176 gst_element_add_pad (GST_ELEMENT (self), self->video_sinkpad);
179 gst_pad_new_from_static_template (&mask_sink_pad_template, "mask_sink");
180 gst_pad_set_chain_function (self->mask_sinkpad,
181 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_chain));
182 gst_pad_set_event_function (self->mask_sinkpad,
183 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_event));
184 gst_pad_set_getcaps_function (self->mask_sinkpad,
185 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_getcaps));
186 gst_element_add_pad (GST_ELEMENT (self), self->mask_sinkpad);
188 self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
189 gst_pad_set_event_function (self->srcpad,
190 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_event));
191 gst_pad_set_getcaps_function (self->srcpad,
192 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_getcaps));
193 gst_pad_set_query_function (self->srcpad,
194 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_query));
195 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
197 self->mask_mutex = g_mutex_new ();
198 self->mask_cond = g_cond_new ();
200 gst_shape_wipe_reset (self);
204 gst_shape_wipe_get_property (GObject * object, guint prop_id,
205 GValue * value, GParamSpec * pspec)
207 GstShapeWipe *self = GST_SHAPE_WIPE (object);
211 g_value_set_float (value, self->mask_position);
214 g_value_set_float (value, self->mask_border);
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gst_shape_wipe_set_property (GObject * object, guint prop_id,
224 const GValue * value, GParamSpec * pspec)
226 GstShapeWipe *self = GST_SHAPE_WIPE (object);
230 gfloat f = g_value_get_float (value);
232 GST_LOG_OBJECT (self, "Setting mask position: %f", f);
233 self->mask_position = f;
237 gfloat f = g_value_get_float (value);
239 GST_LOG_OBJECT (self, "Setting mask border: %f", f);
240 self->mask_border = f;
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 gst_shape_wipe_finalize (GObject * object)
252 GstShapeWipe *self = GST_SHAPE_WIPE (object);
254 gst_shape_wipe_reset (self);
257 g_cond_free (self->mask_cond);
258 self->mask_cond = NULL;
260 if (self->mask_mutex)
261 g_mutex_free (self->mask_mutex);
262 self->mask_mutex = NULL;
264 G_OBJECT_CLASS (parent_class)->finalize (object);
268 gst_shape_wipe_reset (GstShapeWipe * self)
270 GST_DEBUG_OBJECT (self, "Resetting internal state");
273 gst_buffer_unref (self->mask);
276 g_mutex_lock (self->mask_mutex);
277 g_cond_signal (self->mask_cond);
278 g_mutex_unlock (self->mask_mutex);
280 gst_video_info_init (&self->info);
283 gst_segment_init (&self->segment, GST_FORMAT_TIME);
285 gst_shape_wipe_reset_qos (self);
286 self->frame_duration = 0;
290 gst_shape_wipe_video_sink_setcaps (GstShapeWipe * self, GstCaps * caps)
295 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
297 if (!gst_video_info_from_caps (&info, caps))
300 if (self->info.width != info.width || self->info.height != info.height) {
301 g_mutex_lock (self->mask_mutex);
304 gst_buffer_unref (self->mask);
306 g_mutex_unlock (self->mask_mutex);
311 self->frame_duration =
312 gst_util_uint64_scale (GST_SECOND, info.fps_d, info.fps_n);
314 self->frame_duration = 0;
316 ret = gst_pad_set_caps (self->srcpad, caps);
323 GST_ERROR_OBJECT (self, "Invalid caps");
329 gst_shape_wipe_video_sink_getcaps (GstPad * pad, GstCaps * filter)
331 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
334 if (gst_pad_has_current_caps (pad))
335 return gst_pad_get_current_caps (pad);
337 tmp = gst_pad_peer_get_caps (self->srcpad, NULL);
339 ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
340 gst_caps_unref (tmp);
342 ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
345 GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
347 if (gst_caps_is_empty (ret))
350 tmp = gst_pad_peer_get_caps (pad, NULL);
352 GST_LOG_OBJECT (pad, "peerpad accepted caps: %" GST_PTR_FORMAT, tmp);
354 GstCaps *intersection;
356 intersection = gst_caps_intersect (tmp, ret);
357 gst_caps_unref (tmp);
358 gst_caps_unref (ret);
362 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, tmp);
364 if (gst_caps_is_empty (ret))
367 if (self->info.height && self->info.width) {
370 n = gst_caps_get_size (ret);
371 for (i = 0; i < n; i++) {
372 GstStructure *s = gst_caps_get_structure (ret, i);
374 gst_structure_set (s, "width", G_TYPE_INT, self->info.width, "height",
375 G_TYPE_INT, self->info.height, NULL);
379 tmp = gst_pad_peer_get_caps (self->mask_sinkpad, NULL);
381 GST_LOG_OBJECT (pad, "mask accepted caps: %" GST_PTR_FORMAT, tmp);
383 GstCaps *intersection, *tmp2;
386 tmp = gst_caps_make_writable (tmp);
388 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
390 intersection = gst_caps_intersect (tmp, tmp2);
391 gst_caps_unref (tmp);
392 gst_caps_unref (tmp2);
395 n = gst_caps_get_size (tmp);
397 for (i = 0; i < n; i++) {
398 GstStructure *s = gst_caps_get_structure (tmp, i);
400 gst_structure_remove_fields (s, "format", "framerate", NULL);
401 gst_structure_set_name (s, "video/x-raw");
404 intersection = gst_caps_intersect (tmp, ret);
405 gst_caps_unref (tmp);
406 gst_caps_unref (ret);
411 gst_object_unref (self);
413 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
419 gst_shape_wipe_mask_sink_setcaps (GstShapeWipe * self, GstCaps * caps)
423 gint width, height, bpp;
425 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
427 s = gst_caps_get_structure (caps, 0);
429 if (!gst_structure_get_int (s, "width", &width) ||
430 !gst_structure_get_int (s, "height", &height) ||
431 !gst_structure_get_int (s, "bpp", &bpp)) {
436 if ((self->info.width != width || self->info.height != height) &&
437 self->info.width > 0 && self->info.height > 0) {
438 GST_ERROR_OBJECT (self, "Mask caps must have the same width/height "
439 "as the video caps");
443 self->info.width = width;
444 self->info.height = height;
447 self->mask_bpp = bpp;
454 gst_shape_wipe_mask_sink_getcaps (GstPad * pad, GstCaps * filter)
456 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
460 if (gst_pad_has_current_caps (pad))
461 return gst_pad_get_current_caps (pad);
463 tmp = gst_pad_peer_get_caps (self->video_sinkpad, NULL);
466 gst_caps_intersect (tmp,
467 gst_pad_get_pad_template_caps (self->video_sinkpad));
468 gst_caps_unref (tmp);
470 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
473 GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
475 if (gst_caps_is_empty (ret))
478 tmp = gst_pad_peer_get_caps (self->srcpad, NULL);
479 GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
482 GstCaps *intersection;
484 intersection = gst_caps_intersect (ret, tmp);
485 gst_caps_unref (ret);
486 gst_caps_unref (tmp);
490 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
492 if (gst_caps_is_empty (ret))
495 n = gst_caps_get_size (ret);
496 tmp = gst_caps_new_empty ();
497 for (i = 0; i < n; i++) {
498 GstStructure *s = gst_caps_get_structure (ret, i);
501 gst_structure_set_name (s, "video/x-raw");
502 gst_structure_remove_fields (s, "format", "framerate", NULL);
504 if (self->info.width && self->info.height)
505 gst_structure_set (s, "width", G_TYPE_INT, self->info.width, "height",
506 G_TYPE_INT, self->info.height, NULL);
508 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
510 t = gst_structure_copy (s);
512 gst_structure_set (s, "format", G_TYPE_STRING, GST_VIDEO_NE (GRAY16), NULL);
513 gst_structure_set (t, "format", G_TYPE_STRING, "GRAY8", NULL);
515 gst_caps_append_structure (tmp, t);
517 gst_caps_append (ret, tmp);
519 tmp = gst_pad_peer_get_caps (pad, NULL);
520 GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, tmp);
523 GstCaps *intersection;
525 intersection = gst_caps_intersect (tmp, ret);
526 gst_caps_unref (tmp);
527 gst_caps_unref (ret);
532 gst_object_unref (self);
534 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
540 gst_shape_wipe_src_getcaps (GstPad * pad, GstCaps * filter)
542 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
545 if (gst_pad_has_current_caps (pad))
546 return gst_pad_get_current_caps (pad);
547 else if (gst_pad_has_current_caps (self->video_sinkpad))
548 return gst_pad_get_current_caps (self->video_sinkpad);
550 tmp = gst_pad_peer_get_caps (self->video_sinkpad, NULL);
553 gst_caps_intersect (tmp,
554 gst_pad_get_pad_template_caps (self->video_sinkpad));
555 gst_caps_unref (tmp);
557 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
560 GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
562 if (gst_caps_is_empty (ret))
565 tmp = gst_pad_peer_get_caps (pad, NULL);
566 GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, ret);
568 GstCaps *intersection;
570 intersection = gst_caps_intersect (tmp, ret);
571 gst_caps_unref (tmp);
572 gst_caps_unref (ret);
576 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
578 if (gst_caps_is_empty (ret))
581 if (self->info.height && self->info.width) {
584 n = gst_caps_get_size (ret);
585 for (i = 0; i < n; i++) {
586 GstStructure *s = gst_caps_get_structure (ret, i);
588 gst_structure_set (s, "width", G_TYPE_INT, self->info.width, "height",
589 G_TYPE_INT, self->info.height, NULL);
593 tmp = gst_pad_peer_get_caps (self->mask_sinkpad, NULL);
594 GST_LOG_OBJECT (pad, "mask sink accepted caps: %" GST_PTR_FORMAT, ret);
596 GstCaps *intersection, *tmp2;
599 tmp = gst_caps_make_writable (tmp);
600 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
602 intersection = gst_caps_intersect (tmp, tmp2);
603 gst_caps_unref (tmp);
604 gst_caps_unref (tmp2);
607 n = gst_caps_get_size (tmp);
609 for (i = 0; i < n; i++) {
610 GstStructure *s = gst_caps_get_structure (tmp, i);
612 gst_structure_remove_fields (s, "format", "framerate", NULL);
613 gst_structure_set_name (s, "video/x-raw");
616 intersection = gst_caps_intersect (tmp, ret);
617 gst_caps_unref (tmp);
618 gst_caps_unref (ret);
624 gst_object_unref (self);
626 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
632 gst_shape_wipe_video_sink_query (GstPad * pad, GstQuery * query)
634 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
637 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
638 gst_query_type_get_name (GST_QUERY_TYPE (query)));
640 ret = gst_pad_peer_query (self->srcpad, query);
642 gst_object_unref (self);
647 gst_shape_wipe_src_query (GstPad * pad, GstQuery * query)
649 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
652 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
653 gst_query_type_get_name (GST_QUERY_TYPE (query)));
655 ret = gst_pad_peer_query (self->video_sinkpad, query);
657 gst_object_unref (self);
662 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
663 GstClockTimeDiff diff, GstClockTime timestamp)
665 GST_OBJECT_LOCK (self);
666 self->proportion = proportion;
667 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
668 if (G_UNLIKELY (diff > 0))
669 self->earliest_time = timestamp + 2 * diff + self->frame_duration;
671 self->earliest_time = timestamp + diff;
673 self->earliest_time = GST_CLOCK_TIME_NONE;
675 GST_OBJECT_UNLOCK (self);
679 gst_shape_wipe_reset_qos (GstShapeWipe * self)
681 gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
685 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
688 GST_OBJECT_LOCK (self);
689 *proportion = self->proportion;
690 *time = self->earliest_time;
691 GST_OBJECT_UNLOCK (self);
694 /* Perform qos calculations before processing the next frame. Returns TRUE if
695 * the frame should be processed, FALSE if the frame can be dropped entirely */
697 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
699 GstClockTime qostime, earliest_time;
702 /* no timestamp, can't do QoS => process frame */
703 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
704 GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
708 /* get latest QoS observation values */
709 gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
711 /* skip qos if we have no observation (yet) => process frame */
712 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
713 GST_LOG_OBJECT (self, "no observation yet, process frame");
717 /* qos is done on running time */
718 qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
721 /* see how our next timestamp relates to the latest qos timestamp */
722 GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
723 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
725 if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
726 GST_DEBUG_OBJECT (self, "we are late, drop frame");
730 GST_LOG_OBJECT (self, "process frame");
734 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
736 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstVideoFrame * inframe, \
737 GstVideoFrame * maskframe, GstVideoFrame * outframe) \
739 const guint##depth *mask = (const guint##depth *) GST_VIDEO_FRAME_PLANE_DATA (maskframe, 0); \
740 const guint8 *input = (const guint8 *) GST_VIDEO_FRAME_PLANE_DATA (inframe, 0); \
741 guint8 *output = (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (outframe, 0); \
743 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->info.width) : \
744 GST_ROUND_UP_4 (self->info.width)) - self->info.width; \
745 gfloat position = self->mask_position; \
746 gfloat low = position - (self->mask_border / 2.0f); \
747 gfloat high = position + (self->mask_border / 2.0f); \
748 guint32 low_i, high_i, round_i; \
749 gint width = self->info.width, height = self->info.height; \
761 low_i = low * 65536; \
762 high_i = high * 65536; \
763 round_i = (high_i - low_i) >> 1; \
765 for (i = 0; i < height; i++) { \
766 for (j = 0; j < width; j++) { \
767 guint32 in = *mask << shift; \
770 output[a] = 0x00; /* A */ \
771 output[r] = input[r]; /* R */ \
772 output[g] = input[g]; /* G */ \
773 output[b] = input[b]; /* B */ \
774 } else if (in >= high_i) { \
775 output[a] = input[a]; /* A */ \
776 output[r] = input[r]; /* R */ \
777 output[g] = input[g]; /* G */ \
778 output[b] = input[b]; /* B */ \
781 /* Note: This will never overflow or be larger than 255! */ \
782 val = (((in - low_i) << 16) + round_i) / (high_i - low_i); \
783 val = (val * input[a] + 32768) >> 16; \
785 output[a] = val; /* A */ \
786 output[r] = input[r]; /* R */ \
787 output[g] = input[g]; /* G */ \
788 output[b] = input[b]; /* B */ \
795 mask += mask_increment; \
799 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
800 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
802 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
803 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
806 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
808 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
809 GstFlowReturn ret = GST_FLOW_OK;
810 GstBuffer *mask = NULL, *outbuf = NULL;
811 GstClockTime timestamp;
812 gboolean new_outbuf = FALSE;
813 GstVideoFrame inframe, outframe, maskframe;
815 if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&self->info) ==
816 GST_VIDEO_FORMAT_UNKNOWN))
819 timestamp = GST_BUFFER_TIMESTAMP (buffer);
821 gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
823 if (GST_CLOCK_TIME_IS_VALID (timestamp))
824 gst_object_sync_values (G_OBJECT (self), timestamp);
826 GST_LOG_OBJECT (self,
827 "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %f",
828 GST_TIME_ARGS (timestamp), self->mask_position);
830 g_mutex_lock (self->mask_mutex);
835 g_cond_wait (self->mask_cond, self->mask_mutex);
837 if (self->mask == NULL || self->shutdown) {
840 mask = gst_buffer_ref (self->mask);
842 g_mutex_unlock (self->mask_mutex);
844 if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
847 /* Try to blend inplace, if it's not possible
848 * get a new buffer from downstream. */
849 if (!gst_buffer_is_writable (buffer)) {
850 outbuf = gst_buffer_new_allocate (NULL, gst_buffer_get_size (buffer), 0);
851 gst_buffer_copy_into (outbuf, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
857 gst_video_frame_map (&inframe, &self->info, buffer, GST_MAP_READ);
858 gst_video_frame_map (&maskframe, &self->info, mask, GST_MAP_READ);
859 gst_video_frame_map (&outframe, &self->info, outbuf, GST_MAP_WRITE);
861 switch (GST_VIDEO_INFO_FORMAT (&self->info)) {
862 case GST_VIDEO_FORMAT_AYUV:
863 case GST_VIDEO_FORMAT_ARGB:
864 case GST_VIDEO_FORMAT_ABGR:
865 if (self->mask_bpp == 16)
866 gst_shape_wipe_blend_argb_16 (self, &inframe, &maskframe, &outframe);
868 gst_shape_wipe_blend_argb_8 (self, &inframe, &maskframe, &outframe);
870 case GST_VIDEO_FORMAT_BGRA:
871 case GST_VIDEO_FORMAT_RGBA:
872 if (self->mask_bpp == 16)
873 gst_shape_wipe_blend_bgra_16 (self, &inframe, &maskframe, &outframe);
875 gst_shape_wipe_blend_bgra_8 (self, &inframe, &maskframe, &outframe);
878 g_assert_not_reached ();
882 gst_video_frame_unmap (&outframe);
883 gst_video_frame_unmap (&maskframe);
884 gst_video_frame_unmap (&inframe);
886 gst_buffer_unref (mask);
888 gst_buffer_unref (buffer);
890 ret = gst_pad_push (self->srcpad, outbuf);
891 if (G_UNLIKELY (ret != GST_FLOW_OK))
899 GST_ERROR_OBJECT (self, "No valid caps yet");
900 gst_buffer_unref (buffer);
901 return GST_FLOW_NOT_NEGOTIATED;
905 GST_DEBUG_OBJECT (self, "Shutting down");
906 gst_buffer_unref (buffer);
907 return GST_FLOW_WRONG_STATE;
911 GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
912 gst_buffer_unref (buffer);
913 gst_buffer_unref (mask);
918 GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
919 gst_flow_get_name (ret));
925 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
927 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
928 GstFlowReturn ret = GST_FLOW_OK;
930 g_mutex_lock (self->mask_mutex);
931 GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
933 gst_buffer_replace (&self->mask, buffer);
934 g_cond_signal (self->mask_cond);
935 g_mutex_unlock (self->mask_mutex);
937 gst_buffer_unref (buffer);
942 static GstStateChangeReturn
943 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
945 GstShapeWipe *self = GST_SHAPE_WIPE (element);
946 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
948 switch (transition) {
949 case GST_STATE_CHANGE_READY_TO_PAUSED:
950 self->shutdown = FALSE;
952 case GST_STATE_CHANGE_PAUSED_TO_READY:
953 /* Unblock video sink chain function */
954 g_mutex_lock (self->mask_mutex);
955 self->shutdown = TRUE;
956 g_cond_signal (self->mask_cond);
957 g_mutex_unlock (self->mask_mutex);
963 if (GST_ELEMENT_CLASS (parent_class)->change_state)
964 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
966 switch (transition) {
967 case GST_STATE_CHANGE_PAUSED_TO_READY:
968 gst_shape_wipe_reset (self);
978 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
980 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
983 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
985 switch (GST_EVENT_TYPE (event)) {
990 gst_event_parse_caps (event, &caps);
991 ret = gst_shape_wipe_video_sink_setcaps (self, caps);
992 gst_event_unref (event);
995 case GST_EVENT_SEGMENT:
999 gst_event_copy_segment (event, &seg);
1000 if (seg.format == GST_FORMAT_TIME) {
1001 GST_DEBUG_OBJECT (pad,
1002 "Got SEGMENT event in GST_FORMAT_TIME %" GST_PTR_FORMAT, &seg);
1003 self->segment = seg;
1005 gst_segment_init (&self->segment, GST_FORMAT_TIME);
1009 case GST_EVENT_FLUSH_STOP:
1010 gst_shape_wipe_reset_qos (self);
1013 ret = gst_pad_push_event (self->srcpad, event);
1017 gst_object_unref (self);
1022 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1024 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1026 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1028 switch (GST_EVENT_TYPE (event)) {
1029 case GST_EVENT_CAPS:
1033 gst_event_parse_caps (event, &caps);
1034 gst_shape_wipe_mask_sink_setcaps (self, caps);
1037 case GST_EVENT_FLUSH_STOP:
1038 g_mutex_lock (self->mask_mutex);
1039 gst_buffer_replace (&self->mask, NULL);
1040 g_mutex_unlock (self->mask_mutex);
1046 /* Dropping all events here */
1047 gst_event_unref (event);
1049 gst_object_unref (self);
1054 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1056 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1059 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1061 switch (GST_EVENT_TYPE (event)) {
1062 case GST_EVENT_QOS:{
1064 GstClockTimeDiff diff;
1065 GstClockTime timestamp;
1068 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1070 gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1074 ret = gst_pad_push_event (self->video_sinkpad, event);
1078 gst_object_unref (self);
1083 plugin_init (GstPlugin * plugin)
1085 GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1086 "shapewipe element");
1088 gst_controller_init (NULL, NULL);
1090 if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1091 GST_TYPE_SHAPE_WIPE))
1097 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1100 "Shape Wipe transition filter",
1101 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)