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