2014-05-27 10:40:09 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2014 Lubosz Sarnecki <lubosz@gmail.com>
|
2016-01-14 06:45:03 +00:00
|
|
|
* Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
|
2014-05-27 10:40:09 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-gltransformation
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: gltransformation
|
2014-05-27 10:40:09 +00:00
|
|
|
*
|
|
|
|
* Transforms video on the GPU.
|
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Examples
|
2014-05-27 10:40:09 +00:00
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 gltestsrc ! gltransformation rotation-z=45 ! glimagesink
|
2014-05-27 10:40:09 +00:00
|
|
|
* ]| A pipeline to rotate by 45 degrees
|
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 gltestsrc ! gltransformation translation-x=0.5 ! glimagesink
|
2014-05-27 10:40:09 +00:00
|
|
|
* ]| Translate the video by 0.5
|
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 gltestsrc ! gltransformation scale-y=0.5 scale-x=0.5 ! glimagesink
|
2014-05-27 10:40:09 +00:00
|
|
|
* ]| Resize the video by 0.5
|
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 gltestsrc ! gltransformation rotation-x=-45 ortho=True ! glimagesink
|
2014-05-27 10:40:09 +00:00
|
|
|
* ]| Rotate the video around the X-Axis by -45° with an orthographic projection
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2014-05-27 10:40:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2020-12-11 14:42:32 +00:00
|
|
|
#include "gstglelements.h"
|
2014-05-27 10:40:09 +00:00
|
|
|
#include "gstgltransformation.h"
|
2015-02-18 22:10:48 +00:00
|
|
|
|
|
|
|
#include <gst/gl/gstglapi.h>
|
|
|
|
#include <graphene-gobject.h>
|
2016-11-10 08:14:24 +00:00
|
|
|
#include "gstglutils.h"
|
2014-05-27 10:40:09 +00:00
|
|
|
|
|
|
|
#define GST_CAT_DEFAULT gst_gl_transformation_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
2015-04-27 08:01:53 +00:00
|
|
|
#define gst_gl_transformation_parent_class parent_class
|
|
|
|
|
2016-08-19 05:35:49 +00:00
|
|
|
#define VEC4_FORMAT "%f,%f,%f,%f"
|
|
|
|
#define VEC4_ARGS(v) graphene_vec4_get_x (v), graphene_vec4_get_y (v), graphene_vec4_get_z (v), graphene_vec4_get_w (v)
|
|
|
|
#define VEC3_FORMAT "%f,%f,%f"
|
|
|
|
#define VEC3_ARGS(v) graphene_vec3_get_x (v), graphene_vec3_get_y (v), graphene_vec3_get_z (v)
|
|
|
|
#define VEC2_FORMAT "%f,%f"
|
|
|
|
#define VEC2_ARGS(v) graphene_vec2_get_x (v), graphene_vec2_get_y (v)
|
|
|
|
#define POINT3D_FORMAT "%f,%f,%f"
|
|
|
|
#define POINT3D_ARGS(p) (p)->x, (p)->y, (p)->z
|
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2014-07-07 08:52:57 +00:00
|
|
|
PROP_FOV,
|
2014-05-27 10:40:09 +00:00
|
|
|
PROP_ORTHO,
|
|
|
|
PROP_TRANSLATION_X,
|
|
|
|
PROP_TRANSLATION_Y,
|
|
|
|
PROP_TRANSLATION_Z,
|
|
|
|
PROP_ROTATION_X,
|
|
|
|
PROP_ROTATION_Y,
|
|
|
|
PROP_ROTATION_Z,
|
|
|
|
PROP_SCALE_X,
|
2014-07-07 08:52:57 +00:00
|
|
|
PROP_SCALE_Y,
|
2015-02-28 00:07:44 +00:00
|
|
|
PROP_MVP,
|
|
|
|
PROP_PIVOT_X,
|
|
|
|
PROP_PIVOT_Y,
|
|
|
|
PROP_PIVOT_Z,
|
2014-05-27 10:40:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DEBUG_INIT \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_transformation_debug, "gltransformation", 0, "gltransformation element");
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstGLTransformation, gst_gl_transformation,
|
|
|
|
GST_TYPE_GL_FILTER, DEBUG_INIT);
|
2020-12-11 14:42:32 +00:00
|
|
|
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (gltransformation, "gltransformation",
|
|
|
|
GST_RANK_NONE, GST_TYPE_GL_TRANSFORMATION, gl_element_init (plugin));
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
static void gst_gl_transformation_finalize (GObject * object);
|
2014-05-27 10:40:09 +00:00
|
|
|
static void gst_gl_transformation_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_gl_transformation_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
|
|
|
static gboolean gst_gl_transformation_set_caps (GstGLFilter * filter,
|
|
|
|
GstCaps * incaps, GstCaps * outcaps);
|
2016-01-14 06:45:03 +00:00
|
|
|
static gboolean gst_gl_transformation_src_event (GstBaseTransform * trans,
|
|
|
|
GstEvent * event);
|
2016-05-14 13:32:06 +00:00
|
|
|
static gboolean gst_gl_transformation_filter_meta (GstBaseTransform * trans,
|
|
|
|
GstQuery * query, GType api, const GstStructure * params);
|
|
|
|
static gboolean gst_gl_transformation_decide_allocation (GstBaseTransform *
|
|
|
|
trans, GstQuery * query);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2016-11-07 15:21:20 +00:00
|
|
|
static void gst_gl_transformation_gl_stop (GstGLBaseFilter * filter);
|
|
|
|
static gboolean gst_gl_transformation_gl_start (GstGLBaseFilter * base_filter);
|
2016-07-12 02:59:57 +00:00
|
|
|
static gboolean gst_gl_transformation_callback (gpointer stuff);
|
2014-05-27 10:40:09 +00:00
|
|
|
static void gst_gl_transformation_build_mvp (GstGLTransformation *
|
|
|
|
transformation);
|
|
|
|
|
2016-05-14 13:32:06 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_gl_transformation_prepare_output_buffer (GstBaseTransform * trans,
|
|
|
|
GstBuffer * inbuf, GstBuffer ** outbuf);
|
|
|
|
static gboolean gst_gl_transformation_filter (GstGLFilter * filter,
|
|
|
|
GstBuffer * inbuf, GstBuffer * outbuf);
|
2014-05-27 10:40:09 +00:00
|
|
|
static gboolean gst_gl_transformation_filter_texture (GstGLFilter * filter,
|
2016-07-11 14:30:22 +00:00
|
|
|
GstGLMemory * in_tex, GstGLMemory * out_tex);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_transformation_class_init (GstGLTransformationClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *element_class;
|
2016-01-14 06:45:03 +00:00
|
|
|
GstBaseTransformClass *base_transform_class;
|
2014-05-27 10:40:09 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
2016-01-14 06:45:03 +00:00
|
|
|
base_transform_class = GST_BASE_TRANSFORM_CLASS (klass);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2017-07-06 11:09:50 +00:00
|
|
|
gst_gl_filter_add_rgba_pad_templates (GST_GL_FILTER_CLASS (klass));
|
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
gobject_class->set_property = gst_gl_transformation_set_property;
|
|
|
|
gobject_class->get_property = gst_gl_transformation_get_property;
|
|
|
|
|
2016-01-14 06:45:03 +00:00
|
|
|
base_transform_class->src_event = gst_gl_transformation_src_event;
|
2016-05-14 13:32:06 +00:00
|
|
|
base_transform_class->decide_allocation =
|
|
|
|
gst_gl_transformation_decide_allocation;
|
|
|
|
base_transform_class->filter_meta = gst_gl_transformation_filter_meta;
|
2016-01-14 06:45:03 +00:00
|
|
|
|
2016-11-07 15:21:20 +00:00
|
|
|
GST_GL_BASE_FILTER_CLASS (klass)->gl_start = gst_gl_transformation_gl_start;
|
|
|
|
GST_GL_BASE_FILTER_CLASS (klass)->gl_stop = gst_gl_transformation_gl_stop;
|
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
GST_GL_FILTER_CLASS (klass)->set_caps = gst_gl_transformation_set_caps;
|
2016-05-14 13:32:06 +00:00
|
|
|
GST_GL_FILTER_CLASS (klass)->filter = gst_gl_transformation_filter;
|
2014-05-27 10:40:09 +00:00
|
|
|
GST_GL_FILTER_CLASS (klass)->filter_texture =
|
|
|
|
gst_gl_transformation_filter_texture;
|
2016-05-14 13:32:06 +00:00
|
|
|
GST_BASE_TRANSFORM_CLASS (klass)->prepare_output_buffer =
|
|
|
|
gst_gl_transformation_prepare_output_buffer;
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2014-07-07 08:52:57 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_FOV,
|
|
|
|
g_param_spec_float ("fov", "Fov", "Field of view angle in degrees",
|
2014-05-27 10:40:09 +00:00
|
|
|
0.0, G_MAXFLOAT, 90.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ORTHO,
|
|
|
|
g_param_spec_boolean ("ortho", "Orthographic",
|
|
|
|
"Use orthographic projection", FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
/* Rotation */
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ROTATION_X,
|
|
|
|
g_param_spec_float ("rotation-x", "X Rotation",
|
|
|
|
"Rotates the video around the X-Axis in degrees.",
|
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ROTATION_Y,
|
|
|
|
g_param_spec_float ("rotation-y", "Y Rotation",
|
|
|
|
"Rotates the video around the Y-Axis in degrees.",
|
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ROTATION_Z,
|
|
|
|
g_param_spec_float ("rotation-z", "Z Rotation",
|
|
|
|
"Rotates the video around the Z-Axis in degrees.",
|
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
/* Translation */
|
|
|
|
g_object_class_install_property (gobject_class, PROP_TRANSLATION_X,
|
|
|
|
g_param_spec_float ("translation-x", "X Translation",
|
2015-02-19 02:21:01 +00:00
|
|
|
"Translates the video at the X-Axis, in universal [0-1] coordinate.",
|
2014-05-27 10:40:09 +00:00
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_TRANSLATION_Y,
|
|
|
|
g_param_spec_float ("translation-y", "Y Translation",
|
2015-02-19 02:21:01 +00:00
|
|
|
"Translates the video at the Y-Axis, in universal [0-1] coordinate.",
|
2014-05-27 10:40:09 +00:00
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_TRANSLATION_Z,
|
|
|
|
g_param_spec_float ("translation-z", "Z Translation",
|
2015-02-19 02:21:01 +00:00
|
|
|
"Translates the video at the Z-Axis, in universal [0-1] coordinate.",
|
2014-05-27 10:40:09 +00:00
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
/* Scale */
|
|
|
|
g_object_class_install_property (gobject_class, PROP_SCALE_X,
|
|
|
|
g_param_spec_float ("scale-x", "X Scale",
|
2015-02-19 01:41:14 +00:00
|
|
|
"Scale multiplier for the X-Axis.",
|
2016-01-14 08:03:04 +00:00
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 1.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2014-05-27 10:40:09 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_SCALE_Y,
|
|
|
|
g_param_spec_float ("scale-y", "Y Scale",
|
2015-02-19 01:41:14 +00:00
|
|
|
"Scale multiplier for the Y-Axis.",
|
2016-01-14 08:03:04 +00:00
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 1.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2015-02-28 00:07:44 +00:00
|
|
|
/* Pivot */
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PIVOT_X,
|
|
|
|
g_param_spec_float ("pivot-x", "X Pivot",
|
|
|
|
"Rotation pivot point X coordinate, where 0 is the center,"
|
2016-05-24 13:30:09 +00:00
|
|
|
" -1 the left border, +1 the right border and <-1, >1 outside.",
|
2015-02-28 00:07:44 +00:00
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PIVOT_Y,
|
|
|
|
g_param_spec_float ("pivot-y", "Y Pivot",
|
|
|
|
"Rotation pivot point X coordinate, where 0 is the center,"
|
2016-05-24 13:30:09 +00:00
|
|
|
" -1 the left border, +1 the right border and <-1, >1 outside.",
|
2015-02-28 00:07:44 +00:00
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PIVOT_Z,
|
|
|
|
g_param_spec_float ("pivot-z", "Z Pivot",
|
|
|
|
"Relevant for rotation in 3D space. You look into the negative Z axis direction",
|
|
|
|
-G_MAXFLOAT, G_MAXFLOAT, 0.0,
|
2016-05-24 13:30:09 +00:00
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2015-02-28 00:07:44 +00:00
|
|
|
|
2014-07-07 08:52:57 +00:00
|
|
|
/* MVP */
|
|
|
|
g_object_class_install_property (gobject_class, PROP_MVP,
|
|
|
|
g_param_spec_boxed ("mvp-matrix",
|
|
|
|
"Modelview Projection Matrix",
|
|
|
|
"The final Graphene 4x4 Matrix for transformation",
|
2016-01-14 06:45:03 +00:00
|
|
|
GRAPHENE_TYPE_MATRIX, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2014-07-07 08:52:57 +00:00
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
gst_element_class_set_metadata (element_class, "OpenGL transformation filter",
|
|
|
|
"Filter/Effect/Video", "Transform video on the GPU",
|
2023-05-19 13:50:11 +00:00
|
|
|
"Lubosz Sarnecki <lubosz@gmail.com>, "
|
2016-01-14 06:45:03 +00:00
|
|
|
"Matthew Waters <matthew@centricular.com>");
|
2014-11-27 10:05:45 +00:00
|
|
|
|
2015-03-13 10:25:35 +00:00
|
|
|
GST_GL_BASE_FILTER_CLASS (klass)->supported_gl_api =
|
2014-11-27 10:05:45 +00:00
|
|
|
GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2;
|
2022-04-06 16:08:46 +00:00
|
|
|
|
|
|
|
gobject_class->finalize = gst_gl_transformation_finalize;
|
2014-05-27 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_transformation_init (GstGLTransformation * filter)
|
|
|
|
{
|
|
|
|
filter->shader = NULL;
|
2014-07-07 08:52:57 +00:00
|
|
|
filter->fov = 90;
|
|
|
|
filter->aspect = 1.0;
|
2016-10-27 03:41:26 +00:00
|
|
|
filter->znear = 0.1f;
|
2017-08-17 03:42:21 +00:00
|
|
|
filter->zfar = 100.0;
|
2014-05-27 10:40:09 +00:00
|
|
|
|
|
|
|
filter->xscale = 1.0;
|
|
|
|
filter->yscale = 1.0;
|
|
|
|
|
|
|
|
filter->in_tex = 0;
|
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
/* let graphene alloc its structures with correct memory alignment, otherwise
|
|
|
|
* unaligned memory access faults can occur */
|
|
|
|
filter->model_matrix = graphene_matrix_alloc ();
|
|
|
|
filter->view_matrix = graphene_matrix_alloc ();
|
|
|
|
filter->projection_matrix = graphene_matrix_alloc ();
|
|
|
|
filter->inv_model_matrix = graphene_matrix_alloc ();
|
|
|
|
filter->inv_view_matrix = graphene_matrix_alloc ();
|
|
|
|
filter->inv_projection_matrix = graphene_matrix_alloc ();
|
|
|
|
filter->mvp_matrix = graphene_matrix_alloc ();
|
|
|
|
|
|
|
|
filter->camera_position = graphene_vec3_alloc ();
|
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
gst_gl_transformation_build_mvp (filter);
|
|
|
|
}
|
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
static void
|
|
|
|
gst_gl_transformation_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstGLTransformation *transformation;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_GL_TRANSFORMATION (object));
|
|
|
|
|
|
|
|
transformation = GST_GL_TRANSFORMATION (object);
|
|
|
|
|
|
|
|
graphene_matrix_free (transformation->model_matrix);
|
|
|
|
graphene_matrix_free (transformation->view_matrix);
|
|
|
|
graphene_matrix_free (transformation->projection_matrix);
|
|
|
|
graphene_matrix_free (transformation->inv_model_matrix);
|
|
|
|
graphene_matrix_free (transformation->inv_view_matrix);
|
|
|
|
graphene_matrix_free (transformation->inv_projection_matrix);
|
|
|
|
graphene_matrix_free (transformation->mvp_matrix);
|
|
|
|
|
|
|
|
graphene_vec3_free (transformation->camera_position);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
static void
|
|
|
|
gst_gl_transformation_build_mvp (GstGLTransformation * transformation)
|
|
|
|
{
|
2016-01-14 13:00:29 +00:00
|
|
|
GstGLFilter *filter = GST_GL_FILTER (transformation);
|
2016-01-14 06:45:03 +00:00
|
|
|
graphene_matrix_t modelview_matrix;
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2016-01-14 13:00:29 +00:00
|
|
|
if (!filter->out_info.finfo) {
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_init_identity (transformation->model_matrix);
|
|
|
|
graphene_matrix_init_identity (transformation->view_matrix);
|
|
|
|
graphene_matrix_init_identity (transformation->projection_matrix);
|
2016-01-14 13:00:29 +00:00
|
|
|
} else {
|
|
|
|
graphene_point3d_t translation_vector =
|
|
|
|
GRAPHENE_POINT3D_INIT (transformation->xtranslation * 2.0 *
|
|
|
|
transformation->aspect,
|
|
|
|
transformation->ytranslation * 2.0,
|
|
|
|
transformation->ztranslation * 2.0);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2016-01-14 13:00:29 +00:00
|
|
|
graphene_point3d_t pivot_vector =
|
|
|
|
GRAPHENE_POINT3D_INIT (-transformation->xpivot * transformation->aspect,
|
|
|
|
transformation->ypivot,
|
|
|
|
-transformation->zpivot);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2016-01-14 13:00:29 +00:00
|
|
|
graphene_point3d_t negative_pivot_vector;
|
2015-02-28 00:07:44 +00:00
|
|
|
|
2016-01-14 13:00:29 +00:00
|
|
|
graphene_vec3_t center;
|
|
|
|
graphene_vec3_t up;
|
2014-07-07 08:52:57 +00:00
|
|
|
|
2016-02-02 23:59:23 +00:00
|
|
|
gboolean current_passthrough;
|
2016-01-14 13:00:29 +00:00
|
|
|
gboolean passthrough;
|
2014-07-07 08:52:57 +00:00
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_vec3_init (transformation->camera_position, 0.f, 0.f, 1.f);
|
2016-01-14 13:00:29 +00:00
|
|
|
graphene_vec3_init (¢er, 0.f, 0.f, 0.f);
|
|
|
|
graphene_vec3_init (&up, 0.f, 1.f, 0.f);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2016-01-14 13:00:29 +00:00
|
|
|
/* Translate into pivot origin */
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_init_translate (transformation->model_matrix,
|
2016-02-02 23:59:23 +00:00
|
|
|
&pivot_vector);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2016-01-14 13:00:29 +00:00
|
|
|
/* Scale */
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_scale (transformation->model_matrix,
|
2016-01-14 13:00:29 +00:00
|
|
|
transformation->xscale, transformation->yscale, 1.0f);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2016-02-02 23:59:23 +00:00
|
|
|
/* Rotation */
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_rotate (transformation->model_matrix,
|
2016-01-14 13:00:29 +00:00
|
|
|
transformation->xrotation, graphene_vec3_x_axis ());
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_rotate (transformation->model_matrix,
|
2016-01-14 13:00:29 +00:00
|
|
|
transformation->yrotation, graphene_vec3_y_axis ());
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_rotate (transformation->model_matrix,
|
2016-01-14 13:00:29 +00:00
|
|
|
transformation->zrotation, graphene_vec3_z_axis ());
|
2016-01-14 07:42:05 +00:00
|
|
|
|
2016-02-02 23:59:23 +00:00
|
|
|
/* Translate back from pivot origin */
|
2016-01-14 13:00:29 +00:00
|
|
|
graphene_point3d_scale (&pivot_vector, -1.0, &negative_pivot_vector);
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_translate (transformation->model_matrix,
|
2016-01-14 13:00:29 +00:00
|
|
|
&negative_pivot_vector);
|
|
|
|
|
|
|
|
/* Translation */
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_translate (transformation->model_matrix,
|
2016-01-14 13:00:29 +00:00
|
|
|
&translation_vector);
|
|
|
|
|
|
|
|
if (transformation->ortho) {
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_init_ortho (transformation->projection_matrix,
|
2016-01-14 13:00:29 +00:00
|
|
|
-transformation->aspect, transformation->aspect,
|
|
|
|
-1, 1, transformation->znear, transformation->zfar);
|
|
|
|
} else {
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_init_perspective (transformation->projection_matrix,
|
2016-01-14 13:00:29 +00:00
|
|
|
transformation->fov,
|
|
|
|
transformation->aspect, transformation->znear, transformation->zfar);
|
|
|
|
}
|
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_init_look_at (transformation->view_matrix,
|
|
|
|
transformation->camera_position, ¢er, &up);
|
2016-01-14 13:00:29 +00:00
|
|
|
|
2016-02-02 23:59:23 +00:00
|
|
|
current_passthrough =
|
|
|
|
gst_base_transform_is_passthrough (GST_BASE_TRANSFORM (transformation));
|
2016-01-14 13:00:29 +00:00
|
|
|
passthrough = transformation->xtranslation == 0.
|
2016-01-14 07:42:05 +00:00
|
|
|
&& transformation->ytranslation == 0.
|
2016-02-02 23:59:23 +00:00
|
|
|
&& transformation->ztranslation == 0. && transformation->xrotation == 0.
|
|
|
|
&& transformation->yrotation == 0. && transformation->zrotation == 0.
|
|
|
|
&& transformation->xscale == 1. && transformation->yscale == 1.
|
2016-01-14 07:42:05 +00:00
|
|
|
&& gst_video_info_is_equal (&filter->in_info, &filter->out_info);
|
|
|
|
gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (transformation),
|
|
|
|
passthrough);
|
2016-02-02 23:59:23 +00:00
|
|
|
if (current_passthrough != passthrough) {
|
|
|
|
gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (transformation));
|
|
|
|
}
|
2016-01-14 07:42:05 +00:00
|
|
|
}
|
2016-01-14 13:00:29 +00:00
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_multiply (transformation->model_matrix,
|
|
|
|
transformation->view_matrix, &modelview_matrix);
|
2016-01-14 13:00:29 +00:00
|
|
|
graphene_matrix_multiply (&modelview_matrix,
|
2022-04-06 16:08:46 +00:00
|
|
|
transformation->projection_matrix, transformation->mvp_matrix);
|
|
|
|
|
|
|
|
graphene_matrix_inverse (transformation->model_matrix,
|
|
|
|
transformation->inv_model_matrix);
|
|
|
|
graphene_matrix_inverse (transformation->view_matrix,
|
|
|
|
transformation->inv_view_matrix);
|
|
|
|
graphene_matrix_inverse (transformation->projection_matrix,
|
|
|
|
transformation->inv_projection_matrix);
|
2014-05-27 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_transformation_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstGLTransformation *filter = GST_GL_TRANSFORMATION (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2014-07-07 08:52:57 +00:00
|
|
|
case PROP_FOV:
|
|
|
|
filter->fov = g_value_get_float (value);
|
2014-05-27 10:40:09 +00:00
|
|
|
break;
|
|
|
|
case PROP_ORTHO:
|
|
|
|
filter->ortho = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
case PROP_TRANSLATION_X:
|
|
|
|
filter->xtranslation = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_TRANSLATION_Y:
|
|
|
|
filter->ytranslation = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_TRANSLATION_Z:
|
|
|
|
filter->ztranslation = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_ROTATION_X:
|
|
|
|
filter->xrotation = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_ROTATION_Y:
|
|
|
|
filter->yrotation = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_ROTATION_Z:
|
|
|
|
filter->zrotation = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_SCALE_X:
|
|
|
|
filter->xscale = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_SCALE_Y:
|
|
|
|
filter->yscale = g_value_get_float (value);
|
|
|
|
break;
|
2015-02-28 00:07:44 +00:00
|
|
|
case PROP_PIVOT_X:
|
|
|
|
filter->xpivot = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_PIVOT_Y:
|
|
|
|
filter->ypivot = g_value_get_float (value);
|
|
|
|
break;
|
|
|
|
case PROP_PIVOT_Z:
|
|
|
|
filter->zpivot = g_value_get_float (value);
|
|
|
|
break;
|
2014-05-27 10:40:09 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gst_gl_transformation_build_mvp (filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_transformation_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstGLTransformation *filter = GST_GL_TRANSFORMATION (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2014-07-07 08:52:57 +00:00
|
|
|
case PROP_FOV:
|
|
|
|
g_value_set_float (value, filter->fov);
|
2014-05-27 10:40:09 +00:00
|
|
|
break;
|
|
|
|
case PROP_ORTHO:
|
|
|
|
g_value_set_boolean (value, filter->ortho);
|
|
|
|
break;
|
|
|
|
case PROP_TRANSLATION_X:
|
|
|
|
g_value_set_float (value, filter->xtranslation);
|
|
|
|
break;
|
|
|
|
case PROP_TRANSLATION_Y:
|
|
|
|
g_value_set_float (value, filter->ytranslation);
|
|
|
|
break;
|
|
|
|
case PROP_TRANSLATION_Z:
|
|
|
|
g_value_set_float (value, filter->ztranslation);
|
|
|
|
break;
|
|
|
|
case PROP_ROTATION_X:
|
|
|
|
g_value_set_float (value, filter->xrotation);
|
|
|
|
break;
|
|
|
|
case PROP_ROTATION_Y:
|
|
|
|
g_value_set_float (value, filter->yrotation);
|
|
|
|
break;
|
|
|
|
case PROP_ROTATION_Z:
|
|
|
|
g_value_set_float (value, filter->zrotation);
|
|
|
|
break;
|
|
|
|
case PROP_SCALE_X:
|
|
|
|
g_value_set_float (value, filter->xscale);
|
|
|
|
break;
|
|
|
|
case PROP_SCALE_Y:
|
|
|
|
g_value_set_float (value, filter->yscale);
|
|
|
|
break;
|
2015-02-28 00:07:44 +00:00
|
|
|
case PROP_PIVOT_X:
|
|
|
|
g_value_set_float (value, filter->xpivot);
|
|
|
|
break;
|
|
|
|
case PROP_PIVOT_Y:
|
|
|
|
g_value_set_float (value, filter->ypivot);
|
|
|
|
break;
|
|
|
|
case PROP_PIVOT_Z:
|
|
|
|
g_value_set_float (value, filter->zpivot);
|
|
|
|
break;
|
2014-07-07 08:52:57 +00:00
|
|
|
case PROP_MVP:
|
2016-08-19 05:35:49 +00:00
|
|
|
/* FIXME: need to decompose this to support navigation events */
|
2022-04-06 16:08:46 +00:00
|
|
|
g_value_set_boxed (value, (gconstpointer) filter->mvp_matrix);
|
2014-07-07 08:52:57 +00:00
|
|
|
break;
|
2014-05-27 10:40:09 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gl_transformation_set_caps (GstGLFilter * filter, GstCaps * incaps,
|
|
|
|
GstCaps * outcaps)
|
|
|
|
{
|
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (filter);
|
|
|
|
|
|
|
|
transformation->aspect =
|
|
|
|
(gdouble) GST_VIDEO_INFO_WIDTH (&filter->out_info) /
|
|
|
|
(gdouble) GST_VIDEO_INFO_HEIGHT (&filter->out_info);
|
|
|
|
|
2014-11-13 01:12:46 +00:00
|
|
|
transformation->caps_change = TRUE;
|
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
gst_gl_transformation_build_mvp (transformation);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-01-14 06:45:03 +00:00
|
|
|
static void
|
2016-08-19 05:35:49 +00:00
|
|
|
_intersect_plane_and_ray (graphene_plane_t * video_plane, graphene_ray_t * ray,
|
|
|
|
graphene_point3d_t * result)
|
2016-01-14 06:45:03 +00:00
|
|
|
{
|
2016-08-19 05:35:49 +00:00
|
|
|
float t = graphene_ray_get_distance_to_plane (ray, video_plane);
|
|
|
|
GST_TRACE ("Calculated a distance of %f to the plane", t);
|
|
|
|
graphene_ray_get_position_at (ray, t, result);
|
|
|
|
}
|
2016-01-14 06:45:03 +00:00
|
|
|
|
2016-08-19 05:35:49 +00:00
|
|
|
static void
|
|
|
|
_screen_coord_to_world_ray (GstGLTransformation * transformation, float x,
|
|
|
|
float y, graphene_ray_t * ray)
|
|
|
|
{
|
|
|
|
GstGLFilter *filter = GST_GL_FILTER (transformation);
|
|
|
|
gfloat w = (gfloat) GST_VIDEO_INFO_WIDTH (&filter->in_info);
|
|
|
|
gfloat h = (gfloat) GST_VIDEO_INFO_HEIGHT (&filter->in_info);
|
|
|
|
graphene_vec3_t ray_eye_vec3, ray_world_dir, *ray_origin, *ray_direction;
|
|
|
|
graphene_vec3_t ray_ortho_dir;
|
|
|
|
graphene_point3d_t ray_clip, ray_eye;
|
|
|
|
graphene_vec2_t screen_coord;
|
2016-01-14 06:45:03 +00:00
|
|
|
|
2016-08-19 05:35:49 +00:00
|
|
|
/* GL is y-flipped. i.e. 0, 0 is the bottom left corner in screen space */
|
|
|
|
graphene_vec2_init (&screen_coord, (2. * x / w - 1.) / transformation->aspect,
|
|
|
|
1. - 2. * y / h);
|
|
|
|
|
|
|
|
graphene_point3d_init (&ray_clip, graphene_vec2_get_x (&screen_coord),
|
|
|
|
graphene_vec2_get_y (&screen_coord), -1.);
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_transform_point3d (transformation->inv_projection_matrix,
|
2016-08-19 05:35:49 +00:00
|
|
|
&ray_clip, &ray_eye);
|
|
|
|
|
|
|
|
graphene_vec3_init (&ray_eye_vec3, ray_eye.x, ray_eye.y, -1.);
|
|
|
|
|
|
|
|
if (transformation->ortho) {
|
|
|
|
graphene_vec3_init (&ray_ortho_dir, 0., 0., 1.);
|
|
|
|
|
|
|
|
ray_origin = &ray_eye_vec3;
|
|
|
|
ray_direction = &ray_ortho_dir;
|
|
|
|
} else {
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_transform_vec3 (transformation->inv_view_matrix,
|
2016-08-19 05:35:49 +00:00
|
|
|
&ray_eye_vec3, &ray_world_dir);
|
|
|
|
graphene_vec3_normalize (&ray_world_dir, &ray_world_dir);
|
2016-01-14 06:45:03 +00:00
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
ray_origin = transformation->camera_position;
|
2016-08-19 05:35:49 +00:00
|
|
|
ray_direction = &ray_world_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
graphene_ray_init_from_vec3 (ray, ray_origin, ray_direction);
|
|
|
|
|
|
|
|
GST_TRACE_OBJECT (transformation, "Calculated ray origin: " VEC3_FORMAT
|
|
|
|
" direction: " VEC3_FORMAT " from screen coordinates: " VEC2_FORMAT
|
|
|
|
" with %s projection",
|
|
|
|
VEC3_ARGS (ray_origin), VEC3_ARGS (ray_direction),
|
|
|
|
VEC2_ARGS (&screen_coord),
|
|
|
|
transformation->ortho ? "ortho" : "perspection");
|
2016-01-14 06:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-08-19 05:35:49 +00:00
|
|
|
_init_world_video_plane (GstGLTransformation * transformation,
|
|
|
|
graphene_plane_t * video_plane)
|
2016-01-14 06:45:03 +00:00
|
|
|
{
|
2016-08-19 05:35:49 +00:00
|
|
|
graphene_point3d_t bottom_left, bottom_right, top_left, top_right;
|
|
|
|
graphene_point3d_t world_bottom_left, world_bottom_right;
|
|
|
|
graphene_point3d_t world_top_left, world_top_right;
|
|
|
|
|
|
|
|
graphene_point3d_init (&top_left, -transformation->aspect, 1., 0.);
|
|
|
|
graphene_point3d_init (&top_right, transformation->aspect, 1., 0.);
|
|
|
|
graphene_point3d_init (&bottom_left, -transformation->aspect, -1., 0.);
|
|
|
|
graphene_point3d_init (&bottom_right, transformation->aspect, -1., 0.);
|
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_transform_point3d (transformation->model_matrix,
|
2016-08-19 05:35:49 +00:00
|
|
|
&bottom_left, &world_bottom_left);
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_transform_point3d (transformation->model_matrix,
|
2016-08-19 05:35:49 +00:00
|
|
|
&bottom_right, &world_bottom_right);
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_transform_point3d (transformation->model_matrix,
|
2016-08-19 05:35:49 +00:00
|
|
|
&top_left, &world_top_left);
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_transform_point3d (transformation->model_matrix,
|
2016-08-19 05:35:49 +00:00
|
|
|
&top_right, &world_top_right);
|
|
|
|
|
|
|
|
graphene_plane_init_from_points (video_plane, &world_bottom_left,
|
|
|
|
&world_top_right, &world_top_left);
|
|
|
|
}
|
2016-01-14 06:45:03 +00:00
|
|
|
|
2016-08-19 05:35:49 +00:00
|
|
|
static gboolean
|
|
|
|
_screen_coord_to_model_coord (GstGLTransformation * transformation,
|
|
|
|
double x, double y, double *res_x, double *res_y)
|
|
|
|
{
|
|
|
|
GstGLFilter *filter = GST_GL_FILTER (transformation);
|
|
|
|
double w = (double) GST_VIDEO_INFO_WIDTH (&filter->in_info);
|
|
|
|
double h = (double) GST_VIDEO_INFO_HEIGHT (&filter->in_info);
|
|
|
|
graphene_point3d_t world_point, model_coord;
|
|
|
|
graphene_plane_t video_plane;
|
|
|
|
graphene_ray_t ray;
|
|
|
|
double new_x, new_y;
|
|
|
|
|
|
|
|
_init_world_video_plane (transformation, &video_plane);
|
|
|
|
_screen_coord_to_world_ray (transformation, x, y, &ray);
|
|
|
|
_intersect_plane_and_ray (&video_plane, &ray, &world_point);
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_transform_point3d (transformation->inv_model_matrix,
|
2016-08-19 05:35:49 +00:00
|
|
|
&world_point, &model_coord);
|
|
|
|
|
|
|
|
/* ndc to pixels. We render the frame Y-flipped so need to unflip the
|
|
|
|
* y coordinate */
|
|
|
|
new_x = (model_coord.x + 1.) * w / 2;
|
|
|
|
new_y = (1. - model_coord.y) * h / 2;
|
|
|
|
|
|
|
|
if (new_x < 0. || new_x > w || new_y < 0. || new_y > h)
|
|
|
|
/* coords off video surface */
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (transformation, "converted %f,%f to %f,%f", x, y, new_x,
|
|
|
|
new_y);
|
|
|
|
|
|
|
|
if (res_x)
|
|
|
|
*res_x = new_x;
|
|
|
|
if (res_y)
|
|
|
|
*res_y = new_y;
|
|
|
|
|
|
|
|
return TRUE;
|
2016-01-14 06:45:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 05:35:49 +00:00
|
|
|
#if 0
|
|
|
|
/* debugging facilities for transforming vertices from model space to screen
|
|
|
|
* space */
|
|
|
|
static void
|
|
|
|
_ndc_to_viewport (GstGLTransformation * transformation, graphene_vec3_t * ndc,
|
|
|
|
int x, int y, int w, int h, float near, float far, graphene_vec3_t * result)
|
|
|
|
{
|
|
|
|
GstGLFilter *filter = GST_GL_FILTER (transformation);
|
|
|
|
/* center of the viewport */
|
|
|
|
int o_x = x + w / 2;
|
|
|
|
int o_y = y + h / 2;
|
|
|
|
|
|
|
|
graphene_vec3_init (result, graphene_vec3_get_x (ndc) * w / 2 + o_x,
|
|
|
|
graphene_vec3_get_y (ndc) * h / 2 + o_y,
|
|
|
|
(far - near) * graphene_vec3_get_z (ndc) / 2 + (far + near) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_perspective_division (graphene_vec4_t * clip, graphene_vec3_t * result)
|
|
|
|
{
|
|
|
|
float w = graphene_vec4_get_w (clip);
|
|
|
|
|
|
|
|
graphene_vec3_init (result, graphene_vec4_get_x (clip) / w,
|
|
|
|
graphene_vec4_get_y (clip) / w, graphene_vec4_get_z (clip) / w);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_vertex_to_screen_coord (GstGLTransformation * transformation,
|
|
|
|
graphene_vec4_t * vertex, graphene_vec3_t * view)
|
|
|
|
{
|
|
|
|
GstGLFilter *filter = GST_GL_FILTER (transformation);
|
|
|
|
gint w = GST_VIDEO_INFO_WIDTH (&filter->in_info);
|
|
|
|
gint h = GST_VIDEO_INFO_HEIGHT (&filter->in_info);
|
|
|
|
graphene_vec4_t clip;
|
|
|
|
graphene_vec3_t ndc;
|
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_transform_vec4 (transformation->mvp_matrix, vertex, &clip);
|
2016-08-19 05:35:49 +00:00
|
|
|
_perspective_division (&clip, &ndc);
|
|
|
|
_ndc_to_viewport (transformation, &ndc, 0, 0, w, h, 0., 1., view);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-14 06:45:03 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_transformation_src_event (GstBaseTransform * trans, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (trans);
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (trans, "handling %s event", GST_EVENT_TYPE_NAME (event));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2016-08-19 05:35:49 +00:00
|
|
|
case GST_EVENT_NAVIGATION:{
|
|
|
|
gdouble x, y;
|
2022-02-14 15:08:23 +00:00
|
|
|
event = gst_event_make_writable (event);
|
2016-01-14 06:45:03 +00:00
|
|
|
|
2022-02-14 15:08:23 +00:00
|
|
|
if (gst_navigation_event_get_coordinates (event, &x, &y)) {
|
2016-08-19 05:35:49 +00:00
|
|
|
gdouble new_x, new_y;
|
2016-01-14 06:45:03 +00:00
|
|
|
|
2016-08-19 05:35:49 +00:00
|
|
|
if (!_screen_coord_to_model_coord (transformation, x, y, &new_x,
|
|
|
|
&new_y)) {
|
2016-01-14 06:45:03 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-02-14 15:08:23 +00:00
|
|
|
gst_navigation_event_set_coordinates (event, x, y);
|
2016-01-14 06:45:03 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-08-19 05:35:49 +00:00
|
|
|
}
|
2016-01-14 06:45:03 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-14 13:32:06 +00:00
|
|
|
static gboolean
|
|
|
|
gst_gl_transformation_filter_meta (GstBaseTransform * trans, GstQuery * query,
|
|
|
|
GType api, const GstStructure * params)
|
|
|
|
{
|
|
|
|
if (api == GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (api == GST_GL_SYNC_META_API_TYPE)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gl_transformation_decide_allocation (GstBaseTransform * trans,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (trans);
|
|
|
|
|
|
|
|
if (!GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
|
|
|
|
query))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (gst_query_find_allocation_meta (query,
|
|
|
|
GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE, NULL)) {
|
|
|
|
transformation->downstream_supports_affine_meta = TRUE;
|
|
|
|
} else {
|
|
|
|
transformation->downstream_supports_affine_meta = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-11-13 01:12:46 +00:00
|
|
|
static void
|
2016-11-07 15:21:20 +00:00
|
|
|
gst_gl_transformation_gl_stop (GstGLBaseFilter * base_filter)
|
2014-11-13 01:12:46 +00:00
|
|
|
{
|
2016-11-07 15:21:20 +00:00
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (base_filter);
|
|
|
|
const GstGLFuncs *gl = base_filter->context->gl_vtable;
|
2014-11-13 01:12:46 +00:00
|
|
|
|
|
|
|
if (transformation->vao) {
|
|
|
|
gl->DeleteVertexArrays (1, &transformation->vao);
|
|
|
|
transformation->vao = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transformation->vertex_buffer) {
|
|
|
|
gl->DeleteBuffers (1, &transformation->vertex_buffer);
|
|
|
|
transformation->vertex_buffer = 0;
|
|
|
|
}
|
2015-04-21 05:28:58 +00:00
|
|
|
|
2015-05-01 02:04:28 +00:00
|
|
|
if (transformation->vbo_indices) {
|
|
|
|
gl->DeleteBuffers (1, &transformation->vbo_indices);
|
|
|
|
transformation->vbo_indices = 0;
|
|
|
|
}
|
|
|
|
|
2015-04-27 08:01:53 +00:00
|
|
|
if (transformation->shader) {
|
|
|
|
gst_object_unref (transformation->shader);
|
|
|
|
transformation->shader = NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-07 15:21:20 +00:00
|
|
|
GST_GL_BASE_FILTER_CLASS (parent_class)->gl_stop (base_filter);
|
2014-05-27 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2016-11-07 15:21:20 +00:00
|
|
|
gst_gl_transformation_gl_start (GstGLBaseFilter * base_filter)
|
2014-05-27 10:40:09 +00:00
|
|
|
{
|
2016-11-07 15:21:20 +00:00
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (base_filter);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2016-11-07 15:21:20 +00:00
|
|
|
if (!GST_GL_BASE_FILTER_CLASS (parent_class)->gl_start (base_filter))
|
|
|
|
return FALSE;
|
2015-04-21 07:43:58 +00:00
|
|
|
|
2016-11-07 15:21:20 +00:00
|
|
|
if (gst_gl_context_get_gl_api (base_filter->context)) {
|
2019-02-28 05:17:37 +00:00
|
|
|
gchar *frag_str;
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
frag_str =
|
|
|
|
gst_gl_shader_string_fragment_get_default (base_filter->context,
|
|
|
|
GST_GLSL_VERSION_NONE,
|
|
|
|
GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY);
|
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
/* blocking call, wait until the opengl thread has compiled the shader */
|
2019-02-28 05:17:37 +00:00
|
|
|
ret = gst_gl_context_gen_shader (base_filter->context,
|
2016-05-14 13:32:06 +00:00
|
|
|
gst_gl_shader_string_vertex_mat4_vertex_transform,
|
2019-02-28 05:17:37 +00:00
|
|
|
frag_str, &transformation->shader);
|
|
|
|
|
|
|
|
g_free (frag_str);
|
|
|
|
|
|
|
|
return ret;
|
2014-05-27 10:40:09 +00:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-05-14 13:32:06 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_gl_transformation_prepare_output_buffer (GstBaseTransform * trans,
|
|
|
|
GstBuffer * inbuf, GstBuffer ** outbuf)
|
|
|
|
{
|
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (trans);
|
|
|
|
GstGLFilter *filter = GST_GL_FILTER (trans);
|
|
|
|
|
|
|
|
if (transformation->downstream_supports_affine_meta &&
|
|
|
|
gst_video_info_is_equal (&filter->in_info, &filter->out_info)) {
|
|
|
|
GstVideoAffineTransformationMeta *af_meta;
|
2017-08-17 03:46:04 +00:00
|
|
|
graphene_matrix_t upstream_matrix, tmp, tmp2, inv_aspect, yflip;
|
|
|
|
float upstream[16], downstream[16];
|
2016-05-14 13:32:06 +00:00
|
|
|
|
|
|
|
*outbuf = gst_buffer_make_writable (inbuf);
|
|
|
|
|
|
|
|
af_meta = gst_buffer_get_video_affine_transformation_meta (inbuf);
|
|
|
|
if (!af_meta)
|
|
|
|
af_meta = gst_buffer_add_video_affine_transformation_meta (*outbuf);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (trans, "applying transformation to existing affine "
|
|
|
|
"transformation meta");
|
|
|
|
|
2017-08-29 21:21:05 +00:00
|
|
|
gst_gl_get_affine_transformation_meta_as_ndc (af_meta, upstream);
|
2017-08-17 03:46:04 +00:00
|
|
|
|
2016-05-14 13:32:06 +00:00
|
|
|
/* apply the transformation to the existing affine meta */
|
2017-08-17 03:46:04 +00:00
|
|
|
graphene_matrix_init_from_float (&upstream_matrix, upstream);
|
|
|
|
graphene_matrix_init_scale (&inv_aspect, transformation->aspect, -1., 1.);
|
|
|
|
graphene_matrix_init_scale (&yflip, 1., -1., 1.);
|
2016-05-14 13:32:06 +00:00
|
|
|
|
2017-08-17 03:46:04 +00:00
|
|
|
/* invert the aspect effects */
|
|
|
|
graphene_matrix_multiply (&upstream_matrix, &inv_aspect, &tmp2);
|
|
|
|
/* apply the transformation */
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_multiply (&tmp2, transformation->mvp_matrix, &tmp);
|
2017-08-17 03:46:04 +00:00
|
|
|
/* and undo yflip */
|
|
|
|
graphene_matrix_multiply (&tmp, &yflip, &tmp2);
|
2016-05-14 13:32:06 +00:00
|
|
|
|
2017-08-17 03:46:04 +00:00
|
|
|
graphene_matrix_to_float (&tmp2, downstream);
|
2017-08-29 21:21:05 +00:00
|
|
|
gst_gl_set_affine_transformation_meta_from_ndc (af_meta, downstream);
|
2016-05-14 13:32:06 +00:00
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_BASE_TRANSFORM_CLASS (parent_class)->prepare_output_buffer (trans,
|
|
|
|
inbuf, outbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_gl_transformation_filter (GstGLFilter * filter,
|
|
|
|
GstBuffer * inbuf, GstBuffer * outbuf)
|
|
|
|
{
|
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (filter);
|
|
|
|
|
|
|
|
if (transformation->downstream_supports_affine_meta &&
|
|
|
|
gst_video_info_is_equal (&filter->in_info, &filter->out_info)) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return gst_gl_filter_filter_texture (filter, inbuf, outbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
static gboolean
|
2016-07-11 14:30:22 +00:00
|
|
|
gst_gl_transformation_filter_texture (GstGLFilter * filter,
|
|
|
|
GstGLMemory * in_tex, GstGLMemory * out_tex)
|
2014-05-27 10:40:09 +00:00
|
|
|
{
|
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (filter);
|
|
|
|
|
|
|
|
transformation->in_tex = in_tex;
|
|
|
|
|
2016-07-12 02:59:57 +00:00
|
|
|
gst_gl_framebuffer_draw_to_texture (filter->fbo, out_tex,
|
|
|
|
(GstGLFramebufferFunc) gst_gl_transformation_callback, transformation);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-08-17 03:42:21 +00:00
|
|
|
static const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
|
2014-11-13 01:12:46 +00:00
|
|
|
|
2014-05-27 10:40:09 +00:00
|
|
|
static void
|
2014-11-13 01:12:46 +00:00
|
|
|
_upload_vertices (GstGLTransformation * transformation)
|
2014-05-27 10:40:09 +00:00
|
|
|
{
|
2015-03-13 10:25:35 +00:00
|
|
|
const GstGLFuncs *gl =
|
|
|
|
GST_GL_BASE_FILTER (transformation)->context->gl_vtable;
|
2014-05-27 10:40:09 +00:00
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
2014-11-13 01:12:46 +00:00
|
|
|
GLfloat vertices[] = {
|
2017-08-17 03:42:21 +00:00
|
|
|
-transformation->aspect, -1.0, 0.0, 1.0, 0.0, 0.0,
|
2014-11-13 01:12:46 +00:00
|
|
|
transformation->aspect, -1.0, 0.0, 1.0, 1.0, 0.0,
|
2017-08-17 03:42:21 +00:00
|
|
|
transformation->aspect, 1.0, 0.0, 1.0, 1.0, 1.0,
|
|
|
|
-transformation->aspect, 1.0, 0.0, 1.0, 0.0, 1.0,
|
2014-05-27 10:40:09 +00:00
|
|
|
};
|
2014-11-13 01:12:46 +00:00
|
|
|
/* *INDENT-ON* */
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2014-11-13 01:12:46 +00:00
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, transformation->vertex_buffer);
|
|
|
|
|
|
|
|
gl->BufferData (GL_ARRAY_BUFFER, 4 * 6 * sizeof (GLfloat), vertices,
|
|
|
|
GL_STATIC_DRAW);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_bind_buffer (GstGLTransformation * transformation)
|
|
|
|
{
|
2015-03-13 10:25:35 +00:00
|
|
|
const GstGLFuncs *gl =
|
|
|
|
GST_GL_BASE_FILTER (transformation)->context->gl_vtable;
|
2014-11-13 01:12:46 +00:00
|
|
|
|
2015-05-01 02:04:28 +00:00
|
|
|
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, transformation->vbo_indices);
|
2014-11-13 01:12:46 +00:00
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, transformation->vertex_buffer);
|
|
|
|
|
|
|
|
/* Load the vertex position */
|
|
|
|
gl->VertexAttribPointer (transformation->attr_position, 4, GL_FLOAT,
|
|
|
|
GL_FALSE, 6 * sizeof (GLfloat), (void *) 0);
|
|
|
|
|
|
|
|
/* Load the texture coordinate */
|
|
|
|
gl->VertexAttribPointer (transformation->attr_texture, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
6 * sizeof (GLfloat), (void *) (4 * sizeof (GLfloat)));
|
|
|
|
|
|
|
|
gl->EnableVertexAttribArray (transformation->attr_position);
|
|
|
|
gl->EnableVertexAttribArray (transformation->attr_texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_unbind_buffer (GstGLTransformation * transformation)
|
|
|
|
{
|
2015-03-13 10:25:35 +00:00
|
|
|
const GstGLFuncs *gl =
|
|
|
|
GST_GL_BASE_FILTER (transformation)->context->gl_vtable;
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2015-05-01 02:04:28 +00:00
|
|
|
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
|
2014-11-13 01:12:46 +00:00
|
|
|
gl->BindBuffer (GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
gl->DisableVertexAttribArray (transformation->attr_position);
|
|
|
|
gl->DisableVertexAttribArray (transformation->attr_texture);
|
|
|
|
}
|
|
|
|
|
2016-07-12 02:59:57 +00:00
|
|
|
static gboolean
|
2014-11-13 01:12:46 +00:00
|
|
|
gst_gl_transformation_callback (gpointer stuff)
|
|
|
|
{
|
|
|
|
GstGLFilter *filter = GST_GL_FILTER (stuff);
|
|
|
|
GstGLTransformation *transformation = GST_GL_TRANSFORMATION (filter);
|
2015-03-13 10:25:35 +00:00
|
|
|
GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
|
2014-05-27 10:40:09 +00:00
|
|
|
|
|
|
|
GLfloat temp_matrix[16];
|
|
|
|
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_clear_shader (GST_GL_BASE_FILTER (filter)->context);
|
2014-05-27 10:40:09 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, 0);
|
|
|
|
|
2015-12-11 22:33:38 +00:00
|
|
|
gl->ClearColor (0.f, 0.f, 0.f, 0.f);
|
2014-05-27 10:40:09 +00:00
|
|
|
gl->Clear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
gst_gl_shader_use (transformation->shader);
|
|
|
|
|
|
|
|
gl->ActiveTexture (GL_TEXTURE0);
|
2016-07-11 14:30:22 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, transformation->in_tex->tex_id);
|
2014-05-27 10:40:09 +00:00
|
|
|
gst_gl_shader_set_uniform_1i (transformation->shader, "texture", 0);
|
|
|
|
|
2022-04-06 16:08:46 +00:00
|
|
|
graphene_matrix_to_float (transformation->mvp_matrix, temp_matrix);
|
2016-05-14 13:32:06 +00:00
|
|
|
gst_gl_shader_set_uniform_matrix_4fv (transformation->shader,
|
|
|
|
"u_transformation", 1, GL_FALSE, temp_matrix);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2014-11-13 01:12:46 +00:00
|
|
|
if (!transformation->vertex_buffer) {
|
|
|
|
transformation->attr_position =
|
|
|
|
gst_gl_shader_get_attribute_location (transformation->shader,
|
2016-05-14 13:32:06 +00:00
|
|
|
"a_position");
|
2014-11-13 01:12:46 +00:00
|
|
|
|
|
|
|
transformation->attr_texture =
|
2016-05-14 13:32:06 +00:00
|
|
|
gst_gl_shader_get_attribute_location (transformation->shader,
|
|
|
|
"a_texcoord");
|
2014-11-13 01:12:46 +00:00
|
|
|
|
|
|
|
if (gl->GenVertexArrays) {
|
|
|
|
gl->GenVertexArrays (1, &transformation->vao);
|
|
|
|
gl->BindVertexArray (transformation->vao);
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->GenBuffers (1, &transformation->vertex_buffer);
|
2015-05-01 02:04:28 +00:00
|
|
|
|
|
|
|
gl->GenBuffers (1, &transformation->vbo_indices);
|
|
|
|
gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, transformation->vbo_indices);
|
|
|
|
gl->BufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (indices), indices,
|
|
|
|
GL_STATIC_DRAW);
|
|
|
|
|
2014-11-13 01:12:46 +00:00
|
|
|
transformation->caps_change = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gl->GenVertexArrays)
|
|
|
|
gl->BindVertexArray (transformation->vao);
|
|
|
|
|
2017-03-14 03:15:00 +00:00
|
|
|
if (transformation->caps_change)
|
2014-11-13 01:12:46 +00:00
|
|
|
_upload_vertices (transformation);
|
2017-03-14 03:15:00 +00:00
|
|
|
_bind_buffer (transformation);
|
2014-11-13 01:12:46 +00:00
|
|
|
|
2017-08-17 03:42:21 +00:00
|
|
|
gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2014-11-13 01:12:46 +00:00
|
|
|
if (gl->GenVertexArrays)
|
|
|
|
gl->BindVertexArray (0);
|
2019-01-16 12:09:18 +00:00
|
|
|
else
|
|
|
|
_unbind_buffer (transformation);
|
2014-05-27 10:40:09 +00:00
|
|
|
|
2015-03-13 10:25:35 +00:00
|
|
|
gst_gl_context_clear_shader (GST_GL_BASE_FILTER (filter)->context);
|
2014-11-13 01:12:46 +00:00
|
|
|
transformation->caps_change = FALSE;
|
2016-07-12 02:59:57 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2014-05-27 10:40:09 +00:00
|
|
|
}
|