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 " ; "
100 GST_VIDEO_CAPS_BGRA));
102 static GstStaticPadTemplate mask_sink_pad_template =
103 GST_STATIC_PAD_TEMPLATE ("mask_sink",
106 GST_STATIC_CAPS ("video/x-raw-gray, "
109 "width = " GST_VIDEO_SIZE_RANGE ", "
110 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1 ; "
111 "video/x-raw-gray, " "bpp = 16, " "depth = 16, "
112 "endianness = BYTE_ORDER, " "width = " GST_VIDEO_SIZE_RANGE ", "
113 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1"));
115 static GstStaticPadTemplate src_pad_template =
116 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
117 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB " ; "
118 GST_VIDEO_CAPS_BGRA));
120 GST_DEBUG_CATEGORY_STATIC (gst_shape_wipe_debug);
121 #define GST_CAT_DEFAULT gst_shape_wipe_debug
123 GST_BOILERPLATE (GstShapeWipe, gst_shape_wipe, GstElement, GST_TYPE_ELEMENT);
126 gst_shape_wipe_base_init (gpointer g_class)
128 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
130 gst_element_class_set_details_simple (gstelement_class,
131 "Shape Wipe transition filter",
132 "Filter/Editor/Video",
133 "Adds a shape wipe transition to a video stream",
134 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
136 gst_element_class_add_pad_template (gstelement_class,
137 gst_static_pad_template_get (&video_sink_pad_template));
138 gst_element_class_add_pad_template (gstelement_class,
139 gst_static_pad_template_get (&mask_sink_pad_template));
140 gst_element_class_add_pad_template (gstelement_class,
141 gst_static_pad_template_get (&src_pad_template));
145 gst_shape_wipe_class_init (GstShapeWipeClass * klass)
147 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
148 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
150 gobject_class->finalize = gst_shape_wipe_finalize;
151 gobject_class->set_property = gst_shape_wipe_set_property;
152 gobject_class->get_property = gst_shape_wipe_get_property;
154 g_object_class_install_property (gobject_class, PROP_POSITION,
155 g_param_spec_float ("position", "Position", "Position of the mask",
157 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
158 g_object_class_install_property (gobject_class, PROP_BORDER,
159 g_param_spec_float ("border", "Border", "Border of the mask",
161 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
163 gstelement_class->change_state =
164 GST_DEBUG_FUNCPTR (gst_shape_wipe_change_state);
168 gst_shape_wipe_init (GstShapeWipe * self, GstShapeWipeClass * g_class)
170 self->video_sinkpad =
171 gst_pad_new_from_static_template (&video_sink_pad_template, "video_sink");
172 gst_pad_set_chain_function (self->video_sinkpad,
173 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_chain));
174 gst_pad_set_event_function (self->video_sinkpad,
175 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_event));
176 gst_pad_set_setcaps_function (self->video_sinkpad,
177 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_setcaps));
178 gst_pad_set_getcaps_function (self->video_sinkpad,
179 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_getcaps));
180 gst_pad_set_bufferalloc_function (self->video_sinkpad,
181 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_bufferalloc));
182 gst_pad_set_query_function (self->video_sinkpad,
183 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_query));
184 gst_element_add_pad (GST_ELEMENT (self), self->video_sinkpad);
187 gst_pad_new_from_static_template (&mask_sink_pad_template, "mask_sink");
188 gst_pad_set_chain_function (self->mask_sinkpad,
189 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_chain));
190 gst_pad_set_event_function (self->mask_sinkpad,
191 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_event));
192 gst_pad_set_setcaps_function (self->mask_sinkpad,
193 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_setcaps));
194 gst_pad_set_getcaps_function (self->mask_sinkpad,
195 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_getcaps));
196 gst_element_add_pad (GST_ELEMENT (self), self->mask_sinkpad);
198 self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
199 gst_pad_set_event_function (self->srcpad,
200 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_event));
201 gst_pad_set_getcaps_function (self->srcpad,
202 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_getcaps));
203 gst_pad_set_query_function (self->srcpad,
204 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_query));
205 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
207 self->mask_mutex = g_mutex_new ();
208 self->mask_cond = g_cond_new ();
210 gst_shape_wipe_reset (self);
214 gst_shape_wipe_get_property (GObject * object, guint prop_id,
215 GValue * value, GParamSpec * pspec)
217 GstShapeWipe *self = GST_SHAPE_WIPE (object);
221 g_value_set_float (value, self->mask_position);
224 g_value_set_float (value, self->mask_border);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 gst_shape_wipe_set_property (GObject * object, guint prop_id,
234 const GValue * value, GParamSpec * pspec)
236 GstShapeWipe *self = GST_SHAPE_WIPE (object);
240 self->mask_position = g_value_get_float (value);
243 self->mask_border = g_value_get_float (value);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 gst_shape_wipe_finalize (GObject * object)
254 GstShapeWipe *self = GST_SHAPE_WIPE (object);
256 gst_shape_wipe_reset (self);
259 g_cond_free (self->mask_cond);
260 self->mask_cond = NULL;
262 if (self->mask_mutex)
263 g_mutex_free (self->mask_mutex);
264 self->mask_mutex = NULL;
266 G_OBJECT_CLASS (parent_class)->finalize (object);
270 gst_shape_wipe_reset (GstShapeWipe * self)
273 gst_buffer_unref (self->mask);
276 g_cond_signal (self->mask_cond);
278 self->fmt = GST_VIDEO_FORMAT_UNKNOWN;
279 self->width = self->height = 0;
280 self->mask_position = 0.0;
281 self->mask_border = 0.0;
284 gst_segment_init (&self->segment, GST_FORMAT_TIME);
286 gst_shape_wipe_reset_qos (self);
287 self->frame_duration = 0;
291 gst_shape_wipe_video_sink_bufferalloc (GstPad * pad, guint64 offset, guint size,
292 GstCaps * caps, GstBuffer ** buf)
294 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
295 GstFlowReturn ret = GST_FLOW_OK;
297 GST_DEBUG_OBJECT (pad, "Allocating buffer with offset 0x%" G_GINT64_MODIFIER
298 "x and size %u with caps: %" GST_PTR_FORMAT, offset, size, caps);
302 ret = gst_pad_alloc_buffer (self->srcpad, offset, size, caps, buf);
304 gst_object_unref (self);
310 gst_shape_wipe_video_sink_setcaps (GstPad * pad, GstCaps * caps)
312 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
319 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
321 s = gst_caps_get_structure (caps, 0);
323 if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
324 !gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)) {
330 if (self->width != width || self->height != height) {
331 g_mutex_lock (self->mask_mutex);
333 self->height = height;
336 gst_buffer_unref (self->mask);
338 g_mutex_unlock (self->mask_mutex);
342 self->frame_duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
344 self->frame_duration = 0;
346 ret = gst_pad_set_caps (self->srcpad, caps);
349 gst_object_unref (self);
355 gst_shape_wipe_video_sink_getcaps (GstPad * pad)
357 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
360 if (GST_PAD_CAPS (pad))
361 return gst_caps_copy (GST_PAD_CAPS (pad));
363 tmp = gst_pad_peer_get_caps (self->srcpad);
365 ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
366 gst_caps_unref (tmp);
368 ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
371 tmp = gst_pad_peer_get_caps (pad);
373 GstCaps *intersection;
375 intersection = gst_caps_intersect (tmp, ret);
376 gst_caps_unref (tmp);
377 gst_caps_unref (ret);
381 if (self->height && self->width) {
384 n = gst_caps_get_size (ret);
385 for (i = 0; i < n; i++) {
386 GstStructure *s = gst_caps_get_structure (ret, i);
388 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
389 G_TYPE_INT, self->height, NULL);
393 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
395 GstCaps *intersection, *tmp2;
398 tmp = gst_caps_make_writable (tmp);
400 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
402 intersection = gst_caps_intersect (tmp, tmp2);
403 gst_caps_unref (tmp);
404 gst_caps_unref (tmp2);
407 n = gst_caps_get_size (tmp);
409 tmp2 = gst_caps_new_empty ();
410 for (i = 0; i < n; i++) {
411 GstStructure *s = gst_caps_get_structure (tmp, i);
414 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
415 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
417 gst_structure_set_name (s, "video/x-raw-yuv");
418 c = gst_structure_copy (s);
419 gst_structure_set_name (c, "video/x-raw-rgb");
420 gst_caps_append_structure (tmp2, c);
422 gst_caps_append (tmp, tmp2);
424 intersection = gst_caps_intersect (tmp, ret);
425 gst_caps_unref (tmp);
426 gst_caps_unref (ret);
430 gst_object_unref (self);
432 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
438 gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps)
440 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
443 gint width, height, bpp;
445 GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
447 s = gst_caps_get_structure (caps, 0);
449 if (!gst_structure_get_int (s, "width", &width) ||
450 !gst_structure_get_int (s, "height", &height) ||
451 !gst_structure_get_int (s, "bpp", &bpp)) {
456 if ((self->width != width || self->height != height) &&
457 self->width > 0 && self->height > 0) {
458 GST_ERROR_OBJECT (pad, "Mask caps must have the same width/height "
459 "as the video caps");
464 self->height = height;
467 self->mask_bpp = bpp;
470 gst_object_unref (self);
476 gst_shape_wipe_mask_sink_getcaps (GstPad * pad)
478 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
482 if (GST_PAD_CAPS (pad))
483 return gst_caps_copy (GST_PAD_CAPS (pad));
485 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
488 gst_caps_intersect (tmp,
489 gst_pad_get_pad_template_caps (self->video_sinkpad));
490 gst_caps_unref (tmp);
492 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
495 tmp = gst_pad_peer_get_caps (self->srcpad);
497 GstCaps *intersection;
499 intersection = gst_caps_intersect (ret, tmp);
500 gst_caps_unref (ret);
501 gst_caps_unref (tmp);
505 n = gst_caps_get_size (ret);
506 tmp = gst_caps_new_empty ();
507 for (i = 0; i < n; i++) {
508 GstStructure *s = gst_caps_get_structure (ret, i);
511 gst_structure_set_name (s, "video/x-raw-gray");
512 gst_structure_remove_fields (s, "format", "framerate", "bpp", "depth",
513 "endianness", "framerate", "red_mask", "green_mask", "blue_mask",
516 if (self->width && self->height)
517 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
518 G_TYPE_INT, self->height, NULL);
520 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
522 t = gst_structure_copy (s);
524 gst_structure_set (s, "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16,
525 "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
526 gst_structure_set (t, "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);
528 gst_caps_append_structure (tmp, t);
530 gst_caps_append (ret, tmp);
532 tmp = gst_pad_peer_get_caps (pad);
534 GstCaps *intersection;
536 intersection = gst_caps_intersect (tmp, ret);
537 gst_caps_unref (tmp);
538 gst_caps_unref (ret);
542 gst_object_unref (self);
544 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
550 gst_shape_wipe_src_getcaps (GstPad * pad)
552 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
555 if (GST_PAD_CAPS (pad))
556 return gst_caps_copy (GST_PAD_CAPS (pad));
557 else if (GST_PAD_CAPS (self->video_sinkpad))
558 return gst_caps_copy (GST_PAD_CAPS (self->video_sinkpad));
560 tmp = gst_pad_peer_get_caps (self->video_sinkpad);
563 gst_caps_intersect (tmp,
564 gst_pad_get_pad_template_caps (self->video_sinkpad));
565 gst_caps_unref (tmp);
567 ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
570 tmp = gst_pad_peer_get_caps (pad);
572 GstCaps *intersection;
574 intersection = gst_caps_intersect (tmp, ret);
575 gst_caps_unref (tmp);
576 gst_caps_unref (ret);
580 if (self->height && self->width) {
583 n = gst_caps_get_size (ret);
584 for (i = 0; i < n; i++) {
585 GstStructure *s = gst_caps_get_structure (ret, i);
587 gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
588 G_TYPE_INT, self->height, NULL);
592 tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
594 GstCaps *intersection, *tmp2;
597 tmp = gst_caps_make_writable (tmp);
598 tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
600 intersection = gst_caps_intersect (tmp, tmp2);
601 gst_caps_unref (tmp);
602 gst_caps_unref (tmp2);
605 n = gst_caps_get_size (tmp);
607 tmp2 = gst_caps_new_empty ();
608 for (i = 0; i < n; i++) {
609 GstStructure *s = gst_caps_get_structure (tmp, i);
612 gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
613 "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
615 gst_structure_set_name (s, "video/x-raw-yuv");
616 c = gst_structure_copy (s);
618 gst_caps_append_structure (tmp2, c);
620 gst_caps_append (tmp, tmp2);
622 intersection = gst_caps_intersect (tmp, ret);
623 gst_caps_unref (tmp);
624 gst_caps_unref (ret);
628 gst_object_unref (self);
630 GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
636 gst_shape_wipe_video_sink_query (GstPad * pad, GstQuery * query)
638 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
640 GstPad *peer = gst_pad_get_peer (self->srcpad);
642 GST_DEBUG_OBJECT (pad, "Handling query of type '%s'",
643 gst_query_type_get_name (GST_QUERY_TYPE (query)));
646 GST_INFO_OBJECT (pad, "No peer yet");
649 ret = gst_pad_query (peer, query);
650 gst_object_unref (peer);
653 gst_object_unref (self);
658 gst_shape_wipe_src_query (GstPad * pad, GstQuery * query)
660 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
662 GstPad *peer = gst_pad_get_peer (self->video_sinkpad);
664 GST_DEBUG_OBJECT (pad, "Handling query of type '%s'",
665 gst_query_type_get_name (GST_QUERY_TYPE (query)));
668 GST_INFO_OBJECT (pad, "No peer yet");
671 ret = gst_pad_query (peer, query);
672 gst_object_unref (peer);
675 gst_object_unref (self);
680 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
681 GstClockTimeDiff diff, GstClockTime timestamp)
683 GST_OBJECT_LOCK (self);
684 self->proportion = proportion;
685 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
686 if (G_UNLIKELY (diff > 0))
687 self->earliest_time = timestamp + 2 * diff + self->frame_duration;
689 self->earliest_time = timestamp + diff;
691 self->earliest_time = GST_CLOCK_TIME_NONE;
693 GST_OBJECT_UNLOCK (self);
697 gst_shape_wipe_reset_qos (GstShapeWipe * self)
699 gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
703 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
706 GST_OBJECT_LOCK (self);
707 *proportion = self->proportion;
708 *time = self->earliest_time;
709 GST_OBJECT_UNLOCK (self);
712 /* Perform qos calculations before processing the next frame. Returns TRUE if
713 * the frame should be processed, FALSE if the frame can be dropped entirely */
715 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
717 GstClockTime qostime, earliest_time;
720 /* no timestamp, can't do QoS => process frame */
721 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
722 GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
726 /* get latest QoS observation values */
727 gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
729 /* skip qos if we have no observation (yet) => process frame */
730 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
731 GST_LOG_OBJECT (self, "no observation yet, process frame");
735 /* qos is done on running time */
736 qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
739 /* see how our next timestamp relates to the latest qos timestamp */
740 GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
741 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
743 if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
744 GST_DEBUG_OBJECT (self, "we are late, drop frame");
748 GST_LOG_OBJECT (self, "process frame");
752 #define CREATE_AYUV_FUNCTIONS(depth, scale) \
753 static GstFlowReturn \
754 gst_shape_wipe_blend_ayuv_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
755 GstBuffer * maskbuf, GstBuffer * outbuf) \
757 const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
758 const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
759 guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
761 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
762 GST_ROUND_UP_4 (self->width)) - self->width; \
763 gfloat position = self->mask_position; \
764 gfloat low = position - (self->mask_border / 2.0f); \
765 gfloat high = position + (self->mask_border / 2.0f); \
777 for (i = 0; i < self->height; i++) { \
778 for (j = 0; j < self->width; j++) { \
779 gfloat in = *mask / scale; \
782 output[0] = 0x00; /* A */ \
783 output[1] = 0x00; /* Y */ \
784 output[2] = 0x80; /* U */ \
785 output[3] = 0x80; /* V */ \
786 } else if (in >= high) { \
787 output[0] = 0xff; /* A */ \
788 output[1] = input[1]; /* Y */ \
789 output[2] = input[2]; /* U */ \
790 output[3] = input[3]; /* V */ \
792 gfloat val = 255.0f * ((in - low) / (high - low)); \
794 output[0] = CLAMP (val, 0, 255); /* A */ \
795 output[1] = input[1]; /* Y */ \
796 output[2] = input[2]; /* U */ \
797 output[3] = input[3]; /* V */ \
804 mask += mask_increment; \
807 return GST_FLOW_OK; \
810 CREATE_AYUV_FUNCTIONS (16, 65536.0f);
811 CREATE_AYUV_FUNCTIONS (8, 256.0f);
813 #define CREATE_ARGB_FUNCTIONS(depth, name, scale, a, r, g, b) \
814 static GstFlowReturn \
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); \
838 for (i = 0; i < self->height; i++) { \
839 for (j = 0; j < self->width; j++) { \
840 gfloat in = *mask / scale; \
843 output[a] = 0x00; /* A */ \
844 output[r] = 0x00; /* R */ \
845 output[g] = 0x00; /* G */ \
846 output[b] = 0x00; /* B */ \
847 } else if (in >= high) { \
848 output[a] = 0xff; /* A */ \
849 output[r] = input[r]; /* R */ \
850 output[g] = input[g]; /* G */ \
851 output[b] = input[b]; /* B */ \
853 gfloat val = 255.0f * ((in - low) / (high - low)); \
855 output[a] = CLAMP (val, 0, 255); /* A */ \
856 output[r] = input[r]; /* R */ \
857 output[g] = input[g]; /* G */ \
858 output[b] = input[b]; /* B */ \
865 mask += mask_increment; \
868 return GST_FLOW_OK; \
871 CREATE_ARGB_FUNCTIONS (16, argb, 65536.0f, 0, 1, 2, 3);
872 CREATE_ARGB_FUNCTIONS (8, argb, 256.0f, 0, 1, 2, 3);
874 CREATE_ARGB_FUNCTIONS (16, bgra, 65536.0f, 3, 2, 1, 0);
875 CREATE_ARGB_FUNCTIONS (8, bgra, 256.0f, 3, 2, 1, 0);
878 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
880 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
881 GstFlowReturn ret = GST_FLOW_OK;
882 GstBuffer *mask = NULL, *outbuf = NULL;
883 GstClockTime timestamp;
884 gboolean new_outbuf = FALSE;
886 if (G_UNLIKELY (self->fmt == GST_VIDEO_FORMAT_UNKNOWN))
887 return GST_FLOW_NOT_NEGOTIATED;
889 timestamp = GST_BUFFER_TIMESTAMP (buffer);
891 gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
893 if (GST_CLOCK_TIME_IS_VALID (timestamp))
894 gst_object_sync_values (G_OBJECT (self), timestamp);
896 GST_DEBUG_OBJECT (self,
897 "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %lf",
898 GST_TIME_ARGS (timestamp), self->mask_position);
900 g_mutex_lock (self->mask_mutex);
902 g_cond_wait (self->mask_cond, self->mask_mutex);
904 if (self->mask == NULL) {
905 g_mutex_unlock (self->mask_mutex);
906 gst_buffer_unref (buffer);
907 return GST_FLOW_UNEXPECTED;
909 mask = gst_buffer_ref (self->mask);
911 g_mutex_unlock (self->mask_mutex);
913 if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer))) {
914 gst_buffer_unref (buffer);
915 gst_buffer_unref (mask);
919 /* Try to blend inplace, if it's not possible
920 * get a new buffer from downstream.
922 if (!gst_buffer_is_writable (buffer)) {
924 gst_pad_alloc_buffer_and_set_caps (self->srcpad, GST_BUFFER_OFFSET_NONE,
925 GST_BUFFER_SIZE (buffer), GST_PAD_CAPS (self->srcpad), &outbuf);
926 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
927 gst_buffer_unref (buffer);
928 gst_buffer_unref (mask);
931 gst_buffer_copy_metadata (outbuf, buffer, GST_BUFFER_COPY_ALL);
937 if (self->fmt == GST_VIDEO_FORMAT_AYUV && self->mask_bpp == 16)
938 ret = gst_shape_wipe_blend_ayuv_16 (self, buffer, mask, outbuf);
939 else if (self->fmt == GST_VIDEO_FORMAT_AYUV)
940 ret = gst_shape_wipe_blend_ayuv_8 (self, buffer, mask, outbuf);
941 else if (self->fmt == GST_VIDEO_FORMAT_ARGB && self->mask_bpp == 16)
942 ret = gst_shape_wipe_blend_argb_16 (self, buffer, mask, outbuf);
943 else if (self->fmt == GST_VIDEO_FORMAT_ARGB)
944 ret = gst_shape_wipe_blend_argb_8 (self, buffer, mask, outbuf);
945 else if (self->fmt == GST_VIDEO_FORMAT_BGRA && self->mask_bpp == 16)
946 ret = gst_shape_wipe_blend_bgra_16 (self, buffer, mask, outbuf);
947 else if (self->fmt == GST_VIDEO_FORMAT_BGRA)
948 ret = gst_shape_wipe_blend_bgra_8 (self, buffer, mask, outbuf);
950 g_assert_not_reached ();
952 gst_buffer_unref (mask);
954 gst_buffer_unref (buffer);
956 if (ret != GST_FLOW_OK) {
957 gst_buffer_unref (outbuf);
961 ret = gst_pad_push (self->srcpad, outbuf);
966 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
968 GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
969 GstFlowReturn ret = GST_FLOW_OK;
971 g_mutex_lock (self->mask_mutex);
972 GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
974 gst_buffer_replace (&self->mask, buffer);
975 g_cond_signal (self->mask_cond);
976 g_mutex_unlock (self->mask_mutex);
978 gst_buffer_unref (buffer);
983 static GstStateChangeReturn
984 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
986 GstShapeWipe *self = GST_SHAPE_WIPE (element);
987 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
989 switch (transition) {
990 case GST_STATE_CHANGE_READY_TO_PAUSED:
995 /* Unblock video sink chain function */
996 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
997 g_cond_signal (self->mask_cond);
999 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1000 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1002 switch (transition) {
1003 case GST_STATE_CHANGE_PAUSED_TO_READY:
1004 gst_shape_wipe_reset (self);
1014 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
1016 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1019 GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1021 switch (GST_EVENT_TYPE (event)) {
1022 case GST_EVENT_NEWSEGMENT:{
1025 gint64 start, end, base;
1028 gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
1030 if (fmt == GST_FORMAT_TIME) {
1031 GST_DEBUG_OBJECT (pad,
1032 "Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
1033 GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
1034 GST_TIME_ARGS (end));
1035 gst_segment_set_newsegment (&self->segment, is_update, rate, fmt, start,
1038 gst_segment_init (&self->segment, GST_FORMAT_TIME);
1042 case GST_EVENT_FLUSH_STOP:
1043 gst_shape_wipe_reset_qos (self);
1046 ret = gst_pad_push_event (self->srcpad, event);
1050 gst_object_unref (self);
1055 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1057 GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1059 /* Dropping all events here */
1060 gst_event_unref (event);
1065 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1067 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1070 switch (GST_EVENT_TYPE (event)) {
1071 case GST_EVENT_QOS:{
1072 GstClockTimeDiff diff;
1073 GstClockTime timestamp;
1076 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1078 gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1082 ret = gst_pad_push_event (self->video_sinkpad, event);
1086 gst_object_unref (self);
1091 plugin_init (GstPlugin * plugin)
1093 GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1094 "shapewipe element");
1096 gst_controller_init (NULL, NULL);
1098 if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1099 GST_TYPE_SHAPE_WIPE))
1105 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1108 "Shape Wipe transition filter",
1109 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)