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 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 tmp = gst_pad_peer_get_caps (pad);
390 GstCaps *intersection;
392 intersection = gst_caps_intersect (tmp, ret);
393 gst_caps_unref (tmp);
394 gst_caps_unref (ret);
398 if (self->height && self->width) {
401 n = gst_caps_get_size (ret);
402 for (i = 0; i < n; i++) {
403 GstStructure *s = gst_caps_get_structure (ret, i);
405 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
406 G_TYPE_INT, self->height, NULL);
410 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
412 GstCaps *intersection, *tmp2;
415 tmp = gst_caps_make_writable (tmp);
417 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
419 intersection = gst_caps_intersect (tmp, tmp2);
420 gst_caps_unref (tmp);
421 gst_caps_unref (tmp2);
424 n = gst_caps_get_size (tmp);
426 tmp2 = gst_caps_new_empty ();
427 for (i = 0; i < n; i++) {
428 GstStructure *s = gst_caps_get_structure (tmp, i);
431 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
432 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
434 gst_structure_set_name (s, "video/x-raw-yuv");
435 c = gst_structure_copy (s);
436 gst_structure_set_name (c, "video/x-raw-rgb");
437 gst_caps_append_structure (tmp2, c);
439 gst_caps_append (tmp, tmp2);
441 intersection = gst_caps_intersect (tmp, ret);
442 gst_caps_unref (tmp);
443 gst_caps_unref (ret);
447 gst_object_unref (self);
449 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
455 gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps)
457 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
460 gint width, height, bpp;
462 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
464 s = gst_caps_get_structure (caps, 0);
466 if (!gst_structure_get_int (s, "width", &width) ||
467 !gst_structure_get_int (s, "height", &height) ||
468 !gst_structure_get_int (s, "bpp", &bpp)) {
473 if ((self->width != width || self->height != height) &&
474 self->width > 0 && self->height > 0) {
475 GST_ERROR_OBJECT (pad, "Mask caps must have the same width/height "
476 "as the video caps");
481 self->height = height;
484 self->mask_bpp = bpp;
487 gst_object_unref (self);
493 gst_shape_wipe_mask_sink_getcaps (GstPad * pad)
495 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
499 if (GST_PAD_CAPS (pad))
500 return gst_caps_copy (GST_PAD_CAPS (pad));
502 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
505 gst_caps_intersect (tmp,
506 gst_pad_get_pad_template_caps (self->video_sinkpad));
507 gst_caps_unref (tmp);
509 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
512 tmp = gst_pad_peer_get_caps (self->srcpad);
514 GstCaps *intersection;
516 intersection = gst_caps_intersect (ret, tmp);
517 gst_caps_unref (ret);
518 gst_caps_unref (tmp);
522 n = gst_caps_get_size (ret);
523 tmp = gst_caps_new_empty ();
524 for (i = 0; i < n; i++) {
525 GstStructure *s = gst_caps_get_structure (ret, i);
528 gst_structure_set_name (s, "video/x-raw-gray");
529 gst_structure_remove_fields (s, "format", "framerate", "bpp", "depth",
530 "endianness", "framerate", "red_mask", "green_mask", "blue_mask",
533 if (self->width && self->height)
534 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
535 G_TYPE_INT, self->height, NULL);
537 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
539 t = gst_structure_copy (s);
541 gst_structure_set (s, "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16,
542 "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
543 gst_structure_set (t, "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);
545 gst_caps_append_structure (tmp, t);
547 gst_caps_append (ret, tmp);
549 tmp = gst_pad_peer_get_caps (pad);
551 GstCaps *intersection;
553 intersection = gst_caps_intersect (tmp, ret);
554 gst_caps_unref (tmp);
555 gst_caps_unref (ret);
559 gst_object_unref (self);
561 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
567 gst_shape_wipe_src_getcaps (GstPad * pad)
569 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
572 if (GST_PAD_CAPS (pad))
573 return gst_caps_copy (GST_PAD_CAPS (pad));
574 else if (GST_PAD_CAPS (self->video_sinkpad))
575 return gst_caps_copy (GST_PAD_CAPS (self->video_sinkpad));
577 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
580 gst_caps_intersect (tmp,
581 gst_pad_get_pad_template_caps (self->video_sinkpad));
582 gst_caps_unref (tmp);
584 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
587 tmp = gst_pad_peer_get_caps (pad);
589 GstCaps *intersection;
591 intersection = gst_caps_intersect (tmp, ret);
592 gst_caps_unref (tmp);
593 gst_caps_unref (ret);
597 if (self->height && self->width) {
600 n = gst_caps_get_size (ret);
601 for (i = 0; i < n; i++) {
602 GstStructure *s = gst_caps_get_structure (ret, i);
604 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
605 G_TYPE_INT, self->height, NULL);
609 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
611 GstCaps *intersection, *tmp2;
614 tmp = gst_caps_make_writable (tmp);
615 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
617 intersection = gst_caps_intersect (tmp, tmp2);
618 gst_caps_unref (tmp);
619 gst_caps_unref (tmp2);
622 n = gst_caps_get_size (tmp);
624 tmp2 = gst_caps_new_empty ();
625 for (i = 0; i < n; i++) {
626 GstStructure *s = gst_caps_get_structure (tmp, i);
629 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
630 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
632 gst_structure_set_name (s, "video/x-raw-yuv");
633 c = gst_structure_copy (s);
635 gst_caps_append_structure (tmp2, c);
637 gst_caps_append (tmp, tmp2);
639 intersection = gst_caps_intersect (tmp, ret);
640 gst_caps_unref (tmp);
641 gst_caps_unref (ret);
645 gst_object_unref (self);
647 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
653 gst_shape_wipe_video_sink_query (GstPad * pad, GstQuery * query)
655 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
657 GstPad *peer = gst_pad_get_peer (self->srcpad);
659 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
660 gst_query_type_get_name (GST_QUERY_TYPE (query)));
663 GST_INFO_OBJECT (pad, "No peer yet, dropping query");
666 ret = gst_pad_query (peer, query);
667 gst_object_unref (peer);
670 gst_object_unref (self);
675 gst_shape_wipe_src_query (GstPad * pad, GstQuery * query)
677 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
679 GstPad *peer = gst_pad_get_peer (self->video_sinkpad);
681 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
682 gst_query_type_get_name (GST_QUERY_TYPE (query)));
685 GST_INFO_OBJECT (pad, "No peer yet, dropping query");
688 ret = gst_pad_query (peer, query);
689 gst_object_unref (peer);
692 gst_object_unref (self);
697 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
698 GstClockTimeDiff diff, GstClockTime timestamp)
700 GST_OBJECT_LOCK (self);
701 self->proportion = proportion;
702 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
703 if (G_UNLIKELY (diff > 0))
704 self->earliest_time = timestamp + 2 * diff + self->frame_duration;
706 self->earliest_time = timestamp + diff;
708 self->earliest_time = GST_CLOCK_TIME_NONE;
710 GST_OBJECT_UNLOCK (self);
714 gst_shape_wipe_reset_qos (GstShapeWipe * self)
716 gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
720 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
723 GST_OBJECT_LOCK (self);
724 *proportion = self->proportion;
725 *time = self->earliest_time;
726 GST_OBJECT_UNLOCK (self);
729 /* Perform qos calculations before processing the next frame. Returns TRUE if
730 * the frame should be processed, FALSE if the frame can be dropped entirely */
732 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
734 GstClockTime qostime, earliest_time;
737 /* no timestamp, can't do QoS => process frame */
738 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
739 GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
743 /* get latest QoS observation values */
744 gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
746 /* skip qos if we have no observation (yet) => process frame */
747 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
748 GST_LOG_OBJECT (self, "no observation yet, process frame");
752 /* qos is done on running time */
753 qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
756 /* see how our next timestamp relates to the latest qos timestamp */
757 GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
758 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
760 if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
761 GST_DEBUG_OBJECT (self, "we are late, drop frame");
765 GST_LOG_OBJECT (self, "process frame");
769 #define CREATE_AYUV_FUNCTIONS(depth, shift) \
771 gst_shape_wipe_blend_ayuv_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
772 GstBuffer * maskbuf, GstBuffer * outbuf) \
774 const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
775 const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
776 guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
778 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
779 GST_ROUND_UP_4 (self->width)) - self->width; \
780 gfloat position = self->mask_position; \
781 gfloat low = position - (self->mask_border / 2.0f); \
782 gfloat high = position + (self->mask_border / 2.0f); \
783 guint32 low_i, high_i, position_i, round_i; \
784 gint width = self->width, height = self->height; \
796 position_i = position * 65536; \
797 low_i = low * 65536; \
798 high_i = high * 65536; \
799 round_i = (high_i - low_i) >> 1; \
801 for (i = 0; i < height; i++) { \
802 for (j = 0; j < width; j++) { \
803 guint32 in = *mask << shift; \
806 output[0] = 0x00; /* A */ \
807 output[1] = 0x00; /* Y */ \
808 output[2] = 0x80; /* U */ \
809 output[3] = 0x80; /* V */ \
810 } else if (in >= high_i) { \
811 output[0] = input[0]; /* A */ \
812 output[1] = input[1]; /* Y */ \
813 output[2] = input[2]; /* U */ \
814 output[3] = input[3]; /* V */ \
817 /* Note: This will never overflow or be larger than 255! */ \
818 val = (((in - low_i) << 16) + round_i) / (high_i - low_i); \
819 val = (val * input[0] + 32768) >> 16; \
821 output[0] = val; /* A */ \
822 output[1] = input[1]; /* Y */ \
823 output[2] = input[2]; /* U */ \
824 output[3] = input[3]; /* V */ \
831 mask += mask_increment; \
835 CREATE_AYUV_FUNCTIONS (16, 0);
836 CREATE_AYUV_FUNCTIONS (8, 8);
838 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
840 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
841 GstBuffer * maskbuf, GstBuffer * outbuf) \
843 const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
844 const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
845 guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
847 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
848 GST_ROUND_UP_4 (self->width)) - self->width; \
849 gfloat position = self->mask_position; \
850 gfloat low = position - (self->mask_border / 2.0f); \
851 gfloat high = position + (self->mask_border / 2.0f); \
852 guint32 low_i, high_i, position_i, round_i; \
853 gint width = self->width, height = self->height; \
865 position_i = position * 65536; \
866 low_i = low * 65536; \
867 high_i = high * 65536; \
868 round_i = (high_i - low_i) >> 1; \
870 for (i = 0; i < height; i++) { \
871 for (j = 0; j < width; j++) { \
872 guint32 in = *mask << shift; \
875 output[a] = 0x00; /* A */ \
876 output[r] = 0x00; /* R */ \
877 output[g] = 0x00; /* G */ \
878 output[b] = 0x00; /* B */ \
879 } else if (in >= high_i) { \
880 output[a] = input[a]; /* A */ \
881 output[r] = input[r]; /* R */ \
882 output[g] = input[g]; /* G */ \
883 output[b] = input[b]; /* B */ \
886 /* Note: This will never overflow or be larger than 255! */ \
887 val = (((in - low_i) << 16) + round_i) / (high_i - low_i); \
888 val = (val * input[a] + 32768) >> 16; \
890 output[a] = val; /* A */ \
891 output[r] = input[r]; /* R */ \
892 output[g] = input[g]; /* G */ \
893 output[b] = input[b]; /* B */ \
900 mask += mask_increment; \
904 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
905 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
907 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
908 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
911 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
913 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
914 GstFlowReturn ret = GST_FLOW_OK;
915 GstBuffer *mask = NULL, *outbuf = NULL;
916 GstClockTime timestamp;
917 gboolean new_outbuf = FALSE;
919 if (G_UNLIKELY (self->fmt == GST_VIDEO_FORMAT_UNKNOWN))
922 timestamp = GST_BUFFER_TIMESTAMP (buffer);
924 gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
926 if (GST_CLOCK_TIME_IS_VALID (timestamp))
927 gst_object_sync_values (G_OBJECT (self), timestamp);
929 GST_LOG_OBJECT (self,
930 "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %f",
931 GST_TIME_ARGS (timestamp), self->mask_position);
933 g_mutex_lock (self->mask_mutex);
938 g_cond_wait (self->mask_cond, self->mask_mutex);
940 if (self->mask == NULL || self->shutdown) {
943 mask = gst_buffer_ref (self->mask);
945 g_mutex_unlock (self->mask_mutex);
947 if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
950 /* Try to blend inplace, if it's not possible
951 * get a new buffer from downstream. */
952 if (!gst_buffer_is_writable (buffer)) {
954 gst_pad_alloc_buffer_and_set_caps (self->srcpad, GST_BUFFER_OFFSET_NONE,
955 GST_BUFFER_SIZE (buffer), GST_PAD_CAPS (self->srcpad), &outbuf);
956 if (G_UNLIKELY (ret != GST_FLOW_OK))
959 gst_buffer_copy_metadata (outbuf, buffer, GST_BUFFER_COPY_ALL);
965 if (self->fmt == GST_VIDEO_FORMAT_AYUV && self->mask_bpp == 16)
966 gst_shape_wipe_blend_ayuv_16 (self, buffer, mask, outbuf);
967 else if (self->fmt == GST_VIDEO_FORMAT_AYUV)
968 gst_shape_wipe_blend_ayuv_8 (self, buffer, mask, outbuf);
969 else if (self->fmt == GST_VIDEO_FORMAT_ARGB && self->mask_bpp == 16)
970 gst_shape_wipe_blend_argb_16 (self, buffer, mask, outbuf);
971 else if (self->fmt == GST_VIDEO_FORMAT_ARGB)
972 gst_shape_wipe_blend_argb_8 (self, buffer, mask, outbuf);
973 else if (self->fmt == GST_VIDEO_FORMAT_BGRA && self->mask_bpp == 16)
974 gst_shape_wipe_blend_bgra_16 (self, buffer, mask, outbuf);
975 else if (self->fmt == GST_VIDEO_FORMAT_BGRA)
976 gst_shape_wipe_blend_bgra_8 (self, buffer, mask, outbuf);
978 g_assert_not_reached ();
980 gst_buffer_unref (mask);
982 gst_buffer_unref (buffer);
984 ret = gst_pad_push (self->srcpad, outbuf);
985 if (G_UNLIKELY (ret != GST_FLOW_OK))
992 GST_ERROR_OBJECT (self, "No valid caps yet");
993 gst_buffer_unref (buffer);
994 return GST_FLOW_NOT_NEGOTIATED;
996 GST_DEBUG_OBJECT (self, "Shutting down");
997 gst_buffer_unref (buffer);
998 return GST_FLOW_WRONG_STATE;
1000 GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
1001 gst_buffer_unref (buffer);
1002 gst_buffer_unref (mask);
1005 GST_ERROR_OBJECT (self, "Buffer allocation from downstream failed: %s",
1006 gst_flow_get_name (ret));
1007 gst_buffer_unref (buffer);
1008 gst_buffer_unref (mask);
1011 GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
1012 gst_flow_get_name (ret));
1016 static GstFlowReturn
1017 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
1019 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
1020 GstFlowReturn ret = GST_FLOW_OK;
1022 g_mutex_lock (self->mask_mutex);
1023 GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
1025 gst_buffer_replace (&self->mask, buffer);
1026 g_cond_signal (self->mask_cond);
1027 g_mutex_unlock (self->mask_mutex);
1029 gst_buffer_unref (buffer);
1034 static GstStateChangeReturn
1035 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
1037 GstShapeWipe *self = GST_SHAPE_WIPE (element);
1038 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1040 switch (transition) {
1041 case GST_STATE_CHANGE_READY_TO_PAUSED:
1042 self->shutdown = FALSE;
1044 case GST_STATE_CHANGE_PAUSED_TO_READY:
1045 /* Unblock video sink chain function */
1046 g_mutex_lock (self->mask_mutex);
1047 self->shutdown = TRUE;
1048 g_cond_signal (self->mask_cond);
1049 g_mutex_unlock (self->mask_mutex);
1055 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1056 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1058 switch (transition) {
1059 case GST_STATE_CHANGE_PAUSED_TO_READY:
1060 gst_shape_wipe_reset (self);
1070 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
1072 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1075 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1077 switch (GST_EVENT_TYPE (event)) {
1078 case GST_EVENT_NEWSEGMENT:{
1081 gint64 start, end, base;
1084 gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
1086 if (fmt == GST_FORMAT_TIME) {
1087 GST_DEBUG_OBJECT (pad,
1088 "Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
1089 GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
1090 GST_TIME_ARGS (end));
1091 gst_segment_set_newsegment (&self->segment, is_update, rate, fmt, start,
1094 gst_segment_init (&self->segment, GST_FORMAT_TIME);
1098 case GST_EVENT_FLUSH_STOP:
1099 gst_shape_wipe_reset_qos (self);
1102 ret = gst_pad_push_event (self->srcpad, event);
1106 gst_object_unref (self);
1111 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1113 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1115 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1117 switch (GST_EVENT_TYPE (event)) {
1118 case GST_EVENT_FLUSH_STOP:
1119 g_mutex_lock (self->mask_mutex);
1120 gst_buffer_replace (&self->mask, NULL);
1121 g_mutex_unlock (self->mask_mutex);
1127 /* Dropping all events here */
1128 gst_event_unref (event);
1130 gst_object_unref (self);
1135 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1137 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1140 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1142 switch (GST_EVENT_TYPE (event)) {
1143 case GST_EVENT_QOS:{
1144 GstClockTimeDiff diff;
1145 GstClockTime timestamp;
1148 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1150 gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1154 ret = gst_pad_push_event (self->video_sinkpad, event);
1158 gst_object_unref (self);
1163 plugin_init (GstPlugin * plugin)
1165 GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1166 "shapewipe element");
1168 gst_controller_init (NULL, NULL);
1170 if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1171 GST_TYPE_SHAPE_WIPE))
1177 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1180 "Shape Wipe transition filter",
1181 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)