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_static_pad_template (gstelement_class,
160 &video_sink_pad_template);
161 gst_element_class_add_static_pad_template (gstelement_class,
162 &mask_sink_pad_template);
163 gst_element_class_add_static_pad_template (gstelement_class,
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 ret = gst_pad_get_current_caps (pad);
336 templ = gst_pad_get_pad_template_caps (pad);
337 tmp = gst_pad_peer_query_caps (self->srcpad, NULL);
339 ret = gst_caps_intersect (tmp, templ);
340 gst_caps_unref (templ);
341 gst_caps_unref (tmp);
346 GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
348 if (gst_caps_is_empty (ret))
351 tmp = gst_pad_peer_query_caps (pad, NULL);
353 GST_LOG_OBJECT (pad, "peerpad accepted caps: %" GST_PTR_FORMAT, tmp);
355 GstCaps *intersection;
357 intersection = gst_caps_intersect (tmp, ret);
358 gst_caps_unref (tmp);
359 gst_caps_unref (ret);
363 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, tmp);
365 if (gst_caps_is_empty (ret))
368 if (self->vinfo.height && self->vinfo.width) {
371 ret = gst_caps_make_writable (ret);
372 n = gst_caps_get_size (ret);
373 for (i = 0; i < n; i++) {
374 GstStructure *s = gst_caps_get_structure (ret, i);
376 gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
377 G_TYPE_INT, self->vinfo.height, NULL);
381 tmp = gst_pad_peer_query_caps (self->mask_sinkpad, NULL);
383 GST_LOG_OBJECT (pad, "mask accepted caps: %" GST_PTR_FORMAT, tmp);
385 GstCaps *intersection, *tmp2;
388 tmp2 = gst_pad_get_pad_template_caps (self->mask_sinkpad);
389 intersection = gst_caps_intersect (tmp, tmp2);
390 gst_caps_unref (tmp);
391 gst_caps_unref (tmp2);
394 tmp = gst_caps_make_writable (tmp);
395 n = gst_caps_get_size (tmp);
397 for (i = 0; i < n; i++) {
398 GstStructure *s = gst_caps_get_structure (tmp, i);
400 gst_structure_remove_fields (s, "format", "framerate", NULL);
401 gst_structure_set_name (s, "video/x-raw");
404 intersection = gst_caps_intersect (tmp, ret);
405 gst_caps_unref (tmp);
406 gst_caps_unref (ret);
410 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
416 gst_shape_wipe_mask_sink_setcaps (GstShapeWipe * self, GstCaps * caps)
419 gint width, height, bpp;
422 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
424 if (!gst_video_info_from_caps (&info, caps)) {
429 width = GST_VIDEO_INFO_WIDTH (&info);
430 height = GST_VIDEO_INFO_HEIGHT (&info);
431 bpp = GST_VIDEO_INFO_COMP_DEPTH (&info, 0);
433 if ((self->vinfo.width != width || self->vinfo.height != height) &&
434 self->vinfo.width > 0 && self->vinfo.height > 0) {
435 GST_ERROR_OBJECT (self, "Mask caps must have the same width/height "
436 "as the video caps");
441 self->mask_bpp = bpp;
449 gst_shape_wipe_mask_sink_getcaps (GstShapeWipe * self, GstPad * pad,
452 GstCaps *ret, *tmp, *tcaps;
455 ret = gst_pad_get_current_caps (pad);
459 tcaps = gst_pad_get_pad_template_caps (self->video_sinkpad);
460 tmp = gst_pad_peer_query_caps (self->video_sinkpad, NULL);
462 ret = gst_caps_intersect (tmp, tcaps);
463 gst_caps_unref (tcaps);
464 gst_caps_unref (tmp);
469 GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
471 if (gst_caps_is_empty (ret))
474 tmp = gst_pad_peer_query_caps (self->srcpad, NULL);
475 GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
478 GstCaps *intersection;
480 intersection = gst_caps_intersect (ret, tmp);
481 gst_caps_unref (ret);
482 gst_caps_unref (tmp);
486 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
488 if (gst_caps_is_empty (ret))
491 ret = gst_caps_make_writable (ret);
492 n = gst_caps_get_size (ret);
493 tmp = gst_caps_new_empty ();
494 for (i = 0; i < n; i++) {
495 GstStructure *s = gst_caps_get_structure (ret, i);
498 gst_structure_set_name (s, "video/x-raw");
499 gst_structure_remove_fields (s, "format", "framerate", NULL);
501 if (self->vinfo.width && self->vinfo.height)
502 gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
503 G_TYPE_INT, self->vinfo.height, NULL);
505 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
507 t = gst_structure_copy (s);
509 gst_structure_set (s, "format", G_TYPE_STRING, GST_VIDEO_NE (GRAY16), NULL);
510 gst_structure_set (t, "format", G_TYPE_STRING, "GRAY8", NULL);
512 gst_caps_append_structure (tmp, t);
514 gst_caps_append (ret, tmp);
516 tmp = gst_pad_peer_query_caps (pad, NULL);
517 GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, tmp);
520 GstCaps *intersection;
522 intersection = gst_caps_intersect (tmp, ret);
523 gst_caps_unref (tmp);
524 gst_caps_unref (ret);
529 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
535 gst_shape_wipe_src_getcaps (GstPad * pad, GstCaps * filter)
537 GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
538 GstCaps *templ, *ret, *tmp;
540 ret = gst_pad_get_current_caps (pad);
544 ret = gst_pad_get_current_caps (self->video_sinkpad);
548 templ = gst_pad_get_pad_template_caps (self->video_sinkpad);
549 tmp = gst_pad_peer_query_caps (self->video_sinkpad, NULL);
551 ret = gst_caps_intersect (tmp, templ);
552 gst_caps_unref (templ);
553 gst_caps_unref (tmp);
558 GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
560 if (gst_caps_is_empty (ret))
563 tmp = gst_pad_peer_query_caps (pad, NULL);
564 GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, ret);
566 GstCaps *intersection;
568 intersection = gst_caps_intersect (tmp, ret);
569 gst_caps_unref (tmp);
570 gst_caps_unref (ret);
574 GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
576 if (gst_caps_is_empty (ret))
579 if (self->vinfo.height && self->vinfo.width) {
582 ret = gst_caps_make_writable (ret);
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->vinfo.width, "height",
588 G_TYPE_INT, self->vinfo.height, NULL);
592 tmp = gst_pad_peer_query_caps (self->mask_sinkpad, NULL);
593 GST_LOG_OBJECT (pad, "mask sink accepted caps: %" GST_PTR_FORMAT, ret);
595 GstCaps *intersection, *tmp2;
598 tmp2 = gst_pad_get_pad_template_caps (self->mask_sinkpad);
599 intersection = gst_caps_intersect (tmp, tmp2);
600 gst_caps_unref (tmp);
601 gst_caps_unref (tmp2);
603 tmp = gst_caps_make_writable (intersection);
604 n = gst_caps_get_size (tmp);
606 for (i = 0; i < n; i++) {
607 GstStructure *s = gst_caps_get_structure (tmp, i);
609 gst_structure_remove_fields (s, "format", "framerate", NULL);
610 gst_structure_set_name (s, "video/x-raw");
613 intersection = gst_caps_intersect (tmp, ret);
614 gst_caps_unref (tmp);
615 gst_caps_unref (ret);
621 gst_object_unref (self);
623 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
629 gst_shape_wipe_video_sink_query (GstPad * pad, GstObject * parent,
632 GstShapeWipe *self = (GstShapeWipe *) parent;
635 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
636 gst_query_type_get_name (GST_QUERY_TYPE (query)));
638 switch (GST_QUERY_TYPE (query)) {
641 GstCaps *filter, *caps;
643 gst_query_parse_caps (query, &filter);
644 caps = gst_shape_wipe_video_sink_getcaps (self, pad, filter);
645 gst_query_set_caps_result (query, caps);
646 gst_caps_unref (caps);
651 ret = gst_pad_query_default (pad, parent, query);
659 gst_shape_wipe_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
661 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
664 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
665 gst_query_type_get_name (GST_QUERY_TYPE (query)));
667 switch (GST_QUERY_TYPE (query)) {
670 GstCaps *filter, *caps;
672 gst_query_parse_caps (query, &filter);
673 caps = gst_shape_wipe_src_getcaps (pad, filter);
674 gst_query_set_caps_result (query, caps);
675 gst_caps_unref (caps);
680 ret = gst_pad_peer_query (self->video_sinkpad, query);
688 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
689 GstClockTimeDiff diff, GstClockTime timestamp)
691 GST_OBJECT_LOCK (self);
692 self->proportion = proportion;
693 if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
694 if (G_UNLIKELY (diff > 0))
695 self->earliest_time = timestamp + 2 * diff + self->frame_duration;
697 self->earliest_time = timestamp + diff;
699 self->earliest_time = GST_CLOCK_TIME_NONE;
701 GST_OBJECT_UNLOCK (self);
705 gst_shape_wipe_reset_qos (GstShapeWipe * self)
707 gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
711 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
714 GST_OBJECT_LOCK (self);
715 *proportion = self->proportion;
716 *time = self->earliest_time;
717 GST_OBJECT_UNLOCK (self);
720 /* Perform qos calculations before processing the next frame. Returns TRUE if
721 * the frame should be processed, FALSE if the frame can be dropped entirely */
723 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
725 GstClockTime qostime, earliest_time;
728 /* no timestamp, can't do QoS => process frame */
729 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
730 GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
734 /* get latest QoS observation values */
735 gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
737 /* skip qos if we have no observation (yet) => process frame */
738 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
739 GST_LOG_OBJECT (self, "no observation yet, process frame");
743 /* qos is done on running time */
744 qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
747 /* see how our next timestamp relates to the latest qos timestamp */
748 GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
749 GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
751 if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
752 GST_DEBUG_OBJECT (self, "we are late, drop frame");
756 GST_LOG_OBJECT (self, "process frame");
760 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
762 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstVideoFrame * inframe, \
763 GstVideoFrame * maskframe, GstVideoFrame * outframe) \
765 const guint##depth *mask = (const guint##depth *) GST_VIDEO_FRAME_PLANE_DATA (maskframe, 0); \
766 const guint8 *input = (const guint8 *) GST_VIDEO_FRAME_PLANE_DATA (inframe, 0); \
767 guint8 *output = (guint8 *) GST_VIDEO_FRAME_PLANE_DATA (outframe, 0); \
769 gint width = GST_VIDEO_FRAME_WIDTH (inframe); \
770 gint height = GST_VIDEO_FRAME_HEIGHT (inframe); \
771 guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (width) : \
772 GST_ROUND_UP_4 (width)) - width; \
773 gfloat position = self->mask_position; \
774 gfloat low = position - (self->mask_border / 2.0f); \
775 gfloat high = position + (self->mask_border / 2.0f); \
776 guint32 low_i, high_i, round_i; \
788 low_i = low * 65536; \
789 high_i = high * 65536; \
790 round_i = (high_i - low_i) >> 1; \
792 for (i = 0; i < height; i++) { \
793 for (j = 0; j < width; j++) { \
794 guint32 in = *mask << shift; \
797 output[a] = 0x00; /* A */ \
798 output[r] = input[r]; /* R */ \
799 output[g] = input[g]; /* G */ \
800 output[b] = input[b]; /* B */ \
801 } else if (in >= high_i) { \
802 output[a] = input[a]; /* A */ \
803 output[r] = input[r]; /* R */ \
804 output[g] = input[g]; /* G */ \
805 output[b] = input[b]; /* B */ \
808 /* Note: This will never overflow or be larger than 255! */ \
809 val = (((in - low_i) << 16) + round_i) / (high_i - low_i); \
810 val = (val * input[a] + 32768) >> 16; \
812 output[a] = val; /* A */ \
813 output[r] = input[r]; /* R */ \
814 output[g] = input[g]; /* G */ \
815 output[b] = input[b]; /* B */ \
822 mask += mask_increment; \
826 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
827 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
829 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
830 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
833 gst_shape_wipe_video_sink_chain (GstPad * pad, GstObject * parent,
836 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
837 GstFlowReturn ret = GST_FLOW_OK;
838 GstBuffer *mask = NULL, *outbuf = NULL;
839 GstClockTime timestamp;
840 GstVideoFrame inframe, outframe, maskframe;
842 if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&self->vinfo) ==
843 GST_VIDEO_FORMAT_UNKNOWN))
846 timestamp = GST_BUFFER_TIMESTAMP (buffer);
848 gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
850 if (GST_CLOCK_TIME_IS_VALID (timestamp))
851 gst_object_sync_values (GST_OBJECT (self), timestamp);
853 GST_LOG_OBJECT (self,
854 "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %f",
855 GST_TIME_ARGS (timestamp), self->mask_position);
857 g_mutex_lock (&self->mask_mutex);
862 g_cond_wait (&self->mask_cond, &self->mask_mutex);
864 if (self->mask == NULL || self->shutdown) {
867 mask = gst_buffer_ref (self->mask);
869 g_mutex_unlock (&self->mask_mutex);
871 if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
874 /* Will blend inplace if buffer is writable */
875 outbuf = gst_buffer_make_writable (buffer);
876 gst_video_frame_map (&outframe, &self->vinfo, outbuf, GST_MAP_READWRITE);
877 gst_video_frame_map (&inframe, &self->vinfo, outbuf, GST_MAP_READ);
879 gst_video_frame_map (&maskframe, &self->minfo, mask, GST_MAP_READ);
881 switch (GST_VIDEO_INFO_FORMAT (&self->vinfo)) {
882 case GST_VIDEO_FORMAT_AYUV:
883 case GST_VIDEO_FORMAT_ARGB:
884 case GST_VIDEO_FORMAT_ABGR:
885 if (self->mask_bpp == 16)
886 gst_shape_wipe_blend_argb_16 (self, &inframe, &maskframe, &outframe);
888 gst_shape_wipe_blend_argb_8 (self, &inframe, &maskframe, &outframe);
890 case GST_VIDEO_FORMAT_BGRA:
891 case GST_VIDEO_FORMAT_RGBA:
892 if (self->mask_bpp == 16)
893 gst_shape_wipe_blend_bgra_16 (self, &inframe, &maskframe, &outframe);
895 gst_shape_wipe_blend_bgra_8 (self, &inframe, &maskframe, &outframe);
898 g_assert_not_reached ();
902 gst_video_frame_unmap (&outframe);
903 gst_video_frame_unmap (&inframe);
905 gst_video_frame_unmap (&maskframe);
907 gst_buffer_unref (mask);
909 ret = gst_pad_push (self->srcpad, outbuf);
910 if (G_UNLIKELY (ret != GST_FLOW_OK))
918 GST_ERROR_OBJECT (self, "No valid caps yet");
919 gst_buffer_unref (buffer);
920 return GST_FLOW_NOT_NEGOTIATED;
924 GST_DEBUG_OBJECT (self, "Shutting down");
925 gst_buffer_unref (buffer);
926 return GST_FLOW_FLUSHING;
930 GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
931 gst_buffer_unref (buffer);
932 gst_buffer_unref (mask);
937 if (ret != GST_FLOW_FLUSHING)
938 GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
939 gst_flow_get_name (ret));
945 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstObject * parent,
948 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
949 GstFlowReturn ret = GST_FLOW_OK;
951 g_mutex_lock (&self->mask_mutex);
952 GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
954 gst_buffer_replace (&self->mask, buffer);
955 g_cond_signal (&self->mask_cond);
956 g_mutex_unlock (&self->mask_mutex);
958 gst_buffer_unref (buffer);
963 static GstStateChangeReturn
964 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
966 GstShapeWipe *self = GST_SHAPE_WIPE (element);
967 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
969 switch (transition) {
970 case GST_STATE_CHANGE_READY_TO_PAUSED:
971 self->shutdown = FALSE;
973 case GST_STATE_CHANGE_PAUSED_TO_READY:
974 /* Unblock video sink chain function */
975 g_mutex_lock (&self->mask_mutex);
976 self->shutdown = TRUE;
977 g_cond_signal (&self->mask_cond);
978 g_mutex_unlock (&self->mask_mutex);
984 if (GST_ELEMENT_CLASS (parent_class)->change_state)
985 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
987 switch (transition) {
988 case GST_STATE_CHANGE_PAUSED_TO_READY:
989 gst_shape_wipe_reset (self);
999 gst_shape_wipe_video_sink_event (GstPad * pad, GstObject * parent,
1002 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1005 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1007 switch (GST_EVENT_TYPE (event)) {
1008 case GST_EVENT_CAPS:
1012 gst_event_parse_caps (event, &caps);
1013 ret = gst_shape_wipe_video_sink_setcaps (self, caps);
1014 gst_event_unref (event);
1017 case GST_EVENT_SEGMENT:
1021 gst_event_copy_segment (event, &seg);
1022 if (seg.format == GST_FORMAT_TIME) {
1023 GST_DEBUG_OBJECT (pad,
1024 "Got SEGMENT event in GST_FORMAT_TIME %" GST_PTR_FORMAT, &seg);
1025 self->segment = seg;
1027 gst_segment_init (&self->segment, GST_FORMAT_TIME);
1031 case GST_EVENT_FLUSH_STOP:
1032 gst_shape_wipe_reset_qos (self);
1035 ret = gst_pad_push_event (self->srcpad, event);
1043 gst_shape_wipe_mask_sink_event (GstPad * pad, GstObject * parent,
1046 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1048 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1050 switch (GST_EVENT_TYPE (event)) {
1051 case GST_EVENT_CAPS:
1055 gst_event_parse_caps (event, &caps);
1056 gst_shape_wipe_mask_sink_setcaps (self, caps);
1059 case GST_EVENT_FLUSH_STOP:
1060 g_mutex_lock (&self->mask_mutex);
1061 gst_buffer_replace (&self->mask, NULL);
1062 g_mutex_unlock (&self->mask_mutex);
1068 /* Dropping all events here */
1069 gst_event_unref (event);
1075 gst_shape_wipe_mask_sink_query (GstPad * pad, GstObject * parent,
1078 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1081 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
1082 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1084 switch (GST_QUERY_TYPE (query)) {
1085 case GST_QUERY_CAPS:
1087 GstCaps *filter, *caps;
1089 gst_query_parse_caps (query, &filter);
1090 caps = gst_shape_wipe_mask_sink_getcaps (self, pad, filter);
1091 gst_query_set_caps_result (query, caps);
1092 gst_caps_unref (caps);
1097 ret = gst_pad_query_default (pad, parent, query);
1106 gst_shape_wipe_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1108 GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1111 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1113 switch (GST_EVENT_TYPE (event)) {
1114 case GST_EVENT_QOS:{
1116 GstClockTimeDiff diff;
1117 GstClockTime timestamp;
1120 gst_event_parse_qos (event, &type, &proportion, &diff, ×tamp);
1122 gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1126 ret = gst_pad_push_event (self->video_sinkpad, event);
1134 plugin_init (GstPlugin * plugin)
1136 GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1137 "shapewipe element");
1139 if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1140 GST_TYPE_SHAPE_WIPE))
1146 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1149 "Shape Wipe transition filter",
1150 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)