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,format=(fourcc)AYUV,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,format=(fourcc)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 (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 ";" GST_VIDEO_CAPS_ABGR ";" GST_VIDEO_CAPS_RGBA));
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 ";" GST_VIDEO_CAPS_ABGR ";" GST_VIDEO_CAPS_RGBA));
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 gfloat f = g_value_get_float (value);
245 GST_LOG_OBJECT (self, "Setting mask position: %f", f);
246 self->mask_position = f;
250 gfloat f = g_value_get_float (value);
252 GST_LOG_OBJECT (self, "Setting mask border: %f", f);
253 self->mask_border = f;
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 gst_shape_wipe_finalize (GObject * object)
265 GstShapeWipe *self = GST_SHAPE_WIPE (object);
267 gst_shape_wipe_reset (self);
270 g_cond_free (self->mask_cond);
271 self->mask_cond = NULL;
273 if (self->mask_mutex)
274 g_mutex_free (self->mask_mutex);
275 self->mask_mutex = NULL;
277 G_OBJECT_CLASS (parent_class)->finalize (object);
281 gst_shape_wipe_reset (GstShapeWipe * self)
283 GST_DEBUG_OBJECT (self, "Resetting internal state");
286 gst_buffer_unref (self->mask);
289 g_mutex_lock (self->mask_mutex);
290 g_cond_signal (self->mask_cond);
291 g_mutex_unlock (self->mask_mutex);
293 self->fmt = GST_VIDEO_FORMAT_UNKNOWN;
294 self->width = self->height = 0;
297 gst_segment_init (&self->segment, GST_FORMAT_TIME);
299 gst_shape_wipe_reset_qos (self);
300 self->frame_duration = 0;
304 gst_shape_wipe_video_sink_bufferalloc (GstPad * pad, guint64 offset, guint size,
305 GstCaps * caps, GstBuffer ** buf)
307 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
308 GstFlowReturn ret = GST_FLOW_OK;
310 GST_LOG_OBJECT (pad, "Allocating buffer with offset 0x%" G_GINT64_MODIFIER
311 "x and size %u with caps: %" GST_PTR_FORMAT, offset, size, caps);
315 ret = gst_pad_alloc_buffer (self->srcpad, offset, size, caps, buf);
316 if (G_UNLIKELY (ret != GST_FLOW_OK))
317 GST_ERROR_OBJECT (pad, "Allocating buffer failed: %s",
318 gst_flow_get_name (ret));
320 gst_object_unref (self);
326 gst_shape_wipe_video_sink_setcaps (GstPad * pad, GstCaps * caps)
328 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
335 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
337 s = gst_caps_get_structure (caps, 0);
339 if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
340 !gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)) {
341 GST_ERROR_OBJECT (pad, "Invalid caps");
347 if (self->width != width || self->height != height) {
348 g_mutex_lock (self->mask_mutex);
350 self->height = height;
353 gst_buffer_unref (self->mask);
355 g_mutex_unlock (self->mask_mutex);
359 self->frame_duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
361 self->frame_duration = 0;
363 ret = gst_pad_set_caps (self->srcpad, caps);
366 gst_object_unref (self);
372 gst_shape_wipe_video_sink_getcaps (GstPad * pad)
374 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
377 if (GST_PAD_CAPS (pad))
378 return gst_caps_copy (GST_PAD_CAPS (pad));
380 tmp = gst_pad_peer_get_caps (self->srcpad);
382 ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
383 gst_caps_unref (tmp);
385 ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
388 GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
390 if (gst_caps_is_empty (ret))
393 tmp = gst_pad_peer_get_caps (pad);
395 GST_LOG_OBJECT (pad, "peerpad accepted caps: %" GST_PTR_FORMAT, tmp);
397 GstCaps *intersection;
399 intersection = gst_caps_intersect (tmp, ret);
400 gst_caps_unref (tmp);
401 gst_caps_unref (ret);
405 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, tmp);
407 if (gst_caps_is_empty (ret))
410 if (self->height && self->width) {
413 n = gst_caps_get_size (ret);
414 for (i = 0; i < n; i++) {
415 GstStructure *s = gst_caps_get_structure (ret, i);
417 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
418 G_TYPE_INT, self->height, NULL);
422 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
424 GST_LOG_OBJECT (pad, "mask accepted caps: %" GST_PTR_FORMAT, tmp);
426 GstCaps *intersection, *tmp2;
429 tmp = gst_caps_make_writable (tmp);
431 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
433 intersection = gst_caps_intersect (tmp, tmp2);
434 gst_caps_unref (tmp);
435 gst_caps_unref (tmp2);
438 n = gst_caps_get_size (tmp);
440 tmp2 = gst_caps_new_empty ();
441 for (i = 0; i < n; i++) {
442 GstStructure *s = gst_caps_get_structure (tmp, i);
445 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
446 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
448 gst_structure_set_name (s, "video/x-raw-yuv");
449 c = gst_structure_copy (s);
450 gst_structure_set_name (c, "video/x-raw-rgb");
451 gst_caps_append_structure (tmp2, c);
453 gst_caps_append (tmp, tmp2);
455 intersection = gst_caps_intersect (tmp, ret);
456 gst_caps_unref (tmp);
457 gst_caps_unref (ret);
462 gst_object_unref (self);
464 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
470 gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps)
472 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
475 gint width, height, bpp;
477 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
479 s = gst_caps_get_structure (caps, 0);
481 if (!gst_structure_get_int (s, "width", &width) ||
482 !gst_structure_get_int (s, "height", &height) ||
483 !gst_structure_get_int (s, "bpp", &bpp)) {
488 if ((self->width != width || self->height != height) &&
489 self->width > 0 && self->height > 0) {
490 GST_ERROR_OBJECT (pad, "Mask caps must have the same width/height "
491 "as the video caps");
496 self->height = height;
499 self->mask_bpp = bpp;
502 gst_object_unref (self);
508 gst_shape_wipe_mask_sink_getcaps (GstPad * pad)
510 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
514 if (GST_PAD_CAPS (pad))
515 return gst_caps_copy (GST_PAD_CAPS (pad));
517 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
520 gst_caps_intersect (tmp,
521 gst_pad_get_pad_template_caps (self->video_sinkpad));
522 gst_caps_unref (tmp);
524 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
527 GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
529 if (gst_caps_is_empty (ret))
532 tmp = gst_pad_peer_get_caps (self->srcpad);
533 GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
536 GstCaps *intersection;
538 intersection = gst_caps_intersect (ret, tmp);
539 gst_caps_unref (ret);
540 gst_caps_unref (tmp);
544 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
546 if (gst_caps_is_empty (ret))
549 n = gst_caps_get_size (ret);
550 tmp = gst_caps_new_empty ();
551 for (i = 0; i < n; i++) {
552 GstStructure *s = gst_caps_get_structure (ret, i);
555 gst_structure_set_name (s, "video/x-raw-gray");
556 gst_structure_remove_fields (s, "format", "framerate", "bpp", "depth",
557 "endianness", "framerate", "red_mask", "green_mask", "blue_mask",
560 if (self->width && self->height)
561 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
562 G_TYPE_INT, self->height, NULL);
564 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
566 t = gst_structure_copy (s);
568 gst_structure_set (s, "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16,
569 "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
570 gst_structure_set (t, "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);
572 gst_caps_append_structure (tmp, t);
574 gst_caps_append (ret, tmp);
576 tmp = gst_pad_peer_get_caps (pad);
577 GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, tmp);
580 GstCaps *intersection;
582 intersection = gst_caps_intersect (tmp, ret);
583 gst_caps_unref (tmp);
584 gst_caps_unref (ret);
589 gst_object_unref (self);
591 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
597 gst_shape_wipe_src_getcaps (GstPad * pad)
599 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
602 if (GST_PAD_CAPS (pad))
603 return gst_caps_copy (GST_PAD_CAPS (pad));
604 else if (GST_PAD_CAPS (self->video_sinkpad))
605 return gst_caps_copy (GST_PAD_CAPS (self->video_sinkpad));
607 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
610 gst_caps_intersect (tmp,
611 gst_pad_get_pad_template_caps (self->video_sinkpad));
612 gst_caps_unref (tmp);
614 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
617 GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
619 if (gst_caps_is_empty (ret))
622 tmp = gst_pad_peer_get_caps (pad);
623 GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, ret);
625 GstCaps *intersection;
627 intersection = gst_caps_intersect (tmp, ret);
628 gst_caps_unref (tmp);
629 gst_caps_unref (ret);
633 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
635 if (gst_caps_is_empty (ret))
638 if (self->height && self->width) {
641 n = gst_caps_get_size (ret);
642 for (i = 0; i < n; i++) {
643 GstStructure *s = gst_caps_get_structure (ret, i);
645 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
646 G_TYPE_INT, self->height, NULL);
650 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
651 GST_LOG_OBJECT (pad, "mask sink accepted caps: %" GST_PTR_FORMAT, ret);
653 GstCaps *intersection, *tmp2;
656 tmp = gst_caps_make_writable (tmp);
657 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
659 intersection = gst_caps_intersect (tmp, tmp2);
660 gst_caps_unref (tmp);
661 gst_caps_unref (tmp2);
664 n = gst_caps_get_size (tmp);
666 tmp2 = gst_caps_new_empty ();
667 for (i = 0; i < n; i++) {
668 GstStructure *s = gst_caps_get_structure (tmp, i);
671 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
672 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
674 gst_structure_set_name (s, "video/x-raw-yuv");
675 c = gst_structure_copy (s);
677 gst_caps_append_structure (tmp2, c);
679 gst_caps_append (tmp, tmp2);
681 intersection = gst_caps_intersect (tmp, ret);
682 gst_caps_unref (tmp);
683 gst_caps_unref (ret);
689 gst_object_unref (self);
691 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
697 gst_shape_wipe_video_sink_query (GstPad * pad, GstQuery * query)
699 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
701 GstPad *peer = gst_pad_get_peer (self->srcpad);
703 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
704 gst_query_type_get_name (GST_QUERY_TYPE (query)));
707 GST_INFO_OBJECT (pad, "No peer yet, dropping query");
710 ret = gst_pad_query (peer, query);
711 gst_object_unref (peer);
714 gst_object_unref (self);
719 gst_shape_wipe_src_query (GstPad * pad, GstQuery * query)
721 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
723 GstPad *peer = gst_pad_get_peer (self->video_sinkpad);
725 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
726 gst_query_type_get_name (GST_QUERY_TYPE (query)));
729 GST_INFO_OBJECT (pad, "No peer yet, dropping query");
732 ret = gst_pad_query (peer, query);
733 gst_object_unref (peer);
736 gst_object_unref (self);
741 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
742 GstClockTimeDiff diff, GstClockTime timestamp)
744 GST_OBJECT_LOCK (self);
745 self->proportion = proportion;
746 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
747 if (G_UNLIKELY (diff > 0))
748 self->earliest_time = timestamp + 2 * diff + self->frame_duration;
750 self->earliest_time = timestamp + diff;
752 self->earliest_time = GST_CLOCK_TIME_NONE;
754 GST_OBJECT_UNLOCK (self);
758 gst_shape_wipe_reset_qos (GstShapeWipe * self)
760 gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
764 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
767 GST_OBJECT_LOCK (self);
768 *proportion = self->proportion;
769 *time = self->earliest_time;
770 GST_OBJECT_UNLOCK (self);
773 /* Perform qos calculations before processing the next frame. Returns TRUE if
774 * the frame should be processed, FALSE if the frame can be dropped entirely */
776 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
778 GstClockTime qostime, earliest_time;
781 /* no timestamp, can't do QoS => process frame */
782 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
783 GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
787 /* get latest QoS observation values */
788 gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
790 /* skip qos if we have no observation (yet) => process frame */
791 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
792 GST_LOG_OBJECT (self, "no observation yet, process frame");
796 /* qos is done on running time */
797 qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
800 /* see how our next timestamp relates to the latest qos timestamp */
801 GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
802 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
804 if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
805 GST_DEBUG_OBJECT (self, "we are late, drop frame");
809 GST_LOG_OBJECT (self, "process frame");
813 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
815 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
816 GstBuffer * maskbuf, GstBuffer * outbuf) \
818 const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
819 const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
820 guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
822 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
823 GST_ROUND_UP_4 (self->width)) - self->width; \
824 gfloat position = self->mask_position; \
825 gfloat low = position - (self->mask_border / 2.0f); \
826 gfloat high = position + (self->mask_border / 2.0f); \
827 guint32 low_i, high_i, position_i, round_i; \
828 gint width = self->width, height = self->height; \
840 position_i = position * 65536; \
841 low_i = low * 65536; \
842 high_i = high * 65536; \
843 round_i = (high_i - low_i) >> 1; \
845 for (i = 0; i < height; i++) { \
846 for (j = 0; j < width; j++) { \
847 guint32 in = *mask << shift; \
850 output[a] = 0x00; /* A */ \
851 output[r] = input[r]; /* R */ \
852 output[g] = input[g]; /* G */ \
853 output[b] = input[b]; /* B */ \
854 } else if (in >= high_i) { \
855 output[a] = input[a]; /* A */ \
856 output[r] = input[r]; /* R */ \
857 output[g] = input[g]; /* G */ \
858 output[b] = input[b]; /* B */ \
861 /* Note: This will never overflow or be larger than 255! */ \
862 val = (((in - low_i) << 16) + round_i) / (high_i - low_i); \
863 val = (val * input[a] + 32768) >> 16; \
865 output[a] = val; /* A */ \
866 output[r] = input[r]; /* R */ \
867 output[g] = input[g]; /* G */ \
868 output[b] = input[b]; /* B */ \
875 mask += mask_increment; \
879 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
880 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
882 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
883 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
886 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
888 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
889 GstFlowReturn ret = GST_FLOW_OK;
890 GstBuffer *mask = NULL, *outbuf = NULL;
891 GstClockTime timestamp;
892 gboolean new_outbuf = FALSE;
894 if (G_UNLIKELY (self->fmt == GST_VIDEO_FORMAT_UNKNOWN))
897 timestamp = GST_BUFFER_TIMESTAMP (buffer);
899 gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
901 if (GST_CLOCK_TIME_IS_VALID (timestamp))
902 gst_object_sync_values (G_OBJECT (self), timestamp);
904 GST_LOG_OBJECT (self,
905 "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %f",
906 GST_TIME_ARGS (timestamp), self->mask_position);
908 g_mutex_lock (self->mask_mutex);
913 g_cond_wait (self->mask_cond, self->mask_mutex);
915 if (self->mask == NULL || self->shutdown) {
918 mask = gst_buffer_ref (self->mask);
920 g_mutex_unlock (self->mask_mutex);
922 if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
925 /* Try to blend inplace, if it's not possible
926 * get a new buffer from downstream. */
927 if (!gst_buffer_is_writable (buffer)) {
929 gst_pad_alloc_buffer_and_set_caps (self->srcpad, GST_BUFFER_OFFSET_NONE,
930 GST_BUFFER_SIZE (buffer), GST_PAD_CAPS (self->srcpad), &outbuf);
931 if (G_UNLIKELY (ret != GST_FLOW_OK))
934 gst_buffer_copy_metadata (outbuf, buffer, GST_BUFFER_COPY_ALL);
941 case GST_VIDEO_FORMAT_AYUV:
942 case GST_VIDEO_FORMAT_ARGB:
943 case GST_VIDEO_FORMAT_ABGR:
944 if (self->mask_bpp == 16)
945 gst_shape_wipe_blend_argb_16 (self, buffer, mask, outbuf);
947 gst_shape_wipe_blend_argb_8 (self, buffer, mask, outbuf);
949 case GST_VIDEO_FORMAT_BGRA:
950 case GST_VIDEO_FORMAT_RGBA:
951 if (self->mask_bpp == 16)
952 gst_shape_wipe_blend_bgra_16 (self, buffer, mask, outbuf);
954 gst_shape_wipe_blend_bgra_8 (self, buffer, mask, outbuf);
957 g_assert_not_reached ();
961 gst_buffer_unref (mask);
963 gst_buffer_unref (buffer);
965 ret = gst_pad_push (self->srcpad, outbuf);
966 if (G_UNLIKELY (ret != GST_FLOW_OK))
973 GST_ERROR_OBJECT (self, "No valid caps yet");
974 gst_buffer_unref (buffer);
975 return GST_FLOW_NOT_NEGOTIATED;
977 GST_DEBUG_OBJECT (self, "Shutting down");
978 gst_buffer_unref (buffer);
979 return GST_FLOW_WRONG_STATE;
981 GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
982 gst_buffer_unref (buffer);
983 gst_buffer_unref (mask);
986 GST_ERROR_OBJECT (self, "Buffer allocation from downstream failed: %s",
987 gst_flow_get_name (ret));
988 gst_buffer_unref (buffer);
989 gst_buffer_unref (mask);
992 GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
993 gst_flow_get_name (ret));
998 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
1000 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
1001 GstFlowReturn ret = GST_FLOW_OK;
1003 g_mutex_lock (self->mask_mutex);
1004 GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
1006 gst_buffer_replace (&self->mask, buffer);
1007 g_cond_signal (self->mask_cond);
1008 g_mutex_unlock (self->mask_mutex);
1010 gst_buffer_unref (buffer);
1015 static GstStateChangeReturn
1016 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
1018 GstShapeWipe *self = GST_SHAPE_WIPE (element);
1019 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1021 switch (transition) {
1022 case GST_STATE_CHANGE_READY_TO_PAUSED:
1023 self->shutdown = FALSE;
1025 case GST_STATE_CHANGE_PAUSED_TO_READY:
1026 /* Unblock video sink chain function */
1027 g_mutex_lock (self->mask_mutex);
1028 self->shutdown = TRUE;
1029 g_cond_signal (self->mask_cond);
1030 g_mutex_unlock (self->mask_mutex);
1036 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1037 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1039 switch (transition) {
1040 case GST_STATE_CHANGE_PAUSED_TO_READY:
1041 gst_shape_wipe_reset (self);
1051 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
1053 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1056 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1058 switch (GST_EVENT_TYPE (event)) {
1059 case GST_EVENT_NEWSEGMENT:{
1062 gint64 start, end, base;
1065 gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
1067 if (fmt == GST_FORMAT_TIME) {
1068 GST_DEBUG_OBJECT (pad,
1069 "Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
1070 GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
1071 GST_TIME_ARGS (end));
1072 gst_segment_set_newsegment (&self->segment, is_update, rate, fmt, start,
1075 gst_segment_init (&self->segment, GST_FORMAT_TIME);
1079 case GST_EVENT_FLUSH_STOP:
1080 gst_shape_wipe_reset_qos (self);
1083 ret = gst_pad_push_event (self->srcpad, event);
1087 gst_object_unref (self);
1092 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1094 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1096 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1098 switch (GST_EVENT_TYPE (event)) {
1099 case GST_EVENT_FLUSH_STOP:
1100 g_mutex_lock (self->mask_mutex);
1101 gst_buffer_replace (&self->mask, NULL);
1102 g_mutex_unlock (self->mask_mutex);
1108 /* Dropping all events here */
1109 gst_event_unref (event);
1111 gst_object_unref (self);
1116 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1118 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1121 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1123 switch (GST_EVENT_TYPE (event)) {
1124 case GST_EVENT_QOS:{
1125 GstClockTimeDiff diff;
1126 GstClockTime timestamp;
1129 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1131 gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1135 ret = gst_pad_push_event (self->video_sinkpad, event);
1139 gst_object_unref (self);
1144 plugin_init (GstPlugin * plugin)
1146 GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1147 "shapewipe element");
1149 gst_controller_init (NULL, NULL);
1151 if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1152 GST_TYPE_SHAPE_WIPE))
1158 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1161 "Shape Wipe transition filter",
1162 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)