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