effectv: Remove get_unit_size implementations
[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 <string.h>
41 #include <math.h>
42
43 #include <gst/gst.h>
44
45 #include <gst/video/video.h>
46 #include <gst/video/gstvideofilter.h>
47
48 #ifndef M_PI
49 #define M_PI    3.14159265358979323846
50 #endif
51
52 #define GST_TYPE_WARPTV \
53   (gst_warptv_get_type())
54 #define GST_WARPTV(obj) \
55   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_WARPTV,GstWarpTV))
56 #define GST_WARPTV_CLASS(klass) \
57   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_WARPTV,GstWarpTVClass))
58 #define GST_IS_WARPTV(obj) \
59   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WARPTV))
60 #define GST_IS_WARPTV_CLASS(klass) \
61   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WARPTV))
62
63 typedef struct _GstWarpTV GstWarpTV;
64 typedef struct _GstWarpTVClass GstWarpTVClass;
65
66 struct _GstWarpTV
67 {
68   GstVideoFilter videofilter;
69
70   gint width, height;
71   gint *offstable;
72   gint32 *disttable;
73   gint32 ctable[1024];
74   gint32 sintable[1024 + 256];
75   gint tval;
76 };
77
78 struct _GstWarpTVClass
79 {
80   GstVideoFilterClass parent_class;
81 };
82
83 GST_BOILERPLATE (GstWarpTV, gst_warptv, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
84
85 static void initSinTable (GstWarpTV * filter);
86 static void initOffsTable (GstWarpTV * filter);
87 static void initDistTable (GstWarpTV * filter);
88
89 static GstStaticPadTemplate gst_warptv_src_template =
90     GST_STATIC_PAD_TEMPLATE ("src",
91     GST_PAD_SRC,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
94         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
95     );
96
97 static GstStaticPadTemplate gst_warptv_sink_template =
98     GST_STATIC_PAD_TEMPLATE ("sink",
99     GST_PAD_SINK,
100     GST_PAD_ALWAYS,
101     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
102         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
103     );
104
105 static gboolean
106 gst_warptv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
107     GstCaps * outcaps)
108 {
109   GstWarpTV *filter = GST_WARPTV (btrans);
110   GstStructure *structure;
111   gboolean ret = FALSE;
112
113   structure = gst_caps_get_structure (incaps, 0);
114
115   if (gst_structure_get_int (structure, "width", &filter->width) &&
116       gst_structure_get_int (structure, "height", &filter->height)) {
117     g_free (filter->disttable);
118     g_free (filter->offstable);
119
120     filter->offstable = g_malloc (filter->height * sizeof (guint32));
121     filter->disttable =
122         g_malloc (filter->width * filter->height * sizeof (guint32));
123
124     initSinTable (filter);
125     initOffsTable (filter);
126     initDistTable (filter);
127     ret = TRUE;
128   }
129
130   return ret;
131 }
132
133 static void
134 initSinTable (GstWarpTV * filter)
135 {
136   gint32 *tptr, *tsinptr;
137   double i;
138
139   tsinptr = tptr = filter->sintable;
140
141   for (i = 0; i < 1024; i++)
142     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
143
144   for (i = 0; i < 256; i++)
145     *tptr++ = *tsinptr++;
146 }
147
148 static void
149 initOffsTable (GstWarpTV * filter)
150 {
151   int y;
152
153   for (y = 0; y < filter->height; y++) {
154     filter->offstable[y] = y * filter->width;
155   }
156 }
157
158 static void
159 initDistTable (GstWarpTV * filter)
160 {
161   gint32 halfw, halfh, *distptr;
162
163 #ifdef PS2
164   float x, y, m;
165 #else
166   double x, y, m;
167 #endif
168
169   halfw = filter->width >> 1;
170   halfh = filter->height >> 1;
171
172   distptr = filter->disttable;
173
174   m = sqrt ((double) (halfw * halfw + halfh * halfh));
175
176   for (y = -halfh; y < halfh; y++)
177     for (x = -halfw; x < halfw; x++)
178 #ifdef PS2
179       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
180 #else
181       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
182 #endif
183 }
184
185 static GstFlowReturn
186 gst_warptv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
187 {
188   GstWarpTV *warptv = GST_WARPTV (trans);
189   int width = warptv->width;
190   int height = warptv->height;
191   guint32 *src = (guint32 *) GST_BUFFER_DATA (in);
192   guint32 *dest = (guint32 *) GST_BUFFER_DATA (out);
193   gint xw, yw, cw;
194   gint32 c, i, x, y, dx, dy, maxx, maxy;
195   gint32 skip, *ctptr, *distptr;
196   gint32 *sintable, *ctable;
197   GstFlowReturn ret = GST_FLOW_OK;
198
199   xw = (gint) (sin ((warptv->tval + 100) * M_PI / 128) * 30);
200   yw = (gint) (sin ((warptv->tval) * M_PI / 256) * -35);
201   cw = (gint) (sin ((warptv->tval - 70) * M_PI / 64) * 50);
202   xw += (gint) (sin ((warptv->tval - 10) * M_PI / 512) * 40);
203   yw += (gint) (sin ((warptv->tval + 30) * M_PI / 512) * 40);
204
205   ctptr = warptv->ctable;
206   distptr = warptv->disttable;
207   sintable = warptv->sintable;
208   ctable = warptv->ctable;
209
210   skip = 0;                     /* video_width*sizeof(RGB32)/4 - video_width;; */
211   c = 0;
212
213   for (x = 0; x < 512; x++) {
214     i = (c >> 3) & 0x3FE;
215     *ctptr++ = ((sintable[i] * yw) >> 15);
216     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
217     c += cw;
218   }
219   maxx = width - 2;
220   maxy = height - 2;
221
222   for (y = 0; y < height - 1; y++) {
223     for (x = 0; x < width; x++) {
224       i = *distptr++;
225       dx = ctable[i + 1] + x;
226       dy = ctable[i] + y;
227
228       if (dx < 0)
229         dx = 0;
230       else if (dx > maxx)
231         dx = maxx;
232
233       if (dy < 0)
234         dy = 0;
235       else if (dy > maxy)
236         dy = maxy;
237       *dest++ = src[warptv->offstable[dy] + dx];
238     }
239     dest += skip;
240   }
241
242   warptv->tval = (warptv->tval + 1) & 511;
243
244   return ret;
245 }
246
247 static gboolean
248 gst_warptv_start (GstBaseTransform * trans)
249 {
250   GstWarpTV *warptv = GST_WARPTV (trans);
251
252   warptv->tval = 0;
253
254   return TRUE;
255 }
256
257 static void
258 gst_warptv_finalize (GObject * object)
259 {
260   GstWarpTV *warptv = GST_WARPTV (object);
261
262   g_free (warptv->offstable);
263   warptv->offstable = NULL;
264   g_free (warptv->disttable);
265   warptv->disttable = NULL;
266 }
267
268 static void
269 gst_warptv_base_init (gpointer g_class)
270 {
271   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
272
273   gst_element_class_set_details_simple (element_class, "WarpTV effect",
274       "Filter/Effect/Video",
275       "WarpTV does realtime goo'ing of the video input",
276       "Sam Lantinga <slouken@devolution.com>");
277
278   gst_element_class_add_pad_template (element_class,
279       gst_static_pad_template_get (&gst_warptv_sink_template));
280   gst_element_class_add_pad_template (element_class,
281       gst_static_pad_template_get (&gst_warptv_src_template));
282 }
283
284 static void
285 gst_warptv_class_init (GstWarpTVClass * klass)
286 {
287   GObjectClass *gobject_class = (GObjectClass *) klass;
288   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
289
290   gobject_class->finalize = gst_warptv_finalize;
291
292   trans_class->start = GST_DEBUG_FUNCPTR (gst_warptv_start);
293   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_warptv_set_caps);
294   trans_class->transform = GST_DEBUG_FUNCPTR (gst_warptv_transform);
295 }
296
297 static void
298 gst_warptv_init (GstWarpTV * warptv, GstWarpTVClass * klass)
299 {
300 }