[MOVED FROM BAD 19/29] shapewipe: Don't reset properties when going PAUSED->READY
[platform/upstream/gstreamer.git] / gst / shapewipe / gstshapewipe.c
1 /* GStreamer
2  * Copyright (C) 2009 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       self->mask_position = g_value_get_float (value);
244       break;
245     case PROP_BORDER:
246       self->mask_border = g_value_get_float (value);
247       break;
248     default:
249       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250       break;
251   }
252 }
253
254 static void
255 gst_shape_wipe_finalize (GObject * object)
256 {
257   GstShapeWipe *self = GST_SHAPE_WIPE (object);
258
259   gst_shape_wipe_reset (self);
260
261   if (self->mask_cond)
262     g_cond_free (self->mask_cond);
263   self->mask_cond = NULL;
264
265   if (self->mask_mutex)
266     g_mutex_free (self->mask_mutex);
267   self->mask_mutex = NULL;
268
269   G_OBJECT_CLASS (parent_class)->finalize (object);
270 }
271
272 static void
273 gst_shape_wipe_reset (GstShapeWipe * self)
274 {
275   if (self->mask)
276     gst_buffer_unref (self->mask);
277   self->mask = NULL;
278
279   g_cond_signal (self->mask_cond);
280
281   self->fmt = GST_VIDEO_FORMAT_UNKNOWN;
282   self->width = self->height = 0;
283   self->mask_bpp = 0;
284
285   gst_segment_init (&self->segment, GST_FORMAT_TIME);
286
287   gst_shape_wipe_reset_qos (self);
288   self->frame_duration = 0;
289 }
290
291 static GstFlowReturn
292 gst_shape_wipe_video_sink_bufferalloc (GstPad * pad, guint64 offset, guint size,
293     GstCaps * caps, GstBuffer ** buf)
294 {
295   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
296   GstFlowReturn ret = GST_FLOW_OK;
297
298   GST_DEBUG_OBJECT (pad, "Allocating buffer with offset 0x%" G_GINT64_MODIFIER
299       "x and size %u with caps: %" GST_PTR_FORMAT, offset, size, caps);
300
301   *buf = NULL;
302
303   ret = gst_pad_alloc_buffer (self->srcpad, offset, size, caps, buf);
304
305   gst_object_unref (self);
306
307   return ret;
308 }
309
310 static gboolean
311 gst_shape_wipe_video_sink_setcaps (GstPad * pad, GstCaps * caps)
312 {
313   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
314   gboolean ret = TRUE;
315   GstStructure *s;
316   GstVideoFormat fmt;
317   gint width, height;
318   gint fps_n, fps_d;
319
320   GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
321
322   s = gst_caps_get_structure (caps, 0);
323
324   if (!gst_video_format_parse_caps (caps, &fmt, &width, &height) ||
325       !gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d)) {
326     ret = FALSE;
327     goto done;
328   }
329
330   self->fmt = fmt;
331   if (self->width != width || self->height != height) {
332     g_mutex_lock (self->mask_mutex);
333     self->width = width;
334     self->height = height;
335
336     if (self->mask)
337       gst_buffer_unref (self->mask);
338     self->mask = NULL;
339     g_mutex_unlock (self->mask_mutex);
340   }
341
342   if (fps_n != 0)
343     self->frame_duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
344   else
345     self->frame_duration = 0;
346
347   ret = gst_pad_set_caps (self->srcpad, caps);
348
349 done:
350   gst_object_unref (self);
351
352   return ret;
353 }
354
355 static GstCaps *
356 gst_shape_wipe_video_sink_getcaps (GstPad * pad)
357 {
358   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
359   GstCaps *ret, *tmp;
360
361   if (GST_PAD_CAPS (pad))
362     return gst_caps_copy (GST_PAD_CAPS (pad));
363
364   tmp = gst_pad_peer_get_caps (self->srcpad);
365   if (tmp) {
366     ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
367     gst_caps_unref (tmp);
368   } else {
369     ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
370   }
371
372   tmp = gst_pad_peer_get_caps (pad);
373   if (tmp) {
374     GstCaps *intersection;
375
376     intersection = gst_caps_intersect (tmp, ret);
377     gst_caps_unref (tmp);
378     gst_caps_unref (ret);
379     ret = intersection;
380   }
381
382   if (self->height && self->width) {
383     guint i, n;
384
385     n = gst_caps_get_size (ret);
386     for (i = 0; i < n; i++) {
387       GstStructure *s = gst_caps_get_structure (ret, i);
388
389       gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
390           G_TYPE_INT, self->height, NULL);
391     }
392   }
393
394   tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
395   if (tmp) {
396     GstCaps *intersection, *tmp2;
397     guint i, n;
398
399     tmp = gst_caps_make_writable (tmp);
400
401     tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
402
403     intersection = gst_caps_intersect (tmp, tmp2);
404     gst_caps_unref (tmp);
405     gst_caps_unref (tmp2);
406     tmp = intersection;
407
408     n = gst_caps_get_size (tmp);
409
410     tmp2 = gst_caps_new_empty ();
411     for (i = 0; i < n; i++) {
412       GstStructure *s = gst_caps_get_structure (tmp, i);
413       GstStructure *c;
414
415       gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
416           "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
417           NULL);
418       gst_structure_set_name (s, "video/x-raw-yuv");
419       c = gst_structure_copy (s);
420       gst_structure_set_name (c, "video/x-raw-rgb");
421       gst_caps_append_structure (tmp2, c);
422     }
423     gst_caps_append (tmp, tmp2);
424
425     intersection = gst_caps_intersect (tmp, ret);
426     gst_caps_unref (tmp);
427     gst_caps_unref (ret);
428     ret = intersection;
429   }
430
431   gst_object_unref (self);
432
433   GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
434
435   return ret;
436 }
437
438 static gboolean
439 gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps)
440 {
441   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
442   gboolean ret = TRUE;
443   GstStructure *s;
444   gint width, height, bpp;
445
446   GST_DEBUG_OBJECT (pad, "Setting caps: %" GST_PTR_FORMAT, caps);
447
448   s = gst_caps_get_structure (caps, 0);
449
450   if (!gst_structure_get_int (s, "width", &width) ||
451       !gst_structure_get_int (s, "height", &height) ||
452       !gst_structure_get_int (s, "bpp", &bpp)) {
453     ret = FALSE;
454     goto done;
455   }
456
457   if ((self->width != width || self->height != height) &&
458       self->width > 0 && self->height > 0) {
459     GST_ERROR_OBJECT (pad, "Mask caps must have the same width/height "
460         "as the video caps");
461     ret = FALSE;
462     goto done;
463   } else {
464     self->width = width;
465     self->height = height;
466   }
467
468   self->mask_bpp = bpp;
469
470 done:
471   gst_object_unref (self);
472
473   return ret;
474 }
475
476 static GstCaps *
477 gst_shape_wipe_mask_sink_getcaps (GstPad * pad)
478 {
479   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
480   GstCaps *ret, *tmp;
481   guint i, n;
482
483   if (GST_PAD_CAPS (pad))
484     return gst_caps_copy (GST_PAD_CAPS (pad));
485
486   tmp = gst_pad_peer_get_caps (self->video_sinkpad);
487   if (tmp) {
488     ret =
489         gst_caps_intersect (tmp,
490         gst_pad_get_pad_template_caps (self->video_sinkpad));
491     gst_caps_unref (tmp);
492   } else {
493     ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
494   }
495
496   tmp = gst_pad_peer_get_caps (self->srcpad);
497   if (tmp) {
498     GstCaps *intersection;
499
500     intersection = gst_caps_intersect (ret, tmp);
501     gst_caps_unref (ret);
502     gst_caps_unref (tmp);
503     ret = intersection;
504   }
505
506   n = gst_caps_get_size (ret);
507   tmp = gst_caps_new_empty ();
508   for (i = 0; i < n; i++) {
509     GstStructure *s = gst_caps_get_structure (ret, i);
510     GstStructure *t;
511
512     gst_structure_set_name (s, "video/x-raw-gray");
513     gst_structure_remove_fields (s, "format", "framerate", "bpp", "depth",
514         "endianness", "framerate", "red_mask", "green_mask", "blue_mask",
515         "alpha_mask", NULL);
516
517     if (self->width && self->height)
518       gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
519           G_TYPE_INT, self->height, NULL);
520
521     gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
522
523     t = gst_structure_copy (s);
524
525     gst_structure_set (s, "bpp", G_TYPE_INT, 16, "depth", G_TYPE_INT, 16,
526         "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
527     gst_structure_set (t, "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8, NULL);
528
529     gst_caps_append_structure (tmp, t);
530   }
531   gst_caps_append (ret, tmp);
532
533   tmp = gst_pad_peer_get_caps (pad);
534   if (tmp) {
535     GstCaps *intersection;
536
537     intersection = gst_caps_intersect (tmp, ret);
538     gst_caps_unref (tmp);
539     gst_caps_unref (ret);
540     ret = intersection;
541   }
542
543   gst_object_unref (self);
544
545   GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
546
547   return ret;
548 }
549
550 static GstCaps *
551 gst_shape_wipe_src_getcaps (GstPad * pad)
552 {
553   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
554   GstCaps *ret, *tmp;
555
556   if (GST_PAD_CAPS (pad))
557     return gst_caps_copy (GST_PAD_CAPS (pad));
558   else if (GST_PAD_CAPS (self->video_sinkpad))
559     return gst_caps_copy (GST_PAD_CAPS (self->video_sinkpad));
560
561   tmp = gst_pad_peer_get_caps (self->video_sinkpad);
562   if (tmp) {
563     ret =
564         gst_caps_intersect (tmp,
565         gst_pad_get_pad_template_caps (self->video_sinkpad));
566     gst_caps_unref (tmp);
567   } else {
568     ret = gst_caps_copy (gst_pad_get_pad_template_caps (self->video_sinkpad));
569   }
570
571   tmp = gst_pad_peer_get_caps (pad);
572   if (tmp) {
573     GstCaps *intersection;
574
575     intersection = gst_caps_intersect (tmp, ret);
576     gst_caps_unref (tmp);
577     gst_caps_unref (ret);
578     ret = intersection;
579   }
580
581   if (self->height && self->width) {
582     guint i, n;
583
584     n = gst_caps_get_size (ret);
585     for (i = 0; i < n; i++) {
586       GstStructure *s = gst_caps_get_structure (ret, i);
587
588       gst_structure_set (s, "width", G_TYPE_INT, self->width, "height",
589           G_TYPE_INT, self->height, NULL);
590     }
591   }
592
593   tmp = gst_pad_peer_get_caps (self->mask_sinkpad);
594   if (tmp) {
595     GstCaps *intersection, *tmp2;
596     guint i, n;
597
598     tmp = gst_caps_make_writable (tmp);
599     tmp2 = gst_caps_copy (gst_pad_get_pad_template_caps (self->mask_sinkpad));
600
601     intersection = gst_caps_intersect (tmp, tmp2);
602     gst_caps_unref (tmp);
603     gst_caps_unref (tmp2);
604
605     tmp = intersection;
606     n = gst_caps_get_size (tmp);
607
608     tmp2 = gst_caps_new_empty ();
609     for (i = 0; i < n; i++) {
610       GstStructure *s = gst_caps_get_structure (tmp, i);
611       GstStructure *c;
612
613       gst_structure_remove_fields (s, "format", "bpp", "depth", "endianness",
614           "framerate", "red_mask", "green_mask", "blue_mask", "alpha_mask",
615           NULL);
616       gst_structure_set_name (s, "video/x-raw-yuv");
617       c = gst_structure_copy (s);
618
619       gst_caps_append_structure (tmp2, c);
620     }
621     gst_caps_append (tmp, tmp2);
622
623     intersection = gst_caps_intersect (tmp, ret);
624     gst_caps_unref (tmp);
625     gst_caps_unref (ret);
626     ret = intersection;
627   }
628
629   gst_object_unref (self);
630
631   GST_DEBUG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
632
633   return ret;
634 }
635
636 static gboolean
637 gst_shape_wipe_video_sink_query (GstPad * pad, GstQuery * query)
638 {
639   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
640   gboolean ret;
641   GstPad *peer = gst_pad_get_peer (self->srcpad);
642
643   GST_DEBUG_OBJECT (pad, "Handling query of type '%s'",
644       gst_query_type_get_name (GST_QUERY_TYPE (query)));
645
646   if (!peer) {
647     GST_INFO_OBJECT (pad, "No peer yet");
648     ret = FALSE;
649   } else {
650     ret = gst_pad_query (peer, query);
651     gst_object_unref (peer);
652   }
653
654   gst_object_unref (self);
655   return ret;
656 }
657
658 static gboolean
659 gst_shape_wipe_src_query (GstPad * pad, GstQuery * query)
660 {
661   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
662   gboolean ret;
663   GstPad *peer = gst_pad_get_peer (self->video_sinkpad);
664
665   GST_DEBUG_OBJECT (pad, "Handling query of type '%s'",
666       gst_query_type_get_name (GST_QUERY_TYPE (query)));
667
668   if (!peer) {
669     GST_INFO_OBJECT (pad, "No peer yet");
670     ret = FALSE;
671   } else {
672     ret = gst_pad_query (peer, query);
673     gst_object_unref (peer);
674   }
675
676   gst_object_unref (self);
677   return ret;
678 }
679
680 static void
681 gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
682     GstClockTimeDiff diff, GstClockTime timestamp)
683 {
684   GST_OBJECT_LOCK (self);
685   self->proportion = proportion;
686   if (G_LIKELY (timestamp != GST_CLOCK_TIME_NONE)) {
687     if (G_UNLIKELY (diff > 0))
688       self->earliest_time = timestamp + 2 * diff + self->frame_duration;
689     else
690       self->earliest_time = timestamp + diff;
691   } else {
692     self->earliest_time = GST_CLOCK_TIME_NONE;
693   }
694   GST_OBJECT_UNLOCK (self);
695 }
696
697 static void
698 gst_shape_wipe_reset_qos (GstShapeWipe * self)
699 {
700   gst_shape_wipe_update_qos (self, 0.5, 0, GST_CLOCK_TIME_NONE);
701 }
702
703 static void
704 gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
705     GstClockTime * time)
706 {
707   GST_OBJECT_LOCK (self);
708   *proportion = self->proportion;
709   *time = self->earliest_time;
710   GST_OBJECT_UNLOCK (self);
711 }
712
713 /* Perform qos calculations before processing the next frame. Returns TRUE if
714  * the frame should be processed, FALSE if the frame can be dropped entirely */
715 static gboolean
716 gst_shape_wipe_do_qos (GstShapeWipe * self, GstClockTime timestamp)
717 {
718   GstClockTime qostime, earliest_time;
719   gdouble proportion;
720
721   /* no timestamp, can't do QoS => process frame */
722   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp))) {
723     GST_LOG_OBJECT (self, "invalid timestamp, can't do QoS, process frame");
724     return TRUE;
725   }
726
727   /* get latest QoS observation values */
728   gst_shape_wipe_read_qos (self, &proportion, &earliest_time);
729
730   /* skip qos if we have no observation (yet) => process frame */
731   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (earliest_time))) {
732     GST_LOG_OBJECT (self, "no observation yet, process frame");
733     return TRUE;
734   }
735
736   /* qos is done on running time */
737   qostime = gst_segment_to_running_time (&self->segment, GST_FORMAT_TIME,
738       timestamp);
739
740   /* see how our next timestamp relates to the latest qos timestamp */
741   GST_LOG_OBJECT (self, "qostime %" GST_TIME_FORMAT ", earliest %"
742       GST_TIME_FORMAT, GST_TIME_ARGS (qostime), GST_TIME_ARGS (earliest_time));
743
744   if (qostime != GST_CLOCK_TIME_NONE && qostime <= earliest_time) {
745     GST_DEBUG_OBJECT (self, "we are late, drop frame");
746     return FALSE;
747   }
748
749   GST_LOG_OBJECT (self, "process frame");
750   return TRUE;
751 }
752
753 #define CREATE_AYUV_FUNCTIONS(depth, shift) \
754 static GstFlowReturn \
755 gst_shape_wipe_blend_ayuv_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
756     GstBuffer * maskbuf, GstBuffer * outbuf) \
757 { \
758   const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
759   const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
760   guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
761   guint i, j; \
762   guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
763                            GST_ROUND_UP_4 (self->width)) - self->width; \
764   gfloat position = self->mask_position; \
765   gfloat low = position - (self->mask_border / 2.0f); \
766   gfloat high = position + (self->mask_border / 2.0f); \
767   guint32 low_i, high_i, position_i, round_i; \
768   gint width = self->width, height = self->height; \
769   \
770   if (low < 0.0f) { \
771     high = 0.0f; \
772     low = 0.0f; \
773   } \
774   \
775   if (high > 1.0f) { \
776     low = 1.0f; \
777     high = 1.0f; \
778   } \
779   \
780   position_i = position * 65536; \
781   low_i = low * 65536; \
782   high_i = high * 65536; \
783   round_i = (high_i - low_i) >> 1; \
784   \
785   for (i = 0; i < height; i++) { \
786     for (j = 0; j < width; j++) { \
787       guint32 in = *mask << shift; \
788       \
789       if (in < low_i) { \
790         output[0] = 0x00;       /* A */ \
791         output[1] = 0x00;       /* Y */ \
792         output[2] = 0x80;       /* U */ \
793         output[3] = 0x80;       /* V */ \
794       } else if (in >= high_i) { \
795         output[0] = 0xff;       /* A */ \
796         output[1] = input[1];   /* Y */ \
797         output[2] = input[2];   /* U */ \
798         output[3] = input[3];   /* V */ \
799       } else { \
800         guint32 val; \
801         /* Note: This will never overflow or be larger than 255! */ \
802         val = ((((in - low_i) << 16) + round_i) / (high_i - low_i)) >> 8; \
803         \
804         output[0] = val;        /* A */ \
805         output[1] = input[1];   /* Y */ \
806         output[2] = input[2];   /* U */ \
807         output[3] = input[3];   /* V */ \
808       } \
809       \
810       mask++; \
811       input += 4; \
812       output += 4; \
813     } \
814     mask += mask_increment; \
815   } \
816   \
817   return GST_FLOW_OK; \
818 }
819
820 CREATE_AYUV_FUNCTIONS (16, 0);
821 CREATE_AYUV_FUNCTIONS (8, 8);
822
823 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
824 static GstFlowReturn \
825 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
826     GstBuffer * maskbuf, GstBuffer * outbuf) \
827 { \
828   const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
829   const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
830   guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
831   guint i, j; \
832   guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
833                            GST_ROUND_UP_4 (self->width)) - self->width; \
834   gfloat position = self->mask_position; \
835   gfloat low = position - (self->mask_border / 2.0f); \
836   gfloat high = position + (self->mask_border / 2.0f); \
837   guint32 low_i, high_i, position_i, round_i; \
838   gint width = self->width, height = self->height; \
839   \
840   if (low < 0.0f) { \
841     high = 0.0f; \
842     low = 0.0f; \
843   } \
844   \
845   if (high > 1.0f) { \
846     low = 1.0f; \
847     high = 1.0f; \
848   } \
849   \
850   position_i = position * 65536; \
851   low_i = low * 65536; \
852   high_i = high * 65536; \
853   round_i = (high_i - low_i) >> 1; \
854   \
855   for (i = 0; i < height; i++) { \
856     for (j = 0; j < width; j++) { \
857       guint32 in = *mask << shift; \
858       \
859       if (in < low) { \
860         output[a] = 0x00;       /* A */ \
861         output[r] = 0x00;       /* R */ \
862         output[g] = 0x00;       /* G */ \
863         output[b] = 0x00;       /* B */ \
864       } else if (in >= high) { \
865         output[a] = 0xff;       /* A */ \
866         output[r] = input[r];   /* R */ \
867         output[g] = input[g];   /* G */ \
868         output[b] = input[b];   /* B */ \
869       } else { \
870         guint32 val; \
871         /* Note: This will never overflow or be larger than 255! */ \
872         val = ((((in - low_i) << 16) + round_i) / (high_i - low_i)) >> 8; \
873         \
874         output[a] = val;        /* A */ \
875         output[r] = input[r];   /* R */ \
876         output[g] = input[g];   /* G */ \
877         output[b] = input[b];   /* B */ \
878       } \
879       \
880       mask++; \
881       input += 4; \
882       output += 4; \
883     } \
884     mask += mask_increment; \
885   } \
886   \
887   return GST_FLOW_OK; \
888 }
889
890 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
891 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
892
893 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
894 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
895
896 static GstFlowReturn
897 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
898 {
899   GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
900   GstFlowReturn ret = GST_FLOW_OK;
901   GstBuffer *mask = NULL, *outbuf = NULL;
902   GstClockTime timestamp;
903   gboolean new_outbuf = FALSE;
904
905   if (G_UNLIKELY (self->fmt == GST_VIDEO_FORMAT_UNKNOWN))
906     return GST_FLOW_NOT_NEGOTIATED;
907
908   timestamp = GST_BUFFER_TIMESTAMP (buffer);
909   timestamp =
910       gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
911
912   if (GST_CLOCK_TIME_IS_VALID (timestamp))
913     gst_object_sync_values (G_OBJECT (self), timestamp);
914
915   GST_DEBUG_OBJECT (self,
916       "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %lf",
917       GST_TIME_ARGS (timestamp), self->mask_position);
918
919   g_mutex_lock (self->mask_mutex);
920   if (!self->mask)
921     g_cond_wait (self->mask_cond, self->mask_mutex);
922
923   if (self->mask == NULL) {
924     g_mutex_unlock (self->mask_mutex);
925     gst_buffer_unref (buffer);
926     return GST_FLOW_UNEXPECTED;
927   } else {
928     mask = gst_buffer_ref (self->mask);
929   }
930   g_mutex_unlock (self->mask_mutex);
931
932   if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer))) {
933     gst_buffer_unref (buffer);
934     gst_buffer_unref (mask);
935     return GST_FLOW_OK;
936   }
937
938   /* Try to blend inplace, if it's not possible
939    * get a new buffer from downstream.
940    */
941   if (!gst_buffer_is_writable (buffer)) {
942     ret =
943         gst_pad_alloc_buffer_and_set_caps (self->srcpad, GST_BUFFER_OFFSET_NONE,
944         GST_BUFFER_SIZE (buffer), GST_PAD_CAPS (self->srcpad), &outbuf);
945     if (G_UNLIKELY (ret != GST_FLOW_OK)) {
946       gst_buffer_unref (buffer);
947       gst_buffer_unref (mask);
948       return ret;
949     }
950     gst_buffer_copy_metadata (outbuf, buffer, GST_BUFFER_COPY_ALL);
951     new_outbuf = TRUE;
952   } else {
953     outbuf = buffer;
954   }
955
956   if (self->fmt == GST_VIDEO_FORMAT_AYUV && self->mask_bpp == 16)
957     ret = gst_shape_wipe_blend_ayuv_16 (self, buffer, mask, outbuf);
958   else if (self->fmt == GST_VIDEO_FORMAT_AYUV)
959     ret = gst_shape_wipe_blend_ayuv_8 (self, buffer, mask, outbuf);
960   else if (self->fmt == GST_VIDEO_FORMAT_ARGB && self->mask_bpp == 16)
961     ret = gst_shape_wipe_blend_argb_16 (self, buffer, mask, outbuf);
962   else if (self->fmt == GST_VIDEO_FORMAT_ARGB)
963     ret = gst_shape_wipe_blend_argb_8 (self, buffer, mask, outbuf);
964   else if (self->fmt == GST_VIDEO_FORMAT_BGRA && self->mask_bpp == 16)
965     ret = gst_shape_wipe_blend_bgra_16 (self, buffer, mask, outbuf);
966   else if (self->fmt == GST_VIDEO_FORMAT_BGRA)
967     ret = gst_shape_wipe_blend_bgra_8 (self, buffer, mask, outbuf);
968   else
969     g_assert_not_reached ();
970
971   gst_buffer_unref (mask);
972   if (new_outbuf)
973     gst_buffer_unref (buffer);
974
975   if (ret != GST_FLOW_OK) {
976     gst_buffer_unref (outbuf);
977     return ret;
978   }
979
980   ret = gst_pad_push (self->srcpad, outbuf);
981   return ret;
982 }
983
984 static GstFlowReturn
985 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
986 {
987   GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
988   GstFlowReturn ret = GST_FLOW_OK;
989
990   g_mutex_lock (self->mask_mutex);
991   GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
992
993   gst_buffer_replace (&self->mask, buffer);
994   g_cond_signal (self->mask_cond);
995   g_mutex_unlock (self->mask_mutex);
996
997   gst_buffer_unref (buffer);
998
999   return ret;
1000 }
1001
1002 static GstStateChangeReturn
1003 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
1004 {
1005   GstShapeWipe *self = GST_SHAPE_WIPE (element);
1006   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1007
1008   switch (transition) {
1009     case GST_STATE_CHANGE_READY_TO_PAUSED:
1010     default:
1011       break;
1012   }
1013
1014   /* Unblock video sink chain function */
1015   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1016     g_cond_signal (self->mask_cond);
1017
1018   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1019     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1020
1021   switch (transition) {
1022     case GST_STATE_CHANGE_PAUSED_TO_READY:
1023       gst_shape_wipe_reset (self);
1024       break;
1025     default:
1026       break;
1027   }
1028
1029   return ret;
1030 }
1031
1032 static gboolean
1033 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
1034 {
1035   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1036   gboolean ret;
1037
1038   GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1039
1040   switch (GST_EVENT_TYPE (event)) {
1041     case GST_EVENT_NEWSEGMENT:{
1042       GstFormat fmt;
1043       gboolean is_update;
1044       gint64 start, end, base;
1045       gdouble rate;
1046
1047       gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
1048           &end, &base);
1049       if (fmt == GST_FORMAT_TIME) {
1050         GST_DEBUG_OBJECT (pad,
1051             "Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
1052             GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
1053             GST_TIME_ARGS (end));
1054         gst_segment_set_newsegment (&self->segment, is_update, rate, fmt, start,
1055             end, base);
1056       } else {
1057         gst_segment_init (&self->segment, GST_FORMAT_TIME);
1058       }
1059     }
1060       /* fall through */
1061     case GST_EVENT_FLUSH_STOP:
1062       gst_shape_wipe_reset_qos (self);
1063       /* fall through */
1064     default:
1065       ret = gst_pad_push_event (self->srcpad, event);
1066       break;
1067   }
1068
1069   gst_object_unref (self);
1070   return ret;
1071 }
1072
1073 static gboolean
1074 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1075 {
1076   GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1077
1078   /* Dropping all events here */
1079   gst_event_unref (event);
1080   return TRUE;
1081 }
1082
1083 static gboolean
1084 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1085 {
1086   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1087   gboolean ret;
1088
1089   switch (GST_EVENT_TYPE (event)) {
1090     case GST_EVENT_QOS:{
1091       GstClockTimeDiff diff;
1092       GstClockTime timestamp;
1093       gdouble proportion;
1094
1095       gst_event_parse_qos (event, &proportion, &diff, &timestamp);
1096
1097       gst_shape_wipe_update_qos (self, proportion, diff, timestamp);
1098     }
1099       /* fall through */
1100     default:
1101       ret = gst_pad_push_event (self->video_sinkpad, event);
1102       break;
1103   }
1104
1105   gst_object_unref (self);
1106   return ret;
1107 }
1108
1109 static gboolean
1110 plugin_init (GstPlugin * plugin)
1111 {
1112   GST_DEBUG_CATEGORY_INIT (gst_shape_wipe_debug, "shapewipe", 0,
1113       "shapewipe element");
1114
1115   gst_controller_init (NULL, NULL);
1116
1117   if (!gst_element_register (plugin, "shapewipe", GST_RANK_NONE,
1118           GST_TYPE_SHAPE_WIPE))
1119     return FALSE;
1120
1121   return TRUE;
1122 }
1123
1124 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1125     GST_VERSION_MINOR,
1126     "shapewipe",
1127     "Shape Wipe transition filter",
1128     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)