2 * Copyright (C) 2009 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 static GstStaticPadTemplate video_sink_pad_template =
96 GST_STATIC_PAD_TEMPLATE ("video_sink",
99 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB));
101 static GstStaticPadTemplate mask_sink_pad_template =
102 GST_STATIC_PAD_TEMPLATE ("mask_sink",
105 GST_STATIC_CAPS ("video/x-raw-gray, "
108 "width = " GST_VIDEO_SIZE_RANGE ", "
109 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1 ; "
110 "video/x-raw-gray, " "bpp = 16, " "depth = 16, "
111 "endianness = BYTE_ORDER, " "width = " GST_VIDEO_SIZE_RANGE ", "
112 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1"));
114 static GstStaticPadTemplate src_pad_template =
115 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
116 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB));
118 GST_DEBUG_CATEGORY_STATIC (gst_shape_wipe_debug);
119 #define GST_CAT_DEFAULT gst_shape_wipe_debug
121 GST_BOILERPLATE (GstShapeWipe, gst_shape_wipe, GstElement, GST_TYPE_ELEMENT);
124 gst_shape_wipe_base_init (gpointer g_class)
126 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
128 gst_element_class_set_details_simple (gstelement_class,
129 "Shape Wipe transition filter",
130 "Filter/Editor/Video",
131 "Adds a shape wipe transition to a video stream",
132 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
134 gst_element_class_add_pad_template (gstelement_class,
135 gst_static_pad_template_get (&video_sink_pad_template));
136 gst_element_class_add_pad_template (gstelement_class,
137 gst_static_pad_template_get (&mask_sink_pad_template));
138 gst_element_class_add_pad_template (gstelement_class,
139 gst_static_pad_template_get (&src_pad_template));
143 gst_shape_wipe_class_init (GstShapeWipeClass * klass)
145 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
146 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
148 gobject_class->finalize = gst_shape_wipe_finalize;
149 gobject_class->set_property = gst_shape_wipe_set_property;
150 gobject_class->get_property = gst_shape_wipe_get_property;
152 g_object_class_install_property (gobject_class, PROP_POSITION,
153 g_param_spec_float ("position", "Position", "Position of the mask",
155 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
156 g_object_class_install_property (gobject_class, PROP_BORDER,
157 g_param_spec_float ("border", "Border", "Border of the mask",
159 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
161 gstelement_class->change_state =
162 GST_DEBUG_FUNCPTR (gst_shape_wipe_change_state);
166 gst_shape_wipe_init (GstShapeWipe * self, GstShapeWipeClass * g_class)
168 self->video_sinkpad =
169 gst_pad_new_from_static_template (&video_sink_pad_template, "video_sink");
170 gst_pad_set_chain_function (self->video_sinkpad,
171 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_chain));
172 gst_pad_set_event_function (self->video_sinkpad,
173 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_event));
174 gst_pad_set_setcaps_function (self->video_sinkpad,
175 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_setcaps));
176 gst_pad_set_getcaps_function (self->video_sinkpad,
177 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_getcaps));
178 gst_pad_set_bufferalloc_function (self->video_sinkpad,
179 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_bufferalloc));
180 gst_pad_set_query_function (self->video_sinkpad,
181 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_query));
182 gst_element_add_pad (GST_ELEMENT (self), self->video_sinkpad);
185 gst_pad_new_from_static_template (&mask_sink_pad_template, "mask_sink");
186 gst_pad_set_chain_function (self->mask_sinkpad,
187 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_chain));
188 gst_pad_set_event_function (self->mask_sinkpad,
189 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_event));
190 gst_pad_set_setcaps_function (self->mask_sinkpad,
191 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_setcaps));
192 gst_pad_set_getcaps_function (self->mask_sinkpad,
193 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_getcaps));
194 gst_element_add_pad (GST_ELEMENT (self), self->mask_sinkpad);
196 self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
197 gst_pad_set_event_function (self->srcpad,
198 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_event));
199 gst_pad_set_getcaps_function (self->srcpad,
200 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_getcaps));
201 gst_pad_set_query_function (self->srcpad,
202 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_query));
203 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
205 self->mask_mutex = g_mutex_new ();
206 self->mask_cond = g_cond_new ();
208 gst_shape_wipe_reset (self);
212 gst_shape_wipe_get_property (GObject * object, guint prop_id,
213 GValue * value, GParamSpec * pspec)
215 GstShapeWipe *self = GST_SHAPE_WIPE (object);
219 g_value_set_float (value, self->mask_position);
222 g_value_set_float (value, self->mask_border);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gst_shape_wipe_set_property (GObject * object, guint prop_id,
232 const GValue * value, GParamSpec * pspec)
234 GstShapeWipe *self = GST_SHAPE_WIPE (object);
238 self->mask_position = g_value_get_float (value);
241 self->mask_border = g_value_get_float (value);
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)
271 gst_buffer_unref (self->mask);
274 g_cond_signal (self->mask_cond);
276 self->fmt = GST_VIDEO_FORMAT_UNKNOWN;
277 self->width = self->height = 0;
278 self->mask_position = 0.0;
279 self->mask_border = 0.0;
282 gst_segment_init (&self->segment, GST_FORMAT_TIME);
284 gst_shape_wipe_reset_qos (self);
285 self->frame_duration = 0;
289 gst_shape_wipe_video_sink_bufferalloc (GstPad * pad, guint64 offset, guint size,
290 GstCaps * caps, GstBuffer ** buf)
292 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
293 GstFlowReturn ret = GST_FLOW_OK;
295 GST_DEBUG_OBJECT (pad, "Allocating buffer with offset 0x%" G_GINT64_MODIFIER
296 "x and size %u with caps: %" GST_PTR_FORMAT, offset, size, caps);
300 ret = gst_pad_alloc_buffer (self->srcpad, offset, size, caps, buf);
302 gst_object_unref (self);
308 gst_shape_wipe_video_sink_setcaps (GstPad * pad, GstCaps * caps)
310 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
317 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
319 s = gst_caps_get_structure (caps, 0);
321 if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
322 !gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)) {
328 if (self->width != width || self->height != height) {
329 g_mutex_lock (self->mask_mutex);
331 self->height = height;
334 gst_buffer_unref (self->mask);
336 g_mutex_unlock (self->mask_mutex);
340 self->frame_duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
342 self->frame_duration = 0;
344 ret = gst_pad_set_caps (self->srcpad, caps);
347 gst_object_unref (self);
353 gst_shape_wipe_video_sink_getcaps (GstPad * pad)
355 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
358 if (GST_PAD_CAPS (pad))
359 return gst_caps_copy (GST_PAD_CAPS (pad));
361 tmp = gst_pad_peer_get_caps (self->srcpad);
363 ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
364 gst_caps_unref (tmp);
366 ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
369 tmp = gst_pad_peer_get_caps (pad);
371 GstCaps *intersection;
373 intersection = gst_caps_intersect (tmp, ret);
374 gst_caps_unref (tmp);
375 gst_caps_unref (ret);
379 if (self->height && self->width) {
382 n = gst_caps_get_size (ret);
383 for (i = 0; i < n; i++) {
384 GstStructure *s = gst_caps_get_structure (ret, i);
386 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
387 G_TYPE_INT, self->height, NULL);
391 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
393 GstCaps *intersection, *tmp2;
396 tmp = gst_caps_make_writable (tmp);
398 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
400 intersection = gst_caps_intersect (tmp, tmp2);
401 gst_caps_unref (tmp);
402 gst_caps_unref (tmp2);
405 n = gst_caps_get_size (tmp);
407 tmp2 = gst_caps_new_empty ();
408 for (i = 0; i < n; i++) {
409 GstStructure *s = gst_caps_get_structure (tmp, i);
412 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
413 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
415 gst_structure_set_name (s, "video/x-raw-yuv");
416 c = gst_structure_copy (s);
417 gst_structure_set_name (c, "video/x-raw-rgb");
418 gst_caps_append_structure (tmp2, c);
420 gst_caps_append (tmp, tmp2);
422 intersection = gst_caps_intersect (tmp, ret);
423 gst_caps_unref (tmp);
424 gst_caps_unref (ret);
428 gst_object_unref (self);
430 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
436 gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps)
438 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
441 gint width, height, bpp;
443 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
445 s = gst_caps_get_structure (caps, 0);
447 if (!gst_structure_get_int (s, "width", &width) ||
448 !gst_structure_get_int (s, "height", &height) ||
449 !gst_structure_get_int (s, "bpp", &bpp)) {
454 if ((self->width != width || self->height != height) &&
455 self->width > 0 && self->height > 0) {
456 GST_ERROR_OBJECT (pad, "Mask caps must have the same width/height "
457 "as the video caps");
462 self->height = height;
465 self->mask_bpp = bpp;
468 gst_object_unref (self);
474 gst_shape_wipe_mask_sink_getcaps (GstPad * pad)
476 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
480 if (GST_PAD_CAPS (pad))
481 return gst_caps_copy (GST_PAD_CAPS (pad));
483 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
486 gst_caps_intersect (tmp,
487 gst_pad_get_pad_template_caps (self->video_sinkpad));
488 gst_caps_unref (tmp);
490 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
493 tmp = gst_pad_peer_get_caps (self->srcpad);
495 GstCaps *intersection;
497 intersection = gst_caps_intersect (ret, tmp);
498 gst_caps_unref (ret);
499 gst_caps_unref (tmp);
503 n = gst_caps_get_size (ret);
504 tmp = gst_caps_new_empty ();
505 for (i = 0; i < n; i++) {
506 GstStructure *s = gst_caps_get_structure (ret, i);
509 gst_structure_set_name (s, "video/x-raw-gray");
510 gst_structure_remove_fields (s, "format", "framerate", "bpp", "depth",
511 "endianness", "framerate", "red_mask", "green_mask", "blue_mask",
514 if (self->width && self->height)
515 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
516 G_TYPE_INT, self->height, NULL);
518 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
520 t = gst_structure_copy (s);
522 gst_structure_set (s, "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16,
523 "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
524 gst_structure_set (t, "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);
526 gst_caps_append_structure (tmp, t);
528 gst_caps_append (ret, tmp);
530 tmp = gst_pad_peer_get_caps (pad);
532 GstCaps *intersection;
534 intersection = gst_caps_intersect (tmp, ret);
535 gst_caps_unref (tmp);
536 gst_caps_unref (ret);
540 gst_object_unref (self);
542 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
548 gst_shape_wipe_src_getcaps (GstPad * pad)
550 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
553 if (GST_PAD_CAPS (pad))
554 return gst_caps_copy (GST_PAD_CAPS (pad));
555 else if (GST_PAD_CAPS (self->video_sinkpad))
556 return gst_caps_copy (GST_PAD_CAPS (self->video_sinkpad));
558 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
561 gst_caps_intersect (tmp,
562 gst_pad_get_pad_template_caps (self->video_sinkpad));
563 gst_caps_unref (tmp);
565 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
568 tmp = gst_pad_peer_get_caps (pad);
570 GstCaps *intersection;
572 intersection = gst_caps_intersect (tmp, ret);
573 gst_caps_unref (tmp);
574 gst_caps_unref (ret);
578 if (self->height && self->width) {
581 n = gst_caps_get_size (ret);
582 for (i = 0; i < n; i++) {
583 GstStructure *s = gst_caps_get_structure (ret, i);
585 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
586 G_TYPE_INT, self->height, NULL);
590 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
592 GstCaps *intersection, *tmp2;
595 tmp = gst_caps_make_writable (tmp);
596 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
598 intersection = gst_caps_intersect (tmp, tmp2);
599 gst_caps_unref (tmp);
600 gst_caps_unref (tmp2);
603 n = gst_caps_get_size (tmp);
605 tmp2 = gst_caps_new_empty ();
606 for (i = 0; i < n; i++) {
607 GstStructure *s = gst_caps_get_structure (tmp, i);
610 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
611 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
613 gst_structure_set_name (s, "video/x-raw-yuv");
614 c = gst_structure_copy (s);
616 gst_caps_append_structure (tmp2, c);
618 gst_caps_append (tmp, tmp2);
620 intersection = gst_caps_intersect (tmp, ret);
621 gst_caps_unref (tmp);
622 gst_caps_unref (ret);
626 gst_object_unref (self);
628 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
634 gst_shape_wipe_video_sink_query (GstPad * pad, GstQuery * query)
636 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
638 GstPad *peer = gst_pad_get_peer (self->srcpad);
640 GST_DEBUG_OBJECT (pad, "Handling query of type '%s'",
641 gst_query_type_get_name (GST_QUERY_TYPE (query)));
644 GST_INFO_OBJECT (pad, "No peer yet");
647 ret = gst_pad_query (peer, query);
648 gst_object_unref (peer);
651 gst_object_unref (self);
656 gst_shape_wipe_src_query (GstPad * pad, GstQuery * query)
658 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
660 GstPad *peer = gst_pad_get_peer (self->video_sinkpad);
662 GST_DEBUG_OBJECT (pad, "Handling query of type '%s'",
663 gst_query_type_get_name (GST_QUERY_TYPE (query)));
666 GST_INFO_OBJECT (pad, "No peer yet");
669 ret = gst_pad_query (peer, query);
670 gst_object_unref (peer);
673 gst_object_unref (self);
678 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
679 GstClockTimeDiff diff, GstClockTime timestamp)
681 GST_OBJECT_LOCK (self);
682 self->proportion = proportion;
683 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
684 if (G_UNLIKELY (diff > 0))
685 self->earliest_time = timestamp + 2 * diff + self->frame_duration;
687 self->earliest_time = timestamp + diff;
689 self->earliest_time = GST_CLOCK_TIME_NONE;
691 GST_OBJECT_UNLOCK (self);
695 gst_shape_wipe_reset_qos (GstShapeWipe * self)
697 gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
701 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
704 GST_OBJECT_LOCK (self);
705 *proportion = self->proportion;
706 *time = self->earliest_time;
707 GST_OBJECT_UNLOCK (self);
710 /* Perform qos calculations before processing the next frame. Returns TRUE if
711 * the frame should be processed, FALSE if the frame can be dropped entirely */
713 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
715 GstClockTime qostime, earliest_time;
718 /* no timestamp, can't do QoS => process frame */
719 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
720 GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
724 /* get latest QoS observation values */
725 gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
727 /* skip qos if we have no observation (yet) => process frame */
728 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
729 GST_LOG_OBJECT (self, "no observation yet, process frame");
733 /* qos is done on running time */
734 qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
737 /* see how our next timestamp relates to the latest qos timestamp */
738 GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
739 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
741 if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
742 GST_DEBUG_OBJECT (self, "we are late, drop frame");
746 GST_LOG_OBJECT (self, "process frame");
750 #define CREATE_AYUV_FUNCTIONS(depth, scale) \
751 static GstFlowReturn \
752 gst_shape_wipe_blend_ayuv_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
753 GstBuffer * maskbuf, GstBuffer * outbuf) \
755 const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
756 const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
757 guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
759 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
760 GST_ROUND_UP_4 (self->width)) - self->width; \
761 gfloat position = self->mask_position; \
762 gfloat low = position - (self->mask_border / 2.0f); \
763 gfloat high = position + (self->mask_border / 2.0f); \
775 for (i = 0; i < self->height; i++) { \
776 for (j = 0; j < self->width; j++) { \
777 gfloat in = *mask / scale; \
780 output[0] = 0x00; /* A */ \
781 output[1] = 0x00; /* Y */ \
782 output[2] = 0x80; /* U */ \
783 output[3] = 0x80; /* V */ \
784 } else if (in >= high) { \
785 output[0] = 0xff; /* A */ \
786 output[1] = input[1]; /* Y */ \
787 output[2] = input[2]; /* U */ \
788 output[3] = input[3]; /* V */ \
790 gfloat val = 255.0f * ((in - low) / (high - low)); \
792 output[0] = CLAMP (val, 0, 255); /* A */ \
793 output[1] = input[1]; /* Y */ \
794 output[2] = input[2]; /* U */ \
795 output[3] = input[3]; /* V */ \
802 mask += mask_increment; \
805 return GST_FLOW_OK; \
808 CREATE_AYUV_FUNCTIONS (16, 65536.0f);
809 CREATE_AYUV_FUNCTIONS (8, 256.0f);
811 #define CREATE_ARGB_FUNCTIONS(depth, scale) \
812 static GstFlowReturn \
813 gst_shape_wipe_blend_argb_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
814 GstBuffer * maskbuf, GstBuffer * outbuf) \
816 const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
817 const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
818 guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
820 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
821 GST_ROUND_UP_4 (self->width)) - self->width; \
822 gfloat position = self->mask_position; \
823 gfloat low = position - (self->mask_border / 2.0f); \
824 gfloat high = position + (self->mask_border / 2.0f); \
836 for (i = 0; i < self->height; i++) { \
837 for (j = 0; j < self->width; j++) { \
838 gfloat in = *mask / scale; \
841 output[0] = 0x00; /* A */ \
842 output[1] = 0x00; /* R */ \
843 output[2] = 0x00; /* G */ \
844 output[3] = 0x00; /* B */ \
845 } else if (in >= high) { \
846 output[0] = 0xff; /* A */ \
847 output[1] = input[1]; /* R */ \
848 output[2] = input[2]; /* G */ \
849 output[3] = input[3]; /* B */ \
851 gfloat val = 255.0f * ((in - low) / (high - low)); \
853 output[0] = CLAMP (val, 0, 255); /* A */ \
854 output[1] = input[1]; /* R */ \
855 output[2] = input[2]; /* G */ \
856 output[3] = input[3]; /* B */ \
863 mask += mask_increment; \
866 return GST_FLOW_OK; \
869 CREATE_ARGB_FUNCTIONS (16, 65536.0f);
870 CREATE_ARGB_FUNCTIONS (8, 256.0f);
873 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
875 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
876 GstFlowReturn ret = GST_FLOW_OK;
877 GstBuffer *mask = NULL, *outbuf = NULL;
878 GstClockTime timestamp;
879 gboolean new_outbuf = FALSE;
881 if (G_UNLIKELY (self->fmt == GST_VIDEO_FORMAT_UNKNOWN))
882 return GST_FLOW_NOT_NEGOTIATED;
884 timestamp = GST_BUFFER_TIMESTAMP (buffer);
886 gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
888 if (GST_CLOCK_TIME_IS_VALID (timestamp))
889 gst_object_sync_values (G_OBJECT (self), timestamp);
891 GST_DEBUG_OBJECT (self,
892 "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %lf",
893 GST_TIME_ARGS (timestamp), self->mask_position);
895 g_mutex_lock (self->mask_mutex);
897 g_cond_wait (self->mask_cond, self->mask_mutex);
899 if (self->mask == NULL) {
900 g_mutex_unlock (self->mask_mutex);
901 gst_buffer_unref (buffer);
902 return GST_FLOW_UNEXPECTED;
904 mask = gst_buffer_ref (self->mask);
906 g_mutex_unlock (self->mask_mutex);
908 if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer))) {
909 gst_buffer_unref (buffer);
910 gst_buffer_unref (mask);
914 /* Try to blend inplace, if it's not possible
915 * get a new buffer from downstream.
917 if (!gst_buffer_is_writable (buffer)) {
919 gst_pad_alloc_buffer_and_set_caps (self->srcpad, GST_BUFFER_OFFSET_NONE,
920 GST_BUFFER_SIZE (buffer), GST_PAD_CAPS (self->srcpad), &outbuf);
921 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
922 gst_buffer_unref (buffer);
923 gst_buffer_unref (mask);
926 gst_buffer_copy_metadata (outbuf, buffer, GST_BUFFER_COPY_ALL);
932 if (self->fmt == GST_VIDEO_FORMAT_AYUV && self->mask_bpp == 16)
933 ret = gst_shape_wipe_blend_ayuv_16 (self, buffer, mask, outbuf);
934 else if (self->fmt == GST_VIDEO_FORMAT_AYUV)
935 ret = gst_shape_wipe_blend_ayuv_8 (self, buffer, mask, outbuf);
936 else if (self->fmt == GST_VIDEO_FORMAT_ARGB && self->mask_bpp == 16)
937 ret = gst_shape_wipe_blend_argb_16 (self, buffer, mask, outbuf);
938 else if (self->fmt == GST_VIDEO_FORMAT_ARGB)
939 ret = gst_shape_wipe_blend_argb_8 (self, buffer, mask, outbuf);
941 g_assert_not_reached ();
943 gst_buffer_unref (mask);
945 gst_buffer_unref (buffer);
947 if (ret != GST_FLOW_OK) {
948 gst_buffer_unref (outbuf);
952 ret = gst_pad_push (self->srcpad, outbuf);
957 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
959 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
960 GstFlowReturn ret = GST_FLOW_OK;
962 g_mutex_lock (self->mask_mutex);
963 GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
965 gst_buffer_replace (&self->mask, buffer);
966 g_cond_signal (self->mask_cond);
967 g_mutex_unlock (self->mask_mutex);
969 gst_buffer_unref (buffer);
974 static GstStateChangeReturn
975 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
977 GstShapeWipe *self = GST_SHAPE_WIPE (element);
978 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
980 switch (transition) {
981 case GST_STATE_CHANGE_READY_TO_PAUSED:
986 /* Unblock video sink chain function */
987 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
988 g_cond_signal (self->mask_cond);
990 if (GST_ELEMENT_CLASS (parent_class)->change_state)
991 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
993 switch (transition) {
994 case GST_STATE_CHANGE_PAUSED_TO_READY:
995 gst_shape_wipe_reset (self);
1005 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
1007 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1010 GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1012 switch (GST_EVENT_TYPE (event)) {
1013 case GST_EVENT_NEWSEGMENT:{
1016 gint64 start, end, base;
1019 gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
1021 if (fmt == GST_FORMAT_TIME) {
1022 GST_DEBUG_OBJECT (pad,
1023 "Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
1024 GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
1025 GST_TIME_ARGS (end));
1026 gst_segment_set_newsegment (&self->segment, is_update, rate, fmt, start,
1029 gst_segment_init (&self->segment, GST_FORMAT_TIME);
1033 case GST_EVENT_FLUSH_STOP:
1034 gst_shape_wipe_reset_qos (self);
1037 ret = gst_pad_push_event (self->srcpad, event);
1041 gst_object_unref (self);
1046 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1048 GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1050 /* Dropping all events here */
1051 gst_event_unref (event);
1056 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1058 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1061 switch (GST_EVENT_TYPE (event)) {
1062 case GST_EVENT_QOS:{
1063 GstClockTimeDiff diff;
1064 GstClockTime timestamp;
1067 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1069 gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1073 ret = gst_pad_push_event (self->video_sinkpad, event);
1077 gst_object_unref (self);
1082 plugin_init (GstPlugin * plugin)
1084 GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1085 "shapewipe element");
1087 gst_controller_init (NULL, NULL);
1089 if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1090 GST_TYPE_SHAPE_WIPE))
1096 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1099 "Shape Wipe transition filter",
1100 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)