[MOVED FROM BAD 23/29] shapewipe: Move chain function error cases at the end of the...
[platform/upstream/gstreamer.git] / gst / shapewipe / gstshapewipe.c
1 /* GStreamer
2  * Copyright (C) 2009,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-shapewipe
22  *
23  * The shapewipe element provides custom transitions on video streams
24  * based on a grayscale bitmap. The state of the transition can be
25  * controlled by the position property and an optional blended border
26  * can be added by the border property.
27  *
28  * Transition bitmaps can be downloaded from the
29  * <ulink url="http://cinelerra.org/transitions.php">Cinelerra transition</ulink>
30  * page.
31  *
32  * <refsect2>
33  * <title>Example launch line</title>
34  * |[
35  * gst-launch -v videotestsrc ! video/x-raw-yuv,width=640,height=480 ! shapewipe position=0.5 name=shape ! videomixer name=mixer ! ffmpegcolorspace ! autovideosink     filesrc location=mask.png ! typefind ! decodebin2 ! ffmpegcolorspace ! videoscale ! queue ! shape.mask_sink    videotestsrc pattern=snow ! video/x-raw-yuv,width=640,height=480 ! queue ! mixer.
36  * ]| This pipeline adds the transition from mask.png with position 0.5 to an SMPTE test screen and snow.
37  * </refsect2>
38  */
39
40
41 #ifdef HAVE_CONFIG_H
42 #  include "config.h"
43 #endif
44
45 #include <string.h>
46
47 #include <gst/gst.h>
48 #include <gst/controller/gstcontroller.h>
49
50 #include "gstshapewipe.h"
51
52 static void gst_shape_wipe_finalize (GObject * object);
53 static void gst_shape_wipe_get_property (GObject * object, guint prop_id,
54     GValue * value, GParamSpec * pspec);
55 static void gst_shape_wipe_set_property (GObject * object, guint prop_id,
56     const GValue * value, GParamSpec * pspec);
57
58 static void gst_shape_wipe_reset (GstShapeWipe * self);
59 static void gst_shape_wipe_update_qos (GstShapeWipe * self, gdouble proportion,
60     GstClockTimeDiff diff, GstClockTime time);
61 static void gst_shape_wipe_reset_qos (GstShapeWipe * self);
62 static void gst_shape_wipe_read_qos (GstShapeWipe * self, gdouble * proportion,
63     GstClockTime * time);
64
65 static GstStateChangeReturn gst_shape_wipe_change_state (GstElement * element,
66     GstStateChange transition);
67
68 static GstFlowReturn gst_shape_wipe_video_sink_chain (GstPad * pad,
69     GstBuffer * buffer);
70 static gboolean gst_shape_wipe_video_sink_event (GstPad * pad,
71     GstEvent * event);
72 static gboolean gst_shape_wipe_video_sink_setcaps (GstPad * pad,
73     GstCaps * caps);
74 static GstCaps *gst_shape_wipe_video_sink_getcaps (GstPad * pad);
75 static GstFlowReturn gst_shape_wipe_video_sink_bufferalloc (GstPad * pad,
76     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
77 static gboolean gst_shape_wipe_video_sink_query (GstPad * pad,
78     GstQuery * query);
79 static GstFlowReturn gst_shape_wipe_mask_sink_chain (GstPad * pad,
80     GstBuffer * buffer);
81 static gboolean gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event);
82 static gboolean gst_shape_wipe_mask_sink_setcaps (GstPad * pad, GstCaps * caps);
83 static GstCaps *gst_shape_wipe_mask_sink_getcaps (GstPad * pad);
84 static gboolean gst_shape_wipe_src_event (GstPad * pad, GstEvent * event);
85 static GstCaps *gst_shape_wipe_src_getcaps (GstPad * pad);
86 static gboolean gst_shape_wipe_src_query (GstPad * pad, GstQuery * query);
87
88 enum
89 {
90   PROP_0,
91   PROP_POSITION,
92   PROP_BORDER
93 };
94
95 #define DEFAULT_POSITION 0.0
96 #define DEFAULT_BORDER 0.0
97
98 static GstStaticPadTemplate video_sink_pad_template =
99     GST_STATIC_PAD_TEMPLATE ("video_sink",
100     GST_PAD_SINK,
101     GST_PAD_ALWAYS,
102     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB " ; "
103         GST_VIDEO_CAPS_BGRA));
104
105 static GstStaticPadTemplate mask_sink_pad_template =
106     GST_STATIC_PAD_TEMPLATE ("mask_sink",
107     GST_PAD_SINK,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS ("video/x-raw-gray, "
110         "bpp = 8, "
111         "depth = 8, "
112         "width = " GST_VIDEO_SIZE_RANGE ", "
113         "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1 ; "
114         "video/x-raw-gray, " "bpp = 16, " "depth = 16, "
115         "endianness = BYTE_ORDER, " "width = " GST_VIDEO_SIZE_RANGE ", "
116         "height = " GST_VIDEO_SIZE_RANGE ", " "framerate = 0/1"));
117
118 static GstStaticPadTemplate src_pad_template =
119     GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
120     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") " ; " GST_VIDEO_CAPS_ARGB " ; "
121         GST_VIDEO_CAPS_BGRA));
122
123 GST_DEBUG_CATEGORY_STATIC (gst_shape_wipe_debug);
124 #define GST_CAT_DEFAULT gst_shape_wipe_debug
125
126 GST_BOILERPLATE (GstShapeWipe, gst_shape_wipe, GstElement, GST_TYPE_ELEMENT);
127
128 static void
129 gst_shape_wipe_base_init (gpointer g_class)
130 {
131   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
132
133   gst_element_class_set_details_simple (gstelement_class,
134       "Shape Wipe transition filter",
135       "Filter/Editor/Video",
136       "Adds a shape wipe transition to a video stream",
137       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
138
139   gst_element_class_add_pad_template (gstelement_class,
140       gst_static_pad_template_get (&video_sink_pad_template));
141   gst_element_class_add_pad_template (gstelement_class,
142       gst_static_pad_template_get (&mask_sink_pad_template));
143   gst_element_class_add_pad_template (gstelement_class,
144       gst_static_pad_template_get (&src_pad_template));
145 }
146
147 static void
148 gst_shape_wipe_class_init (GstShapeWipeClass * klass)
149 {
150   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
152
153   gobject_class->finalize = gst_shape_wipe_finalize;
154   gobject_class->set_property = gst_shape_wipe_set_property;
155   gobject_class->get_property = gst_shape_wipe_get_property;
156
157   g_object_class_install_property (gobject_class, PROP_POSITION,
158       g_param_spec_float ("position", "Position", "Position of the mask",
159           0.0, 1.0, DEFAULT_POSITION,
160           G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
161   g_object_class_install_property (gobject_class, PROP_BORDER,
162       g_param_spec_float ("border", "Border", "Border of the mask",
163           0.0, 1.0, DEFAULT_BORDER,
164           G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE));
165
166   gstelement_class->change_state =
167       GST_DEBUG_FUNCPTR (gst_shape_wipe_change_state);
168 }
169
170 static void
171 gst_shape_wipe_init (GstShapeWipe * self, GstShapeWipeClass * g_class)
172 {
173   self->video_sinkpad =
174       gst_pad_new_from_static_template (&video_sink_pad_template, "video_sink");
175   gst_pad_set_chain_function (self->video_sinkpad,
176       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_chain));
177   gst_pad_set_event_function (self->video_sinkpad,
178       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_event));
179   gst_pad_set_setcaps_function (self->video_sinkpad,
180       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_setcaps));
181   gst_pad_set_getcaps_function (self->video_sinkpad,
182       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_getcaps));
183   gst_pad_set_bufferalloc_function (self->video_sinkpad,
184       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_bufferalloc));
185   gst_pad_set_query_function (self->video_sinkpad,
186       GST_DEBUG_FUNCPTR (gst_shape_wipe_video_sink_query));
187   gst_element_add_pad (GST_ELEMENT (self), self->video_sinkpad);
188
189   self->mask_sinkpad =
190       gst_pad_new_from_static_template (&mask_sink_pad_template, "mask_sink");
191   gst_pad_set_chain_function (self->mask_sinkpad,
192       GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_chain));
193   gst_pad_set_event_function (self->mask_sinkpad,
194       GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_event));
195   gst_pad_set_setcaps_function (self->mask_sinkpad,
196       GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_setcaps));
197   gst_pad_set_getcaps_function (self->mask_sinkpad,
198       GST_DEBUG_FUNCPTR (gst_shape_wipe_mask_sink_getcaps));
199   gst_element_add_pad (GST_ELEMENT (self), self->mask_sinkpad);
200
201   self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
202   gst_pad_set_event_function (self->srcpad,
203       GST_DEBUG_FUNCPTR (gst_shape_wipe_src_event));
204   gst_pad_set_getcaps_function (self->srcpad,
205       GST_DEBUG_FUNCPTR (gst_shape_wipe_src_getcaps));
206   gst_pad_set_query_function (self->srcpad,
207       GST_DEBUG_FUNCPTR (gst_shape_wipe_src_query));
208   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
209
210   self->mask_mutex = g_mutex_new ();
211   self->mask_cond = g_cond_new ();
212
213   gst_shape_wipe_reset (self);
214 }
215
216 static void
217 gst_shape_wipe_get_property (GObject * object, guint prop_id,
218     GValue * value, GParamSpec * pspec)
219 {
220   GstShapeWipe *self = GST_SHAPE_WIPE (object);
221
222   switch (prop_id) {
223     case PROP_POSITION:
224       g_value_set_float (value, self->mask_position);
225       break;
226     case PROP_BORDER:
227       g_value_set_float (value, self->mask_border);
228       break;
229     default:
230       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231       break;
232   }
233 }
234
235 static void
236 gst_shape_wipe_set_property (GObject * object, guint prop_id,
237     const GValue * value, GParamSpec * pspec)
238 {
239   GstShapeWipe *self = GST_SHAPE_WIPE (object);
240
241   switch (prop_id) {
242     case PROP_POSITION:
243       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 void \
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
818 CREATE_AYUV_FUNCTIONS (16, 0);
819 CREATE_AYUV_FUNCTIONS (8, 8);
820
821 #define CREATE_ARGB_FUNCTIONS(depth, name, shift, a, r, g, b) \
822 static void \
823 gst_shape_wipe_blend_##name##_##depth (GstShapeWipe * self, GstBuffer * inbuf, \
824     GstBuffer * maskbuf, GstBuffer * outbuf) \
825 { \
826   const guint##depth *mask = (const guint##depth *) GST_BUFFER_DATA (maskbuf); \
827   const guint8 *input = (const guint8 *) GST_BUFFER_DATA (inbuf); \
828   guint8 *output = (guint8 *) GST_BUFFER_DATA (outbuf); \
829   guint i, j; \
830   guint mask_increment = ((depth == 16) ? GST_ROUND_UP_2 (self->width) : \
831                            GST_ROUND_UP_4 (self->width)) - self->width; \
832   gfloat position = self->mask_position; \
833   gfloat low = position - (self->mask_border / 2.0f); \
834   gfloat high = position + (self->mask_border / 2.0f); \
835   guint32 low_i, high_i, position_i, round_i; \
836   gint width = self->width, height = self->height; \
837   \
838   if (low < 0.0f) { \
839     high = 0.0f; \
840     low = 0.0f; \
841   } \
842   \
843   if (high > 1.0f) { \
844     low = 1.0f; \
845     high = 1.0f; \
846   } \
847   \
848   position_i = position * 65536; \
849   low_i = low * 65536; \
850   high_i = high * 65536; \
851   round_i = (high_i - low_i) >> 1; \
852   \
853   for (i = 0; i < height; i++) { \
854     for (j = 0; j < width; j++) { \
855       guint32 in = *mask << shift; \
856       \
857       if (in < low) { \
858         output[a] = 0x00;       /* A */ \
859         output[r] = 0x00;       /* R */ \
860         output[g] = 0x00;       /* G */ \
861         output[b] = 0x00;       /* B */ \
862       } else if (in >= high) { \
863         output[a] = 0xff;       /* A */ \
864         output[r] = input[r];   /* R */ \
865         output[g] = input[g];   /* G */ \
866         output[b] = input[b];   /* B */ \
867       } else { \
868         guint32 val; \
869         /* Note: This will never overflow or be larger than 255! */ \
870         val = ((((in - low_i) << 16) + round_i) / (high_i - low_i)) >> 8; \
871         \
872         output[a] = val;        /* A */ \
873         output[r] = input[r];   /* R */ \
874         output[g] = input[g];   /* G */ \
875         output[b] = input[b];   /* B */ \
876       } \
877       \
878       mask++; \
879       input += 4; \
880       output += 4; \
881     } \
882     mask += mask_increment; \
883   } \
884 }
885
886 CREATE_ARGB_FUNCTIONS (16, argb, 0, 0, 1, 2, 3);
887 CREATE_ARGB_FUNCTIONS (8, argb, 8, 0, 1, 2, 3);
888
889 CREATE_ARGB_FUNCTIONS (16, bgra, 0, 3, 2, 1, 0);
890 CREATE_ARGB_FUNCTIONS (8, bgra, 8, 3, 2, 1, 0);
891
892 static GstFlowReturn
893 gst_shape_wipe_video_sink_chain (GstPad * pad, GstBuffer * buffer)
894 {
895   GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
896   GstFlowReturn ret = GST_FLOW_OK;
897   GstBuffer *mask = NULL, *outbuf = NULL;
898   GstClockTime timestamp;
899   gboolean new_outbuf = FALSE;
900
901   if (G_UNLIKELY (self->fmt == GST_VIDEO_FORMAT_UNKNOWN))
902     goto not_negotiated;
903
904   timestamp = GST_BUFFER_TIMESTAMP (buffer);
905   timestamp =
906       gst_segment_to_stream_time (&self->segment, GST_FORMAT_TIME, timestamp);
907
908   if (GST_CLOCK_TIME_IS_VALID (timestamp))
909     gst_object_sync_values (G_OBJECT (self), timestamp);
910
911   GST_DEBUG_OBJECT (self,
912       "Blending buffer with timestamp %" GST_TIME_FORMAT " at position %lf",
913       GST_TIME_ARGS (timestamp), self->mask_position);
914
915   g_mutex_lock (self->mask_mutex);
916   if (self->shutdown)
917     goto shutdown;
918
919   if (!self->mask)
920     g_cond_wait (self->mask_cond, self->mask_mutex);
921
922   if (self->mask == NULL || self->shutdown) {
923     goto shutdown;
924   } else {
925     mask = gst_buffer_ref (self->mask);
926   }
927   g_mutex_unlock (self->mask_mutex);
928
929   if (!gst_shape_wipe_do_qos (self, GST_BUFFER_TIMESTAMP (buffer)))
930     goto qos;
931
932   /* Try to blend inplace, if it's not possible
933    * get a new buffer from downstream. */
934   if (!gst_buffer_is_writable (buffer)) {
935     ret =
936         gst_pad_alloc_buffer_and_set_caps (self->srcpad, GST_BUFFER_OFFSET_NONE,
937         GST_BUFFER_SIZE (buffer), GST_PAD_CAPS (self->srcpad), &outbuf);
938     if (G_UNLIKELY (ret != GST_FLOW_OK))
939       goto alloc_failed;
940
941     gst_buffer_copy_metadata (outbuf, buffer, GST_BUFFER_COPY_ALL);
942     new_outbuf = TRUE;
943   } else {
944     outbuf = buffer;
945   }
946
947   if (self->fmt == GST_VIDEO_FORMAT_AYUV && self->mask_bpp == 16)
948     gst_shape_wipe_blend_ayuv_16 (self, buffer, mask, outbuf);
949   else if (self->fmt == GST_VIDEO_FORMAT_AYUV)
950     gst_shape_wipe_blend_ayuv_8 (self, buffer, mask, outbuf);
951   else if (self->fmt == GST_VIDEO_FORMAT_ARGB && self->mask_bpp == 16)
952     gst_shape_wipe_blend_argb_16 (self, buffer, mask, outbuf);
953   else if (self->fmt == GST_VIDEO_FORMAT_ARGB)
954     gst_shape_wipe_blend_argb_8 (self, buffer, mask, outbuf);
955   else if (self->fmt == GST_VIDEO_FORMAT_BGRA && self->mask_bpp == 16)
956     gst_shape_wipe_blend_bgra_16 (self, buffer, mask, outbuf);
957   else if (self->fmt == GST_VIDEO_FORMAT_BGRA)
958     gst_shape_wipe_blend_bgra_8 (self, buffer, mask, outbuf);
959   else
960     g_assert_not_reached ();
961
962   gst_buffer_unref (mask);
963   if (new_outbuf)
964     gst_buffer_unref (buffer);
965
966   ret = gst_pad_push (self->srcpad, outbuf);
967   if (G_UNLIKELY (ret != GST_FLOW_OK))
968     goto push_failed;
969
970   return ret;
971
972   /* Errors */
973 not_negotiated:
974   GST_ERROR_OBJECT (self, "No valid caps yet");
975   gst_buffer_unref (buffer);
976   return GST_FLOW_NOT_NEGOTIATED;
977 shutdown:
978   GST_DEBUG_OBJECT (self, "Shutting down");
979   gst_buffer_unref (buffer);
980   return GST_FLOW_WRONG_STATE;
981 qos:
982   GST_DEBUG_OBJECT (self, "Dropping buffer because of QoS");
983   gst_buffer_unref (buffer);
984   gst_buffer_unref (mask);
985   return GST_FLOW_OK;
986 alloc_failed:
987   GST_ERROR_OBJECT (self, "Buffer allocation from downstream failed: %s",
988       gst_flow_get_name (ret));
989   gst_buffer_unref (buffer);
990   gst_buffer_unref (mask);
991   return ret;
992 push_failed:
993   GST_ERROR_OBJECT (self, "Pushing buffer downstream failed: %s",
994       gst_flow_get_name (ret));
995   return ret;
996 }
997
998 static GstFlowReturn
999 gst_shape_wipe_mask_sink_chain (GstPad * pad, GstBuffer * buffer)
1000 {
1001   GstShapeWipe *self = GST_SHAPE_WIPE (GST_PAD_PARENT (pad));
1002   GstFlowReturn ret = GST_FLOW_OK;
1003
1004   g_mutex_lock (self->mask_mutex);
1005   GST_DEBUG_OBJECT (self, "Setting new mask buffer: %" GST_PTR_FORMAT, buffer);
1006
1007   gst_buffer_replace (&self->mask, buffer);
1008   g_cond_signal (self->mask_cond);
1009   g_mutex_unlock (self->mask_mutex);
1010
1011   gst_buffer_unref (buffer);
1012
1013   return ret;
1014 }
1015
1016 static GstStateChangeReturn
1017 gst_shape_wipe_change_state (GstElement * element, GstStateChange transition)
1018 {
1019   GstShapeWipe *self = GST_SHAPE_WIPE (element);
1020   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1021
1022   switch (transition) {
1023     case GST_STATE_CHANGE_READY_TO_PAUSED:
1024       self->shutdown = FALSE;
1025       break;
1026     case GST_STATE_CHANGE_PAUSED_TO_READY:
1027       /* Unblock video sink chain function */
1028       g_mutex_lock (self->mask_mutex);
1029       self->shutdown = TRUE;
1030       g_cond_signal (self->mask_cond);
1031       g_mutex_unlock (self->mask_mutex);
1032       break;
1033     default:
1034       break;
1035   }
1036
1037   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1038     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1039
1040   switch (transition) {
1041     case GST_STATE_CHANGE_PAUSED_TO_READY:
1042       gst_shape_wipe_reset (self);
1043       break;
1044     default:
1045       break;
1046   }
1047
1048   return ret;
1049 }
1050
1051 static gboolean
1052 gst_shape_wipe_video_sink_event (GstPad * pad, GstEvent * event)
1053 {
1054   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1055   gboolean ret;
1056
1057   GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1058
1059   switch (GST_EVENT_TYPE (event)) {
1060     case GST_EVENT_NEWSEGMENT:{
1061       GstFormat fmt;
1062       gboolean is_update;
1063       gint64 start, end, base;
1064       gdouble rate;
1065
1066       gst_event_parse_new_segment (event, &is_update, &rate, &fmt, &start,
1067           &end, &base);
1068       if (fmt == GST_FORMAT_TIME) {
1069         GST_DEBUG_OBJECT (pad,
1070             "Got NEWSEGMENT event in GST_FORMAT_TIME, passing on (%"
1071             GST_TIME_FORMAT " - %" GST_TIME_FORMAT ")", GST_TIME_ARGS (start),
1072             GST_TIME_ARGS (end));
1073         gst_segment_set_newsegment (&self->segment, is_update, rate, fmt, start,
1074             end, base);
1075       } else {
1076         gst_segment_init (&self->segment, GST_FORMAT_TIME);
1077       }
1078     }
1079       /* fall through */
1080     case GST_EVENT_FLUSH_STOP:
1081       gst_shape_wipe_reset_qos (self);
1082       /* fall through */
1083     default:
1084       ret = gst_pad_push_event (self->srcpad, event);
1085       break;
1086   }
1087
1088   gst_object_unref (self);
1089   return ret;
1090 }
1091
1092 static gboolean
1093 gst_shape_wipe_mask_sink_event (GstPad * pad, GstEvent * event)
1094 {
1095   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1096
1097   GST_DEBUG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
1098
1099   switch (GST_EVENT_TYPE (event)) {
1100     case GST_EVENT_FLUSH_STOP:
1101       g_mutex_lock (self->mask_mutex);
1102       gst_buffer_replace (&self->mask, NULL);
1103       g_mutex_unlock (self->mask_mutex);
1104       break;
1105     default:
1106       break;
1107   }
1108
1109   /* Dropping all events here */
1110   gst_event_unref (event);
1111
1112   gst_object_unref (self);
1113   return TRUE;
1114 }
1115
1116 static gboolean
1117 gst_shape_wipe_src_event (GstPad * pad, GstEvent * event)
1118 {
1119   GstShapeWipe *self = GST_SHAPE_WIPE (gst_pad_get_parent (pad));
1120   gboolean ret;
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)