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