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