• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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 /**
37  * SECTION:element-warptv
38  * @title: warptv
39  *
40  * WarpTV does realtime goo'ing of the video input.
41  *
42  * ## Example launch line
43  * |[
44  * gst-launch-1.0 -v videotestsrc ! warptv ! videoconvert ! autovideosink
45  * ]| This pipeline shows the effect of warptv on a test stream.
46  *
47  */
48 
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 
53 #include "gsteffectv.h"
54 #include <string.h>
55 #include <math.h>
56 
57 #include "gstwarp.h"
58 #include <gst/video/gstvideometa.h>
59 #include <gst/video/gstvideopool.h>
60 
61 #ifndef M_PI
62 #define M_PI    3.14159265358979323846
63 #endif
64 
65 #define gst_warptv_parent_class parent_class
66 G_DEFINE_TYPE (GstWarpTV, gst_warptv, GST_TYPE_VIDEO_FILTER);
67 GST_ELEMENT_REGISTER_DEFINE (warptv, "warptv", GST_RANK_NONE, GST_TYPE_WARPTV);
68 
69 static void initSinTable ();
70 static void initDistTable (GstWarpTV * filter, gint width, gint height);
71 
72 static GstStaticPadTemplate gst_warptv_src_template =
73 GST_STATIC_PAD_TEMPLATE ("src",
74     GST_PAD_SRC,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
77     );
78 
79 static GstStaticPadTemplate gst_warptv_sink_template =
80 GST_STATIC_PAD_TEMPLATE ("sink",
81     GST_PAD_SINK,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
84     );
85 
86 static gboolean
gst_warptv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)87 gst_warptv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
88     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
89 {
90   GstWarpTV *filter = GST_WARPTV (vfilter);
91   gint width, height;
92 
93   width = GST_VIDEO_INFO_WIDTH (in_info);
94   height = GST_VIDEO_INFO_HEIGHT (in_info);
95 
96   g_free (filter->disttable);
97   filter->disttable = g_malloc (width * height * sizeof (guint32));
98   initDistTable (filter, width, height);
99 
100   return TRUE;
101 }
102 
103 static gint32 sintable[1024 + 256];
104 
105 static void
initSinTable(void)106 initSinTable (void)
107 {
108   gint32 *tptr, *tsinptr;
109   gint i;
110 
111   tsinptr = tptr = sintable;
112 
113   for (i = 0; i < 1024; i++)
114     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
115 
116   for (i = 0; i < 256; i++)
117     *tptr++ = *tsinptr++;
118 }
119 
120 static void
initDistTable(GstWarpTV * filter,gint width,gint height)121 initDistTable (GstWarpTV * filter, gint width, gint height)
122 {
123   gint32 halfw, halfh, *distptr;
124   gint x, y;
125   float m;
126 
127   halfw = width >> 1;
128   halfh = height >> 1;
129 
130   distptr = filter->disttable;
131 
132   m = sqrt ((double) (halfw * halfw + halfh * halfh));
133 
134   for (y = -halfh; y < halfh; y++)
135     for (x = -halfw; x < halfw; x++)
136 #ifdef PS2
137       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
138 #else
139       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
140 #endif
141 }
142 
143 static GstFlowReturn
gst_warptv_transform_frame(GstVideoFilter * filter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)144 gst_warptv_transform_frame (GstVideoFilter * filter, GstVideoFrame * in_frame,
145     GstVideoFrame * out_frame)
146 {
147   GstWarpTV *warptv = GST_WARPTV (filter);
148   gint width, height;
149   gint xw, yw, cw;
150   gint32 c, i, x, y, dx, dy, maxx, maxy;
151   gint32 *ctptr, *distptr;
152   gint32 *ctable;
153   guint32 *src, *dest;
154   gint sstride, dstride;
155 
156   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
157   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
158 
159   sstride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
160   dstride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
161 
162   width = GST_VIDEO_FRAME_WIDTH (in_frame);
163   height = GST_VIDEO_FRAME_HEIGHT (in_frame);
164 
165   GST_OBJECT_LOCK (warptv);
166   xw = (gint) (sin ((warptv->tval + 100) * M_PI / 128) * 30);
167   yw = (gint) (sin ((warptv->tval) * M_PI / 256) * -35);
168   cw = (gint) (sin ((warptv->tval - 70) * M_PI / 64) * 50);
169   xw += (gint) (sin ((warptv->tval - 10) * M_PI / 512) * 40);
170   yw += (gint) (sin ((warptv->tval + 30) * M_PI / 512) * 40);
171 
172   ctptr = warptv->ctable;
173   distptr = warptv->disttable;
174   ctable = warptv->ctable;
175 
176   c = 0;
177 
178   for (x = 0; x < 512; x++) {
179     i = (c >> 3) & 0x3FE;
180     *ctptr++ = ((sintable[i] * yw) >> 15);
181     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
182     c += cw;
183   }
184   maxx = width - 2;
185   maxy = height - 2;
186 
187   for (y = 0; y < height - 1; y++) {
188     for (x = 0; x < width; x++) {
189       i = *distptr++;
190       dx = ctable[i + 1] + x;
191       dy = ctable[i] + y;
192 
193       if (dx < 0)
194         dx = 0;
195       else if (dx > maxx)
196         dx = maxx;
197 
198       if (dy < 0)
199         dy = 0;
200       else if (dy > maxy)
201         dy = maxy;
202 
203       dest[x] = src[dy * sstride / 4 + dx];
204     }
205     dest += dstride / 4;
206   }
207 
208   warptv->tval = (warptv->tval + 1) & 511;
209   GST_OBJECT_UNLOCK (warptv);
210 
211   return GST_FLOW_OK;
212 }
213 
214 static gboolean
gst_warptv_start(GstBaseTransform * trans)215 gst_warptv_start (GstBaseTransform * trans)
216 {
217   GstWarpTV *warptv = GST_WARPTV (trans);
218 
219   warptv->tval = 0;
220 
221   return TRUE;
222 }
223 
224 static void
gst_warptv_finalize(GObject * object)225 gst_warptv_finalize (GObject * object)
226 {
227   GstWarpTV *warptv = GST_WARPTV (object);
228 
229   g_free (warptv->disttable);
230   warptv->disttable = NULL;
231 
232   G_OBJECT_CLASS (parent_class)->finalize (object);
233 }
234 
235 static void
gst_warptv_class_init(GstWarpTVClass * klass)236 gst_warptv_class_init (GstWarpTVClass * klass)
237 {
238   GObjectClass *gobject_class = (GObjectClass *) klass;
239   GstElementClass *gstelement_class = (GstElementClass *) klass;
240   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
241   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
242 
243   gobject_class->finalize = gst_warptv_finalize;
244 
245   gst_element_class_set_static_metadata (gstelement_class, "WarpTV effect",
246       "Filter/Effect/Video",
247       "WarpTV does realtime goo'ing of the video input",
248       "Sam Lantinga <slouken@devolution.com>");
249 
250   gst_element_class_add_static_pad_template (gstelement_class,
251       &gst_warptv_sink_template);
252   gst_element_class_add_static_pad_template (gstelement_class,
253       &gst_warptv_src_template);
254 
255   trans_class->start = GST_DEBUG_FUNCPTR (gst_warptv_start);
256 
257   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_warptv_set_info);
258   vfilter_class->transform_frame =
259       GST_DEBUG_FUNCPTR (gst_warptv_transform_frame);
260 
261   initSinTable ();
262 }
263 
264 static void
gst_warptv_init(GstWarpTV * warptv)265 gst_warptv_init (GstWarpTV * warptv)
266 {
267   /* nothing to do */
268 }
269