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