Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / effectv / gstvertigo.c
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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, 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 -v videotestsrc ! vertigotv ! ffmpegcolorspace ! 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 #include <gst/video/video.h>
47 #include <gst/controller/gstcontroller.h>
48
49 GST_BOILERPLATE (GstVertigoTV, gst_vertigotv, GstVideoFilter,
50     GST_TYPE_VIDEO_FILTER);
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_RGBx ";" GST_VIDEO_CAPS_BGRx
62 #else
63 #define CAPS_STR GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_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
81 gst_vertigotv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
82     GstCaps * outcaps)
83 {
84   GstVertigoTV *filter = GST_VERTIGOTV (btrans);
85   GstStructure *structure;
86   gboolean ret = FALSE;
87
88   structure = gst_caps_get_structure (incaps, 0);
89
90   GST_OBJECT_LOCK (filter);
91   if (gst_structure_get_int (structure, "width", &filter->width) &&
92       gst_structure_get_int (structure, "height", &filter->height)) {
93     gint area = filter->width * filter->height;
94
95     g_free (filter->buffer);
96     filter->buffer = (guint32 *) g_malloc0 (area * 2 * sizeof (guint32));
97
98     filter->current_buffer = filter->buffer;
99     filter->alt_buffer = filter->buffer + area;
100     filter->phase = 0;
101
102     ret = TRUE;
103   }
104   GST_OBJECT_UNLOCK (filter);
105
106   return ret;
107 }
108
109 static void
110 gst_vertigotv_set_parms (GstVertigoTV * filter)
111 {
112   double vx, vy;
113   double t;
114   double x, y;
115   double dizz;
116
117   dizz = sin (filter->phase) * 10 + sin (filter->phase * 1.9 + 5) * 5;
118
119   x = filter->width / 2;
120   y = filter->height / 2;
121
122   t = (x * x + y * y) * filter->zoomrate;
123
124   if (filter->width > filter->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
158 gst_vertigotv_transform (GstBaseTransform * trans, GstBuffer * in,
159     GstBuffer * out)
160 {
161   GstVertigoTV *filter = GST_VERTIGOTV (trans);
162   guint32 *src, *dest, *p;
163   guint32 v;
164   gint x, y, ox, oy, i, width, height, area;
165   GstFlowReturn ret = GST_FLOW_OK;
166   GstClockTime timestamp, stream_time;
167
168   timestamp = GST_BUFFER_TIMESTAMP (in);
169   stream_time =
170       gst_segment_to_stream_time (&trans->segment, 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 (G_OBJECT (filter), stream_time);
177
178   src = (guint32 *) GST_BUFFER_DATA (in);
179   dest = (guint32 *) GST_BUFFER_DATA (out);
180
181   GST_OBJECT_LOCK (filter);
182
183   width = filter->width;
184   height = filter->height;
185   area = width * height;
186
187   gst_vertigotv_set_parms (filter);
188   p = filter->alt_buffer;
189
190   for (y = height; y > 0; y--) {
191     ox = filter->sx;
192     oy = filter->sy;
193
194     for (x = width; x > 0; x--) {
195       i = (oy >> 16) * width + (ox >> 16);
196       if (i < 0)
197         i = 0;
198       if (i >= area)
199         i = area;
200
201       v = filter->current_buffer[i] & 0xfcfcff;
202       v = (v * 3) + ((*src++) & 0xfcfcff);
203
204       *p++ = (v >> 2);
205       ox += filter->dx;
206       oy += filter->dy;
207     }
208     filter->sx -= filter->dy;
209     filter->sy += filter->dx;
210   }
211
212   memcpy (dest, filter->alt_buffer, area * sizeof (guint32));
213
214   p = filter->current_buffer;
215   filter->current_buffer = filter->alt_buffer;
216   filter->alt_buffer = p;
217   GST_OBJECT_UNLOCK (filter);
218
219   return ret;
220 }
221
222 static gboolean
223 gst_vertigotv_start (GstBaseTransform * trans)
224 {
225   GstVertigoTV *filter = GST_VERTIGOTV (trans);
226
227   filter->phase = 0.0;
228
229   return TRUE;
230 }
231
232 static void
233 gst_vertigotv_set_property (GObject * object, guint prop_id,
234     const GValue * value, GParamSpec * pspec)
235 {
236   GstVertigoTV *filter = GST_VERTIGOTV (object);
237
238   GST_OBJECT_LOCK (filter);
239   switch (prop_id) {
240     case PROP_SPEED:
241       filter->phase_increment = g_value_get_float (value);
242       break;
243     case PROP_ZOOM_SPEED:
244       filter->zoomrate = g_value_get_float (value);
245       break;
246     default:
247       break;
248   }
249   GST_OBJECT_UNLOCK (filter);
250 }
251
252 static void
253 gst_vertigotv_get_property (GObject * object, guint prop_id, GValue * value,
254     GParamSpec * pspec)
255 {
256   GstVertigoTV *filter = GST_VERTIGOTV (object);
257
258   switch (prop_id) {
259     case PROP_SPEED:
260       g_value_set_float (value, filter->phase_increment);
261       break;
262     case PROP_ZOOM_SPEED:
263       g_value_set_float (value, filter->zoomrate);
264       break;
265     default:
266       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267       break;
268   }
269 }
270
271 static void
272 gst_vertigotv_finalize (GObject * object)
273 {
274   GstVertigoTV *filter = GST_VERTIGOTV (object);
275
276   g_free (filter->buffer);
277   filter->buffer = NULL;
278
279   G_OBJECT_CLASS (parent_class)->finalize (object);
280 }
281
282 static void
283 gst_vertigotv_base_init (gpointer g_class)
284 {
285   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
286
287   gst_element_class_set_details_simple (element_class, "VertigoTV effect",
288       "Filter/Effect/Video",
289       "A loopback alpha blending effector with rotating and scaling",
290       "Wim Taymans <wim.taymans@chello.be>");
291
292   gst_element_class_add_static_pad_template (element_class,
293       &gst_vertigotv_sink_template);
294   gst_element_class_add_static_pad_template (element_class,
295       &gst_vertigotv_src_template);
296 }
297
298 static void
299 gst_vertigotv_class_init (GstVertigoTVClass * klass)
300 {
301   GObjectClass *gobject_class = (GObjectClass *) klass;
302   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
303
304   gobject_class->set_property = gst_vertigotv_set_property;
305   gobject_class->get_property = gst_vertigotv_get_property;
306   gobject_class->finalize = gst_vertigotv_finalize;
307
308   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SPEED,
309       g_param_spec_float ("speed", "Speed", "Control the speed of movement",
310           0.01, 100.0, 0.02, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ZOOM_SPEED,
312       g_param_spec_float ("zoom-speed", "Zoom Speed",
313           "Control the rate of zooming", 1.01, 1.1, 1.01,
314           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
315
316   trans_class->start = GST_DEBUG_FUNCPTR (gst_vertigotv_start);
317   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_vertigotv_set_caps);
318   trans_class->transform = GST_DEBUG_FUNCPTR (gst_vertigotv_transform);
319 }
320
321 static void
322 gst_vertigotv_init (GstVertigoTV * filter, GstVertigoTVClass * klass)
323 {
324   filter->buffer = NULL;
325   filter->phase = 0.0;
326   filter->phase_increment = 0.02;
327   filter->zoomrate = 1.01;
328 }