ges: Add a method to retrieve the 'natural' size of VideoSource
[platform/upstream/gst-editing-services.git] / ges / ges-video-source.c
index af1a34c..3ba996c 100644 (file)
  * @title: GESVideoSource
  * @short_description: Base Class for video sources
  *
- * # Children Properties:
+ * ## Children Properties:
+ *
  * You can use the following children properties through the
  * #ges_track_element_set_child_property and alike set of methods:
- *
- * <informaltable frame="none">
- * <tgroup cols="3">
- * <colspec colname="properties_type" colwidth="150px"/>
- * <colspec colname="properties_name" colwidth="200px"/>
- * <colspec colname="properties_flags" colwidth="400px"/>
- * <tbody>
- * <row>
- *  <entry role="property_type"><link linkend="gdouble"><type>double</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--alpha">alpha</link></entry>
- *  <entry>The desired alpha for the stream.</entry>
- * </row>
- * <row>
- *  <entry role="property_type"><link linkend="gint"><type>gint</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--posx">posx</link></entry>
- *  <entry>The desired x position for the stream.</entry>
- * </row>
- * <row>
- *  <entry role="property_type"><link linkend="gint"><type>gint</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--posy">posy</link></entry>
- *  <entry>The desired y position for the stream</entry>
- * </row>
- * <row>
- *  <entry role="property_type"><link linkend="guint"><type>guint</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--zorder">zorder</link></entry>
- *  <entry>The desired z order for the stream</entry>
- * </row>
- * <row>
- *  <entry role="property_type"><link linkend="gint"><type>gint</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--width">width</link></entry>
- *  <entry>The desired width for that source. Set to 0 if size is not mandatory, will be set to width of the current track.</entry>
- * </row>
- * <row>
- *  <entry role="property_type"><link linkend="gint"><type>gint</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--height">height</link></entry>
- *  <entry>The desired height for that source. Set to 0 if size is not mandatory, will be set to height of the current track.</entry>
- * </row>
- * <row>
- *  <entry role="property_type"><link linkend="GstDeinterlaceModes"><type>GstDeinterlaceModes</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--deinterlace-mode">deinterlace-mode</link></entry>
- *  <entry>Deinterlace Mode</entry>
- * </row>
- * <row>
- *  <entry role="property_type"><link linkend="GstDeinterlaceFields"><type>GstDeinterlaceFields</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--deinterlace-fields">deinterlace-fields</link></entry>
- *  <entry>Fields to use for deinterlacing</entry>
- * </row>
- * <row>
- *  <entry role="property_type"><link linkend="GstDeinterlaceFieldLayout"><type>GstDeinterlaceFieldLayout</type></link></entry>
- *  <entry role="property_name"><link linkend="GESVideoSource--deinterlace-tff">deinterlace-tff</link></entry>
- *  <entry>Deinterlace top field first</entry>
- * </row>
- * </tbody>
- * </tgroup>
- * </informaltable>
+ * 
+ * - #gdouble `alpha`: The desired alpha for the stream.
+ * - #gint `posx`: The desired x position for the stream.
+ * - #gint `posy`: The desired y position for the stream
+ * - #gint `width`: The desired width for that source.
+ *   Set to 0 if size is not mandatory, will be set to width of the current track.
+ * - #gint `height`: The desired height for that source.
+ *   Set to 0 if size is not mandatory, will be set to height of the current track.
+ * - #GstDeinterlaceModes `deinterlace-mode`: Deinterlace Mode
+ * - #GstDeinterlaceFields `deinterlace-fields`: Fields to use for deinterlacing
+ * - #GstDeinterlaceFieldLayout `deinterlace-tff`: Deinterlace top field first
+ * - #GstVideoOrientationMethod `video-direction`: The desired video rotation and flipping.
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include <gst/pbutils/missing-plugins.h>
+#include <gst/video/video.h>
 
 #include "ges-internal.h"
 #include "ges/ges-meta-container.h"
@@ -93,7 +55,6 @@
 #include "gstframepositioner.h"
 
 #define parent_class ges_video_source_parent_class
-G_DEFINE_ABSTRACT_TYPE (GESVideoSource, ges_video_source, GES_TYPE_SOURCE);
 
 struct _GESVideoSourcePrivate
 {
@@ -101,6 +62,9 @@ struct _GESVideoSourcePrivate
   GstElement *capsfilter;
 };
 
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GESVideoSource, ges_video_source,
+    GES_TYPE_SOURCE);
+
 /* TrackElement VMethods */
 
 static gboolean
