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