[MOVED FROM BAD 28/29] shapewipe: Scale mask alpha values by the source alpha values
[platform/upstream/gstreamer.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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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 -v videotestsrc ! video/x-raw-yuv,width=640,height=480 ! shapewipe position=0.5 name=shape ! videomixer name=mixer ! ffmpegcolorspace ! autovideosink     filesrc location=mask.png ! typefind ! decodebin2 ! ffmpegcolorspace ! videoscale ! queue ! shape.mask_sink    videotestsrc pattern=snow ! video/x-raw-yuv,width=640,height=480 ! queue ! mixer.
36  * ]| This pipeline adds the transition from mask.png with position 0.5 to an SMPTE test screen and snow.
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 #include <gst/controller/gstcontroller.h>
49
50 #include "gstshapewipe.h"
51
52 static void gst_shape_wipe_finalize (GObject * object);
53 static void gst_shape_wipe_get_property (GObject * object, guint prop_id,
54     GValue * value, GParamSpec * pspec);
55 static void gst_shape_wipe_set_property (GObject * object, guint prop_id,
56     const GValue * value, GParamSpec * pspec);
57
58 static void gst_shape_wipe_reset (GstShapeWipe * self);
59 static void gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
60     GstClockTimeDiff diff, GstClockTime time);
61 static void gst_shape_wipe_reset_qos (GstShapeWipe * self);
62 static void gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
63     GstClockTime * time);
64
65 static GstStateChangeReturn gst_shape_wipe_change_state (GstElement * element,
66     GstStateChange transition);
67
68 static GstFlowReturn gst_shape_wipe_video_sink_chain (GstPad * pad,
69     GstBuffer * buffer);
70 static gboolean gst_shape_wipe_video_sink_event (GstPad * pad,
71     GstEvent * event);
72 static gboolean gst_shape_wipe_video_sink_setcaps (GstPad * pad,
73     GstCaps * caps);
74 static GstCaps *gst_shape_wipe_video_sink_getcaps (GstPad * pad);
75 static GstFlowReturn gst_shape_wipe_video_sink_bufferalloc (GstPad * pad,
76     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
77 static gboolean gst_shape_wipe_video_sink_query (GstPad * pad,
78     GstQuery * query);
79 static GstFlowReturn gst_shape_wipe_mask_sink_chain (GstPad * pad,
80     GstBuffer * buffer);
81 static gboolean gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event);
82 static gboolean gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps);
83 static GstCaps *gst_shape_wipe_mask_sink_getcaps (GstPad * pad);
84 static gboolean gst_shape_wipe_src_event (GstPad * pad, GstEvent * event);
85 static GstCaps *gst_shape_wipe_src_getcaps (GstPad * pad);
86 static gboolean gst_shape_wipe_src_query (GstPad * pad, GstQuery * query);
87
88 enum
89 {
90   PROP_0,
91   PROP_POSITION,
92   PROP_BORDER
93 };
94
95 #define DEFAULT_POSITION 0.0
96 #define DEFAULT_BORDER 0.0
97
98 static GstStaticPadTemplate video_sink_pad_template =
99     GST_STATIC_PAD_TEMPLATE ("video_sink",
100     GST_PAD_SINK,
101     GST_PAD_ALWAYS,
102     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB " ; "
103         GST_VIDEO_CAPS_BGRA));
104
105 static GstStaticPadTemplate mask_sink_pad_template =
106     GST_STATIC_PAD_TEMPLATE ("mask_sink",
107     GST_PAD_SINK,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS ("video/x-raw-gray, "
110         "bpp = 8, "
111         "depth = 8, "
112         "width = " GST_VIDEO_SIZE_RANGE ", "
113         "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1 ; "
114         "video/x-raw-gray, " "bpp = 16, " "depth = 16, "
115         "endianness = BYTE_ORDER, " "width = " GST_VIDEO_SIZE_RANGE ", "
116         "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1"));
117
118 static GstStaticPadTemplate src_pad_template =
119     GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
120     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB " ; "
121         GST_VIDEO_CAPS_BGRA));
122
123 GST_DEBUG_CATEGORY_STATIC (gst_shape_wipe_debug);
124 #define GST_CAT_DEFAULT gst_shape_wipe_debug
125
126 GST_BOILERPLATE (GstShapeWipe, gst_shape_wipe, GstElement, GST_TYPE_ELEMENT);
127
128 static void
129 gst_shape_wipe_base_init (gpointer g_class)
130 {
131   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
132
133   gst_element_class_set_details_simple (gstelement_class,
134       "Shape Wipe transition filter",
135       "Filter/Editor/Video",
136       "Adds a shape wipe transition to a video stream",
137       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
138
139   gst_element_class_add_pad_template (gstelement_class,
140       gst_static_pad_template_get (&video_sink_pad_template));
141   gst_element_class_add_pad_template (gstelement_class,
142       gst_static_pad_template_get (&mask_sink_pad_template));
143   gst_element_class_add_pad_template (gstelement_class,
144       gst_static_pad_template_get (&src_pad_template));
145 }
146
147 static void
148 gst_shape_wipe_class_init (GstShapeWipeClass * klass)
149 {
150   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
152
153   gobject_class->finalize = gst_shape_wipe_finalize;
154   gobject_class->set_property = gst_shape_wipe_set_property;
155   gobject_class->get_property = gst_shape_wipe_get_property;
156
157   g_object_class_install_property (gobject_class, PROP_POSITION,
158       g_param_spec_float ("position", "Position", "Position of the mask",
159           0.0, 1.0, DEFAULT_POSITION,
160           G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
161   g_object_class_install_property (gobject_class, PROP_BORDER,
162       g_param_spec_float ("border", "Border", "Border of the mask",
163           0.0, 1.0, DEFAULT_BORDER,
164           G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
165
166   gstelement_class->change_state =
167       GST_DEBUG_FUNCPTR (gst_shape_wipe_change_state);
168 }
169
170 static void
171 gst_shape_wipe_init (GstShapeWipe * self, GstShapeWipeClass * g_class)
172 {
173   self->video_sinkpad =
174       gst_pad_new_from_static_template (&video_sink_pad_template, "video_sink");
175   gst_pad_set_chain_function (self->video_sinkpad,
176       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_chain));
177   gst_pad_set_event_function (self->video_sinkpad,
178       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_event));
179   gst_pad_set_setcaps_function (self->video_sinkpad,
180       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_setcaps));
181   gst_pad_set_getcaps_function (self->video_sinkpad,
182       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_getcaps));
183   gst_pad_set_bufferalloc_function (self->video_sinkpad,
184       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_bufferalloc));
185   gst_pad_set_query_function (self->video_sinkpad,
186       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_query));
187   gst_element_add_pad (GST_ELEMENT (self), self->video_sinkpad);
188
189   self->mask_sinkpad =
190       gst_pad_new_from_static_template (&mask_sink_pad_template, "mask_sink");
191   gst_pad_set_chain_function (self->mask_sinkpad,
192       GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_chain));
193   gst_pad_set_event_function (self->mask_sinkpad,
194       GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_event));
195   gst_pad_set_setcaps_function (self->mask_sinkpad,
196       GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_setcaps));
197   gst_pad_set_getcaps_function (self->mask_sinkpad,
198       GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_getcaps));
199   gst_element_add_pad (GST_ELEMENT (self), self->mask_sinkpad);
200
201   self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
202   gst_pad_set_event_function (self->srcpad,
203       GST_DEBUG_FUNCPTR (gst_shape_wipe_src_event));
204   gst_pad_set_getcaps_function (self->srcpad,
205       GST_DEBUG_FUNCPTR (gst_shape_wipe_src_getcaps));
206   gst_pad_set_query_function (self->srcpad,
207       GST_DEBUG_FUNCPTR (gst_shape_wipe_src_query));
208   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
209
210   self->mask_mutex = g_mutex_new ();
211   self->mask_cond = g_cond_new ();
212
213   gst_shape_wipe_reset (self);
214 }
215
216 static void
217 gst_shape_wipe_get_property (GObject * object, guint prop_id,
218     GValue * value, GParamSpec * pspec)
219 {
220   GstShapeWipe *self = GST_SHAPE_WIPE (object);
221
222   switch (prop_id) {
223     case PROP_POSITION:
224       g_value_set_float (value, self->mask_position);
225       break;
226     case PROP_BORDER:
227       g_value_set_float (value, self->mask_border);
228       break;
229     default:
230       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231       break;
232   }
233 }
234
235 static void
236 gst_shape_wipe_set_property (GObject * object, guint prop_id,
237     const GValue * value, GParamSpec * pspec)
238 {
239   GstShapeWipe *self = GST_SHAPE_WIPE (object);
240
241   switch (prop_id) {
242     case PROP_POSITION:{
243       gfloat f = g_value_get_float (value);
244
245       GST_LOG_OBJECT (self, "Setting mask position: %f", f);
246       self->mask_position = f;
247       break;
248     }
249     case PROP_BORDER:{
250       gfloat f = g_value_get_float (value);
251
252       GST_LOG_OBJECT (self, "Setting mask border: %f", f);
253       self->mask_border = f;
254       break;
255     }
256     default:
257       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258       break;
259   }
260 }
261
262 static void
263 gst_shape_wipe_finalize (GObject * object)
264 {
265   GstShapeWipe *self = GST_SHAPE_WIPE (object);
266
267   gst_shape_wipe_reset (self);
268
269   if (self->mask_cond)
270     g_cond_free (self->mask_cond);
271   self->mask_cond = NULL;
272
273   if (self->mask_mutex)
274     g_mutex_free (self->mask_mutex);
275   self->mask_mutex = NULL;
276
277   G_OBJECT_CLASS (parent_class)->finalize (object);
278 }
279
280 static void
281 gst_shape_wipe_reset (GstShapeWipe * self)
282 {
283   GST_DEBUG_OBJECT (self, "Resetting internal state");
284
285   if (self->mask)
286     gst_buffer_unref (self->mask);
287   self->mask = NULL;
288
289   g_mutex_lock (self->mask_mutex);
290   g_cond_signal (self->mask_cond);
291   g_mutex_unlock (self->mask_mutex);
292
293   self->fmt = GST_VIDEO_FORMAT_UNKNOWN;
294   self->width = self->height = 0;
295   self->mask_bpp = 0;
296
297   gst_segment_init (&self->segment, GST_FORMAT_TIME);
298
299   gst_shape_wipe_reset_qos (self);
300   self->frame_duration = 0;
301 }
302
303 static GstFlowReturn
304 gst_shape_wipe_video_sink_bufferalloc (GstPad * pad, guint64 offset, guint size,
305     GstCaps * caps, GstBuffer ** buf)
306 {
307   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
308   GstFlowReturn ret = GST_FLOW_OK;
309
310   GST_LOG_OBJECT (pad, "Allocating buffer with offset 0x%" G_GINT64_MODIFIER
311       "x and size %u with caps: %" GST_PTR_FORMAT, offset, size, caps);
312
313   *buf = NULL;
314
315   ret = gst_pad_alloc_buffer (self->srcpad, offset, size, caps, buf);
316   if (G_UNLIKELY (ret != GST_FLOW_OK))
317     GST_ERROR_OBJECT (pad, "Allocating buffer failed: %s",
318         gst_flow_get_name (ret));
319
320   gst_object_unref (self);
321
322   return ret;
323 }
324
325 static gboolean
326 gst_shape_wipe_video_sink_setcaps (GstPad * pad, GstCaps * caps)
327 {
328   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
329   gboolean ret = TRUE;
330   GstStructure *s;
331   GstVideoFormat fmt;
332   gint width, height;
333   gint fps_n, fps_d;
334
335   GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
336
337   s = gst_caps_get_structure (caps, 0);
338
339   if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
340       !gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)) {
341     GST_ERROR_OBJECT (pad, "Invalid caps");
342     ret = FALSE;
343     goto done;
344   }
345
346   self->fmt = fmt;
347   if (self->width != width || self->height != height) {
348     g_mutex_lock (self->mask_mutex);
349     self->width = width;
350     self->height = height;
351
352     if (self->mask)
353       gst_buffer_unref (self->mask);
354     self->mask = NULL;
355     g_mutex_unlock (self->mask_mutex);
356   }
357
358   if (fps_n != 0)
359     self->frame_duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
360   else
361     self->frame_duration = 0;
362
363   ret = gst_pad_set_caps (self->srcpad, caps);
364
365 done:
366   gst_object_unref (self);
367
368   return ret;
369 }
370
371 static GstCaps *
372 gst_shape_wipe_video_sink_getcaps (GstPad * pad)
373 {
374   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
375   GstCaps *ret, *tmp;
376
377   if (GST_PAD_CAPS (pad))
378     return gst_caps_copy (GST_PAD_CAPS (pad));
379
380   tmp = gst_pad_peer_get_caps (self->srcpad);
381   if (tmp) {
382     ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
383     gst_caps_unref (tmp);
384   } else {
385     ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
386   }
387
388   tmp = gst_pad_peer_get_caps (pad);
389   if (tmp) {
390     GstCaps *intersection;
391
392     intersection = gst_caps_intersect (tmp, ret);
393     gst_caps_unref (tmp);
394     gst_caps_unref (ret);
395     ret = intersection;
396   }
397
398   if (self->height && self->width) {
399     guint i, n;
400
401     n = gst_caps_get_size (ret);
402     for (i = 0; i < n; i++) {
403       GstStructure *s = gst_caps_get_structure (ret, i);
404
405       gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
406           G_TYPE_INT, self->height, NULL);
407     }
408   }
409
410   tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
411   if (tmp) {
412     GstCaps *intersection, *tmp2;
413     guint i, n;
414
415     tmp = gst_caps_make_writable (tmp);
416
417     tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
418
419     intersection = gst_caps_intersect (tmp, tmp2);
420     gst_caps_unref (tmp);
421     gst_caps_unref (tmp2);
422     tmp = intersection;
423
424     n = gst_caps_get_size (tmp);
425
426     tmp2 = gst_caps_new_empty ();
427     for (i = 0; i < n; i++) {
428       GstStructure *s = gst_caps_get_structure (tmp, i);
429       GstStructure *c;
430
431       gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
432           "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
433           NULL);
434       gst_structure_set_name (s, "video/x-raw-yuv");
435       c = gst_structure_copy (s);
436       gst_structure_set_name (c, "video/x-raw-rgb");
437       gst_caps_append_structure (tmp2, c);
438     }
439     gst_caps_append (tmp, tmp2);
440
441     intersection = gst_caps_intersect (tmp, ret);
442     gst_caps_unref (tmp);
443     gst_caps_unref (ret);
444     ret = intersection;
445   }
446
447   gst_object_unref (self);
448
449   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
450
451   return ret;
452 }
453
454 static gboolean
455 gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps)
456 {
457   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
458   gboolean ret = TRUE;
459   GstStructure *s;
460   gint width, height, bpp;
461
462   GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
463
464   s = gst_caps_get_structure (caps, 0);
465
466   if (!gst_structure_get_int (s, "width", &width) ||
467       !gst_structure_get_int (s, "height", &height) ||
468       !gst_structure_get_int (s, "bpp", &bpp)) {
469     ret = FALSE;
470     goto done;
471   }
472
473   if ((self->width != width || self->height != height) &&
474       self->width > 0 && self->height > 0) {
475     GST_ERROR_OBJECT (pad, "Mask caps must have the same width/height "
476         "as the video caps");
477     ret = FALSE;
478     goto done;
479   } else {
480     self->width = width;
481     self->height = height;
482   }
483
484   self->mask_bpp = bpp;
485
486 done:
487   gst_object_unref (self);
488
489   return ret;
490 }
491
492 static GstCaps *
493 gst_shape_wipe_mask_sink_getcaps (GstPad * pad)
494 {
495   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
496   GstCaps *ret, *tmp;
497   guint i, n;
498
499   if (GST_PAD_CAPS (pad))
500     return gst_caps_copy (GST_PAD_CAPS (pad));
501
502   tmp = gst_pad_peer_get_caps (self->video_sinkpad);
503   if (tmp) {
504     ret =
505         gst_caps_intersect (tmp,
506         gst_pad_get_pad_template_caps (self->video_sinkpad));
507     gst_caps_unref (tmp);
508   } else {
509     ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
510   }
511
512   tmp = gst_pad_peer_get_caps (self->srcpad);
513   if (tmp) {
514     GstCaps *intersection;
515
516     intersection = gst_caps_intersect (ret, tmp);
517     gst_caps_unref (ret);
518     gst_caps_unref (tmp);
519     ret = intersection;
520   }
521
522   n = gst_caps_get_size (ret);
523   tmp = gst_caps_new_empty ();
524   for (i = 0; i < n; i++) {
525     GstStructure *s = gst_caps_get_structure (ret, i);
526     GstStructure *t;
527
528     gst_structure_set_name (s, "video/x-raw-gray");
529     gst_structure_remove_fields (s, "format", "framerate", "bpp", "depth",
530         "endianness", "framerate", "red_mask", "green_mask", "blue_mask",
531         "alpha_mask", NULL);
532
533     if (self->width && self->height)
534       gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
535           G_TYPE_INT, self->height, NULL);
536
537     gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
538
539     t = gst_structure_copy (s);
540
541     gst_structure_set (s, "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16,
542         "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
543     gst_structure_set (t, "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);
544
545     gst_caps_append_structure (tmp, t);
546   }
547   gst_caps_append (ret, tmp);
548
549   tmp = gst_pad_peer_get_caps (pad);
550   if (tmp) {
551     GstCaps *intersection;
552
553     intersection = gst_caps_intersect (tmp, ret);
554     gst_caps_unref (tmp);
555     gst_caps_unref (ret);
556     ret = intersection;
557   }
558
559   gst_object_unref (self);
560
561   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
562
563   return ret;
564 }
565
566 static GstCaps *
567 gst_shape_wipe_src_getcaps (GstPad * pad)
568 {
569   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
570   GstCaps *ret, *tmp;
571
572   if (GST_PAD_CAPS (pad))
573     return gst_caps_copy (GST_PAD_CAPS (pad));
574   else if (GST_PAD_CAPS (self->video_sinkpad))
575     return gst_caps_copy (GST_PAD_CAPS (self->video_sinkpad));
576
577   tmp = gst_pad_peer_get_caps (self->video_sinkpad);
578   if (tmp) {
579     ret =
580         gst_caps_intersect (tmp,
581         gst_pad_get_pad_template_caps (self->video_sinkpad));
582     gst_caps_unref (tmp);
583   } else {
584     ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
585   }
586
587   tmp = gst_pad_peer_get_caps (pad);
588   if (tmp) {
589     GstCaps *intersection;
590
591     intersection = gst_caps_intersect (tmp, ret);
592     gst_caps_unref (tmp);
593     gst_caps_unref (ret);
594     ret = intersection;
595   }
596
597   if (self->height && self->width) {
598     guint i, n;
599
600     n = gst_caps_get_size (ret);
601     for (i = 0; i < n; i++) {
602       GstStructure *s = gst_caps_get_structure (ret, i);
603
604       gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
605           G_TYPE_INT, self->height, NULL);
606     }
607   }
608
609   tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
610   if (tmp) {
611     GstCaps *intersection, *tmp2;
612     guint i, n;
613
614     tmp = gst_caps_make_writable (tmp);
615     tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
616
617     intersection = gst_caps_intersect (tmp, tmp2);
618     gst_caps_unref (tmp);
619     gst_caps_unref (tmp2);
620
621     tmp = intersection;
622     n = gst_caps_get_size (tmp);
623
624     tmp2 = gst_caps_new_empty ();
625     for (i = 0; i < n; i++) {
626       GstStructure *s = gst_caps_get_structure (tmp, i);
627       GstStructure *c;
628
629       gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
630           "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
631           NULL);
632       gst_structure_set_name (s, "video/x-raw-yuv");
633       c = gst_structure_copy (s);
634
635       gst_caps_append_structure (tmp2, c);
636     }
637     gst_caps_append (tmp, tmp2);
638
639     intersection = gst_caps_intersect (tmp, ret);
640     gst_caps_unref (tmp);
641     gst_caps_unref (ret);
642     ret = intersection;
643   }
644
645   gst_object_unref (self);
646
647   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
648
649   return ret;
650 }
651
652 static gboolean
653 gst_shape_wipe_video_sink_query (GstPad * pad, GstQuery * query)
654 {
655   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
656   gboolean ret;
657   GstPad *peer = gst_pad_get_peer (self->srcpad);
658
659   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
660       gst_query_type_get_name (GST_QUERY_TYPE (query)));
661
662   if (!peer) {
663     GST_INFO_OBJECT (pad, "No peer yet, dropping query");
664     ret = FALSE;
665   } else {
666     ret = gst_pad_query (peer, query);
667     gst_object_unref (peer);
668   }
669
670   gst_object_unref (self);
671   return ret;
672 }
673
674 static gboolean
675 gst_shape_wipe_src_query (GstPad * pad, GstQuery * query)
676 {
677   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
678   gboolean ret;
679   GstPad *peer = gst_pad_get_peer (self->video_sinkpad);
680
681   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
682       gst_query_type_get_name (GST_QUERY_TYPE (query)));
683
684   if (!peer) {
685     GST_INFO_OBJECT (pad, "No peer yet, dropping query");
686     ret = FALSE;
687   } else {
688     ret = gst_pad_query (peer, query);
689     gst_object_unref (peer);
690   }
691
692   gst_object_unref (self);
693   return ret;
694 }
695
696 static void
697 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
698     GstClockTimeDiff diff, GstClockTime timestamp)
699 {
700   GST_OBJECT_LOCK (self);
701   self->proportion = proportion;
702   if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
703     if (G_UNLIKELY (diff > 0))
704       self->earliest_time = timestamp + 2 * diff + self->frame_duration;
705     else
706       self->earliest_time = timestamp + diff;
707   } else {
708     self->earliest_time = GST_CLOCK_TIME_NONE;
709   }
710   GST_OBJECT_UNLOCK (self);
711 }
712
713 static void
714 gst_shape_wipe_reset_qos (GstShapeWipe * self)
715 {
716   gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
717 }
718
719 static void
720 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
721     GstClockTime * time)
722 {
723   GST_OBJECT_LOCK (self);
724   *proportion = self->proportion;
725   *time = self->earliest_time;
726   GST_OBJECT_UNLOCK (self);
727 }
728
729 /* Perform qos calculations before processing the next frame. Returns TRUE if
730  * the frame should be processed, FALSE if the frame can be dropped entirely */
731 static gboolean
732 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
733 {
734   GstClockTime qostime, earliest_time;
735   gdouble proportion;
736
737   /* no timestamp, can't do QoS => process frame */
738   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
739     GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
740     return TRUE;
741   }
742
743   /* get latest QoS observation values */
744   gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
745
746   /* skip qos if we have no observation (yet) => process frame */
747   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
748     GST_LOG_OBJECT (self, "no observation yet, process frame");
749     return TRUE;
750   }
751
752   /* qos is done on running time */
753   qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
754       timestamp);
755
756   /* see how our next timestamp relates to the latest qos timestamp */
757   GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
758       GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
759
760   if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
761     GST_DEBUG_OBJECT (self, "we are late, drop frame");
762     return FALSE;
763   }
764
765   GST_LOG_OBJECT (self, "process frame");
766   return TRUE;
767 }
768
769 #define CREATE_AYUV_FUNCTIONS(depth, shift) \
770 static void \
771 gst_shape_wipe_blend_ayuv_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
772     GstBuffer * maskbuf, GstBuffer * outbuf) \
773 { \
774   const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
775   const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
776   guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
777   guint i, j; \
778   guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
779                            GST_ROUND_UP_4 (self->width)) - self->width; \
780   gfloat position = self->mask_position; \
781   gfloat low = position - (self->mask_border / 2.0f); \
782   gfloat high = position + (self->mask_border / 2.0f); \
783   guint32 low_i, high_i, position_i, round_i; \
784   gint width = self->width, height = self->height; \
785   \
786   if (low < 0.0f) { \
787     high = 0.0f; \
788     low = 0.0f; \
789   } \
790   \
791   if (high > 1.0f) { \
792     low = 1.0f; \
793     high = 1.0f; \
794   } \
795   \
796   position_i = position * 65536; \
797   low_i = low * 65536; \
798   high_i = high * 65536; \
799   round_i = (high_i - low_i) >> 1; \
800   \
801   for (i = 0; i < height; i++) { \
802     for (j = 0; j < width; j++) { \
803       guint32 in = *mask << shift; \
804       \
805       if (in < low_i) { \
806         output[0] = 0x00;       /* A */ \
807         output[1] = 0x00;       /* Y */ \
808         output[2] = 0x80;       /* U */ \
809         output[3] = 0x80;       /* V */ \
810       } else if (in >= high_i) { \
811         output[0] = input[0];   /* A */ \
812         output[1] = input[1];   /* Y */ \
813         output[2] = input[2];   /* U */ \
814         output[3] = input[3];   /* V */ \
815       } else { \
816         guint32 val; \
817         /* Note: This will never overflow or be larger than 255! */ \
818         val = (((in - low_i) << 16) + round_i) / (high_i - low_i); \
819         val = (val * input[0] + 32768) >> 16; \
820         \
821         output[0] = val;        /* A */ \
822         output[1] = input[1];   /* Y */ \
823         output[2] = input[2];   /* U */ \
824         output[3] = input[3];   /* V */ \
825       } \
826       \
827       mask++; \
828       input += 4; \
829       output += 4; \
830     } \
831     mask += mask_increment; \
832   } \
833 }
834
835 CREATE_AYUV_FUNCTIONS (16, 0);
836 CREATE_AYUV_FUNCTIONS (8, 8);
837
838 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
839 static void \
840 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
841     GstBuffer * maskbuf, GstBuffer * outbuf) \
842 { \
843   const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
844   const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
845   guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
846   guint i, j; \
847   guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
848                            GST_ROUND_UP_4 (self->width)) - self->width; \
849   gfloat position = self->mask_position; \
850   gfloat low = position - (self->mask_border / 2.0f); \
851   gfloat high = position + (self->mask_border / 2.0f); \
852   guint32 low_i, high_i, position_i, round_i; \
853   gint width = self->width, height = self->height; \
854   \
855   if (low < 0.0f) { \
856     high = 0.0f; \
857     low = 0.0f; \
858   } \
859   \
860   if (high > 1.0f) { \
861     low = 1.0f; \
862     high = 1.0f; \
863   } \
864   \
865   position_i = position * 65536; \
866   low_i = low * 65536; \
867   high_i = high * 65536; \
868   round_i = (high_i - low_i) >> 1; \
869   \
870   for (i = 0; i < height; i++) { \
871     for (j = 0; j < width; j++) { \
872       guint32 in = *mask << shift; \
873       \
874       if (in < low_i) { \
875         output[a] = 0x00;       /* A */ \
876         output[r] = 0x00;       /* R */ \
877         output[g] = 0x00;       /* G */ \
878         output[b] = 0x00;       /* B */ \
879       } else if (in >= high_i) { \
880         output[a] = input[a];   /* A */ \
881         output[r] = input[r];   /* R */ \
882         output[g] = input[g];   /* G */ \
883         output[b] = input[b];   /* B */ \
884       } else { \
885         guint32 val; \
886         /* Note: This will never overflow or be larger than 255! */ \
887         val = (((in - low_i) << 16) + round_i) / (high_i - low_i); \
888         val = (val * input[a] + 32768) >> 16; \
889         \
890         output[a] = val;        /* A */ \
891         output[r] = input[r];   /* R */ \
892         output[g] = input[g];   /* G */ \
893         output[b] = input[b];   /* B */ \
894       } \
895       \
896       mask++; \
897       input += 4; \
898       output += 4; \
899     } \
900     mask += mask_increment; \
901   } \
902 }
903
904 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
905 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
906
907 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
908 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
909
910 static GstFlowReturn
911 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
912 {
913   GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
914   GstFlowReturn ret = GST_FLOW_OK;
915   GstBuffer *mask = NULL, *outbuf = NULL;
916   GstClockTime timestamp;
917   gboolean new_outbuf = FALSE;
918
919   if (G_UNLIKELY (self->fmt == GST_VIDEO_FORMAT_UNKNOWN))
920     goto not_negotiated;
921
922   timestamp = GST_BUFFER_TIMESTAMP (buffer);
923   timestamp =
924       gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
925
926   if (GST_CLOCK_TIME_IS_VALID (timestamp))
927     gst_object_sync_values (G_OBJECT (self), timestamp);
928
929   GST_LOG_OBJECT (self,
930       "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %f",
931       GST_TIME_ARGS (timestamp), self->mask_position);
932
933   g_mutex_lock (self->mask_mutex);
934   if (self->shutdown)
935     goto shutdown;
936
937   if (!self->mask)
938     g_cond_wait (self->mask_cond, self->mask_mutex);
939
940   if (self->mask == NULL || self->shutdown) {
941     goto shutdown;
942   } else {
943     mask = gst_buffer_ref (self->mask);
944   }
945   g_mutex_unlock (self->mask_mutex);
946
947   if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
948     goto qos;
949
950   /* Try to blend inplace, if it's not possible
951    * get a new buffer from downstream. */
952   if (!gst_buffer_is_writable (buffer)) {
953     ret =
954         gst_pad_alloc_buffer_and_set_caps (self->srcpad, GST_BUFFER_OFFSET_NONE,
955         GST_BUFFER_SIZE (buffer), GST_PAD_CAPS (self->srcpad), &outbuf);
956     if (G_UNLIKELY (ret != GST_FLOW_OK))
957       goto alloc_failed;
958
959     gst_buffer_copy_metadata (outbuf, buffer, GST_BUFFER_COPY_ALL);
960     new_outbuf = TRUE;
961   } else {
962     outbuf = buffer;
963   }
964
965   if (self->fmt == GST_VIDEO_FORMAT_AYUV && self->mask_bpp == 16)
966     gst_shape_wipe_blend_ayuv_16 (self, buffer, mask, outbuf);
967   else if (self->fmt == GST_VIDEO_FORMAT_AYUV)
968     gst_shape_wipe_blend_ayuv_8 (self, buffer, mask, outbuf);
969   else if (self->fmt == GST_VIDEO_FORMAT_ARGB && self->mask_bpp == 16)
970     gst_shape_wipe_blend_argb_16 (self, buffer, mask, outbuf);
971   else if (self->fmt == GST_VIDEO_FORMAT_ARGB)
972     gst_shape_wipe_blend_argb_8 (self, buffer, mask, outbuf);
973   else if (self->fmt == GST_VIDEO_FORMAT_BGRA && self->mask_bpp == 16)
974     gst_shape_wipe_blend_bgra_16 (self, buffer, mask, outbuf);
975   else if (self->fmt == GST_VIDEO_FORMAT_BGRA)
976     gst_shape_wipe_blend_bgra_8 (self, buffer, mask, outbuf);
977   else
978     g_assert_not_reached ();
979
980   gst_buffer_unref (mask);
981   if (new_outbuf)
982     gst_buffer_unref (buffer);
983
984   ret = gst_pad_push (self->srcpad, outbuf);
985   if (G_UNLIKELY (ret != GST_FLOW_OK))
986     goto push_failed;
987
988   return ret;
989
990   /* Errors */
991 not_negotiated:
992   GST_ERROR_OBJECT (self, "No valid caps yet");
993   gst_buffer_unref (buffer);
994   return GST_FLOW_NOT_NEGOTIATED;
995 shutdown:
996   GST_DEBUG_OBJECT (self, "Shutting down");
997   gst_buffer_unref (buffer);
998   return GST_FLOW_WRONG_STATE;
999 qos:
1000   GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
1001   gst_buffer_unref (buffer);
1002   gst_buffer_unref (mask);
1003   return GST_FLOW_OK;
1004 alloc_failed:
1005   GST_ERROR_OBJECT (self, "Buffer allocation from downstream failed: %s",
1006       gst_flow_get_name (ret));
1007   gst_buffer_unref (buffer);
1008   gst_buffer_unref (mask);
1009   return ret;
1010 push_failed:
1011   GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
1012       gst_flow_get_name (ret));
1013   return ret;
1014 }
1015
1016 static GstFlowReturn
1017 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
1018 {
1019   GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
1020   GstFlowReturn ret = GST_FLOW_OK;
1021
1022   g_mutex_lock (self->mask_mutex);
1023   GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
1024
1025   gst_buffer_replace (&self->mask, buffer);
1026   g_cond_signal (self->mask_cond);
1027   g_mutex_unlock (self->mask_mutex);
1028
1029   gst_buffer_unref (buffer);
1030
1031   return ret;
1032 }
1033
1034 static GstStateChangeReturn
1035 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
1036 {
1037   GstShapeWipe *self = GST_SHAPE_WIPE (element);
1038   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1039
1040   switch (transition) {
1041     case GST_STATE_CHANGE_READY_TO_PAUSED:
1042       self->shutdown = FALSE;
1043       break;
1044     case GST_STATE_CHANGE_PAUSED_TO_READY:
1045       /* Unblock video sink chain function */
1046       g_mutex_lock (self->mask_mutex);
1047       self->shutdown = TRUE;
1048       g_cond_signal (self->mask_cond);
1049       g_mutex_unlock (self->mask_mutex);
1050       break;
1051     default:
1052       break;
1053   }
1054
1055   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1056     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1057
1058   switch (transition) {
1059     case GST_STATE_CHANGE_PAUSED_TO_READY:
1060       gst_shape_wipe_reset (self);
1061       break;
1062     default:
1063       break;
1064   }
1065
1066   return ret;
1067 }
1068
1069 static gboolean
1070 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
1071 {
1072   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1073   gboolean ret;
1074
1075   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1076
1077   switch (GST_EVENT_TYPE (event)) {
1078     case GST_EVENT_NEWSEGMENT:{
1079       GstFormat fmt;
1080       gboolean is_update;
1081       gint64 start, end, base;
1082       gdouble rate;
1083
1084       gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
1085           &end, &base);
1086       if (fmt == GST_FORMAT_TIME) {
1087         GST_DEBUG_OBJECT (pad,
1088             "Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
1089             GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
1090             GST_TIME_ARGS (end));
1091         gst_segment_set_newsegment (&self->segment, is_update, rate, fmt, start,
1092             end, base);
1093       } else {
1094         gst_segment_init (&self->segment, GST_FORMAT_TIME);
1095       }
1096     }
1097       /* fall through */
1098     case GST_EVENT_FLUSH_STOP:
1099       gst_shape_wipe_reset_qos (self);
1100       /* fall through */
1101     default:
1102       ret = gst_pad_push_event (self->srcpad, event);
1103       break;
1104   }
1105
1106   gst_object_unref (self);
1107   return ret;
1108 }
1109
1110 static gboolean
1111 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1112 {
1113   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1114
1115   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1116
1117   switch (GST_EVENT_TYPE (event)) {
1118     case GST_EVENT_FLUSH_STOP:
1119       g_mutex_lock (self->mask_mutex);
1120       gst_buffer_replace (&self->mask, NULL);
1121       g_mutex_unlock (self->mask_mutex);
1122       break;
1123     default:
1124       break;
1125   }
1126
1127   /* Dropping all events here */
1128   gst_event_unref (event);
1129
1130   gst_object_unref (self);
1131   return TRUE;
1132 }
1133
1134 static gboolean
1135 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1136 {
1137   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1138   gboolean ret;
1139
1140   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1141
1142   switch (GST_EVENT_TYPE (event)) {
1143     case GST_EVENT_QOS:{
1144       GstClockTimeDiff diff;
1145       GstClockTime timestamp;
1146       gdouble proportion;
1147
1148       gst_event_parse_qos (event, &proportion, &diff, &timestamp);
1149
1150       gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1151     }
1152       /* fall through */
1153     default:
1154       ret = gst_pad_push_event (self->video_sinkpad, event);
1155       break;
1156   }
1157
1158   gst_object_unref (self);
1159   return ret;
1160 }
1161
1162 static gboolean
1163 plugin_init (GstPlugin * plugin)
1164 {
1165   GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1166       "shapewipe element");
1167
1168   gst_controller_init (NULL, NULL);
1169
1170   if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1171           GST_TYPE_SHAPE_WIPE))
1172     return FALSE;
1173
1174   return TRUE;
1175 }
1176
1177 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1178     GST_VERSION_MINOR,
1179     "shapewipe",
1180     "Shape Wipe transition filter",
1181     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)