1 /* GStreamer
2  * Copyright (C) 2009,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
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.
8  *
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.
13  *
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.
18  */
19 
20 /**
21  * SECTION:element-shapewipe
22  *
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.
27  *
28  * Transition bitmaps can be downloaded from the
29  * <ulink url="http://cinelerra.org/transitions.php">Cinelerra transition</ulink>
30  * page.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
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.
37  * </refsect2>
38  */
39 
40 
41 #ifdef HAVE_CONFIG_H
42 #  include "config.h"
43 #endif
44 
45 #include <string.h>
46 
47 #include <gst/gst.h>
48 
49 #include "gstshapewipe.h"
50 
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);
56 
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,
62     GstClockTime * time);
63 
64 static GstStateChangeReturn gst_shape_wipe_change_state (GstElement * element,
65     GstStateChange transition);
66 
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,
72     GstCaps * caps);
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,
82     GstCaps * caps);
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,
88     GstEvent * event);
89 static GstCaps *gst_shape_wipe_src_getcaps (GstPad * pad, GstCaps * filter);
90 static gboolean gst_shape_wipe_src_query (GstPad * pad, GstObject * parent,
91     GstQuery * query);
92 
93 enum
94 {
95   PROP_0,
96   PROP_POSITION,
97   PROP_BORDER
98 };
99 
100 #define DEFAULT_POSITION 0.0
101 #define DEFAULT_BORDER 0.0
102 
103 static GstStaticPadTemplate video_sink_pad_template =
104 GST_STATIC_PAD_TEMPLATE ("video_sink",
105     GST_PAD_SINK,
106     GST_PAD_ALWAYS,
107     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, ARGB, BGRA, ABGR, RGBA }")));
108 
109 static GstStaticPadTemplate mask_sink_pad_template =
110     GST_STATIC_PAD_TEMPLATE ("mask_sink",
111     GST_PAD_SINK,
112     GST_PAD_ALWAYS,
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"));
120 
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 }")));
124 
125 GST_DEBUG_CATEGORY_STATIC (gst_shape_wipe_debug);
126 #define GST_CAT_DEFAULT gst_shape_wipe_debug
127 
128 #define gst_shape_wipe_parent_class parent_class
129 G_DEFINE_TYPE (GstShapeWipe, gst_shape_wipe, GST_TYPE_ELEMENT);
130 
131 static void
gst_shape_wipe_class_init(GstShapeWipeClass * klass)132 gst_shape_wipe_class_init (GstShapeWipeClass * klass)
133 {
134   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
136 
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;
140 
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));
149 
150   gstelement_class->change_state =
151       GST_DEBUG_FUNCPTR (gst_shape_wipe_change_state);
152 
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>");
158 
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,
164       &src_pad_template);
165 }
166 
167 static void
gst_shape_wipe_init(GstShapeWipe * self)168 gst_shape_wipe_init (GstShapeWipe * self)
169 {
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);
179 
180   self->mask_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);
189 
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);
196 
197   g_mutex_init (&self->mask_mutex);
198   g_cond_init (&self->mask_cond);
199 
200   gst_shape_wipe_reset (self);
201 }
202 
203 static void
gst_shape_wipe_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)204 gst_shape_wipe_get_property (GObject * object, guint prop_id,
205     GValue * value, GParamSpec * pspec)
206 {
207   GstShapeWipe *self = GST_SHAPE_WIPE (object);
208 
209   switch (prop_id) {
210     case PROP_POSITION:
211       g_value_set_float (value, self->mask_position);
212       break;
213     case PROP_BORDER:
214       g_value_set_float (value, self->mask_border);
215       break;
216     default:
217       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218       break;
219   }
220 }
221 
222 static void
gst_shape_wipe_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)223 gst_shape_wipe_set_property (GObject * object, guint prop_id,
224     const GValue * value, GParamSpec * pspec)
225 {
226   GstShapeWipe *self = GST_SHAPE_WIPE (object);
227 
228   switch (prop_id) {
229     case PROP_POSITION:{
230       gfloat f = g_value_get_float (value);
231 
232       GST_LOG_OBJECT (self, "Setting mask position: %f", f);
233       self->mask_position = f;
234       break;
235     }
236     case PROP_BORDER:{
237       gfloat f = g_value_get_float (value);
238 
239       GST_LOG_OBJECT (self, "Setting mask border: %f", f);
240       self->mask_border = f;
241       break;
242     }
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246   }
247 }
248 
249 static void
gst_shape_wipe_finalize(GObject * object)250 gst_shape_wipe_finalize (GObject * object)
251 {
252   GstShapeWipe *self = GST_SHAPE_WIPE (object);
253 
254   gst_shape_wipe_reset (self);
255 
256   g_cond_clear (&self->mask_cond);
257   g_mutex_clear (&self->mask_mutex);
258 
259   G_OBJECT_CLASS (parent_class)->finalize (object);
260 }
261 
262 static void
gst_shape_wipe_reset(GstShapeWipe * self)263 gst_shape_wipe_reset (GstShapeWipe * self)
264 {
265   GST_DEBUG_OBJECT (self, "Resetting internal state");
266 
267   if (self->mask)
268     gst_buffer_unref (self->mask);
269   self->mask = NULL;
270 
271   g_mutex_lock (&self->mask_mutex);
272   g_cond_signal (&self->mask_cond);
273   g_mutex_unlock (&self->mask_mutex);
274 
275   gst_video_info_init (&self->vinfo);
276   gst_video_info_init (&self->minfo);
277   self->mask_bpp = 0;
278 
279   gst_segment_init (&self->segment, GST_FORMAT_TIME);
280 
281   gst_shape_wipe_reset_qos (self);
282   self->frame_duration = 0;
283 }
284 
285 static gboolean
gst_shape_wipe_video_sink_setcaps(GstShapeWipe * self,GstCaps * caps)286 gst_shape_wipe_video_sink_setcaps (GstShapeWipe * self, GstCaps * caps)
287 {
288   gboolean ret = TRUE;
289   GstVideoInfo info;
290 
291   GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
292 
293   if (!gst_video_info_from_caps (&info, caps))
294     goto invalid_caps;
295 
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);
299     if (self->mask)
300       gst_buffer_unref (self->mask);
301     self->mask = NULL;
302     g_mutex_unlock (&self->mask_mutex);
303   }
304 
305 
306   if (info.fps_n != 0)
307     self->frame_duration =
308         gst_util_uint64_scale (GST_SECOND, info.fps_d, info.fps_n);
309   else
310     self->frame_duration = 0;
311 
312   self->vinfo = info;
313 
314   ret = gst_pad_set_caps (self->srcpad, caps);
315 
316   return ret;
317 
318   /* ERRORS */
319 invalid_caps:
320   {
321     GST_ERROR_OBJECT (self, "Invalid caps");
322     return FALSE;
323   }
324 }
325 
326 static GstCaps *
gst_shape_wipe_video_sink_getcaps(GstShapeWipe * self,GstPad * pad,GstCaps * filter)327 gst_shape_wipe_video_sink_getcaps (GstShapeWipe * self, GstPad * pad,
328     GstCaps * filter)
329 {
330   GstCaps *templ, *ret, *tmp;
331 
332   ret = gst_pad_get_current_caps (pad);
333   if (ret != NULL)
334     return ret;
335 
336   templ = gst_pad_get_pad_template_caps (pad);
337   tmp = gst_pad_peer_query_caps (self->srcpad, NULL);
338   if (tmp) {
339     ret = gst_caps_intersect (tmp, templ);
340     gst_caps_unref (templ);
341     gst_caps_unref (tmp);
342   } else {
343     ret = templ;
344   }
345 
346   GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
347 
348   if (gst_caps_is_empty (ret))
349     goto done;
350 
351   tmp = gst_pad_peer_query_caps (pad, NULL);
352 
353   GST_LOG_OBJECT (pad, "peerpad accepted caps: %" GST_PTR_FORMAT, tmp);
354   if (tmp) {
355     GstCaps *intersection;
356 
357     intersection = gst_caps_intersect (tmp, ret);
358     gst_caps_unref (tmp);
359     gst_caps_unref (ret);
360     ret = intersection;
361   }
362 
363   GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, tmp);
364 
365   if (gst_caps_is_empty (ret))
366     goto done;
367 
368   if (self->vinfo.height && self->vinfo.width) {
369     guint i, n;
370 
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);
375 
376       gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
377           G_TYPE_INT, self->vinfo.height, NULL);
378     }
379   }
380 
381   tmp = gst_pad_peer_query_caps (self->mask_sinkpad, NULL);
382 
383   GST_LOG_OBJECT (pad, "mask accepted caps: %" GST_PTR_FORMAT, tmp);
384   if (tmp) {
385     GstCaps *intersection, *tmp2;
386     guint i, n;
387 
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);
392     tmp = intersection;
393 
394     tmp = gst_caps_make_writable (tmp);
395     n = gst_caps_get_size (tmp);
396 
397     for (i = 0; i < n; i++) {
398       GstStructure *s = gst_caps_get_structure (tmp, i);
399 
400       gst_structure_remove_fields (s, "format", "framerate", NULL);
401       gst_structure_set_name (s, "video/x-raw");
402     }
403 
404     intersection = gst_caps_intersect (tmp, ret);
405     gst_caps_unref (tmp);
406     gst_caps_unref (ret);
407     ret = intersection;
408   }
409 done:
410   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
411 
412   return ret;
413 }
414 
415 static gboolean
gst_shape_wipe_mask_sink_setcaps(GstShapeWipe * self,GstCaps * caps)416 gst_shape_wipe_mask_sink_setcaps (GstShapeWipe * self, GstCaps * caps)
417 {
418   gboolean ret = TRUE;
419   gint width, height, bpp;
420   GstVideoInfo info;
421 
422   GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
423 
424   if (!gst_video_info_from_caps (&info, caps)) {
425     ret = FALSE;
426     goto done;
427   }
428 
429   width = GST_VIDEO_INFO_WIDTH (&info);
430   height = GST_VIDEO_INFO_HEIGHT (&info);
431   bpp = GST_VIDEO_INFO_COMP_DEPTH (&info, 0);
432 
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");
437     ret = FALSE;
438     goto done;
439   }
440 
441   self->mask_bpp = bpp;
442   self->minfo = info;
443 
444 done:
445   return ret;
446 }
447 
448 static GstCaps *
gst_shape_wipe_mask_sink_getcaps(GstShapeWipe * self,GstPad * pad,GstCaps * filter)449 gst_shape_wipe_mask_sink_getcaps (GstShapeWipe * self, GstPad * pad,
450     GstCaps * filter)
451 {
452   GstCaps *ret, *tmp, *tcaps;
453   guint i, n;
454 
455   ret = gst_pad_get_current_caps (pad);
456   if (ret != NULL)
457     return ret;
458 
459   tcaps = gst_pad_get_pad_template_caps (self->video_sinkpad);
460   tmp = gst_pad_peer_query_caps (self->video_sinkpad, NULL);
461   if (tmp) {
462     ret = gst_caps_intersect (tmp, tcaps);
463     gst_caps_unref (tcaps);
464     gst_caps_unref (tmp);
465   } else {
466     ret = tcaps;
467   }
468 
469   GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
470 
471   if (gst_caps_is_empty (ret))
472     goto done;
473 
474   tmp = gst_pad_peer_query_caps (self->srcpad, NULL);
475   GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
476 
477   if (tmp) {
478     GstCaps *intersection;
479 
480     intersection = gst_caps_intersect (ret, tmp);
481     gst_caps_unref (ret);
482     gst_caps_unref (tmp);
483     ret = intersection;
484   }
485 
486   GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
487 
488   if (gst_caps_is_empty (ret))
489     goto done;
490 
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);
496     GstStructure *t;
497 
498     gst_structure_set_name (s, "video/x-raw");
499     gst_structure_remove_fields (s, "format", "framerate", NULL);
500 
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);
504 
505     gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
506 
507     t = gst_structure_copy (s);
508 
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);
511 
512     gst_caps_append_structure (tmp, t);
513   }
514   gst_caps_append (ret, tmp);
515 
516   tmp = gst_pad_peer_query_caps (pad, NULL);
517   GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, tmp);
518 
519   if (tmp) {
520     GstCaps *intersection;
521 
522     intersection = gst_caps_intersect (tmp, ret);
523     gst_caps_unref (tmp);
524     gst_caps_unref (ret);
525     ret = intersection;
526   }
527 
528 done:
529   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
530 
531   return ret;
532 }
533 
534 static GstCaps *
gst_shape_wipe_src_getcaps(GstPad * pad,GstCaps * filter)535 gst_shape_wipe_src_getcaps (GstPad * pad, GstCaps * filter)
536 {
537   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
538   GstCaps *templ, *ret, *tmp;
539 
540   ret = gst_pad_get_current_caps (pad);
541   if (ret != NULL)
542     return ret;
543 
544   ret = gst_pad_get_current_caps (self->video_sinkpad);
545   if (ret != NULL)
546     return ret;
547 
548   templ = gst_pad_get_pad_template_caps (self->video_sinkpad);
549   tmp = gst_pad_peer_query_caps (self->video_sinkpad, NULL);
550   if (tmp) {
551     ret = gst_caps_intersect (tmp, templ);
552     gst_caps_unref (templ);
553     gst_caps_unref (tmp);
554   } else {
555     ret = templ;
556   }
557 
558   GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
559 
560   if (gst_caps_is_empty (ret))
561     goto done;
562 
563   tmp = gst_pad_peer_query_caps (pad, NULL);
564   GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, ret);
565   if (tmp) {
566     GstCaps *intersection;
567 
568     intersection = gst_caps_intersect (tmp, ret);
569     gst_caps_unref (tmp);
570     gst_caps_unref (ret);
571     ret = intersection;
572   }
573 
574   GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
575 
576   if (gst_caps_is_empty (ret))
577     goto done;
578 
579   if (self->vinfo.height && self->vinfo.width) {
580     guint i, n;
581 
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);
586 
587       gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
588           G_TYPE_INT, self->vinfo.height, NULL);
589     }
590   }
591 
592   tmp = gst_pad_peer_query_caps (self->mask_sinkpad, NULL);
593   GST_LOG_OBJECT (pad, "mask sink accepted caps: %" GST_PTR_FORMAT, ret);
594   if (tmp) {
595     GstCaps *intersection, *tmp2;
596     guint i, n;
597 
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);
602 
603     tmp = gst_caps_make_writable (intersection);
604     n = gst_caps_get_size (tmp);
605 
606     for (i = 0; i < n; i++) {
607       GstStructure *s = gst_caps_get_structure (tmp, i);
608 
609       gst_structure_remove_fields (s, "format", "framerate", NULL);
610       gst_structure_set_name (s, "video/x-raw");
611     }
612 
613     intersection = gst_caps_intersect (tmp, ret);
614     gst_caps_unref (tmp);
615     gst_caps_unref (ret);
616     ret = intersection;
617   }
618 
619 done:
620 
621   gst_object_unref (self);
622 
623   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
624 
625   return ret;
626 }
627 
628 static gboolean
gst_shape_wipe_video_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)629 gst_shape_wipe_video_sink_query (GstPad * pad, GstObject * parent,
630     GstQuery * query)
631 {
632   GstShapeWipe *self = (GstShapeWipe *) parent;
633   gboolean ret;
634 
635   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
636       gst_query_type_get_name (GST_QUERY_TYPE (query)));
637 
638   switch (GST_QUERY_TYPE (query)) {
639     case GST_QUERY_CAPS:
640     {
641       GstCaps *filter, *caps;
642 
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);
647       ret = TRUE;
648       break;
649     }
650     default:
651       ret = gst_pad_query_default (pad, parent, query);
652       break;
653   }
654 
655   return ret;
656 }
657 
658 static gboolean
gst_shape_wipe_src_query(GstPad * pad,GstObject * parent,GstQuery * query)659 gst_shape_wipe_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
660 {
661   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
662   gboolean ret;
663 
664   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
665       gst_query_type_get_name (GST_QUERY_TYPE (query)));
666 
667   switch (GST_QUERY_TYPE (query)) {
668     case GST_QUERY_CAPS:
669     {
670       GstCaps *filter, *caps;
671 
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);
676       ret = TRUE;
677       break;
678     }
679     default:
680       ret = gst_pad_peer_query (self->video_sinkpad, query);
681       break;
682   }
683 
684   return ret;
685 }
686 
687 static void
gst_shape_wipe_update_qos(GstShapeWipe * self,gdouble proportion,GstClockTimeDiff diff,GstClockTime timestamp)688 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
689     GstClockTimeDiff diff, GstClockTime timestamp)
690 {
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;
696     else
697       self->earliest_time = timestamp + diff;
698   } else {
699     self->earliest_time = GST_CLOCK_TIME_NONE;
700   }
701   GST_OBJECT_UNLOCK (self);
702 }
703 
704 static void
gst_shape_wipe_reset_qos(GstShapeWipe * self)705 gst_shape_wipe_reset_qos (GstShapeWipe * self)
706 {
707   gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
708 }
709 
710 static void
gst_shape_wipe_read_qos(GstShapeWipe * self,gdouble * proportion,GstClockTime * time)711 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
712     GstClockTime * time)
713 {
714   GST_OBJECT_LOCK (self);
715   *proportion = self->proportion;
716   *time = self->earliest_time;
717   GST_OBJECT_UNLOCK (self);
718 }
719 
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 */
722 static gboolean
gst_shape_wipe_do_qos(GstShapeWipe * self,GstClockTime timestamp)723 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
724 {
725   GstClockTime qostime, earliest_time;
726   gdouble proportion;
727 
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");
731     return TRUE;
732   }
733 
734   /* get latest QoS observation values */
735   gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
736 
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");
740     return TRUE;
741   }
742 
743   /* qos is done on running time */
744   qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
745       timestamp);
746 
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));
750 
751   if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
752     GST_DEBUG_OBJECT (self, "we are late, drop frame");
753     return FALSE;
754   }
755 
756   GST_LOG_OBJECT (self, "process frame");
757   return TRUE;
758 }
759 
760 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
761 static void \
762 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstVideoFrame * inframe, \
763     GstVideoFrame * maskframe, GstVideoFrame * outframe) \
764 { \
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); \
768   guint i, j; \
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; \
777   \
778   if (low < 0.0f) { \
779     high = 0.0f; \
780     low = 0.0f; \
781   } \
782   \
783   if (high > 1.0f) { \
784     low = 1.0f; \
785     high = 1.0f; \
786   } \
787   \
788   low_i = low * 65536; \
789   high_i = high * 65536; \
790   round_i = (high_i - low_i) >> 1; \
791   \
792   for (i = 0; i < height; i++) { \
793     for (j = 0; j < width; j++) { \
794       guint32 in = *mask << shift; \
795       \
796       if (in < low_i) { \
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 */ \
806       } else { \
807         guint32 val; \
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; \
811         \
812         output[a] = val;        /* A */ \
813         output[r] = input[r];   /* R */ \
814         output[g] = input[g];   /* G */ \
815         output[b] = input[b];   /* B */ \
816       } \
817       \
818       mask++; \
819       input += 4; \
820       output += 4; \
821     } \
822     mask += mask_increment; \
823   } \
824 }
825 
826 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
827 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
828 
829 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
830 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
831 
832 static GstFlowReturn
gst_shape_wipe_video_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)833 gst_shape_wipe_video_sink_chain (GstPad * pad, GstObject * parent,
834     GstBuffer * buffer)
835 {
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;
841 
842   if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&self->vinfo) ==
843           GST_VIDEO_FORMAT_UNKNOWN))
844     goto not_negotiated;
845 
846   timestamp = GST_BUFFER_TIMESTAMP (buffer);
847   timestamp =
848       gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
849 
850   if (GST_CLOCK_TIME_IS_VALID (timestamp))
851     gst_object_sync_values (GST_OBJECT (self), timestamp);
852 
853   GST_LOG_OBJECT (self,
854       "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %f",
855       GST_TIME_ARGS (timestamp), self->mask_position);
856 
857   g_mutex_lock (&self->mask_mutex);
858   if (self->shutdown)
859     goto shutdown;
860 
861   if (!self->mask)
862     g_cond_wait (&self->mask_cond, &self->mask_mutex);
863 
864   if (self->mask == NULL || self->shutdown) {
865     goto shutdown;
866   } else {
867     mask = gst_buffer_ref (self->mask);
868   }
869   g_mutex_unlock (&self->mask_mutex);
870 
871   if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
872     goto qos;
873 
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);
878 
879   gst_video_frame_map (&maskframe, &self->minfo, mask, GST_MAP_READ);
880 
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);
887       else
888         gst_shape_wipe_blend_argb_8 (self, &inframe, &maskframe, &outframe);
889       break;
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);
894       else
895         gst_shape_wipe_blend_bgra_8 (self, &inframe, &maskframe, &outframe);
896       break;
897     default:
898       g_assert_not_reached ();
899       break;
900   }
901 
902   gst_video_frame_unmap (&outframe);
903   gst_video_frame_unmap (&inframe);
904 
905   gst_video_frame_unmap (&maskframe);
906 
907   gst_buffer_unref (mask);
908 
909   ret = gst_pad_push (self->srcpad, outbuf);
910   if (G_UNLIKELY (ret != GST_FLOW_OK))
911     goto push_failed;
912 
913   return ret;
914 
915   /* Errors */
916 not_negotiated:
917   {
918     GST_ERROR_OBJECT (self, "No valid caps yet");
919     gst_buffer_unref (buffer);
920     return GST_FLOW_NOT_NEGOTIATED;
921   }
922 shutdown:
923   {
924     GST_DEBUG_OBJECT (self, "Shutting down");
925     gst_buffer_unref (buffer);
926     return GST_FLOW_FLUSHING;
927   }
928 qos:
929   {
930     GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
931     gst_buffer_unref (buffer);
932     gst_buffer_unref (mask);
933     return GST_FLOW_OK;
934   }
935 push_failed:
936   {
937     if (ret != GST_FLOW_FLUSHING)
938       GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
939           gst_flow_get_name (ret));
940     return ret;
941   }
942 }
943 
944 static GstFlowReturn
gst_shape_wipe_mask_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)945 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstObject * parent,
946     GstBuffer * buffer)
947 {
948   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
949   GstFlowReturn ret = GST_FLOW_OK;
950 
951   g_mutex_lock (&self->mask_mutex);
952   GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
953 
954   gst_buffer_replace (&self->mask, buffer);
955   g_cond_signal (&self->mask_cond);
956   g_mutex_unlock (&self->mask_mutex);
957 
958   gst_buffer_unref (buffer);
959 
960   return ret;
961 }
962 
963 static GstStateChangeReturn
gst_shape_wipe_change_state(GstElement * element,GstStateChange transition)964 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
965 {
966   GstShapeWipe *self = GST_SHAPE_WIPE (element);
967   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
968 
969   switch (transition) {
970     case GST_STATE_CHANGE_READY_TO_PAUSED:
971       self->shutdown = FALSE;
972       break;
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);
979       break;
980     default:
981       break;
982   }
983 
984   if (GST_ELEMENT_CLASS (parent_class)->change_state)
985     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
986 
987   switch (transition) {
988     case GST_STATE_CHANGE_PAUSED_TO_READY:
989       gst_shape_wipe_reset (self);
990       break;
991     default:
992       break;
993   }
994 
995   return ret;
996 }
997 
998 static gboolean
gst_shape_wipe_video_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)999 gst_shape_wipe_video_sink_event (GstPad * pad, GstObject * parent,
1000     GstEvent * event)
1001 {
1002   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1003   gboolean ret;
1004 
1005   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1006 
1007   switch (GST_EVENT_TYPE (event)) {
1008     case GST_EVENT_CAPS:
1009     {
1010       GstCaps *caps;
1011 
1012       gst_event_parse_caps (event, &caps);
1013       ret = gst_shape_wipe_video_sink_setcaps (self, caps);
1014       gst_event_unref (event);
1015       break;
1016     }
1017     case GST_EVENT_SEGMENT:
1018     {
1019       GstSegment seg;
1020 
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;
1026       } else {
1027         gst_segment_init (&self->segment, GST_FORMAT_TIME);
1028       }
1029     }
1030       /* fall through */
1031     case GST_EVENT_FLUSH_STOP:
1032       gst_shape_wipe_reset_qos (self);
1033       /* fall through */
1034     default:
1035       ret = gst_pad_push_event (self->srcpad, event);
1036       break;
1037   }
1038 
1039   return ret;
1040 }
1041 
1042 static gboolean
gst_shape_wipe_mask_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)1043 gst_shape_wipe_mask_sink_event (GstPad * pad, GstObject * parent,
1044     GstEvent * event)
1045 {
1046   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1047 
1048   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1049 
1050   switch (GST_EVENT_TYPE (event)) {
1051     case GST_EVENT_CAPS:
1052     {
1053       GstCaps *caps;
1054 
1055       gst_event_parse_caps (event, &caps);
1056       gst_shape_wipe_mask_sink_setcaps (self, caps);
1057       break;
1058     }
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);
1063       break;
1064     default:
1065       break;
1066   }
1067 
1068   /* Dropping all events here */
1069   gst_event_unref (event);
1070 
1071   return TRUE;
1072 }
1073 
1074 static gboolean
gst_shape_wipe_mask_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)1075 gst_shape_wipe_mask_sink_query (GstPad * pad, GstObject * parent,
1076     GstQuery * query)
1077 {
1078   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1079   gboolean ret;
1080 
1081   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
1082       gst_query_type_get_name (GST_QUERY_TYPE (query)));
1083 
1084   switch (GST_QUERY_TYPE (query)) {
1085     case GST_QUERY_CAPS:
1086     {
1087       GstCaps *filter, *caps;
1088 
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);
1093       ret = TRUE;
1094       break;
1095     }
1096     default:
1097       ret = gst_pad_query_default (pad, parent, query);
1098       break;
1099   }
1100 
1101   return ret;
1102 }
1103 
1104 
1105 static gboolean
gst_shape_wipe_src_event(GstPad * pad,GstObject * parent,GstEvent * event)1106 gst_shape_wipe_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1107 {
1108   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1109   gboolean ret;
1110 
1111   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1112 
1113   switch (GST_EVENT_TYPE (event)) {
1114     case GST_EVENT_QOS:{
1115       GstQOSType type;
1116       GstClockTimeDiff diff;
1117       GstClockTime timestamp;
1118       gdouble proportion;
1119 
1120       gst_event_parse_qos (event, &type, &proportion, &diff, &timestamp);
1121 
1122       gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1123     }
1124       /* fall through */
1125     default:
1126       ret = gst_pad_push_event (self->video_sinkpad, event);
1127       break;
1128   }
1129 
1130   return ret;
1131 }
1132 
1133 static gboolean
plugin_init(GstPlugin * plugin)1134 plugin_init (GstPlugin * plugin)
1135 {
1136   GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1137       "shapewipe element");
1138 
1139   if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1140           GST_TYPE_SHAPE_WIPE))
1141     return FALSE;
1142 
1143   return TRUE;
1144 }
1145 
1146 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1147     GST_VERSION_MINOR,
1148     shapewipe,
1149     "Shape Wipe transition filter",
1150     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
1151