2 * Copyright (C) 2013 Mathieu Duponchelle <mduponchelle1@gmail.com>
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.
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.
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., 51 Franklin Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
26 #include <gst/video/video.h>
28 #include "gstframepositioner.h"
29 #include "ges-internal.h"
31 GST_DEBUG_CATEGORY_STATIC (_framepositioner);
32 #undef GST_CAT_DEFAULT
33 #define GST_CAT_DEFAULT _framepositioner
35 /* We need to define a max number of pixel so we can interpolate them */
36 #define MAX_PIXELS 100000
37 #define MIN_PIXELS -100000
39 static void gst_frame_positioner_set_property (GObject * object,
40 guint property_id, const GValue * value, GParamSpec * pspec);
41 static void gst_frame_positioner_get_property (GObject * object,
42 guint property_id, GValue * value, GParamSpec * pspec);
43 static GstFlowReturn gst_frame_positioner_transform_ip (GstBaseTransform *
44 trans, GstBuffer * buf);
47 gst_frame_positioner_meta_init (GstMeta * meta, gpointer params,
49 static gboolean gst_frame_positioner_meta_transform (GstBuffer * dest,
50 GstMeta * meta, GstBuffer * buffer, GQuark type, gpointer data);
65 static GParamSpec *properties[PROP_LAST];
67 static GstStaticPadTemplate gst_frame_positioner_src_template =
68 GST_STATIC_PAD_TEMPLATE ("src",
71 GST_STATIC_CAPS ("video/x-raw(ANY)")
74 static GstStaticPadTemplate gst_frame_positioner_sink_template =
75 GST_STATIC_PAD_TEMPLATE ("sink",
78 GST_STATIC_CAPS ("video/x-raw(ANY)")
81 G_DEFINE_TYPE (GstFramePositioner, gst_frame_positioner,
82 GST_TYPE_BASE_TRANSFORM);
85 gst_compositor_operator_get_type_and_default_value (int *default_operator_value)
87 static gsize _init = 0;
88 static int operator_value = 0;
89 static GType operator_gtype = G_TYPE_NONE;
91 if (g_once_init_enter (&_init)) {
92 GstElement *compositor =
93 gst_element_factory_create (ges_get_compositor_factory (), NULL);
95 GstPad *compositorPad =
96 gst_element_request_pad_simple (compositor, "sink_%u");
99 g_object_class_find_property (G_OBJECT_GET_CLASS (compositorPad),
104 g_value_get_enum (g_param_spec_get_default_value (pspec));
105 operator_gtype = pspec->value_type;
108 gst_element_release_request_pad (compositor, compositorPad);
109 gst_object_unref (compositorPad);
110 gst_object_unref (compositor);
112 g_once_init_leave (&_init, 1);
115 if (default_operator_value)
116 *default_operator_value = operator_value;
118 return operator_gtype;
122 _weak_notify_cb (GstFramePositioner * pos, GObject * old)
124 pos->current_track = NULL;
128 is_user_positionned (GstFramePositioner * self)
131 GParamSpec *positioning_props[] = {
132 properties[PROP_WIDTH],
133 properties[PROP_HEIGHT],
134 properties[PROP_POSX],
135 properties[PROP_POSY],
138 if (self->user_positioned)
141 for (i = 0; i < G_N_ELEMENTS (positioning_props); i++) {
142 GstControlBinding *b = gst_object_get_control_binding (GST_OBJECT (self),
143 positioning_props[i]->name);
146 gst_object_unref (b);
155 auto_position (GstFramePositioner * self)
157 gdouble scaled_width = -1, scaled_height = -1, x, y;
159 if (is_user_positionned (self)) {
160 GST_DEBUG_OBJECT (self, "Was positioned by the user, not auto positioning");
164 if (!self->natural_width || !self->natural_height)
167 if (self->track_width == self->natural_width &&
168 self->track_height == self->natural_height)
172 gst_util_uint64_scale_int (self->natural_height, self->track_width,
173 self->natural_width);
174 scaled_width = self->track_width;
175 if (scaled_height > self->track_height) {
176 scaled_height = self->track_height;
178 gst_util_uint64_scale_int (self->natural_width, self->track_height,
179 self->natural_height);
182 x = MAX (0, (self->track_width - scaled_width) / 2.f);
183 y = MAX (0, (self->track_height - scaled_height) / 2.f);
185 GST_INFO_OBJECT (self, "Scalling video to match track size from "
187 self->natural_width, self->natural_height, scaled_width, scaled_height);
188 self->width = scaled_width;
189 self->height = scaled_height;
199 gint old_track_value;
202 } RepositionPropertyData;
205 reposition_properties (GstFramePositioner * pos, gint old_track_width,
206 gint old_track_height)
209 RepositionPropertyData props_data[] = {
210 {&pos->width, old_track_width, pos->track_width, properties[PROP_WIDTH]},
211 {&pos->height, old_track_height, pos->track_height,
212 properties[PROP_HEIGHT]},
213 {&pos->posx, old_track_width, pos->track_width, properties[PROP_POSX]},
214 {&pos->posy, old_track_height, pos->track_height, properties[PROP_POSY]},
217 for (i = 0; i < G_N_ELEMENTS (props_data); i++) {
220 GstTimedValueControlSource *source = NULL;
222 RepositionPropertyData d = props_data[i];
223 GstControlBinding *binding =
224 gst_object_get_control_binding (GST_OBJECT (pos), d.pspec->name);
227 *(d.value) * (gdouble) d.track_value / (gdouble) d.old_track_value;
232 if (!GST_IS_DIRECT_CONTROL_BINDING (binding)) {
233 GST_FIXME_OBJECT (pos, "Implement support for control binding type: %s",
234 G_OBJECT_TYPE_NAME (binding));
239 g_object_get (binding, "control_source", &source, NULL);
240 if (!source || !GST_IS_TIMED_VALUE_CONTROL_SOURCE (source)) {
241 GST_FIXME_OBJECT (pos, "Implement support for control source type: %s",
242 source ? G_OBJECT_TYPE_NAME (source) : "NULL");
248 gst_timed_value_control_source_get_all (GST_TIMED_VALUE_CONTROL_SOURCE
254 g_object_get (binding, "absolute", &absolute, NULL);
255 for (tmp = values; tmp; tmp = tmp->next) {
256 GstTimedValue *value = tmp->data;
258 gst_timed_value_control_source_set (source, value->timestamp,
259 value->value * d.track_value / d.old_track_value);
262 g_list_free (values);
265 gst_clear_object (&source);
266 gst_object_unref (binding);
271 gst_frame_positioner_update_properties (GstFramePositioner * pos,
272 gboolean track_mixing, gint old_track_width, gint old_track_height)
276 if (pos->capsfilter == NULL)
279 caps = gst_caps_from_string ("video/x-raw(ANY)");
281 if (pos->track_width && pos->track_height &&
282 (!track_mixing || !pos->scale_in_compositor)) {
283 gst_caps_set_simple (caps, "width", G_TYPE_INT,
284 pos->track_width, "height", G_TYPE_INT, pos->track_height, NULL);
287 if (pos->fps_n != -1)
288 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, pos->fps_n,
291 if (pos->par_n != -1)
292 gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
293 pos->par_n, pos->par_d, NULL);
295 if (!pos->track_width || !pos->track_height) {
296 GST_INFO_OBJECT (pos, "Track doesn't have a proper size, not "
297 "positioning the source");
299 } else if (auto_position (pos))
302 if (!old_track_height || !old_track_width) {
303 GST_DEBUG_OBJECT (pos, "No old track size, can not properly reposition");
307 if ((!pos->natural_width || !pos->natural_height) &&
308 (!pos->width || !pos->height)) {
309 GST_DEBUG_OBJECT (pos, "No natural aspect ratio and no user set "
310 " image size, can't not reposition.");
314 if (gst_util_fraction_compare (old_track_width, old_track_height,
315 pos->track_width, pos->track_height)) {
316 GST_INFO_OBJECT (pos, "Not repositioning as track size change didn't"
317 " keep the same aspect ratio (previous %dx%d("
318 "ratio=%f), new: %dx%d(ratio=%f)",
319 old_track_width, old_track_height,
320 (gdouble) old_track_width / (gdouble) old_track_height,
321 pos->track_width, pos->track_height,
322 (gdouble) pos->track_width / (gdouble) pos->track_height);
326 reposition_properties (pos, old_track_width, old_track_height);
329 GST_DEBUG_OBJECT (pos, "setting caps %" GST_PTR_FORMAT, caps);
331 g_object_set (pos->capsfilter, "caps", caps, NULL);
333 gst_caps_unref (caps);
337 sync_properties_from_track (GstFramePositioner * pos, GESTrack * track)
340 gint old_track_width, old_track_height;
343 g_object_get (track, "restriction-caps", &caps, NULL);
347 if (caps && gst_caps_get_size (caps) > 0) {
348 GstStructure *structure;
350 structure = gst_caps_get_structure (caps, 0);
351 if (!gst_structure_get_int (structure, "width", &width))
353 if (!gst_structure_get_int (structure, "height", &height))
355 if (!gst_structure_get_fraction (structure, "framerate", &(pos->fps_n),
359 if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio",
360 &(pos->par_n), &(pos->par_d)))
364 old_track_width = pos->track_width;
365 old_track_height = pos->track_height;
367 pos->track_width = width;
368 pos->track_height = height;
370 GST_DEBUG_OBJECT (pos, "syncing framerate from caps : %d/%d", pos->fps_n,
373 gst_caps_unref (caps);
375 gst_frame_positioner_update_properties (pos, ges_track_get_mixing (track),
376 old_track_width, old_track_height);
380 _track_restriction_changed_cb (GESTrack * track, GParamSpec * arg G_GNUC_UNUSED,
381 GstFramePositioner * pos)
383 sync_properties_from_track (pos, track);
387 set_track (GstFramePositioner * pos)
391 if (pos->current_track) {
392 g_signal_handlers_disconnect_by_func (pos->current_track,
393 (GCallback) _track_restriction_changed_cb, pos);
394 g_object_weak_unref (G_OBJECT (pos->current_track),
395 (GWeakNotify) _weak_notify_cb, pos);
398 new_track = ges_track_element_get_track (pos->track_source);
400 pos->current_track = new_track;
401 g_object_weak_ref (G_OBJECT (new_track), (GWeakNotify) _weak_notify_cb,
403 GST_DEBUG_OBJECT (pos, "connecting to track : %p", pos->current_track);
405 g_signal_connect (pos->current_track, "notify::restriction-caps",
406 (GCallback) _track_restriction_changed_cb, pos);
407 sync_properties_from_track (pos, pos->current_track);
409 pos->current_track = NULL;
414 _track_changed_cb (GESTrackElement * trksrc, GParamSpec * arg G_GNUC_UNUSED,
415 GstFramePositioner * pos)
421 _trk_element_weak_notify_cb (GstFramePositioner * pos, GObject * old)
423 pos->track_source = NULL;
424 gst_object_unref (pos);
428 ges_frame_positioner_set_source_and_filter (GstFramePositioner * pos,
429 GESTrackElement * trksrc, GstElement * capsfilter)
431 pos->track_source = trksrc;
432 pos->capsfilter = capsfilter;
433 pos->current_track = ges_track_element_get_track (trksrc);
435 g_object_weak_ref (G_OBJECT (trksrc),
436 (GWeakNotify) _trk_element_weak_notify_cb, gst_object_ref (pos));
437 g_signal_connect (trksrc, "notify::track", (GCallback) _track_changed_cb,
443 gst_frame_positioner_dispose (GObject * object)
445 GstFramePositioner *pos = GST_FRAME_POSITIONNER (object);
447 if (pos->track_source) {
448 g_signal_handlers_disconnect_by_func (pos->track_source, _track_changed_cb,
450 pos->track_source = NULL;
453 if (pos->current_track) {
454 g_signal_handlers_disconnect_by_func (pos->current_track,
455 _track_restriction_changed_cb, pos);
456 g_object_weak_unref (G_OBJECT (pos->current_track),
457 (GWeakNotify) _weak_notify_cb, pos);
458 pos->current_track = NULL;
461 G_OBJECT_CLASS (gst_frame_positioner_parent_class)->dispose (object);
465 gst_frame_positioner_class_init (GstFramePositionerClass * klass)
467 int default_operator_value = 0;
468 GType operator_gtype =
469 gst_compositor_operator_get_type_and_default_value
470 (&default_operator_value);
471 guint n_pspecs = PROP_LAST;
473 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
474 GstBaseTransformClass *base_transform_class =
475 GST_BASE_TRANSFORM_CLASS (klass);
477 GST_DEBUG_CATEGORY_INIT (_framepositioner, "framepositioner",
478 GST_DEBUG_FG_YELLOW, "ges frame positioner");
480 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
481 &gst_frame_positioner_src_template);
482 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
483 &gst_frame_positioner_sink_template);
485 gobject_class->set_property = gst_frame_positioner_set_property;
486 gobject_class->get_property = gst_frame_positioner_get_property;
487 gobject_class->dispose = gst_frame_positioner_dispose;
488 base_transform_class->transform_ip =
489 GST_DEBUG_FUNCPTR (gst_frame_positioner_transform_ip);
492 * gstframepositioner:alpha:
494 * The desired alpha for the stream.
496 properties[PROP_ALPHA] =
497 g_param_spec_double ("alpha", "alpha", "alpha of the stream", 0.0, 1.0,
498 1.0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE);
501 * gstframepositioner:posx:
503 * The desired x position for the stream.
505 properties[PROP_POSX] =
506 g_param_spec_int ("posx", "posx", "x position of the stream", MIN_PIXELS,
507 MAX_PIXELS, 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE);
511 * gstframepositioner:posy:
513 * The desired y position for the stream.
515 properties[PROP_POSY] =
516 g_param_spec_int ("posy", "posy", "y position of the stream", MIN_PIXELS,
517 MAX_PIXELS, 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE);
520 * gstframepositioner:zorder:
522 * The desired z order for the stream.
524 properties[PROP_ZORDER] =
525 g_param_spec_uint ("zorder", "zorder", "z order of the stream", 0,
526 G_MAXUINT, 0, G_PARAM_READWRITE);
529 * gesframepositioner:width:
531 * The desired width for that source.
532 * Set to 0 if size is not mandatory, will be set to width of the current track.
534 properties[PROP_WIDTH] =
535 g_param_spec_int ("width", "width", "width of the source", 0, MAX_PIXELS,
536 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE);
539 * gesframepositioner:height:
541 * The desired height for that source.
542 * Set to 0 if size is not mandatory, will be set to height of the current track.
544 properties[PROP_HEIGHT] =
545 g_param_spec_int ("height", "height", "height of the source", 0,
546 MAX_PIXELS, 0, G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE);
549 * gesframepositioner:operator:
551 * The blending operator for the source.
553 if (operator_gtype != G_TYPE_NONE) {
554 properties[PROP_OPERATOR] =
555 g_param_spec_enum ("operator", "Operator",
556 "Blending operator to use for blending this pad over the previous ones",
557 operator_gtype, default_operator_value,
558 (G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE |
559 GST_PARAM_CONDITIONALLY_AVAILABLE | G_PARAM_STATIC_STRINGS));
564 g_object_class_install_properties (gobject_class, n_pspecs, properties);
566 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
567 "frame positioner", "Metadata",
568 "This element provides with tagging facilities",
569 "mduponchelle1@gmail.com");
573 gst_frame_positioner_init (GstFramePositioner * framepositioner)
575 int default_operator_value;
576 gst_compositor_operator_get_type_and_default_value (&default_operator_value);
578 framepositioner->alpha = 1.0;
579 framepositioner->posx = 0.0;
580 framepositioner->posy = 0.0;
581 framepositioner->zorder = 0;
582 framepositioner->width = 0;
583 framepositioner->height = 0;
584 framepositioner->operator = default_operator_value;
585 framepositioner->fps_n = -1;
586 framepositioner->fps_d = -1;
587 framepositioner->track_width = 0;
588 framepositioner->track_height = 0;
589 framepositioner->capsfilter = NULL;
590 framepositioner->track_source = NULL;
591 framepositioner->current_track = NULL;
592 framepositioner->scale_in_compositor = TRUE;
594 framepositioner->par_n = -1;
595 framepositioner->par_d = 1;
599 gst_frame_positioner_set_property (GObject * object, guint property_id,
600 const GValue * value, GParamSpec * pspec)
602 GstFramePositioner *framepositioner = GST_FRAME_POSITIONNER (object);
603 gboolean track_mixing = TRUE;
605 if (framepositioner->current_track)
606 track_mixing = ges_track_get_mixing (framepositioner->current_track);
609 GST_OBJECT_LOCK (framepositioner);
610 switch (property_id) {
612 framepositioner->alpha = g_value_get_double (value);
615 framepositioner->posx = g_value_get_int (value);
616 framepositioner->user_positioned = TRUE;
619 framepositioner->posy = g_value_get_int (value);
620 framepositioner->user_positioned = TRUE;
623 framepositioner->zorder = g_value_get_uint (value);
626 framepositioner->user_positioned = TRUE;
627 framepositioner->width = g_value_get_int (value);
628 gst_frame_positioner_update_properties (framepositioner, track_mixing,
632 framepositioner->user_positioned = TRUE;
633 framepositioner->height = g_value_get_int (value);
634 gst_frame_positioner_update_properties (framepositioner, track_mixing,
638 framepositioner->operator = g_value_get_enum (value);
639 gst_frame_positioner_update_properties (framepositioner, track_mixing,
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
646 GST_OBJECT_UNLOCK (framepositioner);
650 gst_frame_positioner_get_property (GObject * object, guint property_id,
651 GValue * value, GParamSpec * pspec)
653 GstFramePositioner *pos = GST_FRAME_POSITIONNER (object);
654 gint real_width, real_height;
656 switch (property_id) {
658 g_value_set_double (value, pos->alpha);
661 g_value_set_int (value, round (pos->posx));
664 g_value_set_int (value, round (pos->posy));
667 g_value_set_uint (value, pos->zorder);
670 if (pos->scale_in_compositor) {
671 g_value_set_int (value, round (pos->width));
674 pos->width > 0 ? round (pos->width) : round (pos->track_width);
675 g_value_set_int (value, real_width);
679 if (pos->scale_in_compositor) {
680 g_value_set_int (value, round (pos->height));
683 pos->height > 0 ? round (pos->height) : round (pos->track_height);
684 g_value_set_int (value, real_height);
688 g_value_set_enum (value, pos->operator);
691 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
697 gst_frame_positioner_meta_api_get_type (void)
700 static const gchar *tags[] = { "video", NULL };
702 if (g_once_init_enter (&type)) {
703 GType _type = gst_meta_api_type_register ("GstFramePositionerApi", tags);
704 g_once_init_leave (&type, _type);
709 static const GstMetaInfo *
710 gst_frame_positioner_get_info (void)
712 static const GstMetaInfo *meta_info = NULL;
714 if (g_once_init_enter ((GstMetaInfo **) & meta_info)) {
715 const GstMetaInfo *meta =
716 gst_meta_register (gst_frame_positioner_meta_api_get_type (),
717 "GstFramePositionerMeta",
718 sizeof (GstFramePositionerMeta), gst_frame_positioner_meta_init,
720 gst_frame_positioner_meta_transform);
721 g_once_init_leave ((GstMetaInfo **) & meta_info, (GstMetaInfo *) meta);
727 gst_frame_positioner_meta_init (GstMeta * meta, gpointer params,
730 int default_operator_value = 0;
731 GstFramePositionerMeta *smeta;
733 smeta = (GstFramePositionerMeta *) meta;
735 gst_compositor_operator_get_type_and_default_value (&default_operator_value);
738 smeta->posx = smeta->posy = smeta->height = smeta->width = 0;
740 smeta->operator = default_operator_value;
746 gst_frame_positioner_meta_transform (GstBuffer * dest, GstMeta * meta,
747 GstBuffer * buffer, GQuark type, gpointer data)
749 GstFramePositionerMeta *dmeta, *smeta;
751 smeta = (GstFramePositionerMeta *) meta;
753 if (GST_META_TRANSFORM_IS_COPY (type)) {
754 /* only copy if the complete data is copied as well */
756 (GstFramePositionerMeta *) gst_buffer_add_meta (dest,
757 gst_frame_positioner_get_info (), NULL);
758 dmeta->alpha = smeta->alpha;
759 dmeta->posx = smeta->posx;
760 dmeta->posy = smeta->posy;
761 dmeta->width = smeta->width;
762 dmeta->height = smeta->height;
763 dmeta->zorder = smeta->zorder;
764 dmeta->operator = smeta->operator;
771 gst_frame_positioner_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
773 GstFramePositionerMeta *meta;
774 GstFramePositioner *framepositioner = GST_FRAME_POSITIONNER (trans);
775 GstClockTime timestamp = GST_BUFFER_PTS (buf);
777 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
778 gst_object_sync_values (GST_OBJECT (trans), timestamp);
782 (GstFramePositionerMeta *) gst_buffer_add_meta (buf,
783 gst_frame_positioner_get_info (), NULL);
785 GST_OBJECT_LOCK (framepositioner);
786 meta->alpha = framepositioner->alpha;
787 meta->posx = round (framepositioner->posx);
788 meta->posy = round (framepositioner->posy);
789 meta->width = round (framepositioner->width);
790 meta->height = round (framepositioner->height);
791 meta->zorder = framepositioner->zorder;
792 meta->operator = framepositioner->operator;
793 GST_OBJECT_UNLOCK (framepositioner);