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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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-1.0 -v videotestsrc ! video/x-raw,format=AYUV,width=640,height=480 ! shapewipe position=0.5 name=shape ! videomixer name=mixer ! videoconvert ! autovideosink filesrc location=mask.png ! typefind ! decodebin ! videoconvert ! videoscale ! queue ! shape.mask_sink videotestsrc pattern=snow ! video/x-raw,format=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.
49 #include "gstshapewipe.h"
51 static void gst_shape_wipe_finalize (GObject * object);
52 static void gst_shape_wipe_get_property (GObject * object, guint prop_id,
53 GValue * value, GParamSpec * pspec);
54 static void gst_shape_wipe_set_property (GObject * object, guint prop_id,
55 const GValue * value, GParamSpec * pspec);
57 static void gst_shape_wipe_reset (GstShapeWipe * self);
58 static void gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
59 GstClockTimeDiff diff, GstClockTime time);
60 static void gst_shape_wipe_reset_qos (GstShapeWipe * self);
61 static void gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
64 static GstStateChangeReturn gst_shape_wipe_change_state (GstElement * element,
65 GstStateChange transition);
67 static GstFlowReturn gst_shape_wipe_video_sink_chain (GstPad * pad,
68 GstObject * parent, GstBuffer * buffer);
69 static gboolean gst_shape_wipe_video_sink_event (GstPad * pad,
70 GstObject * parent, GstEvent * event);
71 static gboolean gst_shape_wipe_video_sink_setcaps (GstShapeWipe * self,
73 static GstCaps *gst_shape_wipe_video_sink_getcaps (GstShapeWipe * self,
74 GstPad * pad, GstCaps * filter);
75 static gboolean gst_shape_wipe_video_sink_query (GstPad * pad,
76 GstObject * parent, GstQuery * query);
77 static GstFlowReturn gst_shape_wipe_mask_sink_chain (GstPad * pad,
78 GstObject * parent, GstBuffer * buffer);
79 static gboolean gst_shape_wipe_mask_sink_event (GstPad * pad,
80 GstObject * parent, GstEvent * event);
81 static gboolean gst_shape_wipe_mask_sink_setcaps (GstShapeWipe * self,
83 static GstCaps *gst_shape_wipe_mask_sink_getcaps (GstShapeWipe * self,
84 GstPad * pad, GstCaps * filter);
85 static gboolean gst_shape_wipe_mask_sink_query (GstPad * pad,
86 GstObject * parent, GstQuery * query);
87 static gboolean gst_shape_wipe_src_event (GstPad * pad, GstObject * parent,
89 static GstCaps *gst_shape_wipe_src_getcaps (GstPad * pad, GstCaps * filter);
90 static gboolean gst_shape_wipe_src_query (GstPad * pad, GstObject * parent,
100 #define DEFAULT_POSITION 0.0
101 #define DEFAULT_BORDER 0.0
103 static GstStaticPadTemplate video_sink_pad_template =
104 GST_STATIC_PAD_TEMPLATE ("video_sink",
107 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, ARGB, BGRA, ABGR, RGBA }")));
109 static GstStaticPadTemplate mask_sink_pad_template =
110 GST_STATIC_PAD_TEMPLATE ("mask_sink",
113 GST_STATIC_CAPS ("video/x-raw, "
114 "format = (string) GRAY8, "
115 "width = " GST_VIDEO_SIZE_RANGE ", "
116 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1 ; "
117 "video/x-raw, " "format = (string) " GST_VIDEO_NE (GRAY16) ", "
118 "width = " GST_VIDEO_SIZE_RANGE ", "
119 "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1"));
121 static GstStaticPadTemplate src_pad_template =
122 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
123 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, ARGB, BGRA, ABGR, RGBA }")));
125 GST_DEBUG_CATEGORY_STATIC (gst_shape_wipe_debug);
126 #define GST_CAT_DEFAULT gst_shape_wipe_debug
128 #define gst_shape_wipe_parent_class parent_class
129 G_DEFINE_TYPE (GstShapeWipe, gst_shape_wipe, GST_TYPE_ELEMENT);
132 gst_shape_wipe_class_init (GstShapeWipeClass * klass)
134 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
137 gobject_class->finalize = gst_shape_wipe_finalize;
138 gobject_class->set_property = gst_shape_wipe_set_property;
139 gobject_class->get_property = gst_shape_wipe_get_property;
141 g_object_class_install_property (gobject_class, PROP_POSITION,
142 g_param_spec_float ("position", "Position", "Position of the mask",
143 0.0, 1.0, DEFAULT_POSITION,
144 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
145 g_object_class_install_property (gobject_class, PROP_BORDER,
146 g_param_spec_float ("border", "Border", "Border of the mask",
147 0.0, 1.0, DEFAULT_BORDER,
148 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
150 gstelement_class->change_state =
151 GST_DEBUG_FUNCPTR (gst_shape_wipe_change_state);
153 gst_element_class_set_static_metadata (gstelement_class,
154 "Shape Wipe transition filter",
155 "Filter/Editor/Video",
156 "Adds a shape wipe transition to a video stream",
157 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
159 gst_element_class_add_pad_template (gstelement_class,
160 gst_static_pad_template_get (&video_sink_pad_template));
161 gst_element_class_add_pad_template (gstelement_class,
162 gst_static_pad_template_get (&mask_sink_pad_template));
163 gst_element_class_add_pad_template (gstelement_class,
164 gst_static_pad_template_get (&src_pad_template));
168 gst_shape_wipe_init (GstShapeWipe * self)
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_query_function (self->video_sinkpad,
177 GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_query));
178 gst_element_add_pad (GST_ELEMENT (self), self->video_sinkpad);
181 gst_pad_new_from_static_template (&mask_sink_pad_template, "mask_sink");
182 gst_pad_set_chain_function (self->mask_sinkpad,
183 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_chain));
184 gst_pad_set_event_function (self->mask_sinkpad,
185 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_event));
186 gst_pad_set_query_function (self->mask_sinkpad,
187 GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_query));
188 gst_element_add_pad (GST_ELEMENT (self), self->mask_sinkpad);
190 self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
191 gst_pad_set_event_function (self->srcpad,
192 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_event));
193 gst_pad_set_query_function (self->srcpad,
194 GST_DEBUG_FUNCPTR (gst_shape_wipe_src_query));
195 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
197 g_mutex_init (&self->mask_mutex);
198 g_cond_init (&self->mask_cond);
200 gst_shape_wipe_reset (self);
204 gst_shape_wipe_get_property (GObject * object, guint prop_id,
205 GValue * value, GParamSpec * pspec)
207 GstShapeWipe *self = GST_SHAPE_WIPE (object);
211 g_value_set_float (value, self->mask_position);
214 g_value_set_float (value, self->mask_border);
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gst_shape_wipe_set_property (GObject * object, guint prop_id,
224 const GValue * value, GParamSpec * pspec)
226 GstShapeWipe *self = GST_SHAPE_WIPE (object);
230 gfloat f = g_value_get_float (value);
232 GST_LOG_OBJECT (self, "Setting mask position: %f", f);
233 self->mask_position = f;
237 gfloat f = g_value_get_float (value);
239 GST_LOG_OBJECT (self, "Setting mask border: %f", f);
240 self->mask_border = f;
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);
256 g_cond_clear (&self->mask_cond);
257 g_mutex_clear (&self->mask_mutex);
259 G_OBJECT_CLASS (parent_class)->finalize (object);
263 gst_shape_wipe_reset (GstShapeWipe * self)
265 GST_DEBUG_OBJECT (self, "Resetting internal state");
268 gst_buffer_unref (self->mask);
271 g_mutex_lock (&self->mask_mutex);
272 g_cond_signal (&self->mask_cond);
273 g_mutex_unlock (&self->mask_mutex);
275 gst_video_info_init (&self->vinfo);
276 gst_video_info_init (&self->minfo);
279 gst_segment_init (&self->segment, GST_FORMAT_TIME);
281 gst_shape_wipe_reset_qos (self);
282 self->frame_duration = 0;
286 gst_shape_wipe_video_sink_setcaps (GstShapeWipe * self, GstCaps * caps)
291 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
293 if (!gst_video_info_from_caps (&info, caps))
296 if ((self->vinfo.width != info.width || self->vinfo.height != info.height) &&
297 self->vinfo.width > 0 && self->vinfo.height > 0) {
298 g_mutex_lock (&self->mask_mutex);
300 gst_buffer_unref (self->mask);
302 g_mutex_unlock (&self->mask_mutex);
307 self->frame_duration =
308 gst_util_uint64_scale (GST_SECOND, info.fps_d, info.fps_n);
310 self->frame_duration = 0;
314 ret = gst_pad_set_caps (self->srcpad, caps);
321 GST_ERROR_OBJECT (self, "Invalid caps");
327 gst_shape_wipe_video_sink_getcaps (GstShapeWipe * self, GstPad * pad,
330 GstCaps *templ, *ret, *tmp;
332 if (gst_pad_has_current_caps (pad))
333 return gst_pad_get_current_caps (pad);
335 templ = gst_pad_get_pad_template_caps (pad);
336 tmp = gst_pad_peer_query_caps (self->srcpad, NULL);
338 ret = gst_caps_intersect (tmp, templ);
339 gst_caps_unref (templ);
340 gst_caps_unref (tmp);
345 GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
347 if (gst_caps_is_empty (ret))
350 tmp = gst_pad_peer_query_caps (pad, NULL);
352 GST_LOG_OBJECT (pad, "peerpad accepted caps: %" GST_PTR_FORMAT, tmp);
354 GstCaps *intersection;
356 intersection = gst_caps_intersect (tmp, ret);
357 gst_caps_unref (tmp);
358 gst_caps_unref (ret);
362 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, tmp);
364 if (gst_caps_is_empty (ret))
367 if (self->vinfo.height && self->vinfo.width) {
370 ret = gst_caps_make_writable (ret);
371 n = gst_caps_get_size (ret);
372 for (i = 0; i < n; i++) {
373 GstStructure *s = gst_caps_get_structure (ret, i);
375 gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
376 G_TYPE_INT, self->vinfo.height, NULL);
380 tmp = gst_pad_peer_query_caps (self->mask_sinkpad, NULL);
382 GST_LOG_OBJECT (pad, "mask accepted caps: %" GST_PTR_FORMAT, tmp);
384 GstCaps *intersection, *tmp2;
387 tmp2 = gst_pad_get_pad_template_caps (self->mask_sinkpad);
388 intersection = gst_caps_intersect (tmp, tmp2);
389 gst_caps_unref (tmp);
390 gst_caps_unref (tmp2);
393 tmp = gst_caps_make_writable (tmp);
394 n = gst_caps_get_size (tmp);
396 for (i = 0; i < n; i++) {
397 GstStructure *s = gst_caps_get_structure (tmp, i);
399 gst_structure_remove_fields (s, "format", "framerate", NULL);
400 gst_structure_set_name (s, "video/x-raw");
403 intersection = gst_caps_intersect (tmp, ret);
404 gst_caps_unref (tmp);
405 gst_caps_unref (ret);
409 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
415 gst_shape_wipe_mask_sink_setcaps (GstShapeWipe * self, GstCaps * caps)
418 gint width, height, bpp;
421 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
423 if (!gst_video_info_from_caps (&info, caps)) {
428 width = GST_VIDEO_INFO_WIDTH (&info);
429 height = GST_VIDEO_INFO_HEIGHT (&info);
430 bpp = GST_VIDEO_INFO_COMP_DEPTH (&info, 0);
432 if ((self->vinfo.width != width || self->vinfo.height != height) &&
433 self->vinfo.width > 0 && self->vinfo.height > 0) {
434 GST_ERROR_OBJECT (self, "Mask caps must have the same width/height "
435 "as the video caps");
440 self->mask_bpp = bpp;
448 gst_shape_wipe_mask_sink_getcaps (GstShapeWipe * self, GstPad * pad,
451 GstCaps *ret, *tmp, *tcaps;
454 if (gst_pad_has_current_caps (pad))
455 return gst_pad_get_current_caps (pad);
457 tcaps = gst_pad_get_pad_template_caps (self->video_sinkpad);
458 tmp = gst_pad_peer_query_caps (self->video_sinkpad, NULL);
460 ret = gst_caps_intersect (tmp, tcaps);
461 gst_caps_unref (tcaps);
462 gst_caps_unref (tmp);
467 GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
469 if (gst_caps_is_empty (ret))
472 tmp = gst_pad_peer_query_caps (self->srcpad, NULL);
473 GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
476 GstCaps *intersection;
478 intersection = gst_caps_intersect (ret, tmp);
479 gst_caps_unref (ret);
480 gst_caps_unref (tmp);
484 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
486 if (gst_caps_is_empty (ret))
489 ret = gst_caps_make_writable (ret);
490 n = gst_caps_get_size (ret);
491 tmp = gst_caps_new_empty ();
492 for (i = 0; i < n; i++) {
493 GstStructure *s = gst_caps_get_structure (ret, i);
496 gst_structure_set_name (s, "video/x-raw");
497 gst_structure_remove_fields (s, "format", "framerate", NULL);
499 if (self->vinfo.width && self->vinfo.height)
500 gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
501 G_TYPE_INT, self->vinfo.height, NULL);
503 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
505 t = gst_structure_copy (s);
507 gst_structure_set (s, "format", G_TYPE_STRING, GST_VIDEO_NE (GRAY16), NULL);
508 gst_structure_set (t, "format", G_TYPE_STRING, "GRAY8", NULL);
510 gst_caps_append_structure (tmp, t);
512 gst_caps_append (ret, tmp);
514 tmp = gst_pad_peer_query_caps (pad, NULL);
515 GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, tmp);
518 GstCaps *intersection;
520 intersection = gst_caps_intersect (tmp, ret);
521 gst_caps_unref (tmp);
522 gst_caps_unref (ret);
527 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
533 gst_shape_wipe_src_getcaps (GstPad * pad, GstCaps * filter)
535 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
536 GstCaps *templ, *ret, *tmp;
538 if (gst_pad_has_current_caps (pad))
539 return gst_pad_get_current_caps (pad);
540 else if (gst_pad_has_current_caps (self->video_sinkpad))
541 return gst_pad_get_current_caps (self->video_sinkpad);
543 templ = gst_pad_get_pad_template_caps (self->video_sinkpad);
544 tmp = gst_pad_peer_query_caps (self->video_sinkpad, NULL);
546 ret = gst_caps_intersect (tmp, templ);
547 gst_caps_unref (templ);
548 gst_caps_unref (tmp);
553 GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
555 if (gst_caps_is_empty (ret))
558 tmp = gst_pad_peer_query_caps (pad, NULL);
559 GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, ret);
561 GstCaps *intersection;
563 intersection = gst_caps_intersect (tmp, ret);
564 gst_caps_unref (tmp);
565 gst_caps_unref (ret);
569 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
571 if (gst_caps_is_empty (ret))
574 if (self->vinfo.height && self->vinfo.width) {
577 ret = gst_caps_make_writable (ret);
578 n = gst_caps_get_size (ret);
579 for (i = 0; i < n; i++) {
580 GstStructure *s = gst_caps_get_structure (ret, i);
582 gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
583 G_TYPE_INT, self->vinfo.height, NULL);
587 tmp = gst_pad_peer_query_caps (self->mask_sinkpad, NULL);
588 GST_LOG_OBJECT (pad, "mask sink accepted caps: %" GST_PTR_FORMAT, ret);
590 GstCaps *intersection, *tmp2;
593 tmp2 = gst_pad_get_pad_template_caps (self->mask_sinkpad);
594 intersection = gst_caps_intersect (tmp, tmp2);
595 gst_caps_unref (tmp);
596 gst_caps_unref (tmp2);
598 tmp = gst_caps_make_writable (intersection);
599 n = gst_caps_get_size (tmp);
601 for (i = 0; i < n; i++) {
602 GstStructure *s = gst_caps_get_structure (tmp, i);
604 gst_structure_remove_fields (s, "format", "framerate", NULL);
605 gst_structure_set_name (s, "video/x-raw");
608 intersection = gst_caps_intersect (tmp, ret);
609 gst_caps_unref (tmp);
610 gst_caps_unref (ret);
616 gst_object_unref (self);
618 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
624 gst_shape_wipe_video_sink_query (GstPad * pad, GstObject * parent,
627 GstShapeWipe *self = (GstShapeWipe *) parent;
630 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
631 gst_query_type_get_name (GST_QUERY_TYPE (query)));
633 switch (GST_QUERY_TYPE (query)) {
636 GstCaps *filter, *caps;
638 gst_query_parse_caps (query, &filter);
639 caps = gst_shape_wipe_video_sink_getcaps (self, pad, filter);
640 gst_query_set_caps_result (query, caps);
641 gst_caps_unref (caps);
646 ret = gst_pad_query_default (pad, parent, query);
654 gst_shape_wipe_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
656 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
659 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
660 gst_query_type_get_name (GST_QUERY_TYPE (query)));
662 switch (GST_QUERY_TYPE (query)) {
665 GstCaps *filter, *caps;
667 gst_query_parse_caps (query, &filter);
668 caps = gst_shape_wipe_src_getcaps (pad, filter);
669 gst_query_set_caps_result (query, caps);
670 gst_caps_unref (caps);
675 ret = gst_pad_peer_query (self->video_sinkpad, query);
683 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
684 GstClockTimeDiff diff, GstClockTime timestamp)
686 GST_OBJECT_LOCK (self);
687 self->proportion = proportion;
688 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
689 if (G_UNLIKELY (diff > 0))
690 self->earliest_time = timestamp + 2 * diff + self->frame_duration;
692 self->earliest_time = timestamp + diff;
694 self->earliest_time = GST_CLOCK_TIME_NONE;
696 GST_OBJECT_UNLOCK (self);
700 gst_shape_wipe_reset_qos (GstShapeWipe * self)
702 gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
706 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
709 GST_OBJECT_LOCK (self);
710 *proportion = self->proportion;
711 *time = self->earliest_time;
712 GST_OBJECT_UNLOCK (self);
715 /* Perform qos calculations before processing the next frame. Returns TRUE if
716 * the frame should be processed, FALSE if the frame can be dropped entirely */
718 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
720 GstClockTime qostime, earliest_time;
723 /* no timestamp, can't do QoS => process frame */
724 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
725 GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
729 /* get latest QoS observation values */
730 gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
732 /* skip qos if we have no observation (yet) => process frame */
733 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
734 GST_LOG_OBJECT (self, "no observation yet, process frame");
738 /* qos is done on running time */
739 qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
742 /* see how our next timestamp relates to the latest qos timestamp */
743 GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
744 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
746 if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
747 GST_DEBUG_OBJECT (self, "we are late, drop frame");
751 GST_LOG_OBJECT (self, "process frame");
755 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
757 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstVideoFrame * inframe, \
758 GstVideoFrame * maskframe, GstVideoFrame * outframe) \
760 const guint##depth *mask = (const guint##depth *) GST_VIDEO_FRAME_PLANE_DATA (maskframe, 0); \
761 const guint8 *input = (const guint8 *) GST_VIDEO_FRAME_PLANE_DATA (inframe, 0); \
762 guint8 *output = (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (outframe, 0); \
764 gint width = GST_VIDEO_FRAME_WIDTH (inframe); \
765 gint height = GST_VIDEO_FRAME_HEIGHT (inframe); \
766 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (width) : \
767 GST_ROUND_UP_4 (width)) - width; \
768 gfloat position = self->mask_position; \
769 gfloat low = position - (self->mask_border / 2.0f); \
770 gfloat high = position + (self->mask_border / 2.0f); \
771 guint32 low_i, high_i, round_i; \
783 low_i = low * 65536; \
784 high_i = high * 65536; \
785 round_i = (high_i - low_i) >> 1; \
787 for (i = 0; i < height; i++) { \
788 for (j = 0; j < width; j++) { \
789 guint32 in = *mask << shift; \
792 output[a] = 0x00; /* A */ \
793 output[r] = input[r]; /* R */ \
794 output[g] = input[g]; /* G */ \
795 output[b] = input[b]; /* B */ \
796 } else if (in >= high_i) { \
797 output[a] = input[a]; /* A */ \
798 output[r] = input[r]; /* R */ \
799 output[g] = input[g]; /* G */ \
800 output[b] = input[b]; /* B */ \
803 /* Note: This will never overflow or be larger than 255! */ \
804 val = (((in - low_i) << 16) + round_i) / (high_i - low_i); \
805 val = (val * input[a] + 32768) >> 16; \
807 output[a] = val; /* A */ \
808 output[r] = input[r]; /* R */ \
809 output[g] = input[g]; /* G */ \
810 output[b] = input[b]; /* B */ \
817 mask += mask_increment; \
821 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
822 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
824 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
825 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
828 gst_shape_wipe_video_sink_chain (GstPad * pad, GstObject * parent,
831 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
832 GstFlowReturn ret = GST_FLOW_OK;
833 GstBuffer *mask = NULL, *outbuf = NULL;
834 GstClockTime timestamp;
835 GstVideoFrame inframe, outframe, maskframe;
837 if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&self->vinfo) ==
838 GST_VIDEO_FORMAT_UNKNOWN))
841 timestamp = GST_BUFFER_TIMESTAMP (buffer);
843 gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
845 if (GST_CLOCK_TIME_IS_VALID (timestamp))
846 gst_object_sync_values (GST_OBJECT (self), timestamp);
848 GST_LOG_OBJECT (self,
849 "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %f",
850 GST_TIME_ARGS (timestamp), self->mask_position);
852 g_mutex_lock (&self->mask_mutex);
857 g_cond_wait (&self->mask_cond, &self->mask_mutex);
859 if (self->mask == NULL || self->shutdown) {
862 mask = gst_buffer_ref (self->mask);
864 g_mutex_unlock (&self->mask_mutex);
866 if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
869 /* Will blend inplace if buffer is writable */
870 outbuf = gst_buffer_make_writable (buffer);
871 gst_video_frame_map (&outframe, &self->vinfo, outbuf, GST_MAP_READWRITE);
872 gst_video_frame_map (&inframe, &self->vinfo, outbuf, GST_MAP_READ);
874 gst_video_frame_map (&maskframe, &self->minfo, mask, GST_MAP_READ);
876 switch (GST_VIDEO_INFO_FORMAT (&self->vinfo)) {
877 case GST_VIDEO_FORMAT_AYUV:
878 case GST_VIDEO_FORMAT_ARGB:
879 case GST_VIDEO_FORMAT_ABGR:
880 if (self->mask_bpp == 16)
881 gst_shape_wipe_blend_argb_16 (self, &inframe, &maskframe, &outframe);
883 gst_shape_wipe_blend_argb_8 (self, &inframe, &maskframe, &outframe);
885 case GST_VIDEO_FORMAT_BGRA:
886 case GST_VIDEO_FORMAT_RGBA:
887 if (self->mask_bpp == 16)
888 gst_shape_wipe_blend_bgra_16 (self, &inframe, &maskframe, &outframe);
890 gst_shape_wipe_blend_bgra_8 (self, &inframe, &maskframe, &outframe);
893 g_assert_not_reached ();
897 gst_video_frame_unmap (&outframe);
898 gst_video_frame_unmap (&inframe);
900 gst_video_frame_unmap (&maskframe);
902 gst_buffer_unref (mask);
904 ret = gst_pad_push (self->srcpad, outbuf);
905 if (G_UNLIKELY (ret != GST_FLOW_OK))
913 GST_ERROR_OBJECT (self, "No valid caps yet");
914 gst_buffer_unref (buffer);
915 return GST_FLOW_NOT_NEGOTIATED;
919 GST_DEBUG_OBJECT (self, "Shutting down");
920 gst_buffer_unref (buffer);
921 return GST_FLOW_FLUSHING;
925 GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
926 gst_buffer_unref (buffer);
927 gst_buffer_unref (mask);
932 if (ret != GST_FLOW_FLUSHING)
933 GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
934 gst_flow_get_name (ret));
940 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstObject * parent,
943 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
944 GstFlowReturn ret = GST_FLOW_OK;
946 g_mutex_lock (&self->mask_mutex);
947 GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
949 gst_buffer_replace (&self->mask, buffer);
950 g_cond_signal (&self->mask_cond);
951 g_mutex_unlock (&self->mask_mutex);
953 gst_buffer_unref (buffer);
958 static GstStateChangeReturn
959 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
961 GstShapeWipe *self = GST_SHAPE_WIPE (element);
962 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
964 switch (transition) {
965 case GST_STATE_CHANGE_READY_TO_PAUSED:
966 self->shutdown = FALSE;
968 case GST_STATE_CHANGE_PAUSED_TO_READY:
969 /* Unblock video sink chain function */
970 g_mutex_lock (&self->mask_mutex);
971 self->shutdown = TRUE;
972 g_cond_signal (&self->mask_cond);
973 g_mutex_unlock (&self->mask_mutex);
979 if (GST_ELEMENT_CLASS (parent_class)->change_state)
980 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
982 switch (transition) {
983 case GST_STATE_CHANGE_PAUSED_TO_READY:
984 gst_shape_wipe_reset (self);
994 gst_shape_wipe_video_sink_event (GstPad * pad, GstObject * parent,
997 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1000 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1002 switch (GST_EVENT_TYPE (event)) {
1003 case GST_EVENT_CAPS:
1007 gst_event_parse_caps (event, &caps);
1008 ret = gst_shape_wipe_video_sink_setcaps (self, caps);
1009 gst_event_unref (event);
1012 case GST_EVENT_SEGMENT:
1016 gst_event_copy_segment (event, &seg);
1017 if (seg.format == GST_FORMAT_TIME) {
1018 GST_DEBUG_OBJECT (pad,
1019 "Got SEGMENT event in GST_FORMAT_TIME %" GST_PTR_FORMAT, &seg);
1020 self->segment = seg;
1022 gst_segment_init (&self->segment, GST_FORMAT_TIME);
1026 case GST_EVENT_FLUSH_STOP:
1027 gst_shape_wipe_reset_qos (self);
1030 ret = gst_pad_push_event (self->srcpad, event);
1038 gst_shape_wipe_mask_sink_event (GstPad * pad, GstObject * parent,
1041 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1043 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1045 switch (GST_EVENT_TYPE (event)) {
1046 case GST_EVENT_CAPS:
1050 gst_event_parse_caps (event, &caps);
1051 gst_shape_wipe_mask_sink_setcaps (self, caps);
1054 case GST_EVENT_FLUSH_STOP:
1055 g_mutex_lock (&self->mask_mutex);
1056 gst_buffer_replace (&self->mask, NULL);
1057 g_mutex_unlock (&self->mask_mutex);
1063 /* Dropping all events here */
1064 gst_event_unref (event);
1070 gst_shape_wipe_mask_sink_query (GstPad * pad, GstObject * parent,
1073 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1076 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
1077 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1079 switch (GST_QUERY_TYPE (query)) {
1080 case GST_QUERY_CAPS:
1082 GstCaps *filter, *caps;
1084 gst_query_parse_caps (query, &filter);
1085 caps = gst_shape_wipe_mask_sink_getcaps (self, pad, filter);
1086 gst_query_set_caps_result (query, caps);
1087 gst_caps_unref (caps);
1092 ret = gst_pad_query_default (pad, parent, query);
1101 gst_shape_wipe_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1103 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1106 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1108 switch (GST_EVENT_TYPE (event)) {
1109 case GST_EVENT_QOS:{
1111 GstClockTimeDiff diff;
1112 GstClockTime timestamp;
1115 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1117 gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1121 ret = gst_pad_push_event (self->video_sinkpad, event);
1129 plugin_init (GstPlugin * plugin)
1131 GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1132 "shapewipe element");
1134 if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1135 GST_TYPE_SHAPE_WIPE))
1141 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1144 "Shape Wipe transition filter",
1145 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)