2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * EffecTV - Realtime Digital Video Effector
5 * Copyright (C) 2001 FUKUCHI Kentarou
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.
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.
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.
23 * From main.c of warp-1.1:
25 * Simple DirectMedia Layer demo
26 * Realtime picture 'gooing'
27 * by sam lantinga slouken@devolution.com
36 #include "gsteffectv.h"
39 #define M_PI 3.14159265358979323846
42 #define GST_TYPE_WARPTV \
43 (gst_warptv_get_type())
44 #define GST_WARPTV(obj) \
45 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_WARPTV,GstWarpTV))
46 #define GST_WARPTV_CLASS(klass) \
47 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstWarpTV))
48 #define GST_IS_WARPTV(obj) \
49 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WARPTV))
50 #define GST_IS_WARPTV_CLASS(obj) \
51 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WARPTV))
53 typedef struct _GstWarpTV GstWarpTV;
54 typedef struct _GstWarpTVClass GstWarpTVClass;
60 GstPad *sinkpad, *srcpad;
66 gint32 sintable[1024+256];
70 struct _GstWarpTVClass
72 GstElementClass parent_class;
75 /* elementfactory information */
76 static GstElementDetails gst_warptv_details = GST_ELEMENT_DETAILS (
78 "Filter/Effect/Video",
79 "WarpTV does realtime goo'ing of the video input",
80 "Sam Lantinga <slouken@devolution.com>"
84 /* Filter signals and args */
96 static void gst_warptv_base_init (gpointer g_class);
97 static void gst_warptv_class_init (GstWarpTVClass * klass);
98 static void gst_warptv_init (GstWarpTV * filter);
100 static void gst_warptv_initialize (GstWarpTV *filter);
102 static void gst_warptv_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_warptv_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static void gst_warptv_chain (GstPad * pad, GstData *_data);
109 static GstElementClass *parent_class = NULL;
110 /*static guint gst_warptv_signals[LAST_SIGNAL] = { 0 }; */
112 GType gst_warptv_get_type (void)
114 static GType warptv_type = 0;
117 static const GTypeInfo warptv_info = {
118 sizeof (GstWarpTVClass),
119 gst_warptv_base_init,
121 (GClassInitFunc) gst_warptv_class_init,
126 (GInstanceInitFunc) gst_warptv_init,
129 warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
135 gst_warptv_base_init (gpointer g_class)
137 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
139 gst_element_class_add_pad_template (element_class,
140 gst_static_pad_template_get (&gst_effectv_src_template));
141 gst_element_class_add_pad_template (element_class,
142 gst_static_pad_template_get (&gst_effectv_sink_template));
144 gst_element_class_set_details (element_class, &gst_warptv_details);
148 gst_warptv_class_init (GstWarpTVClass * klass)
150 GObjectClass *gobject_class;
151 GstElementClass *gstelement_class;
153 gobject_class = (GObjectClass *) klass;
154 gstelement_class = (GstElementClass *) klass;
156 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
158 gobject_class->set_property = gst_warptv_set_property;
159 gobject_class->get_property = gst_warptv_get_property;
162 static GstPadLinkReturn
163 gst_warptv_sinkconnect (GstPad * pad, const GstCaps * caps)
166 GstStructure *structure;
168 filter = GST_WARPTV (gst_pad_get_parent (pad));
169 structure = gst_caps_get_structure (caps, 0);
171 gst_structure_get_int (structure, "width", &filter->width);
172 gst_structure_get_int (structure, "height", &filter->height);
174 gst_warptv_initialize (filter);
176 return gst_pad_try_set_caps (filter->srcpad, caps);
180 gst_warptv_init (GstWarpTV * filter)
182 filter->sinkpad = gst_pad_new_from_template (
183 gst_static_pad_template_get (&gst_effectv_sink_template), "sink");
184 gst_pad_set_chain_function (filter->sinkpad, gst_warptv_chain);
185 gst_pad_set_link_function (filter->sinkpad, gst_warptv_sinkconnect);
186 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
188 filter->srcpad = gst_pad_new_from_template (
189 gst_static_pad_template_get (&gst_effectv_src_template), "src");
190 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
193 filter->disttable = NULL;
194 filter->offstable = NULL;
199 initSinTable (GstWarpTV *filter)
201 gint32 *tptr, *tsinptr;
204 tsinptr = tptr = filter->sintable;
206 for (i = 0; i < 1024; i++)
207 *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
209 for (i = 0; i < 256; i++)
210 *tptr++ = *tsinptr++;
214 initOffsTable (GstWarpTV *filter)
218 for (y = 0; y < filter->height; y++) {
219 filter->offstable[y] = y * filter->width;
224 initDistTable (GstWarpTV *filter)
226 gint32 halfw, halfh, *distptr;
233 halfw = filter->width>> 1;
234 halfh = filter->height >> 1;
236 distptr = filter->disttable;
238 m = sqrt ((double)(halfw * halfw + halfh * halfh));
240 for (y = -halfh; y < halfh; y++)
241 for (x= -halfw; x < halfw; x++)
243 *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
245 *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
250 gst_warptv_initialize (GstWarpTV *filter)
252 g_free (filter->disttable);
253 g_free (filter->offstable);
255 filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));
256 filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
258 initSinTable (filter);
259 initOffsTable (filter);
260 initDistTable (filter);
264 gst_warptv_chain (GstPad * pad, GstData *_data)
266 GstBuffer *buf = GST_BUFFER (_data);
271 gint32 c,i, x,y, dx,dy, maxx, maxy;
272 gint32 width, height, skip, *ctptr, *distptr;
273 gint32 *sintable, *ctable;
275 filter = GST_WARPTV (gst_pad_get_parent (pad));
277 src = (guint32 *) GST_BUFFER_DATA (buf);
279 outbuf = gst_buffer_new ();
280 GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
281 dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
282 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
284 xw = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
285 yw = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
286 cw = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
287 xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
288 yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);
290 ctptr = filter->ctable;
291 distptr = filter->disttable;
292 width = filter->width;
293 height = filter->height;
294 sintable = filter->sintable;
295 ctable = filter->ctable;
297 skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
300 for (x = 0; x < 512; x++) {
301 i = (c >> 3) & 0x3FE;
302 *ctptr++ = ((sintable[i] * yw) >> 15);
303 *ctptr++ = ((sintable[i + 256] * xw) >> 15);
306 maxx = width - 2; maxy = height - 2;
308 for (y = 0; y < height - 1; y++) {
309 for (x = 0; x < width; x++) {
311 dx = ctable [i + 1] + x;
315 else if (dx > maxx) dx = maxx;
318 else if (dy > maxy) dy = maxy;
319 *dest++ = src[filter->offstable[dy] + dx];
324 filter->tval = (filter->tval + 1) & 511;
326 gst_buffer_unref (buf);
328 gst_pad_push (filter->srcpad, GST_DATA (outbuf));
332 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
336 /* it's not null if we got it, but it might not be ours */
337 g_return_if_fail (GST_IS_WARPTV (object));
339 filter = GST_WARPTV (object);
348 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
352 /* it's not null if we got it, but it might not be ours */
353 g_return_if_fail (GST_IS_WARPTV (object));
355 filter = GST_WARPTV (object);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);