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);
142 GESTrackElementClass *track_element_class = GES_TRACK_ELEMENT_CLASS (klass);
144 object_class = G_OBJECT_CLASS (klass);
146 object_class->get_property = ges_video_transition_get_property;
147 object_class->set_property = ges_video_transition_set_property;
148 object_class->dispose = ges_video_transition_dispose;
149 object_class->finalize = ges_video_transition_finalize;
151 track_element_class->ABI.abi.default_track_type = GES_TRACK_TYPE_VIDEO;
154 * GESVideoTransition:border:
156 * This value represents the border width of the transition.
159 properties[PROP_BORDER] =
160 g_param_spec_uint ("border", "Border", "The border width", 0,
161 G_MAXUINT, 0, G_PARAM_READWRITE);
162 g_object_class_install_property (object_class, PROP_BORDER,
163 properties[PROP_BORDER]);
166 * GESVideoTransition:type:
168 * The #GESVideoStandardTransitionType currently applied on the object
170 * Deprecated:1.20: Use ges_timeline_element_[sg]et_child_property instead.
172 properties[PROP_TRANSITION_TYPE] =
173 g_param_spec_enum ("transition-type", "Transition type",
174 "The type of the transition", GES_VIDEO_STANDARD_TRANSITION_TYPE_TYPE,
175 GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE, G_PARAM_READWRITE);
176 g_object_class_install_property (object_class, PROP_TRANSITION_TYPE,
177 properties[PROP_TRANSITION_TYPE]);
180 * GESVideoTransition:invert:
182 * This value represents the direction of the transition.
184 * Deprecated:1.20: Use ges_timeline_element_[sg]et_child_property instead.
186 properties[PROP_INVERT] =
187 g_param_spec_boolean ("invert", "Invert",
188 "Whether the transition is inverted", FALSE, G_PARAM_READWRITE);
189 g_object_class_install_property (object_class, PROP_INVERT,
190 properties[PROP_INVERT]);
192 toclass = GES_TRACK_ELEMENT_CLASS (klass);
193 toclass->create_element = ges_video_transition_create_element;
195 element_class->set_priority = _set_priority;
199 ges_video_transition_init (GESVideoTransition * self)
201 self->priv = ges_video_transition_get_instance_private (self);
203 self->priv->fade_in_control_source = NULL;
204 self->priv->fade_out_control_source = NULL;
205 self->priv->smpte_control_source = NULL;
206 self->priv->smpte = NULL;
207 self->priv->mixer_sink = NULL;
208 self->priv->mixer = NULL;
209 self->priv->mixer_sinka = NULL;
210 self->priv->mixer_sinkb = NULL;
211 self->priv->pending_type = GES_VIDEO_STANDARD_TRANSITION_TYPE_NONE;
212 self->priv->pending_border_value = 0;
213 self->priv->pending_inverted = TRUE;
217 ges_video_transition_release_mixer (GESVideoTransition * self)
219 GESVideoTransitionPrivate *priv = self->priv;
221 if (priv->mixer_ghosta && priv->mixer_ghostb) {
222 gst_element_release_request_pad (priv->mixer, priv->mixer_ghosta);
223 gst_element_release_request_pad (priv->mixer, priv->mixer_ghostb);
224 gst_clear_object (&priv->mixer_ghosta);
225 gst_clear_object (&priv->mixer_ghostb);
228 gst_clear_object (&priv->mixer_sinka);
229 gst_clear_object (&priv->mixer_sinkb);
230 gst_clear_object (&priv->mixer);
234 ges_video_transition_dispose (GObject * object)
236 GESVideoTransition *self = GES_VIDEO_TRANSITION (object);
237 GESVideoTransitionPrivate *priv = self->priv;
239 GST_DEBUG ("disposing");
241 if (priv->fade_in_control_source) {
242 gst_object_unref (priv->fade_in_control_source);
243 priv->fade_in_control_source = NULL;
246 if (priv->fade_out_control_source) {
247 gst_object_unref (priv->fade_out_control_source);
248 priv->fade_out_control_source = NULL;
251 if (priv->smpte_control_source) {
252 gst_object_unref (priv->smpte_control_source);
253 priv->smpte_control_source = NULL;
256 ges_video_transition_release_mixer (self);
258 g_signal_handlers_disconnect_by_func (GES_TRACK_ELEMENT (self),
259 duration_changed_cb, NULL);
261 G_OBJECT_CLASS (ges_video_transition_parent_class)->dispose (object);
265 ges_video_transition_finalize (GObject * object)
267 G_OBJECT_CLASS (ges_video_transition_parent_class)->finalize (object);
271 ges_video_transition_get_property (GObject * object,
272 guint property_id, GValue * value, GParamSpec * pspec)
274 GESVideoTransition *tr = GES_VIDEO_TRANSITION (object);
276 switch (property_id) {
278 g_value_set_uint (value, ges_video_transition_get_border (tr));
280 case PROP_TRANSITION_TYPE:
281 g_value_set_enum (value, ges_video_transition_get_transition_type (tr));
284 g_value_set_boolean (value, ges_video_transition_is_inverted (tr));
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
292 ges_video_transition_set_property (GObject * object,
293 guint property_id, const GValue * value, GParamSpec * pspec)
295 GESVideoTransition *tr = GES_VIDEO_TRANSITION (object);
297 switch (property_id) {
299 ges_video_transition_set_border_internal (tr, g_value_get_uint (value));
301 case PROP_TRANSITION_TYPE:
302 ges_video_transition_set_transition_type_internal (tr,
303 g_value_get_enum (value));
306 ges_video_transition_set_inverted_internal (tr,
307 g_value_get_boolean (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
314 static GstTimedValueControlSource *
315 set_interpolation (GstObject * element, GESVideoTransitionPrivate * priv,
316 const gchar * propname)
318 GstControlSource *control_source;
320 g_object_set (element, propname, (gfloat) 0.0, NULL);
322 control_source = gst_interpolation_control_source_new ();
323 gst_object_add_control_binding (GST_OBJECT (element),
324 gst_direct_control_binding_new (GST_OBJECT (element), propname,
326 g_object_set (control_source, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
328 return GST_TIMED_VALUE_CONTROL_SOURCE (control_source);
332 ges_video_transition_create_element (GESTrackElement * object)
334 GstElement *topbin, *iconva, *iconvb;
335 GstElement *mixer = NULL;
336 GstPad *sinka_target, *sinkb_target, *src_target, *sinka, *sinkb, *src;
337 GESVideoTransition *self;
338 GESVideoTransitionPrivate *priv;
339 const gchar *smpte_properties[] = { "invert", "border", NULL };
341 self = GES_VIDEO_TRANSITION (object);
344 GST_LOG ("creating a video bin");
346 topbin = gst_bin_new ("transition-bin");
348 iconva = gst_element_factory_make ("videoconvert", "tr-csp-a");
349 iconvb = gst_element_factory_make ("videoconvert", "tr-csp-b");
351 gst_element_factory_make ("framepositioner", "frame_tagger");
352 g_object_set (priv->positioner, "zorder",
353 G_MAXUINT - GES_TIMELINE_ELEMENT_PRIORITY (self), NULL);
355 gst_bin_add_many (GST_BIN (topbin), iconva, iconvb, priv->positioner, NULL);
358 g_object_new (GES_TYPE_SMART_MIXER, "name",
359 GES_TIMELINE_ELEMENT_NAME (object), NULL);
360 GES_SMART_MIXER (mixer)->is_transition = TRUE;
361 gst_util_set_object_arg (G_OBJECT (GES_SMART_MIXER (mixer)->mixer),
362 "background", "transparent");
363 gst_bin_add (GST_BIN (topbin), mixer);
366 (GstPad *) link_element_to_mixer_with_smpte (GST_BIN (topbin), iconva,
367 mixer, GES_VIDEO_STANDARD_TRANSITION_TYPE_BAR_WIPE_LR, NULL, priv,
368 &priv->mixer_ghosta);
370 (GstPad *) link_element_to_mixer_with_smpte (GST_BIN (topbin), iconvb,
371 mixer, GES_VIDEO_STANDARD_TRANSITION_TYPE_BAR_WIPE_LR, &priv->smpte,
372 priv, &priv->mixer_ghostb);
373 g_object_set (priv->mixer_sinka, "zorder", 0, NULL);
374 g_object_set (priv->mixer_sinkb, "zorder", 1, NULL);
375 gst_util_set_object_arg (G_OBJECT (priv->mixer_sinka), "operator",
377 GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE ? "source" : "over");
378 gst_util_set_object_arg (G_OBJECT (priv->mixer_sinkb), "operator",
380 GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE ? "add" : "over");
382 fast_element_link (mixer, priv->positioner);
384 sinka_target = gst_element_get_static_pad (iconva, "sink");
385 sinkb_target = gst_element_get_static_pad (iconvb, "sink");
386 src_target = gst_element_get_static_pad (priv->positioner, "src");
388 sinka = gst_ghost_pad_new ("sinka", sinka_target);
389 sinkb = gst_ghost_pad_new ("sinkb", sinkb_target);
390 src = gst_ghost_pad_new ("src", src_target);
392 gst_element_add_pad (topbin, src);
393 gst_element_add_pad (topbin, sinka);
394 gst_element_add_pad (topbin, sinkb);
396 gst_object_unref (sinka_target);
397 gst_object_unref (sinkb_target);
398 gst_object_unref (src_target);
400 /* set up interpolation */
402 priv->fade_out_control_source =
403 set_interpolation (GST_OBJECT (priv->mixer_ghosta), priv, "alpha");
404 priv->fade_in_control_source =
405 set_interpolation (GST_OBJECT (priv->mixer_ghostb), priv, "alpha");
406 priv->smpte_control_source =
407 set_interpolation (GST_OBJECT (priv->smpte), priv, "position");
408 priv->mixer = gst_object_ref (mixer);
410 if (priv->pending_type)
411 ges_video_transition_set_transition_type_internal (self,
414 ges_video_transition_set_transition_type_internal (self, priv->type);
416 ges_video_transition_duration_changed (object,
417 ges_timeline_element_get_duration (GES_TIMELINE_ELEMENT (object)));
419 g_signal_connect (object, "notify::duration",
420 G_CALLBACK (duration_changed_cb), NULL);
422 priv->pending_type = GES_VIDEO_STANDARD_TRANSITION_TYPE_NONE;
424 ges_track_element_add_children_props (GES_TRACK_ELEMENT (self),
425 priv->smpte, NULL, NULL, smpte_properties);
431 link_element_to_mixer_with_smpte (GstBin * bin, GstElement * element,
432 GstElement * mixer, gint type, GstElement ** smpteref,
433 GESVideoTransitionPrivate * priv, GstPad ** ghost)
435 GstPad *srcpad, *mixerpad;
436 GstElement *smptealpha = gst_element_factory_make ("smptealpha", NULL);
438 g_object_set (G_OBJECT (smptealpha),
439 "type", (gint) type, "invert", (gboolean) priv->pending_inverted,
440 "border", priv->pending_border_value, NULL);
441 gst_bin_add (bin, smptealpha);
443 fast_element_link (element, smptealpha);
447 *smpteref = smptealpha;
450 srcpad = gst_element_get_static_pad (smptealpha, "src");
451 *ghost = ges_smart_mixer_get_mixer_pad (GES_SMART_MIXER (mixer), &mixerpad);
452 gst_pad_link_full (srcpad, *ghost, GST_PAD_LINK_CHECK_NOTHING);
453 gst_object_unref (srcpad);
455 return G_OBJECT (mixerpad);
459 ges_video_transition_update_control_source (GstTimedValueControlSource * ts,
460 guint64 duration, gdouble start_value, gdouble end_value)
462 gst_timed_value_control_source_unset_all (ts);
463 gst_timed_value_control_source_set (ts, 0, start_value);
464 gst_timed_value_control_source_set (ts, duration, end_value);
468 ges_video_transition_update_control_sources (GESVideoTransition * self,
469 GESVideoStandardTransitionType type)
471 GESVideoTransitionPrivate *priv = self->priv;
473 ges_timeline_element_get_duration (GES_TIMELINE_ELEMENT (self));
475 GST_LOG ("updating controller");
476 if (type == GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE) {
477 ges_video_transition_update_control_source
478 (priv->fade_in_control_source, duration, 0.0, 1.0);
479 ges_video_transition_update_control_source
480 (priv->fade_out_control_source, duration, 1.0, 0.0);
481 ges_video_transition_update_control_source (priv->smpte_control_source,
484 ges_video_transition_update_control_source
485 (priv->fade_in_control_source, duration, 1.0, 1.0);
486 ges_video_transition_update_control_source
487 (priv->fade_out_control_source, duration, 1.0, 1.0);
488 ges_video_transition_update_control_source (priv->smpte_control_source,
491 GST_LOG ("done updating controller");
495 ges_video_transition_duration_changed (GESTrackElement * object,
498 GESVideoTransition *self = GES_VIDEO_TRANSITION (object);
500 ges_video_transition_update_control_sources (self, self->priv->type);
504 ges_video_transition_set_border_internal (GESVideoTransition * self,
507 GESVideoTransitionPrivate *priv = self->priv;
510 priv->pending_border_value = value;
513 g_object_set (priv->smpte, "border", value, NULL);
517 ges_video_transition_set_inverted_internal (GESVideoTransition *
518 self, gboolean inverted)
520 GESVideoTransitionPrivate *priv = self->priv;
523 priv->pending_inverted = !inverted;
526 g_object_set (priv->smpte, "invert", !inverted, NULL);
529 static inline gboolean
530 ges_video_transition_set_transition_type_internal (GESVideoTransition
531 * self, GESVideoStandardTransitionType type)
533 GESVideoTransitionPrivate *priv = self->priv;
535 GST_DEBUG ("%p %d => %d", self, priv->type, type);
538 priv->pending_type = type;
542 if (type == priv->type) {
543 GST_DEBUG ("%d type is already set on this transition\n", type);
547 ges_video_transition_update_control_sources (self, type);
551 if (type != GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE) {
552 g_object_set (priv->smpte, "type", (gint) type, NULL);
555 gst_util_set_object_arg (G_OBJECT (priv->mixer_sinka), "operator",
557 GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE ? "source" : "over");
558 gst_util_set_object_arg (G_OBJECT (priv->mixer_sinkb), "operator",
560 GES_VIDEO_STANDARD_TRANSITION_TYPE_CROSSFADE ? "add" : "over");
566 * ges_video_transition_set_border:
567 * @self: The #GESVideoTransition to set the border to
568 * @value: The value of the border to set on @object
570 * Set the border property of @self, this value represents
571 * the border width of the transition. In case this value does
572 * not make sense for the current transition type, it is cached
575 * Deprecated:1.20: Use ges_timeline_element_set_child_property instead.
578 ges_video_transition_set_border (GESVideoTransition * self, guint value)
580 ges_video_transition_set_border_internal (self, value);
582 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BORDER]);
586 * ges_video_transition_get_border:
587 * @self: The #GESVideoTransition to get the border from
589 * Get the border property of @self, this value represents
590 * the border width of the transition.
592 * Returns: The border values of @self or -1 if not meaningful
593 * (this will happen when not using a smpte transition).
595 * Deprecated:1.20: Use ges_timeline_element_get_child_property instead.
598 ges_video_transition_get_border (GESVideoTransition * self)
602 if (!self->priv->smpte) {
606 g_object_get (self->priv->smpte, "border", &value, NULL);
612 * ges_video_transition_set_inverted:
613 * @self: The #GESVideoTransition to set invert on
614 * @inverted: %TRUE if the transition should be inverted %FALSE otherwise
616 * Set the invert property of @self, this value represents
617 * the direction of the transition. In case this value does
618 * not make sense for the current transition type, it is cached
621 * Deprecated:1.20: Use ges_timeline_element_set_child_property instead.
624 ges_video_transition_set_inverted (GESVideoTransition * self, gboolean inverted)
626 ges_video_transition_set_inverted_internal (self, inverted);
628 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INVERT]);
632 * ges_video_transition_is_inverted:
633 * @self: The #GESVideoTransition to get the inversion from
635 * Get the invert property of @self, this value represents
636 * the direction of the transition.
638 * Returns: The invert value of @self
640 * Deprecated:1.20: Use ges_timeline_element_get_child_property instead.
643 ges_video_transition_is_inverted (GESVideoTransition * self)
647 if (!self->priv->smpte) {
651 g_object_get (self->priv->smpte, "invert", &inverted, NULL);
657 * ges_video_transition_set_transition_type:
658 * @self: a #GESVideoTransition
659 * @type: a #GESVideoStandardTransitionType
661 * Sets the transition being used to @type.
663 * Returns: %TRUE if the transition type was properly changed, else %FALSE.
666 ges_video_transition_set_transition_type (GESVideoTransition * self,
667 GESVideoStandardTransitionType type)
669 gboolean ret = ges_video_transition_set_transition_type_internal (self, type);
671 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TRANSITION_TYPE]);
677 * ges_video_transition_get_transition_type:
678 * @trans: a #GESVideoTransition
680 * Get the transition type used by @trans.
682 * Returns: The transition type used by @trans.
684 GESVideoStandardTransitionType
685 ges_video_transition_get_transition_type (GESVideoTransition * trans)
687 if (trans->priv->pending_type)
688 return trans->priv->pending_type;
689 return trans->priv->type;
692 /* ges_video_transition_new:
694 * Creates a new #GESVideoTransition.
696 * Returns: (transfer floating) (nullable): The newly created
697 * #GESVideoTransition, or %NULL if there was an error.
700 ges_video_transition_new (void)
702 GESVideoTransition *res;
703 GESAsset *asset = ges_asset_request (GES_TYPE_VIDEO_TRANSITION, NULL, NULL);
705 res = GES_VIDEO_TRANSITION (ges_asset_extract (asset, NULL));
706 gst_object_unref (asset);