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