1 /* GStreamer Editing Services
2 * Copyright (C) 2010 Brandon Lewis <brandon.lewis@collabora.co.uk>
3 * 2010 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:gesvideotransition
23 * @title: GESVideoTransition
24 * @short_description: implements video crossfade transition
31 #include "ges-internal.h"
32 #include "ges-smart-video-mixer.h"
34 #include <gst/controller/gstdirectcontrolbinding.h>
36 #define parent_class ges_video_transition_parent_class
39 ges_video_transition_set_border_internal (GESVideoTransition * self,
42 ges_video_transition_set_inverted_internal (GESVideoTransition *
43 self, gboolean inverted);
44 static inline gboolean
45 ges_video_transition_set_transition_type_internal (GESVideoTransition
46 * self, GESVideoStandardTransitionType type);
47 struct _GESVideoTransitionPrivate
49 GESVideoStandardTransitionType type;
51 /* prevents cases where the transitions have not been created yet */
52 GESVideoStandardTransitionType pending_type;
54 /* these enable video interpolation */
55 GstTimedValueControlSource *fade_in_control_source;
56 GstTimedValueControlSource *fade_out_control_source;
57 GstTimedValueControlSource *smpte_control_source;
59 /* so we can support changing between wipes */
71 /* This is in case the smpte doesn't exist yet */
72 gint pending_border_value;
73 gboolean pending_inverted;
75 GstElement *positioner;
87 static GParamSpec *properties[PROP_LAST];
89 G_DEFINE_TYPE_WITH_PRIVATE (GESVideoTransition, ges_video_transition,
92 #define fast_element_link(a,b) gst_element_link_pads_full((a),"src",(b),"sink",GST_PAD_LINK_CHECK_NOTHING)
94 static GObject *link_element_to_mixer_with_smpte (GstBin * bin,
95 GstElement * element, GstElement * mixer, gint type,
96 GstElement ** smpteref, GESVideoTransitionPrivate * priv, GstPad ** ghost);
99 ges_video_transition_duration_changed (GESTrackElement * self,
102 static GstElement *ges_video_transition_create_element (GESTrackElement * self);
104 static void ges_video_transition_dispose (GObject * object);
106 static void ges_video_transition_finalize (GObject * object);
108 static void ges_video_transition_get_property (GObject * object, guint
109 property_id, GValue * value, GParamSpec * pspec);
111 static void ges_video_transition_set_property (GObject * object, guint
112 property_id, const GValue * value, GParamSpec * pspec);
115 duration_changed_cb (GESTrackElement * self, GParamSpec * arg G_GNUC_UNUSED)
117 ges_video_transition_duration_changed (self,
118 ges_timeline_element_get_duration (GES_TIMELINE_ELEMENT (self)));
122 _set_priority (GESTimelineElement * element, guint32 priority)
125 GESVideoTransition *self = GES_VIDEO_TRANSITION (element);
127 res = GES_TIMELINE_ELEMENT_CLASS (parent_class)->set_priority (element,
130 if (res && self->priv->positioner)
131 g_object_set (self->priv->positioner, "zorder", G_MAXUINT - priority, NULL);
137 ges_video_transition_class_init (GESVideoTransitionClass * klass)
139 GObjectClass *object_class;
140 GESTrackElementClass *toclass;
141 GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
143 object_class = G_OBJECT_CLASS (klass);
145 object_class->get_property = ges_video_transition_get_property;
146 object_class->set_property = ges_video_transition_set_property;
147 object_class->dispose = ges_video_transition_dispose;
148 object_class->finalize = ges_video_transition_finalize;
151 * GESVideoTransition:border:
153 * This value represents the border width of the transition.
156 properties[PROP_BORDER] =
157 g_param_spec_uint ("border", "Border", "The border width", 0,
158 G_MAXUINT, 0, G_PARAM_READWRITE);
159 g_object_class_install_property (object_class, PROP_BORDER,
160 properties[PROP_BORDER]);
163 * GESVideoTransition:type:
165 * The #GESVideoStandardTransitionType currently applied on the object
168 properties[PROP_TRANSITION_TYPE] =
169 g_param_spec_enum ("transition-type", "Transition type",
170 "The type of the transition", GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE,
171 GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE, G_PARAM_READWRITE);
172 g_object_class_install_property (object_class, PROP_TRANSITION_TYPE,
173 properties[PROP_TRANSITION_TYPE]);
176 * GESVideoTransition:invert:
178 * This value represents the direction of the transition.
181 properties[PROP_INVERT] =
182 g_param_spec_boolean ("invert", "Invert",
183 "Whether the transition is inverted", FALSE, G_PARAM_READWRITE);
184 g_object_class_install_property (object_class, PROP_INVERT,
185 properties[PROP_INVERT]);
187 toclass = GES_TRACK_ELEMENT_CLASS (klass);
188 toclass->create_element = ges_video_transition_create_element;
190 element_class->set_priority = _set_priority;
194 ges_video_transition_init (GESVideoTransition * self)
196 self->priv = ges_video_transition_get_instance_private (self);
198 self->priv->fade_in_control_source = NULL;
199 self->priv->fade_out_control_source = NULL;
200 self->priv->smpte_control_source = NULL;
201 self->priv->smpte = NULL;
202 self->priv->mixer_sink = NULL;
203 self->priv->mixer = NULL;
204 self->priv->mixer_sinka = NULL;
205 self->priv->mixer_sinkb = NULL;
206 self->priv->pending_type = GES_VIDEO_STANDARD_TRANSITION_TYPE_NONE;
207 self->priv->pending_border_value = 0;
208 self->priv->pending_inverted = TRUE;
212 ges_video_transition_release_mixer (GESVideoTransition * self)
214 GESVideoTransitionPrivate *priv = self->priv;
216 if (priv->mixer_ghosta && priv->mixer_ghostb) {
217 gst_element_release_request_pad (priv->mixer, priv->mixer_ghosta);
218 gst_element_release_request_pad (priv->mixer, priv->mixer_ghostb);
219 gst_clear_object (&priv->mixer_ghosta);
220 gst_clear_object (&priv->mixer_ghostb);
223 gst_clear_object (&priv->mixer_sinka);
224 gst_clear_object (&priv->mixer_sinkb);
225 gst_clear_object (&priv->mixer);
229 ges_video_transition_dispose (GObject * object)
231 GESVideoTransition *self = GES_VIDEO_TRANSITION (object);
232 GESVideoTransitionPrivate *priv = self->priv;
234 GST_DEBUG ("disposing");
236 if (priv->fade_in_control_source) {
237 gst_object_unref (priv->fade_in_control_source);
238 priv->fade_in_control_source = NULL;
241 if (priv->fade_out_control_source) {
242 gst_object_unref (priv->fade_out_control_source);
243 priv->fade_out_control_source = NULL;
246 if (priv->smpte_control_source) {
247 gst_object_unref (priv->smpte_control_source);
248 priv->smpte_control_source = NULL;
251 ges_video_transition_release_mixer (self);
253 g_signal_handlers_disconnect_by_func (GES_TRACK_ELEMENT (self),
254 duration_changed_cb, NULL);
256 G_OBJECT_CLASS (ges_video_transition_parent_class)->dispose (object);
260 ges_video_transition_finalize (GObject * object)
262 G_OBJECT_CLASS (ges_video_transition_parent_class)->finalize (object);
266 ges_video_transition_get_property (GObject * object,
267 guint property_id, GValue * value, GParamSpec * pspec)
269 GESVideoTransition *tr = GES_VIDEO_TRANSITION (object);
271 switch (property_id) {
273 g_value_set_uint (value, ges_video_transition_get_border (tr));
275 case PROP_TRANSITION_TYPE:
276 g_value_set_enum (value, ges_video_transition_get_transition_type (tr));
279 g_value_set_boolean (value, ges_video_transition_is_inverted (tr));
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
287 ges_video_transition_set_property (GObject * object,
288 guint property_id, const GValue * value, GParamSpec * pspec)
290 GESVideoTransition *tr = GES_VIDEO_TRANSITION (object);
292 switch (property_id) {
294 ges_video_transition_set_border_internal (tr, g_value_get_uint (value));
296 case PROP_TRANSITION_TYPE:
297 ges_video_transition_set_transition_type_internal (tr,
298 g_value_get_enum (value));
301 ges_video_transition_set_inverted_internal (tr,
302 g_value_get_boolean (value));
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
309 static GstTimedValueControlSource *
310 set_interpolation (GstObject * element, GESVideoTransitionPrivate * priv,
311 const gchar * propname)
313 GstControlSource *control_source;
315 g_object_set (element, propname, (gfloat) 0.0, NULL);
317 control_source = gst_interpolation_control_source_new ();
318 gst_object_add_control_binding (GST_OBJECT (element),
319 gst_direct_control_binding_new (GST_OBJECT (element), propname,
321 g_object_set (control_source, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
323 return GST_TIMED_VALUE_CONTROL_SOURCE (control_source);
327 ges_video_transition_create_element (GESTrackElement * object)
329 GstElement *topbin, *iconva, *iconvb, *oconv;
330 GstElement *mixer = NULL;
331 GstPad *sinka_target, *sinkb_target, *src_target, *sinka, *sinkb, *src;
332 GESVideoTransition *self;
333 GESVideoTransitionPrivate *priv;
335 self = GES_VIDEO_TRANSITION (object);
338 GST_LOG ("creating a video bin");
340 topbin = gst_bin_new ("transition-bin");
342 iconva = gst_element_factory_make ("videoconvert", "tr-csp-a");
343 iconvb = gst_element_factory_make ("videoconvert", "tr-csp-b");
345 gst_element_factory_make ("framepositioner", "frame_tagger");
346 g_object_set (priv->positioner, "zorder",
347 G_MAXUINT - GES_TIMELINE_ELEMENT_PRIORITY (self), NULL);
348 oconv = gst_element_factory_make ("videoconvert", "tr-csp-output");
350 gst_bin_add_many (GST_BIN (topbin), iconva, iconvb, priv->positioner,
353 mixer = ges_smart_mixer_new (NULL);
354 GES_SMART_MIXER (mixer)->disable_zorder_alpha = TRUE;
355 g_object_set (GES_SMART_MIXER (mixer)->mixer, "background", 3, NULL); /* transparent */
356 gst_bin_add (GST_BIN (topbin), mixer);
359 (GstPad *) link_element_to_mixer_with_smpte (GST_BIN (topbin), iconva,
360 mixer, GES_VIDEO_STANDARD_TRANSITION_TYPE_BAR_WIPE_LR, NULL, priv,
361 &priv->mixer_ghosta);
363 (GstPad *) link_element_to_mixer_with_smpte (GST_BIN (topbin), iconvb,
364 mixer, GES_VIDEO_STANDARD_TRANSITION_TYPE_BAR_WIPE_LR, &priv->smpte,
365 priv, &priv->mixer_ghostb);
366 g_object_set (priv->mixer_sinka, "zorder", 0, NULL);
367 gst_util_set_object_arg (G_OBJECT (priv->mixer_sinka), "operator", "source");
368 g_object_set (priv->mixer_sinkb, "zorder", 1, NULL);
369 gst_util_set_object_arg (G_OBJECT (priv->mixer_sinkb), "operator", "add");
371 fast_element_link (mixer, priv->positioner);
372 fast_element_link (priv->positioner, oconv);
374 sinka_target = gst_element_get_static_pad (iconva, "sink");
375 sinkb_target = gst_element_get_static_pad (iconvb, "sink");
376 src_target = gst_element_get_static_pad (oconv, "src");
378 sinka = gst_ghost_pad_new ("sinka", sinka_target);
379 sinkb = gst_ghost_pad_new ("sinkb", sinkb_target);
380 src = gst_ghost_pad_new ("src", src_target);
382 gst_element_add_pad (topbin, src);
383 gst_element_add_pad (topbin, sinka);
384 gst_element_add_pad (topbin, sinkb);
386 gst_object_unref (sinka_target);
387 gst_object_unref (sinkb_target);
388 gst_object_unref (src_target);
390 /* set up interpolation */
392 priv->fade_out_control_source =
393 set_interpolation (GST_OBJECT (priv->mixer_ghosta), priv, "alpha");
394 priv->fade_in_control_source =
395 set_interpolation (GST_OBJECT (priv->mixer_ghostb), priv, "alpha");
396 priv->smpte_control_source =
397 set_interpolation (GST_OBJECT (priv->smpte), priv, "position");
398 priv->mixer = gst_object_ref (mixer);
400 if (priv->pending_type)
401 ges_video_transition_set_transition_type_internal (self,
404 ges_video_transition_set_transition_type_internal (self, priv->type);
406 ges_video_transition_duration_changed (object,
407 ges_timeline_element_get_duration (GES_TIMELINE_ELEMENT (object)));
409 g_signal_connect (object, "notify::duration",
410 G_CALLBACK (duration_changed_cb), NULL);
412 priv->pending_type = GES_VIDEO_STANDARD_TRANSITION_TYPE_NONE;
418 link_element_to_mixer_with_smpte (GstBin * bin, GstElement * element,
419 GstElement * mixer, gint type, GstElement ** smpteref,
420 GESVideoTransitionPrivate * priv, GstPad ** ghost)
422 GstPad *srcpad, *mixerpad;
423 GstElement *smptealpha = gst_element_factory_make ("smptealpha", NULL);
425 g_object_set (G_OBJECT (smptealpha),
426 "type", (gint) type, "invert", (gboolean) priv->pending_inverted,
427 "border", priv->pending_border_value, NULL);
428 gst_bin_add (bin, smptealpha);
430 fast_element_link (element, smptealpha);
434 *smpteref = smptealpha;
437 srcpad = gst_element_get_static_pad (smptealpha, "src");
438 *ghost = ges_smart_mixer_get_mixer_pad (GES_SMART_MIXER (mixer), &mixerpad);
439 gst_pad_link_full (srcpad, *ghost, GST_PAD_LINK_CHECK_NOTHING);
440 gst_object_unref (srcpad);
442 return G_OBJECT (mixerpad);
446 ges_video_transition_update_control_source (GstTimedValueControlSource * ts,
447 guint64 duration, gdouble start_value, gdouble end_value)
449 gst_timed_value_control_source_unset_all (ts);
450 gst_timed_value_control_source_set (ts, 0, start_value);
451 gst_timed_value_control_source_set (ts, duration, end_value);
455 ges_video_transition_update_control_sources (GESVideoTransition * self,
456 GESVideoStandardTransitionType type)
458 GESVideoTransitionPrivate *priv = self->priv;
460 ges_timeline_element_get_duration (GES_TIMELINE_ELEMENT (self));
462 GST_LOG ("updating controller");
463 if (type == GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE) {
464 ges_video_transition_update_control_source
465 (priv->fade_in_control_source, duration, 0.0, 1.0);
466 ges_video_transition_update_control_source
467 (priv->fade_out_control_source, duration, 1.0, 0.0);
468 ges_video_transition_update_control_source (priv->smpte_control_source,
471 ges_video_transition_update_control_source
472 (priv->fade_in_control_source, duration, 1.0, 1.0);
473 ges_video_transition_update_control_source
474 (priv->fade_out_control_source, duration, 1.0, 1.0);
475 ges_video_transition_update_control_source (priv->smpte_control_source,
478 GST_LOG ("done updating controller");
482 ges_video_transition_duration_changed (GESTrackElement * object,
485 GESVideoTransition *self = GES_VIDEO_TRANSITION (object);
487 ges_video_transition_update_control_sources (self, self->priv->type);
491 ges_video_transition_set_border_internal (GESVideoTransition * self,
494 GESVideoTransitionPrivate *priv = self->priv;
497 priv->pending_border_value = value;
500 g_object_set (priv->smpte, "border", value, NULL);
504 ges_video_transition_set_inverted_internal (GESVideoTransition *
505 self, gboolean inverted)
507 GESVideoTransitionPrivate *priv = self->priv;
510 priv->pending_inverted = !inverted;
513 g_object_set (priv->smpte, "invert", !inverted, NULL);
516 static inline gboolean
517 ges_video_transition_set_transition_type_internal (GESVideoTransition
518 * self, GESVideoStandardTransitionType type)
520 GESVideoTransitionPrivate *priv = self->priv;
522 GST_DEBUG ("%p %d => %d", self, priv->type, type);
525 priv->pending_type = type;
529 if (type == priv->type) {
530 GST_DEBUG ("%d type is already set on this transition\n", type);
534 ges_video_transition_update_control_sources (self, type);
538 if (type != GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE) {
539 g_object_set (priv->smpte, "type", (gint) type, NULL);
546 * ges_video_transition_set_border:
547 * @self: The #GESVideoTransition to set the border to
548 * @value: The value of the border to set on @object
550 * Set the border property of @self, this value represents
551 * the border width of the transition. In case this value does
552 * not make sense for the current transition type, it is cached
556 ges_video_transition_set_border (GESVideoTransition * self, guint value)
558 ges_video_transition_set_border_internal (self, value);
560 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BORDER]);
564 * ges_video_transition_get_border:
565 * @self: The #GESVideoTransition to get the border from
567 * Get the border property of @self, this value represents
568 * the border width of the transition.
570 * Returns: The border values of @self or -1 if not meaningful
571 * (this will happen when not using a smpte transition).
574 ges_video_transition_get_border (GESVideoTransition * self)
578 if (!self->priv->smpte) {
582 g_object_get (self->priv->smpte, "border", &value, NULL);
588 * ges_video_transition_set_inverted:
589 * @self: The #GESVideoTransition to set invert on
590 * @inverted: %TRUE if the transition should be inverted %FALSE otherwise
592 * Set the invert property of @self, this value represents
593 * the direction of the transition. In case this value does
594 * not make sense for the current transition type, it is cached
598 ges_video_transition_set_inverted (GESVideoTransition * self, gboolean inverted)
600 ges_video_transition_set_inverted_internal (self, inverted);
602 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INVERT]);
606 * ges_video_transition_is_inverted:
607 * @self: The #GESVideoTransition to get the inversion from
609 * Get the invert property of @self, this value represents
610 * the direction of the transition.
612 * Returns: The invert value of @self
615 ges_video_transition_is_inverted (GESVideoTransition * self)
619 if (!self->priv->smpte) {
623 g_object_get (self->priv->smpte, "invert", &inverted, NULL);
629 * ges_video_transition_set_transition_type:
630 * @self: a #GESVideoTransition
631 * @type: a #GESVideoStandardTransitionType
633 * Sets the transition being used to @type.
635 * Returns: %TRUE if the transition type was properly changed, else %FALSE.
638 ges_video_transition_set_transition_type (GESVideoTransition * self,
639 GESVideoStandardTransitionType type)
641 gboolean ret = ges_video_transition_set_transition_type_internal (self, type);
643 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TRANSITION_TYPE]);
649 * ges_video_transition_get_transition_type:
650 * @trans: a #GESVideoTransition
652 * Get the transition type used by @trans.
654 * Returns: The transition type used by @trans.
656 GESVideoStandardTransitionType
657 ges_video_transition_get_transition_type (GESVideoTransition * trans)
659 if (trans->priv->pending_type)
660 return trans->priv->pending_type;
661 return trans->priv->type;
665 * ges_video_transition_new:
667 * Creates a new #GESVideoTransition.
669 * Returns: (transfer floating) (nullable): The newly created
670 * #GESVideoTransition, or %NULL if there was an error.
673 ges_video_transition_new (void)
675 return g_object_new (GES_TYPE_VIDEO_TRANSITION, "track-type",
676 GES_TRACK_TYPE_VIDEO, NULL);