3 * Copyright (C) 2014 Lubosz Sarnecki <lubosz@gmail.com>
4 * Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-gltransformation
24 * @title: gltransformation
26 * Transforms video on the GPU.
30 * gst-launch-1.0 gltestsrc ! gltransformation rotation-z=45 ! glimagesink
31 * ]| A pipeline to rotate by 45 degrees
33 * gst-launch-1.0 gltestsrc ! gltransformation translation-x=0.5 ! glimagesink
34 * ]| Translate the video by 0.5
36 * gst-launch-1.0 gltestsrc ! gltransformation scale-y=0.5 scale-x=0.5 ! glimagesink
37 * ]| Resize the video by 0.5
39 * gst-launch-1.0 gltestsrc ! gltransformation rotation-x=-45 ortho=True ! glimagesink
40 * ]| Rotate the video around the X-Axis by -45° with an orthographic projection
48 #include "gstgltransformation.h"
50 #include <gst/gl/gstglapi.h>
51 #include <graphene-gobject.h>
52 #include "gstglutils.h"
54 #define GST_CAT_DEFAULT gst_gl_transformation_debug
55 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
57 #define gst_gl_transformation_parent_class parent_class
59 #define VEC4_FORMAT "%f,%f,%f,%f"
60 #define VEC4_ARGS(v) graphene_vec4_get_x (v), graphene_vec4_get_y (v), graphene_vec4_get_z (v), graphene_vec4_get_w (v)
61 #define VEC3_FORMAT "%f,%f,%f"
62 #define VEC3_ARGS(v) graphene_vec3_get_x (v), graphene_vec3_get_y (v), graphene_vec3_get_z (v)
63 #define VEC2_FORMAT "%f,%f"
64 #define VEC2_ARGS(v) graphene_vec2_get_x (v), graphene_vec2_get_y (v)
65 #define POINT3D_FORMAT "%f,%f,%f"
66 #define POINT3D_ARGS(p) (p)->x, (p)->y, (p)->z
88 GST_DEBUG_CATEGORY_INIT (gst_gl_transformation_debug, "gltransformation", 0, "gltransformation element");
90 G_DEFINE_TYPE_WITH_CODE (GstGLTransformation, gst_gl_transformation,
91 GST_TYPE_GL_FILTER, DEBUG_INIT);
93 static void gst_gl_transformation_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_gl_transformation_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
98 static gboolean gst_gl_transformation_set_caps (GstGLFilter * filter,
99 GstCaps * incaps, GstCaps * outcaps);
100 static gboolean gst_gl_transformation_src_event (GstBaseTransform * trans,
102 static gboolean gst_gl_transformation_filter_meta (GstBaseTransform * trans,
103 GstQuery * query, GType api, const GstStructure * params);
104 static gboolean gst_gl_transformation_decide_allocation (GstBaseTransform *
105 trans, GstQuery * query);
107 static void gst_gl_transformation_gl_stop (GstGLBaseFilter * filter);
108 static gboolean gst_gl_transformation_gl_start (GstGLBaseFilter * base_filter);
109 static gboolean gst_gl_transformation_callback (gpointer stuff);
110 static void gst_gl_transformation_build_mvp (GstGLTransformation *
114 gst_gl_transformation_prepare_output_buffer (GstBaseTransform * trans,
115 GstBuffer * inbuf, GstBuffer ** outbuf);
116 static gboolean gst_gl_transformation_filter (GstGLFilter * filter,
117 GstBuffer * inbuf, GstBuffer * outbuf);
118 static gboolean gst_gl_transformation_filter_texture (GstGLFilter * filter,
119 GstGLMemory * in_tex, GstGLMemory * out_tex);
122 gst_gl_transformation_class_init (GstGLTransformationClass * klass)
124 GObjectClass *gobject_class;
125 GstElementClass *element_class;
126 GstBaseTransformClass *base_transform_class;
128 gobject_class = (GObjectClass *) klass;
129 element_class = GST_ELEMENT_CLASS (klass);
130 base_transform_class = GST_BASE_TRANSFORM_CLASS (klass);
132 gst_gl_filter_add_rgba_pad_templates (GST_GL_FILTER_CLASS (klass));
134 gobject_class->set_property = gst_gl_transformation_set_property;
135 gobject_class->get_property = gst_gl_transformation_get_property;
137 base_transform_class->src_event = gst_gl_transformation_src_event;
138 base_transform_class->decide_allocation =
139 gst_gl_transformation_decide_allocation;
140 base_transform_class->filter_meta = gst_gl_transformation_filter_meta;
142 GST_GL_BASE_FILTER_CLASS (klass)->gl_start = gst_gl_transformation_gl_start;
143 GST_GL_BASE_FILTER_CLASS (klass)->gl_stop = gst_gl_transformation_gl_stop;
145 GST_GL_FILTER_CLASS (klass)->set_caps = gst_gl_transformation_set_caps;
146 GST_GL_FILTER_CLASS (klass)->filter = gst_gl_transformation_filter;
147 GST_GL_FILTER_CLASS (klass)->filter_texture =
148 gst_gl_transformation_filter_texture;
149 GST_BASE_TRANSFORM_CLASS (klass)->prepare_output_buffer =
150 gst_gl_transformation_prepare_output_buffer;
152 g_object_class_install_property (gobject_class, PROP_FOV,
153 g_param_spec_float ("fov", "Fov", "Field of view angle in degrees",
154 0.0, G_MAXFLOAT, 90.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
156 g_object_class_install_property (gobject_class, PROP_ORTHO,
157 g_param_spec_boolean ("ortho", "Orthographic",
158 "Use orthographic projection", FALSE,
159 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 g_object_class_install_property (gobject_class, PROP_ROTATION_X,
163 g_param_spec_float ("rotation-x", "X Rotation",
164 "Rotates the video around the X-Axis in degrees.",
165 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
166 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (gobject_class, PROP_ROTATION_Y,
169 g_param_spec_float ("rotation-y", "Y Rotation",
170 "Rotates the video around the Y-Axis in degrees.",
171 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
172 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 g_object_class_install_property (gobject_class, PROP_ROTATION_Z,
175 g_param_spec_float ("rotation-z", "Z Rotation",
176 "Rotates the video around the Z-Axis in degrees.",
177 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
178 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 g_object_class_install_property (gobject_class, PROP_TRANSLATION_X,
182 g_param_spec_float ("translation-x", "X Translation",
183 "Translates the video at the X-Axis, in universal [0-1] coordinate.",
184 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
185 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 g_object_class_install_property (gobject_class, PROP_TRANSLATION_Y,
188 g_param_spec_float ("translation-y", "Y Translation",
189 "Translates the video at the Y-Axis, in universal [0-1] coordinate.",
190 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, PROP_TRANSLATION_Z,
194 g_param_spec_float ("translation-z", "Z Translation",
195 "Translates the video at the Z-Axis, in universal [0-1] coordinate.",
196 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 g_object_class_install_property (gobject_class, PROP_SCALE_X,
201 g_param_spec_float ("scale-x", "X Scale",
202 "Scale multiplier for the X-Axis.",
203 -G_MAXFLOAT, G_MAXFLOAT, 1.0,
204 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (gobject_class, PROP_SCALE_Y,
207 g_param_spec_float ("scale-y", "Y Scale",
208 "Scale multiplier for the Y-Axis.",
209 -G_MAXFLOAT, G_MAXFLOAT, 1.0,
210 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
213 g_object_class_install_property (gobject_class, PROP_PIVOT_X,
214 g_param_spec_float ("pivot-x", "X Pivot",
215 "Rotation pivot point X coordinate, where 0 is the center,"
216 " -1 the left border, +1 the right border and <-1, >1 outside.",
217 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (gobject_class, PROP_PIVOT_Y,
221 g_param_spec_float ("pivot-y", "Y Pivot",
222 "Rotation pivot point X coordinate, where 0 is the center,"
223 " -1 the left border, +1 the right border and <-1, >1 outside.",
224 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (gobject_class, PROP_PIVOT_Z,
228 g_param_spec_float ("pivot-z", "Z Pivot",
229 "Relevant for rotation in 3D space. You look into the negative Z axis direction",
230 -G_MAXFLOAT, G_MAXFLOAT, 0.0,
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 g_object_class_install_property (gobject_class, PROP_MVP,
235 g_param_spec_boxed ("mvp-matrix",
236 "Modelview Projection Matrix",
237 "The final Graphene 4x4 Matrix for transformation",
238 GRAPHENE_TYPE_MATRIX, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 gst_element_class_set_metadata (element_class, "OpenGL transformation filter",
241 "Filter/Effect/Video", "Transform video on the GPU",
242 "Lubosz Sarnecki <lubosz@gmail.com>\n"
243 "Matthew Waters <matthew@centricular.com>");
245 GST_GL_BASE_FILTER_CLASS (klass)->supported_gl_api =
246 GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2;
250 gst_gl_transformation_init (GstGLTransformation * filter)
252 filter->shader = NULL;
254 filter->aspect = 1.0;
255 filter->znear = 0.1f;
256 filter->zfar = 100.0;
258 filter->xscale = 1.0;
259 filter->yscale = 1.0;
263 gst_gl_transformation_build_mvp (filter);
267 gst_gl_transformation_build_mvp (GstGLTransformation * transformation)
269 GstGLFilter *filter = GST_GL_FILTER (transformation);
270 graphene_matrix_t modelview_matrix;
272 if (!filter->out_info.finfo) {
273 graphene_matrix_init_identity (&transformation->model_matrix);
274 graphene_matrix_init_identity (&transformation->view_matrix);
275 graphene_matrix_init_identity (&transformation->projection_matrix);
277 graphene_point3d_t translation_vector =
278 GRAPHENE_POINT3D_INIT (transformation->xtranslation * 2.0 *
279 transformation->aspect,
280 transformation->ytranslation * 2.0,
281 transformation->ztranslation * 2.0);
283 graphene_point3d_t pivot_vector =
284 GRAPHENE_POINT3D_INIT (-transformation->xpivot * transformation->aspect,
285 transformation->ypivot,
286 -transformation->zpivot);
288 graphene_point3d_t negative_pivot_vector;
290 graphene_vec3_t center;
293 gboolean current_passthrough;
294 gboolean passthrough;
296 graphene_vec3_init (&transformation->camera_position, 0.f, 0.f, 1.f);
297 graphene_vec3_init (¢er, 0.f, 0.f, 0.f);
298 graphene_vec3_init (&up, 0.f, 1.f, 0.f);
300 /* Translate into pivot origin */
301 graphene_matrix_init_translate (&transformation->model_matrix,
305 graphene_matrix_scale (&transformation->model_matrix,
306 transformation->xscale, transformation->yscale, 1.0f);
309 graphene_matrix_rotate (&transformation->model_matrix,
310 transformation->xrotation, graphene_vec3_x_axis ());
311 graphene_matrix_rotate (&transformation->model_matrix,
312 transformation->yrotation, graphene_vec3_y_axis ());
313 graphene_matrix_rotate (&transformation->model_matrix,
314 transformation->zrotation, graphene_vec3_z_axis ());
316 /* Translate back from pivot origin */
317 graphene_point3d_scale (&pivot_vector, -1.0, &negative_pivot_vector);
318 graphene_matrix_translate (&transformation->model_matrix,
319 &negative_pivot_vector);
322 graphene_matrix_translate (&transformation->model_matrix,
323 &translation_vector);
325 if (transformation->ortho) {
326 graphene_matrix_init_ortho (&transformation->projection_matrix,
327 -transformation->aspect, transformation->aspect,
328 -1, 1, transformation->znear, transformation->zfar);
330 graphene_matrix_init_perspective (&transformation->projection_matrix,
332 transformation->aspect, transformation->znear, transformation->zfar);
335 graphene_matrix_init_look_at (&transformation->view_matrix,
336 &transformation->camera_position, ¢er, &up);
338 current_passthrough =
339 gst_base_transform_is_passthrough (GST_BASE_TRANSFORM (transformation));
340 passthrough = transformation->xtranslation == 0.
341 && transformation->ytranslation == 0.
342 && transformation->ztranslation == 0. && transformation->xrotation == 0.
343 && transformation->yrotation == 0. && transformation->zrotation == 0.
344 && transformation->xscale == 1. && transformation->yscale == 1.
345 && gst_video_info_is_equal (&filter->in_info, &filter->out_info);
346 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (transformation),
348 if (current_passthrough != passthrough) {
349 gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (transformation));
353 graphene_matrix_multiply (&transformation->model_matrix,
354 &transformation->view_matrix, &modelview_matrix);
355 graphene_matrix_multiply (&modelview_matrix,
356 &transformation->projection_matrix, &transformation->mvp_matrix);
358 graphene_matrix_inverse (&transformation->model_matrix,
359 &transformation->inv_model_matrix);
360 graphene_matrix_inverse (&transformation->view_matrix,
361 &transformation->inv_view_matrix);
362 graphene_matrix_inverse (&transformation->projection_matrix,
363 &transformation->inv_projection_matrix);
367 gst_gl_transformation_set_property (GObject * object, guint prop_id,
368 const GValue * value, GParamSpec * pspec)
370 GstGLTransformation *filter = GST_GL_TRANSFORMATION (object);
374 filter->fov = g_value_get_float (value);
377 filter->ortho = g_value_get_boolean (value);
379 case PROP_TRANSLATION_X:
380 filter->xtranslation = g_value_get_float (value);
382 case PROP_TRANSLATION_Y:
383 filter->ytranslation = g_value_get_float (value);
385 case PROP_TRANSLATION_Z:
386 filter->ztranslation = g_value_get_float (value);
388 case PROP_ROTATION_X:
389 filter->xrotation = g_value_get_float (value);
391 case PROP_ROTATION_Y:
392 filter->yrotation = g_value_get_float (value);
394 case PROP_ROTATION_Z:
395 filter->zrotation = g_value_get_float (value);
398 filter->xscale = g_value_get_float (value);
401 filter->yscale = g_value_get_float (value);
404 filter->xpivot = g_value_get_float (value);
407 filter->ypivot = g_value_get_float (value);
410 filter->zpivot = g_value_get_float (value);
413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416 gst_gl_transformation_build_mvp (filter);
420 gst_gl_transformation_get_property (GObject * object, guint prop_id,
421 GValue * value, GParamSpec * pspec)
423 GstGLTransformation *filter = GST_GL_TRANSFORMATION (object);
427 g_value_set_float (value, filter->fov);
430 g_value_set_boolean (value, filter->ortho);
432 case PROP_TRANSLATION_X:
433 g_value_set_float (value, filter->xtranslation);
435 case PROP_TRANSLATION_Y:
436 g_value_set_float (value, filter->ytranslation);
438 case PROP_TRANSLATION_Z:
439 g_value_set_float (value, filter->ztranslation);
441 case PROP_ROTATION_X:
442 g_value_set_float (value, filter->xrotation);
444 case PROP_ROTATION_Y:
445 g_value_set_float (value, filter->yrotation);
447 case PROP_ROTATION_Z:
448 g_value_set_float (value, filter->zrotation);
451 g_value_set_float (value, filter->xscale);
454 g_value_set_float (value, filter->yscale);
457 g_value_set_float (value, filter->xpivot);
460 g_value_set_float (value, filter->ypivot);
463 g_value_set_float (value, filter->zpivot);
466 /* FIXME: need to decompose this to support navigation events */
467 g_value_set_boxed (value, (gconstpointer) & filter->mvp_matrix);
470 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
476 gst_gl_transformation_set_caps (GstGLFilter * filter, GstCaps * incaps,
479 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (filter);
481 transformation->aspect =
482 (gdouble) GST_VIDEO_INFO_WIDTH (&filter->out_info) /
483 (gdouble) GST_VIDEO_INFO_HEIGHT (&filter->out_info);
485 transformation->caps_change = TRUE;
487 gst_gl_transformation_build_mvp (transformation);
493 _intersect_plane_and_ray (graphene_plane_t * video_plane, graphene_ray_t * ray,
494 graphene_point3d_t * result)
496 float t = graphene_ray_get_distance_to_plane (ray, video_plane);
497 GST_TRACE ("Calculated a distance of %f to the plane", t);
498 graphene_ray_get_position_at (ray, t, result);
502 _screen_coord_to_world_ray (GstGLTransformation * transformation, float x,
503 float y, graphene_ray_t * ray)
505 GstGLFilter *filter = GST_GL_FILTER (transformation);
506 gfloat w = (gfloat) GST_VIDEO_INFO_WIDTH (&filter->in_info);
507 gfloat h = (gfloat) GST_VIDEO_INFO_HEIGHT (&filter->in_info);
508 graphene_vec3_t ray_eye_vec3, ray_world_dir, *ray_origin, *ray_direction;
509 graphene_vec3_t ray_ortho_dir;
510 graphene_point3d_t ray_clip, ray_eye;
511 graphene_vec2_t screen_coord;
513 /* GL is y-flipped. i.e. 0, 0 is the bottom left corner in screen space */
514 graphene_vec2_init (&screen_coord, (2. * x / w - 1.) / transformation->aspect,
517 graphene_point3d_init (&ray_clip, graphene_vec2_get_x (&screen_coord),
518 graphene_vec2_get_y (&screen_coord), -1.);
519 graphene_matrix_transform_point3d (&transformation->inv_projection_matrix,
520 &ray_clip, &ray_eye);
522 graphene_vec3_init (&ray_eye_vec3, ray_eye.x, ray_eye.y, -1.);
524 if (transformation->ortho) {
525 graphene_vec3_init (&ray_ortho_dir, 0., 0., 1.);
527 ray_origin = &ray_eye_vec3;
528 ray_direction = &ray_ortho_dir;
530 graphene_matrix_transform_vec3 (&transformation->inv_view_matrix,
531 &ray_eye_vec3, &ray_world_dir);
532 graphene_vec3_normalize (&ray_world_dir, &ray_world_dir);
534 ray_origin = &transformation->camera_position;
535 ray_direction = &ray_world_dir;
538 graphene_ray_init_from_vec3 (ray, ray_origin, ray_direction);
540 GST_TRACE_OBJECT (transformation, "Calculated ray origin: " VEC3_FORMAT
541 " direction: " VEC3_FORMAT " from screen coordinates: " VEC2_FORMAT
542 " with %s projection",
543 VEC3_ARGS (ray_origin), VEC3_ARGS (ray_direction),
544 VEC2_ARGS (&screen_coord),
545 transformation->ortho ? "ortho" : "perspection");
549 _init_world_video_plane (GstGLTransformation * transformation,
550 graphene_plane_t * video_plane)
552 graphene_point3d_t bottom_left, bottom_right, top_left, top_right;
553 graphene_point3d_t world_bottom_left, world_bottom_right;
554 graphene_point3d_t world_top_left, world_top_right;
556 graphene_point3d_init (&top_left, -transformation->aspect, 1., 0.);
557 graphene_point3d_init (&top_right, transformation->aspect, 1., 0.);
558 graphene_point3d_init (&bottom_left, -transformation->aspect, -1., 0.);
559 graphene_point3d_init (&bottom_right, transformation->aspect, -1., 0.);
561 graphene_matrix_transform_point3d (&transformation->model_matrix,
562 &bottom_left, &world_bottom_left);
563 graphene_matrix_transform_point3d (&transformation->model_matrix,
564 &bottom_right, &world_bottom_right);
565 graphene_matrix_transform_point3d (&transformation->model_matrix,
566 &top_left, &world_top_left);
567 graphene_matrix_transform_point3d (&transformation->model_matrix,
568 &top_right, &world_top_right);
570 graphene_plane_init_from_points (video_plane, &world_bottom_left,
571 &world_top_right, &world_top_left);
575 _screen_coord_to_model_coord (GstGLTransformation * transformation,
576 double x, double y, double *res_x, double *res_y)
578 GstGLFilter *filter = GST_GL_FILTER (transformation);
579 double w = (double) GST_VIDEO_INFO_WIDTH (&filter->in_info);
580 double h = (double) GST_VIDEO_INFO_HEIGHT (&filter->in_info);
581 graphene_point3d_t world_point, model_coord;
582 graphene_plane_t video_plane;
586 _init_world_video_plane (transformation, &video_plane);
587 _screen_coord_to_world_ray (transformation, x, y, &ray);
588 _intersect_plane_and_ray (&video_plane, &ray, &world_point);
589 graphene_matrix_transform_point3d (&transformation->inv_model_matrix,
590 &world_point, &model_coord);
592 /* ndc to pixels. We render the frame Y-flipped so need to unflip the
594 new_x = (model_coord.x + 1.) * w / 2;
595 new_y = (1. - model_coord.y) * h / 2;
597 if (new_x < 0. || new_x > w || new_y < 0. || new_y > h)
598 /* coords off video surface */
601 GST_DEBUG_OBJECT (transformation, "converted %f,%f to %f,%f", x, y, new_x,
613 /* debugging facilities for transforming vertices from model space to screen
616 _ndc_to_viewport (GstGLTransformation * transformation, graphene_vec3_t * ndc,
617 int x, int y, int w, int h, float near, float far, graphene_vec3_t * result)
619 GstGLFilter *filter = GST_GL_FILTER (transformation);
620 /* center of the viewport */
624 graphene_vec3_init (result, graphene_vec3_get_x (ndc) * w / 2 + o_x,
625 graphene_vec3_get_y (ndc) * h / 2 + o_y,
626 (far - near) * graphene_vec3_get_z (ndc) / 2 + (far + near) / 2);
630 _perspective_division (graphene_vec4_t * clip, graphene_vec3_t * result)
632 float w = graphene_vec4_get_w (clip);
634 graphene_vec3_init (result, graphene_vec4_get_x (clip) / w,
635 graphene_vec4_get_y (clip) / w, graphene_vec4_get_z (clip) / w);
639 _vertex_to_screen_coord (GstGLTransformation * transformation,
640 graphene_vec4_t * vertex, graphene_vec3_t * view)
642 GstGLFilter *filter = GST_GL_FILTER (transformation);
643 gint w = GST_VIDEO_INFO_WIDTH (&filter->in_info);
644 gint h = GST_VIDEO_INFO_HEIGHT (&filter->in_info);
645 graphene_vec4_t clip;
648 graphene_matrix_transform_vec4 (&transformation->mvp_matrix, vertex, &clip);
649 _perspective_division (&clip, &ndc);
650 _ndc_to_viewport (transformation, &ndc, 0, 0, w, h, 0., 1., view);
655 gst_gl_transformation_src_event (GstBaseTransform * trans, GstEvent * event)
657 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (trans);
658 GstStructure *structure;
661 GST_DEBUG_OBJECT (trans, "handling %s event", GST_EVENT_TYPE_NAME (event));
663 switch (GST_EVENT_TYPE (event)) {
664 case GST_EVENT_NAVIGATION:{
667 GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
669 structure = (GstStructure *) gst_event_get_structure (event);
670 if (gst_structure_get_double (structure, "pointer_x", &x) &&
671 gst_structure_get_double (structure, "pointer_y", &y)) {
672 gdouble new_x, new_y;
674 if (!_screen_coord_to_model_coord (transformation, x, y, &new_x,
676 gst_event_unref (event);
680 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE, new_x,
681 "pointer_y", G_TYPE_DOUBLE, new_y, NULL);
689 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);
695 gst_gl_transformation_filter_meta (GstBaseTransform * trans, GstQuery * query,
696 GType api, const GstStructure * params)
698 if (api == GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE)
701 if (api == GST_GL_SYNC_META_API_TYPE)
708 gst_gl_transformation_decide_allocation (GstBaseTransform * trans,
711 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (trans);
713 if (!GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
717 if (gst_query_find_allocation_meta (query,
718 GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE, NULL)) {
719 transformation->downstream_supports_affine_meta = TRUE;
721 transformation->downstream_supports_affine_meta = FALSE;
728 gst_gl_transformation_gl_stop (GstGLBaseFilter * base_filter)
730 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (base_filter);
731 const GstGLFuncs *gl = base_filter->context->gl_vtable;
733 if (transformation->vao) {
734 gl->DeleteVertexArrays (1, &transformation->vao);
735 transformation->vao = 0;
738 if (transformation->vertex_buffer) {
739 gl->DeleteBuffers (1, &transformation->vertex_buffer);
740 transformation->vertex_buffer = 0;
743 if (transformation->vbo_indices) {
744 gl->DeleteBuffers (1, &transformation->vbo_indices);
745 transformation->vbo_indices = 0;
748 if (transformation->shader) {
749 gst_object_unref (transformation->shader);
750 transformation->shader = NULL;
753 GST_GL_BASE_FILTER_CLASS (parent_class)->gl_stop (base_filter);
757 gst_gl_transformation_gl_start (GstGLBaseFilter * base_filter)
759 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (base_filter);
761 if (!GST_GL_BASE_FILTER_CLASS (parent_class)->gl_start (base_filter))
764 if (gst_gl_context_get_gl_api (base_filter->context)) {
765 /* blocking call, wait until the opengl thread has compiled the shader */
766 return gst_gl_context_gen_shader (base_filter->context,
767 gst_gl_shader_string_vertex_mat4_vertex_transform,
768 gst_gl_shader_string_fragment_default, &transformation->shader);
774 gst_gl_transformation_prepare_output_buffer (GstBaseTransform * trans,
775 GstBuffer * inbuf, GstBuffer ** outbuf)
777 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (trans);
778 GstGLFilter *filter = GST_GL_FILTER (trans);
780 if (transformation->downstream_supports_affine_meta &&
781 gst_video_info_is_equal (&filter->in_info, &filter->out_info)) {
782 GstVideoAffineTransformationMeta *af_meta;
783 graphene_matrix_t upstream_matrix, tmp, tmp2, inv_aspect, yflip;
784 float upstream[16], downstream[16];
786 *outbuf = gst_buffer_make_writable (inbuf);
788 af_meta = gst_buffer_get_video_affine_transformation_meta (inbuf);
790 af_meta = gst_buffer_add_video_affine_transformation_meta (*outbuf);
792 GST_LOG_OBJECT (trans, "applying transformation to existing affine "
793 "transformation meta");
795 gst_gl_get_affine_transformation_meta_as_ndc_ext (af_meta, upstream);
797 /* apply the transformation to the existing affine meta */
798 graphene_matrix_init_from_float (&upstream_matrix, upstream);
799 graphene_matrix_init_scale (&inv_aspect, transformation->aspect, -1., 1.);
800 graphene_matrix_init_scale (&yflip, 1., -1., 1.);
802 /* invert the aspect effects */
803 graphene_matrix_multiply (&upstream_matrix, &inv_aspect, &tmp2);
804 /* apply the transformation */
805 graphene_matrix_multiply (&tmp2, &transformation->mvp_matrix, &tmp);
807 graphene_matrix_multiply (&tmp, &yflip, &tmp2);
809 graphene_matrix_to_float (&tmp2, downstream);
810 gst_gl_set_affine_transformation_meta_from_ndc_ext (af_meta, downstream);
815 return GST_BASE_TRANSFORM_CLASS (parent_class)->prepare_output_buffer (trans,
820 gst_gl_transformation_filter (GstGLFilter * filter,
821 GstBuffer * inbuf, GstBuffer * outbuf)
823 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (filter);
825 if (transformation->downstream_supports_affine_meta &&
826 gst_video_info_is_equal (&filter->in_info, &filter->out_info)) {
829 return gst_gl_filter_filter_texture (filter, inbuf, outbuf);
834 gst_gl_transformation_filter_texture (GstGLFilter * filter,
835 GstGLMemory * in_tex, GstGLMemory * out_tex)
837 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (filter);
839 transformation->in_tex = in_tex;
841 gst_gl_framebuffer_draw_to_texture (filter->fbo, out_tex,
842 (GstGLFramebufferFunc) gst_gl_transformation_callback, transformation);
847 static const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
850 _upload_vertices (GstGLTransformation * transformation)
852 const GstGLFuncs *gl =
853 GST_GL_BASE_FILTER (transformation)->context->gl_vtable;
856 GLfloat vertices[] = {
857 -transformation->aspect, -1.0, 0.0, 1.0, 0.0, 0.0,
858 transformation->aspect, -1.0, 0.0, 1.0, 1.0, 0.0,
859 transformation->aspect, 1.0, 0.0, 1.0, 1.0, 1.0,
860 -transformation->aspect, 1.0, 0.0, 1.0, 0.0, 1.0,
864 gl->BindBuffer (GL_ARRAY_BUFFER, transformation->vertex_buffer);
866 gl->BufferData (GL_ARRAY_BUFFER, 4 * 6 * sizeof (GLfloat), vertices,
871 _bind_buffer (GstGLTransformation * transformation)
873 const GstGLFuncs *gl =
874 GST_GL_BASE_FILTER (transformation)->context->gl_vtable;
876 gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, transformation->vbo_indices);
877 gl->BindBuffer (GL_ARRAY_BUFFER, transformation->vertex_buffer);
879 /* Load the vertex position */
880 gl->VertexAttribPointer (transformation->attr_position, 4, GL_FLOAT,
881 GL_FALSE, 6 * sizeof (GLfloat), (void *) 0);
883 /* Load the texture coordinate */
884 gl->VertexAttribPointer (transformation->attr_texture, 2, GL_FLOAT, GL_FALSE,
885 6 * sizeof (GLfloat), (void *) (4 * sizeof (GLfloat)));
887 gl->EnableVertexAttribArray (transformation->attr_position);
888 gl->EnableVertexAttribArray (transformation->attr_texture);
892 _unbind_buffer (GstGLTransformation * transformation)
894 const GstGLFuncs *gl =
895 GST_GL_BASE_FILTER (transformation)->context->gl_vtable;
897 gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
898 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
900 gl->DisableVertexAttribArray (transformation->attr_position);
901 gl->DisableVertexAttribArray (transformation->attr_texture);
905 gst_gl_transformation_callback (gpointer stuff)
907 GstGLFilter *filter = GST_GL_FILTER (stuff);
908 GstGLTransformation *transformation = GST_GL_TRANSFORMATION (filter);
909 GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
911 GLfloat temp_matrix[16];
913 gst_gl_context_clear_shader (GST_GL_BASE_FILTER (filter)->context);
914 gl->BindTexture (GL_TEXTURE_2D, 0);
916 gl->ClearColor (0.f, 0.f, 0.f, 0.f);
917 gl->Clear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
919 gst_gl_shader_use (transformation->shader);
921 gl->ActiveTexture (GL_TEXTURE0);
922 gl->BindTexture (GL_TEXTURE_2D, transformation->in_tex->tex_id);
923 gst_gl_shader_set_uniform_1i (transformation->shader, "texture", 0);
925 graphene_matrix_to_float (&transformation->mvp_matrix, temp_matrix);
926 gst_gl_shader_set_uniform_matrix_4fv (transformation->shader,
927 "u_transformation", 1, GL_FALSE, temp_matrix);
929 if (!transformation->vertex_buffer) {
930 transformation->attr_position =
931 gst_gl_shader_get_attribute_location (transformation->shader,
934 transformation->attr_texture =
935 gst_gl_shader_get_attribute_location (transformation->shader,
938 if (gl->GenVertexArrays) {
939 gl->GenVertexArrays (1, &transformation->vao);
940 gl->BindVertexArray (transformation->vao);
943 gl->GenBuffers (1, &transformation->vertex_buffer);
945 gl->GenBuffers (1, &transformation->vbo_indices);
946 gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, transformation->vbo_indices);
947 gl->BufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (indices), indices,
950 transformation->caps_change = TRUE;
953 if (gl->GenVertexArrays)
954 gl->BindVertexArray (transformation->vao);
956 if (transformation->caps_change)
957 _upload_vertices (transformation);
958 _bind_buffer (transformation);
960 gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
962 if (gl->GenVertexArrays)
963 gl->BindVertexArray (0);
964 _unbind_buffer (transformation);
966 gst_gl_context_clear_shader (GST_GL_BASE_FILTER (filter)->context);
967 transformation->caps_change = FALSE;