• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (square, "square", GST_RANK_NONE,
85     GST_TYPE_SQUARE, GST_DEBUG_CATEGORY_INIT (gst_square_debug, "square", 0,
86         "square"));
87 
88 /* GObject vmethod implementations */
89 
90 static void
gst_square_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)91 gst_square_set_property (GObject * object, guint prop_id,
92     const GValue * value, GParamSpec * pspec)
93 {
94   GstSquare *square;
95   GstGeometricTransform *gt;
96   gdouble v;
97 
98   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
99   square = GST_SQUARE_CAST (gt);
100 
101   GST_OBJECT_LOCK (square);
102   switch (prop_id) {
103     case PROP_WIDTH:
104       v = g_value_get_double (value);
105       if (v != square->width) {
106         square->width = v;
107         gst_geometric_transform_set_need_remap (gt);
108       }
109       break;
110     case PROP_HEIGHT:
111       v = g_value_get_double (value);
112       if (v != square->height) {
113         square->height = v;
114         gst_geometric_transform_set_need_remap (gt);
115       }
116       break;
117     case PROP_ZOOM:
118       v = g_value_get_double (value);
119       if (v != square->zoom) {
120         square->zoom = v;
121         gst_geometric_transform_set_need_remap (gt);
122       }
123       break;
124     default:
125       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126       break;
127   }
128   GST_OBJECT_UNLOCK (square);
129 }
130 
131 static void
gst_square_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)132 gst_square_get_property (GObject * object, guint prop_id,
133     GValue * value, GParamSpec * pspec)
134 {
135   GstSquare *square;
136   GstGeometricTransform *gt;
137 
138   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
139   square = GST_SQUARE_CAST (gt);
140 
141   switch (prop_id) {
142     case PROP_WIDTH:
143       g_value_set_double (value, square->width);
144       break;
145     case PROP_HEIGHT:
146       g_value_set_double (value, square->height);
147       break;
148     case PROP_ZOOM:
149       g_value_set_double (value, square->zoom);
150       break;
151     default:
152       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153       break;
154   }
155 }
156 
157 static gboolean
square_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)158 square_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
159     gdouble * in_y)
160 {
161   GstSquare *square = GST_SQUARE_CAST (gt);
162   gdouble norm_x;
163   gdouble norm_y;
164 
165   /* frame size */
166   gdouble width = gt->width;
167   gdouble height = gt->height;
168 
169   /* normalize in ((-1.0, -1.0), (1.0, 1.0) */
170   norm_x = 2.0 * x / width - 1.0;
171   norm_y = 2.0 * y / height - 1.0;
172 
173   /* transform */
174   /* zoom at the center, smoothstep around half quadrant and get back to normal */
175   norm_x *=
176       (1.0 / square->zoom) * (1.0 + (square->zoom -
177           1.0) * gst_gm_smoothstep (square->width - 0.125,
178           square->width + 0.125, ABS (norm_x)));
179   norm_y *=
180       (1.0 / square->zoom) * (1.0 + (square->zoom -
181           1.0) * gst_gm_smoothstep (square->height - 0.125,
182           square->height + 0.125, ABS (norm_y)));
183 
184   /* unnormalize */
185   *in_x = 0.5 * (norm_x + 1.0) * width;
186   *in_y = 0.5 * (norm_y + 1.0) * height;
187 
188   GST_DEBUG_OBJECT (square, "Inversely mapped %d %d into %lf %lf",
189       x, y, *in_x, *in_y);
190 
191   return TRUE;
192 }
193 
194 static void
gst_square_class_init(GstSquareClass * klass)195 gst_square_class_init (GstSquareClass * klass)
196 {
197   GObjectClass *gobject_class;
198   GstElementClass *gstelement_class;
199   GstGeometricTransformClass *gstgt_class;
200 
201   gobject_class = (GObjectClass *) klass;
202   gstelement_class = (GstElementClass *) klass;
203   gstgt_class = (GstGeometricTransformClass *) klass;
204 
205   gst_element_class_set_static_metadata (gstelement_class,
206       "square",
207       "Transform/Effect/Video",
208       "Distort center part of the image into a square",
209       "Filippo Argiolas <filippo.argiolas@gmail.com>");
210 
211   gobject_class->set_property = gst_square_set_property;
212   gobject_class->get_property = gst_square_get_property;
213 
214   g_object_class_install_property (gobject_class, PROP_WIDTH,
215       g_param_spec_double ("width", "Width",
216           "Width of the square, relative to the frame width",
217           0.0, 1.0, DEFAULT_WIDTH,
218           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219   g_object_class_install_property (gobject_class, PROP_HEIGHT,
220       g_param_spec_double ("height", "Height",
221           "Height of the square, relative to the frame height",
222           0.0, 1.0, DEFAULT_HEIGHT,
223           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224   g_object_class_install_property (gobject_class, PROP_ZOOM,
225       g_param_spec_double ("zoom", "Zoom",
226           "Zoom amount in the center region",
227           1.0, 100.0, DEFAULT_ZOOM,
228           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 
230   gstgt_class->map_func = square_map;
231 }
232 
233 static void
gst_square_init(GstSquare * filter)234 gst_square_init (GstSquare * filter)
235 {
236   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
237 
238   filter->width = DEFAULT_WIDTH;
239   filter->height = DEFAULT_HEIGHT;
240   filter->zoom = DEFAULT_ZOOM;
241   gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
242 }
243