@@ -132,14 +96,15 @@ ges_video_source_create_element (GESTrackElement * trksrc)
 {
   GstElement *topbin;
   GstElement *sub_element;
-  GstElement *queue = gst_element_factory_make ("queue", NULL);
   GESVideoSourceClass *source_class = GES_VIDEO_SOURCE_GET_CLASS (trksrc);
   GESVideoSource *self;
-  GstElement *positioner, *videoscale, *videorate, *capsfilter, *videoconvert,
-      *deinterlace;
+  GstElement *positioner, *videoflip, *capsfilter, *deinterlace;
   const gchar *positioner_props[] =
       { "alpha", "posx", "posy", "width", "height", NULL };
   const gchar *deinterlace_props[] = { "mode", "fields", "tff", NULL };
+  const gchar *videoflip_props[] = { "video-direction", NULL };
+  gboolean needs_converters = TRUE;
+  GPtrArray *elements;
 
   if (!source_class->create_source)
     return NULL;
@@ -147,45 +112,59 @@ ges_video_source_create_element (GESTrackElement * trksrc)
   sub_element = source_class->create_source (trksrc);
 
   self = (GESVideoSource *) trksrc;
+  if (source_class->ABI.abi.needs_converters)
+    needs_converters = source_class->ABI.abi.needs_converters (self);
+
+  elements = g_ptr_array_new ();
+  g_ptr_array_add (elements, gst_element_factory_make ("queue", NULL));
 
   /* That positioner will add metadata to buffers according to its
      properties, acting like a proxy for our smart-mixer dynamic pads. */
   positioner = gst_element_factory_make ("framepositioner", "frame_tagger");
   g_object_set (positioner, "zorder",
       G_MAXUINT - GES_TIMELINE_ELEMENT_PRIORITY (self), NULL);
-
-  videoscale =
-      gst_element_factory_make ("videoscale", "track-element-videoscale");
-  videoconvert =
-      gst_element_factory_make ("videoconvert", "track-element-videoconvert");
-  videorate = gst_element_factory_make ("videorate", "track-element-videorate");
-  deinterlace = gst_element_factory_make ("deinterlace", "deinterlace");
+  g_ptr_array_add (elements, positioner);
+
+  /* If there's image-orientation tag, make sure the image is correctly oriented
+   * before we scale it. */
+  videoflip = gst_element_factory_make ("videoflip", "track-element-videoflip");
+  g_object_set (videoflip, "video-direction", GST_VIDEO_ORIENTATION_AUTO, NULL);
+  g_ptr_array_add (elements, videoflip);
+
+  if (needs_converters) {
+    g_ptr_array_add (elements, gst_element_factory_make ("videoscale",
+            "track-element-videoscale"));
+    g_ptr_array_add (elements, gst_element_factory_make ("videoconvert",
+            "track-element-videoconvert"));
+  }
+  g_ptr_array_add (elements, gst_element_factory_make ("videorate",
+          "track-element-videorate"));
   capsfilter =
       gst_element_factory_make ("capsfilter", "track-element-capsfilter");
+  g_ptr_array_add (elements, capsfilter);
 
   ges_frame_positioner_set_source_and_filter (GST_FRAME_POSITIONNER
       (positioner), trksrc, capsfilter);
 
   ges_track_element_add_children_props (trksrc, positioner, NULL, NULL,
       positioner_props);
+  ges_track_element_add_children_props (trksrc, videoflip, NULL, NULL,
+      videoflip_props);
 
+  deinterlace = gst_element_factory_make ("deinterlace", "deinterlace");
   if (deinterlace == NULL) {
     post_missing_element_message (sub_element, "deinterlace");
 
     GST_ELEMENT_WARNING (sub_element, CORE, MISSING_PLUGIN,
         ("Missing element '%s' - check your GStreamer installation.",
             "deinterlace"), ("deinterlacing won't work"));
-    topbin =
-        ges_source_create_topbin ("videosrcbin", sub_element, queue,
-        videoconvert, positioner, videoscale, videorate, capsfilter, NULL);
   } else {
+    g_ptr_array_add (elements, deinterlace);
     ges_track_element_add_children_props (trksrc, deinterlace, NULL, NULL,
         deinterlace_props);
-    topbin =
-        ges_source_create_topbin ("videosrcbin", sub_element, queue,
-        videoconvert, deinterlace, positioner, videoscale, videorate,
-        capsfilter, NULL);
   }
+  topbin = ges_source_create_topbin ("videosrcbin", sub_element, elements);
+  g_ptr_array_free (elements, TRUE);
 
   self->priv->positioner = GST_FRAME_POSITIONNER (positioner);
   self->priv->positioner->scale_in_compositor =
@@ -233,8 +212,6 @@ ges_video_source_class_init (GESVideoSourceClass * klass)
   GESTimelineElementClass *element_class = GES_TIMELINE_ELEMENT_CLASS (klass);
   GESVideoSourceClass *video_source_class = GES_VIDEO_SOURCE_CLASS (klass);
 
-  g_type_class_add_private (klass, sizeof (GESVideoSourcePrivate));
-
   element_class->set_priority = _set_priority;
   element_class->lookup_child = _lookup_child;
 
@@ -246,8 +223,36 @@ ges_video_source_class_init (GESVideoSourceClass * klass)
 static void
 ges_video_source_init (GESVideoSource * self)
 {
-  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
-      GES_TYPE_VIDEO_SOURCE, GESVideoSourcePrivate);
+  self->priv = ges_video_source_get_instance_private (self);
   self->priv->positioner = NULL;
   self->priv->capsfilter = NULL;
+
+}
+
+/**
+ * ges_video_source_get_natural_size:
+ * @self: A #GESVideoSource
+ * @width: (out): The natural width of the underlying source
+ * @height: (out): The natural height of the underlying source
+ *
+ * Retrieves the natural size of the video stream. The natural size, is
+ * the size at which it will be displayed if no scaling is being applied.
+ *
+ * NOTE: The sources take into account the potential video rotation applied
+ * by the #videoflip element that is inside the source, effects applied on
+ * the clip which potentially also rotate the element are not taken into
+ * account.
+ *
+ * Returns: %TRUE if the object has a natural size, %FALSE otherwise.
+ */
+gboolean
+ges_video_source_get_natural_size (GESVideoSource * self, gint * width,
+    gint * height)
+{
+  GESVideoSourceClass *klass = GES_VIDEO_SOURCE_GET_CLASS (self);
+
+  if (!klass->ABI.abi.get_natural_size)
+    return FALSE;
+
+  return klass->ABI.abi.get_natural_size (self, width, height);
 }