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-yuv,width=640,height=480 ! shapewipe position=0.5 name=shape ! videomixer name=mixer ! ffmpegcolorspace ! autovideosink filesrc location=mask.png ! typefind ! decodebin2 ! ffmpegcolorspace ! videoscale ! queue ! shape.mask_sink videotestsrc pattern=snow ! video/x-raw-yuv,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 (GstPad * pad,
74 static GstCaps *gst_shape_wipe_video_sink_getcaps (GstPad * pad);
75 static GstFlowReturn gst_shape_wipe_video_sink_bufferalloc (GstPad * pad,
76 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
77 static gboolean gst_shape_wipe_video_sink_query (GstPad * pad,
79 static GstFlowReturn gst_shape_wipe_mask_sink_chain (GstPad * pad,
81 static gboolean gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event);
82 static gboolean gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps);
83 static GstCaps *gst_shape_wipe_mask_sink_getcaps (GstPad * pad);
84 static gboolean gst_shape_wipe_src_event (GstPad * pad, GstEvent * event);
85 static GstCaps *gst_shape_wipe_src_getcaps (GstPad * pad);
86 static gboolean gst_shape_wipe_src_query (GstPad * pad, GstQuery * query);
95 #define DEFAULT_POSITION 0.0
96 #define DEFAULT_BORDER 0.0
98 static GstStaticPadTemplate video_sink_pad_template =
99 GST_STATIC_PAD_TEMPLATE ("video_sink",
102 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB " ; "
103 GST_VIDEO_CAPS_BGRA));
105 static GstStaticPadTemplate mask_sink_pad_template =
106 GST_STATIC_PAD_TEMPLATE ("mask_sink",
109 GST_STATIC_CAPS ("video/x-raw-gray, "
112 "width = " GST_VIDEO_SIZE_RANGE ", "
113 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1 ; "
114 "video/x-raw-gray, " "bpp = 16, " "depth = 16, "
115 "endianness = BYTE_ORDER, " "width = " GST_VIDEO_SIZE_RANGE ", "
116 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1"));
118 static GstStaticPadTemplate src_pad_template =
119 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
120 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB " ; "
121 GST_VIDEO_CAPS_BGRA));
123 GST_DEBUG_CATEGORY_STATIC (gst_shape_wipe_debug);
124 #define GST_CAT_DEFAULT gst_shape_wipe_debug
126 GST_BOILERPLATE (GstShapeWipe, gst_shape_wipe, GstElement, GST_TYPE_ELEMENT);
129 gst_shape_wipe_base_init (gpointer g_class)
131 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
133 gst_element_class_set_details_simple (gstelement_class,
134 "Shape Wipe transition filter",
135 "Filter/Editor/Video",
136 "Adds a shape wipe transition to a video stream",
137 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
139 gst_element_class_add_pad_template (gstelement_class,
140 gst_static_pad_template_get (&video_sink_pad_template));
141 gst_element_class_add_pad_template (gstelement_class,
142 gst_static_pad_template_get (&mask_sink_pad_template));
143 gst_element_class_add_pad_template (gstelement_class,
144 gst_static_pad_template_get (&src_pad_template));
148 gst_shape_wipe_class_init (GstShapeWipeClass * klass)
150 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
153 gobject_class->finalize = gst_shape_wipe_finalize;
154 gobject_class->set_property = gst_shape_wipe_set_property;
155 gobject_class->get_property = gst_shape_wipe_get_property;
157 g_object_class_install_property (gobject_class, PROP_POSITION,
158 g_param_spec_float ("position", "Position", "Position of the mask",
159 0.0, 1.0, DEFAULT_POSITION,
160 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
161 g_object_class_install_property (gobject_class, PROP_BORDER,
162 g_param_spec_float ("border", "Border", "Border of the mask",
163 0.0, 1.0, DEFAULT_BORDER,
164 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
166 gstelement_class->change_state =
167 GST_DEBUG_FUNCPTR (gst_shape_wipe_change_state);
171 gst_shape_wipe_init (GstShapeWipe * self, GstShapeWipeClass * g_class)
173 self->video_sinkpad =
174 gst_pad_new_from_static_template (&video_sink_pad_template, "video_sink");
175 gst_pad_set_chain_function (self->video_sinkpad,
176 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_chain));
177 gst_pad_set_event_function (self->video_sinkpad,
178 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_event));
179 gst_pad_set_setcaps_function (self->video_sinkpad,
180 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_setcaps));
181 gst_pad_set_getcaps_function (self->video_sinkpad,
182 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_getcaps));
183 gst_pad_set_bufferalloc_function (self->video_sinkpad,
184 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_bufferalloc));
185 gst_pad_set_query_function (self->video_sinkpad,
186 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_query));
187 gst_element_add_pad (GST_ELEMENT (self), self->video_sinkpad);
190 gst_pad_new_from_static_template (&mask_sink_pad_template, "mask_sink");
191 gst_pad_set_chain_function (self->mask_sinkpad,
192 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_chain));
193 gst_pad_set_event_function (self->mask_sinkpad,
194 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_event));
195 gst_pad_set_setcaps_function (self->mask_sinkpad,
196 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_setcaps));
197 gst_pad_set_getcaps_function (self->mask_sinkpad,
198 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_getcaps));
199 gst_element_add_pad (GST_ELEMENT (self), self->mask_sinkpad);
201 self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
202 gst_pad_set_event_function (self->srcpad,
203 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_event));
204 gst_pad_set_getcaps_function (self->srcpad,
205 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_getcaps));
206 gst_pad_set_query_function (self->srcpad,
207 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_query));
208 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
210 self->mask_mutex = g_mutex_new ();
211 self->mask_cond = g_cond_new ();
213 gst_shape_wipe_reset (self);
217 gst_shape_wipe_get_property (GObject * object, guint prop_id,
218 GValue * value, GParamSpec * pspec)
220 GstShapeWipe *self = GST_SHAPE_WIPE (object);
224 g_value_set_float (value, self->mask_position);
227 g_value_set_float (value, self->mask_border);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 gst_shape_wipe_set_property (GObject * object, guint prop_id,
237 const GValue * value, GParamSpec * pspec)
239 GstShapeWipe *self = GST_SHAPE_WIPE (object);
243 self->mask_position = g_value_get_float (value);
246 self->mask_border = g_value_get_float (value);
249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 gst_shape_wipe_finalize (GObject * object)
257 GstShapeWipe *self = GST_SHAPE_WIPE (object);
259 gst_shape_wipe_reset (self);
262 g_cond_free (self->mask_cond);
263 self->mask_cond = NULL;
265 if (self->mask_mutex)
266 g_mutex_free (self->mask_mutex);
267 self->mask_mutex = NULL;
269 G_OBJECT_CLASS (parent_class)->finalize (object);
273 gst_shape_wipe_reset (GstShapeWipe * self)
276 gst_buffer_unref (self->mask);
279 g_cond_signal (self->mask_cond);
281 self->fmt = GST_VIDEO_FORMAT_UNKNOWN;
282 self->width = self->height = 0;
285 gst_segment_init (&self->segment, GST_FORMAT_TIME);
287 gst_shape_wipe_reset_qos (self);
288 self->frame_duration = 0;
292 gst_shape_wipe_video_sink_bufferalloc (GstPad * pad, guint64 offset, guint size,
293 GstCaps * caps, GstBuffer ** buf)
295 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
296 GstFlowReturn ret = GST_FLOW_OK;
298 GST_DEBUG_OBJECT (pad, "Allocating buffer with offset 0x%" G_GINT64_MODIFIER
299 "x and size %u with caps: %" GST_PTR_FORMAT, offset, size, caps);
303 ret = gst_pad_alloc_buffer (self->srcpad, offset, size, caps, buf);
305 gst_object_unref (self);
311 gst_shape_wipe_video_sink_setcaps (GstPad * pad, GstCaps * caps)
313 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
320 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
322 s = gst_caps_get_structure (caps, 0);
324 if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
325 !gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)) {
331 if (self->width != width || self->height != height) {
332 g_mutex_lock (self->mask_mutex);
334 self->height = height;
337 gst_buffer_unref (self->mask);
339 g_mutex_unlock (self->mask_mutex);
343 self->frame_duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
345 self->frame_duration = 0;
347 ret = gst_pad_set_caps (self->srcpad, caps);
350 gst_object_unref (self);
356 gst_shape_wipe_video_sink_getcaps (GstPad * pad)
358 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
361 if (GST_PAD_CAPS (pad))
362 return gst_caps_copy (GST_PAD_CAPS (pad));
364 tmp = gst_pad_peer_get_caps (self->srcpad);
366 ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
367 gst_caps_unref (tmp);
369 ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
372 tmp = gst_pad_peer_get_caps (pad);
374 GstCaps *intersection;
376 intersection = gst_caps_intersect (tmp, ret);
377 gst_caps_unref (tmp);
378 gst_caps_unref (ret);
382 if (self->height && self->width) {
385 n = gst_caps_get_size (ret);
386 for (i = 0; i < n; i++) {
387 GstStructure *s = gst_caps_get_structure (ret, i);
389 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
390 G_TYPE_INT, self->height, NULL);
394 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
396 GstCaps *intersection, *tmp2;
399 tmp = gst_caps_make_writable (tmp);
401 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
403 intersection = gst_caps_intersect (tmp, tmp2);
404 gst_caps_unref (tmp);
405 gst_caps_unref (tmp2);
408 n = gst_caps_get_size (tmp);
410 tmp2 = gst_caps_new_empty ();
411 for (i = 0; i < n; i++) {
412 GstStructure *s = gst_caps_get_structure (tmp, i);
415 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
416 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
418 gst_structure_set_name (s, "video/x-raw-yuv");
419 c = gst_structure_copy (s);
420 gst_structure_set_name (c, "video/x-raw-rgb");
421 gst_caps_append_structure (tmp2, c);
423 gst_caps_append (tmp, tmp2);
425 intersection = gst_caps_intersect (tmp, ret);
426 gst_caps_unref (tmp);
427 gst_caps_unref (ret);
431 gst_object_unref (self);
433 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
439 gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps)
441 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
444 gint width, height, bpp;
446 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
448 s = gst_caps_get_structure (caps, 0);
450 if (!gst_structure_get_int (s, "width", &width) ||
451 !gst_structure_get_int (s, "height", &height) ||
452 !gst_structure_get_int (s, "bpp", &bpp)) {
457 if ((self->width != width || self->height != height) &&
458 self->width > 0 && self->height > 0) {
459 GST_ERROR_OBJECT (pad, "Mask caps must have the same width/height "
460 "as the video caps");
465 self->height = height;
468 self->mask_bpp = bpp;
471 gst_object_unref (self);
477 gst_shape_wipe_mask_sink_getcaps (GstPad * pad)
479 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
483 if (GST_PAD_CAPS (pad))
484 return gst_caps_copy (GST_PAD_CAPS (pad));
486 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
489 gst_caps_intersect (tmp,
490 gst_pad_get_pad_template_caps (self->video_sinkpad));
491 gst_caps_unref (tmp);
493 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
496 tmp = gst_pad_peer_get_caps (self->srcpad);
498 GstCaps *intersection;
500 intersection = gst_caps_intersect (ret, tmp);
501 gst_caps_unref (ret);
502 gst_caps_unref (tmp);
506 n = gst_caps_get_size (ret);
507 tmp = gst_caps_new_empty ();
508 for (i = 0; i < n; i++) {
509 GstStructure *s = gst_caps_get_structure (ret, i);
512 gst_structure_set_name (s, "video/x-raw-gray");
513 gst_structure_remove_fields (s, "format", "framerate", "bpp", "depth",
514 "endianness", "framerate", "red_mask", "green_mask", "blue_mask",
517 if (self->width && self->height)
518 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
519 G_TYPE_INT, self->height, NULL);
521 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
523 t = gst_structure_copy (s);
525 gst_structure_set (s, "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16,
526 "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
527 gst_structure_set (t, "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);
529 gst_caps_append_structure (tmp, t);
531 gst_caps_append (ret, tmp);
533 tmp = gst_pad_peer_get_caps (pad);
535 GstCaps *intersection;
537 intersection = gst_caps_intersect (tmp, ret);
538 gst_caps_unref (tmp);
539 gst_caps_unref (ret);
543 gst_object_unref (self);
545 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
551 gst_shape_wipe_src_getcaps (GstPad * pad)
553 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
556 if (GST_PAD_CAPS (pad))
557 return gst_caps_copy (GST_PAD_CAPS (pad));
558 else if (GST_PAD_CAPS (self->video_sinkpad))
559 return gst_caps_copy (GST_PAD_CAPS (self->video_sinkpad));
561 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
564 gst_caps_intersect (tmp,
565 gst_pad_get_pad_template_caps (self->video_sinkpad));
566 gst_caps_unref (tmp);
568 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
571 tmp = gst_pad_peer_get_caps (pad);
573 GstCaps *intersection;
575 intersection = gst_caps_intersect (tmp, ret);
576 gst_caps_unref (tmp);
577 gst_caps_unref (ret);
581 if (self->height && self->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->width, "height",
589 G_TYPE_INT, self->height, NULL);
593 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
595 GstCaps *intersection, *tmp2;
598 tmp = gst_caps_make_writable (tmp);
599 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
601 intersection = gst_caps_intersect (tmp, tmp2);
602 gst_caps_unref (tmp);
603 gst_caps_unref (tmp2);
606 n = gst_caps_get_size (tmp);
608 tmp2 = gst_caps_new_empty ();
609 for (i = 0; i < n; i++) {
610 GstStructure *s = gst_caps_get_structure (tmp, i);
613 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
614 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
616 gst_structure_set_name (s, "video/x-raw-yuv");
617 c = gst_structure_copy (s);
619 gst_caps_append_structure (tmp2, c);
621 gst_caps_append (tmp, tmp2);
623 intersection = gst_caps_intersect (tmp, ret);
624 gst_caps_unref (tmp);
625 gst_caps_unref (ret);
629 gst_object_unref (self);
631 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
637 gst_shape_wipe_video_sink_query (GstPad * pad, GstQuery * query)
639 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
641 GstPad *peer = gst_pad_get_peer (self->srcpad);
643 GST_DEBUG_OBJECT (pad, "Handling query of type '%s'",
644 gst_query_type_get_name (GST_QUERY_TYPE (query)));
647 GST_INFO_OBJECT (pad, "No peer yet");
650 ret = gst_pad_query (peer, query);
651 gst_object_unref (peer);
654 gst_object_unref (self);
659 gst_shape_wipe_src_query (GstPad * pad, GstQuery * query)
661 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
663 GstPad *peer = gst_pad_get_peer (self->video_sinkpad);
665 GST_DEBUG_OBJECT (pad, "Handling query of type '%s'",
666 gst_query_type_get_name (GST_QUERY_TYPE (query)));
669 GST_INFO_OBJECT (pad, "No peer yet");
672 ret = gst_pad_query (peer, query);
673 gst_object_unref (peer);
676 gst_object_unref (self);
681 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
682 GstClockTimeDiff diff, GstClockTime timestamp)
684 GST_OBJECT_LOCK (self);
685 self->proportion = proportion;
686 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
687 if (G_UNLIKELY (diff > 0))
688 self->earliest_time = timestamp + 2 * diff + self->frame_duration;
690 self->earliest_time = timestamp + diff;
692 self->earliest_time = GST_CLOCK_TIME_NONE;
694 GST_OBJECT_UNLOCK (self);
698 gst_shape_wipe_reset_qos (GstShapeWipe * self)
700 gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
704 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
707 GST_OBJECT_LOCK (self);
708 *proportion = self->proportion;
709 *time = self->earliest_time;
710 GST_OBJECT_UNLOCK (self);
713 /* Perform qos calculations before processing the next frame. Returns TRUE if
714 * the frame should be processed, FALSE if the frame can be dropped entirely */
716 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
718 GstClockTime qostime, earliest_time;
721 /* no timestamp, can't do QoS => process frame */
722 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
723 GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
727 /* get latest QoS observation values */
728 gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
730 /* skip qos if we have no observation (yet) => process frame */
731 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
732 GST_LOG_OBJECT (self, "no observation yet, process frame");
736 /* qos is done on running time */
737 qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
740 /* see how our next timestamp relates to the latest qos timestamp */
741 GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
742 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
744 if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
745 GST_DEBUG_OBJECT (self, "we are late, drop frame");
749 GST_LOG_OBJECT (self, "process frame");
753 #define CREATE_AYUV_FUNCTIONS(depth, shift) \
755 gst_shape_wipe_blend_ayuv_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
756 GstBuffer * maskbuf, GstBuffer * outbuf) \
758 const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
759 const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
760 guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
762 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
763 GST_ROUND_UP_4 (self->width)) - self->width; \
764 gfloat position = self->mask_position; \
765 gfloat low = position - (self->mask_border / 2.0f); \
766 gfloat high = position + (self->mask_border / 2.0f); \
767 guint32 low_i, high_i, position_i, round_i; \
768 gint width = self->width, height = self->height; \
780 position_i = position * 65536; \
781 low_i = low * 65536; \
782 high_i = high * 65536; \
783 round_i = (high_i - low_i) >> 1; \
785 for (i = 0; i < height; i++) { \
786 for (j = 0; j < width; j++) { \
787 guint32 in = *mask << shift; \
790 output[0] = 0x00; /* A */ \
791 output[1] = 0x00; /* Y */ \
792 output[2] = 0x80; /* U */ \
793 output[3] = 0x80; /* V */ \
794 } else if (in >= high_i) { \
795 output[0] = 0xff; /* A */ \
796 output[1] = input[1]; /* Y */ \
797 output[2] = input[2]; /* U */ \
798 output[3] = input[3]; /* V */ \
801 /* Note: This will never overflow or be larger than 255! */ \
802 val = ((((in - low_i) << 16) + round_i) / (high_i - low_i)) >> 8; \
804 output[0] = val; /* A */ \
805 output[1] = input[1]; /* Y */ \
806 output[2] = input[2]; /* U */ \
807 output[3] = input[3]; /* V */ \
814 mask += mask_increment; \
818 CREATE_AYUV_FUNCTIONS (16, 0);
819 CREATE_AYUV_FUNCTIONS (8, 8);
821 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
823 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
824 GstBuffer * maskbuf, GstBuffer * outbuf) \
826 const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
827 const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
828 guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
830 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
831 GST_ROUND_UP_4 (self->width)) - self->width; \
832 gfloat position = self->mask_position; \
833 gfloat low = position - (self->mask_border / 2.0f); \
834 gfloat high = position + (self->mask_border / 2.0f); \
835 guint32 low_i, high_i, position_i, round_i; \
836 gint width = self->width, height = self->height; \
848 position_i = position * 65536; \
849 low_i = low * 65536; \
850 high_i = high * 65536; \
851 round_i = (high_i - low_i) >> 1; \
853 for (i = 0; i < height; i++) { \
854 for (j = 0; j < width; j++) { \
855 guint32 in = *mask << shift; \
858 output[a] = 0x00; /* A */ \
859 output[r] = 0x00; /* R */ \
860 output[g] = 0x00; /* G */ \
861 output[b] = 0x00; /* B */ \
862 } else if (in >= high) { \
863 output[a] = 0xff; /* A */ \
864 output[r] = input[r]; /* R */ \
865 output[g] = input[g]; /* G */ \
866 output[b] = input[b]; /* B */ \
869 /* Note: This will never overflow or be larger than 255! */ \
870 val = ((((in - low_i) << 16) + round_i) / (high_i - low_i)) >> 8; \
872 output[a] = val; /* A */ \
873 output[r] = input[r]; /* R */ \
874 output[g] = input[g]; /* G */ \
875 output[b] = input[b]; /* B */ \
882 mask += mask_increment; \
886 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
887 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
889 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
890 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
893 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
895 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
896 GstFlowReturn ret = GST_FLOW_OK;
897 GstBuffer *mask = NULL, *outbuf = NULL;
898 GstClockTime timestamp;
899 gboolean new_outbuf = FALSE;
901 if (G_UNLIKELY (self->fmt == GST_VIDEO_FORMAT_UNKNOWN))
904 timestamp = GST_BUFFER_TIMESTAMP (buffer);
906 gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
908 if (GST_CLOCK_TIME_IS_VALID (timestamp))
909 gst_object_sync_values (G_OBJECT (self), timestamp);
911 GST_DEBUG_OBJECT (self,
912 "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %lf",
913 GST_TIME_ARGS (timestamp), self->mask_position);
915 g_mutex_lock (self->mask_mutex);
920 g_cond_wait (self->mask_cond, self->mask_mutex);
922 if (self->mask == NULL || self->shutdown) {
925 mask = gst_buffer_ref (self->mask);
927 g_mutex_unlock (self->mask_mutex);
929 if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
932 /* Try to blend inplace, if it's not possible
933 * get a new buffer from downstream. */
934 if (!gst_buffer_is_writable (buffer)) {
936 gst_pad_alloc_buffer_and_set_caps (self->srcpad, GST_BUFFER_OFFSET_NONE,
937 GST_BUFFER_SIZE (buffer), GST_PAD_CAPS (self->srcpad), &outbuf);
938 if (G_UNLIKELY (ret != GST_FLOW_OK))
941 gst_buffer_copy_metadata (outbuf, buffer, GST_BUFFER_COPY_ALL);
947 if (self->fmt == GST_VIDEO_FORMAT_AYUV && self->mask_bpp == 16)
948 gst_shape_wipe_blend_ayuv_16 (self, buffer, mask, outbuf);
949 else if (self->fmt == GST_VIDEO_FORMAT_AYUV)
950 gst_shape_wipe_blend_ayuv_8 (self, buffer, mask, outbuf);
951 else if (self->fmt == GST_VIDEO_FORMAT_ARGB && self->mask_bpp == 16)
952 gst_shape_wipe_blend_argb_16 (self, buffer, mask, outbuf);
953 else if (self->fmt == GST_VIDEO_FORMAT_ARGB)
954 gst_shape_wipe_blend_argb_8 (self, buffer, mask, outbuf);
955 else if (self->fmt == GST_VIDEO_FORMAT_BGRA && self->mask_bpp == 16)
956 gst_shape_wipe_blend_bgra_16 (self, buffer, mask, outbuf);
957 else if (self->fmt == GST_VIDEO_FORMAT_BGRA)
958 gst_shape_wipe_blend_bgra_8 (self, buffer, mask, outbuf);
960 g_assert_not_reached ();
962 gst_buffer_unref (mask);
964 gst_buffer_unref (buffer);
966 ret = gst_pad_push (self->srcpad, outbuf);
967 if (G_UNLIKELY (ret != GST_FLOW_OK))
974 GST_ERROR_OBJECT (self, "No valid caps yet");
975 gst_buffer_unref (buffer);
976 return GST_FLOW_NOT_NEGOTIATED;
978 GST_DEBUG_OBJECT (self, "Shutting down");
979 gst_buffer_unref (buffer);
980 return GST_FLOW_WRONG_STATE;
982 GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
983 gst_buffer_unref (buffer);
984 gst_buffer_unref (mask);
987 GST_ERROR_OBJECT (self, "Buffer allocation from downstream failed: %s",
988 gst_flow_get_name (ret));
989 gst_buffer_unref (buffer);
990 gst_buffer_unref (mask);
993 GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
994 gst_flow_get_name (ret));
999 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
1001 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
1002 GstFlowReturn ret = GST_FLOW_OK;
1004 g_mutex_lock (self->mask_mutex);
1005 GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
1007 gst_buffer_replace (&self->mask, buffer);
1008 g_cond_signal (self->mask_cond);
1009 g_mutex_unlock (self->mask_mutex);
1011 gst_buffer_unref (buffer);
1016 static GstStateChangeReturn
1017 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
1019 GstShapeWipe *self = GST_SHAPE_WIPE (element);
1020 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1022 switch (transition) {
1023 case GST_STATE_CHANGE_READY_TO_PAUSED:
1024 self->shutdown = FALSE;
1026 case GST_STATE_CHANGE_PAUSED_TO_READY:
1027 /* Unblock video sink chain function */
1028 g_mutex_lock (self->mask_mutex);
1029 self->shutdown = TRUE;
1030 g_cond_signal (self->mask_cond);
1031 g_mutex_unlock (self->mask_mutex);
1037 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1038 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1040 switch (transition) {
1041 case GST_STATE_CHANGE_PAUSED_TO_READY:
1042 gst_shape_wipe_reset (self);
1052 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
1054 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1057 GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1059 switch (GST_EVENT_TYPE (event)) {
1060 case GST_EVENT_NEWSEGMENT:{
1063 gint64 start, end, base;
1066 gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
1068 if (fmt == GST_FORMAT_TIME) {
1069 GST_DEBUG_OBJECT (pad,
1070 "Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
1071 GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
1072 GST_TIME_ARGS (end));
1073 gst_segment_set_newsegment (&self->segment, is_update, rate, fmt, start,
1076 gst_segment_init (&self->segment, GST_FORMAT_TIME);
1080 case GST_EVENT_FLUSH_STOP:
1081 gst_shape_wipe_reset_qos (self);
1084 ret = gst_pad_push_event (self->srcpad, event);
1088 gst_object_unref (self);
1093 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1095 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1097 GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1099 switch (GST_EVENT_TYPE (event)) {
1100 case GST_EVENT_FLUSH_STOP:
1101 g_mutex_lock (self->mask_mutex);
1102 gst_buffer_replace (&self->mask, NULL);
1103 g_mutex_unlock (self->mask_mutex);
1109 /* Dropping all events here */
1110 gst_event_unref (event);
1112 gst_object_unref (self);
1117 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1119 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1122 switch (GST_EVENT_TYPE (event)) {
1123 case GST_EVENT_QOS:{
1124 GstClockTimeDiff diff;
1125 GstClockTime timestamp;
1128 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1130 gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1134 ret = gst_pad_push_event (self->video_sinkpad, event);
1138 gst_object_unref (self);
1143 plugin_init (GstPlugin * plugin)
1145 GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1146 "shapewipe element");
1148 gst_controller_init (NULL, NULL);
1150 if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1151 GST_TYPE_SHAPE_WIPE))
1157 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1160 "Shape Wipe transition filter",
1161 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)