1 /*
2 * GStreamer
3 * Copyright (C) 2010 Filippo Argiolas <filippo.argiolas@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Alternatively, the contents of this file may be used under the
24 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
25 * which case the following provisions apply instead of the ones
26 * mentioned above:
27 *
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Library General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
32 *
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Library General Public License for more details.
37 *
38 * You should have received a copy of the GNU Library General Public
39 * License along with this library; if not, write to the
40 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
41 * Boston, MA 02110-1301, USA.
42 */
43
44 /**
45 * SECTION:element-square
46 * @title: square
47 * @see_also: geometrictransform
48 *
49 * The square element distorts the center part of the image into a square.
50 *
51 * ## Example launch line
52 * |[
53 * gst-launch-1.0 -v videotestsrc ! square zoom=100 ! videoconvert ! autovideosink
54 * ]|
55 *
56 */
57
58 #ifdef HAVE_CONFIG_H
59 # include <config.h>
60 #endif
61
62 #include <gst/gst.h>
63 #include <math.h>
64
65 #include "gstsquare.h"
66
67 GST_DEBUG_CATEGORY_STATIC (gst_square_debug);
68 #define GST_CAT_DEFAULT gst_square_debug
69
70 enum
71 {
72 PROP_0,
73 PROP_WIDTH,
74 PROP_HEIGHT,
75 PROP_ZOOM
76 };
77
78 #define DEFAULT_WIDTH 0.5
79 #define DEFAULT_HEIGHT 0.5
80 #define DEFAULT_ZOOM 2.0
81
82 #define gst_square_parent_class parent_class
83 G_DEFINE_TYPE (GstSquare, gst_square, GST_TYPE_GEOMETRIC_TRANSFORM);
84
85 /* GObject vmethod implementations */
86
87 static void
gst_square_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)88 gst_square_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec)
90 {
91 GstSquare *square;
92 GstGeometricTransform *gt;
93 gdouble v;
94
95 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
96 square = GST_SQUARE_CAST (gt);
97
98 GST_OBJECT_LOCK (square);
99 switch (prop_id) {
100 case PROP_WIDTH:
101 v = g_value_get_double (value);
102 if (v != square->width) {
103 square->width = v;
104 gst_geometric_transform_set_need_remap (gt);
105 }
106 break;
107 case PROP_HEIGHT:
108 v = g_value_get_double (value);
109 if (v != square->height) {
110 square->height = v;
111 gst_geometric_transform_set_need_remap (gt);
112 }
113 break;
114 case PROP_ZOOM:
115 v = g_value_get_double (value);
116 if (v != square->zoom) {
117 square->zoom = v;
118 gst_geometric_transform_set_need_remap (gt);
119 }
120 break;
121 default:
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123 break;
124 }
125 GST_OBJECT_UNLOCK (square);
126 }
127
128 static void
gst_square_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)129 gst_square_get_property (GObject * object, guint prop_id,
130 GValue * value, GParamSpec * pspec)
131 {
132 GstSquare *square;
133 GstGeometricTransform *gt;
134
135 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
136 square = GST_SQUARE_CAST (gt);
137
138 switch (prop_id) {
139 case PROP_WIDTH:
140 g_value_set_double (value, square->width);
141 break;
142 case PROP_HEIGHT:
143 g_value_set_double (value, square->height);
144 break;
145 case PROP_ZOOM:
146 g_value_set_double (value, square->zoom);
147 break;
148 default:
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 break;
151 }
152 }
153
154 static gboolean
square_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)155 square_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
156 gdouble * in_y)
157 {
158 GstSquare *square = GST_SQUARE_CAST (gt);
159 gdouble norm_x;
160 gdouble norm_y;
161
162 /* frame size */
163 gdouble width = gt->width;
164 gdouble height = gt->height;
165
166 /* normalize in ((-1.0, -1.0), (1.0, 1.0) */
167 norm_x = 2.0 * x / width - 1.0;
168 norm_y = 2.0 * y / height - 1.0;
169
170 /* transform */
171 /* zoom at the center, smoothstep around half quadrant and get back to normal */
172 norm_x *=
173 (1.0 / square->zoom) * (1.0 + (square->zoom -
174 1.0) * gst_gm_smoothstep (square->width - 0.125,
175 square->width + 0.125, ABS (norm_x)));
176 norm_y *=
177 (1.0 / square->zoom) * (1.0 + (square->zoom -
178 1.0) * gst_gm_smoothstep (square->height - 0.125,
179 square->height + 0.125, ABS (norm_y)));
180
181 /* unnormalize */
182 *in_x = 0.5 * (norm_x + 1.0) * width;
183 *in_y = 0.5 * (norm_y + 1.0) * height;
184
185 GST_DEBUG_OBJECT (square, "Inversely mapped %d %d into %lf %lf",
186 x, y, *in_x, *in_y);
187
188 return TRUE;
189 }
190
191 static void
gst_square_class_init(GstSquareClass * klass)192 gst_square_class_init (GstSquareClass * klass)
193 {
194 GObjectClass *gobject_class;
195 GstElementClass *gstelement_class;
196 GstGeometricTransformClass *gstgt_class;
197
198 gobject_class = (GObjectClass *) klass;
199 gstelement_class = (GstElementClass *) klass;
200 gstgt_class = (GstGeometricTransformClass *) klass;
201
202 gst_element_class_set_static_metadata (gstelement_class,
203 "square",
204 "Transform/Effect/Video",
205 "Distort center part of the image into a square",
206 "Filippo Argiolas <filippo.argiolas@gmail.com>");
207
208 gobject_class->set_property = gst_square_set_property;
209 gobject_class->get_property = gst_square_get_property;
210
211 g_object_class_install_property (gobject_class, PROP_WIDTH,
212 g_param_spec_double ("width", "Width",
213 "Width of the square, relative to the frame width",
214 0.0, 1.0, DEFAULT_WIDTH,
215 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_HEIGHT,
217 g_param_spec_double ("height", "Height",
218 "Height of the square, relative to the frame height",
219 0.0, 1.0, DEFAULT_HEIGHT,
220 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_ZOOM,
222 g_param_spec_double ("zoom", "Zoom",
223 "Zoom amount in the center region",
224 1.0, 100.0, DEFAULT_ZOOM,
225 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226
227 gstgt_class->map_func = square_map;
228 }
229
230 static void
gst_square_init(GstSquare * filter)231 gst_square_init (GstSquare * filter)
232 {
233 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
234
235 filter->width = DEFAULT_WIDTH;
236 filter->height = DEFAULT_HEIGHT;
237 filter->zoom = DEFAULT_ZOOM;
238 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
239 }
240
241 gboolean
gst_square_plugin_init(GstPlugin * plugin)242 gst_square_plugin_init (GstPlugin * plugin)
243 {
244 GST_DEBUG_CATEGORY_INIT (gst_square_debug, "square", 0, "square");
245
246 return gst_element_register (plugin, "square", GST_RANK_NONE,
247 GST_TYPE_SQUARE);
248 }
249