VideoFilter inherits from
[platform/upstream/gstreamer.git] / gst / effectv / gstwarp.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  *
5  * EffecTV - Realtime Digital Video Effector
6  * Copyright (C) 2001 FUKUCHI Kentarou
7  *
8  * This library is free software; 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  * This file was (probably) generated from gstvideotemplate.c,
26  * gstvideotemplate.c,v 1.11 2004/01/07 08:56:45 ds Exp 
27  */
28
29 /* From main.c of warp-1.1:
30  *
31  *      Simple DirectMedia Layer demo
32  *      Realtime picture 'gooing'
33  *      by sam lantinga slouken@devolution.com
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <gstvideofilter.h>
41
42 #include <string.h>
43 #include <math.h>
44
45 #include <gst/video/video.h>
46
47 #ifndef M_PI
48 #define M_PI    3.14159265358979323846
49 #endif
50
51 #define GST_TYPE_WARPTV \
52   (gst_warptv_get_type())
53 #define GST_WARPTV(obj) \
54   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_WARPTV,GstWarpTV))
55 #define GST_WARPTV_CLASS(klass) \
56   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_WARPTV,GstWarpTVClass))
57 #define GST_IS_WARPTV(obj) \
58   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WARPTV))
59 #define GST_IS_WARPTV_CLASS(obj) \
60   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WARPTV))
61
62 typedef struct _GstWarpTV GstWarpTV;
63 typedef struct _GstWarpTVClass GstWarpTVClass;
64
65 struct _GstWarpTV
66 {
67   GstVideofilter videofilter;
68
69   gint width, height;
70   gint *offstable;
71   gint32 *disttable;
72   gint32 ctable[1024];
73   gint32 sintable[1024 + 256];
74   gint tval;
75 };
76
77 struct _GstWarpTVClass
78 {
79   GstVideofilterClass parent_class;
80 };
81
82 GType gst_warptv_get_type (void);
83
84 static void initSinTable (GstWarpTV * filter);
85 static void initOffsTable (GstWarpTV * filter);
86 static void initDistTable (GstWarpTV * filter);
87
88 static GstElementDetails warptv_details = GST_ELEMENT_DETAILS ("WarpTV",
89     "Filter/Effect/Video",
90     "WarpTV does realtime goo'ing of the video input",
91     "Sam Lantinga <slouken@devolution.com>");
92
93 static GstStaticPadTemplate gst_warptv_src_template =
94     GST_STATIC_PAD_TEMPLATE ("src",
95     GST_PAD_SRC,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
98         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
99     );
100
101 static GstStaticPadTemplate gst_warptv_sink_template =
102     GST_STATIC_PAD_TEMPLATE ("sink",
103     GST_PAD_SINK,
104     GST_PAD_ALWAYS,
105     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
106         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
107     );
108
109 static GstVideofilterClass *parent_class = NULL;
110
111 static gboolean
112 gst_warptv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
113     GstCaps * outcaps)
114 {
115   GstWarpTV *filter = GST_WARPTV (btrans);
116   GstStructure *structure;
117   gboolean ret = FALSE;
118
119   structure = gst_caps_get_structure (incaps, 0);
120
121   if (gst_structure_get_int (structure, "width", &filter->width) &&
122       gst_structure_get_int (structure, "height", &filter->height)) {
123     g_free (filter->disttable);
124     g_free (filter->offstable);
125
126     filter->offstable = g_malloc (filter->height * sizeof (guint32));
127     filter->disttable =
128         g_malloc (filter->width * filter->height * sizeof (guint32));
129
130     initSinTable (filter);
131     initOffsTable (filter);
132     initDistTable (filter);
133     ret = TRUE;
134   }
135
136   return ret;
137 }
138
139 static gboolean
140 gst_warptv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
141     guint * size)
142 {
143   GstWarpTV *filter;
144   GstStructure *structure;
145   gboolean ret = FALSE;
146   gint width, height;
147
148   filter = GST_WARPTV (btrans);
149
150   structure = gst_caps_get_structure (caps, 0);
151
152   if (gst_structure_get_int (structure, "width", &width) &&
153       gst_structure_get_int (structure, "height", &height)) {
154     *size = width * height * 32 / 8;
155     ret = TRUE;
156     GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
157         width, height);
158   }
159
160   return ret;
161 }
162
163 static void
164 initSinTable (GstWarpTV * filter)
165 {
166   gint32 *tptr, *tsinptr;
167   double i;
168
169   tsinptr = tptr = filter->sintable;
170
171   for (i = 0; i < 1024; i++)
172     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
173
174   for (i = 0; i < 256; i++)
175     *tptr++ = *tsinptr++;
176 }
177
178 static void
179 initOffsTable (GstWarpTV * filter)
180 {
181   int y;
182
183   for (y = 0; y < filter->height; y++) {
184     filter->offstable[y] = y * filter->width;
185   }
186 }
187
188 static void
189 initDistTable (GstWarpTV * filter)
190 {
191   gint32 halfw, halfh, *distptr;
192
193 #ifdef PS2
194   float x, y, m;
195 #else
196   double x, y, m;
197 #endif
198
199   halfw = filter->width >> 1;
200   halfh = filter->height >> 1;
201
202   distptr = filter->disttable;
203
204   m = sqrt ((double) (halfw * halfw + halfh * halfh));
205
206   for (y = -halfh; y < halfh; y++)
207     for (x = -halfw; x < halfw; x++)
208 #ifdef PS2
209       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
210 #else
211       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
212 #endif
213 }
214
215 static GstFlowReturn
216 gst_warptv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
217 {
218   GstWarpTV *warptv = GST_WARPTV (trans);
219   int width = warptv->width;
220   int height = warptv->height;
221   guint32 *src = (guint32 *) GST_BUFFER_DATA (in);
222   guint32 *dest = (guint32 *) GST_BUFFER_DATA (out);
223   gint xw, yw, cw;
224   gint32 c, i, x, y, dx, dy, maxx, maxy;
225   gint32 skip, *ctptr, *distptr;
226   gint32 *sintable, *ctable;
227   GstFlowReturn ret = GST_FLOW_OK;
228
229   gst_buffer_stamp (out, in);
230
231   xw = (gint) (sin ((warptv->tval + 100) * M_PI / 128) * 30);
232   yw = (gint) (sin ((warptv->tval) * M_PI / 256) * -35);
233   cw = (gint) (sin ((warptv->tval - 70) * M_PI / 64) * 50);
234   xw += (gint) (sin ((warptv->tval - 10) * M_PI / 512) * 40);
235   yw += (gint) (sin ((warptv->tval + 30) * M_PI / 512) * 40);
236
237   ctptr = warptv->ctable;
238   distptr = warptv->disttable;
239   sintable = warptv->sintable;
240   ctable = warptv->ctable;
241
242   skip = 0;                     /* video_width*sizeof(RGB32)/4 - video_width;; */
243   c = 0;
244
245   for (x = 0; x < 512; x++) {
246     i = (c >> 3) & 0x3FE;
247     *ctptr++ = ((sintable[i] * yw) >> 15);
248     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
249     c += cw;
250   }
251   maxx = width - 2;
252   maxy = height - 2;
253
254   for (y = 0; y < height - 1; y++) {
255     for (x = 0; x < width; x++) {
256       i = *distptr++;
257       dx = ctable[i + 1] + x;
258       dy = ctable[i] + y;
259
260       if (dx < 0)
261         dx = 0;
262       else if (dx > maxx)
263         dx = maxx;
264
265       if (dy < 0)
266         dy = 0;
267       else if (dy > maxy)
268         dy = maxy;
269       *dest++ = src[warptv->offstable[dy] + dx];
270     }
271     dest += skip;
272   }
273
274   warptv->tval = (warptv->tval + 1) & 511;
275
276   return ret;
277 }
278
279 static void
280 gst_warptv_base_init (gpointer g_class)
281 {
282   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
283
284   gst_element_class_set_details (element_class, &warptv_details);
285
286   gst_element_class_add_pad_template (element_class,
287       gst_static_pad_template_get (&gst_warptv_sink_template));
288   gst_element_class_add_pad_template (element_class,
289       gst_static_pad_template_get (&gst_warptv_src_template));
290 }
291
292 static void
293 gst_warptv_class_init (gpointer klass, gpointer class_data)
294 {
295   GObjectClass *gobject_class;
296   GstElementClass *element_class;
297   GstBaseTransformClass *trans_class;
298
299   gobject_class = (GObjectClass *) klass;
300   element_class = (GstElementClass *) klass;
301   trans_class = (GstBaseTransformClass *) klass;
302
303   parent_class = g_type_class_peek_parent (klass);
304
305   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_warptv_set_caps);
306   trans_class->get_unit_size = GST_DEBUG_FUNCPTR (gst_warptv_get_unit_size);
307   trans_class->transform = GST_DEBUG_FUNCPTR (gst_warptv_transform);
308 }
309
310 static void
311 gst_warptv_init (GTypeInstance * instance, gpointer g_class)
312 {
313 }
314
315 GType
316 gst_warptv_get_type (void)
317 {
318   static GType warptv_type = 0;
319
320   if (!warptv_type) {
321     static const GTypeInfo warptv_info = {
322       sizeof (GstWarpTVClass),
323       gst_warptv_base_init,
324       NULL,
325       gst_warptv_class_init,
326       NULL,
327       NULL,
328       sizeof (GstWarpTV),
329       0,
330       gst_warptv_init,
331     };
332
333     warptv_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
334         "GstWarpTV", &warptv_info, 0);
335   }
336   return warptv_type;
337 }