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 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (perspective, "perspective",
92 GST_RANK_NONE, GST_TYPE_PERSPECTIVE,
93 GST_DEBUG_CATEGORY_INIT (gst_perspective_debug, "perspective", 0,
94 "perspective"));
95
96 static GValueArray *
get_array_from_matrix(GstPerspective * self)97 get_array_from_matrix (GstPerspective * self)
98 {
99 GValue v = { 0, };
100 GValueArray *va;
101 int i;
102
103 va = g_value_array_new (1);
104
105 for (i = 0; i < 9; i++) {
106 g_value_init (&v, G_TYPE_DOUBLE);
107 g_value_set_double (&v, self->matrix[i]);
108 g_value_array_append (va, &v);
109 g_value_unset (&v);
110 }
111
112 return va;
113 }
114
115 static gboolean
set_matrix_from_array(GstPerspective * self,GValueArray * va)116 set_matrix_from_array (GstPerspective * self, GValueArray * va)
117 {
118 guint i;
119
120 if (!va) {
121 GST_WARNING ("Invalid parameter");
122 return FALSE;
123 }
124
125 if (va->n_values != 9) {
126 GST_WARNING ("Invalid number of elements: %d", va->n_values);
127 return FALSE;
128 }
129
130 for (i = 0; i < va->n_values; i++) {
131 GValue *v = g_value_array_get_nth (va, i);
132 self->matrix[i] = g_value_get_double (v);
133 }
134
135 return TRUE;
136 }
137
138 static void
gst_perspective_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)139 gst_perspective_set_property (GObject * object, guint prop_id,
140 const GValue * value, GParamSpec * pspec)
141 {
142 GstPerspective *perspective;
143 GstGeometricTransform *gt;
144 gboolean matrix_ok;
145
146 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
147 perspective = GST_PERSPECTIVE_CAST (object);
148
149 GST_OBJECT_LOCK (perspective);
150 switch (prop_id) {
151 case PROP_MATRIX:
152 matrix_ok =
153 set_matrix_from_array (perspective, g_value_get_boxed (value));
154 if (matrix_ok) {
155 gst_geometric_transform_set_need_remap (gt);
156 }
157 break;
158 default:
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 break;
161 }
162 GST_OBJECT_UNLOCK (perspective);
163 }
164
165 static void
gst_perspective_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)166 gst_perspective_get_property (GObject * object, guint prop_id,
167 GValue * value, GParamSpec * pspec)
168 {
169 GstPerspective *perspective;
170
171 perspective = GST_PERSPECTIVE_CAST (object);
172
173 switch (prop_id) {
174 case PROP_MATRIX:
175 g_value_set_boxed (value, get_array_from_matrix (perspective));
176 break;
177 default:
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 break;
180 }
181 }
182
183 static gboolean
perspective_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)184 perspective_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
185 gdouble * in_y)
186 {
187 GstPerspective *perspective = GST_PERSPECTIVE_CAST (gt);
188 gdouble *m;
189 gdouble xp, yp, w, xi, yi;
190
191 m = perspective->matrix;
192
193 /* Matrix multiplication */
194 xp = (m[0] * x + m[1] * y + m[2]);
195 yp = (m[3] * x + m[4] * y + m[5]);
196 w = (m[6] * x + m[7] * y + m[8]);
197
198 /* Perspective division */
199 xi = xp / w;
200 yi = yp / w;
201
202 /* return values to caller */
203 *in_x = xi;
204 *in_y = yi;
205
206 GST_DEBUG_OBJECT (perspective, "Inversely mapped %d %d into %lf %lf",
207 x, y, *in_x, *in_y);
208
209 return TRUE;
210 }
211
212 static void
gst_perspective_class_init(GstPerspectiveClass * klass)213 gst_perspective_class_init (GstPerspectiveClass * klass)
214 {
215 GObjectClass *gobject_class;
216 GstElementClass *gstelement_class;
217 GstGeometricTransformClass *gstgt_class;
218
219 gobject_class = (GObjectClass *) klass;
220 gstelement_class = (GstElementClass *) klass;
221 gstgt_class = (GstGeometricTransformClass *) klass;
222
223 gst_element_class_set_static_metadata (gstelement_class,
224 "perspective",
225 "Transform/Effect/Video",
226 "Apply a 2D perspective transform",
227 "Antonio Ospite <ospite@studenti.unina.it>");
228
229 gobject_class->set_property = gst_perspective_set_property;
230 gobject_class->get_property = gst_perspective_get_property;
231
232 g_object_class_install_property (gobject_class, PROP_MATRIX,
233 g_param_spec_value_array ("matrix",
234 "Matrix",
235 "Matrix of dimension 3x3 to use in the 2D transform, passed as an array of 9 elements in row-major order",
236 g_param_spec_double ("Element",
237 "Transformation matrix element",
238 "Element of the transformation matrix",
239 -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
240 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS),
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242
243 gstgt_class->map_func = perspective_map;
244 }
245
246 static void
gst_perspective_init(GstPerspective * filter)247 gst_perspective_init (GstPerspective * filter)
248 {
249 /* set the Identity matrix as default */
250 filter->matrix[0] = 1;
251 filter->matrix[1] = 0;
252 filter->matrix[2] = 0;
253
254 filter->matrix[3] = 0;
255 filter->matrix[4] = 1;
256 filter->matrix[5] = 0;
257
258 filter->matrix[6] = 0;
259 filter->matrix[7] = 0;
260 filter->matrix[8] = 1;
261 }
262