audioecho: removed unused variable in set_property
[platform/upstream/gst-plugins-good.git] / gst / shapewipe / gstshapewipe.c
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
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_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));
165 }
166
167 static void
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
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
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
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
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
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 *
327 gst_shape_wipe_video_sink_getcaps (GstShapeWipe * self, GstPad * pad,
328     GstCaps * filter)
329 {
330   GstCaps *templ, *ret, *tmp;
331
332   if (gst_pad_has_current_caps (pad))
333     return gst_pad_get_current_caps (pad);
334
335   templ = gst_pad_get_pad_template_caps (pad);
336   tmp = gst_pad_peer_query_caps (self->srcpad, NULL);
337   if (tmp) {
338     ret = gst_caps_intersect (tmp, templ);
339     gst_caps_unref (templ);
340     gst_caps_unref (tmp);
341   } else {
342     ret = templ;
343   }
344
345   GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
346
347   if (gst_caps_is_empty (ret))
348     goto done;
349
350   tmp = gst_pad_peer_query_caps (pad, NULL);
351
352   GST_LOG_OBJECT (pad, "peerpad accepted caps: %" GST_PTR_FORMAT, tmp);
353   if (tmp) {
354     GstCaps *intersection;
355
356     intersection = gst_caps_intersect (tmp, ret);
357     gst_caps_unref (tmp);
358     gst_caps_unref (ret);
359     ret = intersection;
360   }
361
362   GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, tmp);
363
364   if (gst_caps_is_empty (ret))
365     goto done;
366
367   if (self->vinfo.height && self->vinfo.width) {
368     guint i, n;
369
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);
374
375       gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
376           G_TYPE_INT, self->vinfo.height, NULL);
377     }
378   }
379
380   tmp = gst_pad_peer_query_caps (self->mask_sinkpad, NULL);
381
382   GST_LOG_OBJECT (pad, "mask accepted caps: %" GST_PTR_FORMAT, tmp);
383   if (tmp) {
384     GstCaps *intersection, *tmp2;
385     guint i, n;
386
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);
391     tmp = intersection;
392
393     tmp = gst_caps_make_writable (tmp);
394     n = gst_caps_get_size (tmp);
395
396     for (i = 0; i < n; i++) {
397       GstStructure *s = gst_caps_get_structure (tmp, i);
398
399       gst_structure_remove_fields (s, "format", "framerate", NULL);
400       gst_structure_set_name (s, "video/x-raw");
401     }
402
403     intersection = gst_caps_intersect (tmp, ret);
404     gst_caps_unref (tmp);
405     gst_caps_unref (ret);
406     ret = intersection;
407   }
408 done:
409   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
410
411   return ret;
412 }
413
414 static gboolean
415 gst_shape_wipe_mask_sink_setcaps (GstShapeWipe * self, GstCaps * caps)
416 {
417   gboolean ret = TRUE;
418   gint width, height, bpp;
419   GstVideoInfo info;
420
421   GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
422
423   if (!gst_video_info_from_caps (&info, caps)) {
424     ret = FALSE;
425     goto done;
426   }
427
428   width = GST_VIDEO_INFO_WIDTH (&info);
429   height = GST_VIDEO_INFO_HEIGHT (&info);
430   bpp = GST_VIDEO_INFO_COMP_DEPTH (&info, 0);
431
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");
436     ret = FALSE;
437     goto done;
438   }
439
440   self->mask_bpp = bpp;
441   self->minfo = info;
442
443 done:
444   return ret;
445 }
446
447 static GstCaps *
448 gst_shape_wipe_mask_sink_getcaps (GstShapeWipe * self, GstPad * pad,
449     GstCaps * filter)
450 {
451   GstCaps *ret, *tmp, *tcaps;
452   guint i, n;
453
454   if (gst_pad_has_current_caps (pad))
455     return gst_pad_get_current_caps (pad);
456
457   tcaps = gst_pad_get_pad_template_caps (self->video_sinkpad);
458   tmp = gst_pad_peer_query_caps (self->video_sinkpad, NULL);
459   if (tmp) {
460     ret = gst_caps_intersect (tmp, tcaps);
461     gst_caps_unref (tcaps);
462     gst_caps_unref (tmp);
463   } else {
464     ret = tcaps;
465   }
466
467   GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
468
469   if (gst_caps_is_empty (ret))
470     goto done;
471
472   tmp = gst_pad_peer_query_caps (self->srcpad, NULL);
473   GST_LOG_OBJECT (pad, "srcpad accepted caps: %" GST_PTR_FORMAT, ret);
474
475   if (tmp) {
476     GstCaps *intersection;
477
478     intersection = gst_caps_intersect (ret, tmp);
479     gst_caps_unref (ret);
480     gst_caps_unref (tmp);
481     ret = intersection;
482   }
483
484   GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
485
486   if (gst_caps_is_empty (ret))
487     goto done;
488
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);
494     GstStructure *t;
495
496     gst_structure_set_name (s, "video/x-raw");
497     gst_structure_remove_fields (s, "format", "framerate", NULL);
498
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);
502
503     gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
504
505     t = gst_structure_copy (s);
506
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);
509
510     gst_caps_append_structure (tmp, t);
511   }
512   gst_caps_append (ret, tmp);
513
514   tmp = gst_pad_peer_query_caps (pad, NULL);
515   GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, tmp);
516
517   if (tmp) {
518     GstCaps *intersection;
519
520     intersection = gst_caps_intersect (tmp, ret);
521     gst_caps_unref (tmp);
522     gst_caps_unref (ret);
523     ret = intersection;
524   }
525
526 done:
527   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
528
529   return ret;
530 }
531
532 static GstCaps *
533 gst_shape_wipe_src_getcaps (GstPad * pad, GstCaps * filter)
534 {
535   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
536   GstCaps *templ, *ret, *tmp;
537
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);
542
543   templ = gst_pad_get_pad_template_caps (self->video_sinkpad);
544   tmp = gst_pad_peer_query_caps (self->video_sinkpad, NULL);
545   if (tmp) {
546     ret = gst_caps_intersect (tmp, templ);
547     gst_caps_unref (templ);
548     gst_caps_unref (tmp);
549   } else {
550     ret = templ;
551   }
552
553   GST_LOG_OBJECT (pad, "video sink accepted caps: %" GST_PTR_FORMAT, ret);
554
555   if (gst_caps_is_empty (ret))
556     goto done;
557
558   tmp = gst_pad_peer_query_caps (pad, NULL);
559   GST_LOG_OBJECT (pad, "peer accepted caps: %" GST_PTR_FORMAT, ret);
560   if (tmp) {
561     GstCaps *intersection;
562
563     intersection = gst_caps_intersect (tmp, ret);
564     gst_caps_unref (tmp);
565     gst_caps_unref (ret);
566     ret = intersection;
567   }
568
569   GST_LOG_OBJECT (pad, "intersection: %" GST_PTR_FORMAT, ret);
570
571   if (gst_caps_is_empty (ret))
572     goto done;
573
574   if (self->vinfo.height && self->vinfo.width) {
575     guint i, n;
576
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);
581
582       gst_structure_set (s, "width", G_TYPE_INT, self->vinfo.width, "height",
583           G_TYPE_INT, self->vinfo.height, NULL);
584     }
585   }
586
587   tmp = gst_pad_peer_query_caps (self->mask_sinkpad, NULL);
588   GST_LOG_OBJECT (pad, "mask sink accepted caps: %" GST_PTR_FORMAT, ret);
589   if (tmp) {
590     GstCaps *intersection, *tmp2;
591     guint i, n;
592
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);
597
598     tmp = gst_caps_make_writable (intersection);
599     n = gst_caps_get_size (tmp);
600
601     for (i = 0; i < n; i++) {
602       GstStructure *s = gst_caps_get_structure (tmp, i);
603
604       gst_structure_remove_fields (s, "format", "framerate", NULL);
605       gst_structure_set_name (s, "video/x-raw");
606     }
607
608     intersection = gst_caps_intersect (tmp, ret);
609     gst_caps_unref (tmp);
610     gst_caps_unref (ret);
611     ret = intersection;
612   }
613
614 done:
615
616   gst_object_unref (self);
617
618   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
619
620   return ret;
621 }
622
623 static gboolean
624 gst_shape_wipe_video_sink_query (GstPad * pad, GstObject * parent,
625     GstQuery * query)
626 {
627   GstShapeWipe *self = (GstShapeWipe *) parent;
628   gboolean ret;
629
630   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
631       gst_query_type_get_name (GST_QUERY_TYPE (query)));
632
633   switch (GST_QUERY_TYPE (query)) {
634     case GST_QUERY_CAPS:
635     {
636       GstCaps *filter, *caps;
637
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);
642       ret = TRUE;
643       break;
644     }
645     default:
646       ret = gst_pad_query_default (pad, parent, query);
647       break;
648   }
649
650   return ret;
651 }
652
653 static gboolean
654 gst_shape_wipe_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
655 {
656   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
657   gboolean ret;
658
659   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
660       gst_query_type_get_name (GST_QUERY_TYPE (query)));
661
662   switch (GST_QUERY_TYPE (query)) {
663     case GST_QUERY_CAPS:
664     {
665       GstCaps *filter, *caps;
666
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);
671       ret = TRUE;
672       break;
673     }
674     default:
675       ret = gst_pad_peer_query (self->video_sinkpad, query);
676       break;
677   }
678
679   return ret;
680 }
681
682 static void
683 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
684     GstClockTimeDiff diff, GstClockTime timestamp)
685 {
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;
691     else
692       self->earliest_time = timestamp + diff;
693   } else {
694     self->earliest_time = GST_CLOCK_TIME_NONE;
695   }
696   GST_OBJECT_UNLOCK (self);
697 }
698
699 static void
700 gst_shape_wipe_reset_qos (GstShapeWipe * self)
701 {
702   gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
703 }
704
705 static void
706 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
707     GstClockTime * time)
708 {
709   GST_OBJECT_LOCK (self);
710   *proportion = self->proportion;
711   *time = self->earliest_time;
712   GST_OBJECT_UNLOCK (self);
713 }
714
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 */
717 static gboolean
718 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
719 {
720   GstClockTime qostime, earliest_time;
721   gdouble proportion;
722
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");
726     return TRUE;
727   }
728
729   /* get latest QoS observation values */
730   gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
731
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");
735     return TRUE;
736   }
737
738   /* qos is done on running time */
739   qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
740       timestamp);
741
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));
745
746   if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
747     GST_DEBUG_OBJECT (self, "we are late, drop frame");
748     return FALSE;
749   }
750
751   GST_LOG_OBJECT (self, "process frame");
752   return TRUE;
753 }
754
755 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
756 static void \
757 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstVideoFrame * inframe, \
758     GstVideoFrame * maskframe, GstVideoFrame * outframe) \
759 { \
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); \
763   guint i, j; \
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; \
772   \
773   if (low < 0.0f) { \
774     high = 0.0f; \
775     low = 0.0f; \
776   } \
777   \
778   if (high > 1.0f) { \
779     low = 1.0f; \
780     high = 1.0f; \
781   } \
782   \
783   low_i = low * 65536; \
784   high_i = high * 65536; \
785   round_i = (high_i - low_i) >> 1; \
786   \
787   for (i = 0; i < height; i++) { \
788     for (j = 0; j < width; j++) { \
789       guint32 in = *mask << shift; \
790       \
791       if (in < low_i) { \
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 */ \
801       } else { \
802         guint32 val; \
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; \
806         \
807         output[a] = val;        /* A */ \
808         output[r] = input[r];   /* R */ \
809         output[g] = input[g];   /* G */ \
810         output[b] = input[b];   /* B */ \
811       } \
812       \
813       mask++; \
814       input += 4; \
815       output += 4; \
816     } \
817     mask += mask_increment; \
818   } \
819 }
820
821 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
822 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
823
824 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
825 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
826
827 static GstFlowReturn
828 gst_shape_wipe_video_sink_chain (GstPad * pad, GstObject * parent,
829     GstBuffer * buffer)
830 {
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;
836
837   if (G_UNLIKELY (GST_VIDEO_INFO_FORMAT (&self->vinfo) ==
838           GST_VIDEO_FORMAT_UNKNOWN))
839     goto not_negotiated;
840
841   timestamp = GST_BUFFER_TIMESTAMP (buffer);
842   timestamp =
843       gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
844
845   if (GST_CLOCK_TIME_IS_VALID (timestamp))
846     gst_object_sync_values (GST_OBJECT (self), timestamp);
847
848   GST_LOG_OBJECT (self,
849       "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %f",
850       GST_TIME_ARGS (timestamp), self->mask_position);
851
852   g_mutex_lock (&self->mask_mutex);
853   if (self->shutdown)
854     goto shutdown;
855
856   if (!self->mask)
857     g_cond_wait (&self->mask_cond, &self->mask_mutex);
858
859   if (self->mask == NULL || self->shutdown) {
860     goto shutdown;
861   } else {
862     mask = gst_buffer_ref (self->mask);
863   }
864   g_mutex_unlock (&self->mask_mutex);
865
866   if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
867     goto qos;
868
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);
873
874   gst_video_frame_map (&maskframe, &self->minfo, mask, GST_MAP_READ);
875
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);
882       else
883         gst_shape_wipe_blend_argb_8 (self, &inframe, &maskframe, &outframe);
884       break;
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);
889       else
890         gst_shape_wipe_blend_bgra_8 (self, &inframe, &maskframe, &outframe);
891       break;
892     default:
893       g_assert_not_reached ();
894       break;
895   }
896
897   gst_video_frame_unmap (&outframe);
898   gst_video_frame_unmap (&inframe);
899
900   gst_video_frame_unmap (&maskframe);
901
902   gst_buffer_unref (mask);
903
904   ret = gst_pad_push (self->srcpad, outbuf);
905   if (G_UNLIKELY (ret != GST_FLOW_OK))
906     goto push_failed;
907
908   return ret;
909
910   /* Errors */
911 not_negotiated:
912   {
913     GST_ERROR_OBJECT (self, "No valid caps yet");
914     gst_buffer_unref (buffer);
915     return GST_FLOW_NOT_NEGOTIATED;
916   }
917 shutdown:
918   {
919     GST_DEBUG_OBJECT (self, "Shutting down");
920     gst_buffer_unref (buffer);
921     return GST_FLOW_FLUSHING;
922   }
923 qos:
924   {
925     GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
926     gst_buffer_unref (buffer);
927     gst_buffer_unref (mask);
928     return GST_FLOW_OK;
929   }
930 push_failed:
931   {
932     if (ret != GST_FLOW_FLUSHING)
933       GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
934           gst_flow_get_name (ret));
935     return ret;
936   }
937 }
938
939 static GstFlowReturn
940 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstObject * parent,
941     GstBuffer * buffer)
942 {
943   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
944   GstFlowReturn ret = GST_FLOW_OK;
945
946   g_mutex_lock (&self->mask_mutex);
947   GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
948
949   gst_buffer_replace (&self->mask, buffer);
950   g_cond_signal (&self->mask_cond);
951   g_mutex_unlock (&self->mask_mutex);
952
953   gst_buffer_unref (buffer);
954
955   return ret;
956 }
957
958 static GstStateChangeReturn
959 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
960 {
961   GstShapeWipe *self = GST_SHAPE_WIPE (element);
962   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
963
964   switch (transition) {
965     case GST_STATE_CHANGE_READY_TO_PAUSED:
966       self->shutdown = FALSE;
967       break;
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);
974       break;
975     default:
976       break;
977   }
978
979   if (GST_ELEMENT_CLASS (parent_class)->change_state)
980     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
981
982   switch (transition) {
983     case GST_STATE_CHANGE_PAUSED_TO_READY:
984       gst_shape_wipe_reset (self);
985       break;
986     default:
987       break;
988   }
989
990   return ret;
991 }
992
993 static gboolean
994 gst_shape_wipe_video_sink_event (GstPad * pad, GstObject * parent,
995     GstEvent * event)
996 {
997   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
998   gboolean ret;
999
1000   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1001
1002   switch (GST_EVENT_TYPE (event)) {
1003     case GST_EVENT_CAPS:
1004     {
1005       GstCaps *caps;
1006
1007       gst_event_parse_caps (event, &caps);
1008       ret = gst_shape_wipe_video_sink_setcaps (self, caps);
1009       gst_event_unref (event);
1010       break;
1011     }
1012     case GST_EVENT_SEGMENT:
1013     {
1014       GstSegment seg;
1015
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;
1021       } else {
1022         gst_segment_init (&self->segment, GST_FORMAT_TIME);
1023       }
1024     }
1025       /* fall through */
1026     case GST_EVENT_FLUSH_STOP:
1027       gst_shape_wipe_reset_qos (self);
1028       /* fall through */
1029     default:
1030       ret = gst_pad_push_event (self->srcpad, event);
1031       break;
1032   }
1033
1034   return ret;
1035 }
1036
1037 static gboolean
1038 gst_shape_wipe_mask_sink_event (GstPad * pad, GstObject * parent,
1039     GstEvent * event)
1040 {
1041   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1042
1043   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1044
1045   switch (GST_EVENT_TYPE (event)) {
1046     case GST_EVENT_CAPS:
1047     {
1048       GstCaps *caps;
1049
1050       gst_event_parse_caps (event, &caps);
1051       gst_shape_wipe_mask_sink_setcaps (self, caps);
1052       break;
1053     }
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);
1058       break;
1059     default:
1060       break;
1061   }
1062
1063   /* Dropping all events here */
1064   gst_event_unref (event);
1065
1066   return TRUE;
1067 }
1068
1069 static gboolean
1070 gst_shape_wipe_mask_sink_query (GstPad * pad, GstObject * parent,
1071     GstQuery * query)
1072 {
1073   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1074   gboolean ret;
1075
1076   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
1077       gst_query_type_get_name (GST_QUERY_TYPE (query)));
1078
1079   switch (GST_QUERY_TYPE (query)) {
1080     case GST_QUERY_CAPS:
1081     {
1082       GstCaps *filter, *caps;
1083
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);
1088       ret = TRUE;
1089       break;
1090     }
1091     default:
1092       ret = gst_pad_query_default (pad, parent, query);
1093       break;
1094   }
1095
1096   return ret;
1097 }
1098
1099
1100 static gboolean
1101 gst_shape_wipe_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1102 {
1103   GstShapeWipe *self = GST_SHAPE_WIPE (parent);
1104   gboolean ret;
1105
1106   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1107
1108   switch (GST_EVENT_TYPE (event)) {
1109     case GST_EVENT_QOS:{
1110       GstQOSType type;
1111       GstClockTimeDiff diff;
1112       GstClockTime timestamp;
1113       gdouble proportion;
1114
1115       gst_event_parse_qos (event, &type, &proportion, &diff, &timestamp);
1116
1117       gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1118     }
1119       /* fall through */
1120     default:
1121       ret = gst_pad_push_event (self->video_sinkpad, event);
1122       break;
1123   }
1124
1125   return ret;
1126 }
1127
1128 static gboolean
1129 plugin_init (GstPlugin * plugin)
1130 {
1131   GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1132       "shapewipe element");
1133
1134   if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1135           GST_TYPE_SHAPE_WIPE))
1136     return FALSE;
1137
1138   return TRUE;
1139 }
1140
1141 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1142     GST_VERSION_MINOR,
1143     shapewipe,
1144     "Shape Wipe transition filter",
1145     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)