1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * EffecTV:
5 * Copyright (C) 2001 FUKUCHI Kentarou
6 *
7 * EffecTV is free software. This library is free software;
8 * you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 /**
25 * SECTION:element-vertigotv
26 * @title: vertigotv
27 *
28 * VertigoTV is a loopback alpha blending effector with rotating and scaling.
29 *
30 * ## Example launch line
31 * |[
32 * gst-launch-1.0 -v videotestsrc ! vertigotv ! videoconvert ! autovideosink
33 * ]| This pipeline shows the effect of vertigotv on a test stream.
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <math.h>
42 #include <string.h>
43
44 #include "gsteffectv.h"
45 #include "gstvertigo.h"
46
47 #define gst_vertigotv_parent_class parent_class
48 G_DEFINE_TYPE (GstVertigoTV, gst_vertigotv, GST_TYPE_VIDEO_FILTER);
49 GST_ELEMENT_REGISTER_DEFINE (vertigotv, "vertigotv", GST_RANK_NONE,
50 GST_TYPE_VERTIGOTV);
51
52 /* Filter signals and args */
53 enum
54 {
55 PROP_0,
56 PROP_SPEED,
57 PROP_ZOOM_SPEED
58 };
59
60 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
61 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ RGBx, BGRx }")
62 #else
63 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xRGB, xBGR }")
64 #endif
65
66 static GstStaticPadTemplate gst_vertigotv_src_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
68 GST_PAD_SRC,
69 GST_PAD_ALWAYS,
70 GST_STATIC_CAPS (CAPS_STR)
71 );
72
73 static GstStaticPadTemplate gst_vertigotv_sink_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
75 GST_PAD_SINK,
76 GST_PAD_ALWAYS,
77 GST_STATIC_CAPS (CAPS_STR)
78 );
79
80 static gboolean
gst_vertigotv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)81 gst_vertigotv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
82 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
83 {
84 GstVertigoTV *filter = GST_VERTIGOTV (vfilter);
85 gint area, width, height;
86
87 width = GST_VIDEO_INFO_WIDTH (in_info);
88 height = GST_VIDEO_INFO_HEIGHT (in_info);
89
90 area = width * height;
91
92 g_free (filter->buffer);
93 filter->buffer = (guint32 *) g_malloc0 (area * 2 * sizeof (guint32));
94
95 filter->current_buffer = filter->buffer;
96 filter->alt_buffer = filter->buffer + area;
97 filter->phase = 0;
98
99 return TRUE;
100 }
101
102 static void
gst_vertigotv_set_parms(GstVertigoTV * filter)103 gst_vertigotv_set_parms (GstVertigoTV * filter)
104 {
105 double vx, vy;
106 double t;
107 double x, y;
108 double dizz;
109 gint width, height;
110 GstVideoInfo *info;
111
112 dizz = sin (filter->phase) * 10 + sin (filter->phase * 1.9 + 5) * 5;
113
114 info = &GST_VIDEO_FILTER (filter)->in_info;
115
116 width = GST_VIDEO_INFO_WIDTH (info);
117 height = GST_VIDEO_INFO_HEIGHT (info);
118
119 x = width / 2;
120 y = height / 2;
121
122 t = (x * x + y * y) * filter->zoomrate;
123
124 if (width > height) {
125 if (dizz >= 0) {
126 if (dizz > x)
127 dizz = x;
128 vx = (x * (x - dizz) + y * y) / t;
129 } else {
130 if (dizz < -x)
131 dizz = -x;
132 vx = (x * (x + dizz) + y * y) / t;
133 }
134 vy = (dizz * y) / t;
135 } else {
136 if (dizz >= 0) {
137 if (dizz > y)
138 dizz = y;
139 vx = (x * x + y * (y - dizz)) / t;
140 } else {
141 if (dizz < -y)
142 dizz = -y;
143 vx = (x * x + y * (y + dizz)) / t;
144 }
145 vy = (dizz * x) / t;
146 }
147 filter->dx = vx * 65536;
148 filter->dy = vy * 65536;
149 filter->sx = (-vx * x + vy * y + x + cos (filter->phase * 5) * 2) * 65536;
150 filter->sy = (-vx * y - vy * x + y + sin (filter->phase * 6) * 2) * 65536;
151
152 filter->phase += filter->phase_increment;
153 if (filter->phase > 5700000)
154 filter->phase = 0;
155 }
156
157 static GstFlowReturn
gst_vertigotv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)158 gst_vertigotv_transform_frame (GstVideoFilter * vfilter,
159 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
160 {
161 GstVertigoTV *filter = GST_VERTIGOTV (vfilter);
162 guint32 *src, *dest, *p;
163 guint32 v;
164 gint x, y, ox, oy, i, width, height, area, sstride, dstride;
165 GstClockTime timestamp, stream_time;
166
167 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
168 stream_time =
169 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
170 GST_FORMAT_TIME, timestamp);
171
172 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
173 GST_TIME_ARGS (timestamp));
174
175 if (GST_CLOCK_TIME_IS_VALID (stream_time))
176 gst_object_sync_values (GST_OBJECT (filter), stream_time);
177
178 src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
179 sstride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
180 dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
181 dstride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
182
183 width = GST_VIDEO_FRAME_WIDTH (in_frame);
184 height = GST_VIDEO_FRAME_HEIGHT (in_frame);
185
186 area = width * height;
187
188 sstride /= 4;
189 dstride /= 4;
190
191 gst_vertigotv_set_parms (filter);
192 p = filter->alt_buffer;
193
194 for (y = 0; y < height; y++) {
195 ox = filter->sx;
196 oy = filter->sy;
197
198 for (x = 0; x < width; x++) {
199 i = (oy >> 16) * width + (ox >> 16);
200 if (i < 0)
201 i = 0;
202 if (i >= area)
203 i = area;
204
205 v = filter->current_buffer[i] & 0xfcfcff;
206 v = (v * 3) + (src[x] & 0xfcfcff);
207
208 *p++ = dest[x] = (v >> 2);
209 ox += filter->dx;
210 oy += filter->dy;
211 }
212 filter->sx -= filter->dy;
213 filter->sy += filter->dx;
214
215 src += sstride;
216 dest += dstride;
217 }
218
219 p = filter->current_buffer;
220 filter->current_buffer = filter->alt_buffer;
221 filter->alt_buffer = p;
222
223 return GST_FLOW_OK;
224 }
225
226 static gboolean
gst_vertigotv_start(GstBaseTransform * trans)227 gst_vertigotv_start (GstBaseTransform * trans)
228 {
229 GstVertigoTV *filter = GST_VERTIGOTV (trans);
230
231 filter->phase = 0.0;
232
233 return TRUE;
234 }
235
236 static void
gst_vertigotv_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)237 gst_vertigotv_set_property (GObject * object, guint prop_id,
238 const GValue * value, GParamSpec * pspec)
239 {
240 GstVertigoTV *filter = GST_VERTIGOTV (object);
241
242 GST_OBJECT_LOCK (filter);
243 switch (prop_id) {
244 case PROP_SPEED:
245 filter->phase_increment = g_value_get_float (value);
246 break;
247 case PROP_ZOOM_SPEED:
248 filter->zoomrate = g_value_get_float (value);
249 break;
250 default:
251 break;
252 }
253 GST_OBJECT_UNLOCK (filter);
254 }
255
256 static void
gst_vertigotv_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)257 gst_vertigotv_get_property (GObject * object, guint prop_id, GValue * value,
258 GParamSpec * pspec)
259 {
260 GstVertigoTV *filter = GST_VERTIGOTV (object);
261
262 switch (prop_id) {
263 case PROP_SPEED:
264 g_value_set_float (value, filter->phase_increment);
265 break;
266 case PROP_ZOOM_SPEED:
267 g_value_set_float (value, filter->zoomrate);
268 break;
269 default:
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271 break;
272 }
273 }
274
275 static void
gst_vertigotv_finalize(GObject * object)276 gst_vertigotv_finalize (GObject * object)
277 {
278 GstVertigoTV *filter = GST_VERTIGOTV (object);
279
280 g_free (filter->buffer);
281 filter->buffer = NULL;
282
283 G_OBJECT_CLASS (parent_class)->finalize (object);
284 }
285
286 static void
gst_vertigotv_class_init(GstVertigoTVClass * klass)287 gst_vertigotv_class_init (GstVertigoTVClass * klass)
288 {
289 GObjectClass *gobject_class = (GObjectClass *) klass;
290 GstElementClass *gstelement_class = (GstElementClass *) klass;
291 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
292 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
293
294 gobject_class->set_property = gst_vertigotv_set_property;
295 gobject_class->get_property = gst_vertigotv_get_property;
296 gobject_class->finalize = gst_vertigotv_finalize;
297
298 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SPEED,
299 g_param_spec_float ("speed", "Speed", "Control the speed of movement",
300 0.01, 100.0, 0.02, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ZOOM_SPEED,
302 g_param_spec_float ("zoom-speed", "Zoom Speed",
303 "Control the rate of zooming", 1.01, 1.1, 1.01,
304 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305
306 gst_element_class_set_static_metadata (gstelement_class, "VertigoTV effect",
307 "Filter/Effect/Video",
308 "A loopback alpha blending effector with rotating and scaling",
309 "Wim Taymans <wim.taymans@gmail.be>");
310
311 gst_element_class_add_static_pad_template (gstelement_class,
312 &gst_vertigotv_sink_template);
313 gst_element_class_add_static_pad_template (gstelement_class,
314 &gst_vertigotv_src_template);
315
316 trans_class->start = GST_DEBUG_FUNCPTR (gst_vertigotv_start);
317
318 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_vertigotv_set_info);
319 vfilter_class->transform_frame =
320 GST_DEBUG_FUNCPTR (gst_vertigotv_transform_frame);
321 }
322
323 static void
gst_vertigotv_init(GstVertigoTV * filter)324 gst_vertigotv_init (GstVertigoTV * filter)
325 {
326 filter->buffer = NULL;
327 filter->phase = 0.0;
328 filter->phase_increment = 0.02;
329 filter->zoomrate = 1.01;
330 }
331