• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  * SECTION:element-glvideo_flip
23  * @title: glvideo_flip
24  *
25  * Transforms video on the GPU.
26  *
27  * ## Examples
28  * |[
29  * gst-launch-1.0 videotestsrc ! glupload ! glvideoflip method=clockwise ! glimagesinkelement
30  * ]| This pipeline flips the test image 90 degrees clockwise.
31  *
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 #include "gstglelements.h"
39 #include "gstglvideoflip.h"
40 
41 #define GST_CAT_DEFAULT gst_gl_video_flip_debug
42 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
43 
44 #define DEFAULT_METHOD GST_VIDEO_ORIENTATION_IDENTITY
45 
46 enum
47 {
48   PROP_0,
49   PROP_METHOD,
50   PROP_VIDEO_DIRECTION
51 };
52 
53 static GstStaticPadTemplate _sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), "
57         "format = (string) RGBA, "
58         "width = " GST_VIDEO_SIZE_RANGE ", "
59         "height = " GST_VIDEO_SIZE_RANGE ", "
60         "framerate = " GST_VIDEO_FPS_RANGE ", "
61         "texture-target = (string) 2D"));
62 
63 static GstStaticPadTemplate _src_template = GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), "
67         "format = (string) RGBA, "
68         "width = " GST_VIDEO_SIZE_RANGE ", "
69         "height = " GST_VIDEO_SIZE_RANGE ", "
70         "framerate = " GST_VIDEO_FPS_RANGE ", "
71         "texture-target = (string) 2D"));
72 
73 #define GST_TYPE_GL_VIDEO_FLIP_METHOD (gst_video_flip_method_get_type())
74 static const GEnumValue video_flip_methods[] = {
75   {GST_VIDEO_ORIENTATION_IDENTITY, "Identity (no rotation)", "none"},
76   {GST_VIDEO_ORIENTATION_90R, "Rotate clockwise 90 degrees", "clockwise"},
77   {GST_VIDEO_ORIENTATION_180, "Rotate 180 degrees", "rotate-180"},
78   {GST_VIDEO_ORIENTATION_90L, "Rotate counter-clockwise 90 degrees",
79       "counterclockwise"},
80   {GST_VIDEO_ORIENTATION_HORIZ, "Flip horizontally", "horizontal-flip"},
81   {GST_VIDEO_ORIENTATION_VERT, "Flip vertically", "vertical-flip"},
82   {GST_VIDEO_ORIENTATION_UL_LR,
83       "Flip across upper left/lower right diagonal", "upper-left-diagonal"},
84   {GST_VIDEO_ORIENTATION_UR_LL,
85       "Flip across upper right/lower left diagonal", "upper-right-diagonal"},
86   {GST_VIDEO_ORIENTATION_AUTO,
87       "Select flip method based on image-orientation tag", "automatic"},
88   {0, NULL, NULL},
89 };
90 
91 static GType
gst_video_flip_method_get_type(void)92 gst_video_flip_method_get_type (void)
93 {
94   static GType video_flip_method_type = 0;
95 
96   if (!video_flip_method_type) {
97     video_flip_method_type = g_enum_register_static ("GstGLVideoFlipMethod",
98         video_flip_methods);
99   }
100   return video_flip_method_type;
101 }
102 
103 static void gst_gl_video_flip_finalize (GObject * object);
104 static void gst_gl_video_flip_set_property (GObject * object, guint prop_id,
105     const GValue * value, GParamSpec * pspec);
106 static void gst_gl_video_flip_get_property (GObject * object, guint prop_id,
107     GValue * value, GParamSpec * pspec);
108 
109 static GstPadProbeReturn _input_sink_probe (GstPad * pad,
110     GstPadProbeInfo * info, gpointer user_data);
111 static GstPadProbeReturn _trans_src_probe (GstPad * pad, GstPadProbeInfo * info,
112     gpointer user_data);
113 
114 static void
115 gst_gl_video_flip_video_direction_interface_init (GstVideoDirectionInterface
116     * iface);
117 
118 #define gst_gl_video_flip_parent_class parent_class
119 G_DEFINE_TYPE_WITH_CODE (GstGLVideoFlip, gst_gl_video_flip,
120     GST_TYPE_BIN, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
121         "glvideoflip", 0, "glvideoflip element");
122     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_DIRECTION,
123         gst_gl_video_flip_video_direction_interface_init););
124 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (glvideoflip, "glvideoflip",
125     GST_RANK_NONE, GST_TYPE_GL_VIDEO_FLIP, gl_element_init (plugin));
126 
127 static void
gst_gl_video_flip_video_direction_interface_init(GstVideoDirectionInterface * iface)128 gst_gl_video_flip_video_direction_interface_init (GstVideoDirectionInterface
129     * iface)
130 {
131   /* We implement the video-direction property */
132 }
133 
134 static void
gst_gl_video_flip_class_init(GstGLVideoFlipClass * klass)135 gst_gl_video_flip_class_init (GstGLVideoFlipClass * klass)
136 {
137   GObjectClass *gobject_class;
138   GstElementClass *element_class;
139 
140   gobject_class = (GObjectClass *) klass;
141   element_class = GST_ELEMENT_CLASS (klass);
142 
143   gobject_class->finalize = gst_gl_video_flip_finalize;
144   gobject_class->set_property = gst_gl_video_flip_set_property;
145   gobject_class->get_property = gst_gl_video_flip_get_property;
146 
147   g_object_class_install_property (gobject_class, PROP_METHOD,
148       g_param_spec_enum ("method", "method",
149           "method (deprecated, use video-direction instead)",
150           GST_TYPE_GL_VIDEO_FLIP_METHOD, DEFAULT_METHOD,
151           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
152           G_PARAM_STATIC_STRINGS));
153   g_object_class_override_property (gobject_class, PROP_VIDEO_DIRECTION,
154       "video-direction");
155 
156   gst_element_class_add_static_pad_template (element_class, &_src_template);
157   gst_element_class_add_static_pad_template (element_class, &_sink_template);
158 
159   gst_element_class_set_metadata (element_class, "OpenGL video flip filter",
160       "Filter/Effect/Video", "Flip video on the GPU",
161       "Matthew Waters <matthew@centricular.com>");
162 
163   gst_type_mark_as_plugin_api (GST_TYPE_GL_VIDEO_FLIP_METHOD, 0);
164 }
165 
166 static void
gst_gl_video_flip_init(GstGLVideoFlip * flip)167 gst_gl_video_flip_init (GstGLVideoFlip * flip)
168 {
169   gboolean res = TRUE;
170   GstPad *pad;
171 
172   flip->aspect = 1.0;
173 
174   flip->input_capsfilter = gst_element_factory_make ("capsfilter", NULL);
175   res &= gst_bin_add (GST_BIN (flip), flip->input_capsfilter);
176 
177   flip->transformation = gst_element_factory_make ("gltransformation", NULL);
178   g_object_set (flip->transformation, "ortho", TRUE, NULL);
179   res &= gst_bin_add (GST_BIN (flip), flip->transformation);
180 
181   flip->output_capsfilter = gst_element_factory_make ("capsfilter", NULL);
182   res &= gst_bin_add (GST_BIN (flip), flip->output_capsfilter);
183 
184   res &=
185       gst_element_link_pads (flip->input_capsfilter, "src",
186       flip->transformation, "sink");
187   res &=
188       gst_element_link_pads (flip->transformation, "src",
189       flip->output_capsfilter, "sink");
190 
191   pad = gst_element_get_static_pad (flip->input_capsfilter, "sink");
192   if (!pad) {
193     res = FALSE;
194   } else {
195     GST_DEBUG_OBJECT (flip, "setting target sink pad %" GST_PTR_FORMAT, pad);
196     flip->sinkpad = gst_ghost_pad_new ("sink", pad);
197     flip->sink_probe = gst_pad_add_probe (flip->sinkpad,
198         GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
199         GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM,
200         (GstPadProbeCallback) _input_sink_probe, flip, NULL);
201     gst_element_add_pad (GST_ELEMENT_CAST (flip), flip->sinkpad);
202     gst_object_unref (pad);
203   }
204 
205   pad = gst_element_get_static_pad (flip->transformation, "src");
206   flip->src_probe = gst_pad_add_probe (pad,
207       GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM,
208       (GstPadProbeCallback) _trans_src_probe, flip, NULL);
209   gst_object_unref (pad);
210 
211   pad = gst_element_get_static_pad (flip->output_capsfilter, "src");
212   if (!pad) {
213     res = FALSE;
214   } else {
215     GST_DEBUG_OBJECT (flip, "setting target sink pad %" GST_PTR_FORMAT, pad);
216     flip->srcpad = gst_ghost_pad_new ("src", pad);
217     gst_element_add_pad (GST_ELEMENT_CAST (flip), flip->srcpad);
218     gst_object_unref (pad);
219   }
220 
221   if (!res) {
222     GST_WARNING_OBJECT (flip, "Failed to add/connect the necessary machinery");
223   }
224 }
225 
226 static void
gst_gl_video_flip_finalize(GObject * object)227 gst_gl_video_flip_finalize (GObject * object)
228 {
229   GstGLVideoFlip *flip = GST_GL_VIDEO_FLIP (object);
230 
231   gst_caps_replace (&flip->input_caps, NULL);
232 
233   G_OBJECT_CLASS (parent_class)->finalize (object);
234 }
235 
236 /* Caps negotiation happens like this:
237  *
238  * 1. caps/accept-caps queries bypass the capsfilters on either side of the
239  *    transformation element so the fixed caps don't get in the way.
240  * 2. Receiving a caps event on the sink pad will set fixed caps on either side
241  *    of the transformation element.
242  */
243 static GstCaps *
_transform_caps(GstGLVideoFlip * vf,GstPadDirection direction,GstCaps * caps)244 _transform_caps (GstGLVideoFlip * vf, GstPadDirection direction, GstCaps * caps)
245 {
246   GstCaps *output = gst_caps_copy (caps);
247   gint i;
248 
249   for (i = 0; i < gst_caps_get_size (output); i++) {
250     GstStructure *structure = gst_caps_get_structure (output, i);
251     gint width, height;
252     gint par_n, par_d;
253 
254     if (gst_structure_get_int (structure, "width", &width) &&
255         gst_structure_get_int (structure, "height", &height)) {
256 
257       switch (vf->active_method) {
258         case GST_VIDEO_ORIENTATION_90R:
259         case GST_VIDEO_ORIENTATION_90L:
260         case GST_VIDEO_ORIENTATION_UL_LR:
261         case GST_VIDEO_ORIENTATION_UR_LL:
262           gst_structure_set (structure, "width", G_TYPE_INT, height,
263               "height", G_TYPE_INT, width, NULL);
264           if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
265                   &par_n, &par_d)) {
266             if (par_n != 1 || par_d != 1) {
267               GValue val = { 0, };
268 
269               g_value_init (&val, GST_TYPE_FRACTION);
270               gst_value_set_fraction (&val, par_d, par_n);
271               gst_structure_set_value (structure, "pixel-aspect-ratio", &val);
272               g_value_unset (&val);
273             }
274           }
275           break;
276         case GST_VIDEO_ORIENTATION_IDENTITY:
277         case GST_VIDEO_ORIENTATION_180:
278         case GST_VIDEO_ORIENTATION_HORIZ:
279         case GST_VIDEO_ORIENTATION_VERT:
280           break;
281         default:
282           g_assert_not_reached ();
283           break;
284       }
285     }
286   }
287 
288   return output;
289 }
290 
291 /* with object lock */
292 static void
_set_active_method(GstGLVideoFlip * vf,GstVideoOrientationMethod method,GstCaps * caps)293 _set_active_method (GstGLVideoFlip * vf, GstVideoOrientationMethod method,
294     GstCaps * caps)
295 {
296   gfloat rot_z = 0., scale_x = 1.0, scale_y = 1.0;
297   GstCaps *output_caps, *templ;
298   GstPad *srcpad;
299 
300   switch (method) {
301     case GST_VIDEO_ORIENTATION_IDENTITY:
302       break;
303     case GST_VIDEO_ORIENTATION_90R:
304       scale_x *= vf->aspect;
305       scale_y *= 1. / vf->aspect;
306       rot_z = 90.;
307       break;
308     case GST_VIDEO_ORIENTATION_180:
309       rot_z = 180.;
310       break;
311     case GST_VIDEO_ORIENTATION_90L:
312       scale_x *= vf->aspect;
313       scale_y *= 1. / vf->aspect;
314       rot_z = 270.;
315       break;
316     case GST_VIDEO_ORIENTATION_HORIZ:
317       scale_x *= -1.;
318       break;
319     case GST_VIDEO_ORIENTATION_UR_LL:
320       scale_x *= -vf->aspect;
321       scale_y *= 1. / vf->aspect;
322       rot_z = 90.;
323       break;
324     case GST_VIDEO_ORIENTATION_VERT:
325       scale_x *= -1.;
326       rot_z = 180.;
327       break;
328     case GST_VIDEO_ORIENTATION_UL_LR:
329       scale_x *= -vf->aspect;
330       scale_y *= 1. / vf->aspect;
331       rot_z = 270.;
332       break;
333     default:
334       break;
335   }
336   vf->active_method = method;
337 
338   output_caps = _transform_caps (vf, GST_PAD_SINK, caps);
339   gst_caps_replace (&vf->input_caps, caps);
340 
341   srcpad = gst_element_get_static_pad (vf->transformation, "src");
342   templ = gst_pad_get_pad_template_caps (srcpad);
343   gst_object_unref (srcpad);
344 
345   gst_caps_append (output_caps, gst_caps_ref (templ));
346   GST_OBJECT_UNLOCK (vf);
347 
348   g_object_set (vf->input_capsfilter, "caps", gst_caps_ref (caps), NULL);
349   g_object_set (vf->output_capsfilter, "caps", output_caps, NULL);
350   g_object_set (vf->transformation, "rotation-z", rot_z, "scale-x", scale_x,
351       "scale-y", scale_y, NULL);
352   GST_OBJECT_LOCK (vf);
353 }
354 
355 static void
gst_gl_video_flip_set_method(GstGLVideoFlip * vf,GstVideoOrientationMethod method,gboolean from_tag)356 gst_gl_video_flip_set_method (GstGLVideoFlip * vf,
357     GstVideoOrientationMethod method, gboolean from_tag)
358 {
359   GST_OBJECT_LOCK (vf);
360 
361   if (method == GST_VIDEO_ORIENTATION_CUSTOM) {
362     GST_WARNING_OBJECT (vf, "unsupported custom orientation");
363     GST_OBJECT_UNLOCK (vf);
364     return;
365   }
366 
367   /* Store updated method */
368   if (from_tag)
369     vf->tag_method = method;
370   else
371     vf->method = method;
372 
373   /* Get the new method */
374   if (vf->method == GST_VIDEO_ORIENTATION_AUTO)
375     method = vf->tag_method;
376   else
377     method = vf->method;
378 
379   if (vf->input_caps)
380     _set_active_method (vf, method, vf->input_caps);
381   else {
382     /* just store the configured method here. The actual transform configuration
383      * will be done once caps are configured. See caps handling in
384      * _input_sink_probe. */
385     vf->active_method = method;
386   }
387 
388   GST_OBJECT_UNLOCK (vf);
389 }
390 
391 static void
gst_gl_video_flip_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)392 gst_gl_video_flip_set_property (GObject * object, guint prop_id,
393     const GValue * value, GParamSpec * pspec)
394 {
395   GstGLVideoFlip *vf = GST_GL_VIDEO_FLIP (object);
396 
397   switch (prop_id) {
398     case PROP_METHOD:
399     case PROP_VIDEO_DIRECTION:
400       gst_gl_video_flip_set_method (vf, g_value_get_enum (value), FALSE);
401       break;
402     default:
403       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
404       break;
405   }
406 }
407 
408 static void
gst_gl_video_flip_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)409 gst_gl_video_flip_get_property (GObject * object, guint prop_id,
410     GValue * value, GParamSpec * pspec)
411 {
412   GstGLVideoFlip *vf = GST_GL_VIDEO_FLIP (object);
413 
414   switch (prop_id) {
415     case PROP_METHOD:
416     case PROP_VIDEO_DIRECTION:
417       g_value_set_enum (value, vf->method);
418       break;
419     default:
420       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
421       break;
422   }
423 }
424 
425 static GstPadProbeReturn
_input_sink_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)426 _input_sink_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
427 {
428   GstGLVideoFlip *vf = GST_GL_VIDEO_FLIP (user_data);
429 
430   if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM) {
431     GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
432 
433     switch (GST_EVENT_TYPE (event)) {
434       case GST_EVENT_TAG:{
435         GstTagList *taglist;
436         GstVideoOrientationMethod method;
437 
438         gst_event_parse_tag (event, &taglist);
439 
440         if (gst_video_orientation_from_tag (taglist, &method))
441           gst_gl_video_flip_set_method (vf, method, TRUE);
442         break;
443       }
444       case GST_EVENT_CAPS:{
445         GstCaps *caps;
446         GstVideoInfo v_info;
447 
448         gst_event_parse_caps (event, &caps);
449         GST_OBJECT_LOCK (vf);
450         if (gst_video_info_from_caps (&v_info, caps))
451           vf->aspect =
452               (gfloat) GST_VIDEO_INFO_WIDTH (&v_info) /
453               (gfloat) GST_VIDEO_INFO_HEIGHT (&v_info);
454         else
455           vf->aspect = 1.0;
456         _set_active_method (vf, vf->active_method, caps);
457         GST_OBJECT_UNLOCK (vf);
458         break;
459       }
460       default:
461         break;
462     }
463   } else if (GST_PAD_PROBE_INFO_TYPE (info) &
464       GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) {
465     GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
466 
467     switch (GST_QUERY_TYPE (query)) {
468         /* bypass the capsfilter */
469       case GST_QUERY_CAPS:
470       case GST_QUERY_ACCEPT_CAPS:{
471         GstPad *pad = gst_element_get_static_pad (vf->transformation, "sink");
472         if (gst_pad_query (pad, query)) {
473           gst_object_unref (pad);
474           return GST_PAD_PROBE_HANDLED;
475         } else {
476           gst_object_unref (pad);
477           return GST_PAD_PROBE_DROP;
478         }
479       }
480       default:
481         break;
482     }
483   }
484 
485   return GST_PAD_PROBE_OK;
486 }
487 
488 static GstPadProbeReturn
_trans_src_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)489 _trans_src_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
490 {
491   GstGLVideoFlip *vf = GST_GL_VIDEO_FLIP (user_data);
492 
493   if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) {
494     GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
495 
496     switch (GST_QUERY_TYPE (query)) {
497         /* bypass the capsfilter */
498       case GST_QUERY_CAPS:
499       case GST_QUERY_ACCEPT_CAPS:{
500         if (gst_pad_peer_query (vf->srcpad, query))
501           return GST_PAD_PROBE_HANDLED;
502         else
503           return GST_PAD_PROBE_DROP;
504       }
505       default:
506         break;
507     }
508   }
509 
510   return GST_PAD_PROBE_OK;
511 }
512