1 /*
2  * GStreamer
3  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
4  * Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Alternatively, the contents of this file may be used under the
25  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
26  * which case the following provisions apply instead of the ones
27  * mentioned above:
28  *
29  * This library is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Library General Public
31  * License as published by the Free Software Foundation; either
32  * version 2 of the License, or (at your option) any later version.
33  *
34  * This library is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37  * Library General Public License for more details.
38  *
39  * You should have received a copy of the GNU Library General Public
40  * License along with this library; if not, write to the
41  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
42  * Boston, MA 02110-1301, USA.
43  */
44 
45 /*
46  * Perspective matrix multiplication taken from:
47  * http://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/PerspectiveTransform.html
48  */
49 
50 /**
51  * SECTION:element-perspective
52  * @title: perspective
53  * @see_also: geometrictransform
54  *
55  * The perspective element applies a 2D perspective transform.
56  *
57  * ## Example launch line
58  * |[
59  * gst-launch-1.0 -v videotestsrc ! perspective ! videoconvert ! autovideosink
60  * ]|
61  *
62  */
63 
64 /* FIXME: suppress warnings for deprecated API such as GValueArray
65  * with newer GLib versions (>= 2.31.0)
66  *
67  * It is just not possible to switch to GArray yet because the python API in
68  * 1.2.0 still passes iterable properties as GValueArray */
69 #define GLIB_DISABLE_DEPRECATION_WARNINGS
70 
71 #ifdef HAVE_CONFIG_H
72 #  include <config.h>
73 #endif
74 
75 #include <gst/gst.h>
76 
77 #include "gstperspective.h"
78 
79 GST_DEBUG_CATEGORY_STATIC (gst_perspective_debug);
80 #define GST_CAT_DEFAULT gst_perspective_debug
81 
82 enum
83 {
84   PROP_0,
85   PROP_MATRIX
86 };
87 
88 
89 #define gst_perspective_parent_class parent_class
90 G_DEFINE_TYPE (GstPerspective, gst_perspective, GST_TYPE_GEOMETRIC_TRANSFORM);
91 
92 static GValueArray *
get_array_from_matrix(GstPerspective * self)93 get_array_from_matrix (GstPerspective * self)
94 {
95   GValue v = { 0, };
96   GValueArray *va;
97   int i;
98 
99   va = g_value_array_new (1);
100 
101   for (i = 0; i < 9; i++) {
102     g_value_init (&v, G_TYPE_DOUBLE);
103     g_value_set_double (&v, self->matrix[i]);
104     g_value_array_append (va, &v);
105     g_value_unset (&v);
106   }
107 
108   return va;
109 }
110 
111 static gboolean
set_matrix_from_array(GstPerspective * self,GValueArray * va)112 set_matrix_from_array (GstPerspective * self, GValueArray * va)
113 {
114   guint i;
115 
116   if (!va) {
117     GST_WARNING ("Invalid parameter");
118     return FALSE;
119   }
120 
121   if (va->n_values != 9) {
122     GST_WARNING ("Invalid number of elements: %d", va->n_values);
123     return FALSE;
124   }
125 
126   for (i = 0; i < va->n_values; i++) {
127     GValue *v = g_value_array_get_nth (va, i);
128     self->matrix[i] = g_value_get_double (v);
129   }
130 
131   return TRUE;
132 }
133 
134 static void
gst_perspective_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)135 gst_perspective_set_property (GObject * object, guint prop_id,
136     const GValue * value, GParamSpec * pspec)
137 {
138   GstPerspective *perspective;
139   GstGeometricTransform *gt;
140   gboolean matrix_ok;
141 
142   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
143   perspective = GST_PERSPECTIVE_CAST (object);
144 
145   GST_OBJECT_LOCK (perspective);
146   switch (prop_id) {
147     case PROP_MATRIX:
148       matrix_ok =
149           set_matrix_from_array (perspective, g_value_get_boxed (value));
150       if (matrix_ok) {
151         gst_geometric_transform_set_need_remap (gt);
152       }
153       break;
154     default:
155       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156       break;
157   }
158   GST_OBJECT_UNLOCK (perspective);
159 }
160 
161 static void
gst_perspective_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)162 gst_perspective_get_property (GObject * object, guint prop_id,
163     GValue * value, GParamSpec * pspec)
164 {
165   GstPerspective *perspective;
166 
167   perspective = GST_PERSPECTIVE_CAST (object);
168 
169   switch (prop_id) {
170     case PROP_MATRIX:
171       g_value_set_boxed (value, get_array_from_matrix (perspective));
172       break;
173     default:
174       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175       break;
176   }
177 }
178 
179 static gboolean
perspective_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)180 perspective_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
181     gdouble * in_y)
182 {
183   GstPerspective *perspective = GST_PERSPECTIVE_CAST (gt);
184   gdouble *m;
185   gdouble xp, yp, w, xi, yi;
186 
187   m = perspective->matrix;
188 
189   /* Matrix multiplication */
190   xp = (m[0] * x + m[1] * y + m[2]);
191   yp = (m[3] * x + m[4] * y + m[5]);
192   w = (m[6] * x + m[7] * y + m[8]);
193 
194   /* Perspective division */
195   xi = xp / w;
196   yi = yp / w;
197 
198   /* return values to caller */
199   *in_x = xi;
200   *in_y = yi;
201 
202   GST_DEBUG_OBJECT (perspective, "Inversely mapped %d %d into %lf %lf",
203       x, y, *in_x, *in_y);
204 
205   return TRUE;
206 }
207 
208 static void
gst_perspective_class_init(GstPerspectiveClass * klass)209 gst_perspective_class_init (GstPerspectiveClass * klass)
210 {
211   GObjectClass *gobject_class;
212   GstElementClass *gstelement_class;
213   GstGeometricTransformClass *gstgt_class;
214 
215   gobject_class = (GObjectClass *) klass;
216   gstelement_class = (GstElementClass *) klass;
217   gstgt_class = (GstGeometricTransformClass *) klass;
218 
219   gst_element_class_set_static_metadata (gstelement_class,
220       "perspective",
221       "Transform/Effect/Video",
222       "Apply a 2D perspective transform",
223       "Antonio Ospite <ospite@studenti.unina.it>");
224 
225   gobject_class->set_property = gst_perspective_set_property;
226   gobject_class->get_property = gst_perspective_get_property;
227 
228   g_object_class_install_property (gobject_class, PROP_MATRIX,
229       g_param_spec_value_array ("matrix",
230           "Matrix",
231           "Matrix of dimension 3x3 to use in the 2D transform, passed as an array of 9 elements in row-major order",
232           g_param_spec_double ("Element",
233               "Transformation matrix element",
234               "Element of the transformation matrix",
235               -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
236               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
237           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 
239   gstgt_class->map_func = perspective_map;
240 }
241 
242 static void
gst_perspective_init(GstPerspective * filter)243 gst_perspective_init (GstPerspective * filter)
244 {
245   /* set the Identity matrix as default */
246   filter->matrix[0] = 1;
247   filter->matrix[1] = 0;
248   filter->matrix[2] = 0;
249 
250   filter->matrix[3] = 0;
251   filter->matrix[4] = 1;
252   filter->matrix[5] = 0;
253 
254   filter->matrix[6] = 0;
255   filter->matrix[7] = 0;
256   filter->matrix[8] = 1;
257 }
258 
259 gboolean
gst_perspective_plugin_init(GstPlugin * plugin)260 gst_perspective_plugin_init (GstPlugin * plugin)
261 {
262   GST_DEBUG_CATEGORY_INIT (gst_perspective_debug, "perspective", 0,
263       "perspective");
264 
265   return gst_element_register (plugin, "perspective", GST_RANK_NONE,
266       GST_TYPE_PERSPECTIVE);
267 }
268