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